KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > refactoring > XMLRefactoringActionsProvider


1 /*
2  * XMLRefactoringActionsProvider.java
3  *
4  * Created on December 20, 2006, 10:41 PM
5  *
6  * To change this template, choose Tools | Template Manager
7  * and open the template in the editor.
8  */

9
10 package org.netbeans.modules.xml.refactoring;
11
12 import java.util.Collection JavaDoc;
13 import org.netbeans.modules.refactoring.spi.ui.ActionsImplementationProvider;
14 import org.netbeans.modules.refactoring.spi.ui.RefactoringUI;
15 import org.netbeans.modules.refactoring.spi.ui.UI;
16 import org.netbeans.modules.xml.refactoring.impl.RefactoringUtil;
17 import org.netbeans.modules.xml.refactoring.ui.j.spi.ui.DeleteRefactoringUI;
18 import org.netbeans.modules.xml.refactoring.ui.j.spi.ui.FileRenameRefactoringUI;
19 import org.netbeans.modules.xml.refactoring.ui.j.spi.ui.RenameRefactoringUI;
20 import org.netbeans.modules.xml.refactoring.ui.j.spi.ui.WhereUsedQueryUI;
21 import org.netbeans.modules.xml.refactoring.ui.util.AnalysisUtilities;
22 import org.netbeans.modules.xml.refactoring.ui.views.WhereUsedView;
23 import org.netbeans.modules.xml.xam.Component;
24 import org.netbeans.modules.xml.xam.Model;
25 import org.netbeans.modules.xml.xam.Nameable;
26 import org.netbeans.modules.xml.xam.NamedReferenceable;
27 import org.netbeans.modules.xml.xam.Referenceable;
28 import org.openide.cookies.EditorCookie;
29 import org.openide.filesystems.FileObject;
30 import org.openide.loaders.DataObject;
31 import org.openide.nodes.Node;
32 import org.openide.text.CloneableEditorSupport;
33 import org.openide.util.Lookup;
34 import org.openide.windows.TopComponent;
35
36 /**
37  *
38  * @author Sonali
39  */

40 public class XMLRefactoringActionsProvider extends ActionsImplementationProvider {
41     
42     /** Creates a new instance of XMLRefactoringActionsProvider */
43     public XMLRefactoringActionsProvider() {
44     }
45      public boolean canFindUsages(Lookup lookup) {
46          //System.out.println("XMLRefactoringActionsProvider: canFindUsages called");
47
Collection JavaDoc<? extends Node> nodes = lookup.lookupAll(Node.class);
48          if(nodes.size() !=1){
49              return false;
50          }
51              
52          Node[] n = nodes.toArray(new Node[0]);
53          Referenceable ref = AnalysisUtilities.getReferenceable(n);
54                
55          return ref instanceof Referenceable;
56       
57  }
58
59   public void doFindUsages(Lookup lookup) {
60     Collection JavaDoc<? extends Node> nodes = lookup.lookupAll(Node.class);
61     Node[] n = nodes.toArray(new Node[0]);
62     
63     Referenceable ref = AnalysisUtilities.getReferenceable(n);
64     assert ref != null:"The node's NamedReferenceable should not be null";
65     WhereUsedView wuv = new WhereUsedView(ref);
66     WhereUsedQueryUI ui = new WhereUsedQueryUI(wuv, ref);
67     UI.openRefactoringUI(ui);
68   
69   }
70   
71  public boolean canRename(Lookup lookup) {
72      System.out.println("my canRename called");
73      Collection JavaDoc<? extends Node> nodes = lookup.lookupAll(Node.class);
74          if(nodes.size() !=1){
75              return false;
76          }
77              
78          Node[] n = nodes.toArray(new Node[0]);
79          Referenceable ref = AnalysisUtilities.getReferenceable(n);
80          if ( ref instanceof Model && RefactoringUtil.isWritable((Model)ref) )
81              return true;
82          if ( ref instanceof Nameable && RefactoringUtil.isWritable(RefactorRequest.getModel(ref)) ) {
83              return true;
84           }
85          
86      return false;
87  }
88
89  public void doRename(Lookup lookup) {
90      Collection JavaDoc<? extends Node> nodes = lookup.lookupAll(Node.class);
91      System.out.println("my rename impl called");
92      Node[] n = nodes.toArray(new Node[0]);
93    
94      final Referenceable ref = AnalysisUtilities.getReferenceable(n);
95      final EditorCookie ec = lookup.lookup(EditorCookie.class);
96      RefactoringUI ui = null;
97      if(ref instanceof Model) {
98          Model model = (Model) ref;
99          ui = new FileRenameRefactoringUI(model);
100      } else if(ref instanceof Nameable) {
101           ui = new RenameRefactoringUI(Nameable.class.cast(ref));
102      }
103            
104      if(isFromEditor(ec)){
105            TopComponent activetc = TopComponent.getRegistry().getActivated();
106            UI.openRefactoringUI(ui, activetc);
107      } else {
108            UI.openRefactoringUI(ui);
109     }
110         
111    
112    }
113  
114  public boolean canDelete(Lookup lookup) {
115         Collection JavaDoc<? extends Node> nodes = lookup.lookupAll(Node.class);
116         if(nodes.size() !=1){
117              return false;
118          }
119              
120          Node[] n = nodes.toArray(new Node[0]);
121          Referenceable ref = AnalysisUtilities.getReferenceable(n);
122         
123         if (ref instanceof Component && ((Component)ref).getParent() == null) {
124             return false;
125         }
126         if( ref instanceof NamedReferenceable && RefactoringUtil.isWritable(RefactorRequest.getModel(ref)) && n[0].canDestroy() )
127             return true;
128         
129         return false;
130     }
131  
132  public void doDelete(Lookup lookup){
133       Collection JavaDoc<? extends Node> nodes = lookup.lookupAll(Node.class);
134       System.out.println("my delete impl called");
135       Node[] n = nodes.toArray(new Node[0]);
136       
137       
138       final Referenceable ref = AnalysisUtilities.getReferenceable(n);
139       final EditorCookie ec = lookup.lookup(EditorCookie.class);
140    
141       DeleteRefactoringUI ui = new DeleteRefactoringUI(NamedReferenceable.class.cast(ref));
142           
143       if(isFromEditor(ec)){
144            TopComponent activetc = TopComponent.getRegistry().getActivated();
145            UI.openRefactoringUI(ui, activetc);
146       } else {
147            UI.openRefactoringUI(ui);
148       }
149       
150  }
151  
152  private boolean isFromEditor(EditorCookie ec) {
153         if (ec != null && ec.getOpenedPanes() != null) {
154             TopComponent activetc = TopComponent.getRegistry().getActivated();
155             if (activetc instanceof CloneableEditorSupport.Pane) {
156                 return true;
157             }
158         }
159         return false;
160   }
161     
162           
163    
164 public class FindUsagesRunnable implements Runnable JavaDoc {
165         private Node[] nodes;
166         private RefactoringUI ui;
167         
168         public FindUsagesRunnable(Node[] n) {
169             this.nodes=n;
170         }
171         
172         public final void run() {
173           
174             Referenceable ref = AnalysisUtilities.getReferenceable(nodes);
175             assert ref != null:"The node's NamedReferenceable should not be null";
176             WhereUsedView wuv = new WhereUsedView(ref);
177             WhereUsedQueryUI ui = new WhereUsedQueryUI(wuv, ref);
178             UI.openRefactoringUI(ui);
179             
180         }
181   }
182
183
184 }
185
186
Popular Tags