KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > util > gui > LanguageDialog


1 /*
2
3    Copyright 2000-2001,2003 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16
17  */

18 package org.apache.batik.util.gui;
19
20 import java.awt.Component JavaDoc;
21 import java.awt.FlowLayout JavaDoc;
22 import java.awt.GridBagConstraints JavaDoc;
23 import java.awt.GridBagLayout JavaDoc;
24 import java.awt.GridLayout JavaDoc;
25 import java.awt.Insets JavaDoc;
26 import java.awt.event.ActionEvent JavaDoc;
27 import java.net.URL JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.Locale JavaDoc;
30 import java.util.Map JavaDoc;
31 import java.util.MissingResourceException JavaDoc;
32 import java.util.ResourceBundle JavaDoc;
33 import java.util.StringTokenizer JavaDoc;
34
35 import javax.swing.AbstractAction JavaDoc;
36 import javax.swing.Action JavaDoc;
37 import javax.swing.BorderFactory JavaDoc;
38 import javax.swing.DefaultListModel JavaDoc;
39 import javax.swing.Icon JavaDoc;
40 import javax.swing.ImageIcon JavaDoc;
41 import javax.swing.JButton JavaDoc;
42 import javax.swing.JDialog JavaDoc;
43 import javax.swing.JFrame JavaDoc;
44 import javax.swing.JLabel JavaDoc;
45 import javax.swing.JList JavaDoc;
46 import javax.swing.JPanel JavaDoc;
47 import javax.swing.JScrollPane JavaDoc;
48 import javax.swing.ListCellRenderer JavaDoc;
49 import javax.swing.ListSelectionModel JavaDoc;
50 import javax.swing.event.ListSelectionEvent JavaDoc;
51 import javax.swing.event.ListSelectionListener JavaDoc;
52
53 import org.apache.batik.util.gui.resource.ActionMap;
54 import org.apache.batik.util.gui.resource.ButtonFactory;
55 import org.apache.batik.util.gui.resource.MissingListenerException;
56 import org.apache.batik.util.gui.resource.ResourceManager;
57
58 /**
59  * This class represents a language selection dialog.
60  *
61  * @author <a HREF="mailto:stephane@hillion.org">Stephane Hillion</a>
62  * @author <a HREF="mailto:cjolif@ilog.fr">Christophe Jolif</a>
63  * @version $Id: LanguageDialog.java,v 1.8 2004/08/18 07:15:54 vhardy Exp $
64  */

65 public class LanguageDialog extends JDialog JavaDoc implements ActionMap {
66
67     /**
68      * The return value if 'OK' is chosen.
69      */

70     public final static int OK_OPTION = 0;
71
72     /**
73      * The return value if 'Cancel' is chosen.
74      */

75     public final static int CANCEL_OPTION = 1;
76
77     /**
78      * The resource file name
79      */

80     protected final static String JavaDoc RESOURCES =
81         "org.apache.batik.util.gui.resources.LanguageDialogMessages";
82
83     /**
84      * The resource bundle
85      */

86     protected static ResourceBundle JavaDoc bundle;
87
88     /**
89      * The resource manager
90      */

91     protected static ResourceManager resources;
92
93     static {
94         bundle = ResourceBundle.getBundle(RESOURCES, Locale.getDefault());
95         resources = new ResourceManager(bundle);
96     }
97
98     /**
99      * The map that contains the listeners
100      */

101     protected Map JavaDoc listeners = new HashMap JavaDoc();
102
103     /**
104      * The user languages panel.
105      */

106     protected Panel JavaDoc panel = new Panel JavaDoc();
107
108     /**
109      * The last return code.
110      */

111     protected int returnCode;
112
113     /**
114      * Creates a new LanguageDialog object.
115      */

116     public LanguageDialog(JFrame JavaDoc f) {
117         super(f);
118         setModal(true);
119         setTitle(resources.getString("Dialog.title"));
120
121         listeners.put("OKButtonAction", new OKButtonAction());
122         listeners.put("CancelButtonAction", new CancelButtonAction());
123
124         getContentPane().add(panel);
125         getContentPane().add("South", createButtonsPanel());
126
127         pack();
128     }
129
130     /**
131      * Shows the dialog.
132      * @return OK_CANCEL or CANCEL_OPTION.
133      */

134     public int showDialog() {
135         show();
136         return returnCode;
137     }
138
139     /**
140      * Sets the user languages.
141      */

142     public void setLanguages(String JavaDoc s) {
143         panel.setLanguages(s);
144     }
145
146     /**
147      * Returns the user languages.
148      */

149     public String JavaDoc getLanguages() {
150         return panel.getLanguages();
151     }
152
153     // ActionMap implementation ///////////////////////////////////////
154

155     /**
156      * Returns the action associated with the given string
157      * or null on error
158      * @param key the key mapped with the action to get
159      * @throws MissingListenerException if the action is not found
160      */

161     public Action JavaDoc getAction(String JavaDoc key) throws MissingListenerException {
162         return (Action JavaDoc)listeners.get(key);
163     }
164
165     /**
166      * Creates the OK/Cancel buttons panel
167      */

168     protected JPanel JavaDoc createButtonsPanel() {
169         JPanel JavaDoc p = new JPanel JavaDoc(new FlowLayout JavaDoc(FlowLayout.RIGHT));
170         ButtonFactory bf = new ButtonFactory(bundle, this);
171         p.add(bf.createJButton("OKButton"));
172         p.add(bf.createJButton("CancelButton"));
173
174         return p;
175     }
176
177     /**
178      * The language selection panel.
179      */

180     public static class Panel extends JPanel JavaDoc implements ActionMap {
181         /**
182          * The user languages list
183          */

184         protected JList JavaDoc userList;
185
186         /**
187          * The languages list
188          */

189         protected JList JavaDoc languageList;
190
191         /**
192          * The user list model
193          */

194         protected DefaultListModel JavaDoc userListModel = new DefaultListModel JavaDoc();
195
196         /**
197          * The language list model
198          */

199         protected DefaultListModel JavaDoc languageListModel = new DefaultListModel JavaDoc();
200
201         /**
202          * The AddLanguageButton.
203          */

204         protected JButton JavaDoc addLanguageButton;
205
206         /**
207          * The RemoveLanguageButton.
208          */

209         protected JButton JavaDoc removeLanguageButton;
210
211         /**
212          * The UpLanguageButton.
213          */

214         protected JButton JavaDoc upLanguageButton;
215
216         /**
217          * The DownLanguageButton.
218          */

219         protected JButton JavaDoc downLanguageButton;
220
221         /**
222          * The ClearLanguageButton.
223          */

224         protected JButton JavaDoc clearLanguageButton;
225
226         /**
227          * The map that contains the listeners
228          */

229         protected Map JavaDoc listeners = new HashMap JavaDoc();
230
231         /**
232          * The cached map for country icons (takes more than 2 secs.
233          * to be computed).
234          */

235         private static Map JavaDoc iconMap = null;
236
237         /**
238          * Creates a new Panel object.
239          */

240         public Panel() {
241             super(new GridBagLayout JavaDoc());
242
243             initCountryIcons();
244
245             setBorder(BorderFactory.createTitledBorder
246                       (BorderFactory.createEtchedBorder(),
247                        resources.getString("Panel.title")));
248
249             listeners.put("AddLanguageButtonAction",
250                           new AddLanguageButtonAction());
251             listeners.put("RemoveLanguageButtonAction",
252                           new RemoveLanguageButtonAction());
253             listeners.put("UpLanguageButtonAction",
254                           new UpLanguageButtonAction());
255             listeners.put("DownLanguageButtonAction",
256                           new DownLanguageButtonAction());
257             listeners.put("ClearLanguageButtonAction",
258                           new ClearLanguageButtonAction());
259
260             // Initalize the lists
261
userList = new JList JavaDoc(userListModel);
262             userList.setCellRenderer(new IconAndTextCellRenderer());
263
264             languageList = new JList JavaDoc(languageListModel);
265             languageList.setCellRenderer(new IconAndTextCellRenderer());
266
267             StringTokenizer JavaDoc st;
268             st = new StringTokenizer JavaDoc(resources.getString("Country.list"), " ");
269             while (st.hasMoreTokens()) {
270                 languageListModel.addElement(st.nextToken());
271             }
272
273             // Layout out the children
274
ExtendedGridBagConstraints constraints =
275                 new ExtendedGridBagConstraints();
276             constraints.insets = new Insets JavaDoc(5, 5, 5, 5);
277
278             constraints.weightx = 1.0;
279             constraints.weighty = 1.0;
280             constraints.fill = GridBagConstraints.BOTH;
281
282             // The languages list
283
constraints.setGridBounds(0, 0, 1, 1);
284             JScrollPane JavaDoc sp = new JScrollPane JavaDoc();
285             sp.setBorder(BorderFactory.createCompoundBorder
286                          (BorderFactory.createTitledBorder
287                           (BorderFactory.createEmptyBorder(),
288                            resources.getString("Languages.title")),
289                           BorderFactory.createLoweredBevelBorder()));
290             sp.getViewport().add(languageList);
291             this.add(sp, constraints);
292
293             languageList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
294             languageList.addListSelectionListener
295                 (new LanguageListSelectionListener());
296
297             // The user languages list
298
constraints.setGridBounds(2, 0, 1, 1);
299
300             JScrollPane JavaDoc sp2 = new JScrollPane JavaDoc();
301             sp2.setBorder(BorderFactory.createCompoundBorder
302                           (BorderFactory.createTitledBorder
303                            (BorderFactory.createEmptyBorder(),
304                             resources.getString("User.title")),
305                            BorderFactory.createLoweredBevelBorder()));
306             sp2.getViewport().add(userList);
307             this.add(sp2, constraints);
308
309             userList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
310             userList.addListSelectionListener(new UserListSelectionListener());
311
312             // The info label
313
constraints.setGridBounds(0, 1, 3, 1);
314             constraints.weightx = 0;
315             constraints.weighty = 0;
316             this.add(new JLabel JavaDoc(resources.getString("InfoLabel.text")),
317                      constraints);
318
319             // The buttons
320
ButtonFactory bf = new ButtonFactory(bundle, this);
321             JPanel JavaDoc p = new JPanel JavaDoc(new GridLayout JavaDoc(5, 1, 0, 3));
322             p.add(addLanguageButton = bf.createJButton("AddLanguageButton"));
323             addLanguageButton.setEnabled(false);
324             p.add(removeLanguageButton =
325                   bf.createJButton("RemoveLanguageButton"));
326             removeLanguageButton.setEnabled(false);
327             p.add(upLanguageButton = bf.createJButton("UpLanguageButton"));
328             upLanguageButton.setEnabled(false);
329             p.add(downLanguageButton = bf.createJButton("DownLanguageButton"));
330             downLanguageButton.setEnabled(false);
331             p.add(clearLanguageButton =
332                   bf.createJButton("ClearLanguageButton"));
333             clearLanguageButton.setEnabled(false);
334
335             JPanel JavaDoc t = new JPanel JavaDoc(new GridBagLayout JavaDoc());
336             constraints.setGridBounds(1, 0, 1, 1);
337             this.add(t, constraints);
338
339             constraints.fill = GridBagConstraints.HORIZONTAL;
340             constraints.setGridBounds(0, 0, 1, 1);
341             constraints.insets = new Insets JavaDoc(0, 0, 0, 0);
342             t.add(p, constraints);
343
344             sp2.setPreferredSize(sp.getPreferredSize());
345         }
346
347         /**
348          * Allows to pre-initialize icons used by the <code>Panel</code>
349          * constructor. It is not neccessary to call it and it should
350          * be called only once.
351          * This method is safe to be called by another thread than the
352          * event thread as it doesn't manipulate Swing <code>JComponent</code>
353          * instances.
354          */

355         public synchronized static void initCountryIcons()
356         {
357             // don't need to init several times...
358
if (iconMap == null) {
359                 iconMap = new HashMap JavaDoc();
360                 StringTokenizer JavaDoc st;
361                 st = new StringTokenizer JavaDoc(resources.getString("Country.list"),
362                                          " ");
363                 while (st.hasMoreTokens()) {
364                     computeCountryIcon(LanguageDialog.Panel.class,
365                                        st.nextToken());
366                 }
367             }
368         }
369
370         /**
371          * Returns the selected user languages.
372          */

373         public String JavaDoc getLanguages() {
374             StringBuffer JavaDoc result = new StringBuffer JavaDoc();
375             if (userListModel.getSize() > 0) {
376                 result.append(userListModel.getElementAt(0));
377
378                 for (int i = 1; i < userListModel.getSize(); i++) {
379                     result.append("," + userListModel.getElementAt(i));
380                 }
381             }
382             return result.toString();
383         }
384
385         /**
386          * Sets the user languages.
387          */

388         public void setLanguages(String JavaDoc str) {
389             int len = userListModel.getSize();
390             for (int i = 0; i < len; i++) {
391                 Object JavaDoc o = userListModel.getElementAt(0);
392                 userListModel.removeElementAt(0);
393                 int size = languageListModel.getSize();
394                 int n = 0;
395                 while (n < size) {
396                     String JavaDoc s = (String JavaDoc)languageListModel.getElementAt(n);
397                     if (s.compareTo(o) > 0) {
398                         break;
399                     }
400                     n++;
401                 }
402                 languageListModel.insertElementAt(o, n);
403             }
404
405             StringTokenizer JavaDoc st;
406             st = new StringTokenizer JavaDoc(str, ",");
407             while (st.hasMoreTokens()) {
408                 String JavaDoc s = st.nextToken();
409                 userListModel.addElement(s);
410                 languageListModel.removeElement(s);
411             }
412
413             updateButtons();
414         }
415
416         /**
417          * Updates the state of the buttons
418          */

419         protected void updateButtons() {
420             int size = userListModel.size();
421             int i = userList.getSelectedIndex();
422
423             boolean empty = size == 0;
424             boolean selected = i != -1;
425             boolean zeroSelected = i == 0;
426             boolean lastSelected = i == size - 1;
427
428             removeLanguageButton.setEnabled(!empty && selected);
429             upLanguageButton.setEnabled(!empty && selected && !zeroSelected);
430             downLanguageButton.setEnabled(!empty && selected && !lastSelected);
431             clearLanguageButton.setEnabled(!empty);
432
433             size = languageListModel.size();
434             i = languageList.getSelectedIndex();
435
436             empty = size == 0;
437             selected = i != -1;
438
439             addLanguageButton.setEnabled(!empty && selected);
440         }
441
442         /**
443          * returns the full string associated with a country code.
444          */

445         protected String JavaDoc getCountryText(String JavaDoc code) {
446             return resources.getString(code + ".text");
447         }
448
449         /**
450          * returns the icon associated with a country code.
451          */

452         protected Icon JavaDoc getCountryIcon(String JavaDoc code) {
453             return computeCountryIcon(getClass(), code);
454         }
455
456         private static Icon JavaDoc computeCountryIcon(Class JavaDoc ref,
457                                                String JavaDoc code) {
458             ImageIcon JavaDoc icon = null;
459             try {
460                 if ((icon = (ImageIcon JavaDoc)iconMap.get(code)) != null)
461                     return icon;
462                 String JavaDoc s = resources.getString(code + ".icon");
463                 URL JavaDoc url = ref.getResource(s);
464                 if (url != null) {
465                     iconMap.put(code, icon = new ImageIcon JavaDoc(url));
466                     return icon;
467                 }
468             } catch (MissingResourceException JavaDoc e) {
469             }
470             return new ImageIcon JavaDoc(ref.getResource("resources/blank.gif"));
471         }
472
473         // ActionMap implementation ///////////////////////////////////////
474

475         /**
476          * Returns the action associated with the given string
477          * or null on error
478          * @param key the key mapped with the action to get
479          * @throws MissingListenerException if the action is not found
480          */

481         public Action JavaDoc getAction(String JavaDoc key) throws MissingListenerException {
482             return (Action JavaDoc)listeners.get(key);
483         }
484
485         /**
486          * The action associated with the 'add' button
487          */

488         protected class AddLanguageButtonAction extends AbstractAction JavaDoc {
489             public void actionPerformed(ActionEvent JavaDoc e) {
490                 int i = languageList.getSelectedIndex();
491                 Object JavaDoc o = languageListModel.getElementAt(i);
492                 languageListModel.removeElementAt(i);
493                 userListModel.addElement(o);
494                 userList.setSelectedValue(o, true);
495             }
496         }
497
498         /**
499          * The action associated with the 'remove' button
500          */

501         protected class RemoveLanguageButtonAction extends AbstractAction JavaDoc {
502             public void actionPerformed(ActionEvent JavaDoc e) {
503                 int i = userList.getSelectedIndex();
504                 Object JavaDoc o = userListModel.getElementAt(i);
505                 userListModel.removeElementAt(i);
506
507                 int size = languageListModel.getSize();
508                 int n = 0;
509                 while (n < size) {
510                     String JavaDoc s = (String JavaDoc)languageListModel.getElementAt(n);
511                     if (s.compareTo(o) > 0) {
512                         break;
513                     }
514                     n++;
515                 }
516                 languageListModel.insertElementAt(o, n);
517                 languageList.setSelectedValue(o, true);
518                 updateButtons();
519             }
520         }
521
522         /**
523          * The action associated with the 'up' button
524          */

525         protected class UpLanguageButtonAction extends AbstractAction JavaDoc {
526             public void actionPerformed(ActionEvent JavaDoc e) {
527                 int i = userList.getSelectedIndex();
528                 Object JavaDoc o = userListModel.getElementAt(i);
529                 userListModel.removeElementAt(i);
530                 userListModel.insertElementAt(o, i - 1);
531                 userList.setSelectedIndex(i - 1);
532             }
533         }
534
535         /**
536          * The action associated with the 'down' button
537          */

538         protected class DownLanguageButtonAction extends AbstractAction JavaDoc {
539             public void actionPerformed(ActionEvent JavaDoc e) {
540                 int i = userList.getSelectedIndex();
541                 Object JavaDoc o = userListModel.getElementAt(i);
542                 userListModel.removeElementAt(i);
543                 userListModel.insertElementAt(o, i + 1);
544                 userList.setSelectedIndex(i + 1);
545             }
546         }
547
548         /**
549          * The action associated with the 'clear' button
550          */

551         protected class ClearLanguageButtonAction extends AbstractAction JavaDoc {
552             public void actionPerformed(ActionEvent JavaDoc e) {
553                 int len = userListModel.getSize();
554                 for (int i = 0; i < len; i++) {
555                     Object JavaDoc o = userListModel.getElementAt(0);
556                     userListModel.removeElementAt(0);
557                     int size = languageListModel.getSize();
558                     int n = 0;
559                     while (n < size) {
560                         String JavaDoc s = (String JavaDoc)languageListModel.getElementAt(n);
561                         if (s.compareTo(o) > 0) {
562                             break;
563                         }
564                         n++;
565                     }
566                     languageListModel.insertElementAt(o, n);
567                 }
568                 updateButtons();
569             }
570         }
571
572         /**
573          * To manage selection modifications
574          */

575         protected class LanguageListSelectionListener
576             implements ListSelectionListener JavaDoc {
577             public void valueChanged(ListSelectionEvent JavaDoc e) {
578                 int i = languageList.getSelectedIndex();
579                 userList.getSelectionModel().clearSelection();
580                 languageList.setSelectedIndex(i);
581                 updateButtons();
582             }
583         }
584
585         /**
586          * To manage selection modifications
587          */

588         protected class UserListSelectionListener
589             implements ListSelectionListener JavaDoc {
590             public void valueChanged(ListSelectionEvent JavaDoc e) {
591                 int i = userList.getSelectedIndex();
592                 languageList.getSelectionModel().clearSelection();
593                 userList.setSelectedIndex(i);
594                 updateButtons();
595             }
596         }
597
598         /**
599          * To display icons and text in the lists.
600          */

601         protected class IconAndTextCellRenderer
602             extends JLabel JavaDoc
603             implements ListCellRenderer JavaDoc {
604             public IconAndTextCellRenderer() {
605                 this.setOpaque(true);
606                 this.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
607             }
608             public Component JavaDoc getListCellRendererComponent(JList JavaDoc list,
609                                                           Object JavaDoc value,
610                                                           int index,
611                                                           boolean isSelected,
612                                                           boolean cellHasFocus){
613                 String JavaDoc s = (String JavaDoc)value;
614                 this.setText(getCountryText(s));
615                 this.setIcon(getCountryIcon(s));
616                 this.setEnabled(list.isEnabled());
617                 this.setFont(list.getFont());
618                 if (isSelected) {
619                     this.setBackground(list.getSelectionBackground());
620                     this.setForeground(list.getSelectionForeground());
621                 } else {
622                     this.setBackground(list.getBackground());
623                     this.setForeground(list.getForeground());
624                 }
625                 return this;
626             }
627         }
628     }
629
630     /**
631      * The action associated with the 'OK' button
632      */

633     protected class OKButtonAction extends AbstractAction JavaDoc {
634         public void actionPerformed(ActionEvent JavaDoc e) {
635             returnCode = OK_OPTION;
636             dispose();
637         }
638     }
639
640     /**
641      * The action associated with the 'Cancel' button
642      */

643     protected class CancelButtonAction extends AbstractAction JavaDoc {
644         public void actionPerformed(ActionEvent JavaDoc e) {
645             returnCode = CANCEL_OPTION;
646             dispose();
647         }
648     }
649 }
650
Popular Tags