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.faces.dt_1_2.component.html;
042:
043: import javax.faces.component.UIComponent;
044:
045: import org.w3c.dom.DocumentFragment;
046: import org.w3c.dom.Element;
047: import org.w3c.dom.Node;
048: import org.w3c.dom.NodeList;
049:
050: import com.sun.rave.designtime.DisplayAction;
051: import com.sun.rave.designtime.Result;
052: import com.sun.rave.designtime.DesignBean;
053: import com.sun.rave.designtime.DesignProperty;
054: import com.sun.rave.designtime.DesignEvent;
055: import com.sun.rave.designtime.markup.MarkupDesignBean;
056: import com.sun.rave.designtime.markup.MarkupDesignInfo;
057: import com.sun.rave.designtime.markup.MarkupPosition;
058: import com.sun.rave.designtime.markup.MarkupRenderContext;
059: import javax.faces.component.html.HtmlOutputLabel;
060: import javax.faces.component.html.HtmlOutputText;
061: import org.netbeans.modules.visualweb.faces.dt.util.ComponentBundle;
062:
063: public class HtmlOutputLabelDesignInfo implements MarkupDesignInfo {
064:
065: private static final ComponentBundle bundle = ComponentBundle
066: .getBundle(HtmlOutputLabelDesignInfo.class);
067:
068: public Class getBeanClass() {
069: return HtmlOutputLabel.class;
070: }
071:
072: public boolean acceptParent(DesignBean parentBean,
073: DesignBean childBean, Class childClass) {
074: return true;
075: }
076:
077: public boolean acceptChild(DesignBean parentBean,
078: DesignBean childBean, Class childClass) {
079: return true;
080: }
081:
082: public Result beanCreatedSetup(DesignBean bean) {
083: try {
084: bean.getProperty("for").setValue(bean.getInstanceName()); //NOI18N
085: DesignBean output = bean.getDesignContext().createBean(
086: HtmlOutputText.class.getName(), bean, null);
087: output.setInstanceName(bean.getInstanceName() + "Text",
088: true); //NOI18N
089: output.getProperty("value").setValue(
090: bundle.getMessage("htmlOutputLabel")); //NOI18N
091: } catch (Exception e) {
092: e.printStackTrace();
093: }
094: return Result.SUCCESS;
095: }
096:
097: public Result beanPastedSetup(DesignBean bean) {
098: return Result.SUCCESS;
099: }
100:
101: public Result beanDeletedCleanup(DesignBean bean) {
102: return Result.SUCCESS;
103: }
104:
105: public DisplayAction[] getContextItems(DesignBean bean) {
106: return new DisplayAction[0];//new DisplayAction[] { new RowDataBindingCustomizerAction(), new ObjectBindingCustomizerAction() };
107: }
108:
109: public boolean acceptLink(DesignBean targetBean,
110: DesignBean sourceBean, Class sourceClass) {
111: return UIComponent.class.isAssignableFrom(sourceClass);
112: }
113:
114: public Result linkBeans(DesignBean targetBean, DesignBean sourceBean) {
115: if (sourceBean.getInstance() instanceof UIComponent
116: && sourceBean != targetBean) {
117: DesignProperty prop = targetBean.getProperty("for"); // NOI18N
118: if (prop != null) {
119: prop.setValue(sourceBean.getInstanceName());
120: }
121: }
122: return Result.SUCCESS;
123: }
124:
125: public void beanContextActivated(DesignBean bean) {
126: }
127:
128: public void beanContextDeactivated(DesignBean bean) {
129: }
130:
131: public void instanceNameChanged(DesignBean bean,
132: String oldInstanceName) {
133: }
134:
135: public void beanChanged(DesignBean bean) {
136: }
137:
138: public void propertyChanged(DesignProperty prop, Object oldValue) {
139: }
140:
141: public void eventChanged(DesignEvent event) {
142: }
143:
144: public void customizeRender(MarkupDesignBean bean,
145: MarkupRenderContext renderContext) {
146: DocumentFragment documentFragment = renderContext
147: .getDocumentFragment();
148: MarkupPosition begin = renderContext.getBeginPosition();
149: MarkupPosition end = renderContext.getEndPosition();
150:
151: if (begin == end) {
152: return;
153: }
154: assert begin.getUnderParent() == end.getUnderParent();
155: Node child = begin.getBeforeSibling();
156: Node stop = end.getBeforeSibling();
157: for (child = begin.getBeforeSibling(); child != stop; child = child
158: .getNextSibling()) {
159: if (child.getNodeType() == Node.ELEMENT_NODE) {
160: Element element = (Element) child;
161: if ("label".equals(element.getTagName())) { //NOI18N
162: // Must have a for attribute
163: /*
164: * this will never trigger since a for was added in annotate create
165: * without the for in annotate create, the jsf code asserts before
166: * we get to customizeRender
167: */
168: if (!element.hasAttribute("for")) { //NOI18N
169: element.setAttribute("for", ""); //NOI18N
170: }
171: // If no PCDATA, set the instancename as the PCDATA
172: boolean hasPcData = false;
173: NodeList children = element.getChildNodes();
174: int childCount = children.getLength();
175: for (int j = 0; j < childCount; j++) {
176: if (children.item(j).getNodeType() == Node.TEXT_NODE) {
177: hasPcData = true;
178: break;
179: }
180: }
181: if (!hasPcData) {
182: element
183: .appendChild(element.getOwnerDocument()
184: .createTextNode(
185: bean.getInstanceName()));
186: }
187: }
188: }
189: }
190: }
191: }
|