BrightSide Workbench Full Report + Source Code
FolderNameComboModel.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2011 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 package org.turro.attach.zul;
19 
20 import java.util.*;
21 import org.turro.attach.db.AttachPU;
22 import org.turro.elephant.db.WhereClause;
23 import org.turro.jpa.Dao;
24 import org.turro.plugin.attach.IAttachFolder;
25 import org.zkoss.lang.Strings;
26 import org.zkoss.zul.AbstractListModel;
27 import org.zkoss.zul.ListModel;
28 import org.zkoss.zul.ListSubModel;
29 import org.zkoss.zul.event.ListDataEvent;
30 import org.zkoss.zul.ext.Sortable;
31 
37 @Deprecated
38 public class FolderNameComboModel extends AbstractListModel implements Sortable<IAttachFolder>, ListSubModel, java.io.Serializable {
39 
40  private static final long serialVersionUID = 20081029L;
41 
42  private Object[] data;
43  private IAttachFolder parentFolder;
44 
45  public FolderNameComboModel(Object[] data, IAttachFolder parentFolder) {
46  this.data = data;
47  this.parentFolder = parentFolder;
48  }
49 
50  public FolderNameComboModel(List data, IAttachFolder parentFolder) {
51  this.data = data.toArray(new Object[data.size()]);
52  this.parentFolder = parentFolder;
53  }
54 
55  @Override
56  public Object getElementAt(int index) {
57  if(data == null || data.length <= index) return null;
58  return data[index];
59  }
60 
61  @Override
62  public int getSize() {
63  return data.length;
64  }
65 
66  @Override
67  public void sort(Comparator cmpr, boolean ascending) {
68  Arrays.sort(data, cmpr);
69  fireEvent(ListDataEvent.CONTENTS_CHANGED, -1, -1);
70  }
71 
72  @Override
73  public String getSortDirection(Comparator cmpr) {
74  return "natural";
75  }
76 
77  @Override
78  public ListModel getSubModel(Object value, int nRows) {
79  Dao dao = new AttachPU();
80  WhereClause wc = new WhereClause();
81  wc.addClause("select distinct attachment.path from Attachment as attachment");
82  wc.addClause("where path like :path");
83  wc.addClause("order by attachment.path");
84  wc.addNamedValue("path", rootName());
85  if(nRows < 1) nRows = 10;
86  return new FolderNameComboModel(cookList(dao.getResultList(wc, nRows), (String)value), parentFolder);
87  }
88 
89  private List<String> cookList(List<String> list, String value) {
90  ArrayList<String> result = new ArrayList<String>();
91  String fp = parentFolder.getFolderPath();
92  String pn[] = fp.split("\\/");
93  if(pn.length > 2) {
94  for(String s : list) {
95  String spn = null, cp[] = s.split("\\/");
96  boolean match = true;
97  for(int i = 0; i < pn.length; i++) {
98  if(i >= cp.length) {
99  match = false;
100  break;
101  }
102  if(i != 2 && !Strings.isBlank(pn[i]) && !pn[i].equals(cp[i])) {
103  match = false;
104  break;
105  }
106  }
107  if(match) {
108  for(int i = pn.length; i < cp.length; i++) {
109  spn = (spn != null ? spn + "/" : "") + cp[i];
110  }
111  if(!result.contains(spn) && spn != null) result.add(spn);
112  }
113  }
114  }
115  Collections.sort(result, String.CASE_INSENSITIVE_ORDER);
116  return result;
117  }
118 
119  private String rootName() {
120  String s[] = parentFolder.getFolderPath().split("\\/");
121  return (s.length > 1 ? "/" + s[1] : "") + "/%";
122  }
123 
124 }
ListModel getSubModel(Object value, int nRows)
FolderNameComboModel(List data, IAttachFolder parentFolder)
void sort(Comparator cmpr, boolean ascending)
FolderNameComboModel(Object[] data, IAttachFolder parentFolder)
void addNamedValue(String name, Object value)