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.beans.beaninfo;
043:
044: import java.awt.Dialog;
045: import java.util.concurrent.Future;
046: import javax.lang.model.element.TypeElement;
047: import org.netbeans.api.java.source.CompilationController;
048: import org.netbeans.api.java.source.CompilationInfo;
049: import org.netbeans.api.java.source.JavaSource;
050: import org.netbeans.modules.beans.PatternAnalyser;
051: import org.openide.filesystems.FileObject;
052: import org.openide.loaders.DataObject;
053: import org.openide.nodes.Node;
054: import org.openide.util.Exceptions;
055: import org.openide.util.HelpCtx;
056: import org.openide.util.NbBundle;
057: import org.openide.util.RequestProcessor;
058: import org.openide.util.RequestProcessor.Task;
059: import org.openide.util.actions.NodeAction;
060:
061: /**
062: * Generate BI action.
063: *
064: * @author Petr Hrebejk
065: */
066: public final class GenerateBeanInfoAction extends NodeAction implements
067: java.awt.event.ActionListener {
068: private Dialog biDialog;
069:
070: /** generated Serialized Version UID */
071: //static final long serialVersionUID = 1391479985940417455L;
072: // The dialog for BeanInfo generation
073: static final long serialVersionUID = -4937492476805017833L;
074:
075: /** Human presentable name of the action. This should be
076: * presented as an item in a menu.
077: * @return the name of the action
078: */
079: public String getName() {
080: return getString("CTL_GENBI_MenuItem");
081: }
082:
083: /** The action's icon location.
084: * @return the action's icon location
085: */
086: @Override
087: protected String iconResource() {
088: return null;
089: //return "/org/netbeans/modules/javadoc/resources/searchDoc.gif"; // NOI18N
090: }
091:
092: /** Help context where to find more about the action.
093: * @return the help context for this action
094: */
095: public HelpCtx getHelpCtx() {
096: return HelpCtx.DEFAULT_HELP;
097: }
098:
099: protected boolean enable(Node[] activatedNodes) {
100: if (activatedNodes.length != 1) {
101: return false;
102: } else {
103: FileObject fo = findFileObject(activatedNodes[0]);
104: return fo != null && JavaSource.forFileObject(fo) != null
105: && !fo.getName().endsWith("BeanInfo"); //NOI18N
106: }
107: }
108:
109: /** This method is called by one of the "invokers" as a result of
110: * some user's action that should lead to actual "performing" of the action.
111: * This default implementation calls the assigned actionPerformer if it
112: * is not null otherwise the action is ignored.
113: */
114: public void performAction(final Node[] nodes) {
115:
116: if (nodes.length != 1)
117: return;
118:
119: // Open the diaog for bean info generation
120:
121: final BiPanel biPanel = new BiPanel();
122:
123: // Get pattern analyser & bean info and create BiAnalyser & BiNode
124:
125: FileObject javaFile = findFileObject(nodes[0]);
126: final BeanInfoWorker performer = new BeanInfoWorker(javaFile,
127: biPanel);
128: performer.analyzePatterns();
129:
130: performer.waitFinished();
131: performer.bia.openSource();
132: }
133:
134: @Override
135: protected boolean asynchronous() {
136: return false;
137: }
138:
139: private void initAccessibility() {
140: biDialog.getAccessibleContext().setAccessibleDescription(
141: getString("ACSD_BeanInfoEditorDialog"));
142: }
143:
144: private static FileObject findFileObject(Node n) {
145: DataObject dobj = n.getCookie(DataObject.class);
146: return dobj != null ? dobj.getPrimaryFile() : null;
147: }
148:
149: static String getString(String key) {
150: return NbBundle.getBundle(
151: "org.netbeans.modules.beans.beaninfo.Bundle")
152: .getString(key);
153: }
154:
155: static final class BeanInfoWorker implements Runnable,
156: org.netbeans.api.java.source.Task<CompilationController> {
157:
158: private final BiPanel biPanel;
159: private final FileObject javaFile;
160: private boolean isCancelled = false;
161: private Node biNode;
162: private BiAnalyser bia;
163: private Task task;
164: private int state = 0;
165:
166: public BeanInfoWorker(FileObject javaFile, BiPanel biPanel) {
167: this .javaFile = javaFile;
168: this .biPanel = biPanel;
169: }
170:
171: public Task analyzePatterns() {
172: checkState(0);
173: task = RequestProcessor.getDefault().post(this );
174: return task;
175: }
176:
177: public void updateUI() {
178: waitFinished();
179: checkState(1);
180: state = 2;
181: fillBiPanel();
182: }
183:
184: public void generateSources() {
185: waitFinished();
186: checkState(2);
187: state = 3;
188: // task.schedule(0);
189: run();
190: }
191:
192: public boolean isCancelled() {
193: return isCancelled;
194: }
195:
196: public void waitFinished() {
197: if (task == null) {
198: throw new IllegalStateException();
199: }
200: task.waitFinished();
201: }
202:
203: public boolean isModelModified() {
204: waitFinished();
205: return bia.isModified();
206: }
207:
208: public void run() {
209: if (isCancelled) {
210: return;
211: }
212: switch (state) {
213: case 0:
214: analyzePatternsImpl();
215: break;
216: case 2:
217: fillBiPanel();
218: break;
219: case 3:
220: generateSourcesImpl();
221: break;
222: }
223: }
224:
225: public void run(CompilationController javac) throws Exception {
226: if (isCancelled) {
227: return;
228: }
229: javac.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED);
230: switch (state) {
231: case 1:
232: analyzePatternsImpl(javac);
233: break;
234: }
235: }
236:
237: private void analyzePatternsImpl(CompilationInfo javac) {
238: checkState(1);
239: String clsname = javaFile.getName();
240: TypeElement clselm = null;
241: for (TypeElement top : javac.getTopLevelElements()) {
242: if (clsname.contentEquals(top.getSimpleName())) {
243: clselm = top;
244: }
245: }
246:
247: PatternAnalyser pa = new PatternAnalyser(javaFile, null);
248: pa.analyzeAll(javac, clselm);
249: // XXX analyze also superclasses here
250: try {
251: bia = new BiAnalyser(pa, javac);
252: } catch (Exception ex) {
253: isCancelled = true;
254: Exceptions.printStackTrace(ex);
255: }
256: }
257:
258: private void analyzePatternsImpl() {
259: if (javaFile == null) {
260: isCancelled = true;
261: return;
262: }
263: checkState(0);
264: state = 1;
265: try {
266: Future<Void> f = JavaSource.forFileObject(javaFile)
267: .runWhenScanFinished(this , true);
268: f.get();
269: isCancelled = f.isCancelled();
270: } catch (Exception ex) {
271: isCancelled = true;
272: Exceptions.printStackTrace(ex);
273: }
274: }
275:
276: private void fillBiPanel() {
277: biNode = BiNode.createBiNode(bia);
278: biPanel.setContext(biNode);
279: biPanel.expandAll();
280: }
281:
282: private void generateSourcesImpl() {
283: if (!isCancelled()) {
284: bia.regenerateSource();
285: }
286: }
287:
288: private void checkState(int expected) {
289: if (state != expected) {
290: throw new IllegalStateException();
291: }
292: }
293: }
294: }
|