1 19 20 package org.netbeans.modules.java.editor.imports; 21 22 import java.awt.Component ; 23 import java.awt.Dimension ; 24 import java.awt.Font ; 25 import java.awt.FontMetrics ; 26 import java.awt.Graphics ; 27 import java.util.EventObject ; 28 import javax.swing.JComboBox ; 29 import javax.swing.JComponent ; 30 import javax.swing.JTable ; 31 import javax.swing.ListSelectionModel ; 32 import javax.swing.event.CellEditorListener ; 33 import javax.swing.event.ChangeEvent ; 34 import javax.swing.event.EventListenerList ; 35 import javax.swing.table.TableCellEditor ; 36 import javax.swing.table.TableCellRenderer ; 37 import javax.swing.table.TableColumn ; 38 import org.openide.awt.HtmlRenderer; 39 import org.openide.util.NbBundle; 40 41 47 public class FixDuplicateImportStmts extends javax.swing.JPanel { 48 private PackagesTblModel tblModel = null; 49 50 public FixDuplicateImportStmts() { 51 initComponents(); 52 } 53 54 public void initPanel(String [] simpleNames, String [][] choices, String [] defaults) { 55 initComponentsMore(simpleNames, choices, defaults); 56 setAccessible(); 57 } 58 59 private void initComponentsMore(String simpleNames[], String choices[][], String defaults[]) { 60 tblModel = new PackagesTblModel(simpleNames.length); 61 packagesTbl.setModel(tblModel); 62 packagesTbl.setColumnSelectionInterval(1,1); 63 packagesTbl.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 64 65 packagesTbl.setSurrendersFocusOnKeystroke(true); 66 67 68 for (int i=0; i<simpleNames.length; i++){ 69 packagesTbl.setValueAt(simpleNames[i], i, 0 ); 70 } 71 72 for (int i=0; i<choices.length; i++){ 73 JComboBox combo = new JComboBox (choices[i]); 74 combo.setSelectedItem(defaults[i]); 75 combo.setRenderer(HtmlRenderer.createRenderer()); 76 combo.getAccessibleContext().setAccessibleDescription(getBundleString("FixDupImportStmts_Combo_ACSD")); combo.getAccessibleContext().setAccessibleName(getBundleString("FixDupImportStmts_Combo_Name_ACSD")); packagesTbl.setValueAt(combo, i, 1 ); 79 } 80 81 packagesTbl.setDefaultRenderer( JComponent .class, new JComponentCellRenderer() ); 82 packagesTbl.setDefaultEditor( JComponent .class, new JComponentCellEditor() ); 83 84 adjustTableSize(packagesTbl, 5, 560); 86 87 jLabel1.setText(getBundleString("FixDupImportStmts_IntroLbl")); jLabel1.setLabelFor(packagesTbl); 90 jLabel1.setDisplayedMnemonic(getBundleString("FixDupImportStmts_IntroLbl_Mnemonic").charAt(0)); } 92 93 private static String getBundleString(String s) { 94 return NbBundle.getMessage(FixDuplicateImportStmts.class, s); 95 } 96 97 98 private void setAccessible() { 99 packagesTbl.requestFocusInWindow(); 101 102 enableEnterToClose(packagesTbl); 104 getAccessibleContext().setAccessibleDescription(getBundleString("FixDupImportStmts_IntroLbl")); } 106 107 public String [] getSelections() { 108 return tblModel.getSelections(); 109 } 110 111 private void adjustTableSize(JTable table, int rows, int width) { 113 int margin = table.getIntercellSpacing().height; 114 int unit = table.getRowHeight() + margin; 115 int height = rows * unit - margin; 116 Dimension dim = new Dimension (width, height); 117 table.setPreferredScrollableViewportSize(dim); 118 } 119 120 static private void enableEnterToClose(javax.swing.JTable table) 121 { 122 javax.swing.InputMap inputMap = 123 table.getInputMap(table.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); 124 javax.swing.KeyStroke esc = javax.swing.KeyStroke.getKeyStroke("ENTER"); javax.swing.InputMap parentMap = inputMap.getParent(); 126 parentMap.remove(esc); 127 } 128 129 134 private void initComponents() { 136 java.awt.GridBagConstraints gridBagConstraints; 137 138 jLabel1 = new javax.swing.JLabel (); 139 jScrollPane1 = new javax.swing.JScrollPane (); 140 packagesTbl = new JTableX(); 141 jPanel1 = new javax.swing.JPanel (); 142 143 setLayout(new java.awt.GridBagLayout ()); 144 145 setBorder(new javax.swing.border.EmptyBorder (new java.awt.Insets (12, 12, 12, 12))); 146 setPreferredSize(new java.awt.Dimension (560, 200)); 147 jLabel1.setText("<html>~More than one class found in classpath for some Type Name in source. Select class to use in import statement for each Type Name below:</html>"); 148 gridBagConstraints = new java.awt.GridBagConstraints (); 149 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 150 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 151 gridBagConstraints.weightx = 0.1; 152 gridBagConstraints.insets = new java.awt.Insets (0, 0, 6, 0); 153 add(jLabel1, gridBagConstraints); 154 155 packagesTbl.setModel(new javax.swing.table.DefaultTableModel ( 156 new Object [][] { 157 {"MyType", "com.foo.AllTypes"}, 158 {"OtherType", "com.foo.AllTypes"}, 159 {null, null}, 160 {null, null}, 161 {null, null} 162 }, 163 new String [] { 164 "~Type Name", "~Class to Import" 165 } 166 ) { 167 Class [] types = new Class [] { 168 java.lang.String .class, java.lang.Object .class 169 }; 170 boolean[] canEdit = new boolean [] { 171 false, true 172 }; 173 174 public Class getColumnClass(int columnIndex) { 175 return types [columnIndex]; 176 } 177 178 public boolean isCellEditable(int rowIndex, int columnIndex) { 179 return canEdit [columnIndex]; 180 } 181 }); 182 jScrollPane1.setViewportView(packagesTbl); 183 packagesTbl.getAccessibleContext().setAccessibleDescription(java.util.ResourceBundle.getBundle("org/netbeans/modules/editor/java/Bundle").getString("FixDupImportStmts_Table_ACSD")); 184 185 gridBagConstraints = new java.awt.GridBagConstraints (); 186 gridBagConstraints.gridx = 0; 187 gridBagConstraints.gridy = 1; 188 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 189 gridBagConstraints.weightx = 1.0; 190 gridBagConstraints.weighty = 1.0; 191 add(jScrollPane1, gridBagConstraints); 192 193 gridBagConstraints = new java.awt.GridBagConstraints (); 194 gridBagConstraints.gridx = 0; 195 gridBagConstraints.gridy = 2; 196 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 197 gridBagConstraints.weightx = 1.0; 198 add(jPanel1, gridBagConstraints); 199 200 } 201 203 204 private javax.swing.JLabel jLabel1; 206 private javax.swing.JPanel jPanel1; 207 private javax.swing.JScrollPane jScrollPane1; 208 private javax.swing.JTable packagesTbl; 209 211 215 private static class PackagesTblModel extends javax.swing.table.DefaultTableModel { 216 217 String columnNames[] = { 218 getBundleString("FixDupImportStmts_TblCol1Hdr"), getBundleString("FixDupImportStmts_TblCol2Hdr") }; 221 222 public PackagesTblModel(int rowCount) { 223 super(rowCount, 2); 224 } 225 226 public String getColumnName(int col){ 227 return columnNames[col]; 228 } 229 230 public boolean isCellEditable(int row, int col) { 231 return (col == 1); 232 } 233 234 public String [] getSelections() { 235 int rowCount = getRowCount(); 236 String ret[] = new String [rowCount]; 237 for (int i = 0; i<rowCount; i++ ){ 238 ret [i] = (String )((JComboBox )getValueAt(i, 1)).getSelectedItem(); 239 } 240 return ret; 241 } 242 243 } 244 245 247 private static class JTableX extends JTable 249 { 250 252 private boolean needCalcRowHeight = true; 253 254 public JTableX () {} 255 256 public void updateUI() { 257 super.updateUI(); 258 needCalcRowHeight = true; 259 } 260 261 public void paint(Graphics g) { 262 if (needCalcRowHeight) { 263 calcRowHeight(g); 264 } 265 super.paint(g); 266 } 267 268 272 private void calcRowHeight(Graphics g) { 273 Font f = getFont(); 274 FontMetrics fm = g.getFontMetrics(f); 275 int rowHeight = (int) (fm.getHeight() * 1.4); 276 needCalcRowHeight = false; 277 setRowHeight(rowHeight); 278 } 279 280 281 292 293 public TableCellRenderer getCellRenderer(int row, int column) { 294 TableColumn tableColumn = getColumnModel().getColumn(column); 295 TableCellRenderer renderer = tableColumn.getCellRenderer(); 296 if (renderer == null) { 297 Class c = getColumnClass(column); 298 if( c.equals(Object .class) ) 299 { 300 Object o = getValueAt(row,column); 301 if( o != null ) 302 c = getValueAt(row,column).getClass(); 303 } 304 renderer = getDefaultRenderer(c); 305 } 306 return renderer; 307 } 308 309 public TableCellEditor getCellEditor(int row, int column) { 310 TableColumn tableColumn = getColumnModel().getColumn(column); 311 TableCellEditor editor = tableColumn.getCellEditor(); 312 if (editor == null) { 313 Class c = getColumnClass(column); 314 if( c.equals(Object .class) ) 315 { 316 Object o = getValueAt(row,column); 317 if( o != null ) 318 c = getValueAt(row,column).getClass(); 319 } 320 editor = getDefaultEditor(c); 321 } 322 return editor; 323 } 324 325 } 326 327 328 private class JComponentCellRenderer implements TableCellRenderer { 329 public JComponentCellRenderer () {} 330 331 public Component getTableCellRendererComponent(JTable table, Object value, 332 boolean isSelected, boolean hasFocus, int row, int column) { 333 return (JComponent )value; 334 } 335 } 336 337 338 private class JComponentCellEditor implements TableCellEditor { 339 340 protected EventListenerList listenerList = new EventListenerList (); 341 transient protected ChangeEvent changeEvent = null; 342 343 protected JComponent editorComponent = null; 344 345 public JComponentCellEditor () {} 346 347 public Component getComponent() { 348 return editorComponent; 349 } 350 351 352 public Object getCellEditorValue() { 353 return editorComponent; 354 } 355 356 public boolean isCellEditable(EventObject anEvent) { 357 return true; 358 } 359 360 public boolean shouldSelectCell(EventObject anEvent) { 361 return true; 362 } 363 364 public boolean stopCellEditing() { 365 fireEditingStopped(); 366 return true; 367 } 368 369 public void cancelCellEditing() { 370 fireEditingCanceled(); 371 } 372 373 public void addCellEditorListener(CellEditorListener l) { 374 listenerList.add(CellEditorListener .class, l); 375 } 376 377 public void removeCellEditorListener(CellEditorListener l) { 378 listenerList.remove(CellEditorListener .class, l); 379 } 380 381 protected void fireEditingStopped() { 382 Object [] listeners = listenerList.getListenerList(); 383 for (int i = listeners.length-2; i>=0; i-=2) { 386 if (listeners[i]==CellEditorListener .class) { 387 if (changeEvent == null) 389 changeEvent = new ChangeEvent (this); 390 ((CellEditorListener )listeners[i+1]).editingStopped(changeEvent); 391 } 392 } 393 } 394 395 protected void fireEditingCanceled() { 396 Object [] listeners = listenerList.getListenerList(); 398 for (int i = listeners.length-2; i>=0; i-=2) { 401 if (listeners[i]==CellEditorListener .class) { 402 if (changeEvent == null) 404 changeEvent = new ChangeEvent (this); 405 ((CellEditorListener )listeners[i+1]).editingCanceled(changeEvent); 406 } 407 } 408 } 409 410 public Component getTableCellEditorComponent(JTable table, Object value, 412 boolean isSelected, int row, int column) { 413 editorComponent = (JComponent )value; 414 return editorComponent; 415 } 416 417 } 419 } | Popular Tags |