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: * PageFlowController.java
042: *
043: * Created on March 1, 2007, 1:12 PM
044: *
045: * To change this template, choose Tools | Template Manager
046: * and open the template in the editor.
047: */
048:
049: package org.netbeans.modules.web.jsf.navigation;
050:
051: import java.awt.Dimension;
052: import java.awt.Image;
053: import java.awt.event.ItemEvent;
054: import java.awt.event.ItemListener;
055: import java.awt.event.MouseAdapter;
056: import java.awt.event.MouseEvent;
057: import java.lang.ref.Reference;
058: import java.lang.ref.WeakReference;
059: import java.util.Date;
060: import java.util.HashMap;
061: import java.util.Map;
062: import java.util.Set;
063: import java.util.WeakHashMap;
064: import java.util.logging.Level;
065: import java.util.logging.LogRecord;
066: import java.util.logging.Logger;
067: import javax.swing.ImageIcon;
068: import javax.swing.JButton;
069: import javax.swing.JComboBox;
070: import org.openide.util.NbBundle;
071:
072: /**
073: *
074: * @author joelle
075: */
076: public class PageFlowToolbarUtilities {
077:
078: private static final Logger LOGGER = Logger
079: .getLogger("org.netbeans.modules.web.jsf.navigation");
080:
081: // private static final int SCOPE_FACESCONFIG = 1;
082: // private static final int SCOPE_PROJECT = 2;
083:
084: public static enum Scope {
085:
086: SCOPE_FACESCONFIG, SCOPE_PROJECT, SCOPE_ALL_FACESCONFIG
087: }
088:
089: private static Map<Scope, String> scope2String = new HashMap<Scope, String>();
090: private static Map<String, Scope> string2Scope = new HashMap<String, Scope>();
091: private static final String TT_SCOPE = NbBundle.getMessage(
092: PageFlowToolbarUtilities.class, "TT_Scope_DropDown");
093: private static final String LBL_SCOPE_FACESCONFIG = NbBundle
094: .getMessage(PageFlowToolbarUtilities.class,
095: "LBL_Scope_FacesConfig");
096: private static final String LBL_SCOPE_PROJECT = NbBundle
097: .getMessage(PageFlowToolbarUtilities.class,
098: "LBL_Scope_Project");
099: private static final String LBL_SCOPE_ALL_PROJECT = NbBundle
100: .getMessage(PageFlowToolbarUtilities.class,
101: "LBL_Scope_All_FacesConfig");
102: private static final String TT_LAYOUTPAGES = NbBundle.getMessage(
103: PageFlowToolbarUtilities.class, "TT_LayoutPages");
104: static {
105: /* Loading these for quick access */
106: scope2String
107: .put(Scope.SCOPE_FACESCONFIG, LBL_SCOPE_FACESCONFIG);
108: scope2String.put(Scope.SCOPE_PROJECT, LBL_SCOPE_PROJECT);
109: scope2String.put(Scope.SCOPE_ALL_FACESCONFIG,
110: LBL_SCOPE_ALL_PROJECT);
111: string2Scope
112: .put(LBL_SCOPE_FACESCONFIG, Scope.SCOPE_FACESCONFIG);
113: string2Scope.put(LBL_SCOPE_PROJECT, Scope.SCOPE_PROJECT);
114: string2Scope.put(LBL_SCOPE_ALL_PROJECT,
115: Scope.SCOPE_ALL_FACESCONFIG);
116: }
117: private Scope currentScope;
118:
119: // private PageFlowController pfc;
120:
121: public static final String getScopeLabel(Scope scope) {
122: return scope2String.get(scope);
123: }
124:
125: public static final Scope getScope(String scopeStr) {
126: return string2Scope.get(scopeStr);
127: }
128:
129: /** Creates a new instance of PageFlowController
130: * @param scene
131: */
132: private PageFlowToolbarUtilities(PageFlowView view) {
133: currentScope = Scope.SCOPE_PROJECT;
134: setPageFlowView(view);
135: }
136:
137: private static PageFlowToolbarUtilities instance;
138: private static final Map<PageFlowView, PageFlowToolbarUtilities> map = new WeakHashMap<PageFlowView, PageFlowToolbarUtilities>();
139:
140: /**
141: * Remove PageFlowView from the map in PageFlowToolbarUtilities
142: */
143: protected static boolean removePageFlowView(PageFlowView view) {
144: if (map.containsKey(view)) {
145: map.remove(view);
146: return true;
147: }
148: return false;
149: }
150:
151: /**
152: *Accessor to get the instance. Singleton pattern
153: * @return pageFlowUtilities instance
154: */
155: public static PageFlowToolbarUtilities getInstance(PageFlowView view) {
156: PageFlowToolbarUtilities myInstance = map.get(view);
157: if (myInstance == null) {
158: myInstance = new PageFlowToolbarUtilities(view);
159: map.put(view, myInstance);
160: }
161: return myInstance;
162: }
163:
164: public static Set<PageFlowView> getViews() {
165: return map.keySet();
166: }
167:
168: /**
169: * Get the current page flow editor scope
170: * @return currentScope (LBL_SCOPE_PROJECT,LBL_SCOPE_FACESCONFIG)
171: */
172: public Scope getCurrentScope() {
173: return currentScope;
174: }
175:
176: public void setCurrentScope(Scope scope) {
177: this .currentScope = scope;
178: if (scopeBox != null
179: && !scopeBox.getSelectedItem().equals(
180: getScopeLabel(currentScope))) {
181: scopeBox.setSelectedItem(getScopeLabel(currentScope));
182: }
183: }
184:
185: private JComboBox scopeBox;
186:
187: /**
188: * Creates a JComboBox for the user to select the scope type.
189: * @param view
190: * @param pfc
191: * @return
192: */
193: public JComboBox createScopeComboBox() {
194: JComboBox comboBox = new JComboBox();
195: comboBox.addItem(getScopeLabel(Scope.SCOPE_FACESCONFIG));
196: comboBox.addItem(getScopeLabel(Scope.SCOPE_PROJECT));
197: comboBox.addItem(getScopeLabel(Scope.SCOPE_ALL_FACESCONFIG));
198:
199: //Set the appropriate size of the combo box so it doesn't take up the whole page.
200: Dimension prefSize = comboBox.getPreferredSize();
201: comboBox.setMinimumSize(prefSize);
202: comboBox.setMaximumSize(prefSize);
203:
204: comboBox.setSelectedItem(getScopeLabel(currentScope));
205:
206: comboBox.addItemListener(new ItemListener() {
207:
208: public void itemStateChanged(ItemEvent event) {
209: PageFlowView view = getPageFlowView();
210: if (event.getStateChange() == ItemEvent.SELECTED) {
211: String newScope = (String) event.getItem();
212: /* Save Locations before switching scope */
213: view.saveLocations();
214:
215: LogRecord record = new LogRecord(Level.FINE,
216: "PageFLowEditor Scope Changed To:"
217: + newScope);
218: record
219: .setSourceClassName("PageFlowUtilities.ItemListener");
220: record.setSourceMethodName("itemStateChanged");
221: record.setParameters(new Object[] { newScope,
222: new Date() });
223: LOGGER.log(record);
224:
225: setCurrentScope(getScope(newScope));
226: //As we are setting the current scope, we should update the controller and update the scene. But what happens with setup?
227: /* We don't want the background process to continue adding pins to the pages */
228: // view.clearBackgroundPinAddingProcess();
229: /* You don't want to override the data you just stored */
230: view.getPageFlowController().setupGraphNoSaveData();
231: }
232: view.requestMultiViewActive();
233: }
234: });
235: comboBox.setToolTipText(TT_SCOPE);
236: scopeBox = comboBox;
237: return comboBox;
238: }
239:
240: private static final Image LAYOUT_ICON = org.openide.util.Utilities
241: .loadImage("org/netbeans/modules/web/jsf/navigation/resources/navigation.gif"); // NOI18N
242: private JButton layoutButton = null;
243:
244: /**
245: * Creates a JComboBox for the user to select the scope type.
246: * @param view
247: * @param pfc
248: * @return
249: */
250: public JButton createLayoutButton() {
251:
252: if (layoutButton != null) {
253: return layoutButton;
254: }
255:
256: layoutButton = new JButton(new ImageIcon(LAYOUT_ICON));
257: //Set the appropriate size of the combo box so it doesn't take up the whole page.
258: // Dimension prefSize = layoutButton.getPreferredSize();
259: // layoutButton.setMinimumSize(prefSize);
260: // layoutButton.setMaximumSize(prefSize);
261: layoutButton.setToolTipText(TT_LAYOUTPAGES);
262: layoutButton.addMouseListener(new MouseAdapter() {
263:
264: @Override
265: public void mouseClicked(MouseEvent e) {
266: getPageFlowView().layoutNodes();
267: getPageFlowView().requestMultiViewActive();
268: }
269: });
270: return layoutButton;
271: }
272:
273: private Reference<PageFlowView> pageFlowViewRef;
274:
275: /* Get's the PageFlowView of the current utilities */
276: public final PageFlowView getPageFlowView() {
277: return pageFlowViewRef.get();
278: }
279:
280: /* Sets the PageFlowView in a WeakReference */
281: public void setPageFlowView(PageFlowView view) {
282: pageFlowViewRef = new WeakReference<PageFlowView>(view);
283: }
284: }
|