001: /*
002: * The contents of this file are subject to the terms of the Common Development
003: * and Distribution License (the License). You may not use this file except in
004: * compliance with the License.
005: *
006: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
007: * or http://www.netbeans.org/cddl.txt.
008: *
009: * When distributing Covered Code, include this CDDL Header Notice in each file
010: * and include the License file at http://www.netbeans.org/cddl.txt.
011: * If applicable, add the following below the CDDL Header, with the fields
012: * enclosed by brackets [] replaced by your own identifying information:
013: * "Portions Copyrighted [year] [name of copyright owner]"
014: *
015: * The Original Software is NetBeans. The Initial Developer of the Original
016: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
017: * Microsystems, Inc. All Rights Reserved.
018: */
019: package org.netbeans.modules.bpel.nodes.actions;
020:
021: import org.netbeans.modules.bpel.nodes.actions.BpelNodeAction;
022: import java.io.IOException;
023: import java.util.concurrent.Callable;
024: import org.netbeans.modules.bpel.model.api.Activity;
025: import org.netbeans.modules.bpel.model.api.ActivityHolder;
026: import org.netbeans.modules.bpel.model.api.BpelContainer;
027: import org.netbeans.modules.bpel.model.api.BpelEntity;
028: import org.netbeans.modules.bpel.model.api.BpelModel;
029: import org.netbeans.modules.bpel.model.api.CompositeActivity;
030: import org.netbeans.modules.bpel.model.api.ExtendableActivity;
031: import org.netbeans.modules.bpel.model.api.Scope;
032: import org.netbeans.modules.bpel.model.api.ScopeHolder;
033: import org.netbeans.modules.bpel.model.api.Sequence;
034: import org.openide.ErrorManager;
035: import org.openide.util.NbBundle;
036:
037: /**
038: *
039: * @author Vitaly Bychkov
040: * @version 1.0
041: */
042: public abstract class AbstractWrapWithAction<Wrapper extends Activity>
043: extends BpelNodeAction implements WrapWithAction<Wrapper> {
044:
045: public AbstractWrapWithAction() {
046: }
047:
048: protected boolean enable(BpelEntity[] bpelEntities) {
049: if (!super .enable(bpelEntities)) {
050: return false;
051: }
052:
053: BpelEntity entity = bpelEntities[0];
054: BpelContainer parent = entity.getParent();
055: return parent != null /* TODO r && isContainerSupportChildType(parent)*/;
056: }
057:
058: public boolean isContainerSupportChildType(BpelContainer container) {
059: if (container instanceof ScopeHolder) {
060: return false;
061: }
062: return true;
063: }
064:
065: // protected String getBundleName() {
066: // return NbBundle.getMessage(BpelNodeAction.class, "CTL_WrapWithSequenceAction"); // NOI18N
067: // }
068:
069: // public ActionType getType() {
070: // return ActionType.WRAP_WITH_SEQUENCE;
071: // }
072:
073: private void addActivity(BpelContainer parent,
074: ExtendableActivity child, int index) {
075: if (parent instanceof ScopeHolder && child instanceof Scope) {
076: addActivity((ScopeHolder) parent, (Scope) child);
077: } else if (parent instanceof CompositeActivity) {
078: addActivity((CompositeActivity) parent, child, index);
079: } else if (parent instanceof ActivityHolder) {
080: addActivity((ActivityHolder) parent, child);
081: }
082: }
083:
084: private void addActivity(ScopeHolder parent, Scope child) {
085: parent.setScope(child);
086: }
087:
088: private void addActivity(CompositeActivity parent,
089: ExtendableActivity child, int index) {
090: assert parent != null && child != null;
091:
092: if (index < 0) {
093: return;
094: }
095:
096: if (index >= parent.sizeOfActivities()) {
097: parent.addActivity(child);
098: } else {
099: parent.insertActivity(child, index);
100: }
101: }
102:
103: private void addActivity(ActivityHolder parent,
104: ExtendableActivity child) {
105: parent.setActivity(child);
106: }
107:
108: private int getActivityIndex(CompositeActivity parent,
109: BpelEntity child) {
110: assert parent != null && child != null;
111: int activityIndex = -1;
112: for (int i = 0; i < parent.sizeOfActivities(); i++) {
113: if (child.equals(parent.getActivity(i))) {
114: activityIndex = i;
115: break;
116: }
117: }
118: return activityIndex;
119: }
120:
121: protected void performAction(BpelEntity[] bpelEntities) {
122: if (!enable(bpelEntities)) {
123: return;
124: }
125: final BpelEntity entity = bpelEntities[0];
126:
127: Callable performActionCall = new Callable<Object>() {
128: public Object call() throws Exception {
129: BpelContainer container = entity.getParent();
130: BpelModel bpelModel = container.getBpelModel();
131: // get entity index for composite Activity
132: int activityIndex = -1;
133: if (container instanceof CompositeActivity) {
134: activityIndex = getActivityIndex(
135: (CompositeActivity) container, entity);
136: }
137:
138: Activity newWrapper = getWrapEntity(container);
139:
140: BpelEntity wrappedEntity = entity.cut();
141: if (wrappedEntity instanceof ExtendableActivity) {
142: if (newWrapper instanceof ScopeHolder
143: && !(entity instanceof Scope)) {
144: Scope scope = bpelModel.getBuilder()
145: .createScope();
146: scope
147: .setActivity((ExtendableActivity) wrappedEntity);
148: wrappedEntity = scope;
149: }
150:
151: if (container instanceof ScopeHolder
152: && !(newWrapper instanceof Scope)) {
153: if (bpelModel != null) {
154: addActivity(newWrapper,
155: (ExtendableActivity) wrappedEntity,
156: 0);
157:
158: wrappedEntity = newWrapper;
159: newWrapper = bpelModel.getBuilder()
160: .createScope();
161: }
162: }
163:
164: addActivity(newWrapper,
165: (ExtendableActivity) wrappedEntity, 0);
166:
167: addActivity(container, newWrapper, activityIndex);
168: }
169: // container.getBpelModel().sync();
170: return null;
171: }
172: };
173:
174: try {
175: entity.getBpelModel().invoke(performActionCall, null);
176: } catch (Exception ex) {
177: ErrorManager.getDefault().notify(ex);
178: }
179: }
180:
181: }
|