OSDN Git Service

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