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.refactoring;
042:
043: import java.io.IOException;
044: import java.net.URL;
045: import java.net.URISyntaxException;
046: import java.util.ArrayList;
047: import java.util.HashMap;
048: import java.util.HashSet;
049: import java.util.List;
050: import java.util.Map;
051: import java.util.Set;
052:
053: import org.openide.filesystems.FileObject;
054: import org.openide.filesystems.URLMapper;
055:
056: import org.netbeans.modules.refactoring.api.Problem;
057: import org.netbeans.modules.refactoring.api.MoveRefactoring;
058: import org.netbeans.modules.refactoring.spi.RefactoringElementImplementation;
059: import org.netbeans.modules.refactoring.spi.RefactoringElementsBag;
060:
061: import org.netbeans.modules.xml.refactoring.ErrorItem;
062: import org.netbeans.modules.xml.refactoring.spi.RefactoringUtil;
063: import org.netbeans.modules.xml.refactoring.spi.SharedUtils;
064: import org.netbeans.modules.xml.refactoring.XMLRefactoringTransaction;
065:
066: import org.netbeans.modules.xml.xam.Component;
067: import org.netbeans.modules.xml.xam.Model;
068: import org.netbeans.modules.xml.xam.Referenceable;
069:
070: import org.netbeans.modules.bpel.model.api.Import;
071: import org.netbeans.modules.bpel.model.api.events.VetoException;
072: import static org.netbeans.modules.soa.ui.util.UI.*;
073:
074: /**
075: * @author Vladimir Yaroslavskiy
076: * @version 2007.03.16
077: */
078: final class Mover extends Plugin {
079:
080: Mover(MoveRefactoring refactoring) {
081: myRequest = refactoring;
082: }
083:
084: public Problem fastCheckParameters() {
085: URL url = ((MoveRefactoring) myRequest).getTarget().lookup(
086: URL.class);
087:
088: if (url == null) {
089: return null;
090: }
091: FileObject target = URLMapper.findFileObject(url);
092:
093: if (target != null && !target.canWrite()) {
094: return new Problem(true, i18n(Mover.class,
095: "ERR_PackageIsReadOnly")); // NOI18N
096: }
097: return null;
098: }
099:
100: public Problem checkParameters() {
101: return null;
102: }
103:
104: public Problem prepare(RefactoringElementsBag refactoringElements) {
105: Referenceable reference = myRequest.getRefactoringSource()
106: .lookup(Referenceable.class);
107:
108: if (reference == null) {
109: return null;
110: }
111: if (!(reference instanceof Model)) {
112: return null;
113: }
114: Set<Component> roots = getRoots(reference);
115: List<Element> elements = new ArrayList<Element>();
116:
117: for (Component root : roots) {
118: List<Element> founds = find(reference, root);
119:
120: if (founds != null) {
121: elements.addAll(founds);
122: }
123: }
124: if (elements.size() > 0) {
125: List<Model> models = getModels(elements);
126: List<ErrorItem> errors = RefactoringUtil
127: .precheckUsageModels(models, true);
128:
129: if (errors != null && errors.size() > 0) {
130: return processErrors(errors);
131: }
132: }
133: XMLRefactoringTransaction transaction = myRequest.getContext()
134: .lookup(XMLRefactoringTransaction.class);
135: transaction.register(this , elements);
136: refactoringElements.registerTransaction(transaction);
137:
138: for (Element element : elements) {
139: element.setTransactionObject(transaction);
140: refactoringElements.add(myRequest, element);
141: }
142: return null;
143: }
144:
145: public void doRefactoring(
146: List<RefactoringElementImplementation> elements)
147: throws IOException {
148: Map<Model, Set<RefactoringElementImplementation>> map = getModelMap(elements);
149: Set<Model> models = map.keySet();
150: Referenceable reference = myRequest.getRefactoringSource()
151: .lookup(Referenceable.class);
152:
153: for (Model model : models) {
154: if (reference instanceof Model) {
155: rename(model, getComponents(map.get(model)));
156: }
157: }
158: }
159:
160: public String getModelReference(Component component) {
161: if (component instanceof Import) {
162: return ((Import) component).getLocation();
163: }
164: return null;
165: }
166:
167: private Map<Model, Set<RefactoringElementImplementation>> getModelMap(
168: List<RefactoringElementImplementation> elements) {
169: Map<Model, Set<RefactoringElementImplementation>> results = new HashMap<Model, Set<RefactoringElementImplementation>>();
170:
171: for (RefactoringElementImplementation element : elements) {
172: Model model = (element.getLookup().lookup(Component.class))
173: .getModel();
174: Set<RefactoringElementImplementation> components = results
175: .get(model);
176:
177: if (components == null) {
178: components = new HashSet<RefactoringElementImplementation>();
179: components.add(element);
180: results.put(model, components);
181: } else {
182: components.add(element);
183: }
184: }
185: return results;
186: }
187:
188: private List<Component> getComponents(
189: Set<RefactoringElementImplementation> elements) {
190: List<Component> component = new ArrayList<Component>(elements
191: .size());
192:
193: for (RefactoringElementImplementation element : elements) {
194: component.add(element.getLookup().lookup(Component.class));
195: }
196: return component;
197: }
198:
199: private void rename(Model model, List<Component> components)
200: throws IOException {
201: if (components == null) {
202: return;
203: }
204: for (Component component : components) {
205: renameFile(model, component);
206: }
207: }
208:
209: private void renameFile(Model model, Component component)
210: throws IOException {
211: //out();
212: //out("FILE RENAME: " + Util.getName(component));
213: if (!(component instanceof Import)) {
214: return;
215: }
216: try {
217: Import _import = (Import) component;
218: String location = _import.getLocation();
219:
220: try {
221: location = SharedUtils.calculateNewLocationString(
222: model, myRequest);
223: } catch (URISyntaxException e) {
224: return;
225: }
226: _import.setLocation(location);
227: } catch (VetoException e) {
228: throw new IOException(e.getMessage());
229: }
230: }
231:
232: private MoveRefactoring myRequest;
233: }
|