KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > refactoring > spi > SharedUtils


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.xml.refactoring.spi;
20
21 import java.io.IOException JavaDoc;
22 import java.net.URI JavaDoc;
23 import java.util.logging.Level JavaDoc;
24 import java.util.logging.Logger JavaDoc;
25 import org.netbeans.api.project.FileOwnerQuery;
26 import org.netbeans.api.project.Project;
27 import org.netbeans.modules.xml.refactoring.DeleteRequest;
28 import org.netbeans.modules.xml.refactoring.ErrorItem;
29 import org.netbeans.modules.xml.refactoring.FileRenameRequest;
30 import org.netbeans.modules.xml.refactoring.RenameRequest;
31 import org.netbeans.modules.xml.refactoring.Usage;
32 import org.netbeans.modules.xml.refactoring.UsageGroup;
33 import org.netbeans.modules.xml.refactoring.impl.RefactoringUtil;
34 import org.netbeans.modules.xml.retriever.catalog.ProjectCatalogSupport;
35 import org.netbeans.modules.xml.xam.Component;
36 import org.netbeans.modules.xml.xam.Model;
37 import org.netbeans.modules.xml.xam.locator.CatalogModel;
38 import org.openide.filesystems.FileLock;
39 import org.openide.filesystems.FileObject;
40 import org.openide.filesystems.FileUtil;
41 import org.openide.util.NbBundle;
42
43 /**
44  * Shared utilities for service implementation code.
45  *
46  * @author Nam Nguyen
47  */

48 public class SharedUtils {
49     
50     public static void renameTarget(RenameRequest request) throws IOException JavaDoc {
51         if (request == null || request.getTargetModel() == null) return;
52         Model model = request.getTargetModel();
53         boolean startTransaction = ! model.isIntransaction();
54         try {
55             if (startTransaction) {
56                 model.startTransaction();
57             }
58             request.getNameableTarget().setName(request.getNewName());
59         } finally {
60             if (startTransaction && model.isIntransaction()) {
61                 model.endTransaction();
62             }
63         }
64         
65         request.setRenamedTarget(request.getNameableTarget());
66     }
67     
68     public static void deleteTarget(DeleteRequest request) throws IOException JavaDoc {
69         if (request == null || request.getTargetModel() == null) return;
70         Model model = request.getTargetModel();
71         boolean startTransaction = ! model.isIntransaction();
72         try {
73             if (startTransaction) {
74                 model.startTransaction();
75             }
76             model.removeChildComponent(request.getTarget());
77         } finally {
78             if (startTransaction && model.isIntransaction()) {
79                 model.endTransaction();
80             }
81         }
82         request.setDone(true);
83     }
84
85     public static void addCascadeDeleteErrors(DeleteRequest request, Class JavaDoc<? extends Model> referencingModelType) {
86         for (UsageGroup usage : request.getUsages().getUsages()) {
87             if (! (referencingModelType.isAssignableFrom(usage.getModel().getClass()))) {
88                 continue;
89             }
90             String JavaDoc msg = NbBundle.getMessage(RefactoringUtil.class, "MSG_CascadeDeleteNotSupported");
91             for (Object JavaDoc o : usage.getItems()) {
92                 Usage i = (Usage) o; //strange i have to do this
93
request.addError(new ErrorItem(i.getComponent(), msg));
94             }
95         }
96     }
97     
98     public static void addCascadeDeleteErrors(DeleteRequest request, RefactoringEngine engine) {
99         for (UsageGroup usage : request.getUsages().getUsages()) {
100         if (! usage.getEngine().equals(engine)) {
101                 continue;
102             }
103             String JavaDoc msg = NbBundle.getMessage(RefactoringUtil.class, "MSG_CascadeDeleteNotSupported");
104             for (Object JavaDoc o : usage.getItems()) {
105                 Usage i = (Usage) o; //strange i have to do this
106
request.addError(new ErrorItem(i.getComponent(), msg));
107             }
108         }
109     }
110  
111     public static boolean isWritable(FileObject fo) {
112         boolean canLock = false;
113         FileLock lock = null;
114         try {
115             lock = fo.lock();
116             canLock = true;
117         } catch(IOException JavaDoc ioe) {
118             if (lock != null) lock.releaseLock();
119         }
120         return fo != null && fo.canWrite() && canLock;
121     }
122
123     public static String JavaDoc getURI(FileObject fo) {
124         return FileUtil.toFile(fo).toURI().toString();
125     }
126     
127     public static void renameFile(FileRenameRequest request) throws IOException JavaDoc {
128         FileObject referencedFO = request.getFileObject();
129         RefactoringUtil.saveTargetFile(request);
130         referencedFO = renameFile(referencedFO, request.getNewFileName());
131         refreshCatalogModel(request, referencedFO);
132     }
133
134     public static ProjectCatalogSupport getCatalogSupport(FileObject file) {
135         Project p = FileOwnerQuery.getOwner(file);
136         if (p != null) {
137             return (ProjectCatalogSupport) p.getLookup().lookup(ProjectCatalogSupport.class);
138         }
139         return null;
140     }
141     
142     public static void undoRenameFile(FileRenameRequest request) throws IOException JavaDoc {
143         CatalogModel cat = (CatalogModel)
144             request.getTargetModel().getModelSource().getLookup().lookup(CatalogModel.class);
145         FileObject fo = request.getFileObject();
146         fo = renameFile(fo, request.getOldFileName());
147         refreshCatalogModel(request, fo);
148     }
149
150     public static FileObject renameFile(FileObject fo, String JavaDoc newName) throws IOException JavaDoc {
151         String JavaDoc extension = fo.getExt();
152         FileLock lock = null;
153         try {
154             lock = fo.lock();
155             fo.rename(lock, newName, extension);
156             return fo;
157         } finally {
158             if (lock != null) lock.releaseLock();
159         }
160     }
161     
162     public static void refreshCatalogModel(FileRenameRequest request, FileObject referencedFO) {
163         for (UsageGroup ug : request.getUsages().getUsages()) {
164             FileObject referencingFO = ug.getFileObject();
165             ProjectCatalogSupport pcs = getCatalogSupport(referencingFO);
166             if (pcs == null) continue;
167             for (Component uc : ug.getRefactorComponents()) {
168                 String JavaDoc reference = ug.getEngine().getModelReference(uc);
169                 if (reference == null) continue;
170                 try {
171                     if (pcs != null && pcs.removeCatalogEntry(new URI JavaDoc(reference))) {
172                         pcs.createCatalogEntry(referencingFO, referencedFO);
173                     }
174                 } catch(Exception JavaDoc ex) {
175                     Logger.getLogger(SharedUtils.class.getName()).log(Level.FINE, ex.getMessage());
176                 }
177             }
178         }
179     }
180 }
181
Popular Tags