1 19 package org.netbeans.modules.web.jsf.navigation.vwmodel; 20 21 import org.netbeans.modules.web.jsf.navigation.vwmodel.NavigationModel; 22 import org.netbeans.modules.web.jsf.navigation.*; 23 import org.netbeans.modules.web.jsf.navigation.vwmodel.NavigableComponent; 24 import org.netbeans.modules.web.jsf.navigation.vwmodel.Page; 25 import org.netbeans.modules.visualweb.api.insync.InSyncService; 26 import com.sun.rave.designtime.DesignEvent; 27 import org.netbeans.modules.visualweb.project.jsf.api.JsfProjectUtils; 28 import java.awt.Image ; 29 import java.beans.BeanInfo ; 30 import java.net.MalformedURLException ; 31 import java.net.URL ; 32 import java.util.ArrayList ; 33 import java.util.Collections ; 34 import java.util.Comparator ; 35 import java.util.HashMap ; 36 import java.util.Iterator ; 37 import java.util.List ; 38 import java.util.Map ; 39 import javax.faces.component.ActionSource; 40 import javax.faces.component.ActionSource2; 41 import javax.swing.SwingUtilities ; 42 import org.openide.ErrorManager; 43 import org.openide.filesystems.FileObject; 44 import org.openide.filesystems.URLMapper; 45 import org.openide.util.NbBundle; 46 import org.netbeans.api.project.Project; 48 import org.netbeans.modules.visualweb.project.jsf.api.JsfPortletSupport; 49 import org.netbeans.modules.visualweb.project.jsf.api.JsfPortletSupportException; 50 import org.netbeans.modules.visualweb.api.portlet.dd.PortletModeType; 51 import com.sun.rave.designtime.DesignBean; 52 import com.sun.rave.designtime.DesignProperty; 53 import org.netbeans.modules.visualweb.insync.Model; 54 import org.netbeans.modules.visualweb.insync.ModelSet; 55 import org.netbeans.modules.visualweb.insync.UndoEvent; 56 import org.netbeans.modules.visualweb.insync.faces.HtmlBean; 57 import org.netbeans.modules.visualweb.insync.faces.config.NavigationCase; 58 import org.netbeans.modules.visualweb.insync.faces.config.NavigationRule; 59 import org.netbeans.modules.visualweb.insync.live.MethodBindDesignEvent; 60 import org.netbeans.modules.visualweb.insync.live.MethodBindDesignProperty; 61 import org.netbeans.modules.visualweb.insync.models.ConfigModel; 62 import org.netbeans.modules.visualweb.insync.models.FacesModel; 63 import org.netbeans.modules.visualweb.insync.models.FacesModelSet; 64 import org.netbeans.modules.visualweb.xhtml.FormNamePanel; 65 import java.beans.PropertyChangeListener ; 66 import java.beans.PropertyChangeSupport ; 67 import java.io.IOException ; 68 import javax.swing.JDialog ; 69 import org.openide.DialogDescriptor; 70 import org.openide.DialogDisplayer; 71 import org.openide.filesystems.FileSystem; 72 import org.openide.filesystems.Repository; 73 import org.openide.loaders.DataFolder; 74 import org.openide.loaders.DataObject; 75 import org.openide.loaders.DataObjectNotFoundException; 76 77 78 83 public class NavigationModel extends ConfigModel { 84 85 87 public static class Factory implements Model.Factory { 88 public Model newInstance(ModelSet set, FileObject file) { 89 String nameext = file.getNameExt(); 90 return nameext.equals("navigation.xml") ? new NavigationModel(set, file) : null; 91 } 92 } 93 94 96 private List <Page> pages; 97 private Map <String ,Page> pageMap; private List <Link> links; 99 100 public Page ERROR_PAGE = new Page(NbBundle.getMessage(NavigationModel.class, "ErrorPage"), (FileObject) null, this); 101 102 Page currentPage = null; 103 104 public static final String PAGE = "page"; 105 public static final String LINK = "link"; 106 107 private PropertyChangeSupport propertyChangeSupport; 108 109 public void addPropertyChangeListener(PropertyChangeListener propChangeListener){ 110 propertyChangeSupport.addPropertyChangeListener(propChangeListener); 111 } 112 113 public void removePropertyChangeListener(PropertyChangeListener propChangeListener){ 114 propertyChangeSupport.removePropertyChangeListener(propChangeListener); 115 } 116 117 public NavigationModel(ModelSet owner, FileObject file) { 118 super(owner, file); 119 propertyChangeSupport = new PropertyChangeSupport (this); 120 } 121 122 public boolean isCurrentPage(Page page){ 123 return page == getCurrentPage(); 124 } 125 126 public Page getCurrentPage(){ 127 if (currentPage == null){ 128 List <Page> pages = getPages(); 129 if (!pages.isEmpty()){ 130 return pages.get(0); 131 } 132 } 133 return currentPage; 134 } 135 136 public void setCurrentPage(Page page){ 137 currentPage = page; 138 } 139 140 public void moveCurrentPage(){ 141 List <Page> pages = getPages(); 142 int currIndex = pages.indexOf(getCurrentPage()); 143 currIndex++; 144 if(currIndex >= pages.size()){ 145 currIndex = 0; 146 } 147 currentPage = pages.get(currIndex); 148 } 149 150 boolean isPortletProject() { 151 Project project = getProject(); 152 if (project != null && JsfProjectUtils.getPortletSupport(project) != null) { 153 return true; 154 } 155 return false; 156 } 157 158 159 162 String getPageName(Project project, FileObject jspFile) { 163 if (jspFile == null || project == null) 164 return null; 165 FileObject webRoot = JsfProjectUtils.getDocumentRoot(project); 166 String jspPath = jspFile.getParent().getPath(); 167 jspPath = jspPath.substring(webRoot.getPath().length()); 168 if(jspPath.startsWith("/")) jspPath = jspPath.substring(1); 170 if (jspPath.length() > 0) 171 jspPath += "/"; jspPath += jspFile.getNameExt(); 173 return jspPath; 174 } 175 176 177 180 181 public List <Page> getPages() { 182 if (pages != null) 183 return pages; 184 185 pages = new ArrayList <Page>(); 186 pageMap = new HashMap <String ,Page>(); 187 188 List list = InSyncService.getProvider().getWebPages(getProject(), true, false); 190 Iterator it = list.iterator(); 191 while (it.hasNext()) { 192 FileObject fo = (FileObject)it.next(); 193 String name = getPageName(getProject(), fo); 194 Page page = new Page(name, fo, this); 195 pages.add(page); 196 pageMap.put(name, page); 197 } 198 199 Collections.sort(pages, new Comparator () { 201 public int compare(Object object1, Object object2) { 202 return ((Page) object1).getName().compareTo(((Page) object2).getName()); 203 } 204 }); 205 206 getLinks(); 208 209 return pages; 210 } 211 212 215 public List <Link> getLinks() { 216 if (links != null) { 218 return links; 219 } 220 if (pages == null) { 221 getPages(); 223 } 224 links = new ArrayList <Link>(); 225 226 for( Page page : pages) { 228 page.pointedTo = null; 230 page.pointsTo = null; 231 } 232 233 if(ERROR_PAGE != null) { 234 ERROR_PAGE.pointedTo = null; 235 ERROR_PAGE.pointsTo = null; 236 } 237 238 NavigationRule[] navRules = unit.getRules(); 239 for( NavigationRule navRule : navRules) { 240 241 String from = navRule.getFromView(); 242 if ((from != null) && from.startsWith("/")) { from = from.substring(1); 245 } 246 247 NavigationCase[] navCases = navRule.getCases(); 248 for( NavigationCase navCase : navCases ) { 249 String to = navCase.getToView(); 250 String outcome = navCase.getFromOutcome(); 251 252 257 if (to.startsWith("/")) { to = to.substring(1); 260 } 261 262 if (from == null && to == null) { 263 ErrorManager.getDefault().log("Skipping navigation rule " + navRule+ " case " + navCase + " because both from and to are null"); 264 continue; 265 } 266 267 Page fromPage = null; 269 if (from != null) { 270 fromPage = pageMap.get(from); 271 } 272 Page toPage = null; 273 if (to != null) { 274 toPage = pageMap.get(to); 275 } 276 277 if (fromPage == null && toPage == null) { 278 ErrorManager.getDefault().log("Skipping navigation rule " + navRule + " case " + navCase + " because both from and to are null"); 279 continue; 280 } 281 if (fromPage == null) { 282 fromPage = ERROR_PAGE; 283 } else if (toPage == null) { 284 toPage = ERROR_PAGE; 285 } 286 287 Link link = new Link(outcome, fromPage, toPage, navCase); 288 links.add(link); 289 290 if (fromPage.pointsTo == null) { 292 fromPage.pointsTo = new ArrayList (3); 293 } 294 fromPage.pointsTo.add(toPage); 295 296 if (toPage.pointedTo == null) { 297 toPage.pointedTo = new ArrayList (3); 298 } 299 toPage.pointedTo.add(fromPage); 300 301 } 303 } 304 305 return links; 306 } 307 308 protected void handleDomChanged() { 309 if (!SwingUtilities.isEventDispatchThread()) { 310 SwingUtilities.invokeLater(new Runnable () { 311 public void run() { 312 handleDomChanged(); } 314 } 315 ); 316 return; 317 } 318 319 links = null; 321 324 update(); 326 } 327 328 protected void handleModelChanged(final Model m) { 329 if (!SwingUtilities.isEventDispatchThread()) { 330 SwingUtilities.invokeLater(new Runnable () { 331 public void run() { 332 handleModelChanged(m); } 334 } 335 ); 336 return; 337 } 338 339 update(); 340 341 } 342 343 protected void handleProjectChanged() { 344 346 if (!SwingUtilities.isEventDispatchThread()) { 350 SwingUtilities.invokeLater(new Runnable () { 351 public void run() { 352 handleProjectChanged(); } 354 } 355 ); 356 return; 357 } 358 pages = null; 359 pageMap = null; 360 flush(); 362 handleDomChanged(); 363 } 364 365 public void update(){ 366 if ((propertyChangeSupport != null) && (propertyChangeSupport.getPropertyChangeListeners().length > 0)){ 367 List <Page> pages = getPages(); 368 for( Page page: pages ) { 369 updateBeans(page); 370 } 371 List links = getLinks(); 372 propertyChangeSupport.firePropertyChange(PAGE, null, pages); 373 propertyChangeSupport.firePropertyChange(LINK, null, links); 374 } 375 } 376 377 378 424 425 public boolean updateBeans(Page p) { 426 return updateBeans(p, true); 427 } 428 429 public boolean removeNavigableComponent( NavigableComponent navComp) { 430 DesignBean bean = navComp.getBean(); 431 List <DesignBean> zoomedBeans = new ArrayList <DesignBean>(); 433 if( bean != null ){ 434 return bean.getDesignContext().deleteBean(bean); 435 } 436 return false; 437 } 438 439 445 public boolean updateBeans(Page p, boolean redraw) { 446 FacesModel model = p.getModel(); 448 if (model != null && !model.isBusted()) { 451 DesignBean container = model.getRootBean(); 452 List <DesignBean> zoomedBeans = new ArrayList <DesignBean>(); 454 if (container != null) { 455 findCommandBeans(model, container, zoomedBeans, true); 456 } else { 458 } 460 p.setBeans(new ArrayList ()); 461 for( DesignBean bean : zoomedBeans ) { 463 String name = bean.getInstanceName(); 464 465 466 String designContextName = bean.getDesignContext().getDisplayName(); 467 468 469 String pageName = p.getName(); 470 int lastIndex = pageName.lastIndexOf('.'); 471 if( !pageName.substring(0,lastIndex).equals(designContextName)) { 472 name = designContextName + ":" + name; 473 } 474 475 BeanInfo bi = bean.getBeanInfo(); 476 Image icon = bi != null ? bi.getIcon(BeanInfo.ICON_COLOR_16x16) : null; 478 if (icon == null) { 479 icon = GraphUtilities.getCommandIcon(); 481 } 482 String javaeePlatform = JsfProjectUtils.getJ2eePlatformVersion(getProject()); 483 DesignProperty pr; 484 if ((javaeePlatform != null) && JsfProjectUtils.JAVA_EE_5.equals(javaeePlatform)){ 485 pr = bean.getProperty("actionExpression"); }else{ 487 pr = bean.getProperty("action"); } 489 490 String action = pr != null ? pr.getValueSource() : "Unknown"; Object actionO = pr != null ? pr.getValue() : null; 492 493 501 502 NavigableComponent b = new NavigableComponent(bean, action, p, name, icon); 503 if (action != null && action.startsWith("#{")) { b.dynamic = true; 505 if (pr instanceof MethodBindDesignProperty) { 506 MethodBindDesignProperty mpr = (MethodBindDesignProperty)pr; 507 MethodBindDesignEvent mev = mpr.getEventReference(); 508 if (mev != null) { 509 Object ret = mev.getHandlerMethodReturn(); 510 if (ret instanceof String ) { 511 b.setAction((String )ret); 512 } 514 } 515 } 516 } 517 p.getBeans().add(b); 518 } 519 return true; 520 } else { 521 return false; 522 } 524 } 525 526 private static FacesModel getFragmentModel(FacesModel model, DesignBean fragment) { 527 528 DesignProperty prop = fragment.getProperty("file"); if( prop == null ){ 530 return null; 531 } 532 Object fileO = prop.getValue(); 533 if (!(fileO instanceof String )) { 534 return null; 535 } 536 String file = (String )fileO; 537 if ((file == null) || (file.length() == 0)) { 538 return null; 539 } 540 URL reference = model.getMarkupUnit().getBase(); 541 URL url = null; 542 try { 543 url = new URL (reference, file); if (url == null) { 545 return null; 546 } 547 } catch (MalformedURLException e) { 548 ErrorManager.getDefault().notify(e); 549 return null; 550 } 551 552 Project project = model.getProject(); 553 FacesModelSet models = FacesModelSet.getInstance(project); 554 if (models == null) { 555 return null; 556 } 557 FileObject fo = URLMapper.findFileObject(url); 558 if (fo != null) { 559 FacesModel fragmentModel = models.getFacesModel(fo); 560 if (fragmentModel != null) { 561 return fragmentModel; 562 } 563 } 564 return null; 565 } 566 567 570 private static void findCommandBeans(FacesModel model, DesignBean container, List <DesignBean> beans, 571 boolean includeFragments) { 572 if(container == null) 573 return; 574 575 576 for ( DesignBean designBean : container.getChildBeans()) { 577 578 if( designBean.getInstance() instanceof ActionSource || designBean.getInstance() instanceof ActionSource2 ) { 581 586 if (designBean.getInstance().getClass().getName().equals("com.sun.rave.web.ui.component.DropDown") 587 || (designBean.getInstance().getClass().getName().equals("com.sun.webui.jsf.component.DropDown"))){ 588 continue; 589 } 590 beans.add(designBean); 591 } 592 String className = designBean.getInstance() != null ? 593 designBean.getInstance().getClass().getName() : ""; 594 if (includeFragments && className.equals(HtmlBean.PACKAGE+"Jsp_Directive_Include")) { FacesModel fragmentModel = getFragmentModel(model, designBean); 597 if (fragmentModel != null) { 598 findCommandBeans(fragmentModel, fragmentModel.getRootBean(), beans, true); 599 } 600 } else if (designBean.isContainer()) { 601 findCommandBeans(model, designBean, beans, includeFragments); 602 } 603 } 604 } 605 606 609 public boolean setOutcomeNoLayout(Link link, String outcome, DesignProperty addLinkTo, boolean rename) { 610 String oldOutcome = link.getOutcome(); 611 assert oldOutcome.equals(link.getNavcase().getFromOutcome()); 612 String javaeePlatform = null; 613 614 if (outcome.length() > 0) { 615 link.getNavcase().setFromOutcome(outcome); 616 link.setOutcome(outcome); 617 unit.flush(); 618 619 if (link.getFrom().getBeans() == null && link.getFrom() != ERROR_PAGE) { 622 boolean r = updateBeans(link.getFrom()); 623 } 624 if (link.getFrom().getBeans() != null && link.getFrom() != ERROR_PAGE) { 625 UndoEvent undo = null; 626 try { 627 undo = link.getFrom().model.writeLock(null); for( NavigableComponent bean : link.getFrom().getBeans() ) { 629 if (bean.getBean() != null) { 630 DesignProperty pr = getActionProperty(link, bean); 631 if (pr != null) { 632 boolean setValueSource = (oldOutcome != null && oldOutcome.equals(pr.getValueSource())); 634 if (pr instanceof MethodBindDesignProperty) { 635 MethodBindDesignProperty mpr = (MethodBindDesignProperty) pr; 636 MethodBindDesignEvent mev = mpr.getEventReference(); 637 if (mev != null) { 638 boolean modify = false; 639 if(rename){ 642 if((oldOutcome != null) && oldOutcome.equals(mev.getHandlerMethodReturn())){ 643 modify = true; 644 } 645 }else{ if (addLinkTo == pr) { 647 modify = true; 648 } 649 } 650 if (modify){ 651 if (mev.getHandlerName() == null) { 652 setValueSource = true; 653 } else { 654 if(oldOutcome.equals(outcome)) { 659 if (mev.getHandlerMethodReturn() != null ){ 660 oldOutcome = mev.getHandlerMethodReturn().toString(); 661 } else { 662 oldOutcome = null; 663 } 664 } 665 mev.updateReturnStrings(oldOutcome, outcome); 666 setValueSource = false; 667 } 668 } 669 } 670 } 671 if (setValueSource) { 672 pr.setValueSource(outcome); 673 } 674 } 675 } 676 } 677 }finally { 678 if(link.getFrom().model != null){ 680 link.getFrom().model.writeUnlock(undo); 681 }else{ 682 System.out.println("Insync model of the from page retunred null - Needs fix"); 683 } 684 addLinkTo = null; 685 } 686 } 687 return true; 688 } 689 return false; 690 } 691 692 private DesignProperty getActionProperty(Link link, NavigableComponent bean) { 693 String javaeePlatform; 694 javaeePlatform = JsfProjectUtils.getJ2eePlatformVersion(getProject()); 695 DesignProperty pr; 696 if ((javaeePlatform != null) && JsfProjectUtils.JAVA_EE_5.equals(javaeePlatform)){ 697 pr = bean.getBean().getProperty("actionExpression"); if(pr!= null ){ 699 DesignEvent event = link.getFrom().model.getDefaultEvent(pr.getDesignBean()); 700 if (event != null) { 701 link.getFrom().model.createEventHandler(event); 702 } 703 } 704 }else{ 705 pr = bean.getBean().getProperty("action"); } 707 return pr; 708 } 709 710 713 public void setOutcome(Link link, String outcome, boolean rename) { 714 DesignProperty addLinkTo = null; 715 NavigableComponent pb = link.getFrom().getCurrentBean(); 716 if(pb != null && pb.getBean() != null) { 717 String javaeePlatform = JsfProjectUtils.getJ2eePlatformVersion(getProject()); 718 if ((javaeePlatform != null) && JsfProjectUtils.JAVA_EE_5.equals(javaeePlatform)){ 719 addLinkTo = pb.getBean().getProperty("actionExpression"); }else{ 721 addLinkTo = pb.getBean().getProperty("action"); } 723 } 724 if (setOutcomeNoLayout(link, outcome, addLinkTo, rename)) { 725 update(); 726 } 727 } 728 729 public void fileRenamed(String oldName, String newName, String ext, FileObject fo, boolean remove) { 730 if(remove && file == fo){ 753 getOwner().removeModel(this); 754 } else { 755 handleProjectChanged(); 756 } 757 } 758 759 762 private NavigationRule findRule(Page from) { 763 for( NavigationRule rule : unit.getRules() ) { 765 if ((rule != null) && (rule.getFromView() != null) && rule.getFromView().equals(from.getName())) 766 return rule; 767 } 768 NavigationRule rule = unit.addRule(); 769 rule.setFromView(from.getName()); 770 return rule; 771 } 772 773 776 String getDefaultName(Page from) { 777 String basename = "case"; 778 int num = 1; 779 while (true) { 780 String name = basename + Integer.toString(num); 781 boolean used = false; 782 NavigationRule[] navRules = unit.getRules(); 786 for ( NavigationRule navRule: navRules ) { 787 if (!from.getName().equals(navRule.getFromView())) { 788 continue; 790 } 791 NavigationCase[] navCases = navRule.getCases(); 792 for( NavigationCase navCase : navCases ) { 793 if (navCase.getFromOutcome() != null && navCase.getFromOutcome().equals(name)) { 794 used = true; 795 break; 796 } 797 } 798 if (used) 799 break; 800 } 801 802 if (!used) { 804 return name; 805 } 806 num++; 807 } 808 } 809 810 814 public Link addLink(Page from, Page to, NavigableComponent pbean) { 815 Link link = null; 816 try { 817 ignoreEvents = true; 818 819 String outcome = getDefaultName(from); 820 NavigationRule rule = findRule(from); 821 NavigationCase navcase = rule.addCase(); 822 navcase.setToView(to.getName()); 823 navcase.setFromOutcome(outcome); 824 825 link = new Link(outcome, from, to, navcase); 826 links.add(link); 827 828 if (from.pointsTo == null) { 830 from.pointsTo = new ArrayList (3); 831 } 832 from.pointsTo.add(to); 833 834 if (to.pointedTo == null) { 835 to.pointedTo = new ArrayList (3); 836 } 837 to.pointedTo.add(from); 838 839 if (pbean != null) { 841 assignLink(pbean, link); 842 }else{ 843 update(); 844 } 845 } finally { 846 ignoreEvents = false; 847 848 849 unit.flush(); 850 } 851 return link; 852 } 853 854 public void assignLink(NavigableComponent pageBean, Link port) { 855 if(pageBean != null){ 860 DesignProperty addLinkTo; 861 String javaeePlatform = JsfProjectUtils.getJ2eePlatformVersion(getProject()); 862 if ((javaeePlatform != null) && JsfProjectUtils.JAVA_EE_5.equals(javaeePlatform)){ 863 addLinkTo = pageBean.getBean().getProperty("actionExpression"); 864 }else{ 865 addLinkTo = pageBean.getBean().getProperty("action"); 866 } 867 setOutcomeNoLayout(port, port.getOutcome(), addLinkTo, false); 868 } 869 update(); 870 } 871 872 875 public Link removeLink(Link link) { 876 try { 877 ignoreEvents = true; 878 879 NavigationRule rule = link.getNavcase().getRule(); 880 rule.removeCase(link.getNavcase()); 881 882 NavigationCase[] cases = rule.getCases(); 884 if (cases.length == 0) 885 unit.removeRule(rule); 886 887 links = null; 888 889 link.getTo().pointedTo.remove(link.getFrom()); 890 link.getFrom().pointsTo.remove(link.getTo()); 891 892 link.getFrom().setBeanLinks(null); 896 899 903 if (link.getFrom() != ERROR_PAGE && link.getFrom().model == null) { 904 FacesModel model = ((FacesModelSet)owner).getFacesModel(link.getFrom().getDobj().getPrimaryFile()); 906 if (model == null) { 907 ErrorManager.getDefault().log("Data object " + link.getFrom().getDobj() + " ain't got no insync Model!"); 908 return link; 909 } 910 link.getFrom().model = model; 911 link.getFrom().wasModelBusted = model.isBusted(); 912 } 913 914 if ((link != null) && (link.getFrom() != null) && (link.getFrom().model != null) && !link.getFrom().model.isBusted()) { 916 DesignBean container = link.getFrom().model.getRootBean(); 917 List <DesignBean> beans = new ArrayList <DesignBean>(); 918 findCommandBeans(link.getFrom().model, container, beans, false); 922 UndoEvent undo = null; 923 try { 924 undo = link.getFrom().model.writeLock(null); for( DesignBean designBean : beans ) { 926 DesignProperty pr = null; 927 String javaeePlatform = JsfProjectUtils.getJ2eePlatformVersion(getProject()); 928 if ((javaeePlatform != null) && JsfProjectUtils.JAVA_EE_5.equals(javaeePlatform)){ 929 pr = designBean.getProperty("actionExpression"); 930 }else{ 931 pr = designBean.getProperty("action"); 932 } 933 if (pr != null && link.getOutcome().equals(pr.getValueSource())) { 935 pr.unset(); } 938 if (pr instanceof MethodBindDesignProperty) { 940 MethodBindDesignProperty mpr = (MethodBindDesignProperty) pr; 941 MethodBindDesignEvent mev = mpr.getEventReference(); 942 if (mev != null && mev.getHandlerName() != null) { 943 mev.updateReturnStrings(link.getOutcome(), null); 944 } 945 } 946 } 947 } finally { 948 link.getFrom().model.writeUnlock(undo); 949 } 950 } 951 } finally { 952 ignoreEvents = false; 953 954 update(); 955 956 unit.flush(); } 958 return link; 959 } 960 961 public void removePage(Page page) { 962 Project project = getProject(); 964 if (project != null) { 965 FileObject backingFile = FacesModel.getJavaForJsp(page.getDobj().getPrimaryFile()); 966 if (backingFile != null) { 967 973 FileObject jspPage = page.getDobj().getPrimaryFile(); 974 if(null != jspPage) { 975 JsfPortletSupport portletSupport = JsfProjectUtils.getPortletSupport(project); 976 if(null != portletSupport) { 977 try { 978 PortletModeType mode = portletSupport.getPortletMode(jspPage); 979 if(null != mode) { 980 portletSupport.unsetInitialPage(jspPage); 981 } 982 }catch(JsfPortletSupportException jspse) { 983 ErrorManager.getDefault().notify(jspse); 984 } 985 } 986 } 987 try { 988 DataObject backingObj = DataObject.find(backingFile); 989 if (backingObj != null && backingObj.isValid()) { 990 try { 991 backingObj.delete(); 992 } catch (IOException ex) { 993 ErrorManager.getDefault().notify(ex); 994 } 995 } 996 } catch(DataObjectNotFoundException dnfe) { 997 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, dnfe); 998 } 999 } 1000 } 1001 1002 if (page.getDobj().isValid()) { 1003 try { 1004 page.getDobj().delete(); 1005 } catch (IOException ex) { 1006 ErrorManager.getDefault().notify(ex); 1007 } 1008 } 1009 update(); 1010 1011 } 1012 1013 1016 public Page createWebPage() { 1017 Project project = getProject(); 1018 if (project == null) { 1019 return null; 1020 } 1021 1022 FileObject folder = getWebFolder(); 1023 DataFolder folderObj = null; 1024 try { 1025 folderObj = (DataFolder)DataObject.find(folder); 1026 } catch (DataObjectNotFoundException e) { 1027 } 1028 1029 String error = null; 1030 1031 String name = null; 1032 FormNamePanel panel = new FormNamePanel(project, isPortletProject() ? "PortletPage" : "Page"); 1034 1035 String title = NbBundle.getMessage(GraphUtilities.class, "NewFormTitle"); DialogDescriptor dlg = new DialogDescriptor( 1037 panel, 1038 title, 1039 true, 1040 DialogDescriptor.OK_CANCEL_OPTION, 1041 DialogDescriptor.OK_OPTION, 1042 DialogDescriptor.DEFAULT_ALIGN, null, null); 1045 1046 JDialog dialog = (JDialog ) DialogDisplayer.getDefault().createDialog(dlg); 1047 dialog.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(GraphUtilities.class, "NewFormTitleAcessDesc")); 1048 panel.setDescriptor(dlg); 1049 dialog.show(); 1050 String answer = dlg.getValue().toString(); 1051 if (!dlg.getValue().equals(DialogDescriptor.OK_OPTION)) { 1052 return null; 1054 } 1055 1056 name = panel.getName(); 1057 1058 try { 1060 1061 String javaeePlatform = JsfProjectUtils.getJ2eePlatformVersion(getProject()); 1062 FileSystem fs = Repository.getDefault().getDefaultFileSystem(); 1063 String tmpl; 1064 if (isPortletProject()) { 1065 tmpl = "Templates/JsfPortlet/PortletPage.jsp"; 1066 } else { 1067 if ((javaeePlatform != null) && JsfProjectUtils.JAVA_EE_5.equals(javaeePlatform)){ 1068 tmpl = "Templates/JsfApps/Page12.jsp"; }else{ 1070 tmpl = "Templates/JsfApps/Page.jsp"; } 1072 } 1073 FileObject fo = fs.findResource(tmpl); 1074 if (fo == null) 1075 throw new IOException ("Can't find template FileObject for " + tmpl); DataObject webformTemplate = DataObject.find(fo); 1077 DataObject webform = webformTemplate.createFromTemplate(folderObj, name); 1078 1079 if (webform != null) { 1083 1088 FacesModel model = ((FacesModelSet)getOwner()).getFacesModel(webform.getPrimaryFile()); 1089 if (model == null) 1090 ErrorManager.getDefault().log(webform + " has no insync Model!"); 1091 } 1092 Page page = new Page(name, webform.getPrimaryFile(), this); 1093 return page; 1094 1095 } catch (Exception ex) { 1096 ErrorManager.getDefault().notify(ex); 1097 } 1098 return null; 1099 } 1100 1101 public NavigableComponent addPageBean(Page page, int type) { 1102 DesignBean designBean = addComponent("createComponent", page, page.getBeanClassName(type)); 1103 1104 NavigableComponent navComp = solveNavComponent(page, designBean); 1105 1106 update(); 1108 1109 return navComp; 1110 } 1111 1112 private NavigableComponent solveNavComponent(Page page, DesignBean designBean){ 1113 if( designBean == null || page == null ) { 1114 return null; 1115 } 1116 1117 String name = designBean.getInstanceName(); 1119 1120 1121 String designContextName = designBean.getDesignContext().getDisplayName(); 1122 1123 1124 String pageName = page.getName(); 1125 int lastIndex = pageName.lastIndexOf('.'); 1126 if( !pageName.substring(0,lastIndex).equals(designContextName)) { 1127 name = designContextName + ":" + name; 1128 } 1129 1130 BeanInfo bi = designBean.getBeanInfo(); 1131 Image icon = bi != null ? bi.getIcon(BeanInfo.ICON_COLOR_16x16) : null; 1133 if (icon == null) { 1134 icon = GraphUtilities.getCommandIcon(); 1136 } 1137 String javaeePlatform = JsfProjectUtils.getJ2eePlatformVersion(getProject()); 1138 DesignProperty pr; 1139 if ((javaeePlatform != null) && JsfProjectUtils.JAVA_EE_5.equals(javaeePlatform)){ 1140 pr = designBean.getProperty("actionExpression"); }else{ 1142 pr = designBean.getProperty("action"); } 1144 1145 String action = pr != null ? pr.getValueSource() : "Unknown"; 1147 NavigableComponent navComp = new NavigableComponent(designBean, action, page, name, icon); 1148 return navComp; 1149 1150 } 1151 1152 1153 1154 private DesignBean addComponent(String lockDesc, Page page, String className) { 1155 UndoEvent undo = null; 1157 DesignBean bean = null; 1158 try { 1159 undo = page.model.writeLock(lockDesc); 1160 bean = page.model.getLiveUnit().createBean(className, null, null); 1161 if (bean == null) { 1162 return bean; 1163 } 1164 page.model.beanCreated(bean); 1165 } catch (Exception e) { 1166 ErrorManager.getDefault().notify(e); 1167 } finally { 1168 page.model.writeUnlock(undo); 1169 } 1170 page.model.flush(); 1171 return bean; 1172 1173 } 1174} 1175 | Popular Tags |