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.DesignProperty;
047: import com.sun.rave.designtime.markup.MarkupDesignBean;
048: import org.netbeans.modules.visualweb.designer.jsf.JsfSupportUtilities;
049: import org.netbeans.modules.visualweb.spi.designtime.idebridge.action.AbstractDesignBeanAction;
050: import javax.swing.Action;
051: import javax.swing.JMenuItem;
052: import javax.swing.event.ChangeListener;
053: import org.netbeans.modules.visualweb.designer.jsf.JsfForm;
054: import org.netbeans.modules.visualweb.designer.jsf.ui.JsfMultiViewElement;
055: import org.openide.ErrorManager;
056: import org.openide.awt.Actions;
057: import org.openide.util.HelpCtx;
058: import org.openide.util.Lookup;
059: import org.openide.util.NbBundle;
060: import org.w3c.dom.Element;
061:
062: /**
063: * Action allowing inline editing.
064: *
065: * @author Peter Zavadsky
066: * @author Tor Norbye (old functionality implementation -> performActionAt impl)
067: */
068: public class InlineEditAction extends AbstractDesignBeanAction {
069:
070: /** Creates a new instance of InlineEditAction. */
071: public InlineEditAction() {
072: }
073:
074: protected String getDisplayName(DesignBean[] designBeans) {
075: if (designBeans != null && designBeans.length == 1) {
076: String[] propertyNames = getInlineEditablePropertyNames(designBeans[0]);
077: if (propertyNames.length == 1) {
078: return NbBundle.getMessage(InlineEditAction.class,
079: "LBL_InlineEditActionProperty",
080: // processDisplayName(propertyNames[0]));
081: findPropertyDisplayName(designBeans[0],
082: propertyNames[0]));
083: }
084: }
085:
086: return NbBundle.getMessage(InlineEditAction.class,
087: "LBL_InlineEditAction");
088: }
089:
090: protected String getIconBase(DesignBean[] designBeans) {
091: return null;
092: }
093:
094: protected boolean isEnabled(DesignBean[] designBeans) {
095: if (designBeans.length == 0) {
096: return false;
097: }
098: return getInlineEditablePropertyNames(designBeans[0]).length > 0;
099: }
100:
101: protected void performAction(DesignBean[] designBeans) {
102: // XXX Strange impl of the Actions.SubMenu(action, model, isPopup). If the model provides one item,
103: // it doesn't call the performAt(0), but this method.
104: new InlineEditMenuModel(designBeans).performActionAt(0);
105: }
106:
107: protected JMenuItem getMenuPresenter(Action contextAwareAction,
108: Lookup.Result result) {
109: return new Actions.SubMenu(contextAwareAction,
110: new InlineEditMenuModel(getDesignBeans(result)), false);
111: }
112:
113: protected JMenuItem getPopupPresenter(Action contextAwareAction,
114: Lookup.Result result) {
115: return new Actions.SubMenu(contextAwareAction,
116: new InlineEditMenuModel(getDesignBeans(result)), true);
117: }
118:
119: private static String[] getInlineEditablePropertyNames(
120: DesignBean designBean) {
121: if (!(designBean instanceof MarkupDesignBean)) {
122: return new String[0];
123: }
124:
125: // return InlineEditor.getEditablePropertyNames(designBean);
126: // return WebForm.getDomProviderService().getEditablePropertyNames(
127: // WebForm.getDomProviderService().getComponentRootElementForMarkupDesignBean((MarkupDesignBean)designBean));
128: return JsfSupportUtilities.getEditableProperties(designBean);
129: }
130:
131: // // XXX Copied from DesignActions.
132: // private static String processDisplayName(String displayName) {
133: // // XXX Copied from DesignerActions.
134: // // PENDING: If I look up the DesignProperty (to get its value and
135: // // check valuebinding), then I might as well call its getDisplayName
136: // // method too.
137: // // Capitalize the property names such that we get proper camel casing
138: // if (displayName.length() > 1 && Character.isLowerCase(displayName.charAt(0))) {
139: // displayName = Character.toUpperCase(displayName.charAt(0)) +
140: // displayName.substring(1);
141: // }
142: // return displayName;
143: // }
144: private static String findPropertyDisplayName(
145: DesignBean designBean, String propertyName) {
146: if (designBean == null) {
147: return propertyName;
148: }
149: DesignProperty designProperty = designBean
150: .getProperty(propertyName);
151: if (designProperty == null) {
152: return propertyName;
153: }
154: return designProperty.getPropertyDescriptor().getDisplayName();
155: }
156:
157: /** Implementation of the actions submenu model. */
158: private static class InlineEditMenuModel implements
159: Actions.SubMenuModel {
160:
161: private final DesignBean designBean;
162: private final String[] propertyNames;
163:
164: public InlineEditMenuModel(DesignBean[] designBeans) {
165: this .designBean = designBeans.length == 0 ? null
166: : designBeans[0];
167: this .propertyNames = getInlineEditablePropertyNames(designBean);
168: }
169:
170: public int getCount() {
171: return propertyNames.length;
172: }
173:
174: public String getLabel(int i) {
175: String displayName = propertyNames[i];
176:
177: // return processDisplayName(displayName);
178: return findPropertyDisplayName(designBean, displayName);
179: }
180:
181: public HelpCtx getHelpCtx(int i) {
182: // XXX Implement?
183: return null;
184: }
185:
186: public void performActionAt(int i) {
187: if (designBean == null) {
188: return;
189: }
190:
191: // XXX Is it only the first in the selection?
192: if (!(designBean instanceof MarkupDesignBean)) {
193: ErrorManager.getDefault().notify(
194: ErrorManager.INFORMATIONAL,
195: new IllegalStateException(
196: "Bean should be MarkupDesignBean bean="
197: + designBean)); // NOI18N
198: return;
199: }
200:
201: MarkupDesignBean bean = (MarkupDesignBean) designBean;
202:
203: // WebForm webform = WebForm.findWebFormForDesignContext(bean.getDesignContext());
204: //
205: // if (webform == null) {
206: // ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL,
207: // new NullPointerException("Can't find webform for design context=" // NOI18N
208: // + bean.getDesignContext()));
209: // return;
210: // }
211: Designer designer = JsfSupportUtilities
212: .findDesignerForDesignContext(bean
213: .getDesignContext());
214: if (designer == null) {
215: ErrorManager.getDefault().notify(
216: ErrorManager.INFORMATIONAL,
217: new NullPointerException(
218: "Can't find designer for design context=" // NOI18N
219: + bean.getDesignContext()));
220: return;
221: }
222:
223: String propertyName = propertyNames[i];
224:
225: // if (webform.getManager().isInlineEditing()
226: //// && webform.getManager().getInlineEditor().isEditing(bean, propertyName)) {
227: // && webform.getManager().getInlineEditor().isEditing(
228: // WebForm.getDomProviderService().getComponentRootElementForMarkupDesignBean(bean), propertyName)) {
229: // return;
230: // } else {
231: // webform.getManager().finishInlineEditing(false);
232: // }
233: //
234: //// ModelViewMapper mapper = webform.getMapper();
235: //// CssBox box = mapper.findBox(bean);
236: // Element componentRootElement = WebForm.getDomProviderService().getComponentRootElementForMarkupDesignBean(bean);
237: // CssBox box = ModelViewMapper.findBoxForComponentRootElement(webform.getPane().getPageBox(), componentRootElement);
238: // webform.getTopComponent().requestActive();
239: // webform.getManager().startInlineEditing(componentRootElement, propertyName, box, true, true, null, false);
240: JsfMultiViewElement jsfMultiViewElement = JsfForm
241: .findJsfMultiViewElementForDesigner(designer);
242: if (jsfMultiViewElement != null) {
243: jsfMultiViewElement.requestActive();
244: }
245: Element componentRootElement = JsfSupportUtilities
246: .getComponentRootElementForDesignBean(bean);
247: designer.startInlineEditing(componentRootElement,
248: propertyName);
249: }
250:
251: public void addChangeListener(ChangeListener changeListener) {
252: // dummy, this model is not mutable.
253: }
254:
255: public void removeChangeListener(ChangeListener changeListener) {
256: // dummy, this model is not mutable.
257: }
258:
259: } // End of InlineEditMenuModel.
260: }
|