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.xml.refactoring.ui;
042:
043: import java.awt.Component;
044: import java.awt.Dimension;
045: import java.io.IOException;
046: import java.net.MalformedURLException;
047: import java.net.URL;
048: import java.net.URLEncoder;
049: import java.text.MessageFormat;
050: import java.util.ArrayList;
051: import java.util.Collection;
052: import java.util.Collections;
053: import javax.swing.Icon;
054: import javax.swing.ImageIcon;
055: import javax.swing.event.ChangeListener;
056: import org.netbeans.api.project.FileOwnerQuery;
057: import org.netbeans.api.project.Project;
058: import org.netbeans.modules.refactoring.api.AbstractRefactoring;
059: import org.netbeans.modules.refactoring.api.MoveRefactoring;
060: import org.netbeans.modules.refactoring.api.Problem;
061: import org.netbeans.modules.refactoring.api.RefactoringElement;
062: import org.netbeans.modules.refactoring.spi.ui.CustomRefactoringPanel;
063: import org.netbeans.modules.refactoring.spi.ui.RefactoringUI;
064: import org.netbeans.modules.refactoring.spi.ui.RefactoringCustomUI;
065: import org.netbeans.modules.refactoring.spi.ui.TreeElement;
066: import org.netbeans.modules.refactoring.spi.ui.TreeElementFactory;
067: import org.netbeans.modules.xml.nbprefuse.AnalysisViewer;
068: import org.netbeans.modules.xml.refactoring.XMLRefactoringTransaction;
069: import org.netbeans.modules.xml.refactoring.ui.views.WhereUsedView;
070: import org.netbeans.modules.xml.xam.Model;
071: import org.netbeans.modules.xml.xam.Referenceable;
072: import org.openide.filesystems.FileObject;
073: import org.openide.filesystems.URLMapper;
074: import org.openide.util.HelpCtx;
075: import org.openide.util.NbBundle;
076: import org.openide.util.lookup.Lookups;
077: import prefuse.data.Graph;
078: import org.openide.util.Utilities;
079:
080: public class MoveRefactoringUI implements
081: org.netbeans.modules.refactoring.spi.ui.RefactoringUI,
082: RefactoringCustomUI {
083:
084: private MoveFilePanel panel;
085: private MoveRefactoring refactoring;
086: private FileObject targetFile;
087: private String oldFileName;
088: private Project srcProject;
089: private Model target;
090: private boolean disable;
091: private String targetPkgName = "";
092:
093: public MoveRefactoringUI(Model target) {
094: this .target = target;
095: refactoring = new MoveRefactoring(Lookups.singleton(target));
096: XMLRefactoringTransaction transaction = new XMLRefactoringTransaction(
097: (Referenceable) target, refactoring);
098: refactoring.getContext().add(transaction);
099: targetFile = target.getModelSource().getLookup().lookup(
100: FileObject.class);
101: srcProject = FileOwnerQuery.getOwner(targetFile);
102: oldFileName = targetFile.getName();
103:
104: //save the original folder name
105: try {
106: URL url = targetFile.getParent().getURL();
107: refactoring.getContext().add(new URL(url.toExternalForm()));
108: } catch (Exception ex) {
109: ex.printStackTrace();
110: }
111: }
112:
113: /**
114: * Returns name of the refactoring.
115: *
116: * @return Refactoring name.
117: */
118:
119: public String getName() {
120: return new MessageFormat(NbBundle.getMessage(
121: FileRenameRefactoringUI.class, "LBL_FileMove"))
122: .format(new Object[] { oldFileName });
123: }
124:
125: public String getDescription() {
126: return new MessageFormat(NbBundle.getMessage(
127: FileRenameRefactoringUI.class, "DSC_FileMove"))
128: .format(new Object[] { oldFileName, packageName() });
129: }
130:
131: public boolean isQuery() {
132: return false;
133: }
134:
135: public CustomRefactoringPanel getPanel(ChangeListener parent) {
136: if (panel == null) {
137: String pkgName = targetFile.getParent().getName();
138: panel = new MoveFilePanel(parent, pkgName,
139: new MessageFormat(NbBundle.getMessage(
140: MoveFilePanel.class, "LBL_FileMove"))
141: .format(new Object[] { oldFileName }),
142: targetFile);
143:
144: panel.setCombosEnabled(!disable);
145: }
146: return panel;
147: }
148:
149: private Problem setParameters(boolean checkOnly) {
150: if (panel == null)
151: return null;
152: targetPkgName = panel.getPackageName();
153:
154: URL url = URLMapper.findURL(panel.getRootFolder(),
155: URLMapper.EXTERNAL);
156: try {
157: refactoring.setTarget(Lookups.singleton(new URL(url
158: .toExternalForm()
159: + URLEncoder.encode(panel.getPackageName().replace(
160: '.', '/')))));
161: } catch (MalformedURLException ex) {
162: ex.printStackTrace();
163: }
164: if (checkOnly) {
165: return refactoring.fastCheckParameters();
166: } else {
167: return refactoring.checkParameters();
168: }
169: }
170:
171: public Problem checkParameters() {
172: return setParameters(true);
173: }
174:
175: public Problem setParameters() {
176: return setParameters(false);
177: }
178:
179: public AbstractRefactoring getRefactoring() {
180: return refactoring;
181: }
182:
183: public boolean hasParameters() {
184: return true;
185: }
186:
187: public HelpCtx getHelpCtx() {
188: return new HelpCtx(MoveRefactoringUI.class);
189: }
190:
191: private String packageName() {
192: return targetPkgName.trim().length() == 0 ? NbBundle
193: .getMessage(MoveRefactoringUI.class,
194: "LBL_DefaultPackage") : targetPkgName.trim();
195: }
196:
197: public Component getCustomComponent(
198: Collection<RefactoringElement> elements) {
199: WhereUsedView view = new WhereUsedView(target);
200: GraphHelper gh = new GraphHelper(target);
201:
202: ArrayList<TreeElement> nodes = new ArrayList<TreeElement>();
203: for (RefactoringElement element : elements) {
204: TreeElement previewNode = TreeElementFactory
205: .getTreeElement(element);
206: if (previewNode != null)
207: nodes.add(previewNode);
208: }
209:
210: Graph graph = gh.loadGraph(nodes);
211: view.setGraph(graph);
212: AnalysisViewer analysisViewer = new AnalysisViewer();
213: analysisViewer.setCurrentView(view);
214: analysisViewer.getPanel().setMinimumSize(new Dimension(10, 10));
215: analysisViewer.getPanel().setPreferredSize(
216: new Dimension(10, 10));
217: view.showView(analysisViewer);
218:
219: return analysisViewer.getPanel();
220: }
221:
222: public Icon getCustomIcon() {
223: return new ImageIcon(
224: Utilities
225: .loadImage("org/netbeans/modules/xml/refactoring/resources/"
226: + "graphical_view_refactoring.png"));
227: }
228:
229: public String getCustomToolTip() {
230: return NbBundle.getMessage(WhereUsedQueryUI.class,
231: "LBL_ShowGraph");
232: }
233: }
|