KickJava   Java API By Example, From Geeks To Geeks.

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


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.refactoring.java.ui;
21
22 import java.awt.Component JavaDoc;
23 import java.awt.event.ActionEvent JavaDoc;
24 import java.awt.event.ActionListener JavaDoc;
25 import javax.swing.DefaultComboBoxModel JavaDoc;
26 import javax.swing.JPanel JavaDoc;
27 import javax.swing.JTextField JavaDoc;
28 import javax.swing.ListCellRenderer JavaDoc;
29 import javax.swing.event.ChangeListener JavaDoc;
30 import javax.swing.event.DocumentEvent JavaDoc;
31 import javax.swing.event.DocumentListener JavaDoc;
32 import org.netbeans.api.java.project.JavaProjectConstants;
33 import org.netbeans.api.project.FileOwnerQuery;
34 import org.netbeans.api.project.Project;
35 import org.netbeans.api.project.ProjectUtils;
36 import org.netbeans.api.project.SourceGroup;
37 import org.netbeans.api.project.Sources;
38 import org.netbeans.api.project.ui.OpenProjects;
39 import org.netbeans.modules.refactoring.spi.ui.CustomRefactoringPanel;
40 import org.netbeans.spi.java.project.support.ui.PackageView;
41 import org.openide.filesystems.FileObject;
42
43
44 /**
45  * @author Jan Becicka
46  */

47 public class CopyClassPanel extends JPanel JavaDoc implements ActionListener JavaDoc, DocumentListener JavaDoc, CustomRefactoringPanel {
48   
49     private static final ListCellRenderer JavaDoc GROUP_CELL_RENDERER = new MoveClassPanel.GroupCellRenderer();
50     private static final ListCellRenderer JavaDoc PROJECT_CELL_RENDERER = new MoveClassPanel.ProjectCellRenderer();
51     
52     private Project project;
53     private ChangeListener JavaDoc parent;
54     private FileObject fo;
55     private SourceGroup[] groups;
56     private String JavaDoc newName;
57     
58     public CopyClassPanel(final ChangeListener JavaDoc parent, String JavaDoc title, String JavaDoc startPackage, FileObject f, String JavaDoc newName) {
59         setName(title);
60         this.fo = f;
61         this.parent = parent;
62         this.newName = newName;
63         initComponents();
64         setCombosEnabled(true);
65         setThisClassVisible(true);
66         
67         rootComboBox.setRenderer(GROUP_CELL_RENDERER);
68         packageComboBox.setRenderer(PackageView.listRenderer());
69         projectsComboBox.setRenderer( PROJECT_CELL_RENDERER );
70                 
71         rootComboBox.addActionListener( this );
72         packageComboBox.addActionListener( this );
73         projectsComboBox.addActionListener( this );
74         
75         Object JavaDoc textField = packageComboBox.getEditor().getEditorComponent();
76         if (textField instanceof JTextField JavaDoc) {
77             ((JTextField JavaDoc) textField).getDocument().addDocumentListener(this);
78         }
79         newNameTextField.getDocument().addDocumentListener(this);
80         
81         project = fo != null ? FileOwnerQuery.getOwner(fo):OpenProjects.getDefault().getOpenProjects()[0];
82         initValues(startPackage);
83     }
84     
85     private boolean initialized = false;
86     public void initialize() {
87         if (initialized)
88             return ;
89         FileObject fob;
90         do {
91             fob = fo.getFileObject(newName + ".java"); //NOI18N
92
if (fob!=null)
93                 newName += "1";
94         } while (fob!=null);
95         newNameTextField.setText(newName);
96         newNameTextField.setSelectionStart(0);
97         newNameTextField.setSelectionEnd(newNameTextField.getText().length());
98         initialized = true;
99     }
100     
101     public void initValues(String JavaDoc preselectedFolder ) {
102         
103         Project openProjects[] = OpenProjects.getDefault().getOpenProjects();
104         DefaultComboBoxModel JavaDoc projectsModel = new DefaultComboBoxModel JavaDoc( openProjects );
105         projectsComboBox.setModel( projectsModel );
106         projectsComboBox.setSelectedItem( project );
107         
108         updateRoots();
109         updatePackages();
110         if (preselectedFolder != null) {
111             packageComboBox.setSelectedItem(preselectedFolder);
112         }
113         // Determine the extension
114
}
115     
116     public void requestFocus() {
117         newNameTextField.requestFocus();
118     }
119     
120     public FileObject getRootFolder() {
121         return ((SourceGroup) rootComboBox.getSelectedItem()).getRootFolder();
122     }
123     
124     public String JavaDoc getPackageName() {
125         String JavaDoc packageName = packageComboBox.getEditor().getItem().toString();
126         return packageName.replace('.', '/'); // NOI18N
127
}
128     
129     private void fireChange() {
130         parent.stateChanged(null);
131     }
132     
133     /** This method is called from within the constructor to
134      * initialize the form.
135      * WARNING: Do NOT modify this code. The content of this method is
136      * always regenerated by the Form Editor.
137      */

138     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
139
private void initComponents() {
140         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
141
142         labelProject = new javax.swing.JLabel JavaDoc();
143         projectsComboBox = new javax.swing.JComboBox JavaDoc();
144         labelLocation = new javax.swing.JLabel JavaDoc();
145         rootComboBox = new javax.swing.JComboBox JavaDoc();
146         labelPackage = new javax.swing.JLabel JavaDoc();
147         packageComboBox = new javax.swing.JComboBox JavaDoc();
148         bottomPanel = new javax.swing.JPanel JavaDoc();
149         newNameLabel = new javax.swing.JLabel JavaDoc();
150         newNameTextField = new javax.swing.JTextField JavaDoc();
151         isUpdateReferences = new javax.swing.JCheckBox JavaDoc();
152
153         setLayout(new java.awt.GridBagLayout JavaDoc());
154
155         labelProject.setLabelFor(projectsComboBox);
156         org.openide.awt.Mnemonics.setLocalizedText(labelProject, org.openide.util.NbBundle.getMessage(CopyClassPanel.class, "LBL_Project")); // NOI18N
157
gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
158         gridBagConstraints.gridx = 0;
159         gridBagConstraints.gridy = 2;
160         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
161         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 6, 0);
162         add(labelProject, gridBagConstraints);
163         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
164         gridBagConstraints.gridx = 1;
165         gridBagConstraints.gridy = 2;
166         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
167         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
168         gridBagConstraints.weightx = 1.0;
169         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 6, 6, 0);
170         add(projectsComboBox, gridBagConstraints);
171         projectsComboBox.getAccessibleContext().setAccessibleDescription(null);
172
173         labelLocation.setLabelFor(rootComboBox);
174         org.openide.awt.Mnemonics.setLocalizedText(labelLocation, org.openide.util.NbBundle.getMessage(CopyClassPanel.class, "LBL_Location")); // NOI18N
175
gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
176         gridBagConstraints.gridx = 0;
177         gridBagConstraints.gridy = 3;
178         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
179         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 6, 0);
180         add(labelLocation, gridBagConstraints);
181         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
182         gridBagConstraints.gridx = 1;
183         gridBagConstraints.gridy = 3;
184         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
185         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
186         gridBagConstraints.weightx = 1.0;
187         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 6, 6, 0);
188         add(rootComboBox, gridBagConstraints);
189         rootComboBox.getAccessibleContext().setAccessibleDescription(null);
190
191         labelPackage.setLabelFor(packageComboBox);
192         org.openide.awt.Mnemonics.setLocalizedText(labelPackage, org.openide.util.NbBundle.getMessage(CopyClassPanel.class, "LBL_ToPackage")); // NOI18N
193
gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
194         gridBagConstraints.gridx = 0;
195         gridBagConstraints.gridy = 4;
196         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
197         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 12, 0);
198         add(labelPackage, gridBagConstraints);
199
200         packageComboBox.setEditable(true);
201         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
202         gridBagConstraints.gridx = 1;
203         gridBagConstraints.gridy = 4;
204         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
205         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
206         gridBagConstraints.weightx = 1.0;
207         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 6, 12, 0);
208         add(packageComboBox, gridBagConstraints);
209         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
210         gridBagConstraints.gridx = 0;
211         gridBagConstraints.gridy = 5;
212         gridBagConstraints.gridwidth = 2;
213         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
214         gridBagConstraints.weightx = 1.0;
215         gridBagConstraints.weighty = 1.0;
216         add(bottomPanel, gridBagConstraints);
217
218         org.openide.awt.Mnemonics.setLocalizedText(newNameLabel, org.openide.util.NbBundle.getMessage(CopyClassPanel.class, "LBL_NewName")); // NOI18N
219
gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
220         gridBagConstraints.gridx = 0;
221         gridBagConstraints.gridy = 1;
222         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
223         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 6, 0);
224         add(newNameLabel, gridBagConstraints);
225
226         newNameTextField.setText("NewClass");
227         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
228         gridBagConstraints.gridx = 1;
229         gridBagConstraints.gridy = 1;
230         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
231         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
232         gridBagConstraints.weightx = 1.0;
233         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 6, 6, 0);
234         add(newNameTextField, gridBagConstraints);
235
236         org.openide.awt.Mnemonics.setLocalizedText(isUpdateReferences, org.openide.util.NbBundle.getMessage(CopyClassPanel.class, "LBL_CopyWithoutRefactoring")); // NOI18N
237
isUpdateReferences.setBorder(javax.swing.BorderFactory.createEmptyBorder(4, 4, 0, 4));
238         isUpdateReferences.setMargin(new java.awt.Insets JavaDoc(2, 2, 0, 2));
239         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
240         gridBagConstraints.gridx = 0;
241         gridBagConstraints.gridy = 6;
242         gridBagConstraints.gridwidth = 2;
243         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
244         add(isUpdateReferences, gridBagConstraints);
245     }// </editor-fold>//GEN-END:initComponents
246

247     
248     
249     // Variables declaration - do not modify//GEN-BEGIN:variables
250
protected javax.swing.JPanel JavaDoc bottomPanel;
251     private javax.swing.JCheckBox JavaDoc isUpdateReferences;
252     private javax.swing.JLabel JavaDoc labelLocation;
253     private javax.swing.JLabel JavaDoc labelPackage;
254     private javax.swing.JLabel JavaDoc labelProject;
255     private javax.swing.JLabel JavaDoc newNameLabel;
256     private javax.swing.JTextField JavaDoc newNameTextField;
257     private javax.swing.JComboBox JavaDoc packageComboBox;
258     private javax.swing.JComboBox JavaDoc projectsComboBox;
259     private javax.swing.JComboBox JavaDoc rootComboBox;
260     // End of variables declaration//GEN-END:variables
261

262     // ActionListener implementation -------------------------------------------
263

264     public void actionPerformed(ActionEvent JavaDoc e) {
265         if (projectsComboBox == e.getSource()) {
266             project = (Project) projectsComboBox.getSelectedItem();
267             updateRoots();
268             updatePackages();
269         } else
270         if ( rootComboBox == e.getSource() ) {
271             updatePackages();
272         }
273         else if ( packageComboBox == e.getSource() ) {
274         }
275     }
276     
277     // DocumentListener implementation -----------------------------------------
278

279     public void changedUpdate(DocumentEvent JavaDoc e) {
280         fireChange();
281     }
282     
283     public void insertUpdate(DocumentEvent JavaDoc e) {
284         fireChange();
285     }
286     
287     public void removeUpdate(DocumentEvent JavaDoc e) {
288         fireChange();
289     }
290     
291     // Private methods ---------------------------------------------------------
292

293     private void updatePackages() {
294         SourceGroup g = (SourceGroup) rootComboBox.getSelectedItem();
295         packageComboBox.setModel(PackageView.createListView(g));
296     }
297     
298     void setCombosEnabled(boolean enabled) {
299         packageComboBox.setEnabled(enabled);
300         rootComboBox.setEnabled(enabled);
301         projectsComboBox.setEnabled(enabled);
302     }
303     
304     void setThisClassVisible(boolean visible) {
305         newNameLabel.setVisible(visible);
306         newNameTextField.setVisible(visible);
307     }
308
309     public boolean isUpdateReferences() {
310         if (isUpdateReferences.isVisible() && isUpdateReferences.isSelected())
311             return false;
312         return true;
313     }
314     
315     
316     public String JavaDoc getNewName() {
317         return newNameTextField.getText();
318     }
319     
320     private void updateRoots() {
321         Sources sources = ProjectUtils.getSources(project);
322         groups = sources.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA);
323         if (groups.length == 0) {
324             // XXX why?? This is probably wrong. If the project has no Java groups,
325
// you cannot move anything into it.
326
groups = sources.getSourceGroups( Sources.TYPE_GENERIC );
327         }
328
329         int preselectedItem = 0;
330         for( int i = 0; i < groups.length; i++ ) {
331             if (fo!=null) {
332                 try {
333                     if (groups[i].contains(fo)) {
334                         preselectedItem = i;
335                     }
336                 } catch (IllegalArgumentException JavaDoc e) {
337                     // XXX this is a poor abuse of exception handling
338
}
339             }
340         }
341                 
342         // Setup comboboxes
343
rootComboBox.setModel(new DefaultComboBoxModel JavaDoc(groups));
344         rootComboBox.setSelectedIndex(preselectedItem);
345     }
346     
347     public Component JavaDoc getComponent() {
348         return this;
349     }
350
351 }
352
Popular Tags