2 * This file is part of NeverNote
\r
3 * Copyright 2009 Randy Baumgarte
\r
5 * This file may be licensed under the terms of of the
\r
6 * GNU General Public License Version 2 (the ``GPL'').
\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
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
20 package cx.fbn.nevernote.dialog;
\r
22 //**********************************************
\r
23 //**********************************************
\r
24 //* Dialog box used to show watched folders
\r
25 //**********************************************
\r
26 //**********************************************
\r
28 import java.util.ArrayList;
\r
29 import java.util.List;
\r
31 import com.evernote.edam.type.Notebook;
\r
32 import com.trolltech.qt.core.QModelIndex;
\r
33 import com.trolltech.qt.core.Qt.ItemDataRole;
\r
34 import com.trolltech.qt.gui.QAbstractItemView.SelectionBehavior;
\r
35 import com.trolltech.qt.gui.QAbstractItemView.SelectionMode;
\r
36 import com.trolltech.qt.gui.QApplication;
\r
37 import com.trolltech.qt.gui.QDialog;
\r
38 import com.trolltech.qt.gui.QFontMetrics;
\r
39 import com.trolltech.qt.gui.QHBoxLayout;
\r
40 import com.trolltech.qt.gui.QIcon;
\r
41 import com.trolltech.qt.gui.QPushButton;
\r
42 import com.trolltech.qt.gui.QTableWidget;
\r
43 import com.trolltech.qt.gui.QTableWidgetItem;
\r
44 import com.trolltech.qt.gui.QVBoxLayout;
\r
46 import cx.fbn.nevernote.sql.WatchFolderRecord;
\r
48 public class WatchFolder extends QDialog {
\r
49 private final QPushButton okButton;
\r
50 private final QPushButton cancelButton;
\r
51 private final QPushButton addButton;
\r
52 private final QPushButton editButton;
\r
53 private final QPushButton deleteButton;
\r
54 private boolean okClicked;
\r
55 public final QTableWidget table;
\r
56 private final List<Notebook> notebooks;
\r
57 private final List<WatchFolderRecord> records;
\r
58 private final String iconPath = new String("classpath:cx/fbn/nevernote/icons/");
\r
60 public WatchFolder(List<WatchFolderRecord> w, List<Notebook> n) {
\r
61 setWindowIcon(new QIcon(iconPath+"folder.png"));
\r
66 okButton = new QPushButton();
\r
67 okButton.setText(tr("OK"));
\r
68 okButton.pressed.connect(this, "onClicked()");
\r
70 cancelButton = new QPushButton();
\r
71 cancelButton.setText(tr("Cancel"));
\r
72 cancelButton.pressed.connect(this, "onCancel()");
\r
74 QHBoxLayout horizontalLayout = new QHBoxLayout();
\r
75 QHBoxLayout buttonLayout = new QHBoxLayout();
\r
76 buttonLayout.addStretch(1);
\r
77 buttonLayout.addWidget(okButton);
\r
78 buttonLayout.addWidget(cancelButton);
\r
79 setWindowTitle(tr("Auto Import Folders"));
\r
81 table = new QTableWidget(records.size(),3);
\r
82 List<String> headers = new ArrayList<String>();
\r
83 headers.add(tr("Directory"));
\r
84 headers.add(tr("Target Notebook"));
\r
85 headers.add(tr("Keep"));
\r
86 table.setHorizontalHeaderLabels(headers);
\r
87 table.verticalHeader().setVisible(false);
\r
88 table.setAlternatingRowColors(true);
\r
89 table.setSelectionBehavior(SelectionBehavior.SelectRows);
\r
90 table.setSelectionMode(SelectionMode.SingleSelection);
\r
91 table.itemSelectionChanged.connect(this, "tableSelection()");
\r
92 horizontalLayout.addWidget(table);
\r
95 addButton = new QPushButton();
\r
96 addButton.setText(tr("Add"));
\r
97 addButton.clicked.connect(this, "addPressed()");
\r
99 editButton = new QPushButton();
\r
100 editButton.setText(tr("Edit"));
\r
101 editButton.setEnabled(false);
\r
102 editButton.clicked.connect(this, "editPressed()");
\r
104 deleteButton = new QPushButton();
\r
105 deleteButton.setText(tr("Delete"));
\r
106 deleteButton.setEnabled(false);
\r
107 deleteButton.clicked.connect(this, "deletePressed()");
\r
109 QVBoxLayout editLayout = new QVBoxLayout();
\r
110 editLayout.addWidget(addButton);
\r
111 editLayout.addWidget(editButton);
\r
112 editLayout.addWidget(deleteButton);
\r
114 QHBoxLayout listLayout = new QHBoxLayout();
\r
115 listLayout.addLayout(horizontalLayout);
\r
116 listLayout.addLayout(editLayout);
\r
118 QVBoxLayout mainLayout = new QVBoxLayout();
\r
119 mainLayout.addLayout(listLayout);
\r
120 mainLayout.addSpacing(1);
\r
121 mainLayout.addLayout(buttonLayout);
\r
122 setLayout(mainLayout);
\r
124 // QTableWidgetItem dir = new QTableWidgetItem();
\r
125 // QTableWidgetItem book = new QTableWidgetItem();
\r
127 table.setColumnWidth(0, 160);
\r
133 @SuppressWarnings("unused")
\r
134 private void onClicked() {
\r
139 @SuppressWarnings("unused")
\r
140 private void onCancel() {
\r
145 public boolean okClicked() {
\r
149 @SuppressWarnings("unused")
\r
150 private void itemSelected() {
\r
151 okButton.setEnabled(true);
\r
154 private void load() {
\r
155 for (int i=0; i<records.size(); i++) {
\r
156 addRow(i, records.get(i).folder, records.get(i).notebook, records.get(i).keep);
\r
160 private void addRow(int row, String folder, String notebook, boolean keepAfter) {
\r
161 QFontMetrics f = QApplication.fontMetrics();
\r
162 int fontHeight = f.height();
\r
164 QTableWidgetItem dir = new QTableWidgetItem();
\r
165 dir.setText(folder);
\r
166 table.setItem(row, 0, dir);
\r
167 table.setRowHeight(row, fontHeight);
\r
168 dir.setToolTip(folder);
\r
170 QTableWidgetItem book = new QTableWidgetItem();
\r
171 book.setText(notebook);
\r
172 table.setItem(row, 1, book);
\r
174 QTableWidgetItem keep = new QTableWidgetItem();
\r
176 keep.setText(tr("Keep"));
\r
177 keep.setData(ItemDataRole.UserRole, "Keep");
\r
179 keep.setText(tr("Delete"));
\r
180 keep.setData(ItemDataRole.UserRole, "Delete");
\r
182 table.setItem(row, 2, keep);
\r
186 @SuppressWarnings("unused")
\r
187 private void tableSelection() {
\r
188 editButton.setEnabled(true);
\r
189 deleteButton.setEnabled(true);
\r
193 @SuppressWarnings("unused")
\r
194 private void addPressed() {
\r
195 WatchFolderAdd dialog = new WatchFolderAdd(null, notebooks);
\r
197 if (dialog.okClicked()) {
\r
198 String dir = dialog.directory.text();
\r
199 String notebook = dialog.books.currentText();
\r
202 int index = dialog.keep.currentIndex();
\r
203 String value = (String) dialog.keep.itemData(index);
\r
204 if (value.equalsIgnoreCase("keep"))
\r
208 table.insertRow(table.rowCount());
\r
209 addRow(table.rowCount()-1, dir, notebook, keep);
\r
215 @SuppressWarnings("unused")
\r
216 private void editPressed() {
\r
217 WatchFolderRecord record = new WatchFolderRecord();
\r
218 QModelIndex index = table.currentIndex();
\r
219 int row = index.row();
\r
220 QTableWidgetItem item = table.item(row, 0);
\r
221 record.folder = item.text();
\r
222 item = table.item(row, 1);
\r
223 record.notebook = item.text();
\r
224 item = table.item(row,2);
\r
225 if (item.data(ItemDataRole.UserRole).toString().equalsIgnoreCase("keep"))
\r
226 record.keep = true;
\r
228 record.keep = false;
\r
230 WatchFolderAdd dialog = new WatchFolderAdd(record, notebooks);
\r
232 if (dialog.okClicked()) {
\r
233 String dir = dialog.directory.text();
\r
234 String notebook = dialog.books.currentText();
\r
237 if (dialog.keep.itemData(ItemDataRole.UserRole).toString().equalsIgnoreCase("keep"))
\r
241 table.removeRow(row);
\r
242 table.insertRow(table.rowCount());
\r
243 addRow(table.rowCount()-1, dir, notebook, keep);
\r
244 WatchFolderRecord newRecord = new WatchFolderRecord();
\r
245 newRecord.folder = dir;
\r
246 newRecord.notebook = notebook;
\r
247 newRecord.keep = keep;
\r
248 records.add(newRecord);
\r
254 @SuppressWarnings("unused")
\r
255 private void deletePressed() {
\r
256 QModelIndex index = table.currentIndex();
\r
257 int row = index.row();
\r
259 QTableWidgetItem dirWidget = table.item(row, 0);
\r
260 String value = dirWidget.text();
\r
261 table.removeRow(row);
\r
263 for (int i=0; i<records.size(); i++) {
\r
264 if (value.equals(records.get(i).folder)) {
\r
270 if (table.rowCount() == 0) {
\r
271 editButton.setEnabled(false);
\r
272 deleteButton.setEnabled(false);
\r