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.xhtml;
042:
043: import com.sun.rave.designtime.DesignBean;
044: import com.sun.rave.designtime.Result;
045: import com.sun.rave.designtime.event.DesignProjectListener;
046: import com.sun.rave.designtime.markup.MarkupDesignBean;
047: import com.sun.rave.designtime.markup.MarkupDesignContext;
048: import com.sun.rave.designtime.markup.MarkupPosition;
049: import com.sun.rave.propertyeditors.domains.ResourceBundlesDomain;
050:
051: import java.util.BitSet;
052:
053: import org.w3c.dom.Element;
054: import org.w3c.dom.Node;
055: import org.w3c.dom.NodeList;
056:
057: /**
058: * DesignInfo for the F_LoadBundle component
059: *
060: * @author Craig McClanahan
061: * @author gjmurphy
062: */
063: public class F_LoadBundleDesignInfo extends XhtmlDesignInfo implements
064: DesignProjectListener {
065:
066: /**
067: * <p>Set the default value of the <code>var</code> property, based on the
068: * number of <code><f:loadBundle></code> tags that exist in the page.
069: * Also, set the <code>basename</code> property to a default resource
070: * bundle name that should also be added to the project template.</p>
071: *
072: * @param bean {@link DesignBean} for the new component
073: */
074: public Result beanCreatedSetup(DesignBean bean) {
075: bean.getProperty("var").setValue(var(bean));
076: bean.getProperty("basename").setValue(basename(bean));
077: // Attempt to make the load bundle component the first child of the
078: // HTML "html" element, so that element attributes within the head can
079: // reference the resource bundle
080: if (bean.getDesignContext() instanceof MarkupDesignContext) {
081: // Look for parent bean that corresponds to "html" markup element
082: DesignBean parentBean = bean.getBeanParent();
083: while (parentBean != null
084: && parentBean instanceof MarkupDesignBean
085: && !"html".equals(((MarkupDesignBean) parentBean)
086: .getElement().getLocalName())) {
087: parentBean = parentBean.getBeanParent();
088: }
089: // If none found, and top of tree reached, check the siblings of the
090: // original parent
091: if (parentBean == null
092: || !(parentBean instanceof MarkupDesignBean)) {
093: DesignBean[] siblingBeans = bean.getBeanParent()
094: .getChildBeans();
095: for (int i = 0; i < siblingBeans.length; i++) {
096: if (siblingBeans[i] instanceof MarkupDesignBean
097: && "html"
098: .equals(((MarkupDesignBean) siblingBeans[i])
099: .getElement()
100: .getLocalName()))
101: parentBean = siblingBeans[i];
102: }
103: }
104: if (parentBean != null
105: && parentBean instanceof MarkupDesignBean
106: && "html".equals(((MarkupDesignBean) parentBean)
107: .getElement().getLocalName())) {
108: final MarkupDesignContext designContext = (MarkupDesignContext) bean
109: .getDesignContext();
110: final Element finalParentElem = ((MarkupDesignBean) parentBean)
111: .getElement();
112: final DesignBean finalParentBean = parentBean;
113: final DesignBean this Bean = bean;
114: javax.swing.SwingUtilities.invokeLater(new Runnable() {
115: public void run() {
116: NodeList children = finalParentElem
117: .getChildNodes();
118: Element firstChildElem = null;
119: for (int i = 0; i < children.getLength()
120: && firstChildElem == null; i++) {
121: if (children.item(i).getNodeType() == Node.ELEMENT_NODE)
122: firstChildElem = (Element) children
123: .item(i);
124: }
125: if (firstChildElem != null) {
126: MarkupPosition position = new MarkupPosition(
127: finalParentElem, firstChildElem);
128: if (designContext.canMoveBean(this Bean,
129: finalParentBean, position))
130: designContext.moveBean(this Bean,
131: finalParentBean, position);
132: }
133: }
134: });
135: }
136: }
137: return Result.SUCCESS;
138: }
139:
140: public Class getBeanClass() {
141: return F_LoadBundle.class;
142: }
143:
144: public void contextActivated(
145: com.sun.rave.designtime.DesignContext context) {
146: }
147:
148: public void contextClosed(
149: com.sun.rave.designtime.DesignContext context) {
150: }
151:
152: public void contextDeactivated(
153: com.sun.rave.designtime.DesignContext context) {
154: }
155:
156: public void contextOpened(
157: com.sun.rave.designtime.DesignContext context) {
158: }
159:
160: /**
161: * Return an appropriate default resource bundle name to be used for
162: * initializing the <code>basename</code> property.
163: *
164: * @param bean {@link DesignBean} for the new component
165: */
166: private String basename(DesignBean bean) {
167: ResourceBundlesDomain domain = new ResourceBundlesDomain();
168: domain.setDesignProperty(bean.getProperty("basename"));
169: com.sun.rave.propertyeditors.domains.Element[] elements = domain
170: .getElements();
171: String value = null;
172: for (int i = 0; i < elements.length && value == null; i++)
173: value = (String) elements[i].getValue();
174: return value;
175: }
176:
177: // Base name for generated value for "var" property
178: private static final String BASE_NAME = "messages";
179:
180: /**
181: * Return a unique value to be used to initialize the <code>var</code>
182: * property of a new component.
183: *
184: * @param bean {@link DesignBean} for the new component
185: */
186: private String var(DesignBean bean) {
187: // Identify all the suffixes that have already been used
188: BitSet used = new BitSet();
189: DesignBean beans[] = bean.getDesignContext().getBeansOfType(
190: getBeanClass());
191: for (int i = 0; i < beans.length; i++) {
192: String var = (String) beans[i].getProperty("var")
193: .getValue(); //NOI18N
194: if ((var == null) || !var.startsWith(BASE_NAME)) {
195: continue;
196: }
197: String suffix = var.substring(BASE_NAME.length());
198: try {
199: int j = Integer.valueOf(suffix).intValue();
200: if (j >= 0) {
201: used.set(j);
202: }
203: } catch (NumberFormatException e) {
204: ;
205: }
206: }
207: // Use the first positive suffix not yet used
208: int n = 1;
209: while (true) {
210: if (used.get(n)) {
211: n++;
212: continue;
213: }
214: return BASE_NAME + n;
215: }
216: }
217:
218: }
|