OSDN Git Service

Add notebook specific sorting and alter some NeverNote labels to say NixNote.
[neighbornote/NeighborNote.git] / src / cx / fbn / nevernote / dialog / NotebookEdit.java
1 /*
2  * This file is part of NixNote 
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 //**********************************************
23 //**********************************************
24 //* Chaneg or create a notebook
25 //**********************************************
26 //**********************************************
27
28 import java.util.List;
29
30 import com.evernote.edam.type.Notebook;
31 import com.trolltech.qt.gui.QCheckBox;
32 import com.trolltech.qt.gui.QDialog;
33 import com.trolltech.qt.gui.QGridLayout;
34 import com.trolltech.qt.gui.QIcon;
35 import com.trolltech.qt.gui.QLabel;
36 import com.trolltech.qt.gui.QLineEdit;
37 import com.trolltech.qt.gui.QPushButton;
38
39 public class NotebookEdit extends QDialog {
40         private boolean                 okPressed;
41         private final QLineEdit         notebook;
42         private final QCheckBox         localRemote;
43         private final QPushButton               ok;
44         private List<Notebook>  currentNotebooks;
45         private final QCheckBox         isDefault;
46         private boolean startDefault;
47         private String startText;
48         private List<String> stacks;
49         private boolean stackEdit;
50         private final QLabel notebookLabel;
51         private final String iconPath = new String("classpath:cx/fbn/nevernote/icons/");        
52         
53         // Constructor
54         public NotebookEdit() {
55                 okPressed = false;
56                 stackEdit = false;
57                 setWindowTitle(tr("Add Notebook"));
58                 setWindowIcon(new QIcon(iconPath+"notebook-green.png"));
59                 QGridLayout grid = new QGridLayout();
60                 setLayout(grid);
61                 
62                 QGridLayout textLayout = new QGridLayout();
63                 notebook = new QLineEdit();
64                 notebookLabel = new QLabel(tr("Notebook Name"));
65                 textLayout.addWidget(notebookLabel, 1,1);
66                 textLayout.addWidget(notebook, 1, 2);
67                 textLayout.setContentsMargins(10, 10,-10, -10);
68                 grid.addLayout(textLayout,1,1);
69                 
70                 localRemote = new QCheckBox();
71                 localRemote.setText(tr("Local Notebook"));
72                 localRemote.setChecked(false);
73                 grid.addWidget(localRemote, 2,1);
74
75                 isDefault = new QCheckBox();
76                 isDefault.setText(tr("Default Notebook"));
77                 isDefault.setChecked(false);
78                 isDefault.toggled.connect(this, "defaultNotebookChecked(Boolean)");
79                 grid.addWidget(isDefault, 3,1);
80
81                 QGridLayout buttonLayout = new QGridLayout();
82                 ok = new QPushButton(tr("OK"));
83                 ok.clicked.connect(this, "okButtonPressed()");
84                 ok.setEnabled(false);
85                 QPushButton cancel = new QPushButton(tr("Cancel"));
86                 cancel.clicked.connect(this, "cancelButtonPressed()");
87                 notebook.textChanged.connect(this, "textChanged()");
88                 buttonLayout.addWidget(ok, 1, 1);
89                 buttonLayout.addWidget(cancel, 1,2);
90                 grid.addLayout(buttonLayout,4,1);
91         }
92         
93         // The OK button was pressed
94         @SuppressWarnings("unused")
95         private void okButtonPressed() {
96                 okPressed = true;
97                 close();
98         }
99         
100         // The CANCEL button was pressed
101         @SuppressWarnings("unused")
102         private void cancelButtonPressed() {
103                 okPressed = false;
104                 close();
105         }
106         
107         // Get the userid from the field
108         public String getNotebook() {
109                 return notebook.text();
110         }
111         
112         // Set the stack names
113         public void setStacks(List<String> s) {
114                 stacks = s;
115                 stackEdit = true;
116                 notebookLabel.setText(new String(tr("Stack Name")));
117         }
118         
119         // Set the notebook name
120         public void setNotebook(String name) {
121                 if (name.equalsIgnoreCase("All Notebooks")) {
122                         notebook.setEnabled(false);
123                         localRemote.setEnabled(false);
124                         isDefault.setEnabled(false);
125                 }
126                 notebook.setText(name);
127                 startText = name;
128         }
129         
130         // Is this a local notebook?
131         public boolean isLocal() {
132                 return localRemote.isChecked();
133         }
134         
135         // Hide the local/remote checkbox
136         public void setLocalCheckboxEnabled(boolean visible) {
137                 localRemote.setEnabled(visible);
138         }
139         
140         
141         // Check if the OK button was pressed
142         public boolean okPressed() {
143                 return okPressed;
144         }
145         
146         // Set the window title
147         public void setTitle(String s) {
148                 setWindowTitle(s);
149         }
150         
151         // set notebooks 
152         public void setNotebooks(List<Notebook> n) {
153                 currentNotebooks = n;
154         }
155         
156         // Get default notebook
157         public void setDefaultNotebook(boolean val) {
158                 startDefault = val;
159                 isDefault.setChecked(val);
160                 if (val) 
161                         isDefault.setEnabled(true);
162         }
163         public boolean isDefaultNotebook() {
164                 return isDefault.isChecked();
165         }
166         
167         // Action when the default notebook icon is checked
168         @SuppressWarnings("unused")
169         private void defaultNotebookChecked(Boolean val) {
170                 if (val != startDefault || !startText.equals(notebook.text())) 
171                         ok.setEnabled(true);
172                 else
173                         ok.setEnabled(false);
174         }
175         
176         // Hide checkboxes
177         public void hideDefaultCheckbox() {
178                 isDefault.setVisible(false);
179         }
180         public void hideLocalCheckbox() {
181                 localRemote.setVisible(false);
182         }
183         
184         // Watch what text is being entered
185         @SuppressWarnings("unused")
186         private void textChanged() {
187                 if (notebook.text().equals("")) {
188                         ok.setEnabled(false);
189                         return;
190                 }
191                 if (notebook.text().equalsIgnoreCase("All Notebooks")) {
192                         ok.setEnabled(false);
193                         return;
194                 }
195                 if (stackEdit) {
196                         for (int i=0; i<stacks.size(); i++) {
197                                 if (stacks.get(i).equalsIgnoreCase(notebook.text())) {
198                                         ok.setEnabled(false);
199                                         return;
200                                 }
201                         }
202                 }
203                 if (!stackEdit) {
204                         if (currentNotebooks == null) {
205                                 ok.setEnabled(false);
206                                 return;
207                         }
208                         for (int i=0; i<currentNotebooks.size(); i++) {
209                                 String s = currentNotebooks.get(i).getName();
210                                 if (s.equalsIgnoreCase(notebook.text())) {
211                                         ok.setEnabled(false);
212                                         return;
213                                 }
214                         }
215                 }
216                 ok.setEnabled(true);
217         }
218 }