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.action;
043:
044: import org.netbeans.modules.visualweb.api.designer.Designer;
045: import com.sun.rave.designtime.DesignBean;
046: import com.sun.rave.designtime.markup.MarkupDesignBean;
047: import org.netbeans.modules.visualweb.designer.jsf.JsfSupportUtilities;
048: import org.netbeans.modules.visualweb.insync.Util;
049: import org.netbeans.modules.visualweb.spi.designtime.idebridge.action.AbstractDesignBeanAction;
050: import org.openide.ErrorManager;
051: import org.openide.util.NbBundle;
052: import org.w3c.dom.Element;
053:
054: /**
055: * Action selecting the parent bean.
056: *
057: * @author Peter Zavadsky
058: * @author Tor Norbye (old functionality implementation -> isEnabled, performAction impl)
059: */
060: public class SelectParentAction extends AbstractDesignBeanAction {
061:
062: /** Creates a new instance of SelectParentAction */
063: public SelectParentAction() {
064: }
065:
066: protected String getDisplayName(DesignBean[] designBeans) {
067: return NbBundle.getMessage(SelectParentAction.class,
068: "LBL_SelectParentAction");
069: }
070:
071: protected String getIconBase(
072: com.sun.rave.designtime.DesignBean[] designBeans) {
073: return null;
074: }
075:
076: protected boolean isEnabled(DesignBean[] designBeans) {
077: if (designBeans.length == 0) {
078: return false;
079: }
080:
081: DesignBean designBean = designBeans[0];
082: return canSelectParent(designBean);
083: }
084:
085: protected void performAction(DesignBean[] designBeans) {
086: if (designBeans.length == 0) {
087: return;
088: }
089:
090: DesignBean designBean = designBeans[0];
091: selectParent(designBean);
092: }
093:
094: private static boolean canSelectParent(DesignBean designBean) {
095: if (designBean == null) {
096: return false;
097: }
098:
099: DesignBean parent = findSelectableParent(designBean);
100: if (parent == null) {
101: return false;
102: }
103:
104: if (parent == parent.getDesignContext().getRootContainer()) {
105: return false;
106: }
107:
108: if (Util.isSpecialBean(parent)) {
109: return false;
110: }
111:
112: return true;
113: }
114:
115: private static void selectParent(DesignBean designBean) {
116: if (designBean == null) {
117: return;
118: }
119:
120: DesignBean parent = findSelectableParent(designBean);
121: if (parent == null) {
122: return;
123: }
124:
125: Element componentRootElement = JsfSupportUtilities
126: .getComponentRootElementForDesignBean(parent);
127: if (componentRootElement == null) {
128: return;
129: }
130:
131: Designer designer = JsfSupportUtilities
132: .findDesignerForDesignContext(designBean
133: .getDesignContext());
134: if (designer == null) {
135: ErrorManager.getDefault().notify(
136: ErrorManager.INFORMATIONAL,
137: new NullPointerException(
138: "Can't find designer for design context=" // NOI18N
139: + designBean.getDesignContext()));
140: return;
141: }
142: designer.selectComponent(componentRootElement);
143: }
144:
145: // XXX #94766 There are component root elements not mapped to boxes (?!)
146: private static DesignBean findSelectableParent(DesignBean designBean) {
147: if (designBean == null) {
148: return null;
149: }
150:
151: Designer designer = JsfSupportUtilities
152: .findDesignerForDesignContext(designBean
153: .getDesignContext());
154: if (designer == null) {
155: // XXX
156: return null;
157: }
158:
159: DesignBean parent = designBean.getBeanParent();
160: while (parent != null) {
161: Element componentRootElement = JsfSupportUtilities
162: .getComponentRootElementForDesignBean(parent);
163: if (componentRootElement != null) {
164: if (designer
165: .findBoxForComponentRootElement(componentRootElement) != null) {
166: return parent;
167: }
168: }
169:
170: parent = parent.getBeanParent();
171: }
172: return null;
173: }
174:
175: }
|