Changeset 10072 in josm


Ignore:
Timestamp:
2016-03-28T18:27:56+02:00 (9 years ago)
Author:
Don-vip
Message:

refactor duplicated code

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/dialogs/relation/MemberTableMemberCellRenderer.java

    r9867 r10072  
    1515 */
    1616public class MemberTableMemberCellRenderer extends MemberTableCellRenderer {
    17 
    18     /**
    19      * Constructs a new {@code MemberTableMemberCellRenderer}.
    20      */
    21     public MemberTableMemberCellRenderer() {
    22         super();
    23     }
    2417
    2518    protected void renderPrimitive(OsmPrimitive primitive, Dimension cellSize) {
  • trunk/src/org/openstreetmap/josm/gui/dialogs/relation/SelectionTableCellRenderer.java

    r9867 r10072  
    33
    44import java.awt.Color;
    5 import java.awt.Component;
    6 import java.awt.Dimension;
    75
    8 import javax.swing.JLabel;
    96import javax.swing.JTable;
    107import javax.swing.UIManager;
     
    129
    1310import org.openstreetmap.josm.data.osm.OsmPrimitive;
    14 import org.openstreetmap.josm.gui.DefaultNameFormatter;
    1511import org.openstreetmap.josm.gui.util.GuiHelper;
    16 import org.openstreetmap.josm.tools.ImageProvider;
    1712
    1813/**
     
    2015 *
    2116 */
    22 public class SelectionTableCellRenderer extends JLabel implements TableCellRenderer {
    23     public static final Color BGCOLOR_DOUBLE_ENTRY = new Color(254, 226, 214);
    24     public static final Color BGCOLOR_SINGLE_ENTRY = new Color(235, 255, 177);
     17public class SelectionTableCellRenderer extends MemberTableMemberCellRenderer {
     18    public static final Color BGCOLOR_SINGLE_ENTRY = BGCOLOR_IN_JOSM_SELECTION;
    2519
    2620    /**
    2721     * reference to the member table model; required, in order to check whether a
    28      * selected primitive is already used in the member list of the currently edited
    29      * relation
     22     * selected primitive is already used in the member list of the currently edited relation
    3023     */
    31     private MemberTableModel model;
     24    private final MemberTableModel model;
    3225
    3326    /**
    3427     * constructor
     28     * @param model member table model
    3529     */
    36     public SelectionTableCellRenderer() {
    37         setIcon(null);
    38         setOpaque(true);
     30    public SelectionTableCellRenderer(MemberTableModel model) {
     31        this.model = model;
    3932    }
    4033
    41     /**
    42      * reset the renderer
    43      */
    44     protected void reset() {
    45         setBackground(UIManager.getColor("Table.background"));
    46         setForeground(UIManager.getColor("Table.foreground"));
    47         setBorder(null);
    48         setIcon(null);
    49         setToolTipText(null);
    50     }
    51 
    52     protected void renderBackground(OsmPrimitive primitive) {
     34    @Override
     35    protected void renderBackgroundForeground(MemberTableModel model, OsmPrimitive primitive, boolean isSelected) {
    5336        Color bgc = UIManager.getColor("Table.background");
    5437        if (primitive != null && model != null && model.getNumMembersWithPrimitive(primitive) == 1) {
     
    6043    }
    6144
    62     protected void renderPrimitive(OsmPrimitive primitive, Dimension cellSize) {
    63         int size = Math.min(cellSize.width, cellSize.height);
    64         setIcon(ImageProvider.getPadded(primitive, new Dimension(size, size)));
    65         setText(primitive.getDisplayName(DefaultNameFormatter.getInstance()));
    66         setToolTipText(DefaultNameFormatter.getInstance().buildDefaultToolTip(primitive));
    67     }
    68 
    6945    @Override
    70     public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus,
    71             int row, int column) {
    72 
    73         reset();
    74         if (value == null)
    75             return this;
    76 
    77         Dimension cellSize = table.getCellRect(row, column, false).getSize();
    78 
    79         renderBackground((OsmPrimitive) value);
    80         renderPrimitive((OsmPrimitive) value, cellSize);
    81         return this;
    82     }
    83 
    84     public void setMemberTableModel(MemberTableModel model) {
    85         this.model = model;
     46    protected MemberTableModel getModel(JTable table) {
     47        return model;
    8648    }
    8749}
  • trunk/src/org/openstreetmap/josm/gui/dialogs/relation/SelectionTableColumnModel.java

    r3083 r10072  
    1010    public SelectionTableColumnModel(MemberTableModel model) {
    1111        TableColumn col = null;
    12         SelectionTableCellRenderer renderer = new SelectionTableCellRenderer();
    13         renderer.setMemberTableModel(model);
     12        SelectionTableCellRenderer renderer = new SelectionTableCellRenderer(model);
    1413
    1514        // column 0 - the member role
  • trunk/test/unit/org/openstreetmap/josm/gui/dialogs/relation/SelectionTableCellRendererTest.java

    r9907 r10072  
    3030    public void testSelectionTableCellRenderer() {
    3131        MemberTableModel model = new MemberTableModel(null, null, null);
    32         SelectionTableCellRenderer r = new SelectionTableCellRenderer();
    33         r.setMemberTableModel(model);
     32        SelectionTableCellRenderer r = new SelectionTableCellRenderer(model);
    3433        assertEquals(r, r.getTableCellRendererComponent(null, null, false, false, 0, 0));
    3534        assertEquals(r, r.getTableCellRendererComponent(new JTable(model), new Node(), false, false, 0, 0));
Note: See TracChangeset for help on using the changeset viewer.