OSDN Git Service

Insert http:// in front of links if needed.
[neighbornote/NeighborNote.git] / src / cx / fbn / nevernote / dialog / InsertLinkDialog.java
1 /*\r
2  * This file is part of NixNote \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 //**********************************************\r
24 //* This is the dialog used when a user wants\r
25 //* to add a HTML link.\r
26 //**********************************************\r
27 //**********************************************\r
28 \r
29 import com.trolltech.qt.core.Qt;\r
30 import com.trolltech.qt.gui.QDialog;\r
31 import com.trolltech.qt.gui.QGridLayout;\r
32 import com.trolltech.qt.gui.QIcon;\r
33 import com.trolltech.qt.gui.QLabel;\r
34 import com.trolltech.qt.gui.QLineEdit;\r
35 import com.trolltech.qt.gui.QPushButton;\r
36 \r
37 public class InsertLinkDialog extends QDialog {\r
38 \r
39         private boolean         okPressed;\r
40         private final QLineEdit url;\r
41         private final QPushButton ok;\r
42         private String          urlText;\r
43         private final String iconPath = new String("classpath:cx/fbn/nevernote/icons/");\r
44         private final boolean insertHyperlink;\r
45         \r
46         \r
47         // Constructor\r
48         public InsertLinkDialog(boolean insert) {\r
49                 okPressed = false;\r
50                 setWindowTitle(tr("Insert Link"));\r
51                 setWindowIcon(new QIcon(iconPath+"link.png"));\r
52                 QGridLayout grid = new QGridLayout();\r
53                 QGridLayout input = new QGridLayout();\r
54                 QGridLayout button = new QGridLayout();\r
55                 setLayout(grid);\r
56                 insertHyperlink = insert;\r
57                 \r
58                 \r
59                 url = new QLineEdit("");\r
60                 \r
61                 input.addWidget(new QLabel(tr("Url")), 1,1);\r
62                 input.addWidget(url, 1, 2);\r
63                 input.setContentsMargins(10, 10,  -10, -10);\r
64                 grid.addLayout(input, 1,1);\r
65                         \r
66                 ok = new QPushButton(tr("OK"));\r
67                 ok.clicked.connect(this, "accept()");\r
68                 ok.setEnabled(false);\r
69                 \r
70                 QPushButton cancel = new QPushButton(tr("Cancel"));\r
71                 cancel.clicked.connect(this, "reject()");\r
72                 button.addWidget(ok, 1, 1);\r
73                 button.addWidget(cancel, 1,2);\r
74                 grid.addLayout(button, 3, 1);\r
75                 url.textChanged.connect(this, "validateInput()");\r
76                 \r
77                 setAttribute(Qt.WidgetAttribute.WA_DeleteOnClose);\r
78         }\r
79         \r
80         // Get the password \r
81         public String getUrl() {\r
82                 if (urlText.indexOf("://") > 0)\r
83                         return urlText;\r
84                 else\r
85                         return "http://"+urlText;\r
86         }\r
87         // Set the url\r
88         public void setUrl(String u) {\r
89                 url.setText(u);\r
90         }\r
91         // Check if the OK button was pressed\r
92         public boolean okPressed() {\r
93                 return okPressed;\r
94         }\r
95         // Check that we have a valid URL\r
96         @SuppressWarnings("unused")\r
97         private void validateInput() {\r
98                 ok.setEnabled(true);\r
99                 if (url.text().trim().equals("") && insertHyperlink) \r
100                         ok.setEnabled(false);\r
101         }\r
102         \r
103         @Override\r
104         public void accept() {\r
105                 if (ok.isEnabled()) {\r
106                         okPressed = true;\r
107                         urlText = url.text();\r
108                         super.accept();\r
109                 }\r
110         }\r
111         \r
112         @Override\r
113         public void reject() {\r
114                 okPressed=false;\r
115                 super.reject();\r
116         }\r
117 }\r