Changeset 10536 in josm


Ignore:
Timestamp:
2016-07-16T21:01:47+02:00 (8 years ago)
Author:
wiktorn
Message:

Extract ButtonColumn to separate class

Make cache stats public so other may easily operate on cache contents.

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

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/preferences/imagery/CacheContentsPanel.java

    r10462 r10536  
    44import static org.openstreetmap.josm.tools.I18n.tr;
    55
    6 import java.awt.Color;
    7 import java.awt.Component;
    86import java.awt.GridBagLayout;
    97import java.awt.event.ActionEvent;
    10 import java.awt.event.ActionListener;
    118import java.util.ArrayList;
    129import java.util.Collections;
     
    2118
    2219import javax.swing.AbstractAction;
    23 import javax.swing.AbstractCellEditor;
    24 import javax.swing.Action;
    25 import javax.swing.Icon;
    26 import javax.swing.JButton;
    2720import javax.swing.JLabel;
    2821import javax.swing.JPanel;
    2922import javax.swing.JScrollPane;
    3023import javax.swing.JTable;
    31 import javax.swing.UIManager;
    32 import javax.swing.border.LineBorder;
    3324import javax.swing.table.DefaultTableModel;
    34 import javax.swing.table.TableCellEditor;
    35 import javax.swing.table.TableCellRenderer;
    3625import javax.swing.table.TableColumn;
    3726import javax.swing.table.TableModel;
     
    4736import org.openstreetmap.josm.gui.layer.WMTSLayer;
    4837import org.openstreetmap.josm.gui.util.GuiHelper;
     38import org.openstreetmap.josm.gui.widgets.ButtonColumn;
    4939import org.openstreetmap.josm.tools.GBC;
    5040import org.openstreetmap.josm.tools.Pair;
     
    5949public class CacheContentsPanel extends JPanel {
    6050
    61     /**
    62      *
    63      * Class based on:  http://www.camick.com/java/source/ButtonColumn.java
    64      * https://tips4java.wordpress.com/2009/07/12/table-button-column/
    65      *
    66      */
    67     private static final class ButtonColumn extends AbstractCellEditor implements TableCellRenderer, TableCellEditor, ActionListener {
    68         private final Action action;
    69         private final JButton renderButton;
    70         private final JButton editButton;
    71         private Object editorValue;
    72 
    73         private ButtonColumn() {
    74             this(null);
    75         }
    76 
    77         private ButtonColumn(Action action) {
    78             this.action = action;
    79             renderButton = new JButton();
    80             editButton = new JButton();
    81             editButton.setFocusPainted(false);
    82             editButton.addActionListener(this);
    83             editButton.setBorder(new LineBorder(Color.BLUE));
    84         }
    85 
    86         @Override
    87         public Object getCellEditorValue() {
    88             return editorValue;
    89         }
    90 
    91         @Override
    92         public void actionPerformed(ActionEvent e) {
    93             this.action.actionPerformed(e);
    94         }
    95 
    96         @Override
    97         public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
    98             this.editorValue = value;
    99             if (value == null) {
    100                 editButton.setText("");
    101                 editButton.setIcon(null);
    102             } else if (value instanceof Icon) {
    103                 editButton.setText("");
    104                 editButton.setIcon((Icon) value);
    105             } else {
    106                 editButton.setText(value.toString());
    107                 editButton.setIcon(null);
    108             }
    109             this.editorValue = value;
    110             return editButton;
    111         }
    112 
    113         @Override
    114         public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
    115                 boolean hasFocus, int row, int column) {
    116             if (isSelected) {
    117                 renderButton.setForeground(table.getSelectionForeground());
    118                 renderButton.setBackground(table.getSelectionBackground());
    119             } else {
    120                 renderButton.setForeground(table.getForeground());
    121                 renderButton.setBackground(UIManager.getColor("Button.background"));
    122             }
    123 
    124             renderButton.setFocusPainted(hasFocus);
    125 
    126             if (value == null) {
    127                 renderButton.setText("");
    128                 renderButton.setIcon(null);
    129             } else if (value instanceof Icon) {
    130                 renderButton.setText("");
    131                 renderButton.setIcon((Icon) value);
    132             } else {
    133                 renderButton.setText(value.toString());
    134                 renderButton.setIcon(null);
    135             }
    136             return renderButton;
    137         }
    138 
    139     }
    14051
    14152    private final transient ExecutorService executor =
     
    192103    }
    193104
    194     private static String[][] getCacheStats(CacheAccess<String, BufferedImageCacheEntry> cache) {
     105    public static String[][] getCacheStats(CacheAccess<String, BufferedImageCacheEntry> cache) {
    195106        Set<String> keySet = cache.getCacheControl().getKeySet();
    196107        Map<String, int[]> temp = new ConcurrentHashMap<>(); // use int[] as a Object reference to int, gives better performance
Note: See TracChangeset for help on using the changeset viewer.