OSDN Git Service

Add the ability to add & remove notebook stacks.
[neighbornote/NeighborNote.git] / src / cx / fbn / nevernote / dialog / NotebookEdit.java
1 /*
2  * This file is part of NeverNote 
3  * Copyright 2009 Randy Baumgarte
4  * 
5  * This file may be licensed under the terms of of the
6  * GNU General Public License Version 2 (the ``GPL'').
7  *
8  * Software distributed under the License is distributed
9  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
10  * express or implied. See the GPL for the specific language
11  * governing rights and limitations.
12  *
13  * You should have received a copy of the GPL along with this
14  * program. If not, go to http://www.gnu.org/licenses/gpl.html
15  * or write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18 */
19
20 package cx.fbn.nevernote.dialog;
21
22 import java.util.List;
23
24 import com.evernote.edam.type.Notebook;
25 import com.trolltech.qt.gui.QCheckBox;
26 import com.trolltech.qt.gui.QDialog;
27 import com.trolltech.qt.gui.QGridLayout;
28 import com.trolltech.qt.gui.QIcon;
29 import com.trolltech.qt.gui.QLabel;
30 import com.trolltech.qt.gui.QLineEdit;
31 import com.trolltech.qt.gui.QPushButton;
32
33 public class NotebookEdit extends QDialog {
34         private boolean                 okPressed;
35         private final QLineEdit         notebook;
36         private final QCheckBox         localRemote;
37         private final QPushButton               ok;
38         private List<Notebook>  currentNotebooks;
39         private final QCheckBox         isDefault;
40         private boolean startDefault;
41         private String startText;
42         private List<String> stacks;
43         private boolean stackEdit;
44         private final QLabel notebookLabel;
45         private final String iconPath = new String("classpath:cx/fbn/nevernote/icons/");        
46         
47         // Constructor
48         public NotebookEdit() {
49                 okPressed = false;
50                 stackEdit = false;
51                 setWindowTitle(tr("Add Notebook"));
52                 setWindowIcon(new QIcon(iconPath+"notebook-green.png"));
53                 QGridLayout grid = new QGridLayout();
54                 setLayout(grid);
55                 
56                 QGridLayout textLayout = new QGridLayout();
57                 notebook = new QLineEdit();
58                 notebookLabel = new QLabel(tr("Notebook Name"));
59                 textLayout.addWidget(notebookLabel, 1,1);
60                 textLayout.addWidget(notebook, 1, 2);
61                 textLayout.setContentsMargins(10, 10,-10, -10);
62                 grid.addLayout(textLayout,1,1);
63                 
64                 localRemote = new QCheckBox();
65                 localRemote.setText(tr("Local Notebook"));
66                 localRemote.setChecked(false);
67                 grid.addWidget(localRemote, 2,1);
68
69                 isDefault = new QCheckBox();
70                 isDefault.setText(tr("Default Notebook"));
71                 isDefault.setChecked(false);
72                 isDefault.toggled.connect(this, "defaultNotebookChecked(Boolean)");
73                 grid.addWidget(isDefault, 3,1);
74
75                 QGridLayout buttonLayout = new QGridLayout();
76                 ok = new QPushButton(tr("OK"));
77                 ok.clicked.connect(this, "okButtonPressed()");
78                 ok.setEnabled(false);
79                 QPushButton cancel = new QPushButton(tr("Cancel"));
80                 cancel.clicked.connect(this, "cancelButtonPressed()");
81                 notebook.textChanged.connect(this, "textChanged()");
82                 buttonLayout.addWidget(ok, 1, 1);
83                 buttonLayout.addWidget(cancel, 1,2);
84                 grid.addLayout(buttonLayout,4,1);
85         }
86         
87         // The OK button was pressed
88         @SuppressWarnings("unused")
89         private void okButtonPressed() {
90                 okPressed = true;
91                 close();
92         }
93         
94         // The CANCEL button was pressed
95         @SuppressWarnings("unused")
96         private void cancelButtonPressed() {
97                 okPressed = false;
98                 close();
99         }
100         
101         // Get the userid from the field
102         public String getNotebook() {
103                 return notebook.text();
104         }
105         
106         // Set the stack names
107         public void setStacks(List<String> s) {
108                 stacks = s;
109                 stackEdit = true;
110                 notebookLabel.setText(new String(tr("Stack Name")));
111         }
112         
113         // Set the notebook name
114         public void setNotebook(String name) {
115                 if (name.equalsIgnoreCase("All Notebooks")) {
116                         notebook.setEnabled(false);
117                         localRemote.setEnabled(false);
118                         isDefault.setEnabled(false);
119                 }
120                 notebook.setText(name);
121                 startText = name;
122         }
123         
124         // Is this a local notebook?
125         public boolean isLocal() {
126                 return localRemote.isChecked();
127         }
128         
129         // Hide the local/remote checkbox
130         public void setLocalCheckboxEnabled(boolean visible) {
131                 localRemote.setEnabled(visible);
132         }
133         
134         
135         // Check if the OK button was pressed
136         public boolean okPressed() {
137                 return okPressed;
138         }
139         
140         // Set the window title
141         public void setTitle(String s) {
142                 setWindowTitle(s);
143         }
144         
145         // set notebooks 
146         public void setNotebooks(List<Notebook> n) {
147                 currentNotebooks = n;
148         }
149         
150         // Get default notebook
151         public void setDefaultNotebook(boolean val) {
152                 startDefault = val;
153                 isDefault.setChecked(val);
154                 if (val) 
155                         isDefault.setEnabled(true);
156         }
157         public boolean isDefaultNotebook() {
158                 return isDefault.isChecked();
159         }
160         
161         // Action when the default notebook icon is checked
162         private void defaultNotebookChecked(Boolean val) {
163                 if (val != startDefault || !startText.equals(notebook.text())) 
164                         ok.setEnabled(true);
165                 else
166                         ok.setEnabled(false);
167         }
168         
169         // Hide checkboxes
170         public void hideDefaultCheckbox() {
171                 isDefault.setVisible(false);
172         }
173         public void hideLocalCheckbox() {
174                 localRemote.setVisible(false);
175         }
176         
177         // Watch what text is being entered
178         @SuppressWarnings("unused")
179         private void textChanged() {
180                 if (notebook.text().equals("")) {
181                         ok.setEnabled(false);
182                         return;
183                 }
184                 if (notebook.text().equalsIgnoreCase("All Notebooks")) {
185                         ok.setEnabled(false);
186                         return;
187                 }
188                 if (stackEdit) {
189                         for (int i=0; i<stacks.size(); i++) {
190                                 if (stacks.get(i).equalsIgnoreCase(notebook.text())) {
191                                         ok.setEnabled(false);
192                                         return;
193                                 }
194                         }
195                 }
196                 if (!stackEdit) {
197                         if (currentNotebooks == null) {
198                                 ok.setEnabled(false);
199                                 return;
200                         }
201                         for (int i=0; i<currentNotebooks.size(); i++) {
202                                 String s = currentNotebooks.get(i).getName();
203                                 if (s.equalsIgnoreCase(notebook.text())) {
204                                         ok.setEnabled(false);
205                                         return;
206                                 }
207                         }
208                 }
209                 ok.setEnabled(true);
210         }
211 }