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.apisupport.project.ui.customizer;
043:
044: import java.awt.Dialog;
045: import java.awt.event.ActionEvent;
046: import java.awt.event.ActionListener;
047: import java.awt.event.WindowAdapter;
048: import java.awt.event.WindowEvent;
049: import java.io.IOException;
050: import javax.swing.JDialog;
051: import org.netbeans.api.project.Project;
052: import org.netbeans.api.project.ProjectManager;
053: import org.netbeans.api.project.ProjectUtils;
054: import org.netbeans.spi.project.ui.CustomizerProvider;
055: import org.netbeans.spi.project.ui.support.ProjectCustomizer;
056: import org.openide.ErrorManager;
057: import org.openide.util.Lookup;
058: import org.openide.util.Mutex;
059: import org.openide.util.MutexException;
060: import org.openide.util.NbBundle;
061: import org.openide.util.lookup.Lookups;
062: import org.openide.util.lookup.ProxyLookup;
063:
064: /**
065: * Convenient class to be used by {@link CustomizerProvider} implementations.
066: *
067: * @author Martin Krauskopf
068: */
069: abstract class BasicCustomizer implements CustomizerProvider {
070:
071: static final String LAST_SELECTED_PANEL = "lastSelectedPanel"; // NOI18N
072:
073: /** Project <code>this</code> customizer customizes. */
074: private final Project project;
075:
076: /** Keeps reference to a dialog representing <code>this</code> customizer. */
077: private Dialog dialog;
078:
079: private String lastSelectedCategory;
080:
081: private String layerPath;
082:
083: protected BasicCustomizer(final Project project, String path) {
084: this .project = project;
085: layerPath = path;
086: }
087:
088: /**
089: * All changes should be store at this point. Is called under the write
090: * access from {@link ProjectManager#mutex}.
091: */
092: abstract void storeProperties() throws IOException;
093:
094: /**
095: * Gives a chance to do some work after all the changes in a customizer
096: * were successfully saved. Is called under the write access from {@link
097: * ProjectManager#mutex}.
098: */
099: abstract void postSave() throws IOException;
100:
101: /**
102: * Be sure that you will prepare all the data (typically subclass of {@link
103: * ModuleProperties}) needed by a customizer and its panels and that the
104: * data is always up-to-date after this method was called.
105: */
106: abstract Lookup prepareData();
107:
108: abstract void dialogCleanup();
109:
110: protected Project getProject() {
111: return project;
112: }
113:
114: /** Show customizer with the first category selected. */
115: public void showCustomizer() {
116: showCustomizer(null);
117: }
118:
119: /** Show customizer with preselected category. */
120: public void showCustomizer(String preselectedCategory) {
121: showCustomizer(preselectedCategory, null);
122: }
123:
124: public void showCustomizer(String preselectedCategory,
125: String preselectedSubCategory) {
126: if (dialog != null) {
127: dialog.setVisible(true);
128: return;
129: } else {
130: Lookup context = prepareData();
131: if (preselectedCategory == null) {
132: preselectedCategory = lastSelectedCategory;
133: }
134: context = new ProxyLookup(context, Lookups
135: .fixed(new SubCategoryProvider(preselectedCategory,
136: preselectedSubCategory)));
137: OptionListener listener = new OptionListener();
138: dialog = ProjectCustomizer.createCustomizerDialog(
139: layerPath, context, preselectedCategory, listener,
140: null);
141: dialog.addWindowListener(listener);
142: dialog.setTitle(NbBundle.getMessage(getClass(),
143: "LBL_CustomizerTitle", ProjectUtils.getInformation(
144: getProject()).getDisplayName()));
145: dialog.setVisible(true);
146: }
147: }
148:
149: public final void save() {
150: try {
151: ProjectManager.mutex().writeAccess(
152: new Mutex.ExceptionAction<Void>() {
153: public Void run() throws IOException {
154: storeProperties();
155: ProjectManager.getDefault().saveProject(
156: project);
157: return null;
158: }
159: });
160: } catch (MutexException e) {
161: ErrorManager.getDefault().notify(
162: (IOException) e.getException());
163: }
164: }
165:
166: private String findLastSelectedCategory() {
167: if (dialog != null && dialog instanceof JDialog) {
168: return (String) ((JDialog) dialog).getRootPane()
169: .getClientProperty(
170: BasicCustomizer.LAST_SELECTED_PANEL);
171: }
172: return null;
173: }
174:
175: protected class OptionListener extends WindowAdapter implements
176: ActionListener {
177:
178: // Listening to OK button ----------------------------------------------
179: public void actionPerformed(ActionEvent e) {
180: save();
181: }
182:
183: // remove dialog for this customizer's project
184: @Override
185: public void windowClosed(WindowEvent e) {
186: doClose();
187: }
188:
189: @Override
190: public void windowClosing(WindowEvent e) {
191: // Dispose the dialog otherwise the
192: // {@link WindowAdapter#windowClosed} may not be called
193: doClose();
194: }
195:
196: public void doClose() {
197: if (dialog != null) {
198: lastSelectedCategory = findLastSelectedCategory();
199: dialog.removeWindowListener(this );
200: dialog.setVisible(false);
201: dialog.dispose();
202: dialogCleanup();
203: }
204: dialog = null;
205: }
206:
207: }
208:
209: static final class SubCategoryProvider {
210:
211: private String subcategory;
212:
213: private String category;
214:
215: SubCategoryProvider(String category, String subcategory) {
216: this .category = category;
217: this .subcategory = subcategory;
218: }
219:
220: public String getCategory() {
221: return category;
222: }
223:
224: public String getSubcategory() {
225: return subcategory;
226: }
227: }
228:
229: }
|