KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > jsf > refactoring > Occurrences


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-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.web.jsf.refactoring;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Collection JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.logging.Logger JavaDoc;
27 import javax.swing.text.BadLocationException JavaDoc;
28 import javax.swing.text.Position.Bias;
29 import org.netbeans.editor.BaseDocument;
30 import org.netbeans.modules.web.api.webmodule.WebModule;
31 import org.netbeans.modules.web.jsf.JSFConfigDataObject;
32 import org.netbeans.modules.web.jsf.api.ConfigurationUtils;
33 import org.netbeans.modules.web.jsf.api.facesmodel.Converter;
34 import org.netbeans.modules.web.jsf.editor.JSFEditorUtilities;
35 import org.netbeans.modules.web.jsf.api.facesmodel.FacesConfig;
36 import org.netbeans.modules.web.jsf.api.facesmodel.ManagedBean;
37 import org.openide.ErrorManager;
38 import org.openide.filesystems.FileObject;
39 import org.openide.loaders.DataObject;
40 import org.openide.loaders.DataObjectNotFoundException;
41 import org.openide.text.CloneableEditorSupport;
42 import org.openide.text.PositionBounds;
43 import org.openide.text.PositionRef;
44 import org.openide.util.NbBundle;
45
46 /**
47  *
48  * @author Petr Pisl
49  */

50 public class Occurrences {
51     
52     private static final Logger JavaDoc LOGGER = Logger.getLogger(Occurrences.class.getName());
53     
54     public static abstract class OccurrenceItem {
55         // the faces configuration file
56
protected FileObject config;
57         protected String JavaDoc newValue;
58         protected String JavaDoc oldValue;
59         
60         public OccurrenceItem(FileObject config, String JavaDoc newValue, String JavaDoc oldValue){
61             this.config = config;
62             this.newValue = newValue;
63             this.oldValue = oldValue;
64         }
65         
66         public FileObject getFacesConfig() {
67             return config;
68         }
69         
70         public String JavaDoc getElementText(){
71             StringBuffer JavaDoc stringBuffer = new StringBuffer JavaDoc();
72             stringBuffer.append("<font color=\"#0000FF\">");
73             stringBuffer.append("&lt;").append(getXMLElementName()).append("&gt;</font><b>");
74             stringBuffer.append(oldValue).append("</b><font color=\"#0000FF\">&lt;/").append(getXMLElementName());
75             stringBuffer.append("&gt;</font>");
76             return stringBuffer.toString();
77         }
78         
79         protected abstract String JavaDoc getXMLElementName();
80         
81         public abstract void performRename();
82         public abstract void undoRename();
83         public abstract String JavaDoc getRenameMessage();
84         
85         public abstract void performSafeDelete();
86         public abstract void undoSafeDelete();
87         public abstract String JavaDoc getSafeDeleteMessage();
88         
89         public abstract String JavaDoc getWhereUsedMessage();
90         
91         protected PositionBounds createPosition(int startOffset, int endOffset) {
92             try{
93                 DataObject dataObject = DataObject.find(config);
94                 if (dataObject instanceof JSFConfigDataObject){
95                     CloneableEditorSupport editor
96                             = JSFEditorUtilities.findCloneableEditorSupport((JSFConfigDataObject)dataObject);
97                     if (editor != null){
98                         PositionRef start=editor.createPositionRef(startOffset, Bias.Forward);
99                         PositionRef end=editor.createPositionRef(endOffset, Bias.Backward);
100                         return new PositionBounds(start,end);
101                     }
102                 }
103             } catch (DataObjectNotFoundException ex) {
104                 java.util.logging.Logger.getLogger("global").log(java.util.logging.Level.SEVERE,
105                         ex.getMessage(),
106                         ex);
107             }
108             return null;
109         }
110         
111         public PositionBounds getClassDefinitionPosition() {
112             return createPosition(0, 0);
113         };
114         
115         public PositionBounds getElementDefinitionPosition() {
116             return createPosition(0, 0);
117         };
118     }
119     
120     public static class ManagedBeanClassItem extends OccurrenceItem{
121         private final ManagedBean bean;
122         
123         public ManagedBeanClassItem(FileObject config, ManagedBean bean, String JavaDoc newValue){
124             super(config, newValue, bean.getManagedBeanClass());
125             this.bean = bean;
126         }
127         
128         protected String JavaDoc getXMLElementName(){
129             return "managed-bean-class"; //NOI18N
130
}
131         
132         public void performRename(){
133             changeBeanClass(newValue);
134         }
135         
136         public void undoRename(){
137             changeBeanClass(oldValue);
138         }
139         
140         public String JavaDoc getWhereUsedMessage(){
141             return NbBundle.getMessage(Occurrences.class, "MSG_ManagedBeanClass_WhereUsed", //NOI18N
142
new Object JavaDoc[] { bean.getManagedBeanName(), getElementText()});
143         }
144         
145         public String JavaDoc getRenameMessage(){
146             return NbBundle.getMessage(Occurrences.class, "MSG_ManagedBeanClass_Rename", //NOI18N
147
new Object JavaDoc[] { bean.getManagedBeanName(), getElementText()});
148         }
149         
150         public void performSafeDelete() {
151             FacesConfig faces = ConfigurationUtils.getConfigModel(config, true).getRootComponent();
152             Collection JavaDoc<ManagedBean> beans = faces.getManagedBeans();
153             for (Iterator JavaDoc<ManagedBean> it = beans.iterator(); it.hasNext();) {
154                 ManagedBean managedBean = it.next();
155                 if (bean.getManagedBeanName().equals(managedBean.getManagedBeanName())){
156                     faces.getModel().startTransaction();
157                     faces.removeManagedBean(managedBean);
158                     faces.getModel().endTransaction();
159                     continue;
160                 }
161             }
162         }
163         
164         public void undoSafeDelete() {
165             FacesConfig facesConfig = ConfigurationUtils.getConfigModel(config, true).getRootComponent();
166             facesConfig.getModel().startTransaction();
167             facesConfig.addManagedBean(bean);
168             facesConfig.getModel().endTransaction();
169         }
170         
171         public String JavaDoc getSafeDeleteMessage() {
172             return NbBundle.getMessage(Occurrences.class, "MSG_ManagedBeanClass_SafeDelete", //NOI18N
173
new Object JavaDoc[] { bean.getManagedBeanName(), getElementText()});
174         }
175         
176         private void changeBeanClass(String JavaDoc className){
177             FacesConfig facesConfig = ConfigurationUtils.getConfigModel(config, true).getRootComponent();
178             List JavaDoc <ManagedBean> beans = facesConfig.getManagedBeans();
179             for (Iterator JavaDoc<ManagedBean> it = beans.iterator(); it.hasNext();) {
180                 ManagedBean managedBean = it.next();
181                 if (bean.getManagedBeanName().equals(managedBean.getManagedBeanName())){
182                     facesConfig.getModel().startTransaction();
183                     managedBean.setManagedBeanClass(className);
184                     facesConfig.getModel().endTransaction();
185                     continue;
186                 }
187                 
188             }
189         }
190         
191         public PositionBounds getClassDefinitionPosition() {
192             PositionBounds position = null;
193             try{
194                 JSFConfigDataObject dataObject = (JSFConfigDataObject)DataObject.find(config);
195                 BaseDocument document = JSFEditorUtilities.getBaseDocument(dataObject);
196                 int [] offsets = JSFEditorUtilities.getManagedBeanDefinition(document, bean.getManagedBeanName());
197                 String JavaDoc text = document.getText(offsets);
198                 int offset = offsets[0] + text.indexOf(oldValue);
199                 position = createPosition(offset, offset + oldValue.length());
200             } catch (BadLocationException JavaDoc ex) {
201                 ErrorManager.getDefault().notify(ex);
202             } catch (DataObjectNotFoundException ex) {
203                 java.util.logging.Logger.getLogger("global").log(java.util.logging.Level.SEVERE,
204                         ex.getMessage(),
205                         ex);
206             }
207             return position;
208         };
209         
210         public PositionBounds getElementDefinitionPosition() {
211             PositionBounds position = null;
212             try {
213                 JSFConfigDataObject dataObject = (JSFConfigDataObject)DataObject.find(config);
214                 BaseDocument document = JSFEditorUtilities.getBaseDocument(dataObject);
215                 int [] offsets = JSFEditorUtilities.getManagedBeanDefinition(document, bean.getManagedBeanName());
216                 position = createPosition(offsets[0], offsets[1]);
217             } catch (DataObjectNotFoundException ex) {
218                 java.util.logging.Logger.getLogger("global").log(java.util.logging.Level.SEVERE,
219                         ex.getMessage(),
220                         ex);
221             }
222             return position;
223         };
224     }
225     
226     public static class ConverterClassItem extends OccurrenceItem {
227         private final Converter converter;
228         
229         public ConverterClassItem(FileObject config, Converter converter, String JavaDoc newValue){
230             super(config, newValue, converter.getConverterClass());
231             this.converter = converter;
232         }
233         
234         protected String JavaDoc getXMLElementName(){
235             return "converter-class"; //NOI18N
236
}
237         
238         public void performRename(){
239             changeConverterClass(oldValue, newValue);
240         }
241         
242         public void undoRename(){
243             changeConverterClass(newValue, oldValue);
244         }
245         
246         public String JavaDoc getWhereUsedMessage(){
247             return NbBundle.getMessage(Occurrences.class, "MSG_ConverterClass_WhereUsed", getElementText()); //NOI18N
248
}
249         
250         public String JavaDoc getRenameMessage(){
251             return NbBundle.getMessage(Occurrences.class, "MSG_ConverterClass_Rename", getElementText()); //NOI18N
252
}
253         
254         public void performSafeDelete() {
255             FacesConfig facesConfig = ConfigurationUtils.getConfigModel(config, true).getRootComponent();
256             List JavaDoc <Converter> converters = facesConfig.getConverters();
257             for (Iterator JavaDoc<Converter> it = converters.iterator(); it.hasNext();) {
258                 Converter converter = it.next();
259                 if (oldValue.equals(converter.getConverterClass())){
260                     facesConfig.getModel().startTransaction();
261                     facesConfig.removeConverter(converter);
262                     facesConfig.getModel().endTransaction();
263                     continue;
264                 }
265             }
266         }
267         
268         public void undoSafeDelete() {
269             FacesConfig facesConfig = ConfigurationUtils.getConfigModel(config, true).getRootComponent();
270             facesConfig.getModel().startTransaction();
271             facesConfig.addConverter(converter);
272             facesConfig.getModel().endTransaction();
273         }
274         
275         public String JavaDoc getSafeDeleteMessage() {
276             return NbBundle.getMessage(Occurrences.class, "MSG_ConverterClass_SafeDelete", //NOI18N
277
new Object JavaDoc[] { getElementText()});
278         }
279         
280         private void changeConverterClass(String JavaDoc oldClass, String JavaDoc newClass){
281             FacesConfig facesConfig = ConfigurationUtils.getConfigModel(config, true).getRootComponent();
282             List JavaDoc <Converter> converters = facesConfig.getConverters();
283             for (Iterator JavaDoc<Converter> it = converters.iterator(); it.hasNext();) {
284                 Converter converter = it.next();
285                 if (oldClass.equals(converter.getConverterClass())){
286                     converter.getModel().startTransaction();
287                     converter.setConverterClass(newClass);
288                     converter.getModel().endTransaction();
289                     continue;
290                 }
291             }
292         }
293         
294         public PositionBounds getClassDefinitionPosition() {
295             PositionBounds position = null;
296             try{
297                 JSFConfigDataObject dataObject = (JSFConfigDataObject)DataObject.find(config);
298                 BaseDocument document = JSFEditorUtilities.getBaseDocument(dataObject);
299                 int [] offsets = JSFEditorUtilities.getConverterDefinition(document, converter.getConverterForClass());
300                 
301                 String JavaDoc text = document.getText(offsets);
302                 int offset = offsets[0] + text.indexOf(oldValue);
303                 position = createPosition(offset, offset + oldValue.length());
304             } catch (BadLocationException JavaDoc ex) {
305                 ErrorManager.getDefault().notify(ex);
306             } catch (DataObjectNotFoundException ex) {
307                 java.util.logging.Logger.getLogger("global").log(java.util.logging.Level.SEVERE,
308                         ex.getMessage(),
309                         ex);
310             }
311             return position;
312         };
313         
314         public PositionBounds getElementDefinitionPosition() {
315             PositionBounds position = null;
316             try{
317                 JSFConfigDataObject dataObject = (JSFConfigDataObject)DataObject.find(config);
318                 BaseDocument document = JSFEditorUtilities.getBaseDocument(dataObject);
319                 int [] offsets = JSFEditorUtilities.getConverterDefinition(document, converter.getConverterForClass());
320                 position = createPosition(offsets[0], offsets[1]);
321             } catch (DataObjectNotFoundException ex) {
322                 java.util.logging.Logger.getLogger("global").log(java.util.logging.Level.SEVERE,
323                         ex.getMessage(),
324                         ex);
325             }
326             return position;
327         };
328     }
329     
330     public static class ConverterForClassItem extends OccurrenceItem {
331         private final Converter converter;
332         
333         public ConverterForClassItem(FileObject config, Converter converter, String JavaDoc newValue){
334             super(config, newValue, converter.getConverterForClass());
335             this.converter = converter;
336         }
337         
338         protected String JavaDoc getXMLElementName(){
339             return "converter-for-class"; //NOI18N
340
}
341         
342         public void performRename(){
343             changeConverterForClass(oldValue, newValue);
344         }
345         
346         public void undoRename(){
347             changeConverterForClass(newValue, oldValue);
348         }
349         
350         public String JavaDoc getWhereUsedMessage(){
351             return NbBundle.getMessage(Occurrences.class, "MSG_ConverterForClass_WhereUsed", getElementText()); //NOI18N
352
}
353         
354         public String JavaDoc getRenameMessage(){
355             return NbBundle.getMessage(Occurrences.class, "MSG_ConverterForClass_Rename", getElementText()); //NOI18N
356
}
357         
358         public void performSafeDelete() {
359             FacesConfig facesConfig = ConfigurationUtils.getConfigModel(config, true).getRootComponent();
360             List JavaDoc <Converter> converters = facesConfig.getConverters();
361             for (Iterator JavaDoc<Converter> it = converters.iterator(); it.hasNext();) {
362                 Converter converter = it.next();
363                 if (oldValue.equals(converter.getConverterClass())){
364                     facesConfig.getModel().startTransaction();
365                     facesConfig.removeConverter(converter);
366                     facesConfig.getModel().endTransaction();
367                     continue;
368                 }
369             }
370         }
371         
372         public void undoSafeDelete() {
373             FacesConfig facesConfig = ConfigurationUtils.getConfigModel(config, true).getRootComponent();
374             facesConfig.getModel().startTransaction();
375             facesConfig.addConverter(converter);
376             facesConfig.addConverter(converter);
377             facesConfig.getModel().endTransaction();
378         }
379         
380         public String JavaDoc getSafeDeleteMessage() {
381             return NbBundle.getMessage(Occurrences.class, "MSG_ManagedBeanClass_SafeDelete", //NOI18N
382
new Object JavaDoc[] { getElementText()});
383         }
384         
385         private void changeConverterForClass(String JavaDoc oldClass, String JavaDoc newClass){
386             FacesConfig facesConfig = ConfigurationUtils.getConfigModel(config, true).getRootComponent();
387             List JavaDoc<Converter> converters = facesConfig.getConverters();
388             for (Iterator JavaDoc<Converter> it = converters.iterator(); it.hasNext();) {
389                 Converter converter = it.next();
390                 if (oldClass.equals(converter.getConverterForClass())){
391                     converter.getModel().startTransaction();
392                     converter.setConverterForClass(newClass);
393                     converter.getModel().endTransaction();
394                     continue;
395                 }
396             }
397         }
398         
399         public PositionBounds getClassDefinitionPosition() {
400             PositionBounds position = null;
401             try{
402                 JSFConfigDataObject dataObject = (JSFConfigDataObject)DataObject.find(config);
403                 BaseDocument document = JSFEditorUtilities.getBaseDocument(dataObject);
404                 int [] offsets = JSFEditorUtilities.getConverterDefinition(document, converter.getConverterForClass());
405                 String JavaDoc text = document.getText(offsets);
406                 int offset = offsets[0] + text.indexOf(oldValue);
407                 position = createPosition(offset, offset + oldValue.length());
408             } catch (BadLocationException JavaDoc ex) {
409                 ErrorManager.getDefault().notify(ex);
410             } catch (DataObjectNotFoundException ex) {
411                 java.util.logging.Logger.getLogger("global").log(java.util.logging.Level.SEVERE,
412                         ex.getMessage(),
413                         ex);
414             }
415             return position;
416         };
417         
418         public PositionBounds getElementDefinitionPosition() {
419             PositionBounds position = null;
420             try{
421                 JSFConfigDataObject dataObject = (JSFConfigDataObject)DataObject.find(config);
422                 BaseDocument document = JSFEditorUtilities.getBaseDocument(dataObject);
423                 int [] offsets = JSFEditorUtilities.getConverterDefinition(document, converter.getConverterForClass());
424                 position = createPosition(offsets[0], offsets[1]);
425             } catch (DataObjectNotFoundException ex) {
426                 java.util.logging.Logger.getLogger("global").log(java.util.logging.Level.SEVERE,
427                         ex.getMessage(),
428                         ex);
429             }
430             return position;
431         };
432     }
433     
434     public static List JavaDoc <OccurrenceItem> getAllOccurrences(WebModule webModule, String JavaDoc oldName, String JavaDoc newName){
435         List JavaDoc result = new ArrayList JavaDoc();
436         assert webModule != null;
437         assert oldName != null;
438         assert newName != null;
439         
440         LOGGER.fine("getAllOccurences("+ webModule.getDocumentBase().getPath() + ", " + oldName + ", " + newName + ")");
441         if (webModule != null){
442             // find all jsf configuration files in the web module
443
FileObject[] configs = ConfigurationUtils.getFacesConfigFiles(webModule);
444             
445             if (configs != null){
446                 for (int i = 0; i < configs.length; i++) {
447                     FacesConfig facesConfig = ConfigurationUtils.getConfigModel(configs[i], true).getRootComponent();
448                     List JavaDoc <Converter> converters = facesConfig.getConverters();
449                     for (Iterator JavaDoc<Converter> it = converters.iterator(); it.hasNext();) {
450                         Converter converter = it.next();
451                         if (oldName.equals(converter.getConverterClass()))
452                             result.add(new ConverterClassItem(configs[i], converter, newName));
453                         else if (oldName.equals(converter.getConverterForClass()))
454                             result.add(new ConverterForClassItem(configs[i], converter, newName));
455                     }
456                     List JavaDoc<ManagedBean> managedBeans = facesConfig.getManagedBeans();
457                     for (Iterator JavaDoc<ManagedBean> it = managedBeans.iterator(); it.hasNext();) {
458                         ManagedBean managedBean = it.next();
459                         if (oldName.equals(managedBean.getManagedBeanClass()))
460                             result.add(new ManagedBeanClassItem(configs[i], managedBean, newName));
461                         
462                     }
463                     
464                 }
465             }
466         }
467         return result;
468     }
469     
470 }
471
Popular Tags