001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.refactoring.java.ui;
043:
044: import java.awt.Component;
045: import java.awt.event.ActionEvent;
046: import java.awt.event.ActionListener;
047: import javax.swing.DefaultComboBoxModel;
048: import javax.swing.JPanel;
049: import javax.swing.JTextField;
050: import javax.swing.ListCellRenderer;
051: import javax.swing.event.ChangeListener;
052: import javax.swing.event.DocumentEvent;
053: import javax.swing.event.DocumentListener;
054: import org.netbeans.api.java.project.JavaProjectConstants;
055: import org.netbeans.api.project.FileOwnerQuery;
056: import org.netbeans.api.project.Project;
057: import org.netbeans.api.project.ProjectUtils;
058: import org.netbeans.api.project.SourceGroup;
059: import org.netbeans.api.project.Sources;
060: import org.netbeans.api.project.ui.OpenProjects;
061: import org.netbeans.modules.refactoring.spi.ui.CustomRefactoringPanel;
062: import org.netbeans.spi.java.project.support.ui.PackageView;
063: import org.openide.filesystems.FileObject;
064:
065: /**
066: * @author Jan Becicka
067: */
068: public class CopyClassPanel extends JPanel implements ActionListener,
069: DocumentListener, CustomRefactoringPanel {
070:
071: private final ListCellRenderer GROUP_CELL_RENDERER = new MoveClassPanel.GroupCellRenderer();
072: private final ListCellRenderer PROJECT_CELL_RENDERER = new MoveClassPanel.ProjectCellRenderer();
073:
074: private Project project;
075: private ChangeListener parent;
076: private FileObject fo;
077: private SourceGroup[] groups;
078: private String newName;
079:
080: public CopyClassPanel(final ChangeListener parent, String title,
081: String startPackage, FileObject f, String newName) {
082: setName(title);
083: this .fo = f;
084: this .parent = parent;
085: this .newName = newName;
086: initComponents();
087: setCombosEnabled(true);
088: setThisClassVisible(true);
089:
090: rootComboBox.setRenderer(GROUP_CELL_RENDERER);
091: packageComboBox.setRenderer(PackageView.listRenderer());
092: projectsComboBox.setRenderer(PROJECT_CELL_RENDERER);
093:
094: rootComboBox.addActionListener(this );
095: packageComboBox.addActionListener(this );
096: projectsComboBox.addActionListener(this );
097:
098: Object textField = packageComboBox.getEditor()
099: .getEditorComponent();
100: if (textField instanceof JTextField) {
101: ((JTextField) textField).getDocument().addDocumentListener(
102: this );
103: }
104: newNameTextField.getDocument().addDocumentListener(this );
105:
106: project = fo != null ? FileOwnerQuery.getOwner(fo)
107: : OpenProjects.getDefault().getOpenProjects()[0];
108: initValues(startPackage);
109: }
110:
111: private boolean initialized = false;
112:
113: public void initialize() {
114: if (initialized)
115: return;
116: FileObject fob;
117: do {
118: fob = fo.getFileObject(newName + ".java"); //NOI18N
119: if (fob != null)
120: newName += "1"; // NOI18N
121: } while (fob != null);
122: newNameTextField.setText(newName);
123: newNameTextField.setSelectionStart(0);
124: newNameTextField.setSelectionEnd(newNameTextField.getText()
125: .length());
126: initialized = true;
127: }
128:
129: public void initValues(String preselectedFolder) {
130:
131: Project openProjects[] = OpenProjects.getDefault()
132: .getOpenProjects();
133: DefaultComboBoxModel projectsModel = new DefaultComboBoxModel(
134: openProjects);
135: projectsComboBox.setModel(projectsModel);
136: projectsComboBox.setSelectedItem(project);
137:
138: updateRoots();
139: updatePackages();
140: if (preselectedFolder != null) {
141: packageComboBox.setSelectedItem(preselectedFolder);
142: }
143: // Determine the extension
144: }
145:
146: public void requestFocus() {
147: newNameTextField.requestFocus();
148: }
149:
150: public FileObject getRootFolder() {
151: return ((SourceGroup) rootComboBox.getSelectedItem())
152: .getRootFolder();
153: }
154:
155: public String getPackageName() {
156: String packageName = packageComboBox.getEditor().getItem()
157: .toString();
158: return packageName.replace('.', '/'); // NOI18N
159: }
160:
161: private void fireChange() {
162: parent.stateChanged(null);
163: }
164:
165: /** This method is called from within the constructor to
166: * initialize the form.
167: * WARNING: Do NOT modify this code. The content of this method is
168: * always regenerated by the Form Editor.
169: */
170: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
171: private void initComponents() {
172: java.awt.GridBagConstraints gridBagConstraints;
173:
174: labelProject = new javax.swing.JLabel();
175: projectsComboBox = new javax.swing.JComboBox();
176: labelLocation = new javax.swing.JLabel();
177: rootComboBox = new javax.swing.JComboBox();
178: labelPackage = new javax.swing.JLabel();
179: packageComboBox = new javax.swing.JComboBox();
180: bottomPanel = new javax.swing.JPanel();
181: newNameLabel = new javax.swing.JLabel();
182: newNameTextField = new javax.swing.JTextField();
183: isUpdateReferences = new javax.swing.JCheckBox();
184:
185: setLayout(new java.awt.GridBagLayout());
186:
187: labelProject.setLabelFor(projectsComboBox);
188: org.openide.awt.Mnemonics.setLocalizedText(labelProject,
189: org.openide.util.NbBundle.getMessage(
190: CopyClassPanel.class, "LBL_Project")); // NOI18N
191: gridBagConstraints = new java.awt.GridBagConstraints();
192: gridBagConstraints.gridx = 0;
193: gridBagConstraints.gridy = 2;
194: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
195: gridBagConstraints.insets = new java.awt.Insets(0, 0, 6, 0);
196: add(labelProject, gridBagConstraints);
197: labelProject.getAccessibleContext().setAccessibleDescription(
198: "N/A");
199:
200: gridBagConstraints = new java.awt.GridBagConstraints();
201: gridBagConstraints.gridx = 1;
202: gridBagConstraints.gridy = 2;
203: gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
204: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
205: gridBagConstraints.weightx = 1.0;
206: gridBagConstraints.insets = new java.awt.Insets(0, 6, 6, 0);
207: add(projectsComboBox, gridBagConstraints);
208: projectsComboBox.getAccessibleContext()
209: .setAccessibleDescription(null);
210:
211: labelLocation.setLabelFor(rootComboBox);
212: org.openide.awt.Mnemonics.setLocalizedText(labelLocation,
213: org.openide.util.NbBundle.getMessage(
214: CopyClassPanel.class, "LBL_Location")); // NOI18N
215: gridBagConstraints = new java.awt.GridBagConstraints();
216: gridBagConstraints.gridx = 0;
217: gridBagConstraints.gridy = 3;
218: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
219: gridBagConstraints.insets = new java.awt.Insets(0, 0, 6, 0);
220: add(labelLocation, gridBagConstraints);
221: labelLocation.getAccessibleContext().setAccessibleDescription(
222: "N/A");
223:
224: gridBagConstraints = new java.awt.GridBagConstraints();
225: gridBagConstraints.gridx = 1;
226: gridBagConstraints.gridy = 3;
227: gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
228: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
229: gridBagConstraints.weightx = 1.0;
230: gridBagConstraints.insets = new java.awt.Insets(0, 6, 6, 0);
231: add(rootComboBox, gridBagConstraints);
232: rootComboBox.getAccessibleContext().setAccessibleDescription(
233: null);
234:
235: labelPackage.setLabelFor(packageComboBox);
236: org.openide.awt.Mnemonics.setLocalizedText(labelPackage,
237: org.openide.util.NbBundle.getMessage(
238: CopyClassPanel.class, "LBL_ToPackage")); // NOI18N
239: gridBagConstraints = new java.awt.GridBagConstraints();
240: gridBagConstraints.gridx = 0;
241: gridBagConstraints.gridy = 4;
242: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
243: gridBagConstraints.insets = new java.awt.Insets(0, 0, 12, 0);
244: add(labelPackage, gridBagConstraints);
245: labelPackage.getAccessibleContext().setAccessibleDescription(
246: "N/A");
247:
248: packageComboBox.setEditable(true);
249: gridBagConstraints = new java.awt.GridBagConstraints();
250: gridBagConstraints.gridx = 1;
251: gridBagConstraints.gridy = 4;
252: gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
253: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
254: gridBagConstraints.weightx = 1.0;
255: gridBagConstraints.insets = new java.awt.Insets(0, 6, 12, 0);
256: add(packageComboBox, gridBagConstraints);
257: gridBagConstraints = new java.awt.GridBagConstraints();
258: gridBagConstraints.gridx = 0;
259: gridBagConstraints.gridy = 5;
260: gridBagConstraints.gridwidth = 2;
261: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
262: gridBagConstraints.weightx = 1.0;
263: gridBagConstraints.weighty = 1.0;
264: add(bottomPanel, gridBagConstraints);
265:
266: newNameLabel.setLabelFor(newNameTextField);
267: org.openide.awt.Mnemonics.setLocalizedText(newNameLabel,
268: org.openide.util.NbBundle.getMessage(
269: CopyClassPanel.class, "LBL_NewName")); // NOI18N
270: gridBagConstraints = new java.awt.GridBagConstraints();
271: gridBagConstraints.gridx = 0;
272: gridBagConstraints.gridy = 1;
273: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
274: gridBagConstraints.insets = new java.awt.Insets(0, 0, 6, 0);
275: add(newNameLabel, gridBagConstraints);
276: newNameLabel.getAccessibleContext().setAccessibleDescription(
277: "N/A");
278:
279: newNameTextField.setText(org.openide.util.NbBundle.getMessage(
280: CopyClassPanel.class,
281: "CopyClassPanel.newNameTextField.text")); // NOI18N
282: gridBagConstraints = new java.awt.GridBagConstraints();
283: gridBagConstraints.gridx = 1;
284: gridBagConstraints.gridy = 1;
285: gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
286: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
287: gridBagConstraints.weightx = 1.0;
288: gridBagConstraints.insets = new java.awt.Insets(0, 6, 6, 0);
289: add(newNameTextField, gridBagConstraints);
290:
291: org.openide.awt.Mnemonics.setLocalizedText(isUpdateReferences,
292: org.openide.util.NbBundle.getMessage(
293: CopyClassPanel.class,
294: "LBL_CopyWithoutRefactoring")); // NOI18N
295: isUpdateReferences.setBorder(javax.swing.BorderFactory
296: .createEmptyBorder(4, 4, 0, 4));
297: isUpdateReferences.setMargin(new java.awt.Insets(2, 2, 0, 2));
298: isUpdateReferences
299: .addActionListener(new java.awt.event.ActionListener() {
300: public void actionPerformed(
301: java.awt.event.ActionEvent evt) {
302: isUpdateReferencesActionPerformed(evt);
303: }
304: });
305: gridBagConstraints = new java.awt.GridBagConstraints();
306: gridBagConstraints.gridx = 0;
307: gridBagConstraints.gridy = 6;
308: gridBagConstraints.gridwidth = 2;
309: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
310: add(isUpdateReferences, gridBagConstraints);
311: }// </editor-fold>//GEN-END:initComponents
312:
313: private void isUpdateReferencesActionPerformed(
314: java.awt.event.ActionEvent evt) {//GEN-FIRST:event_isUpdateReferencesActionPerformed
315: parent.stateChanged(null);
316: }//GEN-LAST:event_isUpdateReferencesActionPerformed
317:
318: // Variables declaration - do not modify//GEN-BEGIN:variables
319: protected javax.swing.JPanel bottomPanel;
320: private javax.swing.JCheckBox isUpdateReferences;
321: private javax.swing.JLabel labelLocation;
322: private javax.swing.JLabel labelPackage;
323: private javax.swing.JLabel labelProject;
324: private javax.swing.JLabel newNameLabel;
325: private javax.swing.JTextField newNameTextField;
326: private javax.swing.JComboBox packageComboBox;
327: private javax.swing.JComboBox projectsComboBox;
328: private javax.swing.JComboBox rootComboBox;
329:
330: // End of variables declaration//GEN-END:variables
331:
332: // ActionListener implementation -------------------------------------------
333:
334: public void actionPerformed(ActionEvent e) {
335: if (projectsComboBox == e.getSource()) {
336: project = (Project) projectsComboBox.getSelectedItem();
337: updateRoots();
338: updatePackages();
339: } else if (rootComboBox == e.getSource()) {
340: updatePackages();
341: } else if (packageComboBox == e.getSource()) {
342: }
343: }
344:
345: // DocumentListener implementation -----------------------------------------
346:
347: public void changedUpdate(DocumentEvent e) {
348: fireChange();
349: }
350:
351: public void insertUpdate(DocumentEvent e) {
352: fireChange();
353: }
354:
355: public void removeUpdate(DocumentEvent e) {
356: fireChange();
357: }
358:
359: // Private methods ---------------------------------------------------------
360:
361: private void updatePackages() {
362: SourceGroup g = (SourceGroup) rootComboBox.getSelectedItem();
363: packageComboBox.setModel(PackageView.createListView(g));
364: }
365:
366: void setCombosEnabled(boolean enabled) {
367: packageComboBox.setEnabled(enabled);
368: rootComboBox.setEnabled(enabled);
369: projectsComboBox.setEnabled(enabled);
370: isUpdateReferences.setVisible(!enabled);
371: }
372:
373: void setThisClassVisible(boolean visible) {
374: newNameLabel.setVisible(visible);
375: newNameTextField.setVisible(visible);
376: }
377:
378: public boolean isUpdateReferences() {
379: if (isUpdateReferences.isVisible()
380: && isUpdateReferences.isSelected())
381: return false;
382: return true;
383: }
384:
385: public String getNewName() {
386: return newNameTextField.getText();
387: }
388:
389: private void updateRoots() {
390: Sources sources = ProjectUtils.getSources(project);
391: groups = sources
392: .getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA);
393: if (groups.length == 0) {
394: // XXX why?? This is probably wrong. If the project has no Java groups,
395: // you cannot move anything into it.
396: groups = sources.getSourceGroups(Sources.TYPE_GENERIC);
397: }
398:
399: int preselectedItem = 0;
400: for (int i = 0; i < groups.length; i++) {
401: if (fo != null) {
402: try {
403: if (groups[i].contains(fo)) {
404: preselectedItem = i;
405: }
406: } catch (IllegalArgumentException e) {
407: // XXX this is a poor abuse of exception handling
408: }
409: }
410: }
411:
412: // Setup comboboxes
413: rootComboBox.setModel(new DefaultComboBoxModel(groups));
414: rootComboBox.setSelectedIndex(preselectedItem);
415: }
416:
417: public Component getComponent() {
418: return this;
419: }
420:
421: }
|