KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > web > jsf > navigation > PageFlowController


1 /*
2  * PageFlowController.java
3  *
4  * Created on March 1, 2007, 1:12 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.web.jsf.navigation;
11
12 import java.io.IOException JavaDoc;
13 import java.util.Collection JavaDoc;
14 import java.util.HashSet JavaDoc;
15 import java.util.List JavaDoc;
16 import org.netbeans.api.project.FileOwnerQuery;
17 import org.netbeans.api.project.Project;
18 import org.netbeans.modules.web.jsf.api.ConfigurationUtils;
19 import org.netbeans.modules.web.jsf.api.editor.JSFConfigEditorContext;
20 import org.netbeans.modules.web.jsf.api.facesmodel.FacesConfig;
21 import org.netbeans.modules.web.jsf.api.facesmodel.JSFConfigModel;
22 import org.netbeans.modules.web.jsf.api.facesmodel.NavigationCase;
23 import org.netbeans.modules.web.jsf.api.facesmodel.NavigationRule;
24 import org.openide.filesystems.FileObject;
25 import org.openide.loaders.DataNode;
26 import org.openide.loaders.DataObject;
27 import org.openide.loaders.DataObjectNotFoundException;
28 import org.openide.nodes.AbstractNode;
29 import org.openide.nodes.Children;
30 import org.openide.util.Exceptions;
31
32 /**
33  *
34  * @author joelle lam
35  */

36 public class PageFlowController {
37     private PageFlowView view;
38     private JSFConfigModel configModel;
39     private Project project;
40     private Collection JavaDoc<FileObject> webFiles;
41     
42     /** Creates a new instance of PageFlowController
43      * @param context
44      * @param view
45      */

46     public PageFlowController(JSFConfigEditorContext context, PageFlowView view ) {
47         this.view = view;
48         FileObject configFile = context.getFacesConfigFile();
49         configModel = ConfigurationUtils.getConfigModel(configFile,true);
50         project = FileOwnerQuery.getOwner(configFile);
51         webFiles = getAllProjectRelevantFilesObjects();
52         setupGraph();
53         
54     }
55     
56     /**
57      *
58      * @param source
59      * @param target
60      * @param comp
61      * @return
62      */

63     public NavigationCase createLink(AbstractNode source, AbstractNode target, String JavaDoc comp) {
64         
65         String JavaDoc sourceName = source.getDisplayName();
66         int caseNum = 1;
67         
68         configModel.startTransaction();
69         NavigationCase navCase = configModel.getFactory().createNavigationCase();
70         
71         navCase.setToViewId(target.getDisplayName());
72         FacesConfig facesConfig = configModel.getRootComponent();
73         NavigationRule navRule = getRuleWithFromViewID(facesConfig, source.getDisplayName());
74         
75         
76         if (navRule == null) {
77             navRule = configModel.getFactory().createNavigationRule();
78             navRule.setFromViewId(source.getDisplayName());
79             facesConfig.addNavigationRule(navRule);
80         } else {
81             caseNum = getNewCaseNumber(navRule);
82         }
83         
84         navCase.setFromOutcome(CASE_STRING + Integer.toString(caseNum));
85         navRule.addNavigationCase(navCase);
86         configModel.endTransaction();
87         try {
88             configModel.sync();
89         } catch (IOException JavaDoc ex) {
90             Exceptions.printStackTrace(ex);
91         }
92         view.createEdge(navRule, navCase);
93         return navCase;
94         
95     }
96     
97     private final static String JavaDoc CASE_STRING = "case";
98     
99     private int getNewCaseNumber( NavigationRule navRule ) {
100         Collection JavaDoc<String JavaDoc> caseOutcomes = new HashSet JavaDoc<String JavaDoc>();
101         List JavaDoc<NavigationCase> navCases = navRule.getNavigationCases();
102         for( NavigationCase navCase : navCases ){
103             caseOutcomes.add(navCase.getFromOutcome());
104         }
105         
106         int caseNum = 1;
107         while( true ){
108             if( !caseOutcomes.contains(CASE_STRING + Integer.toString(caseNum)) ){
109                 return caseNum;
110             }
111             caseNum++;
112         }
113     }
114     
115     /**
116      * @return the navigation rule. This will be null if none was found
117      **/

118     private NavigationRule getRuleWithFromViewID(FacesConfig facesConfig, String JavaDoc fromViewId ){
119         List JavaDoc<NavigationRule> rules = facesConfig.getNavigationRules();
120         for( NavigationRule rule : rules ){
121             System.out.println("\nDo these match?");
122             System.out.println(rule.getFromViewId() + " == " + fromViewId);
123             if( rule.getFromViewId().equals(fromViewId) ){
124                 System.out.println("Match Found.");
125                 return rule;
126             }
127         }
128         return null;
129     }
130     
131     
132     
133     
134     private Collection JavaDoc<FileObject> getAllProjectRelevantFilesObjects() {
135         FileObject parentFolder = project.getProjectDirectory();
136         FileObject webFileObject = parentFolder.getFileObject("web");
137         Collection JavaDoc<FileObject> webFiles = getProjectJSPFileOjbects(webFileObject);
138         System.out.println("Web Files: " + webFiles);
139         return webFiles;
140         
141         //Add a listener to the Filesystem that listens to fileDelete, fileCreated, etc.
142
//DataObject.find
143
// DataObject.find(parentFolder)
144

145     }
146     
147     
148     private Collection JavaDoc<FileObject> getProjectJSPFileOjbects(FileObject folder ) {
149         Collection JavaDoc<FileObject> webFiles = new HashSet JavaDoc<FileObject>();
150         FileObject[] childrenFiles = folder.getChildren();
151         for( FileObject file : childrenFiles ){
152             if( !file.isFolder() ) {
153                 if( file.getMIMEType().equals("text/x-jsp"))
154                     webFiles.add(file);
155             } else {
156                 webFiles.addAll(getProjectJSPFileOjbects(file));
157             }
158         }
159         
160         return webFiles;
161     }
162     
163     /**
164      * Setup The Graph
165      * Should only be called by init();
166      *
167      **/

168     public void setupGraph(){
169         assert configModel !=null;
170         assert project != null;
171         assert webFiles != null;
172         
173         view.clearGraph();
174         
175         FacesConfig facesConfig = configModel.getRootComponent();
176         
177         List JavaDoc<NavigationRule> rules = facesConfig.getNavigationRules();
178         String JavaDoc currentScope = PageFlowUtilities.getInstance().getCurrentScope();
179         Collection JavaDoc<String JavaDoc> pagesInConfig = getFacesConfigPageNames(rules);
180         if (currentScope.equals(PageFlowUtilities.LBL_SCOPE_FACESCONFIG)){
181             createFacesConfigPageNodes(pagesInConfig);
182         } else if (currentScope.equals(PageFlowUtilities.LBL_SCOPE_PROJECT)) {
183             createAllProjectPageNodes(pagesInConfig);
184         }
185         createAllEdges(rules);
186         // view.layoutGraph();
187

188         view.validateGraph();
189         
190     }
191     
192     private void createAllEdges( List JavaDoc<NavigationRule> rules ){
193         for( NavigationRule rule : rules ) {
194             List JavaDoc<NavigationCase> navCases = rule.getNavigationCases();
195             for( NavigationCase navCase : navCases ){
196                 view.createEdge(rule, navCase);
197             }
198         }
199     }
200     
201     
202     private Collection JavaDoc<String JavaDoc> getFacesConfigPageNames(List JavaDoc<NavigationRule>rules) {
203         // Get all the pages in the faces config.
204
Collection JavaDoc<String JavaDoc> pages = new HashSet JavaDoc<String JavaDoc>();
205         for( NavigationRule rule : rules ){
206             String JavaDoc pageName = rule.getFromViewId();
207             pages.add(pageName);
208             List JavaDoc<NavigationCase> navCases = rule.getNavigationCases();
209             for( NavigationCase navCase : navCases ){
210                 String JavaDoc toPage = navCase.getToViewId();
211                 pages.add(toPage);
212             }
213         }
214         return pages;
215     }
216     
217     private void createAllProjectPageNodes(Collection JavaDoc<String JavaDoc> pagesInConfig) {
218         
219         
220         Collection JavaDoc<String JavaDoc> pages = pagesInConfig;
221         
222         //Create all pages in the project...
223
for( FileObject webFile : webFiles ) {
224             String JavaDoc webFileName = webFile.getNameExt();
225             pages.remove(webFileName);
226             DataNode node = null;
227             try {
228                 node = (DataNode)(DataObject.find(webFile)).getNodeDelegate();
229             } catch ( DataObjectNotFoundException ex ) {
230                 ex.printStackTrace();
231             } catch( ClassCastException JavaDoc cce ){
232                 cce.printStackTrace();
233             }
234             view.createNode(node, null, null);
235         }
236         
237         //Create any pages that don't actually exist but are defined specified by the config file.
238
for( String JavaDoc pageName : pages ){
239             AbstractNode node = new AbstractNode(Children.LEAF);
240             node.setName(pageName);
241             view.createNode(node, null, null);
242         }
243     }
244     
245     private void createFacesConfigPageNodes(Collection JavaDoc<String JavaDoc> pagesInConfig) {
246         Collection JavaDoc<String JavaDoc> pages = pagesInConfig;
247         
248         for( String JavaDoc pageName : pages ) {
249             boolean isFound = false;
250             for( FileObject webFile : webFiles ) {
251                 String JavaDoc webFileName = webFile.getNameExt();
252                 if( webFileName.equals(pageName)) {
253                     DataNode node = null;
254                     try {
255                         node = (DataNode)(DataObject.find(webFile)).getNodeDelegate();
256                     } catch ( DataObjectNotFoundException ex ) {
257                         ex.printStackTrace();
258                     } catch( ClassCastException JavaDoc cce ){
259                         cce.printStackTrace();
260                     }
261                     view.createNode(node, null, null);
262                     isFound = true;
263                 }
264             }
265             if( !isFound ) {
266                 AbstractNode node = new AbstractNode(Children.LEAF);
267                 node.setName(pageName);
268                 view.createNode(node, null, null);
269             }
270             isFound = false;
271         }
272     }
273     
274     
275     
276     
277     
278 }
279
Popular Tags