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.BorderLayout;
045: import java.awt.Component;
046: import java.awt.Dimension;
047: import java.beans.BeanInfo;
048: import java.text.MessageFormat;
049: import java.util.ArrayList;
050: import java.util.Collection;
051: import java.util.HashMap;
052: import java.util.Map;
053: import javax.swing.Icon;
054: import javax.swing.ImageIcon;
055: import javax.swing.JButton;
056: import javax.swing.JDialog;
057: import javax.swing.JFrame;
058: import javax.swing.JPanel;
059: import javax.swing.event.ChangeListener;
060: import org.netbeans.modules.refactoring.api.RefactoringElement;
061: import org.netbeans.modules.refactoring.spi.ui.RefactoringCustomUI;
062: import org.netbeans.modules.refactoring.spi.ui.TreeElement;
063: import org.netbeans.modules.refactoring.spi.ui.TreeElementFactory;
064: import org.netbeans.modules.refactoring.spi.ui.UI;
065: import org.netbeans.modules.xml.nbprefuse.AnalysisViewer;
066: import org.netbeans.modules.xml.nbprefuse.View;
067: import org.netbeans.modules.xml.nbprefuse.util.GraphUtilities;
068: import org.netbeans.modules.refactoring.api.AbstractRefactoring;
069: import org.netbeans.modules.refactoring.spi.ui.CustomRefactoringPanel;
070: import org.netbeans.modules.refactoring.api.Problem;
071: import org.netbeans.modules.refactoring.api.WhereUsedQuery;
072: import org.netbeans.modules.xml.refactoring.spi.SharedUtils;
073: import org.netbeans.modules.xml.refactoring.ui.GraphHelper;
074: import org.netbeans.modules.xml.refactoring.spi.AnalysisUtilities;
075: import org.netbeans.modules.xml.refactoring.ui.views.WhereUsedView;
076: import org.netbeans.modules.xml.xam.Named;
077: import org.netbeans.modules.xml.xam.Referenceable;
078: import org.openide.nodes.Node;
079: import org.openide.util.HelpCtx;
080: import org.openide.util.NbBundle;
081: import org.openide.util.Utilities;
082: import org.openide.util.lookup.Lookups;
083: import prefuse.data.Graph;
084:
085: /**
086: *
087: * @author Jeri Lockhart
088: */
089: public class WhereUsedQueryUI implements
090: org.netbeans.modules.refactoring.spi.ui.RefactoringUI,
091: RefactoringCustomUI {
092:
093: private String name = ""; //NOI18N
094: private WhereUsedQuery query;
095: private Referenceable ref;
096: private WhereUsedView view;
097:
098: /** Creates a new instance of WhereUsedQueryUI */
099: public WhereUsedQueryUI(Referenceable ref) {
100: this .view = view;
101: this .ref = ref;
102: name = SharedUtils.getName(ref);
103: query = new WhereUsedQuery(Lookups.singleton(ref));
104: }
105:
106: ////////////////////////////////////////////////////////////////////////////
107: /** Start Implementation of RefactoringUI
108: */
109: ////////////////////////////////////////////////////////////////////////////
110: /**
111: * Returns refactoring-specific panel containing input fields for
112: * refactoring parameters. This method is called by ParametersPanel
113: * which is responsible for displaying refactoring parameters dialog.
114: * Name of the panel returned from this method will be used as the dialog
115: * name. This panel can use setPreviewEnabled method of the passed
116: * ParametersPanel to enable/disable Preview button of the refactoring
117: * parameters dialog.
118: *
119: * @param parent ParametersPanel that the returned panel will be displayed in.
120: * @return Refactoring-specific parameters panel.
121: */
122: public CustomRefactoringPanel getPanel(ChangeListener parent) {
123: return new WhereUsedPanel(ref, parent);
124: }
125:
126: /**
127: * Implementation of this method should set the refactoring parameters entered
128: * by user into the refactoring-specific parameters panel (returned from getPanel
129: * method) into the underlying refactoring object.
130: *
131: * @return Chain of problems returned from the underlying refactoring object
132: * when trying to set its parameters.
133: */
134: public Problem setParameters() {
135: return null;
136: }
137:
138: /**
139: * Indicates whether this class represents a real refactoring that changes
140: * code or whether it is just a query (e.g. all usages for a class).
141: *
142: * @return <code>true</code> if the class represents only a query,
143: * <code>false</code> if the class represents a real refactoring.
144: */
145: public boolean isQuery() {
146: return true;
147: }
148:
149: public boolean hasParameters() {
150: return false;
151: }
152:
153: /**
154: * Returns underlying refactoring object.
155: *
156: * @return Underlying refactoring object.
157: */
158: public AbstractRefactoring getRefactoring() {
159: return query;
160: }
161:
162: /**
163: * Returns name of the refactoring.
164: *
165: * @return Refactoring name.
166: */
167:
168: public String getName() {
169: return new MessageFormat(NbBundle.getMessage(
170: WhereUsedQueryUI.class, "LBL_WhereUsed"))
171: .format(new Object[] { name });
172: }
173:
174: public HelpCtx getHelpCtx() {
175: return new HelpCtx(WhereUsedQueryUI.class);
176: }
177:
178: /**
179: * Returns description of the refactoring.
180: *
181: * @return Refactoring description.
182: */
183: public String getDescription() {
184: return new MessageFormat(NbBundle.getMessage(
185: WhereUsedQueryUI.class, "DSC_WhereUsed"))
186: .format(new Object[] { name });
187:
188: }
189:
190: public Problem checkParameters() {
191: return null;
192: }
193:
194: public View getView() {
195: return view;
196: }
197:
198: public Referenceable getTarget() {
199: return ref;
200: }
201:
202: /**
203: * View graph View of Usages
204: */
205: public void setView(View view) {
206: this .view = WhereUsedView.class.cast(view);
207: }
208:
209: ////////////////////////////////////////////////////////////////////////////
210: /** End Implementation of RefactoringUI
211: */
212: ////////////////////////////////////////////////////////////////////////////
213: public Component getCustomComponent(
214: Collection<RefactoringElement> elements) {
215: GraphHelper gh = new GraphHelper(ref);
216: WhereUsedView view = new WhereUsedView(ref);
217: // Map<RefactoringElement, TreeElement> nodes = new HashMap<RefactoringElement,TreeElement>();
218: ArrayList<TreeElement> nodes = new ArrayList<TreeElement>();
219: for (RefactoringElement element : elements) {
220: TreeElement previewNode = TreeElementFactory
221: .getTreeElement(element);
222: if (previewNode != null)
223: nodes.add(previewNode);
224: }
225:
226: Graph graph = gh.loadGraph(nodes);
227: view.setGraph(graph);
228: AnalysisViewer analysisViewer = new AnalysisViewer();
229: analysisViewer.setCurrentView(view);
230: analysisViewer.getPanel().setMinimumSize(new Dimension(10, 10));
231: analysisViewer.getPanel().setPreferredSize(
232: new Dimension(10, 10));
233: view.showView(analysisViewer);
234:
235: return analysisViewer.getPanel();
236:
237: }
238:
239: public Icon getCustomIcon() {
240: return new ImageIcon(
241: Utilities
242: .loadImage("org/netbeans/modules/xml/refactoring/resources/"
243: + "graphical_view_refactoring.png"));
244: }
245:
246: public String getCustomToolTip() {
247: return NbBundle.getMessage(WhereUsedQueryUI.class,
248: "LBL_ShowGraph");
249: }
250:
251: }
|