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.ui.RefactoringCustomUI;
057: import org.netbeans.modules.refactoring.spi.ui.TreeElement;
058: import org.netbeans.modules.refactoring.spi.ui.TreeElementFactory;
059: import org.netbeans.modules.xml.nbprefuse.AnalysisViewer;
060: import org.netbeans.modules.xml.nbprefuse.View;
061: import org.netbeans.modules.xml.refactoring.ErrorItem;
062: import org.netbeans.modules.xml.refactoring.XMLRefactoringTransaction;
063: import org.netbeans.modules.xml.refactoring.spi.RefactoringUtil;
064: import org.netbeans.modules.refactoring.api.AbstractRefactoring;
065: import org.netbeans.modules.refactoring.api.Problem;
066: import org.netbeans.modules.refactoring.api.WhereUsedQuery;
067: import org.netbeans.modules.refactoring.spi.ui.CustomRefactoringPanel;
068: import org.netbeans.modules.xml.refactoring.ui.GraphHelper;
069: import org.netbeans.modules.xml.refactoring.ui.views.WhereUsedView;
070: import org.netbeans.modules.xml.xam.Nameable;
071: import org.netbeans.modules.xml.xam.NamedReferenceable;
072: import org.netbeans.modules.xml.xam.Referenceable;
073: import org.openide.util.HelpCtx;
074: import org.openide.util.NbBundle;
075: import org.openide.util.Utilities;
076: import org.openide.util.lookup.Lookups;
077: import prefuse.data.Graph;
078:
079: /**
080: *
081: * @author Jeri Lockhart
082: */
083: public class RenameRefactoringUI implements
084: org.netbeans.modules.refactoring.spi.ui.RefactoringUI,
085: RefactoringCustomUI {
086:
087: private WhereUsedQuery query;
088: private WhereUsedView view;
089: private String newName = "", oldName = "", displayName = ""; //NOI18N
090: private RenamePanel panel;
091: private Nameable target;
092: private boolean editable;
093: private RenameRefactoring refactoring;
094:
095: /** Creates a new instance of WhereUsedQueryUI */
096:
097: public RenameRefactoringUI(Nameable ref) {
098: this .target = ref;
099: oldName = ref.getName();
100: displayName = oldName;
101: this .editable = true;
102: refactoring = new RenameRefactoring(Lookups.singleton(ref));
103: XMLRefactoringTransaction transaction = new XMLRefactoringTransaction(
104: (Referenceable) ref, refactoring);
105: refactoring.getContext().add(transaction);
106: //TEMP solution :: ask jbecika if renameRefactring can have a getOldName()
107: refactoring.getContext().add(oldName);
108: }
109:
110: public RenameRefactoringUI(Nameable ref, String newName) {
111: this .target = ref;
112: oldName = ref.getName();
113: if (displayName != null)
114: displayName = newName;
115: else
116: displayName = oldName;
117: this .editable = true;
118: refactoring = new RenameRefactoring(Lookups.singleton(ref));
119: XMLRefactoringTransaction transaction = new XMLRefactoringTransaction(
120: (Referenceable) ref, refactoring);
121: refactoring.getContext().add(transaction);
122: //TEMP solution :: ask jbecika if renameRefactring can have a getOldName()
123: refactoring.getContext().add(oldName);
124: }
125:
126: /**
127: * Creates a new instance of RenameRefactoringUI.
128: * In addition to whereusedview and namedreferenceable,
129: * new name and editable flag is also provided.
130: * It is called from scn.setname and name property.
131: */
132: public RenameRefactoringUI(WhereUsedView view, Nameable ref,
133: String name, boolean editable) {
134: this .view = view;
135: this .target = ref;
136: oldName = name;
137: this .editable = editable;
138: assert ref instanceof NamedReferenceable;
139: query = new WhereUsedQuery(Lookups
140: .singleton((NamedReferenceable) ref));
141: }
142:
143: ////////////////////////////////////////////////////////////////////////////
144: /** Start Implementation of RefactoringUI
145: */
146: ////////////////////////////////////////////////////////////////////////////
147: /**
148: * Returns refactoring-specific panel containing input fields for
149: * refactoring parameters. This method is called by ParametersPanel
150: * which is responsible for displaying refactoring parameters dialog.
151: * Name of the panel returned from this method will be used as the dialog
152: * name. This panel can use setPreviewEnabled method of the passed
153: * ParametersPanel to enable/disable Preview button of the refactoring
154: * parameters dialog.
155: *
156: * @param parent ParametersPanel that the returned panel will be displayed in.
157: * @return Refactoring-specific parameters panel.
158: */
159: public CustomRefactoringPanel getPanel(ChangeListener parent) {
160: if (panel == null) {
161: String name = oldName;
162: panel = new RenamePanel(displayName, parent, NbBundle
163: .getMessage(RenamePanel.class, "LBL_Rename"),
164: editable, false);
165: }
166: return panel;
167: }
168:
169: /**
170: * Implementation of this method should set the refactoring parameters entered
171: * by user into the refactoring-specific parameters panel (returned from getPanel
172: * method) into the underlying refactoring object.
173: *
174: * @return Chain of problems returned from the underlying refactoring object
175: * when trying to set its parameters.
176: */
177: public Problem setParameters() {
178: // System.out.println("Rename setParameters:: called");
179: newName = panel.getNameValue();
180: // System.out.println("thew NEW NAME is " + newName);
181: if (refactoring instanceof RenameRefactoring) {
182: ((RenameRefactoring) refactoring).setNewName(newName);
183: }
184: return refactoring.checkParameters();
185: //return null;
186: }
187:
188: /**
189: * Indicates whether this class represents a real refactoring that changes
190: * code or whether it is just a query (e.g. all usages for a class).
191: *
192: * @return <code>true</code> if the class represents only a query,
193: * <code>false</code> if the class represents a real refactoring.
194: */
195: public boolean isQuery() {
196: return false;
197: }
198:
199: public boolean hasParameters() {
200: return true;
201: }
202:
203: /**
204: * Returns underlying refactoring object.
205: *
206: * @return Underlying refactoring object.
207: */
208: public AbstractRefactoring getRefactoring() {
209: return refactoring;
210: }
211:
212: /**
213: * Returns name of the refactoring.
214: *
215: * @return Refactoring name.
216: */
217:
218: public String getName() {
219: return new MessageFormat(NbBundle.getMessage(
220: RenameRefactoringUI.class, "LBL_Rename"))
221: .format(new Object[] { oldName });
222: }
223:
224: public HelpCtx getHelpCtx() {
225: return new HelpCtx(RenameRefactoringUI.class);
226: }
227:
228: /**
229: * Returns description of the refactoring.
230: *
231: * @return Refactoring description.
232: */
233: public String getDescription() {
234: return new MessageFormat(NbBundle.getMessage(
235: RenameRefactoringUI.class, "DSC_Rename"))
236: .format(new Object[] { oldName, newName });
237:
238: }
239:
240: public Problem checkParameters() {
241: /*Problem problem = null;
242: ErrorItem error = RefactoringUtil.precheck(this.getRefactorRequest());
243: if (error != null) {
244: Problem p = new Problem(true, error.getMessage());
245: if (problem == null) {
246: problem = p;
247: } else {
248: problem.setNext(p);
249: }
250: }
251: return problem;*/
252:
253: newName = panel.getNameValue();
254: if (refactoring instanceof RenameRefactoring) {
255: ((RenameRefactoring) refactoring).setNewName(newName);
256: }
257: return refactoring.fastCheckParameters();
258: }
259:
260: public View getView() {
261: return view;
262: }
263:
264: public void setView(View view) {
265:
266: this .view = WhereUsedView.class.cast(view);
267: }
268:
269: ////////////////////////////////////////////////////////////////////////////
270: /** End Implementation of RefactoringUI
271: */
272: ////////////////////////////////////////////////////////////////////////////
273: public String getNewName() {
274: if (panel != null) {
275: newName = panel.getNameValue();
276: }
277:
278: return newName == null ? "" : newName; //NOI18N
279: }
280:
281: /**
282: * @param target the Component to be renamed, must be a Nameable
283: *
284: */
285: public void setNameableTarget(Nameable target) {
286: this .target = target;
287: }
288:
289: public Nameable getNameableTarget() {
290: return this .target;
291: }
292:
293: public Referenceable getTarget() {
294: assert target instanceof Referenceable;
295: return (Referenceable) target;
296: }
297:
298: public Component getCustomComponent(
299: Collection<RefactoringElement> elements) {
300: WhereUsedView view = new WhereUsedView((Referenceable) target);
301: GraphHelper gh = new GraphHelper((Referenceable) target);
302:
303: ArrayList<TreeElement> nodes = new ArrayList<TreeElement>();
304: for (RefactoringElement element : elements) {
305: TreeElement previewNode = TreeElementFactory
306: .getTreeElement(element);
307: if (previewNode != null)
308: nodes.add(previewNode);
309: }
310:
311: Graph graph = gh.loadGraph(nodes);
312: view.setGraph(graph);
313: AnalysisViewer analysisViewer = new AnalysisViewer();
314: analysisViewer.setCurrentView(view);
315: analysisViewer.getPanel().setMinimumSize(new Dimension(10, 10));
316: analysisViewer.getPanel().setPreferredSize(
317: new Dimension(10, 10));
318: view.showView(analysisViewer);
319:
320: return analysisViewer.getPanel();
321: }
322:
323: public Icon getCustomIcon() {
324: return new ImageIcon(
325: Utilities
326: .loadImage("org/netbeans/modules/xml/refactoring/resources/"
327: + "graphical_view_refactoring.png"));
328: }
329:
330: public String getCustomToolTip() {
331: return NbBundle.getMessage(WhereUsedQueryUI.class,
332: "LBL_ShowGraph");
333: }
334:
335: }
|