OSDN Git Service

Change the editor button bar from a QHBoxLayout to a QToolBar. This will allow the...
[neighbornote/NeighborNote.git] / src / cx / fbn / nevernote / dialog / EnCryptDialog.java
1 /*\r
2  * This file is part of NeverNote \r
3  * Copyright 2009 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 import com.trolltech.qt.gui.QDialog;\r
24 import com.trolltech.qt.gui.QGridLayout;\r
25 import com.trolltech.qt.gui.QLabel;\r
26 import com.trolltech.qt.gui.QLineEdit;\r
27 import com.trolltech.qt.gui.QPushButton;\r
28 \r
29 public class EnCryptDialog extends QDialog {\r
30 \r
31         private boolean         okPressed;\r
32         private final QLineEdit password;\r
33         private final QLineEdit password2;\r
34         private final QLineEdit hint;\r
35         private final QPushButton ok;\r
36         private final QLabel error;\r
37         \r
38         \r
39         // Constructor\r
40         public EnCryptDialog() {\r
41                 okPressed = false;\r
42                 setWindowTitle(tr("Encrypt Text"));\r
43                 QGridLayout grid = new QGridLayout();\r
44                 QGridLayout input = new QGridLayout();\r
45                 QGridLayout msgGrid = new QGridLayout();\r
46                 QGridLayout button = new QGridLayout();\r
47                 setLayout(grid);\r
48                 \r
49                 \r
50                 hint = new QLineEdit("");\r
51                 password = new QLineEdit("");\r
52                 password.setEchoMode(QLineEdit.EchoMode.Password);\r
53                 password2 = new QLineEdit("");\r
54                 password2.setEchoMode(QLineEdit.EchoMode.Password);\r
55                 \r
56                 \r
57                 input.addWidget(new QLabel(tr("Password")), 1,1);\r
58                 input.addWidget(password, 1, 2);\r
59                 input.addWidget(new QLabel(tr("Verify")), 2,1);\r
60                 input.addWidget(password2, 2, 2);\r
61                 input.addWidget(new QLabel(tr("Hint")), 3,1);\r
62                 input.addWidget(hint, 3, 2);\r
63                 input.setContentsMargins(10, 10,  -10, -10);\r
64                 grid.addLayout(input, 1,1);\r
65                 \r
66                 error = new QLabel();\r
67                 msgGrid.addWidget(error, 1, 1);\r
68                 grid.addLayout(msgGrid, 2, 1);\r
69                 \r
70                 ok = new QPushButton(tr("OK"));\r
71                 ok.clicked.connect(this, "okButtonPressed()");\r
72                 ok.setEnabled(false);\r
73                 \r
74                 QPushButton cancel = new QPushButton(tr("Cancel"));\r
75                 cancel.clicked.connect(this, "cancelButtonPressed()");\r
76                 button.addWidget(ok, 1, 1);\r
77                 button.addWidget(cancel, 1,2);\r
78                 grid.addLayout(button, 3, 1);\r
79                 \r
80                 password.textChanged.connect(this, "validateInput()");\r
81                 password2.textChanged.connect(this, "validateInput()");\r
82                 hint.textChanged.connect(this, "validateInput()");\r
83                 \r
84         }\r
85         \r
86         // The OK button was pressed\r
87         @SuppressWarnings("unused")\r
88         private void okButtonPressed() {\r
89                 okPressed = true;\r
90                 close();\r
91         }\r
92         // The CANCEL button was pressed\r
93         @SuppressWarnings("unused")\r
94         private void cancelButtonPressed() {\r
95                 okPressed = false;\r
96                 close();\r
97         }\r
98         // Get the the validated password from the field\r
99         public String getPasswordVerify() {\r
100                 return password2.text();\r
101         }\r
102         // Get the password \r
103         public String getPassword() {\r
104                 return password.text();\r
105         }\r
106         // Get the password hint\r
107         public String getHint() {\r
108                         return hint.text();\r
109         }\r
110         // Check if the OK button was pressed\r
111         public boolean okPressed() {\r
112                 return okPressed;\r
113         }\r
114         // Check if proper input was input\r
115         @SuppressWarnings("unused")\r
116         private void validateInput() {\r
117                 ok.setEnabled(false);\r
118                 error.setText("");\r
119                 if (password.text().length()<4) {\r
120                         error.setText(tr("Password must be at least 4 characters"));\r
121                         return;\r
122                 }\r
123                 if (!password.text().equals(password2.text())) {\r
124                         error.setText(tr("Passwords do not match"));\r
125                         return;\r
126                 }\r
127                 if (hint.text().trim().equals("")) {\r
128                         error.setText(tr("Hint must be entered"));\r
129                         return;\r
130                 }\r
131                 \r
132                 ok.setEnabled(true);\r
133         }\r
134 }\r