Changeset 6912 in josm for trunk


Ignore:
Timestamp:
2014-03-19T01:01:04+01:00 (6 years ago)
Author:
Don-vip
Message:

Refactor duplicated code

Location:
trunk/src/org/openstreetmap/josm/gui
Files:
5 edited
1 moved

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/conflict/pair/relation/RelationMemberTableCellEditor.java

    r6084 r6912  
    1313import org.openstreetmap.josm.gui.widgets.JosmTextField;
    1414
    15 
    1615/**
    1716 * {@link TableCellEditor} for the role column in a table for {@link RelationMember}s.
    18  *
     17 * @since 1631
    1918 */
    20 public class RelationMemberTableCellEditor extends AbstractCellEditor implements TableCellEditor{
     19public class RelationMemberTableCellEditor extends AbstractCellEditor implements TableCellEditor {
    2120
    2221    private final JosmTextField editor;
     
    5453        return editor.getText();
    5554    }
    56 
    5755}
  • trunk/src/org/openstreetmap/josm/gui/conflict/tags/RelationMemberConflictDecisionEditor.java

    r6084 r6912  
    99import javax.swing.table.TableCellEditor;
    1010
    11 import org.openstreetmap.josm.gui.util.TableCellEditorSupport;
     11import org.openstreetmap.josm.gui.util.CellEditorSupport;
    1212import org.openstreetmap.josm.gui.widgets.JosmComboBox;
    1313
    1414public class RelationMemberConflictDecisionEditor extends JosmComboBox implements TableCellEditor {
    1515
     16    /**
     17     * Constructs a new {@code RelationMemberConflictDecisionEditor}.
     18     */
    1619    public RelationMemberConflictDecisionEditor() {
    1720        super(RelationMemberConflictDecisionType.values());
    1821        setOpaque(true);
    1922        setRenderer(new RelationMemberConflictDecisionRenderer());
    20         tableCellEditorSupport = new TableCellEditorSupport(this);
     23        tableCellEditorSupport = new CellEditorSupport(this);
    2124    }
     25
    2226    /* --------------------------------------------------------------------------------- */
    2327    /* TableCellEditor                                                                   */
     
    3034    }
    3135
    32     private TableCellEditorSupport tableCellEditorSupport;
     36    private final CellEditorSupport tableCellEditorSupport;
    3337    private RelationMemberConflictDecisionType originalValue;
    3438
  • trunk/src/org/openstreetmap/josm/gui/io/ActionFlagsTableCell.java

    r6104 r6912  
    1010import java.awt.event.ActionListener;
    1111import java.util.EventObject;
    12 import java.util.concurrent.CopyOnWriteArrayList;
    1312
    1413import javax.swing.AbstractAction;
     
    1817import javax.swing.JTable;
    1918import javax.swing.event.CellEditorListener;
    20 import javax.swing.event.ChangeEvent;
    2119import javax.swing.table.TableCellEditor;
    2220import javax.swing.table.TableCellRenderer;
    2321
     22import org.openstreetmap.josm.gui.util.CellEditorSupport;
    2423import org.openstreetmap.josm.tools.GBC;
    2524
     
    3029 *
    3130 * Intended usage is like this:
    32  * ActionFlagsTableCell aftc = new ActionFlagsTableCell();
    33  * col = new TableColumn(0);
    34  * col.setCellRenderer(aftc);
    35  * col.setCellEditor(aftc);
     31 * <code>
     32 * <br>ActionFlagsTableCell aftc = new ActionFlagsTableCell();
     33 * <br>col = new TableColumn(0);
     34 * <br>col.setCellRenderer(aftc);
     35 * <br>col.setCellEditor(aftc);
     36 * </code>
    3637 */
    3738class ActionFlagsTableCell extends JPanel implements TableCellRenderer, TableCellEditor {
    38     protected final JCheckBox[] checkBoxes = new JCheckBox[2];
    39     private CopyOnWriteArrayList<CellEditorListener> listeners;
     39    private final JCheckBox[] checkBoxes = new JCheckBox[2];
     40    private final CellEditorSupport cellEditorSupport = new CellEditorSupport(this);
    4041
    4142    private ActionListener al = new ActionListener() {
    4243        @Override
    4344        public void actionPerformed(ActionEvent e) {
    44             fireEditingStopped();
     45            cellEditorSupport.fireEditingStopped();
    4546        }
    4647    };
    4748
    4849    public ActionFlagsTableCell() {
    49         super();
    50         listeners = new CopyOnWriteArrayList<CellEditorListener>();
    51 
    5250        checkBoxes[0] = new JCheckBox(tr("Upload"));
    5351        checkBoxes[1] = new JCheckBox(tr("Save"));
     
    6361                public void actionPerformed(ActionEvent e) {
    6462                    b.setSelected(!b.isSelected());
    65                     fireEditingStopped();
     63                    cellEditorSupport.fireEditingStopped();
    6664                }
    6765            });
     
    9492    @Override
    9593    public void addCellEditorListener(CellEditorListener l) {
    96         if (l != null) {
    97             listeners.addIfAbsent(l);
    98         }
    99     }
    100 
    101     protected void fireEditingCanceled() {
    102         for (CellEditorListener l: listeners) {
    103             l.editingCanceled(new ChangeEvent(this));
    104         }
    105     }
    106 
    107     protected void fireEditingStopped() {
    108         for (CellEditorListener l: listeners) {
    109             l.editingStopped(new ChangeEvent(this));
    110         }
     94        cellEditorSupport.addCellEditorListener(l);
    11195    }
    11296
    11397    @Override
    11498    public void cancelCellEditing() {
    115         fireEditingCanceled();
     99        cellEditorSupport.fireEditingCanceled();
    116100    }
    117101
     
    131115    @Override
    132116    public void removeCellEditorListener(CellEditorListener l) {
    133         listeners.remove(l);
     117        cellEditorSupport.removeCellEditorListener(l);
    134118    }
    135119
     
    141125    @Override
    142126    public boolean stopCellEditing() {
    143         fireEditingStopped();
     127        cellEditorSupport.fireEditingStopped();
    144128        return true;
    145129    }
  • trunk/src/org/openstreetmap/josm/gui/io/LayerNameAndFilePathTableCell.java

    r6889 r6912  
    1414import java.io.File;
    1515import java.util.EventObject;
    16 import java.util.concurrent.CopyOnWriteArrayList;
    1716
    1817import javax.swing.AbstractAction;
     
    2322import javax.swing.JTable;
    2423import javax.swing.event.CellEditorListener;
    25 import javax.swing.event.ChangeEvent;
    2624import javax.swing.table.TableCellEditor;
    2725import javax.swing.table.TableCellRenderer;
    2826
    2927import org.openstreetmap.josm.actions.SaveActionBase;
     28import org.openstreetmap.josm.gui.util.CellEditorSupport;
     29import org.openstreetmap.josm.gui.widgets.JosmTextField;
    3030import org.openstreetmap.josm.tools.GBC;
    31 import org.openstreetmap.josm.gui.widgets.JosmTextField;
    3231
    3332class LayerNameAndFilePathTableCell extends JPanel implements TableCellRenderer, TableCellEditor {
     
    4342    private static final GBC defaultCellStyle = GBC.eol().fill(GBC.HORIZONTAL).insets(2, 0, 2, 0);
    4443
    45     private CopyOnWriteArrayList<CellEditorListener> listeners;
     44    private final CellEditorSupport cellEditorSupport = new CellEditorSupport(this);
    4645    private File value;
    4746
     
    7170        btnFileChooser.setPreferredSize(new Dimension(20, 19));
    7271        btnFileChooser.setOpaque(true);
    73 
    74         listeners = new CopyOnWriteArrayList<CellEditorListener>();
    7572    }
    7673
     
    9390    }
    9491
    95     /** renderer used while the file path is being edited **/
    96     @Override
    97     public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected,
    98             int row, int column) {
     92    @Override
     93    public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
    9994        removeAll();
    10095        SaveLayerInfo info = (SaveLayerInfo)value;
     
    182177    @Override
    183178    public void addCellEditorListener(CellEditorListener l) {
    184         if (l != null) {
    185             listeners.addIfAbsent(l);
    186         }
    187     }
    188 
    189     protected void fireEditingCanceled() {
    190         for (CellEditorListener l: listeners) {
    191             l.editingCanceled(new ChangeEvent(this));
    192         }
    193     }
    194 
    195     protected void fireEditingStopped() {
    196         for (CellEditorListener l: listeners) {
    197             l.editingStopped(new ChangeEvent(this));
    198         }
     179        cellEditorSupport.addCellEditorListener(l);
    199180    }
    200181
    201182    @Override
    202183    public void cancelCellEditing() {
    203         fireEditingCanceled();
     184        cellEditorSupport.fireEditingCanceled();
    204185    }
    205186
     
    216197    @Override
    217198    public void removeCellEditorListener(CellEditorListener l) {
    218         listeners.remove(l);
     199        cellEditorSupport.removeCellEditorListener(l);
    219200    }
    220201
     
    231212            value = new File(tfFilename.getText());
    232213        }
    233         fireEditingStopped();
     214        cellEditorSupport.fireEditingStopped();
    234215        return true;
    235216    }
  • trunk/src/org/openstreetmap/josm/gui/tagging/ac/AutoCompletingTextField.java

    r6890 r6912  
    2020
    2121import org.openstreetmap.josm.Main;
    22 import org.openstreetmap.josm.gui.util.TableCellEditorSupport;
     22import org.openstreetmap.josm.gui.util.CellEditorSupport;
    2323import org.openstreetmap.josm.gui.widgets.JosmTextField;
    2424
    25 
    2625/**
    27  * AutoCompletingTextField is an text field with autocompletion behaviour. It
     26 * AutoCompletingTextField is a text field with autocompletion behaviour. It
    2827 * can be used as table cell editor in {@link JTable}s.
    2928 *
     
    3130 * managed in a {@link AutoCompletionList}.
    3231 *
    33  *
     32 * @since 1762
    3433 */
    3534public class AutoCompletingTextField extends JosmTextField implements ComboBoxEditor, TableCellEditor {
     
    4241    class AutoCompletionDocument extends PlainDocument {
    4342
    44         /**
    45          * inserts a string at a specific position
    46          *
    47          */
    4843        @Override
    4944        public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {
     
    126121    protected AutoCompletionList autoCompletionList = null;
    127122
    128     /**
    129      * creates the default document model for this editor
    130      *
    131      */
    132123    @Override
    133124    protected Document createDefaultModel() {
     
    156147                }
    157148        );
    158         tableCellEditorSupport = new TableCellEditorSupport(this);
    159     }
    160 
    161     /**
    162      * constructor
     149        tableCellEditorSupport = new CellEditorSupport(this);
     150    }
     151
     152    /**
     153     * Constructs a new {@code AutoCompletingTextField}.
    163154     */
    164155    public AutoCompletingTextField() {
     
    166157    }
    167158
     159    /**
     160     * Constructs a new {@code AutoCompletingTextField}.
     161     * @param columns the number of columns to use to calculate the preferred width;
     162     * if columns is set to zero, the preferred width will be whatever naturally results from the component implementation
     163     */
    168164    public AutoCompletingTextField(int columns) {
    169165        super(columns);
     
    178174
    179175    /**
    180      *
     176     * Returns the auto completion list.
    181177     * @return the auto completion list; may be null, if no auto completion list is set
    182178     */
     
    186182
    187183    /**
    188      * sets the auto completion list
     184     * Sets the auto completion list.
    189185     * @param autoCompletionList the auto completion list; if null, auto completion is
    190186     *   disabled
     
    226222    /* ------------------------------------------------------------------------------------ */
    227223
    228     private TableCellEditorSupport tableCellEditorSupport;
     224    private CellEditorSupport tableCellEditorSupport;
    229225    private String originalValue;
    230226
     
    246242        tableCellEditorSupport.removeCellEditorListener(l);
    247243    }
     244
    248245    @Override
    249246    public void cancelCellEditing() {
    250247        restoreOriginalValue();
    251248        tableCellEditorSupport.fireEditingCanceled();
    252 
    253249    }
    254250
  • trunk/src/org/openstreetmap/josm/gui/util/CellEditorSupport.java

    r6909 r6912  
    66import java.util.List;
    77
     8import javax.swing.CellEditor;
    89import javax.swing.event.CellEditorListener;
    910import javax.swing.event.ChangeEvent;
    1011
    11 public class TableCellEditorSupport {
    12     private Object editor;
    13     private List<CellEditorListener> listeners;
     12/**
     13 * Utility class used to ease implementation of {@link CellEditor} interface,
     14 * or one of its sub-interfaces, for classes that cannot extend {@link javax.swing.AbstractCellEditor}.
     15 * @since 6912
     16 */
     17public class CellEditorSupport {
     18    private final CellEditor editor;
     19    private final List<CellEditorListener> listeners;
    1420
    15     public TableCellEditorSupport(Object editor) {
     21    /**
     22     * Constructs a new {@code CellEditorSupport}.
     23     * @param editor The cell editor backed by this
     24     */
     25    public CellEditorSupport(CellEditor editor) {
    1626        this.editor = editor;
    17         listeners = new LinkedList<CellEditorListener>();
     27        this.listeners = new LinkedList<CellEditorListener>();
    1828    }
    1929
     
    2434    }
    2535
    26     public void addCellEditorListener(CellEditorListener l) {
     36    /**
     37     * Worker for {@link CellEditor#addCellEditorListener(CellEditorListener)} method.
     38     * @param l the CellEditorListener
     39     */
     40    public final void addCellEditorListener(CellEditorListener l) {
    2741        synchronized (this) {
    2842            if (l != null && ! listeners.contains(l)) {
     
    3145        }
    3246    }
    33     public void removeCellEditorListener(CellEditorListener l) {
     47
     48    /**
     49     * Worker for {@link CellEditor#removeCellEditorListener(CellEditorListener)} method.
     50     * @param l the CellEditorListener
     51     */
     52    public final void removeCellEditorListener(CellEditorListener l) {
    3453        synchronized (this) {
    3554            if (l != null &&listeners.contains(l)) {
     
    3958    }
    4059
    41     public void fireEditingCanceled() {
     60    /**
     61     * Fires "editing canceled" event to listeners.
     62     */
     63    public final void fireEditingCanceled() {
    4264        for (CellEditorListener listener: getListeners()) {
    4365            listener.editingCanceled(new ChangeEvent(editor));
     
    4567    }
    4668
    47     public void fireEditingStopped() {
     69    /**
     70     * Fires "editing stopped" event to listeners.
     71     */
     72    public final void fireEditingStopped() {
    4873        for (CellEditorListener listener: getListeners()) {
    4974            listener.editingStopped(new ChangeEvent(editor));
Note: See TracChangeset for help on using the changeset viewer.