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.admin.drives;
18  
19  import java.util.Collections;
20  import java.util.Comparator;
21  import java.util.List;
22  import java.util.MissingResourceException;
23  
24  import org.exoplatform.services.cms.drives.DriveData;
25  import org.exoplatform.services.cms.views.ManageViewService;
26  import org.exoplatform.services.cms.views.ViewConfig;
27  import org.exoplatform.web.application.ApplicationMessage;
28  import org.exoplatform.webui.application.WebuiRequestContext;
29  import org.exoplatform.webui.exception.MessageException;
30  import org.exoplatform.webui.form.UIFormInputSet;
31  import org.exoplatform.webui.form.input.UICheckBoxInput;
32  
33  /**
34   * Created by The eXo Platform SARL
35   * Author : Tran The Trong
36   *          trongtt@gmail.com
37   * Jun 28, 2006
38   */
39  public class UIViewsInputSet extends UIFormInputSet {
40  
41    public UIViewsInputSet(String name) throws Exception {
42      super(name);
43    }
44  
45    public String getViewsSelected() throws Exception {
46      StringBuilder selectedView = new StringBuilder() ;
47      List<ViewConfig> views_ = getApplicationComponent(ManageViewService.class).getAllViews();
48      for(ViewConfig view : views_){
49        String viewName= view.getName() ;
50        boolean checked = getUICheckBoxInput(viewName).isChecked() ;
51        if(checked){
52          if(selectedView.length() > 0) selectedView.append(", ") ;
53          selectedView.append(viewName) ;
54        }
55      }
56      if(selectedView.length() < 1 ) {
57        throw new MessageException(new ApplicationMessage("UIDriveForm.msg.drive-views-invalid",
58                                                          null, ApplicationMessage.WARNING)) ;
59      }
60      return selectedView.toString() ;
61    }
62  
63    private void clear() throws Exception {
64      
65      List<ViewConfig> views_ = getApplicationComponent(ManageViewService.class).getAllViews();
66      Collections.sort(views_, new ViewComparator());
67      for(ViewConfig view : views_){
68        String viewName = view.getName() ;
69        if(getUICheckBoxInput(viewName) != null) {
70          getUICheckBoxInput(viewName).setChecked(false) ;
71        }else{
72          addUIFormInput(new UICheckBoxInput(viewName, viewName, null)) ;
73        }
74  
75      }
76    }
77    
78    static public class ViewComparator implements Comparator<ViewConfig> {
79      public int compare(ViewConfig v1, ViewConfig v2) throws ClassCastException {
80        WebuiRequestContext webReqContext = WebuiRequestContext.getCurrentInstance();
81        String displayName1, displayName2;
82        try {
83          displayName1 = webReqContext.getApplicationResourceBundle().getString("UIDriveForm.label." + v1.getName());  
84        } catch(MissingResourceException mre) {
85          displayName1 = v1.getName();
86        }
87        try {
88          displayName2 = webReqContext.getApplicationResourceBundle().getString("UIDriveForm.label." + v2.getName());
89        } catch(MissingResourceException mre) {
90          displayName2 = v2.getName();
91        }
92        return displayName1.compareToIgnoreCase(displayName2);
93      }
94    }
95  
96    public void update(DriveData drive) throws Exception {
97      clear() ;
98      if( drive == null) return ;
99      String views = drive.getViews() ;
100     String[] array = views.split(",") ;
101     for(String view: array){
102       getUICheckBoxInput(view.trim()).setChecked(true) ;
103     }
104   }
105 }