KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > editor > imports > FixDuplicateImportStmts


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.java.editor.imports;
21
22 import java.awt.Component JavaDoc;
23 import java.awt.Dimension JavaDoc;
24 import java.awt.Font JavaDoc;
25 import java.awt.FontMetrics JavaDoc;
26 import java.awt.Graphics JavaDoc;
27 import java.util.EventObject JavaDoc;
28 import javax.swing.JComboBox JavaDoc;
29 import javax.swing.JComponent JavaDoc;
30 import javax.swing.JTable JavaDoc;
31 import javax.swing.ListSelectionModel JavaDoc;
32 import javax.swing.event.CellEditorListener JavaDoc;
33 import javax.swing.event.ChangeEvent JavaDoc;
34 import javax.swing.event.EventListenerList JavaDoc;
35 import javax.swing.table.TableCellEditor JavaDoc;
36 import javax.swing.table.TableCellRenderer JavaDoc;
37 import javax.swing.table.TableColumn JavaDoc;
38 import org.openide.awt.HtmlRenderer;
39 import org.openide.util.NbBundle;
40
41 /**
42  * JTable with custom renderer, so second column looks editable (JComboBox).
43  * Second column also has CellEditor (also a JComboBox).
44  *
45  * @author eakle, Martin Roskanin
46  */

47 public class FixDuplicateImportStmts extends javax.swing.JPanel JavaDoc{
48     private PackagesTblModel tblModel = null;
49     
50     public FixDuplicateImportStmts() {
51         initComponents();
52     }
53     
54     public void initPanel(String JavaDoc[] simpleNames, String JavaDoc[][] choices, String JavaDoc[] defaults) {
55         initComponentsMore(simpleNames, choices, defaults);
56         setAccessible();
57     }
58     
59     private void initComponentsMore(String JavaDoc simpleNames[], String JavaDoc choices[][], String JavaDoc 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 JavaDoc combo = new JComboBox JavaDoc(choices[i]);
74             combo.setSelectedItem(defaults[i]);
75             combo.setRenderer(HtmlRenderer.createRenderer());
76             combo.getAccessibleContext().setAccessibleDescription(getBundleString("FixDupImportStmts_Combo_ACSD")); //NOI18N
77
combo.getAccessibleContext().setAccessibleName(getBundleString("FixDupImportStmts_Combo_Name_ACSD")); //NOI18N
78
packagesTbl.setValueAt(combo, i, 1 );
79         }
80         
81         packagesTbl.setDefaultRenderer( JComponent JavaDoc.class, new JComponentCellRenderer() );
82         packagesTbl.setDefaultEditor( JComponent JavaDoc.class, new JComponentCellEditor() );
83         
84         //packagesTbl.setPreferredScrollableViewportSize(new Dimension(560, 350));
85
adjustTableSize(packagesTbl, 5, 560);
86     
87         // load localized text into widgets:
88
jLabel1.setText(getBundleString("FixDupImportStmts_IntroLbl")); //NOI18N
89
jLabel1.setLabelFor(packagesTbl);
90         jLabel1.setDisplayedMnemonic(getBundleString("FixDupImportStmts_IntroLbl_Mnemonic").charAt(0)); // NOI18N
91
}
92     
93     private static String JavaDoc getBundleString(String JavaDoc s) {
94         return NbBundle.getMessage(FixDuplicateImportStmts.class, s);
95     }
96     
97     
98     private void setAccessible() {
99         // establish initial focus in JTable.
100
packagesTbl.requestFocusInWindow();
101         
102         // remove the built-in behavior of Enter in a JTable so it invoke the dialog's OK btn:
103
enableEnterToClose(packagesTbl);
104         getAccessibleContext().setAccessibleDescription(getBundleString("FixDupImportStmts_IntroLbl")); // NOI18N
105
}
106     
107     public String JavaDoc[] getSelections() {
108         return tblModel.getSelections();
109     }
110     
111     // Set table size based on number of rows desired.
112
private void adjustTableSize(JTable JavaDoc 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 JavaDoc dim = new Dimension JavaDoc(width, height);
117         table.setPreferredScrollableViewportSize(dim);
118     }
119
120     static private void enableEnterToClose(javax.swing.JTable JavaDoc table)
121     {
122         javax.swing.InputMap JavaDoc inputMap =
123             table.getInputMap(table.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
124         javax.swing.KeyStroke JavaDoc esc = javax.swing.KeyStroke.getKeyStroke("ENTER"); // NOI18N
125
javax.swing.InputMap JavaDoc parentMap = inputMap.getParent();
126     parentMap.remove(esc);
127     }
128     
129     /** This method is called from within the constructor to
130      * initialize the form.
131      * WARNING: Do NOT modify this code. The content of this method is
132      * always regenerated by the Form Editor.
133      */

134     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
135
private void initComponents() {
136         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
137
138         jLabel1 = new javax.swing.JLabel JavaDoc();
139         jScrollPane1 = new javax.swing.JScrollPane JavaDoc();
140         packagesTbl = new JTableX();
141         jPanel1 = new javax.swing.JPanel JavaDoc();
142
143         setLayout(new java.awt.GridBagLayout JavaDoc());
144
145         setBorder(new javax.swing.border.EmptyBorder JavaDoc(new java.awt.Insets JavaDoc(12, 12, 12, 12)));
146         setPreferredSize(new java.awt.Dimension JavaDoc(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 JavaDoc();
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 JavaDoc(0, 0, 6, 0);
153         add(jLabel1, gridBagConstraints);
154
155         packagesTbl.setModel(new javax.swing.table.DefaultTableModel JavaDoc(
156             new Object JavaDoc [][] {
157                 {"MyType", "com.foo.AllTypes"},
158                 {"OtherType", "com.foo.AllTypes"},
159                 {null, null},
160                 {null, null},
161                 {null, null}
162             },
163             new String JavaDoc [] {
164                 "~Type Name", "~Class to Import"
165             }
166         ) {
167             Class JavaDoc[] types = new Class JavaDoc [] {
168                 java.lang.String JavaDoc.class, java.lang.Object JavaDoc.class
169             };
170             boolean[] canEdit = new boolean [] {
171                 false, true
172             };
173
174             public Class JavaDoc 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 JavaDoc();
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 JavaDoc();
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     // </editor-fold>//GEN-END:initComponents
202

203     
204     // Variables declaration - do not modify//GEN-BEGIN:variables
205
private javax.swing.JLabel JavaDoc jLabel1;
206     private javax.swing.JPanel JavaDoc jPanel1;
207     private javax.swing.JScrollPane JavaDoc jScrollPane1;
208     private javax.swing.JTable JavaDoc packagesTbl;
209     // End of variables declaration//GEN-END:variables
210

211     // ------------------ Inner Classes -------------------------
212
/*
213      * Service information for a selected Organization
214      */

215     private static class PackagesTblModel extends javax.swing.table.DefaultTableModel JavaDoc {
216         
217         String JavaDoc columnNames[] = {
218             getBundleString("FixDupImportStmts_TblCol1Hdr"), //NOI18N
219
getBundleString("FixDupImportStmts_TblCol2Hdr") //NOI18N
220
};
221         
222         public PackagesTblModel(int rowCount) {
223             super(rowCount, 2);
224         }
225
226         public String JavaDoc 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 JavaDoc[] getSelections() {
235             int rowCount = getRowCount();
236             String JavaDoc ret[] = new String JavaDoc[rowCount];
237             for (int i = 0; i<rowCount; i++ ){
238                 ret [i] = (String JavaDoc)((JComboBox JavaDoc)getValueAt(i, 1)).getSelectedItem();
239             }
240             return ret;
241         }
242         
243     }
244
245     // ----------------------
246

247     // extend JTable to support a cell editor for each row.
248
private static class JTableX extends JTable JavaDoc
249      {
250         //private boolean done = false;
251

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 JavaDoc g) {
262              if (needCalcRowHeight) {
263                  calcRowHeight(g);
264              }
265              super.paint(g);
266          }
267          
268          /** Calculate the height of rows based on the current font. This is
269           * done when the first paint occurs, to ensure that a valid Graphics
270           * object is available.
271           */

272          private void calcRowHeight(Graphics JavaDoc g) {
273              Font JavaDoc f = getFont();
274              FontMetrics JavaDoc fm = g.getFontMetrics(f);
275              int rowHeight = (int) (fm.getHeight() * 1.4);
276              needCalcRowHeight = false;
277              setRowHeight(rowHeight);
278          }
279         
280         
281         // rows were not high enough to show a JComboBox renderer.
282
/*
283         public void paint(Graphics g) {
284             if (!done) {
285                this.setRowHeight((int)(this.getRowHeight() * 1.4));
286                done = true;
287             }
288             super.paint(g);
289         }
290          
291           */

292
293         public TableCellRenderer JavaDoc getCellRenderer(int row, int column) {
294                 TableColumn JavaDoc tableColumn = getColumnModel().getColumn(column);
295                 TableCellRenderer JavaDoc renderer = tableColumn.getCellRenderer();
296                 if (renderer == null) {
297                         Class JavaDoc c = getColumnClass(column);
298                         if( c.equals(Object JavaDoc.class) )
299                         {
300                                 Object JavaDoc 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 JavaDoc getCellEditor(int row, int column) {
310                 TableColumn JavaDoc tableColumn = getColumnModel().getColumn(column);
311                 TableCellEditor JavaDoc editor = tableColumn.getCellEditor();
312                 if (editor == null) {
313                         Class JavaDoc c = getColumnClass(column);
314                         if( c.equals(Object JavaDoc.class) )
315                         {
316                                 Object JavaDoc 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 JavaDoc {
329         public JComponentCellRenderer () {}
330         
331         public Component JavaDoc getTableCellRendererComponent(JTable JavaDoc table, Object JavaDoc value,
332         boolean isSelected, boolean hasFocus, int row, int column) {
333             return (JComponent JavaDoc)value;
334         }
335     }
336
337
338     private class JComponentCellEditor implements TableCellEditor JavaDoc{
339         
340         protected EventListenerList JavaDoc listenerList = new EventListenerList JavaDoc();
341         transient protected ChangeEvent JavaDoc changeEvent = null;
342         
343         protected JComponent JavaDoc editorComponent = null;
344         
345         public JComponentCellEditor () {}
346         
347         public Component JavaDoc getComponent() {
348             return editorComponent;
349         }
350         
351         
352         public Object JavaDoc getCellEditorValue() {
353             return editorComponent;
354         }
355         
356         public boolean isCellEditable(EventObject JavaDoc anEvent) {
357             return true;
358         }
359         
360         public boolean shouldSelectCell(EventObject JavaDoc 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 JavaDoc l) {
374             listenerList.add(CellEditorListener JavaDoc.class, l);
375         }
376         
377         public void removeCellEditorListener(CellEditorListener JavaDoc l) {
378             listenerList.remove(CellEditorListener JavaDoc.class, l);
379         }
380         
381         protected void fireEditingStopped() {
382             Object JavaDoc[] listeners = listenerList.getListenerList();
383             // Process the listeners last to first, notifying
384
// those that are interested in this event
385
for (int i = listeners.length-2; i>=0; i-=2) {
386                 if (listeners[i]==CellEditorListener JavaDoc.class) {
387                     // Lazily create the event:
388
if (changeEvent == null)
389                         changeEvent = new ChangeEvent JavaDoc(this);
390                     ((CellEditorListener JavaDoc)listeners[i+1]).editingStopped(changeEvent);
391                 }
392             }
393         }
394         
395         protected void fireEditingCanceled() {
396             // Guaranteed to return a non-null array
397
Object JavaDoc[] listeners = listenerList.getListenerList();
398             // Process the listeners last to first, notifying
399
// those that are interested in this event
400
for (int i = listeners.length-2; i>=0; i-=2) {
401                 if (listeners[i]==CellEditorListener JavaDoc.class) {
402                     // Lazily create the event:
403
if (changeEvent == null)
404                         changeEvent = new ChangeEvent JavaDoc(this);
405                     ((CellEditorListener JavaDoc)listeners[i+1]).editingCanceled(changeEvent);
406                 }
407             }
408         }
409         
410         // implements javax.swing.table.TableCellEditor
411
public Component JavaDoc getTableCellEditorComponent(JTable JavaDoc table, Object JavaDoc value,
412         boolean isSelected, int row, int column) {
413             editorComponent = (JComponent JavaDoc)value;
414             return editorComponent;
415         }
416         
417     } // End of class JComponentCellEditor
418

419 }
Popular Tags