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.project.ui.actions;
043:
044: import java.beans.PropertyChangeEvent;
045: import java.beans.PropertyChangeListener;
046: import javax.swing.Action;
047: import javax.swing.JMenuItem;
048: import org.netbeans.api.project.Project;
049: import org.netbeans.modules.project.ui.OpenProjectList;
050: import org.netbeans.modules.project.ui.ProjectUtilities;
051: import org.openide.util.Lookup;
052: import org.openide.util.NbBundle;
053: import org.openide.util.WeakListeners;
054: import org.openide.util.actions.Presenter;
055:
056: /** Action for removing project from the open projects tab
057: */
058: public class CloseProject extends ProjectAction implements
059: PropertyChangeListener, Presenter.Popup {
060:
061: private static final String namePattern = NbBundle.getMessage(
062: CloseProject.class, "LBL_CloseProjectAction_Name"); // NOI18N
063: private static final String namePatternPopup = NbBundle.getMessage(
064: CloseProject.class, "LBL_CloseProjectAction_Popup_Name"); // NOI18N
065:
066: private String popupName;
067:
068: private PropertyChangeListener wpcl;
069:
070: /** Creates a new instance of BrowserAction */
071: public CloseProject() {
072: this (null);
073: }
074:
075: public CloseProject(Lookup context) {
076: super ((String) null, namePattern, null, context);
077: wpcl = WeakListeners.propertyChange(this , OpenProjectList
078: .getDefault());
079: OpenProjectList.getDefault().addPropertyChangeListener(wpcl);
080: refresh(getLookup());
081: }
082:
083: protected void actionPerformed(Lookup context) {
084: Project[] projects = ActionsUtil.getProjectsFromLookup(context,
085: null);
086: // show all modified documents, if an user cancel it then no project is closed
087: OpenProjectList.getDefault().close(projects, true);
088: }
089:
090: public void refresh(Lookup context) {
091:
092: super .refresh(context);
093:
094: Project[] projects = ActionsUtil.getProjectsFromLookup(context,
095: null);
096: // XXX make it work better for mutliple open projects
097: if (projects.length == 0
098: || !OpenProjectList.getDefault().isOpen(projects[0])) {
099: setEnabled(false);
100: // setDisplayName( ActionsUtil.formatProjectSensitiveName( namePattern, new Project[0] ) );
101: popupName = ActionsUtil.formatProjectSensitiveName(
102: namePatternPopup, new Project[0]);
103: } else {
104: setEnabled(true);
105: // setDisplayName( ActionsUtil.formatProjectSensitiveName( namePattern, projects ) );
106: popupName = ActionsUtil.formatProjectSensitiveName(
107: namePatternPopup, projects);
108: }
109: }
110:
111: public Action createContextAwareInstance(Lookup actionContext) {
112: return new CloseProject(actionContext);
113: }
114:
115: public void propertyChange(PropertyChangeEvent evt) {
116: refresh(getLookup());
117: }
118:
119: // Implementation of Presenter.Popup ---------------------------------------
120:
121: public JMenuItem getPopupPresenter() {
122: JMenuItem popupPresenter = new JMenuItem(this);
123:
124: popupPresenter.setIcon(null);
125: popupPresenter.setText(popupName);
126:
127: return popupPresenter;
128: }
129:
130: }
|