KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > portal > faces > component > UIPageModelForm


1 /***************************************************************************
2  * Copyright 2001-2003 The eXo Platform SARL All rights reserved. *
3  * Please look at license.txt in info directory for more license detail. *
4  **************************************************************************/

5 package org.exoplatform.portal.faces.component;
6
7 import java.util.ArrayList JavaDoc;
8 import java.util.List JavaDoc;
9 import org.exoplatform.faces.application.ExoFacesMessage;
10 import org.exoplatform.faces.core.component.InformationProvider;
11 import org.exoplatform.faces.core.component.UIRadioBox;
12 import org.exoplatform.faces.core.component.UISimpleForm;
13 import org.exoplatform.faces.core.component.UIStringInput;
14 import org.exoplatform.faces.core.component.model.*;
15 import org.exoplatform.faces.core.event.ExoActionEvent;
16 import org.exoplatform.faces.core.event.ExoActionListener;
17 import org.exoplatform.faces.core.validator.EmptyFieldValidator;
18 import org.exoplatform.services.portal.PortalConfigService;
19 import org.exoplatform.services.portal.model.Page;
20
21
22 /**
23  * Sat, Jan 03, 2004 @ 11:16
24  * @author: Tuan Nguyen
25  * @email: tuan08@users.sourceforge.net
26  * @version: $Id: UIPageModelForm.java,v 1.5 2004/09/29 17:44:51 benjmestrallet Exp $
27  */

28 public class UIPageModelForm extends UISimpleForm {
29   private UIStringInput nameInput_ ;
30   private UIRadioBox templateInput_ ;
31   private PortalConfigService service_ ;
32   
33   public UIPageModelForm(PortalConfigService service) {
34     super("pageModelForm", "post", null) ;
35     service_ = service ;
36     nameInput_ = new UIStringInput("name", "").
37     addValidator(EmptyFieldValidator.class) ;
38     List JavaDoc pageTemplates = getTemplates("template", service) ;
39     templateInput_ = new UIRadioBox("template", null, pageTemplates) ;
40     add(new HeaderRow().
41         add(new Cell("#{UIPageModelForm.header.new-page}").
42             addColspan("2")));
43     add(new Row().
44         add(new LabelCell("#{UIPageModelForm.label.name}")).
45         add(new ComponentCell(this, nameInput_)));
46     add(new Row().
47         add(new LabelCell("#{UIPageModelForm.label.select-template}")).
48         add(new ComponentCell(this, templateInput_)));
49     add(new Row().add(new ListComponentCell().
50         add(new FormButton("#{UIPageModelForm.link.save}", SAVE_ACTION)).
51         add(new FormButton("#{UIPageModelForm.link.cancel}", CANCEL_ACTION)).
52         addColspan("2").addAlign("center"))) ;
53     addActionListener(SaveActionListener.class, SAVE_ACTION) ;
54     addActionListener(CancelActionListener.class, CANCEL_ACTION) ;
55   }
56   
57   
58   
59   static public class SaveActionListener extends ExoActionListener {
60     public void execute(ExoActionEvent event) throws Exception JavaDoc {
61       UIPageModelForm uiForm = (UIPageModelForm) event.getSource();
62       UIPortal uiPortal = (UIPortal) uiForm.getAncestorOfType(UIPortal.class);
63       String JavaDoc pageName = uiForm.nameInput_.getValue();
64       String JavaDoc pageId = uiPortal.getOwner() + ":" + uiForm.nameInput_.getValue();
65       if (uiForm.service_.getPage(pageId) != null) {
66         InformationProvider iprovider = findInformationProvider(uiForm);
67         iprovider.addMessage(new ExoFacesMessage("#{UIPageModelForm.msg.reference-page-exist}"));
68       }
69       Page page =
70         uiForm.service_.getPredefinedTemplate("template", uiForm.templateInput_.getValue());
71       page.setOwner(uiPortal.getOwner());
72       page.setName(pageName);
73       uiForm.service_.savePage(page);
74       UIPageList uiPageList = (UIPageList) uiForm.getSibling(UIPageList.class);
75       uiPageList.update();
76       uiForm.setRenderedSibling(UIPageList.class);
77     }
78   }
79
80   static public class CancelActionListener extends ExoActionListener {
81     public void execute(ExoActionEvent event) throws Exception JavaDoc {
82       UIPageModelForm uiForm = (UIPageModelForm) event.getSource();
83       uiForm.setRenderedSibling(UIPageList.class);
84     }
85   }
86
87   static private List JavaDoc getTemplates(String JavaDoc user, PortalConfigService service) {
88     List JavaDoc pageTemplates = service.getPredefinedTemplates(user);
89     List JavaDoc templates = new ArrayList JavaDoc(pageTemplates.size());
90     String JavaDoc label = null;
91     for (int i = 0; i < pageTemplates.size(); i++) {
92       Page page = (Page) pageTemplates.get(i);
93       String JavaDoc icon = page.getIcon();
94       if (icon != null) {
95         StringBuffer JavaDoc b = new StringBuffer JavaDoc();
96         b.append("<img SRC='" + icon + "' title='" + page.getTitle() + "'/>");
97         label = b.toString();
98       } else {
99         label = page.getTitle();
100       }
101       templates.add(new SelectItem(label, page.getName()));
102     }
103     return templates;
104   }
105   
106 }
Popular Tags