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: package org.netbeans.modules.web.jsf.navigation.graph;
042:
043: import java.awt.Point;
044: import java.util.Collection;
045: import java.util.HashMap;
046: import java.util.Map;
047: import java.util.logging.Logger;
048: import org.netbeans.api.visual.vmd.VMDNodeWidget;
049: import org.netbeans.modules.web.jsf.navigation.Page;
050: import org.netbeans.modules.web.jsf.navigation.PageFlowToolbarUtilities;
051: import org.netbeans.modules.web.jsf.navigation.PageFlowToolbarUtilities.Scope;
052:
053: /**
054: *
055: * @author joelle
056: */
057: public class PageFlowSceneData {
058:
059: private final PageFlowToolbarUtilities utilities;
060: // private PageFlowScene scene;
061: private final Map<String, PageData> facesConfigSceneData = new HashMap<String, PageData>();
062: private final Map<String, PageData> projectSceneData = new HashMap<String, PageData>();
063: private final Map<String, PageData> allFacesConfigSceneData = new HashMap<String, PageData>();
064:
065: /**
066: * PageFlowSceneData keeps scene data for the facesConfigScope and the projectSceneScope.
067: * Because the actual Page object is not necessarily the same, it uses the display name
068: * as the key.
069: * @param scene PageFlowScene
070: * @param utilities PageFlowUtilites
071: **/
072: public PageFlowSceneData(PageFlowToolbarUtilities utilities) {
073: this .utilities = utilities;
074: // this.scene = scene;
075: }
076:
077: private static Logger LOG = Logger
078: .getLogger(PageFlowSceneData.class.getName());
079:
080: /**
081: * Saves the Scene Data for the Current Scene Scope
082: **/
083: public void saveCurrentSceneData(PageFlowScene scene) {
084: switch (utilities.getCurrentScope()) {
085: case SCOPE_FACESCONFIG:
086: facesConfigSceneData.clear();
087: facesConfigSceneData.putAll(createSceneInfo(scene));
088: break;
089: case SCOPE_PROJECT:
090: projectSceneData.clear();
091: projectSceneData.putAll(createSceneInfo(scene));
092: break;
093: case SCOPE_ALL_FACESCONFIG:
094: allFacesConfigSceneData.clear();
095: allFacesConfigSceneData.putAll(createSceneInfo(scene));
096: break;
097: default:
098: LOG.fine("PageFlowSceneData: Unknown State");
099: }
100: //
101: // if ( utilities.getCurrentScope().equals( PageFlowUtilities.Scope.SCOPE_FACESCONFIG) ){
102: // facesConfigSceneData.clear();
103: // facesConfigSceneData.putAll( createSceneInfo(scene ) );
104: // } else if( utilities.getCurrentScope().equals( PageFlowUtilities.Scope.SCOPE_PROJECT)){
105: // projectSceneData.clear();
106: // projectSceneData.putAll( createSceneInfo(scene) );
107: // }
108: }
109:
110: /**
111: * Moves the Point for the oldDisplayName and to the newDisplayName
112: * @param oldDisplayName String
113: * @param newDisplayName String
114: **/
115: public void savePageWithNewName(String oldDisplayName,
116: String newDisplayName) {
117: replaceSceneInfo(facesConfigSceneData, oldDisplayName,
118: newDisplayName);
119: replaceSceneInfo(projectSceneData, oldDisplayName,
120: newDisplayName);
121: replaceSceneInfo(allFacesConfigSceneData, oldDisplayName,
122: newDisplayName);
123: }
124:
125: /**
126: * Loads the Scene Data stored for the current scope into the Scene.
127: **/
128: public void loadSceneData(PageFlowScene scene) {
129: loadSceneData(scene, getCurrentSceneData());
130: }
131:
132: public PageData getPageData(String pageDisplayName) {
133: Map<String, PageData> map = getCurrentSceneData();
134: PageData data = null;
135: if (map != null) {
136: data = map.get(pageDisplayName);
137: }
138: return data;
139: }
140:
141: /*
142: public Point getPageLocation(String pageDisplayName) {
143: Map<String, PageData> map = getCurrentSceneData();
144: Point p = null;
145: if (map != null) {
146: PageData data = map.get(pageDisplayName);
147: if (data != null) {
148: p = data.getPoint();
149: }
150: }
151: return p;
152: }
153:
154: public boolean isMinimized(String pageDisplayName) {
155: Map<String, PageData> map = getCurrentSceneData();
156: boolean isMinimized = false;
157: ;
158: if (map != null) {
159: PageData data = getCurrentSceneData().get(pageDisplayName);
160: if (data != null) {
161: isMinimized = data.isMinimized();
162: }
163: }
164: return isMinimized;
165: }
166: */
167:
168: private void loadSceneData(PageFlowScene scene,
169: Map<String, PageData> sceneInfo) {
170: if (sceneInfo == null) {
171: return;
172: }
173: Collection<Page> pages = scene.getNodes();
174: for (Page page : pages) {
175: PageData data = sceneInfo.get(page.getDisplayName());
176: if (data != null) {
177: VMDNodeWidget pageWidget = (VMDNodeWidget) scene
178: .findWidget(page);
179: pageWidget.setPreferredLocation(data.getPoint());
180: pageWidget.setMinimized(data.isMinimized());
181: }
182: }
183: }
184:
185: private Map<String, PageData> createSceneInfo(PageFlowScene scene) {
186: Map<String, PageData> sceneInfo = new HashMap<String, PageData>();
187: Collection<Page> pages = scene.getNodes();
188: for (Page page : pages) {
189: VMDNodeWidget pageWidget = (VMDNodeWidget) scene
190: .findWidget(page);
191: Point p;
192: if (scene.isValidated()) {
193: p = pageWidget.getLocation();
194: } else {
195: p = pageWidget.getPreferredLocation();
196: }
197: if (p != null) {
198: sceneInfo.put(page.getDisplayName(), createPageData(p,
199: pageWidget.isMinimized()));
200: }
201: }
202: return sceneInfo;
203: }
204:
205: public String getCurrentScopeStr() {
206: return PageFlowToolbarUtilities.getScopeLabel(utilities
207: .getCurrentScope());
208: }
209:
210: public void setCurrentScope(Scope newScope) {
211: utilities.setCurrentScope(newScope);
212: }
213:
214: public void setScopeData(Scope scope, Map<String, PageData> map) {
215: //switch (PageFlowToolbarUtilities.getScope(scope)) {
216: switch (scope) {
217: case SCOPE_FACESCONFIG:
218: facesConfigSceneData.clear();
219: facesConfigSceneData.putAll(map);
220: break;
221: case SCOPE_PROJECT:
222: projectSceneData.clear();
223: projectSceneData.putAll(map);
224: break;
225: case SCOPE_ALL_FACESCONFIG:
226: allFacesConfigSceneData.clear();
227: allFacesConfigSceneData.putAll(map);
228: break;
229: }
230: // if ( scope.equals( PageFlowUtilities.getScopeLabel(PageFlowUtilities.Scope.SCOPE_FACESCONFIG) ) ){
231: // facesConfigSceneData.clear();
232: // facesConfigSceneData.putAll(map);
233: // } else if( scope.equals(PageFlowUtilities.getScopeLabel(PageFlowUtilities.Scope.SCOPE_PROJECT))){
234: // projectSceneData.clear();
235: // projectSceneData.putAll(map);
236: // }
237: }
238:
239: public Map<String, PageData> getScopeData(Scope scope) {
240: Map<String, PageData> sceneInfo = null;
241: // PageFlowToolbarUtilities.Scope scope = PageFlowToolbarUtilities.getScope(scopeStr);
242: switch (scope) {
243: case SCOPE_FACESCONFIG:
244: sceneInfo = facesConfigSceneData;
245: break;
246: case SCOPE_PROJECT:
247: sceneInfo = projectSceneData;
248: break;
249: case SCOPE_ALL_FACESCONFIG:
250: sceneInfo = allFacesConfigSceneData;
251: }
252: // if ( scope.equals( PageFlowUtilities.LBL_SCOPE_FACESCONFIG) ){
253: // sceneInfo = facesConfigSceneData;
254: // } else if( scope.equals(PageFlowUtilities.LBL_SCOPE_PROJECT)){
255: // sceneInfo = projectSceneData;
256: // }
257: return sceneInfo;
258: }
259:
260: private Map<String, PageData> getCurrentSceneData() {
261: Map<String, PageData> currentSceneData = null;
262: switch (utilities.getCurrentScope()) {
263: case SCOPE_FACESCONFIG:
264: currentSceneData = facesConfigSceneData;
265: break;
266: case SCOPE_PROJECT:
267: currentSceneData = projectSceneData;
268: break;
269: case SCOPE_ALL_FACESCONFIG:
270: currentSceneData = allFacesConfigSceneData;
271: break;
272: default:
273: currentSceneData = null;
274: }
275: return currentSceneData;
276: }
277:
278: private void replaceSceneInfo(Map<String, PageData> sceneInfo,
279: String oldDisplayName, String newDisplayName) {
280: assert oldDisplayName != null;
281: assert newDisplayName != null;
282:
283: if (sceneInfo == null || sceneInfo.size() < 1) {
284: return;
285: }
286:
287: PageData data = sceneInfo.remove(oldDisplayName);
288: if (data != null) {
289: sceneInfo.put(newDisplayName, data);
290: }
291: }
292:
293: public static PageData createPageData(Point point,
294: boolean isMinimized) {
295: return new PageData(point, isMinimized);
296: }
297:
298: /* Can I always guarentee that Point is not null? */
299: public static class PageData {
300:
301: private Point point;
302: private boolean isMinimized = false;
303:
304: private PageData() {
305: }
306:
307: public PageData(Point point) {
308: if (point == null) {
309: throw new IllegalArgumentException(
310: "Page Data does not accept null points");
311: }
312: this .point = point;
313: }
314:
315: public PageData(Point point, boolean isMinimized) {
316: this (point);
317: this .isMinimized = isMinimized;
318: }
319:
320: public Point getPoint() {
321: return point;
322: }
323:
324: public boolean isMinimized() {
325: return isMinimized;
326: }
327: }
328: }
|