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.Dimension;
046: import java.text.MessageFormat;
047: import java.util.ArrayList;
048: import java.util.Collection;
049: import java.util.HashMap;
050: import java.util.Map;
051: import javax.swing.Icon;
052: import javax.swing.ImageIcon;
053: import javax.swing.event.ChangeListener;
054: import org.netbeans.modules.refactoring.api.RefactoringElement;
055: import org.netbeans.modules.refactoring.api.RenameRefactoring;
056: import org.netbeans.modules.refactoring.spi.RefactoringElementImplementation;
057: import org.netbeans.modules.refactoring.spi.ui.RefactoringCustomUI;
058: import org.netbeans.modules.refactoring.spi.ui.TreeElement;
059: import org.netbeans.modules.refactoring.spi.ui.TreeElementFactory;
060: import org.netbeans.modules.xml.nbprefuse.AnalysisViewer;
061: import org.netbeans.modules.xml.nbprefuse.View;
062: import org.netbeans.modules.xml.refactoring.ErrorItem;
063: import org.netbeans.modules.xml.refactoring.XMLRefactoringTransaction;
064: import org.netbeans.modules.xml.refactoring.spi.RefactoringUtil;
065: import org.netbeans.modules.refactoring.api.AbstractRefactoring;
066: import org.netbeans.modules.refactoring.api.Problem;
067: import org.netbeans.modules.refactoring.api.WhereUsedQuery;
068: import org.netbeans.modules.refactoring.spi.ui.CustomRefactoringPanel;
069: import org.netbeans.modules.xml.refactoring.ui.GraphHelper;
070: import org.netbeans.modules.xml.refactoring.ui.views.WhereUsedView;
071: import org.netbeans.modules.xml.xam.Model;
072: import org.netbeans.modules.xml.xam.NamedReferenceable;
073: import org.netbeans.modules.xml.xam.Referenceable;
074: import org.openide.filesystems.FileObject;
075: import org.openide.util.HelpCtx;
076: import org.openide.util.NbBundle;
077: import org.openide.util.Utilities;
078: import org.openide.util.lookup.Lookups;
079: import prefuse.data.Graph;
080:
081: /**
082: *
083: * @author Nam Nguyen
084: */
085: public class FileRenameRefactoringUI implements
086: org.netbeans.modules.refactoring.spi.ui.RefactoringUI,
087: RefactoringCustomUI {
088:
089: private WhereUsedQuery query;
090: private WhereUsedView view;
091: private RenamePanel panel;
092: String newName;
093: private Model target;
094: String oldFileName, displayName;
095:
096: private RenameRefactoring refactoring;
097:
098: public FileRenameRefactoringUI(Model target, String newName) {
099: this .target = target;
100: refactoring = new RenameRefactoring(Lookups.singleton(target));
101: oldFileName = (target.getModelSource().getLookup()
102: .lookup(FileObject.class)).getName();
103: if (newName != null)
104: displayName = newName;
105: else
106: displayName = oldFileName;
107: XMLRefactoringTransaction transaction = new XMLRefactoringTransaction(
108: (Referenceable) target, refactoring);
109: refactoring.getContext().add(transaction);
110: //TEMP solution :: ask jbecika if renameRefactoring can have a getOldName()
111: //have filed issue#98842 on Refactoring API..till then use context obj
112: refactoring.getContext().add(oldFileName);
113:
114: }
115:
116: public FileRenameRefactoringUI(Model target) {
117: this .target = target;
118: refactoring = new RenameRefactoring(Lookups.singleton(target));
119: oldFileName = (target.getModelSource().getLookup()
120: .lookup(FileObject.class)).getName();
121: displayName = oldFileName;
122: XMLRefactoringTransaction transaction = new XMLRefactoringTransaction(
123: (Referenceable) target, refactoring);
124: refactoring.getContext().add(transaction);
125: //TEMP solution :: ask jbecika if renameRefactoring can have a getOldName()
126: //have filed issue#98842 on Refactoring API..till then use context obj
127: refactoring.getContext().add(oldFileName);
128:
129: }
130:
131: ////////////////////////////////////////////////////////////////////////////
132: /** Start Implementation of RefactoringUI
133: */
134: ////////////////////////////////////////////////////////////////////////////
135: /**
136: * Returns refactoring-specific panel containing input fields for
137: * refactoring parameters. This method is called by ParametersPanel
138: * which is responsible for displaying refactoring parameters dialog.
139: * Name of the panel returned from this method will be used as the dialog
140: * name. This panel can use setPreviewEnabled method of the passed
141: * ParametersPanel to enable/disable Preview button of the refactoring
142: * parameters dialog.
143: *
144: * @param parent ParametersPanel that the returned panel will be displayed in.
145: * @return Refactoring-specific parameters panel.
146: */
147: public CustomRefactoringPanel getPanel(ChangeListener parent) {
148: if (panel == null) {
149: panel = new RenamePanel(displayName, parent, NbBundle
150: .getMessage(RenamePanel.class, "LBL_FileRename"),
151: true, false);
152: }
153: return panel;
154: }
155:
156: /**
157: * Implementation of this method should set the refactoring parameters entered
158: * by user into the refactoring-specific parameters panel (returned from getPanel
159: * method) into the underlying refactoring object.
160: *
161: * @return Chain of problems returned from the underlying refactoring object
162: * when trying to set its parameters.
163: */
164: public Problem setParameters() {
165: newName = panel.getNameValue();
166: if (refactoring instanceof RenameRefactoring) {
167: ((RenameRefactoring) refactoring).setNewName(newName);
168: }
169: return refactoring.checkParameters();
170:
171: }
172:
173: /**
174: * Indicates whether this class represents a real refactoring that changes
175: * code or whether it is just a query (e.g. all usages for a class).
176: *
177: * @return <code>true</code> if the class represents only a query,
178: * <code>false</code> if the class represents a real refactoring.
179: */
180: public boolean isQuery() {
181: return false;
182: }
183:
184: public boolean hasParameters() {
185: return true;
186: }
187:
188: /**
189: * Returns underlying refactoring object.
190: *
191: * @return Underlying refactoring object.
192: */
193: public AbstractRefactoring getRefactoring() {
194: return refactoring;
195: }
196:
197: /**
198: * Returns name of the refactoring.
199: *
200: * @return Refactoring name.
201: */
202:
203: public String getName() {
204: return new MessageFormat(NbBundle.getMessage(
205: FileRenameRefactoringUI.class, "LBL_FileRename"))
206: .format(new Object[] { oldFileName });
207: }
208:
209: public HelpCtx getHelpCtx() {
210: return new HelpCtx(FileRenameRefactoringUI.class);
211: }
212:
213: /**
214: * Returns description of the refactoring.
215: *
216: * @return Refactoring description.
217: */
218: public String getDescription() {
219: return new MessageFormat(NbBundle.getMessage(
220: FileRenameRefactoringUI.class, "DSC_Rename"))
221: .format(new Object[] { oldFileName,
222: refactoring.getNewName() });
223:
224: }
225:
226: public Problem checkParameters() {
227: newName = panel.getNameValue();
228: if (refactoring instanceof RenameRefactoring) {
229: ((RenameRefactoring) refactoring).setNewName(newName);
230: }
231: return refactoring.fastCheckParameters();
232: }
233:
234: public View getView() {
235: return view;
236: }
237:
238: public void setView(View view) {
239:
240: this .view = WhereUsedView.class.cast(view);
241: }
242:
243: ////////////////////////////////////////////////////////////////////////////
244: /** End Implementation of RefactoringUI
245: */
246: ////////////////////////////////////////////////////////////////////////////
247:
248: public Referenceable getTarget() {
249: return target;
250: }
251:
252: public Component getCustomComponent(
253: Collection<RefactoringElement> elements) {
254: WhereUsedView view = new WhereUsedView(target);
255: GraphHelper gh = new GraphHelper(target);
256:
257: ArrayList<TreeElement> nodes = new ArrayList<TreeElement>();
258: for (RefactoringElement element : elements) {
259: TreeElement previewNode = TreeElementFactory
260: .getTreeElement(element);
261: if (previewNode != null)
262: nodes.add(previewNode);
263: }
264:
265: Graph graph = gh.loadGraph(nodes);
266: view.setGraph(graph);
267: AnalysisViewer analysisViewer = new AnalysisViewer();
268: analysisViewer.setCurrentView(view);
269: analysisViewer.getPanel().setMinimumSize(new Dimension(10, 10));
270: analysisViewer.getPanel().setPreferredSize(
271: new Dimension(10, 10));
272: view.showView(analysisViewer);
273:
274: return analysisViewer.getPanel();
275: }
276:
277: public Icon getCustomIcon() {
278: return new ImageIcon(
279: Utilities
280: .loadImage("org/netbeans/modules/xml/refactoring/resources/"
281: + "graphical_view_refactoring.png"));
282: }
283:
284: public String getCustomToolTip() {
285: return NbBundle.getMessage(WhereUsedQueryUI.class,
286: "LBL_ShowGraph");
287: }
288:
289: }
|