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: * If you wish your version of this file to be governed by only the CDDL
025: * or only the GPL Version 2, indicate your decision by adding
026: * "[Contributor] elects to include this software in this distribution
027: * under the [CDDL or GPL Version 2] license." If you do not indicate a
028: * single choice of license, a recipient has the option to distribute
029: * your version of this file under either the CDDL, the GPL Version 2 or
030: * to extend the choice of license to its licensees as provided above.
031: * However, if you add GPL Version 2 code and therefore, elected the GPL
032: * Version 2 license, then the option applies only if the new code is
033: * made subject to such option by the copyright holder.
034: *
035: * Contributor(s):
036: *
037: * Portions Copyrighted 2008 Sun Microsystems, Inc.
038: */
039: package org.netbeans.modules.cnd.modelui.switcher;
040:
041: import java.io.PrintWriter;
042: import java.util.ArrayList;
043: import java.util.Collection;
044: import javax.swing.Action;
045: import javax.swing.JMenuItem;
046: import org.netbeans.api.progress.ProgressHandle;
047: import org.netbeans.api.progress.ProgressHandleFactory;
048: import org.netbeans.api.project.Project;
049: import org.netbeans.modules.cnd.api.model.CsmFile;
050: import org.netbeans.modules.cnd.api.model.CsmModel;
051: import org.netbeans.modules.cnd.api.model.CsmModelAccessor;
052: import org.netbeans.modules.cnd.api.model.CsmProgressAdapter;
053: import org.netbeans.modules.cnd.api.model.CsmProject;
054: import org.netbeans.modules.cnd.api.project.NativeProject;
055: import org.netbeans.modules.cnd.modelimpl.trace.TraceXRef;
056: import org.openide.nodes.Node;
057: import org.openide.util.HelpCtx;
058: import org.openide.util.NbBundle;
059: import org.openide.util.actions.NodeAction;
060: import org.openide.windows.IOProvider;
061: import org.openide.windows.InputOutput;
062:
063: /**
064: *
065: * @author Vladirmir Voskresensky
066: */
067: public class TestProjectReferencesAction extends NodeAction {
068:
069: private CsmModel model;
070: private static boolean running = false;
071: private final JMenuItem presenter;
072: private final static boolean TEST_XREF = Boolean
073: .getBoolean("test.xref.action"); // NOI18N
074:
075: private enum State {
076:
077: Enabled, Disabled, Indeterminate
078: }
079:
080: public TestProjectReferencesAction() {
081: presenter = new JMenuItem();
082: model = CsmModelAccessor.getModel();
083: org.openide.awt.Actions.connect(presenter, (Action) this , true);
084: }
085:
086: protected boolean enable(Node[] activatedNodes) {
087: if (!TEST_XREF) {
088: return false;
089: }
090: if (model == null) {
091: return false;
092: }
093: if (running) {
094: return false;
095: }
096: Collection<NativeProject> projects = getNativeProjects(getActivatedNodes());
097: if (projects == null) {
098: return false;
099: }
100: return getState(projects) != State.Indeterminate;
101: }
102:
103: public void performAction(final Node[] activatedNodes) {
104: running = true;
105: model.enqueue(new Runnable() {
106:
107: public void run() {
108: try {
109: performAction(getNativeProjects(getActivatedNodes()));
110: } finally {
111: running = false;
112: }
113: }
114: }, "Testing xRef"); //NOI18N
115: }
116:
117: @Override
118: protected boolean asynchronous() {
119: return false;
120: }
121:
122: public String getName() {
123: return NbBundle.getMessage(getClass(),
124: ("CTL_TestProjectReferencesAction")); // NOI18N
125: }
126:
127: public HelpCtx getHelpCtx() {
128: return HelpCtx.DEFAULT_HELP;
129: }
130:
131: @Override
132: public JMenuItem getMenuPresenter() {
133: return getPresenter();
134: }
135:
136: @Override
137: public JMenuItem getPopupPresenter() {
138: return getPresenter();
139: }
140:
141: private JMenuItem getPresenter() {
142: final Collection<NativeProject> projects = getNativeProjects(getActivatedNodes());
143: if (TEST_XREF) {
144: if (projects == null) {
145: this .setEnabled(!running);
146: presenter.setVisible(false);
147: } else {
148: try {
149: presenter.setVisible(true);
150: this .setEnabled(!running);
151: } catch (Throwable thr) {
152: // we are in awt thread;
153: // if exception occurs here, it doesn't allow even to close the project!
154: thr.printStackTrace();
155: this .setEnabled(false);
156: }
157: }
158: } else {
159: presenter.setVisible(false);
160: }
161:
162: return presenter;
163: }
164:
165: /**
166: * Gets the collection of native projects that correspond the given nodes.
167: * @return in the case all nodes correspond to native projects -
168: * collection of native projects; otherwise null
169: */
170: private Collection<NativeProject> getNativeProjects(Node[] nodes) {
171: Collection<NativeProject> projects = new ArrayList<NativeProject>();
172: for (int i = 0; i < nodes.length; i++) {
173: Object o = nodes[i].getValue("Project"); // NOI18N
174: if (!(o instanceof Project)) {
175: return null;
176: }
177: NativeProject nativeProject = (NativeProject) ((Project) o)
178: .getLookup().lookup(NativeProject.class);
179: if (nativeProject == null) {
180: return null;
181: }
182: projects.add(nativeProject);
183: }
184: return projects;
185: }
186:
187: private State getState(Collection<NativeProject> projects) {
188: if (model == null) {
189: return State.Indeterminate;
190: }
191: State state = State.Indeterminate;
192: for (NativeProject p : projects) {
193: State curr = getState(p);
194: if (state == State.Indeterminate) {
195: state = curr;
196: } else {
197: if (state != curr) {
198: return State.Indeterminate;
199: }
200: }
201: }
202: return state;
203: }
204:
205: private State getState(NativeProject p) {
206: CsmProject csmPrj = model.getProject(p);
207: return csmPrj != null && csmPrj.isStable(null) ? State.Enabled
208: : State.Disabled;
209: }
210:
211: private void performAction(Collection<NativeProject> projects) {
212: if (projects != null) {
213: for (NativeProject p : projects) {
214: testProject(p);
215: }
216: }
217: }
218:
219: private void testProject(NativeProject p) {
220: String task = "xRef - " + p.getProjectDisplayName(); // NOI18N
221: InputOutput io = IOProvider.getDefault().getIO(task, false);
222: io.select();
223: final ProgressHandle handle = ProgressHandleFactory
224: .createHandle(task);
225: handle.start();
226: final PrintWriter out = io.getOut();
227: final long[] time = new long[2];
228: time[0] = System.currentTimeMillis();
229: TraceXRef.traceProjectRefsStatistics(p, out,
230: new CsmProgressAdapter() {
231: private int handled = 0;
232:
233: @Override
234: public void projectFilesCounted(CsmProject project,
235: int filesCount) {
236: out.println("Project " + project.getName()
237: + " has " + filesCount + " files"); // NOI18N
238: handle.switchToDeterminate(filesCount);
239: }
240:
241: @Override
242: public void fileParsingStarted(CsmFile file) {
243: handle.progress("Analyzing " + file.getName(),
244: handled++); // NOI18N
245: }
246:
247: @Override
248: public void projectParsingFinished(
249: CsmProject project) {
250: time[1] = System.currentTimeMillis();
251: }
252: });
253: handle.finish();
254: out.println("Analyzing " + p.getProjectDisplayName() + " took "
255: + (time[1] - time[0]) + "ms");
256: }
257: }
|