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 org.netbeans.modules.visualweb.insync.UndoEvent;
045: import org.netbeans.modules.visualweb.insync.beans.Bean;
046: import org.netbeans.modules.visualweb.insync.faces.FacesBean;
047: import org.netbeans.modules.visualweb.insync.faces.FacesBean.UsageInfo;
048: import org.netbeans.modules.visualweb.insync.live.FacesDesignBean;
049: import org.netbeans.modules.visualweb.insync.live.LiveUnit;
050: import org.netbeans.modules.visualweb.insync.models.FacesModel;
051: import org.openide.DialogDisplayer;
052: import org.openide.NotifyDescriptor;
053: import org.openide.util.NbBundle;
054:
055: import com.sun.rave.designtime.DesignBean;
056: import com.sun.rave.designtime.DisplayAction;
057: import com.sun.rave.designtime.Result;
058: import com.sun.rave.designtime.impl.BasicDisplayAction;
059:
060: /**
061: * Action which adds/removes binding attribute.
062: *
063: * @author Sandip Chitale
064: */
065: public class AddRemoveBindingAttributeAction extends
066: AbstractDisplayActionAction {
067:
068: public AddRemoveBindingAttributeAction() {
069: }
070:
071: @Override
072: protected String getDefaultDisplayName() {
073: return NbBundle.getMessage(
074: AddRemoveBindingAttributeAction.class,
075: "LBL_AddRemoveBindingAttributeActionName");
076: }
077:
078: private static DisplayAction[] EMPTY_displayActions = new DisplayAction[0];
079:
080: @Override
081: protected DisplayAction[] getDisplayActions(DesignBean[] designBeans) {
082: if (designBeans.length == 1) {
083: DesignBean designBean = designBeans.length == 0 ? null
084: : designBeans[0];
085: if (isBindingAttributeCapable(designBean)) {
086: if (designBean != null
087: && hasBindingAttribute(designBean)) {
088: return new DisplayAction[] { new RemoveBindingAttributeAction(
089: designBean) };
090: } else {
091: return new DisplayAction[] { new AddBindingAttributeAction(
092: designBean) };
093: }
094: }
095: }
096: return EMPTY_displayActions;
097: }
098:
099: static class AddBindingAttributeAction extends BasicDisplayAction {
100: private DesignBean designBean;
101:
102: public AddBindingAttributeAction(DesignBean designBean) {
103: super (NbBundle.getMessage(
104: AddRemoveBindingAttributeAction.class,
105: "LBL_AddBindingAttributeActionName"));
106: this .designBean = designBean;
107: }
108:
109: public Result invoke() {
110: if (designBean instanceof FacesDesignBean) {
111: FacesDesignBean facesDesignBean = (FacesDesignBean) designBean;
112: FacesModel facesModel = ((LiveUnit) designBean
113: .getDesignContext()).getModel();
114: UndoEvent undo = null;
115: try {
116: undo = facesModel.writeLock(NbBundle.getMessage(
117: AddRemoveBindingAttributeAction.class,
118: "LBL_RemoveBindingAttributeActionName"));
119: facesDesignBean.addBinding();
120: } finally {
121: facesModel.writeUnlock(undo);
122: }
123: }
124: return Result.SUCCESS;
125: }
126: }
127:
128: static class RemoveBindingAttributeAction extends
129: BasicDisplayAction {
130: private DesignBean designBean;
131:
132: public RemoveBindingAttributeAction(DesignBean designBean) {
133: super (NbBundle.getMessage(
134: AddRemoveBindingAttributeAction.class,
135: "LBL_RemoveBindingAttributeActionName"));
136: this .designBean = designBean;
137: }
138:
139: public Result invoke() {
140: if (designBean instanceof FacesDesignBean) {
141: FacesDesignBean facesDesignBean = (FacesDesignBean) designBean;
142: UsageInfo usageInfo = facesDesignBean.getUsageInfo();
143: NotifyDescriptor notifyDescriptor;
144: switch (usageInfo.getUsageStatus()) {
145: case USED:
146: notifyDescriptor = new NotifyDescriptor.Message(
147: NbBundle
148: .getMessage(
149: AddRemoveBindingAttributeAction.class,
150: "ERROR_BINDING_IN_USE")); // NOI18N
151: DialogDisplayer.getDefault().notify(
152: notifyDescriptor);
153: return Result.FAILURE;
154: case INIT_USE_ONLY:
155: notifyDescriptor = new NotifyDescriptor.Confirmation(
156: NbBundle
157: .getMessage(
158: AddRemoveBindingAttributeAction.class,
159: "WARNING_BINDING_IN_USE", // NOI18N
160: usageInfo
161: .getInitializedProperties()
162: .toString()),
163: NbBundle
164: .getMessage(
165: AddRemoveBindingAttributeAction.class,
166: "TITLE_REMOVE_BINDING_ATTRIBUTE") // NOI18N
167: , NotifyDescriptor.OK_CANCEL_OPTION);
168: if (DialogDisplayer.getDefault().notify(
169: notifyDescriptor) == NotifyDescriptor.OK_OPTION) {
170: // fall through
171: } else {
172: return Result.FAILURE;
173: }
174: case NOT_USED:
175: break;
176: }
177: FacesModel facesModel = ((LiveUnit) designBean
178: .getDesignContext()).getModel();
179: UndoEvent undo = null;
180: try {
181: undo = facesModel.writeLock(NbBundle.getMessage(
182: AddRemoveBindingAttributeAction.class,
183: "LBL_RemoveBindingAttributeActionName"));
184: facesDesignBean.removeBinding();
185: } finally {
186: facesModel.writeUnlock(undo);
187: }
188: }
189: return Result.SUCCESS;
190: }
191: }
192:
193: private static boolean hasBindingAttribute(DesignBean designBean) {
194: if (designBean instanceof FacesDesignBean) {
195: FacesDesignBean facesDesignBean = (FacesDesignBean) designBean;
196: Bean bean = facesDesignBean.getBean();
197: if (bean instanceof FacesBean) {
198: FacesBean facesBean = (FacesBean) bean;
199: return facesBean.getAttr(facesBean.BINDING_ATTR) != null;
200: }
201: }
202: return false;
203: }
204:
205: private static boolean isBindingAttributeCapable(
206: DesignBean designBean) {
207: if (designBean instanceof FacesDesignBean) {
208: FacesDesignBean facesDesignBean = (FacesDesignBean) designBean;
209: if (facesDesignBean.getBean() instanceof FacesBean) {
210: return true;
211: }
212: }
213: return false;
214: }
215: }
|