KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > portlets > content > display > component > UIContentConfig


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.content.display.component;
6
7 import java.util.Enumeration;
8 import java.util.HashMap;
9 import java.util.Map;
10 import javax.faces.context.ExternalContext;
11 import javax.faces.context.FacesContext;
12 import javax.portlet.PortletPreferences;
13 import javax.portlet.PortletRequest;
14 import org.exoplatform.container.SessionContainer;
15 import org.exoplatform.faces.core.component.UIExoCommand;
16 import org.exoplatform.faces.core.event.ExoActionEvent;
17 import org.exoplatform.faces.core.event.ExoActionListener;
18 import org.exoplatform.portal.session.ExoPortal;
19 import org.exoplatform.portlets.content.ContentUtil;
20 import org.exoplatform.portlets.content.display.component.model.ContentConfig;
21 import org.exoplatform.services.portal.model.Node;
22 /**
23  * Wed, Dec 22, 2003 @ 23:14
24  * @author: Tuan Nguyen
25  * @email: tuan08@users.sourceforge.net
26  * @version: $Id: UIContentConfig.java,v 1.4 2004/11/03 04:24:55 tuan08 Exp $
27  */

28 public class UIContentConfig extends UIExoCommand {
29     
30     final static public String[] DEFAULT_VALUES = { "title=", "uri=", "encoding="};
31     
32     private Map configs_;
33
34   private boolean modificationAllowed_;
35     
36     public UIContentConfig() {
37         setRendererType("ContentConfigRenderer");
38         setId("UIContentConfig");
39         updateConfigs();
40     addActionListener(DeleteActionListener.class, DELETE_ACTION) ;
41     addActionListener(EditActionListener.class, "edit") ;
42     addActionListener(ModifyActionListener.class, "modify") ;
43     addActionListener(NewActionListener.class, "new") ;
44     }
45     
46     private void updateConfigs() {
47         ExternalContext eContext = FacesContext.getCurrentInstance().getExternalContext();
48         PortletRequest request = (PortletRequest) eContext.getRequest();
49         PortletPreferences prefs = request.getPreferences();
50         Enumeration enum = prefs.getNames();
51         configs_ = new HashMap();
52         while (enum.hasMoreElements()) {
53             String name = (String) enum.nextElement();
54             String[] values = prefs.getValues(name, DEFAULT_VALUES);
55             ContentConfig contentConfig = new ContentConfig(name, values);
56             configs_.put(name, contentConfig);
57         }
58     }
59     
60     public Map getAllConfigs() {
61         return configs_;
62     }
63     
64     public String getFamily() {
65         return "org.exoplatform.portlets.content.display.component.UIContentConfig";
66     }
67   
68   public boolean isModificationAllowed() {
69     return modificationAllowed_;
70   }
71   
72   public void setModificationAllowed(boolean modificationAllowed) {
73     modificationAllowed_ = modificationAllowed;
74   }
75     
76     public void saveContentConfig(String action, ContentConfig config) throws Exception {
77         FacesContext context = getFacesContext();
78         PortletRequest request = (PortletRequest) context.getExternalContext().getRequest();
79         PortletPreferences prefs = request.getPreferences();
80         
81         String uri = config.getUri();
82         if (action.equals(ContentUtil.EDIT_STATE)) {
83             if (uri == null) {
84                 uri = "";
85                 config.setUri(uri);
86             }
87         } else if (action.equals(ContentUtil.MODIFY_STATE)) {
88       if(uri == null || "".equals(uri)){
89         ExoPortal portal = (ExoPortal)SessionContainer.getComponent(ExoPortal.class) ;
90         Node selectedNode = portal.getSelectedNode();
91         uri = selectedNode.getUri();
92         if("/".equals(uri)){
93           uri = "/" + selectedNode.getName();
94         }
95       } else {
96         if(!uri.startsWith("/"))
97           uri = "/" + uri;
98       }
99             ContentUtil.storeContent(uri, config.getContent());
100         }
101         
102         String name = config.getName();
103         String[] tmp = new String[3];
104         tmp[0] = "title=" + config.getTitle();
105         tmp[1] = "uri=" + uri;
106         tmp[2] = "encoding=" + config.getEncoding();
107         
108         prefs.setValues(name, tmp);
109         prefs.store();
110         updateConfigs();
111     }
112   
113     static public class DeleteActionListener extends ExoActionListener {
114         public void execute(ExoActionEvent event) throws Exception {
115             UIContentConfig uiConfig = (UIContentConfig) event.getComponent() ;
116             String name = event.getParameter("name") ;
117             //if (!"default".equals(name)) {
118
//uiConfig.configs_.remove(name);
119
PortletRequest request =
120           (PortletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
121                 PortletPreferences prefs = request.getPreferences();
122                 prefs.reset(name);
123                 prefs.store();
124         uiConfig.updateConfigs();
125             //}
126
}
127     }
128   
129   static public class EditActionListener extends ExoActionListener {
130     public void execute(ExoActionEvent event) throws Exception {
131       UIContentConfig uiConfig = (UIContentConfig) event.getComponent() ;
132       String name = event.getParameter("name");
133       ContentConfig config = (ContentConfig) uiConfig.configs_.get(name);
134       UIContentConfigForm uiConfigForm =
135         (UIContentConfigForm)uiConfig.getSibling(UIContentConfigForm.class);
136       uiConfigForm.setContentConfig(config);
137       uiConfig.setRenderedSibling(UIContentConfigForm.class) ;
138     }
139   }
140   
141   static public class NewActionListener extends ExoActionListener {
142     public void execute(ExoActionEvent event) throws Exception {
143       UIContentConfig uiConfig = (UIContentConfig) event.getComponent() ;
144       UIContentConfigForm uiConfigForm =
145         (UIContentConfigForm)uiConfig.getSibling(UIContentConfigForm.class);
146       uiConfigForm.setContentConfig(null);
147       uiConfig.setRenderedSibling(UIContentConfigForm.class) ;
148     }
149   }
150   
151   static public class ModifyActionListener extends ExoActionListener {
152     public void execute(ExoActionEvent event) throws Exception {
153       UIContentConfig uiConfig = (UIContentConfig) event.getComponent() ;
154       String name = event.getParameter("name");
155       ContentConfig config = (ContentConfig) uiConfig.configs_.get(name);
156       UIContentEditor uiEditor =
157         ( UIContentEditor)uiConfig.getSibling( UIContentEditor.class);
158       uiEditor.setContentConfig(config);
159       uiConfig.setRenderedSibling(UIContentEditor.class) ;
160     }
161   }
162
163 }
Popular Tags