KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Copyright 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.BorderLayout JavaDoc;
21 import java.awt.Component JavaDoc;
22 import java.awt.FlowLayout JavaDoc;
23 import java.awt.GridBagConstraints JavaDoc;
24 import java.awt.GridBagLayout JavaDoc;
25 import java.awt.Insets JavaDoc;
26 import java.awt.event.ActionEvent JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Enumeration JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.Locale JavaDoc;
33 import java.util.Map JavaDoc;
34 import java.util.ResourceBundle JavaDoc;
35 import java.util.StringTokenizer JavaDoc;
36
37 import javax.swing.AbstractAction JavaDoc;
38 import javax.swing.Action JavaDoc;
39 import javax.swing.BorderFactory JavaDoc;
40 import javax.swing.DefaultListModel JavaDoc;
41 import javax.swing.JButton JavaDoc;
42 import javax.swing.JComboBox JavaDoc;
43 import javax.swing.JDialog JavaDoc;
44 import javax.swing.JLabel JavaDoc;
45 import javax.swing.JList JavaDoc;
46 import javax.swing.JOptionPane JavaDoc;
47 import javax.swing.JPanel JavaDoc;
48 import javax.swing.JScrollPane JavaDoc;
49 import javax.swing.ListSelectionModel JavaDoc;
50 import javax.swing.event.ListDataEvent JavaDoc;
51 import javax.swing.event.ListDataListener JavaDoc;
52 import javax.swing.event.ListSelectionEvent JavaDoc;
53 import javax.swing.event.ListSelectionListener JavaDoc;
54
55 import org.apache.batik.util.gui.resource.ActionMap;
56 import org.apache.batik.util.gui.resource.ButtonFactory;
57 import org.apache.batik.util.gui.resource.MissingListenerException;
58 import org.apache.batik.util.gui.resource.ResourceManager;
59
60 /**
61  * This class represents a panel to edit/add/remove CSS media.
62  *
63  * @author <a HREF="mailto:tkormann@apache.org">Thierry Kormann</a>
64  * @version $Id: CSSMediaPanel.java,v 1.6 2004/10/30 16:54:54 deweese Exp $
65  */

66 public class CSSMediaPanel extends JPanel JavaDoc implements ActionMap {
67
68     /**
69      * The resource file name
70      */

71     protected final static String JavaDoc RESOURCES =
72         "org.apache.batik.util.gui.resources.CSSMediaPanel";
73
74     /**
75      * The resource bundle
76      */

77     protected static ResourceBundle JavaDoc bundle;
78
79     /**
80      * The resource manager
81      */

82     protected static ResourceManager resources;
83
84     static {
85         bundle = ResourceBundle.getBundle(RESOURCES, Locale.getDefault());
86         resources = new ResourceManager(bundle);
87     }
88
89     /**
90      * The button to remove a CSS medium from the list.
91      */

92     protected JButton JavaDoc removeButton;
93
94     /**
95      * The button to add a CSS medium from the list.
96      */

97     protected JButton JavaDoc addButton;
98
99     /**
100      * The button to clear the CSS media list.
101      */

102     protected JButton JavaDoc clearButton;
103
104     /**
105      * The list that represents the CSS media.
106      */

107     protected DefaultListModel JavaDoc listModel = new DefaultListModel JavaDoc();
108
109     /**
110      * The list that represents the CSS media.
111      */

112     protected JList JavaDoc mediaList;
113
114     /**
115      * Constructs a new panel to edit CSS media.
116      */

117     public CSSMediaPanel() {
118     super(new GridBagLayout JavaDoc());
119
120     listeners.put("AddButtonAction", new AddButtonAction());
121     listeners.put("RemoveButtonAction", new RemoveButtonAction());
122     listeners.put("ClearButtonAction", new ClearButtonAction());
123
124     setBorder(BorderFactory.createTitledBorder
125           (BorderFactory.createEtchedBorder(),
126            resources.getString("Panel.title")));
127         
128     ExtendedGridBagConstraints constraints =
129         new ExtendedGridBagConstraints();
130
131     constraints.insets = new Insets JavaDoc(5, 5, 5, 5);
132
133     mediaList = new JList JavaDoc();
134     mediaList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
135     mediaList.setModel(listModel);
136     mediaList.addListSelectionListener(new MediaListSelectionListener());
137     listModel.addListDataListener(new MediaListDataListener());
138
139     JScrollPane JavaDoc scrollPane = new JScrollPane JavaDoc();
140     scrollPane.setBorder(BorderFactory.createLoweredBevelBorder());
141     constraints.weightx = 1.0;
142     constraints.weighty = 1.0;
143     constraints.fill = GridBagConstraints.BOTH;
144     constraints.setGridBounds(0, 0, 1, 3);
145     scrollPane.getViewport().add(mediaList);
146     add(scrollPane, constraints);
147
148     ButtonFactory bf = new ButtonFactory(bundle, this);
149     constraints.weightx = 0;
150     constraints.weighty = 0;
151     constraints.fill = GridBagConstraints.HORIZONTAL;
152     constraints.anchor = GridBagConstraints.NORTH;
153
154     addButton = bf.createJButton("AddButton");
155     constraints.setGridBounds(1, 0, 1, 1);
156     add(addButton, constraints);
157
158     removeButton = bf.createJButton("RemoveButton");
159     constraints.setGridBounds(1, 1, 1, 1);
160     add(removeButton, constraints);
161
162     clearButton = bf.createJButton("ClearButton");
163     constraints.setGridBounds(1, 2, 1, 1);
164     add(clearButton, constraints);
165
166     updateButtons();
167     }
168
169     /**
170      * Updates the button states.
171      */

172     protected void updateButtons() {
173     removeButton.setEnabled(!mediaList.isSelectionEmpty());
174     clearButton.setEnabled(!listModel.isEmpty());
175     }
176
177     /**
178      * Sets the list of media to edit.
179      *
180      * @param mediaList the list of media to edit
181      */

182     public void setMedia(List JavaDoc mediaList) {
183     listModel.removeAllElements();
184     Iterator JavaDoc iter = mediaList.iterator();
185     while (iter.hasNext()) {
186         listModel.addElement(iter.next());
187     }
188     }
189
190     /**
191      * Sets the list of media to edit to the specified media list (separated by
192      * space).
193      *
194      * @param media the media separated by space
195      */

196     public void setMedia(String JavaDoc media) {
197     listModel.removeAllElements();
198     StringTokenizer JavaDoc tokens = new StringTokenizer JavaDoc(media, " ");
199     while (tokens.hasMoreTokens()) {
200         listModel.addElement(tokens.nextToken());
201     }
202     }
203     
204     /**
205      * Returns the list of media.
206      */

207     public List JavaDoc getMedia() {
208     List JavaDoc media = new ArrayList JavaDoc(listModel.size());
209     Enumeration JavaDoc e = listModel.elements();
210     while (e.hasMoreElements()) {
211         media.add(e.nextElement());
212     }
213     return media;
214     }
215
216     /**
217      * Returns the media list as a string separated by space.
218      */

219     public String JavaDoc getMediaAsString() {
220     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
221     Enumeration JavaDoc e = listModel.elements();
222     while (e.hasMoreElements()) {
223         buffer.append((String JavaDoc)e.nextElement());
224         buffer.append(" ");
225     }
226     return buffer.toString();
227     }
228
229     /**
230      * Brings up a modal dialog to edit/add/remove CSS media.
231      *
232      * @param parent the parent of this dialog
233      * @param title the title of this dialog
234      */

235     public static int showDialog(Component JavaDoc parent, String JavaDoc title) {
236     return showDialog(parent, title, "");
237     }
238
239     /**
240      * Brings up a modal dialog to edit/add/remove CSS media.
241      *
242      * @param parent the parent of this dialog
243      * @param title the title of this dialog
244      * @param mediaList the list of media
245      */

246     public static int showDialog(Component JavaDoc parent,
247                  String JavaDoc title,
248                  List JavaDoc mediaList) {
249     Dialog JavaDoc dialog = new Dialog JavaDoc(parent, title, mediaList);
250     dialog.setModal(true);
251     dialog.pack();
252     dialog.show();
253     return dialog.getReturnCode();
254     }
255
256     /**
257      * Brings up a modal dialog to edit/add/remove CSS media.
258      *
259      * @param parent the parent of this dialog
260      * @param title the title of this dialog
261      * @param media the list of media
262      */

263     public static int showDialog(Component JavaDoc parent,
264                  String JavaDoc title,
265                  String JavaDoc media) {
266     Dialog JavaDoc dialog = new Dialog JavaDoc(parent, title, media);
267     dialog.setModal(true);
268     dialog.pack();
269     dialog.show();
270     return dialog.getReturnCode();
271     }
272
273     /**
274      * The map that contains the listeners
275      */

276     protected Map JavaDoc listeners = new HashMap JavaDoc();
277     
278     /**
279      * Returns the action associated with the given string or null on error
280      *
281      * @param key the key mapped with the action to get
282      * @throws MissingListenerException if the action is not found
283      */

284     public Action JavaDoc getAction(String JavaDoc key) throws MissingListenerException {
285     return (Action JavaDoc)listeners.get(key);
286     }
287
288     /**
289      * The action associated with the 'Add' button
290      */

291     protected class AddButtonAction extends AbstractAction JavaDoc {
292     public void actionPerformed(ActionEvent JavaDoc e) {
293         AddMediumDialog dialog = new AddMediumDialog(CSSMediaPanel.this);
294         dialog.pack();
295         dialog.show();
296
297         if ((dialog.getReturnCode() == AddMediumDialog.CANCEL_OPTION) ||
298         (dialog.getMedium() == null)) {
299         return;
300         }
301
302         String JavaDoc medium = dialog.getMedium().trim();
303         if (medium.length() == 0 || listModel.contains(medium)) {
304         return;
305         }
306
307         for (int i = 0; i < listModel.size() && medium != null; ++i) {
308         String JavaDoc s = (String JavaDoc)listModel.getElementAt(i);
309         int c = medium.compareTo(s);
310         if (c == 0) {
311             medium = null;
312         } else if (c < 0) {
313             listModel.insertElementAt(medium, i);
314             medium = null;
315         }
316         }
317         if (medium != null) {
318         listModel.addElement(medium);
319         }
320     }
321     }
322
323     /**
324      * The action associated with the 'Remove' button
325      */

326     protected class RemoveButtonAction extends AbstractAction JavaDoc {
327     public void actionPerformed(ActionEvent JavaDoc e) {
328         int index = mediaList.getSelectedIndex();
329         mediaList.clearSelection();
330         if (index >= 0) {
331         listModel.removeElementAt(index);
332         }
333     }
334     }
335
336     /**
337      * The action associated with the 'Clear' button
338      */

339     protected class ClearButtonAction extends AbstractAction JavaDoc {
340     public void actionPerformed(ActionEvent JavaDoc e) {
341         mediaList.clearSelection();
342         listModel.removeAllElements();
343     }
344     }
345
346     /**
347      * To manage selection modifications
348      */

349     protected class MediaListSelectionListener
350     implements ListSelectionListener JavaDoc {
351
352     public void valueChanged(ListSelectionEvent JavaDoc e) {
353         updateButtons();
354     }
355     }
356
357     /**
358      * To manage data modifications in the media list.
359      */

360     protected class MediaListDataListener implements ListDataListener JavaDoc {
361     
362     public void contentsChanged(ListDataEvent JavaDoc e) {
363         updateButtons();
364     }
365     
366     public void intervalAdded(ListDataEvent JavaDoc e) {
367         updateButtons();
368     }
369     
370     public void intervalRemoved(ListDataEvent JavaDoc e) {
371         updateButtons();
372     }
373     }
374
375     ///////////////////////////////////////////////////////////////////////////
376

377     /**
378      * A dialog to add a new CSS medium.
379      */

380     public static class AddMediumDialog extends JDialog JavaDoc implements ActionMap {
381
382     /**
383      * The return value if 'OK' is chosen.
384      */

385     public final static int OK_OPTION = 0;
386     
387     /**
388      * The return value if 'Cancel' is chosen.
389      */

390     public final static int CANCEL_OPTION = 1;
391
392     /**
393      * The new medium.
394      */

395     protected JComboBox JavaDoc medium;
396     
397     /**
398      * The return code.
399      */

400     protected int returnCode;
401
402     /**
403      * Constructs a new AddMediumDialog.
404      *
405      * @param parent the parent of this dialog
406      */

407     public AddMediumDialog(Component JavaDoc parent) {
408         super(JOptionPane.getFrameForComponent(parent),
409           resources.getString("AddMediumDialog.title"));
410         setModal(true);
411
412         listeners.put("OKButtonAction", new OKButtonAction());
413         listeners.put("CancelButtonAction", new CancelButtonAction());
414
415         getContentPane().add(createContentPanel(), BorderLayout.CENTER);
416         getContentPane().add(createButtonsPanel(), BorderLayout.SOUTH);
417     }
418
419     /**
420      * Returns the medium that might be added or null if any.
421      */

422     public String JavaDoc getMedium() {
423         return (String JavaDoc)medium.getSelectedItem();
424     }
425
426     /**
427      * Returns the panel to enter a new CSS medium.
428      */

429     protected Component JavaDoc createContentPanel() {
430         JPanel JavaDoc panel = new JPanel JavaDoc(new BorderLayout JavaDoc());
431         panel.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
432         panel.add(new JLabel JavaDoc(resources.getString("AddMediumDialog.label")),
433               BorderLayout.WEST);
434
435         medium = new JComboBox JavaDoc();
436         medium.setEditable(true);
437         String JavaDoc media = resources.getString("Media.list");
438         StringTokenizer JavaDoc tokens = new StringTokenizer JavaDoc(media, " ");
439         while (tokens.hasMoreTokens()) {
440         medium.addItem(tokens.nextToken());
441         }
442         panel.add(medium, BorderLayout.CENTER);
443         return panel;
444     }
445
446     /**
447      * Returns the button panel.
448      */

449     protected Component JavaDoc createButtonsPanel() {
450         JPanel JavaDoc panel = new JPanel JavaDoc(new FlowLayout JavaDoc(FlowLayout.RIGHT));
451         ButtonFactory bf = new ButtonFactory(bundle, this);
452         panel.add(bf.createJButton("OKButton"));
453         panel.add(bf.createJButton("CancelButton"));
454         return panel;
455     }
456
457     /**
458      * Returns the code that describes how the dialog has been closed (OK or
459      * CANCEL).
460      */

461     public int getReturnCode() {
462         return returnCode;
463     }
464
465     /**
466      * The map that contains the listeners
467      */

468     protected Map JavaDoc listeners = new HashMap JavaDoc();
469     
470     /**
471      * Returns the action associated with the given string or null on error
472      *
473      * @param key the key mapped with the action to get
474      * @throws MissingListenerException if the action is not found
475      */

476     public Action JavaDoc getAction(String JavaDoc key) throws MissingListenerException {
477         return (Action JavaDoc)listeners.get(key);
478     }
479
480     /**
481      * The action associated with the 'OK' button
482      */

483     protected class OKButtonAction extends AbstractAction JavaDoc {
484         public void actionPerformed(ActionEvent JavaDoc e) {
485         returnCode = OK_OPTION;
486         dispose();
487         }
488     }
489     
490     /**
491      * The action associated with the 'Cancel' button
492      */

493     protected class CancelButtonAction extends AbstractAction JavaDoc {
494         public void actionPerformed(ActionEvent JavaDoc e) {
495         returnCode = CANCEL_OPTION;
496         dispose();
497         }
498     }
499     }
500
501     ///////////////////////////////////////////////////////////////////////////
502

503     /**
504      * A dialog to edit/add/remove CSS media.
505      */

506     public static class Dialog extends JDialog JavaDoc implements ActionMap {
507     
508     /**
509      * The return value if 'OK' is chosen.
510      */

511     public final static int OK_OPTION = 0;
512     
513     /**
514      * The return value if 'Cancel' is chosen.
515      */

516     public final static int CANCEL_OPTION = 1;
517     
518     /**
519      * The return code.
520      */

521     protected int returnCode;
522
523     /**
524      * Constructs a new Dialog to edit/add/remove CSS media.
525      */

526     public Dialog() {
527         this(null, "", "");
528     }
529
530     /**
531      * Constructs a new Dialog to edit/add/remove CSS media.
532      *
533      * @param parent the parent of this dialog
534      * @param title the title of this dialog
535      * @param mediaList the media list
536      */

537     public Dialog(Component JavaDoc parent, String JavaDoc title, List JavaDoc mediaList) {
538         super(JOptionPane.getFrameForComponent(parent), title);
539
540         listeners.put("OKButtonAction", new OKButtonAction());
541         listeners.put("CancelButtonAction", new CancelButtonAction());
542
543         CSSMediaPanel panel = new CSSMediaPanel();
544         panel.setMedia(mediaList);
545         getContentPane().add(panel, BorderLayout.CENTER);
546         getContentPane().add(createButtonsPanel(), BorderLayout.SOUTH);
547     }
548
549     /**
550      * Constructs a new Dialog to edit/add/remove CSS media.
551      *
552      * @param parent the parent of this dialog
553      * @param title the title of this dialog
554      * @param media the media list
555      */

556     public Dialog(Component JavaDoc parent, String JavaDoc title, String JavaDoc media) {
557         super(JOptionPane.getFrameForComponent(parent), title);
558
559         listeners.put("OKButtonAction", new OKButtonAction());
560         listeners.put("CancelButtonAction", new CancelButtonAction());
561
562         CSSMediaPanel panel = new CSSMediaPanel();
563         panel.setMedia(media);
564         getContentPane().add(panel, BorderLayout.CENTER);
565         getContentPane().add(createButtonsPanel(), BorderLayout.SOUTH);
566     }
567     
568     /**
569      * Returns the code that describes how the dialog has been closed (OK or
570      * CANCEL).
571      */

572     public int getReturnCode() {
573         return returnCode;
574     }
575     
576     /**
577      * Creates the OK/Cancel buttons panel
578      */

579     protected JPanel JavaDoc createButtonsPanel() {
580         JPanel JavaDoc p = new JPanel JavaDoc(new FlowLayout JavaDoc(FlowLayout.RIGHT));
581         ButtonFactory bf = new ButtonFactory(bundle, this);
582         p.add(bf.createJButton("OKButton"));
583         p.add(bf.createJButton("CancelButton"));
584         return p;
585     }
586
587     /**
588      * The map that contains the listeners
589      */

590     protected Map JavaDoc listeners = new HashMap JavaDoc();
591     
592     /**
593      * Returns the action associated with the given string or null on error
594      *
595      * @param key the key mapped with the action to get
596      * @throws MissingListenerException if the action is not found
597      */

598     public Action JavaDoc getAction(String JavaDoc key) throws MissingListenerException {
599         return (Action JavaDoc)listeners.get(key);
600     }
601
602     /**
603      * The action associated with the 'OK' button
604      */

605     protected class OKButtonAction extends AbstractAction JavaDoc {
606         public void actionPerformed(ActionEvent JavaDoc e) {
607         returnCode = OK_OPTION;
608         dispose();
609         }
610     }
611     
612     /**
613      * The action associated with the 'Cancel' button
614      */

615     protected class CancelButtonAction extends AbstractAction JavaDoc {
616         public void actionPerformed(ActionEvent JavaDoc e) {
617         returnCode = CANCEL_OPTION;
618         dispose();
619         }
620     }
621     }
622
623     /**
624      * Main - debug -
625      */

626     public static void main(String JavaDoc [] args) {
627     String JavaDoc media = "all aural braille embossed handheld print projection screen tty tv";
628     int code = CSSMediaPanel.showDialog(null, "Test", media);
629     System.out.println(code);
630     System.exit(0);
631     }
632 }
633
Popular Tags