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.io.IOException;
045: import java.util.HashSet;
046: import java.util.List;
047: import java.util.Set;
048: import javax.swing.Action;
049: import javax.swing.JMenuItem;
050: import org.netbeans.api.project.FileOwnerQuery;
051: import org.netbeans.api.project.Project;
052: import org.netbeans.modules.project.ui.ExitDialog;
053: import org.netbeans.spi.project.support.ProjectOperations;
054: import org.netbeans.spi.project.ui.CustomizerProvider;
055: import org.openide.DialogDisplayer;
056: import org.openide.NotifyDescriptor;
057: import org.openide.cookies.SaveCookie;
058: import org.openide.loaders.DataObject;
059: import org.openide.util.Lookup;
060: import org.openide.util.NbBundle;
061: import org.openide.util.actions.Presenter;
062: import org.openide.filesystems.FileObject;
063: import org.openide.filesystems.FileUtil;
064:
065: /** Action for invoking project customizer
066: */
067: public class CustomizeProject extends ProjectAction implements
068: Presenter.Popup {
069:
070: private static final String namePattern = NbBundle.getMessage(
071: CustomizeProject.class, "LBL_CustomizeProjectAction_Name"); // NOI18N
072: private static final String namePatternPopup = NbBundle.getMessage(
073: CustomizeProject.class,
074: "LBL_CustomizeProjectAction_Popup_Name"); // NOI18N
075:
076: public CustomizeProject() {
077: this (null);
078: }
079:
080: public CustomizeProject(Lookup context) {
081: super ((String) null, namePattern, null, context);
082: refresh(getLookup());
083: }
084:
085: protected void refresh(Lookup context) {
086:
087: super .refresh(context);
088:
089: Project[] projects = ActionsUtil.getProjectsFromLookup(context,
090: null);
091:
092: if (projects.length != 1
093: || projects[0].getLookup().lookup(
094: CustomizerProvider.class) == null) {
095: setEnabled(false);
096: // setDisplayName( ActionsUtil.formatProjectSensitiveName( namePattern, new Project[0] ) );
097: } else {
098: setEnabled(true);
099: // setDisplayName( ActionsUtil.formatProjectSensitiveName( namePattern, projects ) );
100: }
101:
102: }
103:
104: public void actionPerformed(Lookup context) {
105:
106: Project[] projects = ActionsUtil.getProjectsFromLookup(context,
107: null);
108:
109: if (projects.length == 1) {
110: CustomizerProvider cp = projects[0].getLookup().lookup(
111: CustomizerProvider.class);
112: if (cp != null) {
113: if (!DataObject.getRegistry().getModifiedSet()
114: .isEmpty()) {
115: // #50992: danger! Project properties dialog may try to write to the same config files.
116:
117: //#92011 - reducing the frequency of the dialog popping up.
118: Set<DataObject> candidates = new HashSet<DataObject>();
119: List<FileObject> metadataFiles = ProjectOperations
120: .getMetadataFiles(projects[0]);
121:
122: for (DataObject dobj : DataObject.getRegistry()
123: .getModifiedSet()) {
124: // only consider files from our project
125: if (projects[0] == FileOwnerQuery.getOwner(dobj
126: .getPrimaryFile())) {
127: // now check if it's metadata or data - not 100% bulletproof, but should reduce the probability significantly
128: for (FileObject df : metadataFiles) {
129: if (df.equals(dobj.getPrimaryFile())
130: || (df.isFolder() && FileUtil
131: .isParentOf(
132: df,
133: dobj
134: .getPrimaryFile()))) {
135: candidates.add(dobj);
136: break;
137: }
138: }
139: }
140: }
141: if (!candidates.isEmpty()) {
142: String saveAll = NbBundle.getMessage(
143: CustomizeProject.class,
144: "CustomizeProject.saveAll");
145: Object ret = DialogDisplayer
146: .getDefault()
147: .notify(
148: new NotifyDescriptor(
149: NbBundle
150: .getMessage(
151: CustomizeProject.class,
152: "CustomizeProject.save_modified_files"),
153: NbBundle
154: .getMessage(
155: CustomizeProject.class,
156: "CustomizeProject.save_modified_title"),
157: NotifyDescriptor.OK_CANCEL_OPTION,
158: NotifyDescriptor.WARNING_MESSAGE,
159: new Object[] {
160: saveAll,
161: NotifyDescriptor.CANCEL_OPTION },
162: saveAll));
163: if (ret != saveAll) {
164: return;
165: } else {
166: for (DataObject dobj : candidates) {
167: ExitDialog.doSave(dobj);
168: }
169: }
170: }
171: }
172: cp.showCustomizer();
173: }
174: }
175:
176: }
177:
178: public Action createContextAwareInstance(Lookup actionContext) {
179: return new CustomizeProject(actionContext);
180: }
181:
182: // Implementation of Presenter.Popup ---------------------------------------
183:
184: public JMenuItem getPopupPresenter() {
185: JMenuItem popupPresenter = new JMenuItem(this);
186: popupPresenter.setText(namePatternPopup);
187: popupPresenter.setIcon(null);
188:
189: return popupPresenter;
190: }
191:
192: }
|