KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > portlets > resources > component > UIResource


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.portlets.resources.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.*;
11 import org.exoplatform.faces.core.component.model.*;
12 import org.exoplatform.faces.core.event.CheckRoleInterceptor;
13 import org.exoplatform.faces.core.event.ExoActionEvent;
14 import org.exoplatform.faces.core.event.ExoActionListener;
15 import org.exoplatform.services.resources.ResourceBundleData;
16 import org.exoplatform.services.resources.ResourceBundleService;
17 /**
18  * Wed, March 22, 2004 @ 23:14
19  * @author: Tuan Nguyen
20  * @email: tuan08@users.sourceforge.net
21  * @version: $Id: UIResource.java,v 1.7 2004/08/17 13:07:01 tuan08 Exp $
22  */

23 public class UIResource extends UISimpleForm {
24   private static List JavaDoc LANGUAGES ;
25   static {
26     LANGUAGES = new ArrayList JavaDoc(10) ;
27     LANGUAGES.add(new SelectItem("Default", "en")) ;
28     LANGUAGES.add(new SelectItem("English", "en")) ;
29     LANGUAGES.add(new SelectItem("French", "fr")) ;
30     LANGUAGES.add(new SelectItem("Vietnamese", "vn")) ;
31   }
32   
33   private UITextArea textInput_ ;
34   protected UISelectBox languageInput_ ;
35   protected UIStringInput nameInput_ ;
36   private boolean adminRole_ ;
37   private ResourceBundleData data_ ;
38   private ResourceBundleService service_ ;
39
40   public UIResource(ResourceBundleService service) {
41     super("resource", "post", null) ;
42     setId("UIResource");
43     setClazz("UIResource") ;
44     service_ = service ;
45     nameInput_ = new UIStringInput("name", "") ;
46     languageInput_ = new UISelectBox("language", "en", LANGUAGES) ;
47     languageInput_.setClazz("short") ;
48     textInput_ = new UITextArea("text" , "") ;
49     textInput_.setClazz("large") ;
50     adminRole_ = hasRole("admin") ;
51     add(new Row().
52         add(new ComponentCell(this, textInput_)));
53     add(new Row().
54         add(new ListComponentCell().
55             add("#{UIResource.label.name}").
56             add(this, nameInput_).
57             add(this, languageInput_)));
58     add(new Row().
59         add(new ListComponentCell().
60             add(new FormButton("#{UIResource.button.save}", SAVE_ACTION, EDIT_MODE)).
61             add(new FormButton("#{UIResource.button.edit}", EDIT_ACTION, VIEW_MODE)).
62             add(new FormButton("#{UIResource.button.preview}", VIEW_ACTION, EDIT_MODE)).
63             add(new FormButton("#{UIResource.button.cancel}", CANCEL_ACTION)).
64             addColspan("2").addAlign("center"))) ;
65     addActionListener(EditActionListener.class, EDIT_ACTION) ;
66     addActionListener(PreviewActionListener.class, VIEW_ACTION) ;
67     addActionListener(SaveActionListener.class, SAVE_ACTION) ;
68     addActionListener(CancelActionListener.class, CANCEL_ACTION) ;
69   }
70   
71   public void setResourceBundleData(ResourceBundleData data) {
72     data_ = data ;
73     if (data != null) {
74         textInput_.setValue(data.getData()) ;
75         languageInput_.setValue(data.getLanguage()) ;
76         languageInput_.setEditable(false) ;
77         nameInput_.setValue(data.getName()) ;
78         nameInput_.setEditable(false) ;
79     } else {
80       textInput_.setValue("") ;
81         languageInput_.setValue("") ;
82         nameInput_.setValue("") ;
83         nameInput_.setEditable(true) ;
84         languageInput_.setEditable(true) ;
85     }
86   }
87   
88   static public class CancelActionListener extends ExoActionListener {
89     public void execute(ExoActionEvent event) throws Exception JavaDoc {
90         UIResource uiResource = (UIResource) event.getComponent() ;
91         UIResourcesPortlet uiPortlet = (UIResourcesPortlet)uiResource.getParent() ;
92       UIListResources uiListResources =
93         (UIListResources)uiPortlet.getChildComponentOfType(UIListResources.class);
94       uiPortlet.setRenderedComponent(uiListResources.getId()) ;
95     }
96   }
97   
98   static public class EditActionListener extends ExoActionListener {
99     public EditActionListener() {
100       addInterceptor(new CheckRoleInterceptor("admin")) ;
101     }
102     
103     public void execute(ExoActionEvent event) throws Exception JavaDoc {
104         UIResource uiResource = (UIResource) event.getComponent() ;
105         uiResource.setMode(EDIT_MODE) ;
106     }
107   }
108   
109   static public class PreviewActionListener extends ExoActionListener {
110     public void execute(ExoActionEvent event) throws Exception JavaDoc {
111         UIResource uiResource = (UIResource) event.getComponent() ;
112       uiResource.setMode(VIEW_MODE) ;
113     }
114   }
115   
116   static public class SaveActionListener extends ExoActionListener {
117     public SaveActionListener() {
118       addInterceptor(new CheckRoleInterceptor("admin")) ;
119     }
120     
121     public void execute(ExoActionEvent event) throws Exception JavaDoc {
122         UIResource uiResource = (UIResource) event.getComponent() ;
123         InformationProvider iprovider = findInformationProvider(uiResource) ;
124         String JavaDoc name = uiResource.nameInput_.getValue() ;
125         if(name == null || name.length() == 0) {
126             iprovider.addMessage(new ExoFacesMessage("#{UIResource.msg.resource-name-empty}")) ;
127             return ;
128         }
129         
130         if(uiResource.data_ == null) {
131         uiResource.data_ = uiResource.service_.createResourceBundleDataInstance() ;
132         uiResource.data_.setName(uiResource.nameInput_.getValue()) ;
133         uiResource.data_.setLanguage(uiResource.languageInput_.getValue()) ;
134       }
135       uiResource.data_.setData(uiResource.textInput_.getValue());
136       uiResource.service_.saveResourceBundle(uiResource.data_);
137       iprovider.addMessage(new ExoFacesMessage("#{UIResource.msg.save-resource-success}")) ;
138     }
139   }
140 }
Popular Tags