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-2007 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.cnd.modelui.switcher;
043:
044: import java.awt.event.ActionEvent;
045: import java.awt.event.ActionListener;
046: import java.util.ArrayList;
047: import java.util.Collection;
048: import javax.swing.JCheckBoxMenuItem;
049: import javax.swing.JMenuItem;
050: import org.netbeans.api.project.Project;
051: import org.netbeans.modules.cnd.api.project.NativeProject;
052: import org.netbeans.modules.cnd.modelimpl.csm.core.ModelImpl;
053: import org.netbeans.modules.cnd.api.model.CsmModel;
054: import org.netbeans.modules.cnd.api.model.CsmModelAccessor;
055: import org.openide.nodes.Node;
056: import org.openide.util.HelpCtx;
057: import org.openide.util.NbBundle;
058: import org.openide.util.actions.NodeAction;
059:
060: /**
061: *
062: * @author Vladimir Kvashin
063: */
064: public class SwitchProjectAction extends NodeAction {
065:
066: private JCheckBoxMenuItem presenter;
067: private ModelImpl model;
068: private static boolean running = false;
069:
070: private enum State {
071: Enabled, Disabled, Indeterminate
072: }
073:
074: public SwitchProjectAction() {
075: presenter = new JCheckBoxMenuItem(getName());
076: presenter.addActionListener(new ActionListener() {
077: public void actionPerformed(ActionEvent e) {
078: onActionPerformed();
079: }
080: });
081: CsmModel model = CsmModelAccessor.getModel();
082: if (model instanceof ModelImpl) {
083: this .model = (ModelImpl) model;
084: }
085: }
086:
087: public String getName() {
088: return NbBundle.getMessage(getClass(),
089: ("CTL_SwitchProjectAction")); // NOI18N
090: }
091:
092: public HelpCtx getHelpCtx() {
093: return HelpCtx.DEFAULT_HELP;
094: }
095:
096: public JMenuItem getMenuPresenter() {
097: return getPresenter();
098: }
099:
100: public JMenuItem getPopupPresenter() {
101: return getPresenter();
102: }
103:
104: private JMenuItem getPresenter() {
105: final Collection<NativeProject> projects = getNativeProjects(getActivatedNodes());
106: if (projects == null) {
107: presenter.setEnabled(!running);
108: presenter.setSelected(false);
109: } else {
110: try {
111: State state = getState(projects);
112: if (state == State.Indeterminate) {
113: presenter.setEnabled(!running);
114: presenter.setSelected(false);
115: } else {
116: presenter.setEnabled(!running);
117: presenter.setSelected(state == State.Enabled);
118: }
119: } catch (Throwable thr) {
120: // we are in awt thread;
121: // if exception occurs here, it doesn't allow even to close the project!
122: thr.printStackTrace();
123: presenter.setEnabled(false);
124: presenter.setSelected(true);
125: }
126: }
127: return presenter;
128: }
129:
130: /**
131: * Gets the collection of native projects that correspond the given nodes.
132: * @return in the case all nodes correspond to native projects -
133: * collection of native projects; otherwise null
134: */
135: private Collection<NativeProject> getNativeProjects(Node[] nodes) {
136: Collection<NativeProject> projects = new ArrayList<NativeProject>();
137: for (int i = 0; i < nodes.length; i++) {
138: Object o = nodes[i].getValue("Project"); // NOI18N
139: if (!(o instanceof Project)) {
140: return null;
141: }
142: NativeProject nativeProject = (NativeProject) ((Project) o)
143: .getLookup().lookup(NativeProject.class);
144: if (nativeProject == null) {
145: return null;
146: }
147: projects.add(nativeProject);
148: }
149: return projects;
150: }
151:
152: private State getState(Collection<NativeProject> projects) {
153: if (model == null) {
154: return State.Indeterminate;
155: }
156: State state = State.Indeterminate;
157: for (NativeProject p : projects) {
158: State curr = getState(p);
159: if (state == State.Indeterminate) {
160: state = curr;
161: } else {
162: if (state != curr) {
163: return State.Indeterminate;
164: }
165: }
166: }
167: return state;
168: }
169:
170: private State getState(NativeProject p) {
171: return model.isProjectEnabled(p) ? State.Enabled
172: : State.Disabled;
173: }
174:
175: protected boolean enable(Node[] activatedNodes) {
176: if (model == null) {
177: return false;
178: }
179: if (running) {
180: return false;
181: }
182: Collection<NativeProject> projects = getNativeProjects(getActivatedNodes());
183: if (projects == null) {
184: return false;
185: }
186: return getState(projects) != State.Indeterminate;
187: }
188:
189: private void onActionPerformed() {
190: performAction(getActivatedNodes());
191: }
192:
193: /** Actually nobody but us call this since we have a presenter. */
194: public void performAction(final Node[] activatedNodes) {
195: running = true;
196: model.enqueue(new Runnable() {
197: public void run() {
198: try {
199: performAction(getNativeProjects(getActivatedNodes()));
200: } finally {
201: running = false;
202: }
203: }
204: }, "Switching code model ON/OFF"); //NOI18N
205: }
206:
207: private void performAction(Collection<NativeProject> projects) {
208: if (projects != null) {
209: State state = getState(projects);
210: switch (state) {
211: case Enabled:
212: for (NativeProject p : projects) {
213: model.disableProject(p);
214: }
215: break;
216: case Disabled:
217: for (NativeProject p : projects) {
218: model.enableProject(p);
219: }
220: break;
221: }
222: }
223: }
224:
225: protected boolean asynchronous() {
226: return false;
227: }
228: }
|