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.insync.live;
042:
043: import org.openide.util.NbBundle;
044: import org.netbeans.modules.visualweb.extension.openide.util.Trace;
045:
046: import com.sun.rave.designtime.EventDescriptor;
047: import org.netbeans.modules.visualweb.insync.UndoEvent;
048: import org.netbeans.modules.visualweb.insync.beans.Event;
049: import org.netbeans.modules.visualweb.insync.beans.EventSet;
050: import org.netbeans.modules.visualweb.insync.java.EventMethod;
051:
052: /**
053: * DesignEvent implementation based on delegation to beans.Event and subclasses, using Java and/or
054: * markup source.
055: *
056: * @author Carl Quinn
057: */
058: public class BeansDesignEvent extends SourceDesignEvent {
059:
060: Event event; // the persisted event state if set, i.e. not default, null if not set
061:
062: /**
063: * @param descriptor
064: * @param lbean
065: */
066: public BeansDesignEvent(EventDescriptor descriptor,
067: BeansDesignBean lbean) {
068: super (descriptor, lbean);
069: EventSet es = getEventSet();
070: if (es != null)
071: event = es.getEvent(descriptor
072: .getListenerMethodDescriptor().getName());
073: assert Trace.trace("insync.live", "JLE event:" + event);
074: }
075:
076: /**
077: * @return
078: */
079: private EventSet getEventSet() {
080: BeansDesignBean lbean = (BeansDesignBean) liveBean;
081: EventDescriptor ed = getEventDescriptor();
082: EventSet es = lbean.bean.getEventSet(ed.getEventSetDescriptor()
083: .getName());
084: return es;
085: }
086:
087: /**
088: * @return
089: */
090: private EventSet ensureEventSet() {
091: BeansDesignBean lbean = (BeansDesignBean) getDesignBean();
092: EventDescriptor ed = getEventDescriptor();
093: EventSet es = lbean.bean.setEventSet(ed.getEventSetDescriptor()
094: .getName());
095: return es;
096: }
097:
098: /**
099: * @return
100: */
101: public Event getEvent() {
102: return event;
103: }
104:
105: /*
106: * @see com.sun.rave.designtime.DesignEvent#getHandlerName()
107: */
108: public String getHandlerName() {
109: return event != null ? event.getHandlerName() : null;
110: }
111:
112: protected void initEvent(String handlerName) {
113: EventSet es = ensureEventSet();
114: event = es.setEvent(getEventDescriptor(), getEventDescriptor()
115: .getListenerMethodDescriptor(), handlerName);
116: }
117:
118: /*
119: * @see com.sun.rave.designtime.DesignEvent#setHandlerName(java.lang.String)
120: */
121: public boolean setHandlerName(String handlerName) {
122: if (handlerName == null)
123: handlerName = getDefaultHandlerName();
124:
125: if (handlerName.trim().length() == 0) {
126: return removeHandler();
127: }
128:
129: UndoEvent undoEvent = null;
130: try {
131: String description = NbBundle.getMessage(
132: BeansDesignEvent.class, "SetHandlerName",
133: handlerName); //NOI18N
134: undoEvent = liveBean.unit.model.writeLock(description);
135: if (event == null) {
136: initEvent(handlerName);
137: } else {
138: event.setHandler(handlerName);
139: }
140: } finally {
141: liveBean.unit.model.writeUnlock(undoEvent);
142: }
143: //!CQ HACK to kick our MethodBind live property shadow so the live part is up to date
144: /*
145: if (event instanceof MethodBindEvent) {
146: PropertyDescriptor pdescr = ((MethodBindEvent)event).getBindingProperty().getDescriptor();
147: SourceDesignProperty lp = (SourceDesignProperty)liveBean.getProperty(pdescr);
148: lp.initLive();
149: }*/
150: return true;
151: }
152:
153: /*
154: * @see com.sun.rave.designtime.DesignEvent#isHandled()
155: */
156: public boolean isHandled() {
157: return event == null ? false : event.getHandlerMethod() != null;
158: }
159:
160: /*
161: * @see com.sun.rave.designtime.DesignEvent#removeHandler()
162: */
163: public boolean removeHandler() {
164: if (event != null) {
165: UndoEvent undoEvent = null;
166: try {
167: String description = NbBundle.getMessage(
168: BeansDesignEvent.class, "RemoveHandler"); //NOI18N
169: undoEvent = liveBean.unit.model.writeLock(description);
170: event.getEventSet().unsetEvent(event);
171: event = null;
172: } finally {
173: liveBean.unit.model.writeUnlock(undoEvent);
174: }
175: }
176: return true;
177: }
178:
179: /*
180: * @see com.sun.rave.designtime.DesignEvent#setHandlerMethodSource(java.lang.String)
181: */
182: public void setHandlerMethodSource(String methodBody)
183: throws IllegalArgumentException {
184: UndoEvent undoEvent = null;
185: try {
186: String description = NbBundle.getMessage(
187: BeansDesignEvent.class, "SetHandlerMethodSource",
188: methodBody != null ? methodBody : ""); //NOI18N
189: undoEvent = liveBean.unit.model.writeLock(description);
190: event.getHandlerMethod().replaceBody(methodBody);
191: } finally {
192: liveBean.unit.model.writeUnlock(undoEvent);
193: }
194: }
195:
196: /*
197: * @see com.sun.rave.designtime.DesignEvent#getHandlerMethodSource()
198: */
199: public String getHandlerMethodSource() {
200: return (event != null && event.getHandlerMethod() != null) ? event
201: .getHandlerMethod().getBodyText()
202: : null;
203: }
204:
205: /**
206: * @return the string literal if the last return statement of handler
207: * is a string return statement;
208: */
209: public String getHandlerMethodReturn() {
210: return (event != null && event.getHandlerMethod() != null) ? event
211: .getHandlerMethod().getMethodReturn()
212: : null;
213: }
214:
215: /**
216: * Update the last null return statement if the link is created for first
217: * time, else all the matching string return statements are updated
218: */
219: public void updateReturnStrings(String oldStr, String newStr) {
220: if (event != null) {
221: EventMethod method = event.getHandlerMethod();
222: if (method != null) {
223: //oldStr will be null when the link is created first time
224: if (oldStr == null) { //NOI18N
225: method.updateLastReturnStatement(newStr);
226: } else {
227: method.updateReturnStrings(oldStr, newStr);
228: }
229: }
230: }
231: }
232: }
|