BrightSide Workbench Full Report + Source Code
TagNameComboModel.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.contacts.zul.contact;
19 
20 import java.util.Arrays;
21 import java.util.Comparator;
22 import java.util.List;
23 import java.util.stream.Collectors;
24 import org.turro.tags.Tags;
25 import org.zkoss.zul.AbstractListModel;
26 import org.zkoss.zul.ListModel;
27 import org.zkoss.zul.ListSubModel;
28 import org.zkoss.zul.event.ListDataEvent;
29 import org.zkoss.zul.ext.Sortable;
30 
35 public class TagNameComboModel extends AbstractListModel implements Sortable<String>, ListSubModel, java.io.Serializable {
36 
37  private static final long serialVersionUID = 20081029L;
38 
39  private Object[] data;
40 
41  public TagNameComboModel(Object[] data) {
42  this.data = data;
43  }
44 
45  public TagNameComboModel(List data) {
46  this.data = data.toArray(new Object[data.size()]);
47  }
48 
49  @Override
50  public Object getElementAt(int index) {
51  if(data == null || data.length <= index) return null;
52  return data[index];
53  }
54 
55  @Override
56  public int getSize() {
57  return data.length;
58  }
59 
60  @Override
61  public void sort(Comparator cmpr, boolean ascending) {
62  Arrays.sort(data, cmpr);
63  fireEvent(ListDataEvent.CONTENTS_CHANGED, -1, -1);
64  }
65 
66  @Override
67  public String getSortDirection(Comparator<String> cmpr) {
68  return "natural";
69  }
70 
71  @Override
72  public ListModel getSubModel(Object value, int nRows) {
73  if(nRows < 1) nRows = 10;
74  return new TagNameComboModel(Tags.search("contact", value.toString(), nRows)
75  .stream().map(t -> t.getTagName()).collect(Collectors.toList()));
76  }
77 
78 }
ListModel getSubModel(Object value, int nRows)
void sort(Comparator cmpr, boolean ascending)
String getSortDirection(Comparator< String > cmpr)
static TagSet search(String root, String value, int max)
Definition: Tags.java:47