KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > refactoring > experimental > ui > CleanUpPanel


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 Leon Chiver. All Rights Reserved.
17  */

18 package org.netbeans.modules.refactoring.experimental.ui;
19
20 import java.util.ArrayList JavaDoc;
21 import java.util.BitSet JavaDoc;
22 import java.util.List JavaDoc;
23 import javax.swing.UIManager JavaDoc;
24 import javax.swing.table.AbstractTableModel JavaDoc;
25 import org.netbeans.jmi.javamodel.Resource;
26 import org.netbeans.modules.refactoring.experimental.CleanUpRefactoring;
27 import org.netbeans.modules.refactoring.spi.ui.CustomRefactoringPanel;
28 import org.netbeans.modules.refactoring.ui.UIUtilities;
29
30 /**
31  * @author leon
32  */

33 public class CleanUpPanel extends CustomRefactoringPanel {
34
35     private CleanUpRefactoring refactoring;
36
37     private List JavaDoc/*<Resource>*/ elements;
38     
39     public CleanUpPanel(CleanUpRefactoring refactoring, List JavaDoc/*<Resource>*/ elements) {
40         this.refactoring = refactoring;
41         this.elements = elements;
42         initComponents();
43     }
44     
45     List JavaDoc/*<Resource>*/ getSelectedResources() {
46         return ((ResourceTableModel) elementsTable.getModel()).getSelectedElements();
47     }
48     
49     boolean isCommentInsteadOfRemoving() {
50         return commentInsteadOfRemovingCheckBox.isSelected();
51     }
52     
53     boolean isRemoveUnusedImports() {
54         return removeUnusedImportsCheckBox.isSelected();
55     }
56     
57     boolean isRemoveUnusedFields() {
58         return removeUnusedElementsCheckBox.isSelected();
59     }
60     
61     boolean isRemoveUnusedCallableFeatures() {
62         return removeUnusedElementsCheckBox.isSelected();
63     }
64     
65     boolean isRemoveUnusedLocalVars() {
66         return removeUnusedElementsCheckBox.isSelected();
67     }
68     
69     boolean isRemoveRedundantCasts() {
70         return removeRedundantCastsCheckBox.isSelected();
71     }
72     
73     boolean isRemoveUnusedClasses() {
74         return removeUnusedElementsCheckBox.isSelected();
75     }
76     
77     public void initialize() {
78         elementsTable.setModel(new ResourceTableModel());
79         elementsTable.setDefaultRenderer(Resource.class, new UIUtilities.JavaElementTableCellRenderer());
80         UIUtilities.initColumnWidth(elementsTable, 0, Boolean.TRUE, 4);
81         elementsScrollPane.setBackground(elementsTable.getBackground());
82         elementsScrollPane.getViewport().setBackground(elementsTable.getBackground());
83         // set default row height
84
elementsTable.setRowHeight(18);
85         // set grid color to be consistent with other netbeans tables
86
if (UIManager.getColor("control") != null) { // NOI18N
87
elementsTable.setGridColor(UIManager.getColor("control")); // NOI18N
88
}
89     }
90     
91     public void addNotify() {
92         super.addNotify();
93         elementsTable.changeSelection(0, 0, true, false);
94         elementsTable.editCellAt(0, 0);
95         elementsTable.requestFocus();
96     }
97     
98     private class ResourceTableModel extends AbstractTableModel JavaDoc {
99         
100         private BitSet JavaDoc skippedResourceMask;
101         
102         public ResourceTableModel() {
103             skippedResourceMask = new BitSet JavaDoc(elements.size());
104         }
105         
106         public int getColumnCount() {
107             return 2;
108         }
109         
110         public int getRowCount() {
111             return elements.size();
112         }
113         
114         public Object JavaDoc getValueAt(int rowIndex, int columnIndex) {
115             if (columnIndex == 0) {
116                 return Boolean.valueOf(!skippedResourceMask.get(rowIndex));
117             } else {
118                 return elements.get(rowIndex);
119             }
120         }
121         
122         public void setValueAt(Object JavaDoc aValue, int rowIndex, int columnIndex) {
123             skippedResourceMask.set(rowIndex, !((Boolean JavaDoc) aValue).booleanValue());
124         }
125         
126         public boolean isCellEditable(int rowIndex, int columnIndex) {
127             return columnIndex == 0;
128         }
129         
130         public Class JavaDoc getColumnClass(int columnIndex) {
131             if (columnIndex == 0) {
132                 return Boolean JavaDoc.class;
133             } else {
134                 return Resource.class;
135             }
136         }
137         
138         public String JavaDoc getColumnName(int column) {
139             return column == 0 ?
140                 "" : // NOI18N
141
org.openide.util.NbBundle.getMessage(CleanUpPanel.class, "LBL_JavaClassFile"); // NOI18N
142
}
143         
144         public List JavaDoc/*<Resource>*/ getSelectedElements() {
145             int sz = elements.size();
146             List JavaDoc/*<Resource>*/ result = new ArrayList JavaDoc/*<Resource>*/();
147             for (int i = 0; i < sz; i++) {
148                 if (!skippedResourceMask.get(i)) {
149                     result.add(elements.get(i));
150                 }
151             }
152             return result;
153         }
154         
155     }
156     
157     /** This method is called from within the constructor to
158      * initialize the form.
159      * WARNING: Do NOT modify this code. The content of this method is
160      * always regenerated by the Form Editor.
161      */

162     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
163
private void initComponents() {
164         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
165
166         elementsLabel = new javax.swing.JLabel JavaDoc();
167         elementsScrollPane = new javax.swing.JScrollPane JavaDoc();
168         elementsTable = new javax.swing.JTable JavaDoc();
169         removeUnusedElementsCheckBox = new javax.swing.JCheckBox JavaDoc();
170         removeRedundantCastsCheckBox = new javax.swing.JCheckBox JavaDoc();
171         removeUnusedImportsCheckBox = new javax.swing.JCheckBox JavaDoc();
172         commentInsteadOfRemovingCheckBox = new javax.swing.JCheckBox JavaDoc();
173
174         setLayout(new java.awt.GridBagLayout JavaDoc());
175
176         org.openide.awt.Mnemonics.setLocalizedText(elementsLabel, org.openide.util.NbBundle.getMessage(CleanUpPanel.class, "LBL_ClassesFiles"));
177         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
178         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
179         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
180         add(elementsLabel, gridBagConstraints);
181
182         elementsScrollPane.setOpaque(false);
183         elementsScrollPane.setPreferredSize(new java.awt.Dimension JavaDoc(453, 180));
184         elementsScrollPane.setViewportView(elementsTable);
185
186         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
187         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
188         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
189         gridBagConstraints.weightx = 1.0;
190         gridBagConstraints.weighty = 0.6;
191         add(elementsScrollPane, gridBagConstraints);
192
193         removeUnusedElementsCheckBox.setSelected(true);
194         org.openide.awt.Mnemonics.setLocalizedText(removeUnusedElementsCheckBox, org.openide.util.NbBundle.getMessage(CleanUpPanel.class, "LBL_RemoveUnusedElements"));
195         removeUnusedElementsCheckBox.setToolTipText(org.openide.util.NbBundle.getMessage(CleanUpPanel.class, "TXT_UnusedElementsToolTip"));
196         removeUnusedElementsCheckBox.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
197         removeUnusedElementsCheckBox.setMargin(new java.awt.Insets JavaDoc(0, 0, 0, 0));
198         removeUnusedElementsCheckBox.addChangeListener(new javax.swing.event.ChangeListener JavaDoc() {
199             public void stateChanged(javax.swing.event.ChangeEvent JavaDoc evt) {
200                 removeUnusedElementsCheckBoxStateChanged(evt);
201             }
202         });
203
204         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
205         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
206         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
207         gridBagConstraints.insets = new java.awt.Insets JavaDoc(5, 0, 0, 0);
208         add(removeUnusedElementsCheckBox, gridBagConstraints);
209
210         removeRedundantCastsCheckBox.setSelected(true);
211         org.openide.awt.Mnemonics.setLocalizedText(removeRedundantCastsCheckBox, org.openide.util.NbBundle.getMessage(CleanUpPanel.class, "LBL_RemoveObsoleteCasts"));
212         removeRedundantCastsCheckBox.setToolTipText(org.openide.util.NbBundle.getMessage(CleanUpPanel.class, "TXT_RedundantCastsToolTip"));
213         removeRedundantCastsCheckBox.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
214         removeRedundantCastsCheckBox.setMargin(new java.awt.Insets JavaDoc(0, 0, 0, 0));
215         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
216         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
217         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
218         gridBagConstraints.insets = new java.awt.Insets JavaDoc(5, 0, 0, 0);
219         add(removeRedundantCastsCheckBox, gridBagConstraints);
220
221         removeUnusedImportsCheckBox.setSelected(true);
222         org.openide.awt.Mnemonics.setLocalizedText(removeUnusedImportsCheckBox, org.openide.util.NbBundle.getMessage(CleanUpPanel.class, "LBL_RemoveUnusedImports"));
223         removeUnusedImportsCheckBox.setToolTipText(org.openide.util.NbBundle.getMessage(CleanUpPanel.class, "TXT_RemoveImportsToolTip"));
224         removeUnusedImportsCheckBox.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
225         removeUnusedImportsCheckBox.setMargin(new java.awt.Insets JavaDoc(0, 0, 0, 0));
226         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
227         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
228         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
229         gridBagConstraints.insets = new java.awt.Insets JavaDoc(5, 0, 0, 0);
230         add(removeUnusedImportsCheckBox, gridBagConstraints);
231
232         org.openide.awt.Mnemonics.setLocalizedText(commentInsteadOfRemovingCheckBox, org.openide.util.NbBundle.getMessage(CleanUpPanel.class, "LBL_CommentOut"));
233         commentInsteadOfRemovingCheckBox.setToolTipText(org.openide.util.NbBundle.getMessage(CleanUpPanel.class, "TXT_CommentOut"));
234         commentInsteadOfRemovingCheckBox.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
235         commentInsteadOfRemovingCheckBox.setEnabled(false);
236         commentInsteadOfRemovingCheckBox.setMargin(new java.awt.Insets JavaDoc(0, 0, 0, 0));
237         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
238         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
239         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
240         gridBagConstraints.insets = new java.awt.Insets JavaDoc(10, 0, 0, 0);
241         add(commentInsteadOfRemovingCheckBox, gridBagConstraints);
242
243     }// </editor-fold>//GEN-END:initComponents
244

245     private void removeUnusedElementsCheckBoxStateChanged(javax.swing.event.ChangeEvent JavaDoc evt) {//GEN-FIRST:event_removeUnusedElementsCheckBoxStateChanged
246
//commentInsteadOfRemovingCheckBox.setEnabled(removeUnusedElementsCheckBox.isSelected());
247
}//GEN-LAST:event_removeUnusedElementsCheckBoxStateChanged
248

249                 
250     // Variables declaration - do not modify//GEN-BEGIN:variables
251
private javax.swing.JCheckBox JavaDoc commentInsteadOfRemovingCheckBox;
252     private javax.swing.JLabel JavaDoc elementsLabel;
253     private javax.swing.JScrollPane JavaDoc elementsScrollPane;
254     private javax.swing.JTable JavaDoc elementsTable;
255     private javax.swing.JCheckBox JavaDoc removeRedundantCastsCheckBox;
256     private javax.swing.JCheckBox JavaDoc removeUnusedElementsCheckBox;
257     private javax.swing.JCheckBox JavaDoc removeUnusedImportsCheckBox;
258     // End of variables declaration//GEN-END:variables
259

260 }
261
Popular Tags