KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > refactoring > java > ui > PullUpPanel


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 package org.netbeans.modules.refactoring.java.ui;
20
21 import java.awt.Component JavaDoc;
22 import java.awt.Dimension JavaDoc;
23 import java.util.Set JavaDoc;
24 import javax.swing.AbstractListModel JavaDoc;
25 import javax.swing.ComboBoxModel JavaDoc;
26 import javax.swing.JPanel JavaDoc;
27 import javax.swing.event.ChangeListener JavaDoc;
28 import javax.swing.event.TableModelEvent JavaDoc;
29 import javax.swing.event.TableModelListener JavaDoc;
30 import javax.swing.table.AbstractTableModel JavaDoc;
31 import org.netbeans.api.java.source.TreePathHandle;
32 import org.netbeans.modules.refactoring.java.api.PullUpRefactoring;
33 import org.netbeans.modules.refactoring.spi.ui.CustomRefactoringPanel;
34 import org.openide.util.NbBundle;
35
36
37 /** UI panel for collecting refactoring parameters.
38  *
39  * @author Martin Matula
40  */

41 public class PullUpPanel extends JPanel JavaDoc implements CustomRefactoringPanel {
42     // helper constants describing columns in the table of members
43
private static final String JavaDoc[] COLUMN_NAMES = {"LBL_PullUp_Selected", "LBL_PullUp_Member", "LBL_PullUp_MakeAbstract"}; // NOI18N
44
private static final Class JavaDoc[] COLUMN_CLASSES = {Boolean JavaDoc.class, TreePathHandle.class, Boolean JavaDoc.class};
45     
46     // refactoring this panel provides parameters for
47
private final PullUpRefactoring refactoring;
48     // table model for the table of members
49
private final TableModel JavaDoc tableModel;
50     // pre-selected members (comes from the refactoring action - the elements
51
// that should be pre-selected in the table of members)
52
private Set JavaDoc selectedMembers;
53     // target type to move the members to
54
private TreePathHandle targetType;
55     // data for the members table (first dimension - rows, second dimension - columns)
56
// the columns are: 0 = Selected (true/false), 1 = Member (Java element), 2 = Make Abstract (true/false)
57
private Object JavaDoc[][] members = new Object JavaDoc[0][0];
58     
59     /** Creates new form PullUpPanel
60      * @param refactoring The refactoring this panel provides parameters for.
61      * @param selectedMembers Members that should be pre-selected in the panel
62      * (determined by which nodes the action was invoked on - e.g. if it was
63      * invoked on a method, the method will be pre-selected to be pulled up)
64      */

65     public PullUpPanel(PullUpRefactoring refactoring, Set JavaDoc selectedMembers, final ChangeListener JavaDoc parent) {
66         this.refactoring = refactoring;
67         this.tableModel = new TableModel JavaDoc();
68         this.selectedMembers = selectedMembers;
69         initComponents();
70         setPreferredSize(new Dimension JavaDoc(420, 380));
71         membersTable.getModel().addTableModelListener(new TableModelListener JavaDoc() {
72             public void tableChanged(TableModelEvent JavaDoc e) {
73                 parent.stateChanged(null);
74             }
75         });
76     }
77
78     /** Initialization of the panel (called by the parent window).
79      */

80     public void initialize() {
81 // // retrieve supertypes (will be used in the combo)
82
// JavaClass[] supertypes = refactoring.collectSupertypes();
83
//
84
// // *** initialize combo
85
// // set renderer for the combo (to display name of the class)
86
// supertypeCombo.setRenderer(new UIUtilities.JavaElementListCellRenderer() {
87
// /** Returns display text of the class. The text is returned in the
88
// * following format: SimpleName (package.name). If the class is an inner
89
// * class the text is: Outer.SimpleName (package.name).
90
// */
91
// protected String extractText(Object value) {
92
// // the value is always an instance of JavaClass
93
// JavaClass topLevel = (JavaClass) value;
94
// Object current;
95
// // iterate up through the parents to find the top-level class
96
// while ((current = topLevel.refImmediateComposite()) instanceof JavaClass) {
97
// topLevel = (JavaClass) current;
98
// }
99
// // derive the package name by subtracting the simple name of top-level class
100
// // from the fully qualified name of the top-level class
101
// String packageName = topLevel.getName();
102
// packageName = packageName.substring(0, packageName.length() - topLevel.getSimpleName().length());
103
// // now, get the class name by subtracting the package name from the class FQN
104
// String className = ((JavaClass) value).getName().substring(packageName.length());
105
// // remove the ending dot from the package name and surrond it by parentheses
106
// if (packageName.length() > 0) {
107
// packageName = " (" + packageName.substring(0, packageName.length() - 1) + ")"; // NOI18N
108
// }
109
// // create the displayText (concatenate the class name and package name)
110
// return className.concat(packageName);
111
// }
112
// });
113
// // set combo model
114
// supertypeCombo.setModel(new ComboModel(supertypes));
115
//
116
// // *** initialize table
117
// // set renderer for the second column ("Member") do display name of the feature
118
// membersTable.setDefaultRenderer(COLUMN_CLASSES[1], new UIUtilities.JavaElementTableCellRenderer() {
119
// // override the extractText method to add "implements " prefix to the text
120
// // in case the value is instance of MultipartId (i.e. it represents an interface
121
// // name from implements clause)
122
// protected String extractText(Object value) {
123
// String displayValue = super.extractText(value);
124
// if (value instanceof MultipartId) {
125
// displayValue = "implements " + displayValue; // NOI18N
126
// }
127
// return displayValue;
128
// }
129
// });
130
// // send renderer for the third column ("Make Abstract") to make the checkbox:
131
// // 1. hidden for elements that are not methods
132
// // 2. be disabled for static methods
133
// // 3. be disabled and checked for methods if the target type is an interface
134
// // 4. be disabled and check for abstract methods
135
// membersTable.getColumnModel().getColumn(2).setCellRenderer(new UIUtilities.BooleanTableCellRenderer() {
136
// public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
137
// // make the checkbox checked (even if "Make Abstract" is not set)
138
// // for non-static methods if the target type is an interface
139
// Object object = table.getModel().getValueAt(row, 1);
140
// if (object instanceof Method) {
141
// if ((targetType.isInterface() && !Modifier.isStatic(((Method) object).getModifiers())) || Modifier.isAbstract(((Method) object).getModifiers())) {
142
// value = Boolean.TRUE;
143
// }
144
// }
145
// // the super method automatically makes sure the checkbox is not visible if the
146
// // "Make Abstract" value is null (which holds for non-methods)
147
// // and that the checkbox is disabled if the cell is not editable (which holds for
148
// // static methods all the time and for all methods in case the target type is an interface
149
// // - see the table model)
150
// return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
151
// }
152
// });
153
// // set background color of the scroll pane to be the same as the background
154
// // of the table
155
// scrollPane.setBackground(membersTable.getBackground());
156
// scrollPane.getViewport().setBackground(membersTable.getBackground());
157
// // set default row height
158
// membersTable.setRowHeight(18);
159
// // set grid color to be consistent with other netbeans tables
160
// if (UIManager.getColor("control") != null) { // NOI18N
161
// membersTable.setGridColor(UIManager.getColor("control")); // NOI18N
162
// }
163
// // compute and set the preferred width for the first and the third column
164
// UIUtilities.initColumnWidth(membersTable, 0, Boolean.TRUE, 4);
165
// UIUtilities.initColumnWidth(membersTable, 2, Boolean.TRUE, 4);
166
}
167     
168     // --- GETTERS FOR REFACTORING PARAMETERS ----------------------------------
169

170     /** Getter used by the refactoring UI to get value
171      * of target type.
172      * @return Target type.
173      */

174     public TreePathHandle getTargetType() {
175         return targetType;
176     }
177     
178     /** Getter used by the refactoring UI to get members to be pulled up.
179      * @return Descriptors of members to be pulled up.
180      */

181     public PullUpRefactoring.MemberInfo[] getMembers() {
182 // List list = new ArrayList();
183
// // remeber if the target type is an interface (will be used in the loop)
184
// boolean targetIsInterface = targetType.isInterface();
185
// // go through all rows of a table and collect selected members
186
// for (int i = 0; i < members.length; i++) {
187
// // if the current row is selected, create MemberInfo for it and
188
// // add it to the list of selected members
189
// if (members[i][0].equals(Boolean.TRUE)) {
190
// Object element = members[i][1];
191
// Object member;
192
// if (element instanceof Field) {
193
// member = new PullUpRefactoring.MemberInfo((Field) element);
194
// } else if (element instanceof JavaClass) {
195
// member = new PullUpRefactoring.MemberInfo((JavaClass) element);
196
// } else if (element instanceof MultipartId) {
197
// member = new PullUpRefactoring.MemberInfo((MultipartId) element);
198
// } else {
199
// // for methods the makeAbstract is always set to true if the
200
// // target type is an interface
201
// member = new PullUpRefactoring.MemberInfo((Method) element, targetIsInterface || ((Boolean) members[i][2]).booleanValue());
202
// }
203
// list.add(member);
204
// }
205
// }
206
// // return the array of selected members
207
// return (PullUpRefactoring.MemberInfo[]) list.toArray(new PullUpRefactoring.MemberInfo[list.size()]);
208
return new PullUpRefactoring.MemberInfo[0];
209     }
210     
211     // --- GENERATED CODE ------------------------------------------------------
212

213     /** This method is called from within the constructor to
214      * initialize the form.
215      * WARNING: Do NOT modify this code. The content of this method is
216      * always regenerated by the Form Editor.
217      */

218     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
219
private void initComponents() {
220         supertypePanel = new javax.swing.JPanel JavaDoc();
221         supertypeCombo = new javax.swing.JComboBox JavaDoc();
222         supertypeLabel = new javax.swing.JLabel JavaDoc();
223         chooseLabel = new javax.swing.JLabel JavaDoc();
224         scrollPane = new javax.swing.JScrollPane JavaDoc();
225         membersTable = new javax.swing.JTable JavaDoc();
226
227         setLayout(new java.awt.BorderLayout JavaDoc());
228
229         setBorder(javax.swing.BorderFactory.createEmptyBorder(12, 12, 11, 11));
230         setName(org.openide.util.NbBundle.getMessage(PullUpPanel.class, "LBL_PullUpHeader", new Object JavaDoc[] {UIUtilities.getDisplayText(refactoring.getSourceType())}) /* NOI18N */);
231         supertypePanel.setLayout(new java.awt.BorderLayout JavaDoc(12, 0));
232
233         supertypePanel.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
234         supertypePanel.add(supertypeCombo, java.awt.BorderLayout.CENTER);
235
236         supertypeLabel.setLabelFor(supertypeCombo);
237         org.openide.awt.Mnemonics.setLocalizedText(supertypeLabel, java.util.ResourceBundle.getBundle("org/netbeans/modules/refactoring/ui/Bundle").getString("LBL_PullUp_Supertype"));
238         supertypePanel.add(supertypeLabel, java.awt.BorderLayout.WEST);
239         supertypeLabel.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(PullUpPanel.class, "ACSD_DestinationSupertypeName"));
240         supertypeLabel.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(PullUpPanel.class, "ACSD_DestinationSupertypeDescription"));
241
242         chooseLabel.setLabelFor(membersTable);
243         org.openide.awt.Mnemonics.setLocalizedText(chooseLabel, org.openide.util.NbBundle.getMessage(PullUpPanel.class, "LBL_PullUpLabel"));
244         chooseLabel.setBorder(javax.swing.BorderFactory.createEmptyBorder(6, 0, 0, 0));
245         supertypePanel.add(chooseLabel, java.awt.BorderLayout.SOUTH);
246
247         add(supertypePanel, java.awt.BorderLayout.NORTH);
248
249         membersTable.setModel(tableModel);
250         membersTable.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_NEXT_COLUMN);
251         scrollPane.setViewportView(membersTable);
252         membersTable.getAccessibleContext().setAccessibleName(org.openide.util.NbBundle.getMessage(PullUpPanel.class, "ACSD_MembersToPullUp"));
253         membersTable.getAccessibleContext().setAccessibleDescription(org.openide.util.NbBundle.getMessage(PullUpPanel.class, "ACSD_MembersToPullUpDescription"));
254
255         add(scrollPane, java.awt.BorderLayout.CENTER);
256
257     }
258     // </editor-fold>//GEN-END:initComponents
259

260     
261     // Variables declaration - do not modify//GEN-BEGIN:variables
262
private javax.swing.JLabel JavaDoc chooseLabel;
263     private javax.swing.JTable JavaDoc membersTable;
264     private javax.swing.JScrollPane JavaDoc scrollPane;
265     private javax.swing.JComboBox JavaDoc supertypeCombo;
266     private javax.swing.JLabel JavaDoc supertypeLabel;
267     private javax.swing.JPanel JavaDoc supertypePanel;
268     // End of variables declaration//GEN-END:variables
269

270     public Component JavaDoc getComponent() {
271         return this;
272     }
273
274     // --- MODELS --------------------------------------------------------------
275

276     /** Model for the members table.
277      */

278     private class TableModel extends AbstractTableModel JavaDoc {
279         public int getColumnCount() {
280             return COLUMN_NAMES.length;
281         }
282
283         public String JavaDoc getColumnName(int column) {
284             return UIUtilities.getColumnName(NbBundle.getMessage(PullUpPanel.class, COLUMN_NAMES[column]));
285         }
286
287         public Class JavaDoc getColumnClass(int columnIndex) {
288             return COLUMN_CLASSES[columnIndex];
289         }
290
291         public int getRowCount() {
292             return members.length;
293         }
294
295         public Object JavaDoc getValueAt(int rowIndex, int columnIndex) {
296             return members[rowIndex][columnIndex];
297         }
298
299         public void setValueAt(Object JavaDoc value, int rowIndex, int columnIndex) {
300             members[rowIndex][columnIndex] = value;
301             fireTableDataChanged();
302         }
303
304         public boolean isCellEditable(int rowIndex, int columnIndex) {
305             if (columnIndex == 2) {
306                 // column 2 is editable only in case of non-static methods
307
// if the target type is not an interface
308
// if the method is abstract
309
if (members[rowIndex][2] == null) {
310                     return false;
311                 }
312                 Object JavaDoc element = members[rowIndex][1];
313                 //TODO:
314
//return !targetType.isInterface() && !Modifier.isStatic(((Method) element).getModifiers()) && !Modifier.isAbstract(((Method) element).getModifiers());
315
return false;
316             } else {
317                 // column 0 is always editable, column 1 is never editable
318
return columnIndex == 0;
319             }
320         }
321         
322
323 // /** Method called by target type combo box model when the selection changes
324
// * (i.e. when the selected target type changes).
325
// * Updates table rows based on the change (all members from the source type
326
// * up to the direct subtypes of the target type need to be displayed).
327
// * @param classes Classes the members of which should be displayed (these are all classes
328
// * that are supertypes of source type (including the source type) and at the same time subtypes
329
// * of the target type (excluding the target type).
330
// */
331
// void update(JavaClass[] classes) {
332
// Map map = new HashMap();
333
// // go through the passed classes, collect all members from them and
334
// // create a map mapping a member to an array of java.lang.Object representing
335
// // a future table row corresponding to that member
336
// for (int i = 0; i < classes.length; i++) {
337
// // collect interface names
338
// for (Iterator it = classes[i].getInterfaceNames().iterator(); it.hasNext();) {
339
// Object ifcName = it.next();
340
// map.put(ifcName, new Object[] {Boolean.FALSE, ifcName, null});
341
// }
342
// // collect fields, methods and inner classes
343
// Object[] features = classes[i].getFeatures().toArray();
344
// for (int j = 0; j < features.length; j++) {
345
// if (features[j] instanceof JavaClass || features[j] instanceof Field || features[j] instanceof Method) {
346
// map.put(features[j], new Object[] {Boolean.FALSE, features[j], (features[j] instanceof Method) ? Boolean.FALSE : null});
347
// }
348
// }
349
// }
350
// // select some members if applicable
351
// if (selectedMembers != null) {
352
// // if the collection of pre-selected members is not null
353
// // this is the first creation of the table data ->
354
// // -> select the members from the selectedMembers collection
355
// for (Iterator it = selectedMembers.iterator(); it.hasNext();) {
356
// Object[] value = (Object[]) map.get(it.next());
357
// if (value != null) {
358
// value[0] = Boolean.TRUE;
359
// }
360
// }
361
// selectedMembers = null;
362
// } else {
363
// // this is not the first update of the table content ->
364
// // -> select elements that were selected before the update
365
// // (if they will still be present in the table)
366
// for (int i = 0; i < members.length; i++) {
367
// Object[] value = (Object[]) map.get(members[i][1]);
368
// if (value != null) {
369
// map.put(value[1], members[i]);
370
// }
371
// }
372
// }
373
//
374
// // TODO: remove overrides, since they cannot be pulled up
375
//
376
// // the members are collected
377
// // now, create a tree map (to sort them) and create the table data
378
// TreeMap treeMap = new TreeMap(new Comparator() {
379
// public int compare(Object o1, Object o2) {
380
// NamedElement ne1 = (NamedElement) o1, ne2 = (NamedElement) o2;
381
// // elements are sorted primarily by their class name
382
// int result = ne1.getClass().getName().compareTo(ne2.getClass().getName());
383
// if (result == 0) {
384
// // then by their display text
385
// result = UIUtilities.getDisplayText(ne1).compareTo(UIUtilities.getDisplayText(ne2));
386
// }
387
// if (result == 0) {
388
// // then the mofid is compared (to not take two non-identical
389
// // elements as equals)
390
// result = ne1.refMofId().compareTo(ne2.refMofId());
391
// }
392
// return result;
393
// }
394
// });
395
// treeMap.putAll(map);
396
// members = new Object[treeMap.size()][];
397
// int i = 0;
398
// for (Iterator it = treeMap.values().iterator(); it.hasNext(); i++) {
399
// members[i] = (Object[]) it.next();
400
// }
401
// // fire event to repaint the table
402
// this.fireTableDataChanged();
403
// }
404
// }
405

406     /** Model for combo box for choosing target type.
407      */

408     private class ComboModel extends AbstractListModel JavaDoc implements ComboBoxModel JavaDoc {
409         private final TreePathHandle[] supertypes;
410        
411         /** Creates the combo model.
412          * @param supertypes List of applicable supertypes that may be chosen to be
413          * target types.
414          */

415         ComboModel(TreePathHandle[] supertypes) {
416             this.supertypes = supertypes;
417             if (supertypes.length > 0) {
418                 setSelectedItem(supertypes[0]);
419             }
420         }
421         
422         /** Gets invoked when the selection changes. Computes the classes the members
423          * of which can be pulled up and calls table model's update() method to
424          * update the table content with changed set of members.
425          * @param anItem Class selected to be the target.
426          */

427         public void setSelectedItem(Object JavaDoc anItem) {
428 // if (targetType != anItem) {
429
// targetType = (TreePathHandle) anItem;
430
// // must fire this (according to the ComboBoxModel interface contract)
431
// fireContentsChanged(this, -1, -1);
432
// // compute the classes (they must be superclasses of source type - including it -
433
// // and subtypes of target type)
434
// List classes = new ArrayList();
435
// // add source type (it is always included)
436
// classes.add(refactoring.getSourceType());
437
// for (int i = 0; i < supertypes.length; i++) {
438
// // add the other subtypes of the target type
439
// if (!supertypes[i].equals(targetType) && supertypes[i].isSubTypeOf(targetType)) {
440
// classes.add(supertypes[i]);
441
// }
442
// }
443
// // update the table
444
// tableModel.update((JavaClass[]) classes.toArray(new JavaClass[classes.size()]));
445
// }
446
}
447
448         public Object JavaDoc getSelectedItem() {
449             return targetType;
450         }
451
452         public Object JavaDoc getElementAt(int index) {
453             return supertypes[index];
454         }
455
456         public int getSize() {
457             return supertypes.length;
458         }
459     }
460
461     }
462 }
463
Popular Tags