OSDN Git Service

Correct problem parsing notes where carriage returns did not separate some XML lines...
[neighbornote/NeighborNote.git] / src / cx / fbn / nevernote / gui / TagLineEdit.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 \r
20 package cx.fbn.nevernote.gui;\r
21 \r
22 import java.util.ArrayList;\r
23 import java.util.List;\r
24 \r
25 import com.evernote.edam.type.Tag;\r
26 import com.trolltech.qt.core.QEvent;\r
27 import com.trolltech.qt.core.QModelIndex;\r
28 import com.trolltech.qt.gui.QLineEdit;\r
29 \r
30 import cx.fbn.nevernote.Global;\r
31 \r
32 public class TagLineEdit extends QLineEdit {\r
33         private String text;\r
34         private final boolean changed;\r
35         public Signal2<List<String>, String> text_changed = new Signal2<List<String>, String>();\r
36         public TagLineCompleter tagCompleter;\r
37         public Signal0 focusLost = new Signal0();\r
38         public String currentCompleterSelection;\r
39         \r
40         public TagLineEdit(List<Tag> allTags) {\r
41                 textChanged.connect(this, "textChanged(String)");\r
42                 tagCompleter = new TagLineCompleter(this);\r
43                 text_changed.connect(tagCompleter, "update(List, String)");\r
44                 tagCompleter.activated.connect(this, "completeText(String)");\r
45                 tagCompleter.highlightedIndex.connect(this, "completerSelected(QModelIndex)");\r
46                 changed = false;\r
47                 currentCompleterSelection = null;\r
48         }\r
49         \r
50         public boolean hasChanged() {\r
51                 return changed;\r
52         }\r
53         \r
54         @SuppressWarnings("unused")\r
55         private void textChanged(String t) {\r
56                 text = t;\r
57                 int cursor_pos = cursorPosition();\r
58                 String temp_text = text.substring(0,cursor_pos);\r
59                 int delimeter = temp_text.trim().lastIndexOf(Global.tagDelimeter);\r
60                 String prefix = "";\r
61                 if (delimeter > 0)\r
62                    prefix = temp_text.substring(delimeter+1).trim();\r
63                 else\r
64                         prefix = temp_text.trim();\r
65                 \r
66                 List<String> newTags = new ArrayList<String>();\r
67                 List<String> currentTags = new ArrayList<String>();\r
68                 String list[] = text.split(Global.tagDelimeter);\r
69                 for (String element : list) {\r
70                         currentTags.add(element.trim());\r
71                 }\r
72         \r
73                 text_changed.emit(currentTags, prefix.trim());\r
74 \r
75                 \r
76         }\r
77         \r
78         // This method is used to store the current selection from the completer.  It is\r
79         // saved until later in case focus is lost while one is selected, but Enter is never pressed.\r
80         @SuppressWarnings("unused")\r
81         private void completerSelected(QModelIndex model) {\r
82                 currentCompleterSelection = null;\r
83                 if (model == null)\r
84                         return;\r
85                 tagCompleter.setCurrentRow(model.row());\r
86                 currentCompleterSelection = tagCompleter.currentCompletion();\r
87                 tagCompleter.setCurrentRow(model.row());\r
88         }\r
89         \r
90         public void  completeText(String text){\r
91                 int cursor_pos = cursorPosition();\r
92                 String before_text = text().substring(0,cursor_pos);\r
93                 String after_text = text().substring(cursor_pos);\r
94                 int prefix_len = before_text.lastIndexOf(Global.tagDelimeter);\r
95                 if (prefix_len == -1) {\r
96                         prefix_len = cursor_pos;\r
97                         before_text = "";\r
98                 } else {\r
99                         before_text = before_text.substring(0,cursor_pos-1);\r
100                 }\r
101                 int nextTagPos = after_text.indexOf(Global.tagDelimeter);\r
102                 if (nextTagPos == -1) {\r
103                         nextTagPos = 0;\r
104                         after_text = "";\r
105                 }\r
106                 setText(before_text +text +Global.tagDelimeter +" " +after_text);\r
107                 setCursorPosition(cursor_pos - prefix_len + text().length() +2);\r
108         }\r
109 \r
110         public void setTagList(List<Tag> t) {\r
111                 tagCompleter.resetList();\r
112                 tagCompleter.setTagList(t);\r
113         }\r
114         \r
115         @Override\r
116         public boolean event(QEvent e) {\r
117                 if (e.type().equals(QEvent.Type.FocusOut)) {\r
118                         focusLost.emit();\r
119                 }\r
120                 return super.event(e);\r
121         }\r
122 }\r