OSDN Git Service

28271e385626be9687914143d451b13b53bba91b
[neighbornote/NeighborNote.git] / src / cx / fbn / nevernote / gui / TagLineCompleter.java
1 /*\r
2  * This file is part of NeverNote \r
3  * Copyright 2009 Randy Baumgarte\r
4  * \r
5  * This file may be licensed under the terms of of the\r
6  * GNU General Public License Version 2 (the ``GPL'').\r
7  *\r
8  * Software distributed under the License is distributed\r
9  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either\r
10  * express or implied. See the GPL for the specific language\r
11  * governing rights and limitations.\r
12  *\r
13  * You should have received a copy of the GPL along with this\r
14  * program. If not, go to http://www.gnu.org/licenses/gpl.html\r
15  * or write to the Free Software Foundation, Inc.,\r
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.\r
17  *\r
18 */\r
19 package cx.fbn.nevernote.gui;\r
20 \r
21 import java.util.ArrayList;\r
22 import java.util.List;\r
23 \r
24 import com.evernote.edam.type.Tag;\r
25 import com.trolltech.qt.core.Qt.CaseSensitivity;\r
26 import com.trolltech.qt.gui.QCompleter;\r
27 import com.trolltech.qt.gui.QStringListModel;\r
28 \r
29 public class TagLineCompleter extends QCompleter {\r
30         private List<Tag>                       tags;\r
31         private List<String>            currentTags;\r
32         private QStringListModel        model;  \r
33         private final TagLineEdit                       editor;\r
34         \r
35         public TagLineCompleter(TagLineEdit e) {\r
36                 editor = e;\r
37                 setWidget(editor);\r
38 //              editor.setCompleter(this);\r
39                 currentTags = new ArrayList<String>();\r
40                 setTagList(new ArrayList<Tag>());\r
41                 setCaseSensitivity(CaseSensitivity.CaseInsensitive);\r
42         }\r
43         \r
44         public List<String> getTagList(){\r
45                 return currentTags;\r
46         }\r
47         \r
48         public void update(List<String> t, String prefix) {\r
49                 currentTags = t;\r
50  \r
51                 buildModelList();\r
52                                 \r
53                 setCompletionPrefix(prefix);\r
54                 if (!prefix.trim().equals(""))\r
55                         complete();\r
56         }\r
57         \r
58         public List<Tag> getTags() {\r
59                 return tags;\r
60         }\r
61         \r
62         private List<String> buildModelList() {\r
63                 \r
64                 model = (QStringListModel) model();\r
65                 if (model == null) {\r
66                         model = new QStringListModel();\r
67                         setModel(model);\r
68                 }\r
69                 for (int i=0; i<model.rowCount(); i++)\r
70                         model.removeRow(i);\r
71                 \r
72                 List<String> newTagList = new ArrayList<String>();\r
73 \r
74                 for (int i=0; i<tags.size(); i++) {\r
75                         boolean found=false;\r
76                         for (int j=0; j<currentTags.size() && !found; j++) {\r
77                                 if (currentTags.get(j).trim().equalsIgnoreCase(tags.get(i).getName())) {\r
78                                         found = true;\r
79                                 }\r
80                         }\r
81                         if (!found) \r
82                                 newTagList.add(tags.get(i).getName());\r
83                 }\r
84                 \r
85                 model.setStringList(newTagList);\r
86                 return newTagList;\r
87         }\r
88         \r
89         public void setTagList(List<Tag> t) {\r
90                 tags = t;\r
91                 resetList();\r
92                 buildModelList();\r
93 //              model = new QStringListModel(buildModelList(), this);\r
94 //              setModel(model);\r
95         }\r
96         \r
97         public void resetList() {\r
98                 currentTags.clear();\r
99         }\r
100 \r
101 \r
102 }\r