View Javadoc
1   /*
2    * Copyright (C) 2003-2009 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.wcm.webui.selector.page;
18  
19  import org.exoplatform.webui.application.WebuiRequestContext;
20  import org.exoplatform.webui.config.annotation.ComponentConfig;
21  import org.exoplatform.webui.config.annotation.ComponentConfigs;
22  import org.exoplatform.webui.config.annotation.EventConfig;
23  import org.exoplatform.webui.core.UIComponent;
24  import org.exoplatform.webui.core.UITree;
25  import org.exoplatform.webui.core.lifecycle.UIFormLifecycle;
26  import org.exoplatform.webui.event.Event;
27  import org.exoplatform.webui.event.EventListener;
28  import org.exoplatform.webui.event.Event.Phase;
29  import org.exoplatform.webui.form.UIForm;
30  
31  /**
32   * Created by The eXo Platform SAS
33   * Author : eXoPlatform
34   * chuong.phan@exoplatform.com, phan.le.thanh.chuong@gmail.com
35   * Jun 30, 2009
36   */
37  @ComponentConfigs({
38          @ComponentConfig(
39              lifecycle = UIFormLifecycle.class,
40              template = "classpath:groovy/wcm/webui/selector/page/UIPageSelector.gtmpl",
41              events = {@EventConfig(listeners = UIPageSelector.ChangeNodeActionListener.class, phase = Phase.DECODE)}
42          )
43      }
44  )
45  public class UIPageSelector extends UIForm {
46  
47    /** The source ui component. */
48    private UIComponent sourceUIComponent ;
49  
50    /** The return field name. */
51    private String returnFieldName ;
52  
53    /**
54     * Instantiates a new uI page selector.
55     *
56     * @throws Exception the exception
57     */
58    public UIPageSelector() throws Exception {
59      UIPageNodeSelector pageNodeSelector = addChild(UIPageNodeSelector.class, null, null);
60      UITree uiTree = pageNodeSelector.getChild(UITree.class);
61      uiTree.setUIRightClickPopupMenu(null);
62  
63      UIPageSelectorPanel pageSelectorPanel = addChild(UIPageSelectorPanel.class, null, null);
64      pageSelectorPanel.setSelectedNode(pageNodeSelector.getSelectedNode().getNode());
65      pageSelectorPanel.updateGrid();
66    }
67  
68    /**
69     * Gets the return field name.
70     *
71     * @return the return field name
72     */
73    public String getReturnFieldName() { return returnFieldName; }
74  
75    /**
76     * Sets the return field name.
77     *
78     * @param name the new return field name
79     */
80    public void setReturnFieldName(String name) { this.returnFieldName = name; }
81  
82    /**
83     * Gets the source component.
84     *
85     * @return the source component
86     */
87    public UIComponent getSourceComponent() { return sourceUIComponent; }
88  
89    /**
90     * Sets the source component.
91     *
92     * @param uicomponent the uicomponent
93     * @param initParams the init params
94     */
95    public void setSourceComponent(UIComponent uicomponent, String[] initParams) {
96      sourceUIComponent = uicomponent ;
97      if(initParams == null || initParams.length < 0) return ;
98      for(int i = 0; i < initParams.length; i ++) {
99        if(initParams[i].indexOf("returnField") > -1) {
100         String[] array = initParams[i].split("=") ;
101         returnFieldName = array[1] ;
102         break ;
103       }
104       returnFieldName = initParams[0] ;
105     }
106   }
107 
108   /* (non-Javadoc)
109    * @see org.exoplatform.webui.core.UIComponent#processDecode(org.exoplatform.webui.application.WebuiRequestContext)
110    */
111   public void processDecode(WebuiRequestContext context) throws Exception {
112     super.processDecode(context);
113     String action = context.getRequestParameter(UIForm.ACTION);
114     Event<UIComponent> event = createEvent(action, Event.Phase.DECODE, context) ;
115     if(event != null) event.broadcast() ;
116   }
117 
118   /**
119    * The listener interface for receiving changeNodeAction events.
120    * The class that is interested in processing a changeNodeAction
121    * event implements this interface, and the object created
122    * with that class is registered with a component using the
123    * component's <code>addChangeNodeActionListener</code> method. When
124    * the changeNodeAction event occurs, that object's appropriate
125    * method is invoked.
126    */
127   public static class ChangeNodeActionListener extends EventListener<UIPageSelector> {
128 
129     /* (non-Javadoc)
130      * @see org.exoplatform.webui.event.EventListener#execute(org.exoplatform.webui.event.Event)
131      */
132     public void execute(Event<UIPageSelector> event) throws Exception {
133       UIPageSelector pageSelector = event.getSource() ;
134       UIPageNodeSelector pageNodeSelector = pageSelector.getChild(UIPageNodeSelector.class) ;
135       String uri  = event.getRequestContext().getRequestParameter(OBJECTID) ;
136       UITree tree = pageNodeSelector.getChild(UITree.class) ;
137       if(tree.getParentSelected() == null && (uri == null || uri.length() < 1)){
138         pageNodeSelector.selectNavigation(pageNodeSelector.getId(pageNodeSelector.getSelectedNavigation()));
139       } else {
140         pageNodeSelector.selectUserNodeByUri(uri);
141       }
142 
143       UIPageSelectorPanel pageSelectorPanel = pageSelector.getChild(UIPageSelectorPanel.class);
144       pageSelectorPanel.setSelectedNode(pageNodeSelector.getSelectedNode().getNode());
145       pageSelectorPanel.updateGrid();
146 
147       event.getRequestContext().addUIComponentToUpdateByAjax(pageSelector) ;
148     }
149   }
150 }