OSDN Git Service

Merge branch 'master' of ssh://nevernote.git.sourceforge.net/gitroot/nevernote/nevern...
[neighbornote/NeighborNote.git] / src / cx / fbn / nevernote / dialog / NoteQuickLinkDialog.java
1 /*\r
2  * This file is part of NixNote \r
3  * Copyright 2011 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.dialog;\r
21 \r
22 //**********************************************\r
23 //**********************************************\r
24 //* This is the dialog that shows a user\r
25 //* a quick popup of a note based upon its title.\r
26 //* It is used in the Quick Link function.\r
27 //**********************************************\r
28 //**********************************************\r
29 \r
30 import java.util.List;\r
31 \r
32 import com.evernote.edam.type.Note;\r
33 import com.trolltech.qt.core.QByteArray;\r
34 import com.trolltech.qt.core.QTemporaryFile;\r
35 import com.trolltech.qt.core.Qt.ContextMenuPolicy;\r
36 import com.trolltech.qt.gui.QComboBox;\r
37 import com.trolltech.qt.gui.QDialog;\r
38 import com.trolltech.qt.gui.QHBoxLayout;\r
39 import com.trolltech.qt.gui.QIcon;\r
40 import com.trolltech.qt.gui.QLabel;\r
41 import com.trolltech.qt.gui.QPushButton;\r
42 import com.trolltech.qt.gui.QVBoxLayout;\r
43 \r
44 import cx.fbn.nevernote.gui.BrowserWindow;\r
45 import cx.fbn.nevernote.sql.DatabaseConnection;\r
46 import cx.fbn.nevernote.utilities.ApplicationLogger;\r
47 import cx.fbn.nevernote.utilities.Pair;\r
48 import cx.fbn.nevernote.xml.NoteFormatter;\r
49 \r
50 public class NoteQuickLinkDialog extends QDialog {\r
51         public final QPushButton        ok;\r
52         public final QPushButton        cancel;\r
53         private final DatabaseConnection  conn;\r
54         public final QComboBox          titleCombo;      \r
55         private final BrowserWindow     browser;\r
56         private final ApplicationLogger logger;\r
57         List<Pair<String,String>> results;\r
58         public boolean okPressed;\r
59         private List<QTemporaryFile> tempFiles;\r
60         private final String iconPath = new String("classpath:cx/fbn/nevernote/icons/");\r
61         \r
62         // Constructor\r
63         public NoteQuickLinkDialog(ApplicationLogger l, DatabaseConnection c, String text) {\r
64                 okPressed = false;\r
65                 setWindowTitle(tr("Quick Link Notes"));\r
66                 setWindowIcon(new QIcon(iconPath+"notebook-green.png"));\r
67                 QVBoxLayout main = new QVBoxLayout();\r
68                 setLayout(main);\r
69                 titleCombo = new QComboBox(this);\r
70                 \r
71                 QHBoxLayout comboLayout = new QHBoxLayout();\r
72                 comboLayout.addWidget(new QLabel(tr("Matching Notes:")));\r
73                 comboLayout.addWidget(titleCombo);\r
74                 comboLayout.addStretch(100);\r
75                 \r
76                 main.addLayout(comboLayout);\r
77                                 \r
78                 conn = c;\r
79                 browser = new BrowserWindow(conn);\r
80                 main.addWidget(browser);\r
81                 browser.titleLabel.setVisible(false);\r
82                 browser.notebookBox.setVisible(false);\r
83                 browser.hideButtons();\r
84                 browser.tagEdit.setVisible(false);\r
85                 browser.tagLabel.setVisible(false);\r
86                 \r
87                 QHBoxLayout buttonLayout = new QHBoxLayout();\r
88                 buttonLayout.addStretch(100);\r
89                 ok = new QPushButton(tr("OK"));\r
90                 ok.clicked.connect(this, "okPressed()");\r
91                 \r
92                 cancel = new QPushButton(tr("Cancel"));\r
93                 cancel.clicked.connect(this, "cancelPressed()");\r
94                 \r
95                 buttonLayout.addWidget(ok);\r
96                 buttonLayout.addWidget(cancel);\r
97                 main.addLayout(buttonLayout);\r
98                 \r
99                 browser.getBrowser().setContextMenuPolicy(ContextMenuPolicy.NoContextMenu);\r
100                 logger = l;\r
101                 \r
102                 // Search for matching notes\r
103                 results = conn.getNoteTable().findNotesByTitle(text);\r
104                 \r
105                 // Add the results to the combo box\r
106                 for (int i=0; i<results.size(); i++) {\r
107                         titleCombo.addItem(results.get(i).getSecond(), results.get(i).getFirst());\r
108                 }\r
109                 titleCombo.activated.connect(this, "selectionChanged(String)");\r
110                 \r
111                 // Load the results into the combo box\r
112                 if (results.size() > 0) {\r
113                         Note currentNote = conn.getNoteTable().getNote(results.get(0).getFirst(), true, true, false, true, true);\r
114                         setContent(currentNote);\r
115                 }\r
116         }\r
117 \r
118         // Cancel button pressed\r
119         @SuppressWarnings("unused")\r
120         private void cancelPressed() {\r
121                 this.close();\r
122         }\r
123         \r
124         // OK button pressed\r
125         @SuppressWarnings("unused")\r
126         private void okPressed() {\r
127                 okPressed = true;\r
128                 close();\r
129         }\r
130 \r
131         // When the selection changes, we refresh the browser window with the new content\r
132         @SuppressWarnings("unused")\r
133         private void selectionChanged(String text) {\r
134                 int pos = titleCombo.currentIndex();\r
135                 String guid = results.get(pos).getFirst();\r
136                 Note note = conn.getNoteTable().getNote(guid, true, true, false, true, true);\r
137                 setContent(note);\r
138         }\r
139         \r
140         // Return the note the user is currently viewing\r
141         public String getSelectedNote() {\r
142                 int pos = titleCombo.currentIndex();\r
143                 return results.get(pos).getFirst();\r
144         }\r
145         \r
146         \r
147         // Load the content of the note into the viewing window.\r
148         public void setContent(Note currentNote) {      \r
149                 NoteFormatter formatter = new NoteFormatter(logger, conn, tempFiles);\r
150                 formatter.setNote(currentNote, false);\r
151                 formatter.setHighlight(null);\r
152                 formatter.setNoteHistory(true);\r
153                 \r
154                 StringBuffer js = new StringBuffer();\r
155                 \r
156                 // We need to prepend the note with <HEAD></HEAD> or encoded characters are ugly \r
157                 js.append("<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">");       \r
158                 js.append("<style type=\"text/css\">en-crypt-temp { border-style:solid; border-color:blue; padding:1mm 1mm 1mm 1mm; }</style>");\r
159                 js.append("</head>");\r
160                 js.append(formatter.rebuildNoteHTML());\r
161                 js.append("</HTML>");\r
162                 \r
163                 browser.setNote(currentNote);\r
164                 browser.setContent(new QByteArray(js.toString()));\r
165         }\r
166         \r
167         // give the results from the DB search back to the caller.\r
168         public List<Pair<String,String>> getResults() {\r
169                 return results;\r
170         }\r
171         \r
172         \r
173 }\r
174  \r
175 \r
176 \r
177         \r
178         \r
179         \r
180         \r
181 \r