1 9 10 package org.netbeans.modules.web.jsf.navigation; 11 12 import java.io.IOException ; 13 import java.util.Collection ; 14 import java.util.HashSet ; 15 import java.util.List ; 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 36 public class PageFlowController { 37 private PageFlowView view; 38 private JSFConfigModel configModel; 39 private Project project; 40 private Collection <FileObject> webFiles; 41 42 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 63 public NavigationCase createLink(AbstractNode source, AbstractNode target, String comp) { 64 65 String 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 ex) { 90 Exceptions.printStackTrace(ex); 91 } 92 view.createEdge(navRule, navCase); 93 return navCase; 94 95 } 96 97 private final static String CASE_STRING = "case"; 98 99 private int getNewCaseNumber( NavigationRule navRule ) { 100 Collection <String > caseOutcomes = new HashSet <String >(); 101 List <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 118 private NavigationRule getRuleWithFromViewID(FacesConfig facesConfig, String fromViewId ){ 119 List <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 <FileObject> getAllProjectRelevantFilesObjects() { 135 FileObject parentFolder = project.getProjectDirectory(); 136 FileObject webFileObject = parentFolder.getFileObject("web"); 137 Collection <FileObject> webFiles = getProjectJSPFileOjbects(webFileObject); 138 System.out.println("Web Files: " + webFiles); 139 return webFiles; 140 141 145 } 146 147 148 private Collection <FileObject> getProjectJSPFileOjbects(FileObject folder ) { 149 Collection <FileObject> webFiles = new HashSet <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 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 <NavigationRule> rules = facesConfig.getNavigationRules(); 178 String currentScope = PageFlowUtilities.getInstance().getCurrentScope(); 179 Collection <String > 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 188 view.validateGraph(); 189 190 } 191 192 private void createAllEdges( List <NavigationRule> rules ){ 193 for( NavigationRule rule : rules ) { 194 List <NavigationCase> navCases = rule.getNavigationCases(); 195 for( NavigationCase navCase : navCases ){ 196 view.createEdge(rule, navCase); 197 } 198 } 199 } 200 201 202 private Collection <String > getFacesConfigPageNames(List <NavigationRule>rules) { 203 Collection <String > pages = new HashSet <String >(); 205 for( NavigationRule rule : rules ){ 206 String pageName = rule.getFromViewId(); 207 pages.add(pageName); 208 List <NavigationCase> navCases = rule.getNavigationCases(); 209 for( NavigationCase navCase : navCases ){ 210 String toPage = navCase.getToViewId(); 211 pages.add(toPage); 212 } 213 } 214 return pages; 215 } 216 217 private void createAllProjectPageNodes(Collection <String > pagesInConfig) { 218 219 220 Collection <String > pages = pagesInConfig; 221 222 for( FileObject webFile : webFiles ) { 224 String 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 cce ){ 232 cce.printStackTrace(); 233 } 234 view.createNode(node, null, null); 235 } 236 237 for( String 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 <String > pagesInConfig) { 246 Collection <String > pages = pagesInConfig; 247 248 for( String pageName : pages ) { 249 boolean isFound = false; 250 for( FileObject webFile : webFiles ) { 251 String 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 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 |