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.apisupport.project.ui;
043:
044: import java.awt.EventQueue;
045: import java.awt.Image;
046: import java.awt.event.ActionEvent;
047: import java.beans.PropertyChangeEvent;
048: import java.beans.PropertyChangeListener;
049: import java.io.IOException;
050: import java.util.Collections;
051: import java.util.SortedSet;
052: import java.util.TreeSet;
053: import javax.swing.AbstractAction;
054: import javax.swing.Action;
055: import javax.swing.event.ChangeEvent;
056: import javax.swing.event.ChangeListener;
057: import org.netbeans.api.project.Project;
058: import org.netbeans.api.project.ProjectInformation;
059: import org.netbeans.api.project.ProjectManager;
060: import org.netbeans.api.project.ProjectUtils;
061: import org.netbeans.api.project.ui.OpenProjects;
062: import org.netbeans.modules.apisupport.project.NbModuleProject;
063: import org.netbeans.modules.apisupport.project.Util;
064: import org.netbeans.modules.apisupport.project.suite.SuiteProject;
065: import org.netbeans.modules.apisupport.project.ui.customizer.SuiteUtils;
066: import org.netbeans.modules.apisupport.project.ui.wizard.NewNbModuleWizardIterator;
067: import org.netbeans.spi.project.SubprojectProvider;
068: import org.netbeans.spi.project.ui.support.NodeFactory;
069: import org.netbeans.spi.project.ui.support.NodeFactorySupport;
070: import org.netbeans.spi.project.ui.support.NodeList;
071: import org.openide.DialogDisplayer;
072: import org.openide.ErrorManager;
073: import org.openide.NotifyDescriptor;
074: import org.openide.awt.StatusDisplayer;
075: import org.openide.nodes.AbstractNode;
076: import org.openide.nodes.Children;
077: import org.openide.nodes.Node;
078: import org.openide.util.HelpCtx;
079: import org.openide.util.NbBundle;
080: import org.openide.util.RequestProcessor;
081: import org.openide.util.Utilities;
082: import org.openide.util.actions.CookieAction;
083: import org.openide.util.actions.NodeAction;
084: import org.openide.util.lookup.Lookups;
085: import org.openide.windows.WindowManager;
086:
087: /**
088: * @author mkleint
089: */
090: public class ModulesNodeFactory implements NodeFactory {
091:
092: public ModulesNodeFactory() {
093: }
094:
095: public NodeList createNodes(Project p) {
096: SuiteProject prj = p.getLookup().lookup(SuiteProject.class);
097: assert prj != null;
098: return NodeFactorySupport.fixedNodeList(new ModulesNode(prj));
099: }
100:
101: private static String getMessage(final String key) {
102: return NbBundle.getMessage(SuiteLogicalView.class, key);
103: }
104:
105: /** Represent <em>Modules</em> node in the Suite Logical View. */
106: static final class ModulesNode extends AbstractNode {
107:
108: private SuiteProject suite;
109:
110: ModulesNode(final SuiteProject suite) {
111: super (new ModuleChildren(suite));
112: this .suite = suite;
113: setName("modules"); // NOI18N
114: setDisplayName(getMessage("CTL_Modules"));
115: }
116:
117: public @Override
118: Action[] getActions(boolean context) {
119: return new Action[] {
120: new AddNewSuiteComponentAction(suite),
121: new AddNewLibraryWrapperAction(suite),
122: new AddSuiteComponentAction(suite), };
123: }
124:
125: private Image getIcon(boolean opened) {
126: Image badge = Utilities
127: .loadImage(
128: "org/netbeans/modules/apisupport/project/suite/resources/module-badge.png",
129: true);
130: return Utilities.mergeImages(UIUtil
131: .getTreeFolderIcon(opened), badge, 9, 9);
132: }
133:
134: public @Override
135: Image getIcon(int type) {
136: return getIcon(false);
137: }
138:
139: public @Override
140: Image getOpenedIcon(int type) {
141: return getIcon(true);
142: }
143:
144: static final class ModuleChildren extends
145: Children.Keys<NbModuleProject> implements
146: ChangeListener {
147:
148: private final SuiteProject suite;
149:
150: public ModuleChildren(SuiteProject suite) {
151: suite.getLookup().lookup(SubprojectProvider.class)
152: .addChangeListener(this );
153: this .suite = suite;
154: }
155:
156: protected @Override
157: void addNotify() {
158: updateKeys();
159: }
160:
161: private void updateKeys() {
162: // e.g.(?) Explorer view under Children.MUTEX subsequently calls e.g.
163: // SuiteProject$Info.getSimpleName() which acquires ProjectManager.mutex(). And
164: // since this method might be called under ProjectManager.mutex() write access
165: // and updateKeys() --> setKeys() in turn calls Children.MUTEX write access,
166: // deadlock is here, so preventing it... (also got this under read access)
167: EventQueue.invokeLater(new Runnable() {
168: public void run() {
169: // #70112: sort them.
170: SortedSet<NbModuleProject> subModules = new TreeSet<NbModuleProject>(
171: Util.projectDisplayNameComparator());
172: subModules.addAll(SuiteUtils
173: .getSubProjects(suite));
174: setKeys(subModules);
175: }
176: });
177: }
178:
179: protected @Override
180: void removeNotify() {
181: suite.getLookup().lookup(SubprojectProvider.class)
182: .removeChangeListener(this );
183: setKeys(Collections.<NbModuleProject> emptySet());
184: }
185:
186: protected Node[] createNodes(NbModuleProject p) {
187: return new Node[] { new SuiteComponentNode(p) };
188: }
189:
190: public void stateChanged(ChangeEvent ev) {
191: updateKeys();
192: }
193:
194: }
195:
196: }
197:
198: private static final class AddSuiteComponentAction extends
199: AbstractAction {
200:
201: private final SuiteProject suite;
202:
203: public AddSuiteComponentAction(final SuiteProject suite) {
204: super (getMessage("CTL_AddModule"));
205: this .suite = suite;
206: }
207:
208: public void actionPerformed(ActionEvent evt) {
209: NbModuleProject project = UIUtil.chooseSuiteComponent(
210: WindowManager.getDefault().getMainWindow(), suite);
211: if (project != null) {
212: if (!SuiteUtils.contains(suite, project)) {
213: try {
214: SuiteUtils.addModule(suite, project);
215: ProjectManager.getDefault().saveProject(suite);
216: } catch (IOException ex) {
217: ErrorManager.getDefault().notify(ex);
218: }
219: } else {
220: DialogDisplayer
221: .getDefault()
222: .notify(
223: new NotifyDescriptor.Message(
224: NbBundle
225: .getMessage(
226: SuiteLogicalView.class,
227: "MSG_SuiteAlreadyContainsCNB",
228: project
229: .getCodeNameBase())));
230: }
231: }
232: }
233:
234: }
235:
236: private static final class AddNewSuiteComponentAction extends
237: AbstractAction {
238:
239: private final SuiteProject suite;
240:
241: public AddNewSuiteComponentAction(final SuiteProject suite) {
242: super (getMessage("CTL_AddNewModule"));
243: this .suite = suite;
244: }
245:
246: public void actionPerformed(ActionEvent evt) {
247: NewNbModuleWizardIterator iterator = NewNbModuleWizardIterator
248: .createSuiteComponentIterator(suite);
249: UIUtil.runProjectWizard(iterator, "CTL_NewModuleProject"); // NOI18N
250: }
251:
252: }
253:
254: static final class AddNewLibraryWrapperAction extends
255: AbstractAction {
256:
257: private final Project suiteProvider;
258: private final NbModuleProject target;
259:
260: public AddNewLibraryWrapperAction(final Project suiteProvider,
261: final NbModuleProject target) {
262: super (getMessage("CTL_AddNewLibrary"));
263: this .suiteProvider = suiteProvider;
264: this .target = target;
265: }
266:
267: public AddNewLibraryWrapperAction(final Project suiteProvider) {
268: this (suiteProvider, null);
269: }
270:
271: public void actionPerformed(ActionEvent evt) {
272: NbModuleProject project = UIUtil
273: .runLibraryWrapperWizard(suiteProvider);
274: if (project != null && target != null) {
275: try {
276: Util.addDependency(target, project);
277: ProjectManager.getDefault().saveProject(target);
278: } catch (IOException e) {
279: assert false : e;
280: }
281: }
282: }
283:
284: }
285:
286: /** Represent one module (a suite component) node. */
287: private static final class SuiteComponentNode extends AbstractNode {
288:
289: private final static Action REMOVE_ACTION = new RemoveSuiteComponentAction();
290: private final static Action OPEN_ACTION = new OpenProjectAction();
291:
292: public SuiteComponentNode(final NbModuleProject suiteComponent) {
293: super (Children.LEAF, Lookups
294: .fixed(new Object[] { suiteComponent }));
295: ProjectInformation info = ProjectUtils
296: .getInformation(suiteComponent);
297: setName(info.getName());
298: setDisplayName(info.getDisplayName());
299: setIconBaseWithExtension(NbModuleProject.NB_PROJECT_ICON_PATH);
300: info
301: .addPropertyChangeListener(new PropertyChangeListener() {
302: public void propertyChange(
303: PropertyChangeEvent evt) {
304: if (ProjectInformation.PROP_DISPLAY_NAME
305: .equals(evt.getPropertyName())) {
306: SuiteComponentNode.this
307: .setDisplayName((String) evt
308: .getNewValue());
309: } else if (ProjectInformation.PROP_NAME
310: .equals(evt.getPropertyName())) {
311: SuiteComponentNode.this
312: .setName((String) evt
313: .getNewValue());
314: }
315: }
316: });
317: }
318:
319: public @Override
320: Action[] getActions(boolean context) {
321: return new Action[] { OPEN_ACTION, REMOVE_ACTION };
322: }
323:
324: public @Override
325: Action getPreferredAction() {
326: return OPEN_ACTION;
327: }
328:
329: }
330:
331: private static final class RemoveSuiteComponentAction extends
332: NodeAction {
333:
334: protected void performAction(Node[] activatedNodes) {
335: for (int i = 0; i < activatedNodes.length; i++) {
336: final NbModuleProject suiteComponent = activatedNodes[i]
337: .getLookup().lookup(NbModuleProject.class);
338: assert suiteComponent != null : "NbModuleProject in lookup"; // NOI18N
339: try {
340: NbModuleProject[] modules = SuiteUtils
341: .getDependentModules(suiteComponent);
342: boolean remove = true;
343: if (modules.length > 0) {
344: StringBuffer sb = new StringBuffer("<ul>"); // NOI18N
345: for (int j = 0; j < modules.length; j++) {
346: sb
347: .append("<li>"
348: + ProjectUtils
349: .getInformation(
350: modules[j])
351: .getDisplayName()
352: + "</li>"); // NOI18N
353: }
354: sb.append("</ul>"); // NOI18N
355: String displayName = ProjectUtils
356: .getInformation(suiteComponent)
357: .getDisplayName();
358: String confirmMessage = NbBundle.getMessage(
359: SuiteLogicalView.class,
360: "MSG_RemovingModuleMessage",
361: displayName, sb.toString()); // NOI18N
362: remove = UIUtil
363: .showAcceptCancelDialog(
364: NbBundle
365: .getMessage(
366: SuiteLogicalView.class,
367: "CTL_RemovingModuleTitle",
368: displayName),
369: confirmMessage,
370: getMessage("CTL_RemoveDependency"),
371: null,
372: NotifyDescriptor.QUESTION_MESSAGE);
373: }
374: if (remove) {
375: SuiteUtils
376: .removeModuleFromSuiteWithDependencies(suiteComponent);
377: }
378: } catch (IOException ex) {
379: ErrorManager.getDefault().notify(ex);
380: }
381: }
382: }
383:
384: protected boolean enable(Node[] activatedNodes) {
385: return true;
386: }
387:
388: public String getName() {
389: return getMessage("CTL_RemoveModule");
390: }
391:
392: public HelpCtx getHelpCtx() {
393: return HelpCtx.DEFAULT_HELP;
394: }
395:
396: protected @Override
397: boolean asynchronous() {
398: return false;
399: }
400:
401: }
402:
403: private static final class OpenProjectAction extends CookieAction {
404:
405: protected void performAction(Node[] activatedNodes) {
406: final Project[] projects = new Project[activatedNodes.length];
407: for (int i = 0; i < activatedNodes.length; i++) {
408: Project project = activatedNodes[i].getLookup().lookup(
409: Project.class);
410: projects[i] = project;
411: }
412: RequestProcessor.getDefault().post(new Runnable() {
413: public void run() {
414: StatusDisplayer.getDefault().setStatusText(
415: getMessage("MSG_OpeningProjects"));
416: OpenProjects.getDefault().open(projects, false);
417: }
418: });
419: }
420:
421: public String getName() {
422: return getMessage("CTL_OpenProject");
423: }
424:
425: public HelpCtx getHelpCtx() {
426: return HelpCtx.DEFAULT_HELP;
427: }
428:
429: protected @Override
430: boolean asynchronous() {
431: return false;
432: }
433:
434: protected int mode() {
435: return CookieAction.MODE_ALL;
436: }
437:
438: protected Class[] cookieClasses() {
439: return new Class[] { Project.class };
440: }
441:
442: }
443:
444: }
|