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.xml.refactoring.ui;
043:
044: import java.awt.Component;
045: import java.awt.event.ActionEvent;
046: import java.awt.event.ActionListener;
047: import java.util.ArrayList;
048: import java.util.Arrays;
049: import java.util.Iterator;
050: import java.util.List;
051: import java.util.Set;
052: import javax.swing.DefaultComboBoxModel;
053: import javax.swing.JPanel;
054: import javax.swing.JTextField;
055: import javax.swing.ListCellRenderer;
056: import javax.swing.event.ChangeListener;
057: import javax.swing.event.DocumentEvent;
058: import javax.swing.event.DocumentListener;
059: import org.netbeans.api.project.FileOwnerQuery;
060: import org.netbeans.api.project.Project;
061: import org.netbeans.api.project.ProjectUtils;
062: import org.netbeans.api.project.SourceGroup;
063: import org.netbeans.api.project.Sources;
064: import org.netbeans.api.project.ui.OpenProjects;
065: import org.netbeans.modules.refactoring.spi.ui.CustomRefactoringPanel;
066: import org.netbeans.modules.xml.refactoring.spi.SharedUtils;
067: import org.netbeans.modules.xml.refactoring.ui.views.PackageView;
068: import org.netbeans.spi.project.SubprojectProvider;
069: import org.openide.filesystems.FileObject;
070:
071: /**
072: * @author Jan Becicka
073: */
074: public class CopyPanel extends JPanel implements ActionListener,
075: DocumentListener, CustomRefactoringPanel {
076:
077: private static final ListCellRenderer GROUP_CELL_RENDERER = new MoveFilePanel.GroupCellRenderer();
078: private static final ListCellRenderer PROJECT_CELL_RENDERER = new MoveFilePanel.ProjectCellRenderer();
079:
080: private Project project;
081: private ChangeListener parent;
082: private FileObject fo;
083: private SourceGroup[] groups;
084: private String newName;
085:
086: public CopyPanel(final ChangeListener parent, String title,
087: String startPackage, FileObject f, String newName) {
088: setName(title);
089: this .fo = f;
090: this .parent = parent;
091: this .newName = newName;
092: initComponents();
093: setCombosEnabled(true);
094: setThisClassVisible(true);
095:
096: rootComboBox.setRenderer(GROUP_CELL_RENDERER);
097: packageComboBox.setRenderer(PackageView.listRenderer());
098: projectsComboBox.setRenderer(PROJECT_CELL_RENDERER);
099:
100: rootComboBox.addActionListener(this );
101: packageComboBox.addActionListener(this );
102: projectsComboBox.addActionListener(this );
103:
104: Object textField = packageComboBox.getEditor()
105: .getEditorComponent();
106: if (textField instanceof JTextField) {
107: ((JTextField) textField).getDocument().addDocumentListener(
108: this );
109: }
110: newNameTextField.getDocument().addDocumentListener(this );
111:
112: project = fo != null ? FileOwnerQuery.getOwner(fo)
113: : OpenProjects.getDefault().getOpenProjects()[0];
114: initValues(startPackage);
115: }
116:
117: private boolean initialized = false;
118:
119: public void initialize() {
120: if (initialized)
121: return;
122: newName = newName + 1;
123: newNameTextField.setText(newName);
124: newNameTextField.setSelectionStart(0);
125: newNameTextField.setSelectionEnd(newNameTextField.getText()
126: .length());
127: initialized = true;
128: }
129:
130: public void initValues(String preselectedFolder) {
131:
132: Project openProjects[] = OpenProjects.getDefault()
133: .getOpenProjects();
134: List<Project> op = Arrays.asList(openProjects);
135: java.util.List projectRoots = new java.util.ArrayList();
136: SubprojectProvider provider = (SubprojectProvider) project
137: .getLookup().lookup(SubprojectProvider.class);
138: Set refProjects = provider.getSubprojects();
139: Iterator it = refProjects.iterator();
140: projectRoots.add(project);
141:
142: while (it.hasNext()) {
143: Object o = it.next();
144: Project refPrj = (Project) o;
145: if (op.contains(refPrj))
146: projectRoots.add(refPrj);
147: }
148: Project[] roots = (Project[]) projectRoots
149: .toArray(new Project[projectRoots.size()]);
150: DefaultComboBoxModel projectsModel = new DefaultComboBoxModel(
151: roots);
152: projectsComboBox.setModel(projectsModel);
153: projectsComboBox.setSelectedItem(project);
154:
155: updateRoots();
156: updatePackages();
157: if (preselectedFolder != null) {
158: packageComboBox.setSelectedItem(preselectedFolder);
159: }
160: // Determine the extension
161: }
162:
163: public void requestFocus() {
164: newNameTextField.requestFocus();
165: }
166:
167: public FileObject getRootFolder() {
168: return ((SourceGroup) rootComboBox.getSelectedItem())
169: .getRootFolder();
170: }
171:
172: public String getPackageName() {
173: String packageName = packageComboBox.getEditor().getItem()
174: .toString();
175: return packageName.replace('.', '/'); // NOI18N
176: }
177:
178: private void fireChange() {
179: parent.stateChanged(null);
180: }
181:
182: /** This method is called from within the constructor to
183: * initialize the form.
184: * WARNING: Do NOT modify this code. The content of this method is
185: * always regenerated by the Form Editor.
186: */
187: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
188: private void initComponents() {
189: java.awt.GridBagConstraints gridBagConstraints;
190:
191: labelProject = new javax.swing.JLabel();
192: projectsComboBox = new javax.swing.JComboBox();
193: labelLocation = new javax.swing.JLabel();
194: rootComboBox = new javax.swing.JComboBox();
195: labelPackage = new javax.swing.JLabel();
196: packageComboBox = new javax.swing.JComboBox();
197: bottomPanel = new javax.swing.JPanel();
198: newNameLabel = new javax.swing.JLabel();
199: newNameTextField = new javax.swing.JTextField();
200: isUpdateReferences = new javax.swing.JCheckBox();
201:
202: setLayout(new java.awt.GridBagLayout());
203:
204: labelProject.setLabelFor(projectsComboBox);
205: org.openide.awt.Mnemonics.setLocalizedText(labelProject,
206: org.openide.util.NbBundle.getMessage(CopyPanel.class,
207: "LBL_Project")); // NOI18N
208: gridBagConstraints = new java.awt.GridBagConstraints();
209: gridBagConstraints.gridx = 0;
210: gridBagConstraints.gridy = 2;
211: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
212: gridBagConstraints.insets = new java.awt.Insets(0, 0, 6, 0);
213: add(labelProject, gridBagConstraints);
214: gridBagConstraints = new java.awt.GridBagConstraints();
215: gridBagConstraints.gridx = 1;
216: gridBagConstraints.gridy = 2;
217: gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
218: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
219: gridBagConstraints.weightx = 1.0;
220: gridBagConstraints.insets = new java.awt.Insets(0, 6, 6, 0);
221: add(projectsComboBox, gridBagConstraints);
222: projectsComboBox.getAccessibleContext()
223: .setAccessibleDescription(null);
224:
225: labelLocation.setLabelFor(rootComboBox);
226: org.openide.awt.Mnemonics.setLocalizedText(labelLocation,
227: org.openide.util.NbBundle.getMessage(CopyPanel.class,
228: "LBL_Location")); // NOI18N
229: gridBagConstraints = new java.awt.GridBagConstraints();
230: gridBagConstraints.gridx = 0;
231: gridBagConstraints.gridy = 3;
232: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
233: gridBagConstraints.insets = new java.awt.Insets(0, 0, 6, 0);
234: add(labelLocation, gridBagConstraints);
235: gridBagConstraints = new java.awt.GridBagConstraints();
236: gridBagConstraints.gridx = 1;
237: gridBagConstraints.gridy = 3;
238: gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
239: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
240: gridBagConstraints.weightx = 1.0;
241: gridBagConstraints.insets = new java.awt.Insets(0, 6, 6, 0);
242: add(rootComboBox, gridBagConstraints);
243: rootComboBox.getAccessibleContext().setAccessibleDescription(
244: null);
245:
246: labelPackage.setLabelFor(packageComboBox);
247: org.openide.awt.Mnemonics.setLocalizedText(labelPackage,
248: org.openide.util.NbBundle.getMessage(CopyPanel.class,
249: "LBL_ToPackage")); // NOI18N
250: gridBagConstraints = new java.awt.GridBagConstraints();
251: gridBagConstraints.gridx = 0;
252: gridBagConstraints.gridy = 4;
253: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
254: gridBagConstraints.insets = new java.awt.Insets(0, 0, 12, 0);
255: add(labelPackage, gridBagConstraints);
256:
257: packageComboBox.setEditable(true);
258: gridBagConstraints = new java.awt.GridBagConstraints();
259: gridBagConstraints.gridx = 1;
260: gridBagConstraints.gridy = 4;
261: gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
262: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
263: gridBagConstraints.weightx = 1.0;
264: gridBagConstraints.insets = new java.awt.Insets(0, 6, 12, 0);
265: add(packageComboBox, gridBagConstraints);
266: gridBagConstraints = new java.awt.GridBagConstraints();
267: gridBagConstraints.gridx = 0;
268: gridBagConstraints.gridy = 5;
269: gridBagConstraints.gridwidth = 2;
270: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
271: gridBagConstraints.weightx = 1.0;
272: gridBagConstraints.weighty = 1.0;
273: add(bottomPanel, gridBagConstraints);
274:
275: org.openide.awt.Mnemonics.setLocalizedText(newNameLabel,
276: org.openide.util.NbBundle.getMessage(CopyPanel.class,
277: "LBL_NewName")); // NOI18N
278: gridBagConstraints = new java.awt.GridBagConstraints();
279: gridBagConstraints.gridx = 0;
280: gridBagConstraints.gridy = 1;
281: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
282: gridBagConstraints.insets = new java.awt.Insets(0, 0, 6, 0);
283: add(newNameLabel, gridBagConstraints);
284:
285: newNameTextField.setText("NewClass");
286: gridBagConstraints = new java.awt.GridBagConstraints();
287: gridBagConstraints.gridx = 1;
288: gridBagConstraints.gridy = 1;
289: gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
290: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
291: gridBagConstraints.weightx = 1.0;
292: gridBagConstraints.insets = new java.awt.Insets(0, 6, 6, 0);
293: add(newNameTextField, gridBagConstraints);
294:
295: org.openide.awt.Mnemonics.setLocalizedText(isUpdateReferences,
296: org.openide.util.NbBundle.getMessage(CopyPanel.class,
297: "LBL_CopyWithoutRefactoring")); // NOI18N
298: isUpdateReferences.setBorder(javax.swing.BorderFactory
299: .createEmptyBorder(4, 4, 0, 4));
300: isUpdateReferences.setMargin(new java.awt.Insets(2, 2, 0, 2));
301: gridBagConstraints = new java.awt.GridBagConstraints();
302: gridBagConstraints.gridx = 0;
303: gridBagConstraints.gridy = 6;
304: gridBagConstraints.gridwidth = 2;
305: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
306: add(isUpdateReferences, gridBagConstraints);
307: }// </editor-fold>//GEN-END:initComponents
308:
309: // Variables declaration - do not modify//GEN-BEGIN:variables
310: protected javax.swing.JPanel bottomPanel;
311: private javax.swing.JCheckBox isUpdateReferences;
312: private javax.swing.JLabel labelLocation;
313: private javax.swing.JLabel labelPackage;
314: private javax.swing.JLabel labelProject;
315: private javax.swing.JLabel newNameLabel;
316: private javax.swing.JTextField newNameTextField;
317: private javax.swing.JComboBox packageComboBox;
318: private javax.swing.JComboBox projectsComboBox;
319: private javax.swing.JComboBox rootComboBox;
320:
321: // End of variables declaration//GEN-END:variables
322:
323: // ActionListener implementation -------------------------------------------
324:
325: public void actionPerformed(ActionEvent e) {
326: if (projectsComboBox == e.getSource()) {
327: project = (Project) projectsComboBox.getSelectedItem();
328: updateRoots();
329: updatePackages();
330: } else if (rootComboBox == e.getSource()) {
331: updatePackages();
332: } else if (packageComboBox == e.getSource()) {
333: }
334: }
335:
336: // DocumentListener implementation -----------------------------------------
337:
338: public void changedUpdate(DocumentEvent e) {
339: fireChange();
340: }
341:
342: public void insertUpdate(DocumentEvent e) {
343: fireChange();
344: }
345:
346: public void removeUpdate(DocumentEvent e) {
347: fireChange();
348: }
349:
350: // Private methods ---------------------------------------------------------
351:
352: private void updatePackages() {
353: SourceGroup g = (SourceGroup) rootComboBox.getSelectedItem();
354: packageComboBox.setModel(PackageView.createListView(g));
355: }
356:
357: void setCombosEnabled(boolean enabled) {
358: packageComboBox.setEnabled(enabled);
359: rootComboBox.setEnabled(enabled);
360: projectsComboBox.setEnabled(enabled);
361: isUpdateReferences.setVisible(!enabled);
362: }
363:
364: void setThisClassVisible(boolean visible) {
365: newNameLabel.setVisible(visible);
366: newNameTextField.setVisible(visible);
367: }
368:
369: public boolean isUpdateReferences() {
370: if (isUpdateReferences.isVisible()
371: && isUpdateReferences.isSelected())
372: return false;
373: return true;
374: }
375:
376: public String getNewName() {
377: return newNameTextField.getText();
378: }
379:
380: private void updateRoots() {
381: Sources sources = ProjectUtils.getSources(project);
382: List<SourceGroup> result = new ArrayList<SourceGroup>();
383: List<String> sourceGroupList = SharedUtils.getSourceGroups();
384: for (String type : sourceGroupList) {
385: SourceGroup[] srcGrps = sources.getSourceGroups(type);
386: if (srcGrps != null) {
387: for (SourceGroup srcGrp : srcGrps)
388: result.add(srcGrp);
389: }
390: }
391: //groups = sources.getSourceGroups(Sources.TYPE_GENERIC);
392: groups = result.toArray(new SourceGroup[result.size()]);
393: int preselectedItem = 0;
394: for (int i = 0; i < groups.length; i++) {
395: if (fo != null) {
396: try {
397: if (groups[i].contains(fo)) {
398: preselectedItem = i;
399: }
400: } catch (IllegalArgumentException e) {
401: // XXX this is a poor abuse of exception handling
402: }
403: }
404: }
405:
406: // Setup comboboxes
407: rootComboBox.setModel(new DefaultComboBoxModel(groups));
408: rootComboBox.setSelectedIndex(preselectedItem);
409: }
410:
411: public Component getComponent() {
412: return this;
413: }
414:
415: }
|