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.visualweb.web.ui.dt.component.vforms;
042:
043: import javax.faces.component.ActionSource;
044: import javax.faces.component.EditableValueHolder;
045: import com.sun.rave.designtime.DesignBean;
046: import com.sun.rave.designtime.DesignContext;
047: import com.sun.rave.designtime.DisplayAction;
048: import com.sun.rave.web.ui.component.Form;
049: import java.util.List;
050: import java.util.ArrayList;
051:
052: public class VirtualFormsHelper {
053:
054: public static DisplayAction getContextItem(DesignContext context) {
055: DesignBean formBean = null;
056: DesignBean rootBean = context.getRootContainer();
057: if (rootBean.getInstance() instanceof Form) { //just in case
058: formBean = rootBean;
059: } else {
060: formBean = findFormBeanFromRoot(rootBean);
061: }
062: if (formBean != null) {
063: return new VirtualFormsCustomizerAction(formBean);
064: }
065: return null;
066: }
067:
068: public static DisplayAction getContextItem(DesignBean bean) {
069: if (findFormBean(bean) == null) {
070: return null;
071: }
072: if (bean.getInstance() instanceof EditableValueHolder
073: || bean.getInstance() instanceof ActionSource) {
074: return new EditVirtualFormsCustomizerAction(bean);
075: }
076: return null;
077: }
078:
079: public static DisplayAction getContextItem(DesignBean[] beans) {
080: if (findFormBean(beans) == null) {
081: return null;
082: }
083: for (int i = 0; beans != null && i < beans.length; i++) {
084: if (beans[i].getInstance() instanceof EditableValueHolder
085: || beans[i].getInstance() instanceof ActionSource) {
086: return new EditVirtualFormsCustomizerAction(beans);
087: }
088: }
089: return null;
090: }
091:
092: public static DesignBean findFormBean(DesignBean[] beans) {
093: if (beans == null) {
094: return null;
095: }
096: for (int i = 0; i < beans.length; i++) {
097: DesignBean formBean = findFormBean(beans[i]);
098: if (formBean != null) {
099: return formBean;
100: }
101: }
102: return null;
103: }
104:
105: public static DesignBean findFormBean(DesignBean bean) {
106: if (bean == null) {
107: return null;
108: }
109: if (bean.getInstance() instanceof Form) {
110: return bean;
111: }
112: return findFormBean(bean.getBeanParent());
113: }
114:
115: private static DesignBean findFormBeanFromRoot(DesignBean parent) {
116: if (parent == null) {
117: return null;
118: }
119: DesignBean[] childBeans = parent.getChildBeans();
120: for (int i = 0; childBeans != null && i < childBeans.length; i++) {
121: DesignBean bean = childBeans[i];
122: if (bean.getInstance() instanceof Form) {
123: return bean;
124: }
125: DesignBean formBean = findFormBeanFromRoot(bean);
126: if (formBean != null) {
127: return formBean;
128: }
129: }
130: return null;
131: }
132:
133: public static String getNewVirtualFormName(List vformsList) {
134: List nameList = new ArrayList();
135: for (int i = 0; vformsList != null && i < vformsList.size(); i++) {
136: nameList.add(((Form.VirtualFormDescriptor) vformsList
137: .get(i)).getName());
138: }
139:
140: String name = java.util.ResourceBundle
141: .getBundle(
142: "org/netbeans/modules/visualweb/web/ui/dt/component/vforms/Bundle")
143: .getString("newVirtualForm"); // NOI18N
144:
145: for (int i = 1; i < 999; i++) {
146: if (!nameList.contains(name + i)) {
147: name = name + i;
148: break;
149: }
150: }
151:
152: return name;
153: }
154: }
|