OSDN Git Service

Added option to add tag as a child of a selected tag.
[neighbornote/NeighborNote.git] / src / cx / fbn / nevernote / dialog / TagEdit.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.Tag;
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 TagEdit extends QDialog {
34         private boolean         okPressed;
35         private final QLineEdit tag;
36         QPushButton ok;
37         private final QCheckBox useParentTag;
38         List<Tag>               currentTags;
39         private final String iconPath = new String("classpath:cx/fbn/nevernote/icons/");
40         
41         // Constructor
42         public TagEdit() {
43                 okPressed = false;
44                 setWindowTitle(tr("Add Tag"));
45                 QGridLayout grid = new QGridLayout();
46                 setWindowIcon(new QIcon(iconPath+"tag.png"));
47                 setLayout(grid);
48                 
49                 QGridLayout textGrid = new QGridLayout();
50                 tag = new QLineEdit();
51                 textGrid.addWidget(new QLabel(tr("Tag Name")), 1,1);
52                 textGrid.addWidget(tag, 1, 2);
53                 
54                 textGrid.setContentsMargins(10, 10,-10, -10);
55                 grid.addLayout(textGrid,1,1);
56
57                 useParentTag = new QCheckBox();
58                 useParentTag.setVisible(false);
59                 useParentTag.setChecked(false);
60                 grid.addWidget(useParentTag,2,1);
61                 
62                 QGridLayout buttonGrid = new QGridLayout();
63                 ok = new QPushButton(tr("OK"));
64                 ok.clicked.connect(this, "okButtonPressed()");
65                 ok.setEnabled(false);
66                 QPushButton cancel = new QPushButton(tr("Cancel"));
67                 cancel.clicked.connect(this, "cancelButtonPressed()");
68                 tag.textChanged.connect(this, "textChanged()");
69                 buttonGrid.addWidget(ok, 3, 1);
70                 buttonGrid.addWidget(cancel, 3,2);
71                 grid.addLayout(buttonGrid,3,1);
72         }
73         
74         // The OK button was pressed
75         @SuppressWarnings("unused")
76         private void okButtonPressed() {
77                 okPressed = true;
78                 close();
79         }
80         
81         // The CANCEL button was pressed
82         @SuppressWarnings("unused")
83         private void cancelButtonPressed() {
84                 okPressed = false;
85                 close();
86         }
87         
88         // Get the name from the field
89         public String getTag() {
90                 return tag.text();
91         }
92         
93         // Set the tag name
94         public void setTag(String name) {
95                 tag.setText(name);
96         }
97
98         // Set the tag name
99         public void setParentTag(String name) {
100                 useParentTag.setText(tr("Create as child of \"") +name +"\"");
101                 useParentTag.setVisible(true);
102                 useParentTag.setChecked(true);
103         }
104         public QCheckBox getParentTag() {
105                 return useParentTag;
106         }
107         
108         // Check if the OK button was pressed
109         public boolean okPressed() {
110                 return okPressed;
111         }
112         
113         // Set the window title
114         public void setTitle(String s) {
115                 setWindowTitle(s);
116         }
117         // List of existing tags
118         public void setTagList(List<Tag> t) {
119                 currentTags = t;
120         }
121         // Watch what text is being entered
122         @SuppressWarnings("unused")
123         private void textChanged() {
124                 if (tag.text().equals("")) {
125                         ok.setEnabled(false);
126                         return;
127                 }
128                 if (currentTags == null) {
129                         ok.setEnabled(false);
130                         return;
131                 }
132                 for (int i=0; i<currentTags.size(); i++) {
133                         String s = currentTags.get(i).getName();
134                         if (s.equalsIgnoreCase(tag.text())) {
135                                 ok.setEnabled(false);
136                                 return;
137                         }
138                 }
139                 ok.setEnabled(true);
140         }
141 }