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.bpel.validation.reference;
042:
043: import java.util.List;
044: import org.netbeans.modules.xml.xam.Reference;
045: import org.netbeans.modules.xml.xam.Referenceable;
046:
047: import org.netbeans.modules.bpel.model.api.BpelEntity;
048: import org.netbeans.modules.bpel.model.api.BpelModel;
049: import org.netbeans.modules.bpel.model.api.NamedElement;
050: import org.netbeans.modules.bpel.model.api.Process;
051: import org.netbeans.modules.bpel.model.api.Variable;
052: import org.netbeans.modules.bpel.model.api.VariableContainer;
053: import org.netbeans.modules.bpel.model.api.VariableDeclaration;
054: import org.netbeans.modules.bpel.model.api.VariableReference;
055: import org.netbeans.modules.bpel.model.api.references.ReferenceCollection;
056: import org.netbeans.modules.bpel.validation.core.QuickFix.Adapter;
057: import org.netbeans.modules.bpel.validation.core.Util;
058: import static org.netbeans.modules.soa.ui.util.UI.*;
059:
060: /**
061: * @author Vladimir Yaroslavskiy
062: * @version 2007.12.06
063: */
064: class QuickFix {
065:
066: static Adapter get(BpelEntity entity,
067: Reference<Referenceable> reference) {
068: //out();
069: //out("reference: " + reference.getClass().getName()); // todo m
070: //out(" : " + reference.getType().getName());
071: if (reference.getType().isAssignableFrom(
072: VariableDeclaration.class)) {
073: if (entity instanceof VariableReference) {
074: //out(" VAR");
075: return new SET_VARIABLE((VariableReference) entity,
076: reference.getRefString());
077: }
078: }
079: //out(" null");
080: return null;
081: }
082:
083: // ----------------------------------------
084: static class SET_VARIABLE extends Adapter {
085:
086: SET_VARIABLE(VariableReference reference, String name) {
087: myReference = reference;
088: myName = name;
089: }
090:
091: public boolean canFix() {
092: myVariables = getAppropriateVariables();
093:
094: if (myVariables == null) {
095: return false;
096: }
097: if (myVariables.length == 0) {
098: return false;
099: }
100: if (myVariables.length != 1) { // todo r
101: return false;
102: }
103: return true;
104: }
105:
106: public void doFix() {
107: Variable variable = myVariables[0];
108:
109: myDescription = i18n(QuickFix.class,
110: "QUICK_FIX_Change_varibale_name", variable
111: .getName()); // NOI18N
112:
113: myReference.setVariable(((ReferenceCollection) myReference)
114: .createReference(variable,
115: VariableDeclaration.class));
116: }
117:
118: public String getDescription() {
119: return myDescription;
120: }
121:
122: private Variable[] getAppropriateVariables() {
123: BpelModel model = ((BpelEntity) myReference).getBpelModel();
124:
125: if (model == null) {
126: return null;
127: }
128: Process process = model.getProcess();
129:
130: if (process == null) {
131: return null;
132: }
133: VariableContainer container = process
134: .getVariableContainer();
135:
136: if (container == null) {
137: return null;
138: }
139: Variable[] variables = container.getVariables();
140:
141: if (variables == null) {
142: return null;
143: }
144: List<NamedElement> named = Util.getAppropriate(variables,
145: myName);
146: Variable[] appropriate = new Variable[named.size()];
147:
148: for (int i = 0; i < named.size(); i++) {
149: appropriate[i] = (Variable) named.get(i);
150: }
151: return appropriate;
152: }
153:
154: private String myName;
155: private String myDescription;
156: private Variable[] myVariables;
157: private VariableReference myReference;
158: }
159: }
|