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.ui;
043:
044: import java.util.HashSet;
045: import java.util.Set;
046: import java.util.logging.Level;
047: import java.util.logging.Logger;
048: import javax.swing.Action;
049: import javax.swing.JComponent;
050:
051: import org.netbeans.core.spi.multiview.CloseOperationState;
052: import org.netbeans.core.spi.multiview.MultiViewElement;
053: import org.netbeans.core.spi.multiview.MultiViewElementCallback;
054: import org.netbeans.modules.visualweb.api.designer.Designer;
055: import org.netbeans.modules.visualweb.designer.jsf.JsfForm;
056: import org.openide.awt.UndoRedo;
057: import org.openide.loaders.DataObject;
058: import org.openide.util.Lookup;
059: import org.openide.windows.Mode;
060: import org.openide.windows.TopComponent;
061: import org.openide.windows.TopComponentGroup;
062: import org.openide.windows.WindowManager;
063: import org.w3c.dom.Element;
064: import org.w3c.dom.Node;
065:
066: /**
067: * Implemenation of JSF multiview element.
068: *
069: * @author Peter Zavadsky
070: */
071: public class JsfMultiViewElement implements MultiViewElement {
072:
073: // private static final String PATH_TOOLBAR_FOLDER = "Designer/application/x-designer/Toolbars/Default"; // NOI18N
074:
075: private static final String TOP_COMPONENT_GROUP_JSF_DESIGNER = "jsfdesigner"; // NOI18N
076:
077: // private final Designer designer;
078: private final JsfTopComponent jsfTopComponent;
079:
080: /** Creates a new instance of DesignerMultiViewElement */
081: public JsfMultiViewElement(JsfForm jsfForm, Designer designer,
082: DataObject jspDataObject) {
083: // if (designer == null) {
084: // throw new NullPointerException("The designer parameter is null!"); // NOI18N
085: // }
086: // this.designer = designer;
087: jsfTopComponent = new JsfTopComponent(jsfForm, designer,
088: jspDataObject);
089: }
090:
091: public JsfTopComponent getJsfTopComponent() {
092: return jsfTopComponent;
093: }
094:
095: public JComponent getVisualRepresentation() {
096: // return designer.getVisualRepresentation();
097: return jsfTopComponent.getVisualRepresentation();
098: }
099:
100: // XXX Moved from designer/../DesignerTopComp.
101: // TODO Move it to JsfTopComponent.
102: public JComponent getToolbarRepresentation() {
103: // return designer.getToolbarRepresentation();
104: return jsfTopComponent.getToolbarRepresentation();
105: }
106:
107: public Action[] getActions() {
108: // return designer.getActions();
109: return jsfTopComponent.getActions();
110: }
111:
112: public Lookup getLookup() {
113: // return designer.getLookup();
114: return jsfTopComponent.getLookup();
115: }
116:
117: public void componentOpened() {
118: // designer.componentOpened();
119: jsfTopComponent.componentOpened();
120: }
121:
122: public void componentClosed() {
123: // designer.componentClosed();
124: jsfTopComponent.componentClosed();
125: }
126:
127: public void componentShowing() {
128: // designer.componentShowing();
129: jsfTopComponent.componentShowing();
130:
131: openJsfTopComponentGroupIfNeeded();
132: }
133:
134: public void componentHidden() {
135: // designer.componentHidden();
136: jsfTopComponent.componentHidden();
137:
138: closeJsfTopComponentGroupIfNeeded();
139: }
140:
141: public void componentActivated() {
142: // designer.componentActivated();
143: jsfTopComponent.componentActivated();
144: }
145:
146: public void componentDeactivated() {
147: // designer.componentDeactivated();
148: jsfTopComponent.componentDeactivated();
149: }
150:
151: public UndoRedo getUndoRedo() {
152: // return designer.getUndoRedo();
153: return jsfTopComponent.getUndoRedo();
154: }
155:
156: public void setMultiViewCallback(
157: MultiViewElementCallback multiViewElementCallback) {
158: // designer.setMultiViewCallback(multiViewElementCallback);
159: jsfTopComponent.setMultiViewCallback(multiViewElementCallback);
160: }
161:
162: public CloseOperationState canCloseElement() {
163: // return designer.canCloseElement();
164: return jsfTopComponent.canCloseElement();
165: }
166:
167: // JSF notifications >>>
168: public void modelChanged() {
169: jsfTopComponent.modelChanged();
170: }
171:
172: public void modelRefreshed() {
173: jsfTopComponent.modelRefreshed();
174: }
175:
176: public void nodeChanged(Node node, Node parent,
177: Element[] changedElements) {
178: jsfTopComponent.nodeChanged(node, parent, changedElements);
179: }
180:
181: public void nodeRemoved(Node node, Node parent) {
182: jsfTopComponent.nodeRemoved(node, parent);
183: }
184:
185: public void nodeInserted(Node node, Node parent) {
186: jsfTopComponent.nodeInserted(node, parent);
187: }
188:
189: public void updateErrors() {
190: jsfTopComponent.updateErrors();
191: }
192:
193: public void gridModeUpdated(boolean gridMode) {
194: jsfTopComponent.gridModeUpdated(gridMode);
195: }
196:
197: public void documentReplaced() {
198: jsfTopComponent.documentReplaced();
199: }
200:
201: public void showDropMatch(Element componentRootElement,
202: Element regionElement, int dropType) {
203: jsfTopComponent.showDropMatch(componentRootElement,
204: regionElement, dropType);
205: }
206:
207: public void clearDropMatch() {
208: jsfTopComponent.clearDropMatch();
209: }
210:
211: public void selectComponent(Element componentRootElement) {
212: jsfTopComponent.selectComponent(componentRootElement);
213: }
214:
215: public void inlineEditComponents(Element[] componentRootElements) {
216: jsfTopComponent.inlineEditComponents(componentRootElements);
217: }
218:
219: public void designContextGenerationChanged() {
220: jsfTopComponent.designContextGenerationChanged();
221: }
222:
223: public JsfForm getJsfForm() {
224: return jsfTopComponent.getJsfForm();
225: }
226:
227: public Designer getDesigner() {
228: return jsfTopComponent.getDesigner();
229: }
230:
231: // JSF notifications <<<
232:
233: public TopComponent getMultiViewTopComponent() {
234: return jsfTopComponent.getMultiViewTopComponent();
235: }
236:
237: public void closeMultiView() {
238: jsfTopComponent.closeMultiView();
239: }
240:
241: public boolean isSelectedElement() {
242: return jsfTopComponent.isSelectedInMultiView();
243: }
244:
245: public void modelLoaded() {
246: jsfTopComponent.modelLoaded();
247: }
248:
249: public void requestActive() {
250: jsfTopComponent.requestActive();
251: }
252:
253: private void openJsfTopComponentGroupIfNeeded() {
254: if (isFirstVisibleJsfMultiViewElement()) {
255: TopComponentGroup jsfTopComponentGroup = findJsfDesignerTopComponentGroup();
256: if (jsfTopComponentGroup == null) {
257: log("JSF TopComponentGroup not found, can not open helper windows."); // NOI18N
258: } else {
259: fine("Opening JSF Designer TopComponentGroup"); // NOI18N
260: jsfTopComponentGroup.open();
261: }
262: }
263: }
264:
265: private void closeJsfTopComponentGroupIfNeeded() {
266: if (isLastVisibleJsfMultiViewElement()) {
267: TopComponentGroup jsfTopComponentGroup = findJsfDesignerTopComponentGroup();
268: if (jsfTopComponentGroup == null) {
269: log("JSF TopComponentGroup not found, can not close helper windows."); // NOI18N
270: } else {
271: fine("Closing JSF Designer TopComponentGroup"); // NOI18N
272: jsfTopComponentGroup.close();
273: }
274: }
275: }
276:
277: private TopComponentGroup findJsfDesignerTopComponentGroup() {
278: return WindowManager.getDefault().findTopComponentGroup(
279: TOP_COMPONENT_GROUP_JSF_DESIGNER); // NOI18N
280: }
281:
282: private boolean isFirstVisibleJsfMultiViewElement() {
283: JsfMultiViewElement[] visibleJsfMultiViewElements = findAllVisibleJsfMultiViewElements();
284: if (visibleJsfMultiViewElements.length == 0) {
285: return true;
286: } else if (visibleJsfMultiViewElements.length == 1
287: && visibleJsfMultiViewElements[0] == this ) {
288: return true;
289: } else {
290: return false;
291: }
292: }
293:
294: private boolean isLastVisibleJsfMultiViewElement() {
295: JsfMultiViewElement[] visibleJsfMultiViewElements = findAllVisibleJsfMultiViewElements();
296: if (visibleJsfMultiViewElements.length == 0) {
297: return true;
298: } else if (visibleJsfMultiViewElements.length == 1
299: && visibleJsfMultiViewElements[0] == this ) {
300: return true;
301: } else {
302: return false;
303: }
304: }
305:
306: private static JsfMultiViewElement[] findAllVisibleJsfMultiViewElements() {
307: Set<JsfMultiViewElement> allVisibleJsfMultiViewElements = new HashSet<JsfMultiViewElement>();
308: JsfMultiViewElement[] allJsfMultiViewElements = JsfForm
309: .getJsfMultiViewElements();
310: for (JsfMultiViewElement jsfMultiViewElement : allJsfMultiViewElements) {
311: if (isVisibleJsfMultiViewElement(jsfMultiViewElement)) {
312: allVisibleJsfMultiViewElements.add(jsfMultiViewElement);
313: }
314: }
315: return allVisibleJsfMultiViewElements
316: .toArray(new JsfMultiViewElement[allVisibleJsfMultiViewElements
317: .size()]);
318: }
319:
320: private static boolean isVisibleJsfMultiViewElement(
321: JsfMultiViewElement jsfMultiViewElement) {
322: TopComponent multiView = jsfMultiViewElement
323: .getMultiViewTopComponent();
324: if (multiView == null) {
325: return false;
326: }
327: if (!multiView.isOpened()) {
328: return false;
329: }
330:
331: Mode mode = WindowManager.getDefault().findMode(multiView);
332: if (mode == null) {
333: return false;
334: }
335: if (mode.getSelectedTopComponent() != multiView) {
336: return false;
337: }
338:
339: return jsfMultiViewElement.isSelectedElement();
340: }
341:
342: private static void log(String message) {
343: Logger logger = getLogger();
344: logger.log(Level.INFO, message);
345: }
346:
347: private static void fine(String message) {
348: Logger logger = getLogger();
349: logger.fine(message);
350: }
351:
352: private static Logger getLogger() {
353: return Logger.getLogger(JsfMultiViewElement.class.getName());
354: }
355: }
|