OSDN Git Service

STAR→Add Star, UNSTAR→Remove Star
[neighbornote/NeighborNote.git] / src / cx / fbn / nevernote / gui / TagLineCompleter.java
1 /*\r
2  * This file is part of NixNote/NeighborNote \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         private String                          currentText;\r
35         \r
36         public TagLineCompleter(TagLineEdit e) {\r
37                 editor = e;\r
38                 setWidget(editor);\r
39 //              editor.setCompleter(this);\r
40                 currentTags = new ArrayList<String>();\r
41                 setTagList(new ArrayList<Tag>());\r
42                 setCaseSensitivity(CaseSensitivity.CaseInsensitive);\r
43         }\r
44         \r
45         public List<String> getTagList(){\r
46                 return currentTags;\r
47         }\r
48         \r
49         public void update(List<String> t, String prefix) {\r
50                 currentTags = t;\r
51  \r
52                 buildModelList();\r
53                                 \r
54                 setCompletionPrefix(prefix);\r
55                 if (!prefix.trim().equals(""))\r
56                         complete();\r
57         }\r
58         \r
59         public List<Tag> getTags() {\r
60                 return tags;\r
61         }\r
62         \r
63         private List<String> buildModelList() {\r
64                 \r
65                 model = (QStringListModel) model();\r
66                 if (model == null) {\r
67                         model = new QStringListModel();\r
68                         setModel(model);\r
69                 }\r
70                 for (int i=0; i<model.rowCount(); i++)\r
71                         model.removeRow(i);\r
72                 \r
73                 List<String> newTagList = new ArrayList<String>();\r
74 \r
75                 for (int i=0; i<tags.size(); i++) {\r
76                         boolean found=false;\r
77                         for (int j=0; j<currentTags.size() && !found; j++) {\r
78                                 if (currentTags.get(j).trim().equalsIgnoreCase(tags.get(i).getName())) {\r
79                                         found = true;\r
80                                 }\r
81                         }\r
82                         if (!found) \r
83                                 newTagList.add(tags.get(i).getName());\r
84                 }\r
85                 \r
86                 model.setStringList(newTagList);\r
87                 return newTagList;\r
88         }\r
89         \r
90         public void setTagList(List<Tag> t) {\r
91                 tags = t;\r
92                 resetList();\r
93                 buildModelList();\r
94 //              model = new QStringListModel(buildModelList(), this);\r
95 //              setModel(model);\r
96         }\r
97         \r
98         public void resetList() {\r
99                 currentTags.clear();\r
100         }\r
101         \r
102 \r
103         public String currentText() {\r
104                 return currentText;\r
105         }\r
106         \r
107         public void reset() {\r
108                 currentText = "";\r
109         }\r
110 \r
111 }\r