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.insync.action;
043:
044: import com.sun.rave.designtime.DesignBean;
045: import com.sun.rave.designtime.DesignContext;
046: import com.sun.rave.designtime.DesignProperty;
047: import com.sun.rave.designtime.faces.FacesDesignContext;
048: import org.netbeans.modules.visualweb.insync.live.LiveUnit;
049: import org.netbeans.modules.visualweb.insync.models.FacesModel;
050: import org.netbeans.modules.visualweb.spi.designtime.idebridge.action.AbstractDesignBeanAction;
051: import javax.faces.component.UIComponent;
052: import javax.faces.context.FacesContext;
053: import org.openide.ErrorManager;
054: import org.openide.cookies.OpenCookie;
055: import org.openide.filesystems.FileObject;
056: import org.openide.loaders.DataObject;
057: import org.openide.loaders.DataObjectNotFoundException;
058: import org.openide.util.NbBundle;
059:
060: /**
061: * Action which sets/unsets initial focus.
062: * XXX Mostly copies the old code from designer.
063: *
064: * @author Peter Zavadsky
065: * @author Tor Norbye (old functionality implementation -> performAction impl)
066: */
067: public class InitialFocusAction extends AbstractDesignBeanAction {
068:
069: /** Creates a new instance of InitialFocusAction */
070: public InitialFocusAction() {
071: }
072:
073: protected String getDisplayName(DesignBean[] designBeans) {
074: DesignBean designBean = designBeans.length == 0 ? null
075: : designBeans[0];
076:
077: if (designBean != null && hasFocus(designBean)) {
078: return NbBundle.getMessage(InitialFocusAction.class,
079: "LBL_InitialFocusAction_Clear");
080: } else {
081: return NbBundle.getMessage(InitialFocusAction.class,
082: "LBL_InitialFocusAction_Set");
083: }
084: }
085:
086: protected String getIconBase(DesignBean[] designBeans) {
087: return null;
088: }
089:
090: protected boolean isEnabled(DesignBean[] designBeans) {
091: if (designBeans.length == 0) {
092: return false;
093: }
094:
095: DesignBean designBean = designBeans[0];
096: return designBean.getInstance() instanceof javax.faces.component.EditableValueHolder
097: && !hasTableParent(designBean)
098: && getFocusDesignProperty(designBean) != null;
099: }
100:
101: private static boolean hasTableParent(DesignBean bean) {
102: DesignBean parent = bean.getBeanParent();
103:
104: while (parent != null) {
105: Object instance = parent.getInstance();
106:
107: if (instance instanceof javax.faces.component.UIData
108: || instance instanceof com.sun.rave.web.ui.component.TableRowGroup // Braveheart
109: || instance instanceof com.sun.webui.jsf.component.TableRowGroup) { // Woodstock
110: return true;
111: }
112:
113: parent = parent.getBeanParent();
114: }
115:
116: return false;
117: }
118:
119: protected void performAction(DesignBean[] designBeans) {
120: if (designBeans.length == 0) {
121: ErrorManager.getDefault().notify(
122: ErrorManager.INFORMATIONAL,
123: new IllegalArgumentException(
124: "The array shouldn't be empty."));
125: return;
126: }
127:
128: DesignBean designBean = designBeans[0];
129:
130: if (hasFocus(designBean)) {
131: clearFocus(designBean);
132: } else {
133: setFocus(designBean);
134: }
135: }
136:
137: private static boolean hasFocus(DesignBean designBean) {
138: DesignProperty focusProperty = getFocusDesignProperty(designBean);
139: if (focusProperty == null) {
140: return false;
141: }
142:
143: Object value = focusProperty.getValue();
144: if (value == null) {
145: return false;
146: }
147:
148: return value.equals(getDesignBeanClientId(designBean));
149: }
150:
151: private static void clearFocus(DesignBean designBean) {
152: DesignProperty focusProperty = getFocusDesignProperty(designBean);
153: if (focusProperty == null) {
154: return;
155: }
156:
157: focusProperty.setValue(focusProperty.getUnsetValue());
158: }
159:
160: private static void setFocus(DesignBean designBean) {
161: DesignProperty focusProperty = getFocusDesignProperty(designBean);
162: if (focusProperty == null) {
163: return;
164: }
165:
166: focusProperty.setValue(getDesignBeanClientId(designBean));
167: }
168:
169: private static DesignProperty getFocusDesignProperty(
170: DesignBean designBean) {
171: DesignBean parent = designBean;
172: while (parent != null) {
173: Object instance = parent.getInstance();
174: // XXX Why checking the webui body and not body in general?
175: if (instance != null
176: && isInstanceOfBody(instance.getClass())) {
177: break;
178: }
179:
180: parent = parent.getBeanParent();
181: }
182:
183: return parent == null ? null : parent.getProperty("focus"); // NOI18N
184: }
185:
186: private static String getDesignBeanClientId(DesignBean designBean) {
187: Object instance = designBean.getInstance();
188:
189: if (!(instance instanceof UIComponent)) {
190: return null;
191: }
192:
193: DesignContext designContext = designBean.getDesignContext();
194: FacesContext facesContext = ((FacesDesignContext) designContext)
195: .getFacesContext();
196: UIComponent uiComponent = (UIComponent) instance;
197: return uiComponent.getClientId(facesContext);
198: }
199:
200: private static boolean isInstanceOfBody(Class c) {
201: if (c == null) {
202: return false;
203: }
204:
205: // Insync depends on the webui and woodstock modules, so it is OK to use the compile time
206: // dependency on them
207: return com.sun.rave.web.ui.component.Body.class
208: .isAssignableFrom(c)
209: || com.sun.webui.jsf.component.Body.class
210: .isAssignableFrom(c);
211: }
212: }
|