1 19 package org.netbeans.modules.refactoring.spi.impl; 20 21 import java.awt.Component ; 22 import java.util.Iterator ; 23 import javax.swing.DefaultListCellRenderer ; 24 import javax.swing.ImageIcon ; 25 import javax.swing.JCheckBox ; 26 import javax.swing.JComponent ; 27 import javax.swing.JLabel ; 28 import javax.swing.JList ; 29 import javax.swing.JTable ; 30 import javax.swing.UIManager ; 31 import javax.swing.border.Border ; 32 import javax.swing.border.EmptyBorder ; 33 import javax.swing.table.DefaultTableCellRenderer ; 34 import javax.swing.table.TableCellRenderer ; 35 import javax.swing.table.TableColumn ; 36 import org.openide.util.NbBundle; 37 38 39 44 public final class UIUtilities { 45 private UIUtilities() { 47 } 48 49 59 public static String getDisplayText(Object element) { 60 return "getDisplayText not implemented"; 83 } 84 85 90 public static String getIconBase(Object element) { 91 return ""; 92 } 102 103 108 public static String getColumnName(String name) { 109 return name == null || name.length() == 0 ? " " : name; } 111 112 119 public static void initColumnWidth(JTable table, int index, Object longValue, int padding) { 120 TableColumn column = table.getColumnModel().getColumn(index); 121 122 TableCellRenderer headerRenderer = column.getHeaderRenderer(); 124 if (headerRenderer == null) { 125 headerRenderer = table.getTableHeader().getDefaultRenderer(); 126 } 127 Component comp = headerRenderer.getTableCellRendererComponent( 128 null, column.getHeaderValue(), false, false, 0, 0); 129 int width = comp.getPreferredSize().width; 130 131 comp = table.getDefaultRenderer(table.getModel().getColumnClass(index)).getTableCellRendererComponent( 133 table, longValue, false, false, 0, index); 134 width = Math.max(width, comp.getPreferredSize().width) + 2 * padding; 135 136 column.setPreferredWidth(width); 138 if (longValue instanceof Boolean ) { 141 column.setMaxWidth(width); 142 } 143 } 144 145 148 public static class JavaElementTableCellRenderer extends DefaultTableCellRenderer { 149 public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { 150 super.getTableCellRendererComponent(table, extractText(value), isSelected, hasFocus, row, column); 151 String iconBase = extractIconBase(value); 152 setIcon(iconBase == null ? null : new ImageIcon (org.openide.util.Utilities.loadImage(iconBase + ".gif"))); return this; 154 } 155 156 161 protected String extractIconBase(Object value) { 162 return null; 166 } 168 169 173 protected String extractText(Object value) { 174 if (value==null) 175 return null; 176 if (value instanceof Object ) { 177 return getDisplayText((Object ) value); 178 } else { 179 return value.toString(); 180 } 181 } 182 } 183 184 187 public static class JavaElementListCellRenderer extends DefaultListCellRenderer { 188 public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { 189 super.getListCellRendererComponent(list, extractText(value), index, isSelected, cellHasFocus); 190 String iconBase = extractIconBase(value); 191 if (iconBase != null) { 192 setIcon(new ImageIcon (org.openide.util.Utilities.loadImage(iconBase + ".gif"))); } 194 return this; 195 } 196 197 202 protected String extractIconBase(Object value) { 203 if (value instanceof Object ) { 204 return getIconBase((Object ) value); 205 } else { 206 return null; 207 } 208 } 209 210 214 protected String extractText(Object value) { 215 if (value instanceof Object ) { 216 return getDisplayText((Object ) value); 217 } else { 218 return value.toString(); 219 } 220 } 221 } 222 223 229 public static class BooleanTableCellRenderer extends JCheckBox implements TableCellRenderer { 230 private static final Border noFocusBorder = new EmptyBorder (1, 1, 1, 1); 231 private final JLabel emptyLabel = new JLabel (); 232 233 public BooleanTableCellRenderer() { 234 super(); 235 setHorizontalAlignment(JLabel.CENTER); 236 setBorderPainted(true); 237 emptyLabel.setBorder(noFocusBorder); 238 emptyLabel.setOpaque(true); 239 } 240 241 public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) { 242 JComponent result; 243 if (value == null) { 244 result = emptyLabel; 245 } else { 246 setSelected(((Boolean )value).booleanValue()); 247 setEnabled(table.getModel().isCellEditable(row, column)); 248 result = this; 249 } 250 251 result.setForeground(isSelected ? table.getSelectionForeground() : table.getForeground()); 252 result.setBackground(isSelected ? table.getSelectionBackground() : table.getBackground()); 253 result.setBorder(hasFocus ? UIManager.getBorder("Table.focusCellHighlightBorder") : noFocusBorder); 255 return result; 256 } 257 } 258 } 259 | Popular Tags |