KickJava   Java API By Example, From Geeks To Geeks.

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


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.RefactoringElementImplementation;
35 import org.netbeans.modules.refactoring.spi.ui.RefactoringCustomUI;
36 import org.netbeans.modules.refactoring.spi.ui.TreeElement;
37 import org.netbeans.modules.refactoring.spi.ui.TreeElementFactory;
38 import org.netbeans.modules.xml.nbprefuse.AnalysisViewer;
39 import org.netbeans.modules.xml.nbprefuse.View;
40 import org.netbeans.modules.xml.refactoring.ErrorItem;
41 import org.netbeans.modules.xml.refactoring.FileRenameRequest;
42 import org.netbeans.modules.xml.refactoring.RefactorRequest;
43 import org.netbeans.modules.xml.refactoring.impl.RefactoringUtil;
44 import org.netbeans.modules.refactoring.api.AbstractRefactoring;
45 import org.netbeans.modules.refactoring.api.Problem;
46 import org.netbeans.modules.refactoring.api.WhereUsedQuery;
47 import org.netbeans.modules.refactoring.spi.ui.CustomRefactoringPanel;
48 import org.netbeans.modules.xml.refactoring.ui.j.ui.RefactoringPanel;
49 import org.netbeans.modules.xml.refactoring.ui.j.ui.RenamePanel;
50 import org.netbeans.modules.xml.refactoring.ui.tree.GraphHelper;
51 import org.netbeans.modules.xml.refactoring.ui.views.WhereUsedView;
52 import org.netbeans.modules.xml.xam.Model;
53 import org.netbeans.modules.xml.xam.NamedReferenceable;
54 import org.netbeans.modules.xml.xam.Referenceable;
55 import org.openide.util.HelpCtx;
56 import org.openide.util.NbBundle;
57 import org.openide.util.Utilities;
58 import org.openide.util.lookup.Lookups;
59 import prefuse.data.Graph;
60
61 /**
62  *
63  * @author Nam Nguyen
64  */

65 public class FileRenameRefactoringUI implements org.netbeans.modules.refactoring.spi.ui.RefactoringUI, RefactoringCustomUI {
66     
67     private WhereUsedQuery query;
68     private WhereUsedView view;
69     private RenamePanel panel;
70     private FileRenameRequest request;
71     String JavaDoc newName;
72     private Model target;
73
74     private RenameRefactoring refactoring;
75     
76     /** Creates a new instance of WhereUsedQueryUI */
77     public FileRenameRefactoringUI(WhereUsedView view, Model target) {
78         this(view, new FileRenameRequest(target, "newFileName")); //NOI18N
79
}
80
81     public FileRenameRefactoringUI(WhereUsedView view, FileRenameRequest request) {
82         this.view = view;
83         this.query = new WhereUsedQuery(Lookups.singleton(request.getTarget()));
84         this.request = request;
85         this.target = request.getTarget();
86         refactoring = new RenameRefactoring(Lookups.singleton(target));
87     }
88     
89     public FileRenameRefactoringUI(Model target){
90         this.target = target;
91         refactoring = new RenameRefactoring(Lookups.singleton(target));
92         
93     }
94     
95     ////////////////////////////////////////////////////////////////////////////
96
/** Start Implementation of RefactoringUI
97      */

98     ////////////////////////////////////////////////////////////////////////////
99
/**
100      * Returns refactoring-specific panel containing input fields for
101      * refactoring parameters. This method is called by ParametersPanel
102      * which is responsible for displaying refactoring parameters dialog.
103      * Name of the panel returned from this method will be used as the dialog
104      * name. This panel can use setPreviewEnabled method of the passed
105      * ParametersPanel to enable/disable Preview button of the refactoring
106      * parameters dialog.
107      *
108      * @param parent ParametersPanel that the returned panel will be displayed in.
109      * @return Refactoring-specific parameters panel.
110      */

111     public CustomRefactoringPanel getPanel(ChangeListener JavaDoc parent) {
112          if (panel == null) {
113             panel = new RenamePanel(request.getOldFileName(),
114                     parent, NbBundle.getMessage(RenamePanel.class, "LBL_FileRename"),
115                     true,
116                     false);
117         }
118         return panel;
119     }
120
121     /**
122      * Implementation of this method should set the refactoring parameters entered
123      * by user into the refactoring-specific parameters panel (returned from getPanel
124      * method) into the underlying refactoring object.
125      *
126      * @return Chain of problems returned from the underlying refactoring object
127      * when trying to set its parameters.
128      */

129     public Problem setParameters() {
130         System.out.println("setParameters:: called");
131         newName = panel.getNameValue();
132         System.out.println("thew NEW NAME is " + newName);
133         if (refactoring instanceof RenameRefactoring) {
134             ((RenameRefactoring) refactoring).setNewName(newName);
135             //request.setNewFileName(newName);
136

137         }
138         return refactoring.checkParameters();
139         //return null;
140
}
141
142     /**
143      * Indicates whether this class represents a real refactoring that changes
144      * code or whether it is just a query (e.g. all usages for a class).
145      *
146      * @return <code>true</code> if the class represents only a query,
147      * <code>false</code> if the class represents a real refactoring.
148      */

149     public boolean isQuery() {
150         return false;
151     }
152
153     public boolean hasParameters() {
154         return true;
155     }
156
157     /**
158      * Returns underlying refactoring object.
159      *
160      * @return Underlying refactoring object.
161      */

162     public AbstractRefactoring getRefactoring() {
163         //return query;
164
return refactoring;
165     }
166
167     /**
168      * Returns name of the refactoring.
169      *
170      * @return Refactoring name.
171      */

172    
173     public String JavaDoc getName() {
174         return new MessageFormat JavaDoc(NbBundle.getMessage(RefactoringPanel.class, "LBL_FileRename")).format (
175                     new Object JavaDoc[] { request.getOldFileName() }
176                 );
177     }
178
179     public HelpCtx getHelpCtx() {
180         return new HelpCtx(FileRenameRefactoringUI.class);
181     }
182
183     /**
184      * Returns description of the refactoring.
185      *
186      * @return Refactoring description.
187      */

188     public String JavaDoc getDescription() {
189         return new MessageFormat JavaDoc(NbBundle.getMessage(RefactoringPanel.class, "DSC_Rename")).format (
190                     new Object JavaDoc[] { request.getOldFileName(), request.getNewFileName() }
191                 );
192         
193     }
194
195     public Problem checkParameters() {
196         /*Problem problem = null;
197         ErrorItem error = RefactoringUtil.precheck(request);
198         if (error != null) {
199             Problem p = new Problem(true, error.getMessage());
200             if (problem == null) {
201                 problem = p;
202             } else {
203                 problem.setNext(p);
204             }
205         }
206         return problem;*/

207         
208         newName = panel.getNameValue();
209         if (refactoring instanceof RenameRefactoring) {
210             ((RenameRefactoring) refactoring).setNewName(newName);
211            // request.setNewFileName(newName);
212
}
213         return refactoring.fastCheckParameters();
214     }
215     
216     public View getView() {
217        return view;
218     }
219     
220     public void setView(View view){
221         
222         this.view = WhereUsedView.class.cast(view);
223     }
224     
225     
226     /**
227      *
228      *
229      * @returns RefactorRequest
230      */

231     public RefactorRequest getRefactorRequest() {
232         if (getNewName() != null && getNewName().length() > 0) {
233             request.setNewFileName(getNewName());
234         }
235         return request;
236         
237     }
238     
239     ////////////////////////////////////////////////////////////////////////////
240
/** End Implementation of RefactoringUI
241      */

242     ////////////////////////////////////////////////////////////////////////////
243

244     public String JavaDoc getNewName(){
245         if (panel != null){
246             request.setNewFileName(panel.getNameValue());
247         }
248         return request.getNewFileName() == null? "" : request.getNewFileName(); //NOI18N
249
}
250
251     public Referenceable getTarget() {
252         return getRefactorRequest().getTargetModel();
253     }
254
255     public Component JavaDoc getCustomComponent(Collection JavaDoc<RefactoringElement> elements) {
256         System.out.println("getCustomComponent called");
257         WhereUsedView view = new WhereUsedView(target);
258         Referenceable ref = view.getQueryComponent();
259         GraphHelper gh = new GraphHelper(ref);
260         
261         ArrayList JavaDoc<TreeElement> nodes = new ArrayList JavaDoc<TreeElement>();
262         for (RefactoringElement element: elements) {
263                 TreeElement previewNode = TreeElementFactory.getTreeElement(element);
264             if(previewNode != null)
265                 nodes.add(previewNode);
266         }
267         
268         Graph graph = gh.loadGraph(nodes);
269         view.setGraph(graph);
270         AnalysisViewer analysisViewer = new AnalysisViewer();
271         analysisViewer.getPanel().setMinimumSize(new Dimension JavaDoc(10,10));
272         analysisViewer.getPanel().setPreferredSize(new Dimension JavaDoc(10,10));
273         view.showView(analysisViewer);
274        
275        return analysisViewer.getPanel();
276     }
277
278     public Icon JavaDoc getCustomIcon() {
279          return new ImageIcon JavaDoc(
280             Utilities.loadImage(
281             "org/netbeans/modules/refactoring/api/resources/"+
282             "findusages.png"));
283     }
284
285     public String JavaDoc getCustomToolTip() {
286          return NbBundle.getMessage(WhereUsedQueryUI.class, "LBL_ShowGraph");
287     }
288     
289 }
290
Popular Tags