1 19 24 25 package org.netbeans.modules.j2ee.sun.share.configbean.customizers.common; 26 27 import java.awt.Color ; 28 import java.awt.Component ; 29 import java.awt.Dimension ; 30 import java.awt.event.MouseEvent ; 31 import java.awt.event.MouseMotionAdapter ; 32 import java.awt.FontMetrics ; 33 import java.awt.GridBagConstraints ; 34 import java.awt.GridBagLayout ; 35 import java.awt.Insets ; 36 import java.util.ArrayList ; 37 import java.util.HashMap ; 38 import java.util.Map ; 39 import java.lang.Math ; 40 41 import javax.swing.JComboBox ; 42 import javax.swing.JComponent ; 43 import javax.swing.JLabel ; 44 import javax.swing.JPanel ; 45 import javax.swing.JTable ; 46 import javax.swing.ListSelectionModel ; 47 import javax.swing.DefaultCellEditor ; 48 import javax.swing.table.JTableHeader ; 49 import javax.swing.table.TableCellEditor ; 50 import javax.swing.table.TableCellRenderer ; 51 import javax.swing.table.TableColumnModel ; 52 import javax.swing.table.TableColumn ; 53 import javax.swing.table.TableModel ; 54 55 56 60 public abstract class MethodTablePanel extends JPanel { 61 62 private MethodTableModel model ; 63 64 protected TableWithToolTips methodTable; 65 protected javax.swing.JScrollPane tablePane; 66 protected javax.swing.JPanel tablePanel; 67 68 69 70 public MethodTablePanel(MethodTableModel model){ 71 this.model = model; 72 initComponents(); 73 } 74 75 76 77 public MethodTablePanel() { 78 this.model = getMethodTableModel(); 79 initComponents(); 80 } 81 82 83 protected void setData(){ 84 methodTable.setModel(model); 85 } 86 87 88 protected abstract MethodTableModel getMethodTableModel(); 89 protected abstract String getTablePaneAcsblName(); 90 protected abstract String getTablePaneAcsblDesc(); 91 protected abstract String getTablePaneToolTip(); 92 93 94 protected String getToolTip(int row, int col){ 96 return null; 97 } 98 99 100 protected String getToolTip(int col){ 102 return null; 103 } 104 105 106 protected void initComponents() { 107 if(model == null) return; 109 setLayout(new GridBagLayout ()); 110 111 methodTable = new TableWithToolTips(); 112 113 JTableHeader header = methodTable.getTableHeader(); 114 ColumnHeaderToolTips tips = new ColumnHeaderToolTips(); 115 116 header.addMouseMotionListener(tips); 118 119 methodTable.setModel(model); 120 methodTable.setRowSelectionAllowed(false); 121 methodTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 122 123 GridBagConstraints gridBagConstraints = new java.awt.GridBagConstraints (); 124 javax.swing.JPanel panel = getPanel(); 125 if(panel != null){ 126 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 127 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 128 gridBagConstraints.insets = new java.awt.Insets (6,6,0,5); 129 add(panel, gridBagConstraints); 130 } 131 132 tablePane = new javax.swing.JScrollPane (methodTable); 135 tablePane.setOpaque(true); 136 tablePane.setToolTipText(getTablePaneToolTip()); 137 add(tablePane, getTableGridBagConstraints()); 138 tablePane.getAccessibleContext().setAccessibleName(getTablePaneAcsblName()); 139 tablePane.getAccessibleContext().setAccessibleDescription(getTablePaneAcsblDesc()); 140 } 141 142 protected javax.swing.JPanel getPanel() { 143 return null; 144 } 145 146 protected GridBagConstraints getTableGridBagConstraints(){ 147 GridBagConstraints gridBagConstraints = new java.awt.GridBagConstraints (); 148 gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER; 149 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 150 gridBagConstraints.weightx = 1.0; 151 gridBagConstraints.weighty = 1.0; 152 gridBagConstraints.insets = new java.awt.Insets (6,6,5,5); 153 return gridBagConstraints; 154 } 155 156 157 protected void setColumnRenderer(TableCellRenderer renderer, int col){ 158 TableColumnModel columnModel = methodTable.getColumnModel(); 159 TableColumn column = columnModel.getColumn(col); 160 column.setCellRenderer(renderer); 161 } 162 163 164 protected void setColumnEditor(TableCellEditor editor, int col){ 165 TableColumnModel columnModel = methodTable.getColumnModel(); 166 TableColumn column = columnModel.getColumn(col); 167 column.setCellEditor(editor); 168 } 169 170 171 protected TableColumn getColumn(int n, JTable table) 173 { 174 return ((n >= 0) && (n < table.getColumnCount()))? 175 table.getColumnModel().getColumn(n) : 176 null; 177 } 178 179 180 181 protected void adjustColumnWidth(int c, boolean includeEditor) 182 { 183 adjustColumnWidth(methodTable, c, null, includeEditor); 184 } 185 186 187 protected void adjustColumnWidth(JTable table, int c, Object templateValue, boolean includeEditor) 188 { 189 190 TableColumn tc = getColumn(c, table); 191 if (tc == null) { 192 System.out.println("Invalid column index: " + c); return; 194 } 195 196 197 int actualWidth = tc.getWidth(); 198 199 200 TableCellRenderer hr = tc.getHeaderRenderer(); 202 if (hr == null) { hr = table.getTableHeader().getDefaultRenderer(); } 203 Component hc = 204 hr.getTableCellRendererComponent(table, tc.getHeaderValue(), false, false, 0, c); 205 int prefWidth = hc.getPreferredSize().width; 206 207 208 if (templateValue != null) { 209 TableCellRenderer rend = table.getCellRenderer(0, c); 210 Component comp = 211 getTableCellRendererComponent(rend, table, templateValue, false, false, 0, c, false); 212 Dimension ps = comp.getPreferredSize(); 213 if (ps.width > prefWidth) { prefWidth = ps.width; } 214 } 215 216 217 for (int r = 0; r < methodTable.getRowCount(); r++) { 218 219 220 if (includeEditor && table.getModel().isCellEditable(r, c)) { 221 TableCellEditor tce = table.getCellEditor(r, c); 222 if (tce instanceof DefaultCellEditor ) { 223 Component comp = ((DefaultCellEditor )tce).getComponent(); 224 if (comp instanceof JComboBox ) { 225 Dimension ps = comp.getPreferredSize(); 226 if (ps.width > prefWidth) { prefWidth = ps.width; } 227 } 228 } 229 } 230 231 232 Object v = table.getValueAt(r, c); 233 if (v != null) { 234 TableCellRenderer rend = table.getCellRenderer(r, c); 235 Component comp = 236 getTableCellRendererComponent(rend, table, v, false, false, r, c, false); 237 Dimension ps = comp.getPreferredSize(); 238 if (ps.width > prefWidth) { prefWidth = ps.width; } 239 } 240 } 241 242 243 prefWidth += 4; tc.setMinWidth(prefWidth); 246 tc.setMaxWidth(prefWidth); 247 tc.setPreferredWidth(prefWidth); 248 } 250 251 252 public Component getTableCellRendererComponent(TableCellRenderer rend, 253 JTable tbl, Object val, boolean isSel, boolean focus, int r, 254 int c, boolean rightAlign) { 255 Component renderer = 256 rend.getTableCellRendererComponent(tbl, val, isSel, focus, r, c); 257 258 if (renderer instanceof JComponent ) { 259 JComponent comp = (JComponent )renderer; 260 if (comp instanceof JLabel ) { 261 String str = (val == null) ? "" : val.toString(); 262 FontMetrics fm = getFontMetrics(((JLabel )comp).getFont()); 263 String displayedVal = null; 264 if (rightAlign) { 265 displayedVal = rightAlignLongText( 266 str, 267 fm, 268 cellWidth(tbl,(JLabel )comp, r, c)); 269 } 270 else { 271 displayedVal = leftAlignLongText( 272 str, 273 fm, 274 cellWidth(tbl,(JLabel )comp, r, c)); 275 } 276 ((JLabel )comp).setText(displayedVal); 277 if (!displayedVal.equals(str)) { 278 ((JLabel )comp).setToolTipText(str); 279 } 280 else { 281 ((JLabel )comp).setToolTipText(null); 282 } 283 } 284 if (isSel) { 285 comp.setBackground(tbl.getSelectionBackground()); 286 comp.setOpaque(true); 287 } else { 288 TableModel model = tbl.getModel(); 289 Color bg = model.isCellEditable(r, c)? 290 EnabledBackgroundColor : DisabledBackgroundColor; 291 comp.setBackground(bg); 293 comp.setOpaque(true); 294 } 295 } else { 296 System.out.println("Not a JComponent: " + renderer); 297 } 298 return renderer; 299 } 300 301 302 public int cellWidth(JTable tbl, JLabel label, int r, int c) { 303 int cellWidth = tbl.getCellRect(r, c, false).width; 304 Insets insets = label.getInsets(); 305 cellWidth -= insets.left + insets.right; 306 return cellWidth; 307 } 308 309 310 311 324 public static String rightAlignLongText(String str, FontMetrics fm, int width) { 325 if (str.length() > 0) { 326 int swidth = fm.stringWidth(str); 327 if (width > 0 && swidth > width) { 328 int i = 0; 329 while (swidth > width && i < str.length()) { 330 i += 1; 331 String test = "..." + str.substring(i); swidth = fm.stringWidth(test); 333 } 334 str = "..." + str.substring(i); 335 } 336 } 337 return str; 338 } 339 340 341 354 public static String leftAlignLongText(String str, FontMetrics fm, int width) { 355 if (str.length() > 0) { 356 int swidth = fm.stringWidth(str); 357 if (width > 0 && swidth > width) { 358 int i = 0; 359 while (swidth > width && i < str.length()) { 360 i += 1; 361 String test = str.substring(0, str.length() - i) + "..."; swidth = fm.stringWidth(test); 363 } 364 str = str.substring(0, str.length() - i) + "..."; } 366 } 367 return str; 368 } 369 370 371 public static Color brighter(Color color, double factor) { 372 int r = color.getRed(), g = color.getGreen(), b = color.getBlue(); 373 if ((factor > 0.0) && (factor < 1.0)) { factor = 1.0 - factor; 375 int f = (int)(1.0 / (1.0 - factor)); 376 if ((r == 0) && (g == 0) && (b == 0)) { 377 return new Color (f, f, f); 378 } else { 379 if ((r > 0) && (r < f)) { r = f; } 380 if ((g > 0) && (g < f)) { g = f; } 381 if ((b > 0) && (b < f)) { b = f; } 382 return new Color ( 383 Math.min((int)(r / factor), 255), 384 Math.min((int)(g / factor), 255), 385 Math.min((int)(b / factor), 255)); 386 } 387 } else 388 if ((factor < 0.0) && (factor > -1.0)) { factor = 1.0 + factor; 390 return new Color ( 391 Math.max((int)(r * factor),0), 392 Math.max((int)(g * factor),0), 393 Math.max((int)(b * factor),0)); 394 } 395 return color; 396 } 397 398 399 public void setTablePanePreferredSize(Dimension dimension){ 400 tablePane.setMinimumSize(dimension); 401 tablePane.setPreferredSize(dimension); 402 } 403 404 405 public static final Color DisabledBackgroundColor = Color.lightGray; 406 public static final Color EnabledBackgroundColor = Color.white; 407 public static final Color DisabledTextColor = Color.darkGray; 408 409 410 protected class ColumnHeaderToolTips extends MouseMotionAdapter { 411 int curCol = -1; 414 415 Map tips = new HashMap (); 417 418 public void setToolTip(TableColumn col, String tooltip) { 420 if (tooltip == null) { 421 tips.remove(col); 422 } else { 423 tips.put(col, tooltip); 424 } 425 } 426 427 428 public void mouseMoved(MouseEvent evt) { 429 int col = -1; 430 JTableHeader header = (JTableHeader )evt.getSource(); 431 JTable table = header.getTable(); 432 TableColumnModel colModel = table.getColumnModel(); 433 col = colModel.getColumnIndexAtX(evt.getX()); 434 435 if (col >= 0) { 437 if (col != curCol) { 438 header.setToolTipText(getToolTip(col)); 439 curCol = col; 440 } 441 } 442 } 443 } 444 445 446 protected class TableWithToolTips extends FixedHeightJTable { 447 public Component prepareRenderer(TableCellRenderer renderer, 448 int rowIndex, int vColIndex) { 449 Component c = super.prepareRenderer(renderer, rowIndex, vColIndex); 450 if (c instanceof JComponent ) { 451 JComponent jc = (JComponent )c; 452 jc.setToolTipText(getToolTip(rowIndex, vColIndex)); 453 } 454 return c; 455 } 456 } 457 458 } 459 | Popular Tags |