KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > refactoring > ui > j > spi > ui > RenameRefactoringUI


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.xml.refactoring.ui.j.spi.ui;
21
22 import java.awt.Component JavaDoc;
23 import java.awt.Dimension JavaDoc;
24 import java.text.MessageFormat JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Collection JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.Map JavaDoc;
29 import javax.swing.Icon JavaDoc;
30 import javax.swing.ImageIcon JavaDoc;
31 import javax.swing.event.ChangeListener JavaDoc;
32 import org.netbeans.modules.refactoring.api.RefactoringElement;
33 import org.netbeans.modules.refactoring.api.RenameRefactoring;
34 import org.netbeans.modules.refactoring.spi.ui.RefactoringCustomUI;
35 import org.netbeans.modules.refactoring.spi.ui.TreeElement;
36 import org.netbeans.modules.refactoring.spi.ui.TreeElementFactory;
37 import org.netbeans.modules.xml.nbprefuse.AnalysisViewer;
38 import org.netbeans.modules.xml.nbprefuse.View;
39 import org.netbeans.modules.xml.refactoring.ErrorItem;
40 import org.netbeans.modules.xml.refactoring.RefactorRequest;
41 import org.netbeans.modules.xml.refactoring.RenameRequest;
42 import org.netbeans.modules.xml.refactoring.impl.RefactoringUtil;
43 import org.netbeans.modules.refactoring.api.AbstractRefactoring;
44 import org.netbeans.modules.refactoring.api.Problem;
45 import org.netbeans.modules.refactoring.api.WhereUsedQuery;
46 import org.netbeans.modules.refactoring.spi.ui.CustomRefactoringPanel;
47 import org.netbeans.modules.xml.refactoring.ui.j.ui.RefactoringPanel;
48 import org.netbeans.modules.xml.refactoring.ui.j.ui.RenamePanel;
49 import org.netbeans.modules.xml.refactoring.ui.tree.GraphHelper;
50 import org.netbeans.modules.xml.refactoring.ui.views.WhereUsedView;
51 import org.netbeans.modules.xml.xam.Nameable;
52 import org.netbeans.modules.xml.xam.NamedReferenceable;
53 import org.netbeans.modules.xml.xam.Referenceable;
54 import org.openide.util.HelpCtx;
55 import org.openide.util.NbBundle;
56 import org.openide.util.Utilities;
57 import org.openide.util.lookup.Lookups;
58 import prefuse.data.Graph;
59
60 /**
61  *
62  * @author Jeri Lockhart
63  */

64 public class RenameRefactoringUI implements org.netbeans.modules.refactoring.spi.ui.RefactoringUI, RefactoringCustomUI {
65     
66     private WhereUsedQuery query;
67     private WhereUsedView view;
68     private String JavaDoc newName = "", oldName = ""; //NOI18N
69
private RenamePanel panel;
70     private RenameRequest renameRequest;
71     private Nameable target;
72     private boolean editable;
73     private RenameRefactoring refactoring;
74     
75     /** Creates a new instance of WhereUsedQueryUI */
76     public RenameRefactoringUI(WhereUsedView view, RenameRequest request) {
77         this(view, (Nameable) request.getTarget(), request.getNewName(), true);
78         renameRequest = request;
79         target = (Nameable)request.getTarget();
80         oldName = target.getName();
81         refactoring = new RenameRefactoring(Lookups.singleton(target));
82     }
83
84     public RenameRefactoringUI(WhereUsedView view, Nameable ref) {
85         this(view,ref,ref.getName(),true);
86     }
87     
88     public RenameRefactoringUI(Nameable ref){
89         this.target = ref;
90         oldName = ref.getName();
91         this.editable = true;
92         refactoring = new RenameRefactoring(Lookups.singleton(ref));
93     }
94     
95     /**
96      * Creates a new instance of RenameRefactoringUI.
97      * In addition to whereusedview and namedreferenceable,
98      * new name and editable flag is also provided.
99      * It is called from scn.setname and name property.
100      */

101     public RenameRefactoringUI(WhereUsedView view,
102             Nameable ref, String JavaDoc name, boolean editable) {
103         this.view = view;
104         this.target = ref;
105         oldName = name;
106         this.editable = editable;
107         assert ref instanceof NamedReferenceable;
108         query = new WhereUsedQuery(Lookups.singleton((NamedReferenceable) ref));
109     }
110
111     ////////////////////////////////////////////////////////////////////////////
112
/** Start Implementation of RefactoringUI
113      */

114     ////////////////////////////////////////////////////////////////////////////
115
/**
116      * Returns refactoring-specific panel containing input fields for
117      * refactoring parameters. This method is called by ParametersPanel
118      * which is responsible for displaying refactoring parameters dialog.
119      * Name of the panel returned from this method will be used as the dialog
120      * name. This panel can use setPreviewEnabled method of the passed
121      * ParametersPanel to enable/disable Preview button of the refactoring
122      * parameters dialog.
123      *
124      * @param parent ParametersPanel that the returned panel will be displayed in.
125      * @return Refactoring-specific parameters panel.
126      */

127     public CustomRefactoringPanel getPanel(ChangeListener JavaDoc parent) {
128          if (panel == null) {
129             String JavaDoc name = oldName;
130             panel = new RenamePanel(oldName,
131                     parent, NbBundle.getMessage(RenamePanel.class, "LBL_Rename"),
132                     editable,
133                     false);
134         }
135         return panel;
136     }
137
138     /**
139      * Implementation of this method should set the refactoring parameters entered
140      * by user into the refactoring-specific parameters panel (returned from getPanel
141      * method) into the underlying refactoring object.
142      *
143      * @return Chain of problems returned from the underlying refactoring object
144      * when trying to set its parameters.
145      */

146     public Problem setParameters() {
147         System.out.println("Rename setParameters:: called");
148         newName = panel.getNameValue();
149         System.out.println("thew NEW NAME is " + newName);
150         if (refactoring instanceof RenameRefactoring) {
151             ((RenameRefactoring) refactoring).setNewName(newName);
152         }
153         return refactoring.checkParameters();
154         //return null;
155
}
156
157     /**
158      * Indicates whether this class represents a real refactoring that changes
159      * code or whether it is just a query (e.g. all usages for a class).
160      *
161      * @return <code>true</code> if the class represents only a query,
162      * <code>false</code> if the class represents a real refactoring.
163      */

164     public boolean isQuery() {
165         return false;
166     }
167
168     public boolean hasParameters() {
169         return true;
170     }
171
172     /**
173      * Returns underlying refactoring object.
174      *
175      * @return Underlying refactoring object.
176      */

177     public AbstractRefactoring getRefactoring() {
178         return refactoring;
179     }
180
181     /**
182      * Returns name of the refactoring.
183      *
184      * @return Refactoring name.
185      */

186    
187     public String JavaDoc getName() {
188         return new MessageFormat JavaDoc(NbBundle.getMessage(RefactoringPanel.class, "LBL_Rename")).format (
189                     new Object JavaDoc[] {oldName}
190                 );
191     }
192
193     public HelpCtx getHelpCtx() {
194         return new HelpCtx(RenameRefactoringUI.class);
195     }
196
197     /**
198      * Returns description of the refactoring.
199      *
200      * @return Refactoring description.
201      */

202     public String JavaDoc getDescription() {
203         return new MessageFormat JavaDoc(NbBundle.getMessage(RefactoringPanel.class, "DSC_Rename")).format (
204                     new Object JavaDoc[] {oldName, newName }
205                 );
206         
207     }
208
209     public Problem checkParameters() {
210         /*Problem problem = null;
211         ErrorItem error = RefactoringUtil.precheck(this.getRefactorRequest());
212         if (error != null) {
213             Problem p = new Problem(true, error.getMessage());
214             if (problem == null) {
215                 problem = p;
216             } else {
217                 problem.setNext(p);
218             }
219         }
220         return problem;*/

221         
222         newName = panel.getNameValue();
223         if (refactoring instanceof RenameRefactoring) {
224             ((RenameRefactoring) refactoring).setNewName(newName);
225         }
226         return refactoring.fastCheckParameters();
227     }
228     
229     public View getView() {
230        return view;
231     }
232     
233     public void setView(View view){
234         
235         this.view = WhereUsedView.class.cast(view);
236     }
237     
238     
239     /**
240      *
241      *
242      * @returns RefactorRequest
243      */

244     public RenameRequest getRefactorRequest() {
245         if (renameRequest == null) {
246             renameRequest = new RenameRequest(target, "newName"); //NOI18N
247
}
248         if (getNewName() != null && getNewName().length() > 0) {
249             renameRequest.setNewName(getNewName());
250         }
251         return renameRequest;
252     }
253     
254     ////////////////////////////////////////////////////////////////////////////
255
/** End Implementation of RefactoringUI
256      */

257     ////////////////////////////////////////////////////////////////////////////
258

259     public String JavaDoc getNewName(){
260         if (panel != null){
261             newName = panel.getNameValue();
262         }
263         
264         return newName == null?"":newName; //NOI18N
265
}
266     
267     
268     
269     /**
270      * @param target the Component to be renamed, must be a Nameable
271      *
272      */

273     public void setNameableTarget(Nameable target){
274         this.target = target;
275     }
276     
277     public Nameable getNameableTarget(){
278         return this.target;
279     }
280     
281     public Referenceable getTarget() {
282         assert target instanceof Referenceable;
283         return (Referenceable) target;
284     }
285     
286     
287     public Component JavaDoc getCustomComponent(Collection JavaDoc<RefactoringElement> elements) {
288         System.out.println("getCustomComponent called");
289         WhereUsedView view = new WhereUsedView((Referenceable)target);
290         Referenceable ref = view.getQueryComponent();
291         GraphHelper gh = new GraphHelper(ref);
292         
293         ArrayList JavaDoc<TreeElement> nodes = new ArrayList JavaDoc<TreeElement>();
294         for (RefactoringElement element: elements) {
295                 TreeElement previewNode = TreeElementFactory.getTreeElement(element);
296             if(previewNode != null)
297                 nodes.add(previewNode);
298         }
299         
300         Graph graph = gh.loadGraph(nodes);
301         view.setGraph(graph);
302         AnalysisViewer analysisViewer = new AnalysisViewer();
303         analysisViewer.getPanel().setMinimumSize(new Dimension JavaDoc(10,10));
304         analysisViewer.getPanel().setPreferredSize(new Dimension JavaDoc(10,10));
305         view.showView(analysisViewer);
306        
307        return analysisViewer.getPanel();
308     }
309
310     public Icon JavaDoc getCustomIcon() {
311          return new ImageIcon JavaDoc(
312             Utilities.loadImage(
313             "org/netbeans/modules/refactoring/api/resources/"+
314             "findusages.png"));
315     }
316
317     public String JavaDoc getCustomToolTip() {
318          return NbBundle.getMessage(WhereUsedQueryUI.class, "LBL_ShowGraph");
319     }
320     
321 }
322
Popular Tags