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.visualweb.designer.jsf.palette;
043:
044: import org.netbeans.modules.visualweb.complib.api.ComplibEvent;
045: import org.netbeans.modules.visualweb.complib.api.ComplibListener;
046: import org.netbeans.modules.visualweb.complib.api.ComplibService;
047: import org.netbeans.modules.visualweb.project.jsf.api.JsfProjectUtils;
048: import java.io.IOException;
049: import java.lang.ref.WeakReference;
050: import org.netbeans.api.project.Project;
051: import org.netbeans.modules.visualweb.designer.jsf.palette.JsfPaletteActions;
052: import org.netbeans.modules.visualweb.designer.jsf.palette.MergedPaletteActions;
053: import org.netbeans.spi.palette.PaletteActions;
054: import org.netbeans.spi.palette.PaletteController;
055: import org.netbeans.spi.palette.PaletteFactory;
056: import org.netbeans.spi.palette.PaletteFilter;
057: import org.openide.ErrorManager;
058: import org.openide.util.Exceptions;
059: import org.openide.util.Lookup;
060:
061: /**
062: * Factory for creating JSF <code>PaletteController</code>.
063: *
064: * @author Peter Zavadsky
065: */
066: public final class PaletteControllerFactory {
067:
068: private static final String PALETTE_DIRECTORY_1_4 = "CreatorDesignerPalette"; // NOI18N
069:
070: private static final String PALETTE_DIRECTORY_5 = "CreatorDesignerPalette5"; // NOI18N
071:
072: private static final PaletteControllerFactory INSTANCE = new PaletteControllerFactory();
073:
074: /** Creates a new instance of JsfPaletteControllerFactory */
075: private PaletteControllerFactory() {
076: }
077:
078: public static PaletteControllerFactory getDefault() {
079: return INSTANCE;
080: }
081:
082: /**
083: * The sole purpose of this method is to grab the controller
084: * for to get the palette customizer (palette manager) for
085: * J2EE 5 projects.
086: **/
087: public static PaletteController getJsfPaletteController_5() {
088: return getJsfPaletteController(PALETTE_DIRECTORY_5);
089: }
090:
091: /**
092: * The sole purpose of this method is to grab the controller
093: * for to get the palette customizer (palette manager) for
094: * J2EE 1.4 projects.
095: **/
096: public static PaletteController getJsfPaletteController_1_4() {
097: return getJsfPaletteController(PALETTE_DIRECTORY_1_4);
098: }
099:
100: /**
101: * Only to be used by "getJsfPaletteController_1_4()" and "getJsfPaletteController_5()"
102: * @param paletteDirectory can either be "CreatorDesignerPalette" or "CreatorDesignerPalette5"
103: **/
104: private static PaletteController getJsfPaletteController(
105: String paletteDirectory) {
106: try {
107: PaletteActions paletteActions = new JsfPaletteActions(
108: paletteDirectory);
109: PaletteController controller = PaletteFactory
110: .createPalette(paletteDirectory, paletteActions,
111: null, null);
112: return controller;
113: } catch (IOException ex) {
114: Exceptions.printStackTrace(ex);
115: return null;
116: }
117:
118: }
119:
120: public PaletteController createJsfPaletteController(Project project) {
121: if (project == null) {
122: // XXX
123: return null;
124: }
125:
126: String paletteDirectory;
127: if (JsfProjectUtils.JAVA_EE_5.equals(JsfProjectUtils
128: .getJ2eePlatformVersion(project))) {
129: paletteDirectory = PALETTE_DIRECTORY_5;
130: } else {
131: //Later to be renamed with a 1.4
132: paletteDirectory = PALETTE_DIRECTORY_1_4;
133: }
134:
135: // XXX PaletteController
136: PaletteController controller;
137: try {
138: ComplibService complibService = getComplibService();
139: PaletteFilter complibPaletteFilter;
140: PaletteActions allPaletteActions;
141: if (complibService == null) {
142: ErrorManager
143: .getDefault()
144: .notify(
145: ErrorManager.INFORMATIONAL,
146: new NullPointerException(
147: "There is no ComplibService available!")); // NOI18N
148: complibPaletteFilter = null;
149: allPaletteActions = null;
150: } else {
151: complibPaletteFilter = complibService
152: .createComplibPaletteFilter(project);
153:
154: // Merge in complib PaletteActions
155: PaletteActions jsfPaletteActions = new JsfPaletteActions(
156: paletteDirectory);
157: PaletteActions complibPaletteActions = complibService
158: .createComplibPaletteActions();
159: if (complibPaletteActions != null) {
160: allPaletteActions = new MergedPaletteActions(
161: jsfPaletteActions, complibPaletteActions);
162: } else {
163: allPaletteActions = jsfPaletteActions;
164: }
165: }
166:
167: controller = PaletteFactory.createPalette(paletteDirectory,
168: allPaletteActions, complibPaletteFilter, null);
169:
170: // XXX #6466711 Listening to changes of complib to refresh the palette.
171: JsfComplibListener.getDefault().install();
172: JsfComplibListener.getDefault().setPaletteController(
173: controller);
174:
175: return controller;
176: } catch (IOException ex) {
177: ErrorManager.getDefault().notify(
178: ErrorManager.INFORMATIONAL, ex);
179: controller = null;
180: }
181:
182: return controller;
183: }
184:
185: private static ComplibService getComplibService() {
186: return Lookup.getDefault().lookup(ComplibService.class);
187: }
188:
189: private static class JsfComplibListener implements ComplibListener {
190: private static JsfComplibListener INSTANCE = new JsfComplibListener();
191:
192: private WeakReference<PaletteController> paletteControllerWRef = new WeakReference<PaletteController>(
193: null);
194:
195: private boolean installed;
196:
197: public static JsfComplibListener getDefault() {
198: return INSTANCE;
199: }
200:
201: public void install() {
202: if (installed) {
203: return;
204: }
205: ComplibService complibService = getComplibService();
206: if (complibService == null) {
207: return;
208: }
209: complibService.addComplibListener(this );
210: installed = true;
211: }
212:
213: public void uninstall() {
214: ComplibService complibService = getComplibService();
215: if (complibService == null) {
216: return;
217: }
218: complibService.removeComplibListener(this );
219: installed = false;
220: }
221:
222: public void setPaletteController(
223: PaletteController paletteController) {
224: paletteControllerWRef = new WeakReference<PaletteController>(
225: paletteController);
226: }
227:
228: public void paletteChanged(ComplibEvent evt) {
229: PaletteController paletteController = paletteControllerWRef
230: .get();
231: if (paletteController == null) {
232: return;
233: }
234: paletteController.refresh();
235: }
236: } // End of JsfComplibListener
237:
238: }
|