KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > core > gui > tagging > EditTagDialog


1 package org.columba.core.gui.tagging;
2
3 import java.awt.BorderLayout JavaDoc;
4 import java.awt.Color JavaDoc;
5 import java.awt.Container JavaDoc;
6 import java.awt.Graphics2D JavaDoc;
7 import java.awt.event.ActionEvent JavaDoc;
8 import java.awt.event.ActionListener JavaDoc;
9 import java.awt.image.BufferedImage JavaDoc;
10 import java.util.ResourceBundle JavaDoc;
11
12 import javax.swing.BorderFactory JavaDoc;
13 import javax.swing.Icon JavaDoc;
14 import javax.swing.ImageIcon JavaDoc;
15 import javax.swing.JButton JavaDoc;
16 import javax.swing.JColorChooser JavaDoc;
17 import javax.swing.JDialog JavaDoc;
18 import javax.swing.JFrame JavaDoc;
19 import javax.swing.JLabel JavaDoc;
20 import javax.swing.JPanel JavaDoc;
21 import javax.swing.JScrollPane JavaDoc;
22 import javax.swing.JTextArea JavaDoc;
23 import javax.swing.JTextField JavaDoc;
24 import javax.swing.SwingConstants JavaDoc;
25 import javax.swing.event.DocumentEvent JavaDoc;
26 import javax.swing.event.DocumentListener JavaDoc;
27
28 import org.columba.core.gui.base.MnemonicSetter;
29 import org.columba.core.gui.base.SingleSideEtchedBorder;
30
31 import com.jgoodies.forms.factories.Borders;
32 import com.jgoodies.forms.factories.FormFactory;
33 import com.jgoodies.forms.layout.CellConstraints;
34 import com.jgoodies.forms.layout.ColumnSpec;
35 import com.jgoodies.forms.layout.FormLayout;
36 import com.jgoodies.forms.layout.FormSpec;
37 import com.jgoodies.forms.layout.RowSpec;
38 import com.jgoodies.forms.layout.Sizes;
39
40 public class EditTagDialog extends JDialog JavaDoc implements ActionListener JavaDoc {
41
42     private JPanel JavaDoc dialogPane;
43
44     private JPanel JavaDoc contentPanel;
45
46     private JLabel JavaDoc label1;
47
48     private JTextField JavaDoc textField1;
49
50     private JLabel JavaDoc label2;
51
52     private JButton JavaDoc button1;
53
54     private JPanel JavaDoc buttonBar;
55
56     private JButton JavaDoc okButton;
57
58     private JButton JavaDoc cancelButton;
59
60     private JLabel JavaDoc label3;
61
62     private JScrollPane JavaDoc scrollPane1;
63
64     private JTextArea JavaDoc textArea1;
65
66     private String JavaDoc name;
67
68     private Color JavaDoc color;
69
70     private String JavaDoc description;
71
72     private boolean success = false;
73
74     private ResourceBundle JavaDoc bundle = ResourceBundle
75             .getBundle("org.columba.core.i18n.dialog.tagging");
76
77     public EditTagDialog(JFrame JavaDoc parent) {
78         super(parent, true);
79
80         setTitle(bundle.getString("EditTagDialog.title"));
81
82         initComponents();
83
84         
85         layoutComponents();
86
87         pack();
88         setLocationRelativeTo(null);
89         setVisible(true);
90     }
91
92     public EditTagDialog(JFrame JavaDoc parent, String JavaDoc name, Color JavaDoc color,
93             String JavaDoc description) {
94         super(parent, true);
95
96         this.name = name;
97         this.color = color;
98         this.description = description;
99
100         setTitle(bundle.getString("EditTagDialog.title"));
101
102         initComponents();
103
104         layoutComponents();
105
106         pack();
107         setLocationRelativeTo(null);
108         setVisible(true);
109     }
110
111     private void layoutComponents() {
112         CellConstraints cc = new CellConstraints();
113
114         Container JavaDoc contentPane = getContentPane();
115         contentPane.setLayout(new BorderLayout JavaDoc());
116
117         dialogPane.setLayout(new BorderLayout JavaDoc());
118
119         contentPanel.setBorder(Borders.DIALOG_BORDER);
120         contentPanel.setLayout(new FormLayout(new ColumnSpec[] {
121                 new ColumnSpec(ColumnSpec.RIGHT, Sizes.DEFAULT,
122                         FormSpec.NO_GROW),
123                 FormFactory.LABEL_COMPONENT_GAP_COLSPEC,
124                 new ColumnSpec(ColumnSpec.FILL, Sizes.DEFAULT,
125                         FormSpec.DEFAULT_GROW) },
126                 new RowSpec[] {
127                         FormFactory.DEFAULT_ROWSPEC,
128                         FormFactory.LINE_GAP_ROWSPEC,
129                         FormFactory.DEFAULT_ROWSPEC,
130                         FormFactory.LINE_GAP_ROWSPEC,
131                         new RowSpec(RowSpec.FILL, Sizes.DEFAULT,
132                                 FormSpec.DEFAULT_GROW) }));
133         ((FormLayout) contentPanel.getLayout()).setRowGroups(new int[][] { { 1,
134                 3 } });
135
136         contentPanel.add(label1, cc.xy(1, 1));
137
138         textField1.setColumns(10);
139         contentPanel.add(textField1, cc.xy(3, 1));
140
141         contentPanel.add(label2, cc.xy(1, 3));
142         contentPanel.add(button1, cc.xywh(3, 3, 1, 1, CellConstraints.LEFT,
143                 CellConstraints.CENTER));
144
145         contentPanel.add(label3, cc.xywh(1, 5, 1, 1, CellConstraints.DEFAULT,
146                 CellConstraints.TOP));
147
148         textArea1.setColumns(15);
149         textArea1.setRows(2);
150         scrollPane1.setViewportView(textArea1);
151
152         contentPanel.add(scrollPane1, cc.xy(3, 5));
153
154         dialogPane.add(contentPanel, BorderLayout.CENTER);
155
156         JPanel JavaDoc bottomBar = new JPanel JavaDoc();
157         bottomBar.setLayout(new BorderLayout JavaDoc());
158         bottomBar.add(buttonBar, BorderLayout.EAST);
159         bottomBar.setBorder(new SingleSideEtchedBorder(SwingConstants.TOP));
160
161         buttonBar.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12));
162         buttonBar.setLayout(new FormLayout(new ColumnSpec[] {
163                 FormFactory.GLUE_COLSPEC, FormFactory.BUTTON_COLSPEC,
164                 FormFactory.RELATED_GAP_COLSPEC, FormFactory.BUTTON_COLSPEC },
165                 RowSpec.decodeSpecs("pref")));
166
167         buttonBar.add(okButton, cc.xy(2, 1));
168
169         buttonBar.add(cancelButton, cc.xy(4, 1));
170
171         dialogPane.add(bottomBar, BorderLayout.SOUTH);
172
173         contentPane.add(dialogPane, BorderLayout.CENTER);
174     }
175
176     private void initComponents() {
177
178         dialogPane = new JPanel JavaDoc();
179         contentPanel = new JPanel JavaDoc();
180         label1 = new JLabel JavaDoc();
181         MnemonicSetter.setTextWithMnemonic(label1, bundle
182                 .getString("label1.text"));
183         textField1 = new JTextField JavaDoc();
184         
185         label1.setLabelFor(textField1);
186         if (name != null)
187             textField1.setText(name);
188         label2 = new JLabel JavaDoc();
189         MnemonicSetter.setTextWithMnemonic(label2, bundle
190                 .getString("label2.text"));
191         button1 = new JButton JavaDoc();
192         label2.setLabelFor(button1);
193         button1.addActionListener(new ActionListener JavaDoc() {
194             public void actionPerformed(ActionEvent JavaDoc e) {
195                 color = JColorChooser.showDialog(null, "Choose Tag Color",
196                         color);
197                 if (color != null) {
198                     button1.setIcon(createIcon(color));
199                     button1.setText("");
200                 } else
201                     button1.setText("no Color");
202             }
203         });
204
205         if (color != null)
206             button1.setIcon(createIcon(color));
207         else
208             button1.setText("no Color");
209
210         label3 = new JLabel JavaDoc();
211         MnemonicSetter.setTextWithMnemonic(label3, bundle
212                 .getString("label3.text"));
213         scrollPane1 = new JScrollPane JavaDoc();
214         textArea1 = new JTextArea JavaDoc();
215         if (description != null)
216             textArea1.setText(description);
217
218         label3.setLabelFor(textArea1);
219
220         ResourceBundle JavaDoc globalBundle = ResourceBundle
221                 .getBundle("org.columba.core.i18n.global.global");
222
223         buttonBar = new JPanel JavaDoc();
224         okButton = new JButton JavaDoc();
225         okButton.setActionCommand("OK");
226         okButton.addActionListener(this);
227         MnemonicSetter.setTextWithMnemonic(okButton, globalBundle
228                 .getString("ok"));
229         cancelButton = new JButton JavaDoc();
230         cancelButton.setActionCommand("CANCEL");
231         cancelButton.addActionListener(this);
232         MnemonicSetter.setTextWithMnemonic(cancelButton, globalBundle
233                 .getString("cancel"));
234         
235         textField1.getDocument().addDocumentListener(new MyDocumentListener());
236     }
237
238     public void actionPerformed(ActionEvent JavaDoc e) {
239         if (e.getActionCommand().equals("OK")) {
240
241             success = true;
242
243             name = textField1.getText();
244             description = textArea1.getText();
245
246             setVisible(false);
247         } else if (e.getActionCommand().equals("CANCEL")) {
248             success = false;
249
250             setVisible(false);
251         }
252     }
253
254     private Icon JavaDoc createIcon(Color JavaDoc color) {
255         int width = 16;
256         int height = 16;
257         BufferedImage JavaDoc image = new BufferedImage JavaDoc(width, height,
258                 BufferedImage.TYPE_INT_ARGB);
259
260         Graphics2D JavaDoc graphics = (Graphics2D JavaDoc) image.getGraphics();
261         graphics.setColor(color.darker());
262         graphics.drawRect(1, 1, width - 3, height - 3);
263         graphics.setColor(color);
264         graphics.fillRect(2, 2, width - 4, height - 4);
265         graphics.dispose();
266
267         return new ImageIcon JavaDoc(image);
268     }
269
270     public boolean getSuccess() {
271         return success;
272     }
273
274     public Color JavaDoc getColor() {
275         return color;
276     }
277
278     public String JavaDoc getName() {
279         return name;
280     }
281
282     public String JavaDoc getDescription() {
283         return description;
284     }
285
286     class MyDocumentListener implements DocumentListener JavaDoc {
287
288         public void changedUpdate(DocumentEvent JavaDoc e) {
289             handleUpdate(e);
290         }
291
292         public void insertUpdate(DocumentEvent JavaDoc e) {
293             handleUpdate(e);
294         }
295
296         public void removeUpdate(DocumentEvent JavaDoc e) {
297             handleUpdate(e);
298         }
299
300         private void handleUpdate(DocumentEvent JavaDoc e) {
301             if ( textField1.getText() == null || textField1.getText().length() == 0 )
302                 okButton.setEnabled(false);
303             else
304                 okButton.setEnabled(true);
305         }
306
307     }
308 }
309
Popular Tags