OSDN Git Service

Show User Name
[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 final String iconPath = new String("classpath:cx/fbn/nevernote/icons/");        
43         
44         // Constructor
45         public NotebookEdit() {
46                 okPressed = false;
47                 setWindowTitle(tr("Add Notebook"));
48                 setWindowIcon(new QIcon(iconPath+"notebook-green.png"));
49                 QGridLayout grid = new QGridLayout();
50                 setLayout(grid);
51                 
52                 QGridLayout textLayout = new QGridLayout();
53                 notebook = new QLineEdit();
54                 textLayout.addWidget(new QLabel(tr("Notebook Name")), 1,1);
55                 textLayout.addWidget(notebook, 1, 2);
56                 textLayout.setContentsMargins(10, 10,-10, -10);
57                 grid.addLayout(textLayout,1,1);
58                 
59                 localRemote = new QCheckBox();
60                 localRemote.setText(tr("Local Notebook"));
61                 localRemote.setChecked(false);
62                 grid.addWidget(localRemote, 2,1);
63
64                 isDefault = new QCheckBox();
65                 isDefault.setText(tr("Default Notebook"));
66                 isDefault.setChecked(false);
67                 isDefault.toggled.connect(this, "defaultNotebookChecked(Boolean)");
68                 grid.addWidget(isDefault, 3,1);
69
70                 QGridLayout buttonLayout = new QGridLayout();
71                 ok = new QPushButton(tr("OK"));
72                 ok.clicked.connect(this, "okButtonPressed()");
73                 ok.setEnabled(false);
74                 QPushButton cancel = new QPushButton(tr("Cancel"));
75                 cancel.clicked.connect(this, "cancelButtonPressed()");
76                 notebook.textChanged.connect(this, "textChanged()");
77                 buttonLayout.addWidget(ok, 1, 1);
78                 buttonLayout.addWidget(cancel, 1,2);
79                 grid.addLayout(buttonLayout,4,1);
80         }
81         
82         // The OK button was pressed
83         @SuppressWarnings("unused")
84         private void okButtonPressed() {
85                 okPressed = true;
86                 close();
87         }
88         
89         // The CANCEL button was pressed
90         @SuppressWarnings("unused")
91         private void cancelButtonPressed() {
92                 okPressed = false;
93                 close();
94         }
95         
96         // Get the userid from the field
97         public String getNotebook() {
98                 return notebook.text();
99         }
100         
101         // Set the notebook name
102         public void setNotebook(String name) {
103                 if (name.equalsIgnoreCase("All Notebooks")) {
104                         notebook.setEnabled(false);
105                         localRemote.setEnabled(false);
106                         isDefault.setEnabled(false);
107                 }
108                 notebook.setText(name);
109                 startText = name;
110         }
111         
112         // Is this a local notebook?
113         public boolean isLocal() {
114                 return localRemote.isChecked();
115         }
116         
117         // Hide the local/remote checkbox
118         public void setLocalCheckboxEnabled(boolean visible) {
119                 localRemote.setEnabled(visible);
120         }
121         
122         
123         // Check if the OK button was pressed
124         public boolean okPressed() {
125                 return okPressed;
126         }
127         
128         // Set the window title
129         public void setTitle(String s) {
130                 setWindowTitle(s);
131         }
132         
133         // set notebooks 
134         public void setNotebooks(List<Notebook> n) {
135                 currentNotebooks = n;
136         }
137         
138         // Get default notebook
139         public void setDefaultNotebook(boolean val) {
140                 startDefault = val;
141                 isDefault.setChecked(val);
142                 if (val) 
143                         isDefault.setEnabled(true);
144         }
145         public boolean isDefaultNotebook() {
146                 return isDefault.isChecked();
147         }
148         
149         // Action when the default notebook icon is checked
150         private void defaultNotebookChecked(Boolean val) {
151                 if (val != startDefault || !startText.equals(notebook.text())) 
152                         ok.setEnabled(true);
153                 else
154                         ok.setEnabled(false);
155         }
156         
157         // Watch what text is being entered
158         @SuppressWarnings("unused")
159         private void textChanged() {
160                 if (notebook.text().equals("")) {
161                         ok.setEnabled(false);
162                         return;
163                 }
164                 if (notebook.text().equalsIgnoreCase("All Notebooks")) {
165                         ok.setEnabled(false);
166                         return;
167                 }
168                 if (currentNotebooks == null) {
169                         ok.setEnabled(false);
170                         return;
171                 }
172                 for (int i=0; i<currentNotebooks.size(); i++) {
173                         String s = currentNotebooks.get(i).getName();
174                         if (s.equalsIgnoreCase(notebook.text())) {
175                                 ok.setEnabled(false);
176                                 return;
177                         }
178                 }
179                 ok.setEnabled(true);
180         }
181 }