View Javadoc

1   package fr.ove.palette.swing;
2   
3   import javax.swing.*;
4   import javax.swing.event.*;
5   import java.awt.*;
6   import java.awt.event.*;
7   import java.util.*;
8   import fr.ove.palette.*;
9   import fr.ove.palette.events.*;
10  
11  /***
12  * The palette.<BR>
13  * The palette throws @see PaletteEvent to the registered @see PaletteEventListener.<BR>
14  * These events contain a @see PaletteInfo object which contains relevant information for
15  * the listeners.
16  * <P>
17  * The palette can be made of objects which have the <CODE>addActionListener(ActionListener l)</CODE>.
18  * Only objects such as buttons are relevant in a palette, but if you want to add other kind of objects
19  * having the <CODE>addActionListener(ActionListener l)</CODE> method, what you have to do is the
20  * something like what is following:
21  * <UL>
22  * <LI>Palette palette = new Palette(...);</LI>
23  * <LI>MyStuff stuff = new MyStuff(...);  // Which extends java.awt.Component</LI>
24  * <LI>palette.getCorrespondance().put(stuff, new PalettreInfo(...));</LI>
25  * <LI>stuff.addActionListener(palette.getActionListener()); // MyStuff must implement this methods (at least)
26  * to send ActionEvent to objects which want to be informed of actions on it (such as button, ... see before)</LI>
27  * <LI>palette.add(stuff);</LI>
28  * </UL>
29  * Or you can subclass this class and implement your own </CODE>addStuffToPalette(...)</CODE> doing the
30  * given pice of code.<BR>
31  *
32  * @author © 1999 DIRAT Laurent
33  * @version 1.0  19/09/1999
34  */
35  public class Palette extends JPanel {
36      /***
37      * The <CODE>ActionListener</CODE> of each button in the palette.<BR>
38      * It is in charge of throwing an event with the <CODE>PaletteInfo</CODE>
39      * corresponding to the button pressed, to the listener(s) of the palette.
40      */
41      private static ActionListener actionListener;
42      
43      /***
44      * The correspondance between the button pressed in the palette and the information to send
45      * to the listener(s) of the palette.<BR>
46      * This hashtable has as key the button in the palette and as value, the @see PaletteInfo
47      * corresponding to the button.
48      */
49      private Hashtable correspondance = new Hashtable();
50      
51      /***
52      * The list of listeners of the palette
53      */
54      private Vector listeners = new Vector();
55      
56      /***
57      * The contstructor.
58      * @param constraint the constraint (<CODE>PaletteLayout.ROW_CONSTRAINT</CODE> or
59      * <CODE>PaletteLayout.COLUMN_CONSTRAINT</CODE>) for ordering the components in the palette.
60      * @param nbElement the number max of element allowed by row (resp. column) with the 
61      * <CODE>PaletteLayout.ROW_CONSTRAINT</CODE> (resp. PaletteLayout.<CODE>COLUMN_CONSTRAINT</CODE>) constraint.
62      */
63      public Palette(byte constraint, int nbElement) {
64          super();
65          setLayout(new PaletteLayout(constraint, nbElement));
66          
67          actionListener = new ActionListener() {
68              public void actionPerformed(ActionEvent e) {
69                  Object src = e.getSource();
70                  PaletteEvent paletteEvent = new PaletteEvent(src);
71                  paletteEvent.setPaletteInfo((PaletteInfo) correspondance.get(src));
72                  firePaletteEvent(paletteEvent);
73              }
74          };
75      }
76      
77      /***
78      * Adds a button into the palette with the specified information (@see PaletteInfo).
79      * @param button the button to add into the palette.
80      * @param paletteInfo the specified palette information
81      */
82      public void addButtonToPalette(JButton button, PaletteInfo paletteInfo) {
83          add(button);
84          button.addActionListener(actionListener);
85          correspondance.put(button, paletteInfo);
86      }
87      
88      /***
89      * Returns the correspondances (i.e. the hastable) of the stuff in the palette and their
90      * associated relevant information to send to the listener of the instance.
91      */
92      public Hashtable getCorrespondance() {
93          return correspondance;
94      }
95      
96      /***
97      * Returns the @see ActionListener share by the different instance of <CODE>Palette</CODE> which
98      * is in charge of throwing @see PaletteEvent to all the listeners.
99      */
100     public ActionListener getActionListener() {
101         return actionListener;
102     }
103     
104     /***
105     * Registers another listener to be informed of changes of the palette.
106     * @param paletteListener a listener to add.
107     */
108     public void addPaletteListener(PaletteListener paletteListener) {
109         listeners.addElement(paletteListener);
110     }
111 
112     /***
113     * Removes a listener.
114     * @param paletteListener the listener to remove.
115     */
116     public void removePaletteListener(PaletteListener paletteListener) {
117         listeners.removeElement(paletteListener);
118     }
119 
120     /***
121     * Fires a PaletteEvent event to registered listeners.
122     * @param paletteEvent event encapsulating relevant information.
123     */
124     public void firePaletteEvent(PaletteEvent paletteEvent) {
125         for (int i = 0; i < listeners.size(); i++)
126           ((PaletteListener) listeners.elementAt(i)).consumePaletteEvent(paletteEvent);
127     }
128 }