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: package org.netbeans.modules.refactoring.java.ui;
042:
043: import java.awt.BorderLayout;
044: import java.io.IOException;
045: import java.io.UnsupportedEncodingException;
046: import java.net.MalformedURLException;
047: import java.net.URL;
048: import java.net.URLEncoder;
049: import java.util.ArrayList;
050: import java.util.LinkedList;
051: import java.util.List;
052: import java.util.Set;
053: import java.util.Vector;
054: import java.util.logging.Logger;
055: import javax.swing.JLabel;
056: import javax.swing.JList;
057: import javax.swing.JScrollPane;
058: import javax.swing.border.EmptyBorder;
059: import javax.swing.event.ChangeListener;
060: import org.netbeans.api.java.classpath.ClassPath;
061: import org.netbeans.api.queries.VisibilityQuery;
062: import org.netbeans.modules.refactoring.api.AbstractRefactoring;
063: import org.netbeans.modules.refactoring.api.MoveRefactoring;
064: import org.netbeans.modules.refactoring.api.Problem;
065: import org.netbeans.modules.refactoring.java.RetoucheUtils;
066: import org.netbeans.modules.refactoring.spi.ui.CustomRefactoringPanel;
067: import org.netbeans.modules.refactoring.spi.ui.RefactoringUI;
068: import org.netbeans.modules.refactoring.spi.ui.RefactoringUIBypass;
069: import org.openide.awt.Mnemonics;
070: import org.openide.explorer.view.NodeRenderer;
071: import org.openide.filesystems.FileObject;
072: import org.openide.filesystems.URLMapper;
073: import org.openide.loaders.DataFolder;
074: import org.openide.loaders.DataObject;
075: import org.openide.loaders.DataObjectNotFoundException;
076: import org.openide.nodes.Node;
077: import org.openide.util.Exceptions;
078: import org.openide.util.HelpCtx;
079: import org.openide.util.NbBundle;
080: import org.openide.util.datatransfer.PasteType;
081: import org.openide.util.lookup.Lookups;
082:
083: /**
084: * @author Jan Becicka
085: */
086: public class MoveClassesUI implements RefactoringUI,
087: RefactoringUIBypass {
088:
089: private List<FileObject> resources;
090: private Set<FileObject> javaObjects;
091: private MovePanel panel;
092: private MoveRefactoring refactoring;
093: private String targetPkgName = "";
094: private boolean disable;
095: private FileObject targetFolder;
096: private PasteType pasteType;
097:
098: static final String getString(String key) {
099: return NbBundle.getMessage(MoveClassUI.class, key);
100: }
101:
102: public MoveClassesUI(Set<FileObject> javaObjects) {
103: this (javaObjects, null, null);
104: }
105:
106: public MoveClassesUI(Set<FileObject> javaObjects,
107: FileObject targetFolder, PasteType paste) {
108: this .disable = targetFolder != null;
109: this .targetFolder = targetFolder;
110: this .javaObjects = javaObjects;
111: this .pasteType = paste;
112: if (!disable) {
113: resources = new ArrayList(javaObjects);
114: }
115: }
116:
117: public String getName() {
118: return getString("LBL_MoveClasses");
119: }
120:
121: public String getDescription() {
122: return getName();
123: }
124:
125: public boolean isQuery() {
126: return false;
127: }
128:
129: public CustomRefactoringPanel getPanel(ChangeListener parent) {
130: if (panel == null) {
131: String pkgName = null;
132: if (targetFolder != null) {
133: ClassPath cp = ClassPath.getClassPath(targetFolder,
134: ClassPath.SOURCE);
135: if (cp != null)
136: pkgName = cp.getResourceName(targetFolder, '.',
137: false);
138: }
139: panel = new MovePanel(parent, pkgName != null ? pkgName
140: : getDOPackageName(((FileObject) javaObjects
141: .iterator().next()).getParent()),
142: getString("LBL_MoveClassesHeadline"));
143: }
144: return panel;
145: }
146:
147: // private static String getResPackageName(Resource res) {
148: // String name = res.getName();
149: // if ( name.indexOf('/') == -1 )
150: // return "";
151: // return name.substring(0, name.lastIndexOf('/')).replace('/','.');
152: // }
153: private static String getDOPackageName(FileObject f) {
154: ClassPath cp = ClassPath.getClassPath(f, ClassPath.SOURCE);
155: if (cp != null) {
156: return cp.getResourceName(f, '.', false);
157: } else {
158: Logger.getLogger("org.netbeans.modules.refactoring.java")
159: .info("Cannot find classpath for " + f.getPath());
160: return f.getName();
161: }
162: }
163:
164: private String packageName() {
165: return targetPkgName.trim().length() == 0 ? getString("LBL_DefaultPackage")
166: : targetPkgName.trim();
167: }
168:
169: private Problem setParameters(boolean checkOnly) {
170: if (panel == null)
171: return null;
172: URL url = URLMapper.findURL(panel.getRootFolder(),
173: URLMapper.EXTERNAL);
174: try {
175: refactoring.setTarget(Lookups.singleton(new URL(url
176: .toExternalForm()
177: + URLEncoder.encode(panel.getPackageName().replace(
178: '.', '/'), "utf-8")))); // NOI18N
179: } catch (UnsupportedEncodingException ex) {
180: Exceptions.printStackTrace(ex);
181: } catch (MalformedURLException ex) {
182: Exceptions.printStackTrace(ex);
183: }
184: if (checkOnly) {
185: return refactoring.fastCheckParameters();
186: } else {
187: return refactoring.checkParameters();
188: }
189: }
190:
191: public Problem checkParameters() {
192: return setParameters(true);
193: }
194:
195: public Problem setParameters() {
196: return setParameters(false);
197: }
198:
199: public AbstractRefactoring getRefactoring() {
200: if (refactoring == null) {
201: if (disable) {
202: refactoring = new MoveRefactoring(Lookups
203: .fixed(javaObjects.toArray()));
204: refactoring.getContext().add(
205: RetoucheUtils.getClasspathInfoFor(javaObjects
206: .toArray(new FileObject[javaObjects
207: .size()])));
208: } else {
209: refactoring = new MoveRefactoring(Lookups
210: .fixed(resources.toArray()));
211: refactoring.getContext().add(
212: RetoucheUtils.getClasspathInfoFor(resources
213: .toArray(new FileObject[resources
214: .size()])));
215: }
216: }
217: return refactoring;
218: }
219:
220: private final Vector getNodes() {
221: Vector<Node> result = new Vector(javaObjects.size());
222: LinkedList<FileObject> q = new LinkedList<FileObject>(
223: javaObjects);
224: while (!q.isEmpty()) {
225: FileObject f = q.removeFirst();
226: if (!VisibilityQuery.getDefault().isVisible(f))
227: continue;
228: DataObject d = null;
229: try {
230: d = DataObject.find(f);
231: } catch (DataObjectNotFoundException ex) {
232: ex.printStackTrace();
233: }
234: if (d instanceof DataFolder) {
235: for (DataObject o : ((DataFolder) d).getChildren()) {
236: q.addLast(o.getPrimaryFile());
237: }
238: } else {
239: result.add(d.getNodeDelegate());
240: }
241: }
242: return result;
243: }
244:
245: public boolean hasParameters() {
246: return true;
247: }
248:
249: public HelpCtx getHelpCtx() {
250: return new HelpCtx(MoveClassesUI.class);
251: }
252:
253: public boolean isRefactoringBypassRequired() {
254: return !panel.isUpdateReferences();
255: }
256:
257: public void doRefactoringBypass() throws IOException {
258: pasteType.paste();
259: }
260:
261: // MovePanel ...............................................................
262: class MovePanel extends MoveClassPanel {
263: public MovePanel(final ChangeListener parent,
264: String startPackage, String headLine) {
265: super (parent, startPackage, headLine,
266: targetFolder != null ? targetFolder
267: : (FileObject) javaObjects.iterator()
268: .next());
269: setCombosEnabled(!disable);
270: JList list = new JList(getNodes());
271: list.setCellRenderer(new NodeRenderer());
272: list.setVisibleRowCount(5);
273: JScrollPane pane = new JScrollPane(list);
274: bottomPanel.setBorder(new EmptyBorder(8, 0, 0, 0));
275: bottomPanel.setLayout(new BorderLayout());
276: bottomPanel.add(pane, BorderLayout.CENTER);
277: JLabel listOf = new JLabel();
278: Mnemonics.setLocalizedText(listOf, NbBundle.getMessage(
279: MoveClassesUI.class, "LBL_ListOfClasses"));
280: bottomPanel.add(listOf, BorderLayout.NORTH);
281: }
282: }
283: }
|