View Javadoc
1   /*
2    * Copyright (C) 2003-2007 eXo Platform SAS.
3    *
4    * This program is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Affero General Public License
6    * as published by the Free Software Foundation; either version 3
7    * of the License, or (at your option) any later version.
8    *
9    * This program is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU General Public License
15   * along with this program; if not, see<http://www.gnu.org/licenses/>.
16   */
17  package org.exoplatform.ecm.webui.component.explorer;
18  
19  import java.util.ArrayList;
20  import java.util.List;
21  
22  import javax.portlet.PortletPreferences;
23  
24  import org.exoplatform.ecm.webui.form.UIFormInputSetWithAction;
25  import org.exoplatform.ecm.webui.selector.UISelectable;
26  import org.exoplatform.services.cms.drives.DriveData;
27  import org.exoplatform.web.application.ApplicationMessage;
28  import org.exoplatform.webui.application.WebuiRequestContext;
29  import org.exoplatform.webui.application.portlet.PortletRequestContext;
30  import org.exoplatform.webui.config.annotation.ComponentConfig;
31  import org.exoplatform.webui.config.annotation.EventConfig;
32  import org.exoplatform.webui.core.UIApplication;
33  import org.exoplatform.webui.core.UIComponent;
34  import org.exoplatform.webui.core.UIPopupWindow;
35  import org.exoplatform.webui.core.lifecycle.UIFormLifecycle;
36  import org.exoplatform.webui.core.model.SelectItemOption;
37  import org.exoplatform.webui.event.Event;
38  import org.exoplatform.webui.event.Event.Phase;
39  import org.exoplatform.webui.event.EventListener;
40  import org.exoplatform.webui.form.UIForm;
41  import org.exoplatform.webui.form.UIFormSelectBox;
42  import org.exoplatform.webui.form.UIFormStringInput;
43  import org.exoplatform.webui.form.input.UICheckBoxInput;
44  
45  /**
46   * Created by The eXo Platform SARL
47   * Author : Ly Dinh Quang
48   *          quang.ly@exoplatform.com
49   * 3 févr. 09
50   */
51  @ComponentConfig(lifecycle = UIFormLifecycle.class, template = "system:/groovy/webui/form/UIFormWithTitle.gtmpl", events = {
52      @EventConfig(phase = Phase.DECODE, listeners = UIJcrExplorerEditForm.SaveActionListener.class),
53      @EventConfig(phase = Phase.DECODE, listeners = UIJcrExplorerEditForm.CancelActionListener.class),
54      @EventConfig(phase = Phase.DECODE, listeners = UIJcrExplorerEditForm.SelectTypeActionListener.class),
55      @EventConfig(phase = Phase.DECODE, listeners = UIJcrExplorerEditForm.SelectDriveActionListener.class),
56      @EventConfig(phase = Phase.DECODE, listeners = UIJcrExplorerEditForm.SelectNodePathActionListener.class) }
57  )
58  public class UIJcrExplorerEditForm extends UIForm implements UISelectable {
59    private boolean flagSelectRender = false;
60  
61    public static final String PARAM_PATH_ACTION = "SelectNodePath";
62  
63    public static final String PARAM_PATH_INPUT = "nodePath";
64  
65    private static final String POPUP_SELECT_PATH_INPUT = "PopupSelectPath";
66  
67  
68    public UIJcrExplorerEditForm() throws Exception {
69      List<SelectItemOption<String>> listType = new ArrayList<SelectItemOption<String>>();
70      String usecase = getPreference().getValue(UIJCRExplorerPortlet.USECASE, "");
71      listType.add(new SelectItemOption<String>("Selection", "selection"));
72      listType.add(new SelectItemOption<String>("Jailed", "jailed"));
73      listType.add(new SelectItemOption<String>("Personal", "personal"));    
74      listType.add(new SelectItemOption<String>("Parameterize", "parameterize"));
75      UIFormSelectBox typeSelectBox = new UIFormSelectBox(UIJCRExplorerPortlet.USECASE, UIJCRExplorerPortlet.USECASE, listType);
76      typeSelectBox.setValue(usecase);
77      typeSelectBox.setOnChange("SelectType");
78      addChild(typeSelectBox);
79  
80      UIFormInputSetWithAction driveNameInput = new UIFormInputSetWithAction("DriveNameInput");
81      UIFormStringInput stringInputDrive = new UIFormStringInput(UIJCRExplorerPortlet.DRIVE_NAME,
82                                                                 UIJCRExplorerPortlet.DRIVE_NAME,
83                                                                 null);
84      stringInputDrive.setValue(getPreference().getValue(UIJCRExplorerPortlet.DRIVE_NAME, ""));
85      stringInputDrive.setDisabled(true);
86      driveNameInput.addUIFormInput(stringInputDrive);
87      driveNameInput.setActionInfo(UIJCRExplorerPortlet.DRIVE_NAME, new String[] {"SelectDrive"});
88      addUIComponentInput(driveNameInput);
89  
90      UIFormInputSetWithAction uiParamPathInput = new UIFormInputSetWithAction(PARAM_PATH_ACTION);
91      UIFormStringInput pathInput = new UIFormStringInput(UIJCRExplorerPortlet.PARAMETERIZE_PATH,
92                                                          UIJCRExplorerPortlet.PARAMETERIZE_PATH,
93                                                          null);
94      pathInput.setValue(getPreference().getValue(UIJCRExplorerPortlet.PARAMETERIZE_PATH, ""));
95      pathInput.setDisabled(true);
96      uiParamPathInput.addUIFormInput(pathInput);
97      uiParamPathInput.setActionInfo(UIJCRExplorerPortlet.PARAMETERIZE_PATH, new String[] {PARAM_PATH_ACTION});
98      addUIComponentInput(uiParamPathInput);
99  
100     driveNameInput.setRendered(true);
101     uiParamPathInput.setRendered(false);
102 
103     UICheckBoxInput uiFormCheckBoxTop = new UICheckBoxInput(UIJCRExplorerPortlet.SHOW_TOP_BAR,
104                                                                                       UIJCRExplorerPortlet.SHOW_TOP_BAR,
105                                                                                       true);
106     uiFormCheckBoxTop.setChecked(Boolean.parseBoolean(getPreference().getValue(UIJCRExplorerPortlet.SHOW_TOP_BAR, "true")));
107     addUIFormInput(uiFormCheckBoxTop);
108 
109     UICheckBoxInput uiFormCheckBoxAction = new UICheckBoxInput(UIJCRExplorerPortlet.SHOW_ACTION_BAR,
110                                                                                          UIJCRExplorerPortlet.SHOW_ACTION_BAR,
111                                                                                          true);
112     uiFormCheckBoxAction.setChecked(Boolean.parseBoolean(getPreference().getValue(UIJCRExplorerPortlet.SHOW_ACTION_BAR, "true")));
113     addUIFormInput(uiFormCheckBoxAction);
114 
115     UICheckBoxInput uiFormCheckBoxSide = new UICheckBoxInput(UIJCRExplorerPortlet.SHOW_SIDE_BAR,
116                                                                                        UIJCRExplorerPortlet.SHOW_SIDE_BAR,
117                                                                                        true);
118     uiFormCheckBoxSide.setChecked(Boolean.parseBoolean(getPreference().getValue(UIJCRExplorerPortlet.SHOW_SIDE_BAR, "true")));
119     addUIFormInput(uiFormCheckBoxSide);
120 
121     UICheckBoxInput uiFormCheckBoxFilter = new UICheckBoxInput(UIJCRExplorerPortlet.SHOW_FILTER_BAR,
122                                                                                          UIJCRExplorerPortlet.SHOW_FILTER_BAR,
123                                                                                          true);
124     uiFormCheckBoxFilter.setChecked(Boolean.parseBoolean(getPreference().getValue(UIJCRExplorerPortlet.SHOW_FILTER_BAR, "true")));
125     addUIFormInput(uiFormCheckBoxFilter);
126 
127     if(usecase.equals(UIJCRExplorerPortlet.PERSONAL)) {
128       driveNameInput.setRendered(false);
129     } else if (usecase.equals(UIJCRExplorerPortlet.PARAMETERIZE)) {
130       uiParamPathInput.setRendered(true);
131     }
132     setActions(new  String[] {"Save", "Cancel"});
133   }
134   
135   public boolean isFlagSelectRender() {
136     return flagSelectRender;
137   }
138 
139   public void setFlagSelectRender(boolean flagSelectRender) {
140     this.flagSelectRender = flagSelectRender;
141   }
142 
143   private PortletPreferences getPreference() {
144     PortletRequestContext pcontext = (PortletRequestContext)WebuiRequestContext.getCurrentInstance();
145     return pcontext.getRequest().getPreferences();
146   }
147 
148   public static class CancelActionListener extends EventListener<UIJcrExplorerEditForm>{
149     public void execute(Event<UIJcrExplorerEditForm> event) throws Exception {
150       UIJcrExplorerEditForm uiForm = event.getSource();
151       PortletPreferences pref = uiForm.getPreference();
152       UIFormSelectBox typeSelectBox = uiForm.getChildById(UIJCRExplorerPortlet.USECASE);
153       typeSelectBox.setValue(pref.getValue(UIJCRExplorerPortlet.USECASE, ""));
154       UIFormInputSetWithAction driveNameInput = uiForm.getChildById("DriveNameInput");
155       UIFormStringInput stringInputDrive = driveNameInput.getUIStringInput(UIJCRExplorerPortlet.DRIVE_NAME);
156       stringInputDrive.setValue(pref.getValue(UIJCRExplorerPortlet.DRIVE_NAME, ""));
157 
158       UICheckBoxInput checkBoxShowTopBar = uiForm.getChildById(UIJCRExplorerPortlet.SHOW_TOP_BAR);
159       checkBoxShowTopBar.setChecked(Boolean.parseBoolean(pref.getValue(UIJCRExplorerPortlet.SHOW_TOP_BAR, "true")));
160       UICheckBoxInput checkBoxShowActionBar = uiForm.getChildById(UIJCRExplorerPortlet.SHOW_ACTION_BAR);
161       checkBoxShowActionBar.setChecked(Boolean.parseBoolean(pref.getValue(UIJCRExplorerPortlet.SHOW_ACTION_BAR, "true")));
162       UICheckBoxInput checkBoxShowLeftBar = uiForm.getChildById(UIJCRExplorerPortlet.SHOW_SIDE_BAR);
163       checkBoxShowLeftBar.setChecked(Boolean.parseBoolean(pref.getValue(UIJCRExplorerPortlet.SHOW_SIDE_BAR, "true")));
164       UICheckBoxInput checkBoxShowFilterBar = uiForm.getChildById(UIJCRExplorerPortlet.SHOW_FILTER_BAR);
165       checkBoxShowFilterBar.setChecked(Boolean.parseBoolean(pref.getValue(UIJCRExplorerPortlet.SHOW_FILTER_BAR,
166                                                                           "true")));
167 
168       // update
169       UIFormInputSetWithAction uiParamPathInput = uiForm.getChildById(PARAM_PATH_ACTION);
170       UIFormStringInput stringInputPath = uiParamPathInput.getUIStringInput(UIJCRExplorerPortlet.PARAMETERIZE_PATH);
171       stringInputPath.setValue(pref.getValue(UIJCRExplorerPortlet.PARAMETERIZE_PATH, ""));
172 
173       if (pref.getValue(UIJCRExplorerPortlet.USECASE, "").equals(UIJCRExplorerPortlet.JAILED)) {
174         driveNameInput.setRendered(true);
175         uiParamPathInput.setRendered(false);
176       } else if (pref.getValue(UIJCRExplorerPortlet.USECASE, "").equals(UIJCRExplorerPortlet.PARAMETERIZE)) {
177         driveNameInput.setRendered(true);
178         uiParamPathInput.setRendered(true);
179       } else {
180         driveNameInput.setRendered(false);
181         uiParamPathInput.setRendered(false);
182       }
183 
184       UIApplication uiApp  = uiForm.getAncestorOfType(UIApplication.class);
185       uiApp.addMessage(new ApplicationMessage("UIJcrExplorerEditForm.msg.fields-cancelled", null));
186       event.getRequestContext().addUIComponentToUpdateByAjax(uiForm);
187     }
188   }
189 
190   public static class SelectTypeActionListener extends EventListener<UIJcrExplorerEditForm>{
191     public void execute(Event<UIJcrExplorerEditForm> event) throws Exception {
192       UIJcrExplorerEditForm uiForm = event.getSource();
193       UIJCRExplorerPortlet uiJExplorerPortlet = uiForm.getAncestorOfType(UIJCRExplorerPortlet.class);
194       UIFormSelectBox typeSelectBox = uiForm.getChildById(UIJCRExplorerPortlet.USECASE);
195       UIFormInputSetWithAction driveNameInput = uiForm.getChildById("DriveNameInput");
196       UIFormInputSetWithAction uiParamPathInput = uiForm.getChildById(PARAM_PATH_ACTION);
197       driveNameInput.setRendered(true);
198       uiParamPathInput.setRendered(false);
199       if (typeSelectBox.getValue().equals(UIJCRExplorerPortlet.JAILED)) {
200         UIFormStringInput stringInputDrive = driveNameInput.getUIStringInput(UIJCRExplorerPortlet.DRIVE_NAME);
201         stringInputDrive.setRendered(true);
202         stringInputDrive.setValue("");
203         driveNameInput.setRendered(true);      
204       } else if(typeSelectBox.getValue().equals(UIJCRExplorerPortlet.SELECTION)) {
205         UIFormStringInput stringInputDrive =
206           driveNameInput.getUIStringInput(UIJCRExplorerPortlet.DRIVE_NAME);
207         if(stringInputDrive.isRendered()) stringInputDrive.setRendered(false);
208       } else if(typeSelectBox.getValue().equals(UIJCRExplorerPortlet.PERSONAL)) {
209         DriveData personalPrivateDrive = uiJExplorerPortlet.getUserDrive();
210         UIFormStringInput stringInputDrive =
211           driveNameInput.getUIStringInput(UIJCRExplorerPortlet.DRIVE_NAME);
212         stringInputDrive.setRendered(true);
213         stringInputDrive.setValue(personalPrivateDrive.getName());
214         driveNameInput.setRendered(false);
215         UIApplication uiApp = uiForm.getAncestorOfType(UIApplication.class);
216         uiApp.addMessage(new ApplicationMessage("UIJcrExplorerEditForm.msg.personal-usecase", null));
217       } else if(typeSelectBox.getValue().equals(UIJCRExplorerPortlet.PARAMETERIZE)) {
218         UIFormStringInput stringInputDrive = driveNameInput.getUIStringInput(UIJCRExplorerPortlet.DRIVE_NAME);
219         stringInputDrive.setRendered(true);
220         stringInputDrive.setValue("");
221         driveNameInput.setRendered(true);
222 
223         UIFormStringInput stringInputDrivePath = uiParamPathInput.getUIStringInput(UIJCRExplorerPortlet.PARAMETERIZE_PATH);
224         stringInputDrivePath.setValue("");
225         uiParamPathInput.setRendered(true);
226       }
227       event.getRequestContext().addUIComponentToUpdateByAjax(uiForm);
228     }
229   }
230 
231   public static class SaveActionListener extends EventListener<UIJcrExplorerEditForm>{
232     public void execute(Event<UIJcrExplorerEditForm> event) throws Exception {
233       UIJcrExplorerEditForm uiForm = event.getSource();
234       PortletPreferences pref = uiForm.getPreference();
235       UIFormSelectBox typeSelectBox = uiForm.getChildById(UIJCRExplorerPortlet.USECASE);
236 
237       UIFormInputSetWithAction driveNameInput = uiForm.getChildById("DriveNameInput");
238       UIFormStringInput stringInputDrive = driveNameInput.getUIStringInput(UIJCRExplorerPortlet.DRIVE_NAME);
239 
240       UICheckBoxInput uiFormCheckBoxTop = uiForm.getChildById(UIJCRExplorerPortlet.SHOW_TOP_BAR);
241       UICheckBoxInput uiFormCheckBoxAction = uiForm.getChildById(UIJCRExplorerPortlet.SHOW_ACTION_BAR);
242       UICheckBoxInput uiFormCheckBoxSide = uiForm.getChildById(UIJCRExplorerPortlet.SHOW_SIDE_BAR);
243       UICheckBoxInput uiFormCheckBoxFilter= uiForm.getChildById(UIJCRExplorerPortlet.SHOW_FILTER_BAR);
244 
245       String nodePath = ((UIFormStringInput)uiForm.findComponentById(UIJCRExplorerPortlet.PARAMETERIZE_PATH)).getValue();
246       String driveName = stringInputDrive.getValue();
247       String useCase = typeSelectBox.getValue();
248 
249       if (useCase.equals(UIJCRExplorerPortlet.JAILED) ) {
250         if ((driveName == null) || (driveName.length() == 0)) {
251           UIApplication uiApp = uiForm.getAncestorOfType(UIApplication.class);
252           uiApp.addMessage(new ApplicationMessage("UIJcrExplorerEditForm.msg.notNullDriveName", null,
253               ApplicationMessage.WARNING));
254           return;
255         }
256       } else if (useCase.equals(UIJCRExplorerPortlet.PARAMETERIZE)) {
257         if ((nodePath == null) || (nodePath.length() == 0)) {
258           UIApplication uiApp = uiForm.getAncestorOfType(UIApplication.class);
259           uiApp.addMessage(new ApplicationMessage("UIJcrExplorerEditForm.msg.notNullPath", null,
260               ApplicationMessage.WARNING));
261           return;
262         }
263       }
264 
265       if (useCase.equals(UIJCRExplorerPortlet.SELECTION)) {
266         //driveName = pref.getValue("driveName", "");
267         nodePath = "/";
268       } else {
269 //        uiForm.setFlagSelectRender(true);
270       }
271       uiForm.setFlagSelectRender(true);
272 
273       pref.setValue(UIJCRExplorerPortlet.USECASE, useCase);
274       pref.setValue(UIJCRExplorerPortlet.DRIVE_NAME, driveName);
275       pref.setValue(UIJCRExplorerPortlet.PARAMETERIZE_PATH, nodePath);
276       pref.setValue(UIJCRExplorerPortlet.SHOW_ACTION_BAR, String.valueOf(uiFormCheckBoxAction.getValue()));
277       pref.setValue(UIJCRExplorerPortlet.SHOW_TOP_BAR, String.valueOf(uiFormCheckBoxTop.getValue()));
278       pref.setValue(UIJCRExplorerPortlet.SHOW_SIDE_BAR, String.valueOf(uiFormCheckBoxSide.getValue()));
279       pref.setValue(UIJCRExplorerPortlet.SHOW_FILTER_BAR, String.valueOf(uiFormCheckBoxFilter.getValue()));
280 
281       pref.store();
282 
283       UIApplication  uiApp = uiForm.getAncestorOfType(UIApplication.class);
284       uiApp.addMessage(new ApplicationMessage("UIJcrExplorerEditForm.msg.fields-saved", null));
285       event.getRequestContext().addUIComponentToUpdateByAjax(uiForm);
286     }
287   }
288 
289   public static class SelectDriveActionListener extends EventListener<UIJcrExplorerEditForm>{
290     public void execute(Event<UIJcrExplorerEditForm> event) throws Exception {
291       UIJcrExplorerEditForm uiForm = event.getSource();
292       UIJcrExplorerEditContainer editContainer = uiForm.getParent();
293 
294       UIPopupWindow popupWindow = editContainer.initPopup("PopUpSelectDrive");
295       UIDriveSelector driveSelector = editContainer.createUIComponent(UIDriveSelector.class, null, null);
296       driveSelector.updateGrid();
297       popupWindow.setUIComponent(driveSelector);
298       event.getRequestContext().addUIComponentToUpdateByAjax(uiForm.getParent());
299     }
300   }
301 
302   public static class SelectNodePathActionListener extends EventListener<UIJcrExplorerEditForm>{
303     public void execute(Event<UIJcrExplorerEditForm> event) throws Exception {
304       UIJcrExplorerEditForm uiForm = event.getSource();
305       UIJcrExplorerEditContainer editContainer = uiForm.getParent();
306       UIFormInputSetWithAction driveNameInput = uiForm.getChildById("DriveNameInput");
307       UIFormStringInput stringInputDrive = driveNameInput.getUIStringInput(UIJCRExplorerPortlet.DRIVE_NAME);
308       String driveName = stringInputDrive.getValue();
309  
310       if (driveName == null || driveName.length() == 0) {
311         UIApplication uiApp = uiForm.getAncestorOfType(UIApplication.class);
312         uiApp.addMessage(new ApplicationMessage("UIJcrExplorerEditForm.msg.personal-usecase", null,
313             ApplicationMessage.WARNING));
314         return;
315       }
316       editContainer.initPopupDriveBrowser(POPUP_SELECT_PATH_INPUT, driveName);
317       event.getRequestContext().addUIComponentToUpdateByAjax(uiForm.getParent());
318     }
319   }
320 
321   public void doSelect(String selectField, Object value) throws Exception {
322     String stValue = null;
323     if ("/".equals(String.valueOf(value))) {
324       stValue = "/";
325     } else {
326       if (String.valueOf(value).split(":/").length > 1) stValue = String.valueOf(value).split(":/")[1];
327       else stValue = "/";
328     }
329     ((UIFormStringInput)findComponentById(selectField)).setValue(stValue);
330     UIJcrExplorerEditContainer uiContainer = getParent();
331     for (UIComponent uiChild : uiContainer.getChildren()) {
332       if (uiChild.getId().equals(POPUP_SELECT_PATH_INPUT)) {
333         UIPopupWindow uiPopup = uiContainer.getChildById(uiChild.getId());
334         uiPopup.setRendered(false);
335         uiPopup.setShow(false);
336       }
337     }
338   }
339 }