Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/memoire/bu/BuCheckBox3States.java


1   /**
2    * @modification $Date: 2002/12/16 18:56:24 $
3    * @statut       unstable
4    * @file         BuCheckBox3States.java
5    * @version      0.36
6    * @author       Bertrand Marchand
7    * @email        guillaume@desnoix.com
8    * @license      GNU General Public License 2 (GPL2)
9    * @copyright    1998-2001 Guillaume Desnoix
10   * (c)1998-2001 Bertrand Marchand
11   */
12  
13  package com.memoire.bu;
14  
15  import com.memoire.bu.*;
16  import com.memoire.dnd.*;
17  import com.memoire.fu.*;
18  import com.memoire.re.*;
19  
20  
21  /**
22   * @creation     15/12/98
23   * @modification 05/01/99
24   * @statut       instable
25   */
26  
27  import java.awt.*;
28  import java.awt.event.*;
29  import java.util.*;
30  import javax.swing.*;
31  
32  /**
33   * Un checkbox permettant 3 états (activé, désactivé, mixte). Dans ce dernier
34   * cas, le bouton apparaît en grisé
35   * @version 0.02, 05/01/99
36   * @author Bertrand Marchand
37   */
38  public class BuCheckBox3States extends BuCheckBox
39  {
40    public static final int STATE_MIXED      = 0;                    // On reprend les status Item pour rester compatible avec la méthode
41    public static final int STATE_SELECTED   = ItemEvent.SELECTED;   // ItemEvent.getStateChanged(). On rajoute un état BuCheckBox3States.STATE_MIXED.
42    public static final int STATE_DESELECTED = ItemEvent.DESELECTED;
43  
44    private int state_;                      // Etat (SELECTED, DESELECTED, MIXED)
45    private Icon iconStateMixed = null;      // L'icon de status mixte (pourrait par la suite être issu de iconDefault).
46    private Icon iconDefault = null;         // L'icon donné par le programmeur
47    private Vector itemListenerList = null;  // Les items listener du composant
48    private boolean defaultEvent_;           // Definit si l'évenement ItemEvent provient d'une des classes mères
49  
50  
51    /**
52     * Creates an initially deselected checkbox button with no text, no icon.
53     */
54    public BuCheckBox3States () {
55      this(null, null, STATE_DESELECTED);
56    }
57  
58    /**
59     * Creates an initially deselected checkbox with an icon.
60     *
61     * @param icon  the Icon image to display
62     */
63    public BuCheckBox3States(Icon icon) {
64      this(null, icon, STATE_DESELECTED);
65    }
66  
67    /**
68     * Creates a checkbox with an icon and specifies its state.
69     *
70     * @param icon  the Icon image to display
71     * @param state an int value indicating the initial selection
72     *        state. The possible values are STATE_DESELECTED,
73     *        STATE_SELECTED, STATE_MIXED.
74     */
75    public BuCheckBox3States(Icon icon, int state) {
76      this(null, icon, state);
77    }
78  
79    /**
80     * Creates an initially deselected checkbox with text.
81     *
82     * @param text the text of the checkbox.
83     */
84    public BuCheckBox3States (String text) {
85      this(text, null, STATE_DESELECTED);
86    }
87  
88    /**
89     * Creates a checkbox with text and specifies its state.
90     *
91     * @param text the text of the checkbox.
92     * @param state an int value indicating the initial selection
93     *        state. The possible values are STATE_DESELECTED,
94     *        STATE_SELECTED, STATE_MIXED.
95     */
96    public BuCheckBox3States (String text, int state) {
97      this(text, null, state);
98    }
99  
100   /**
101    * Creates an initially deselected checkbox with
102    * the specified text and icon.
103    *
104    * @param text the text of the checkbox.
105    * @param icon  the Icon image to display
106    */
107   public BuCheckBox3States(String text, Icon icon) {
108     this(text, icon, STATE_DESELECTED);
109   }
110 
111   /**
112    * Creates a checkbox with the specified text and icon and specifies its state.
113    *
114    * @param text the text of the checkbox.
115    * @param icon  the Icon image to display
116    * @param state an int value indicating the initial selection
117    *        state. The possible values are STATE_DESELECTED,
118    *        STATE_SELECTED, STATE_MIXED.
119    */
120   public BuCheckBox3States(String text, Icon icon, int state) {
121     super(text, icon);
122     iconDefault = icon;
123     iconStateMixed = new IconStateMixed();
124     itemListenerList = new Vector();
125     defaultEvent_ = true;
126     this.setState(state);
127   }
128 
129   /**
130    * Affectation du status (SELECTED, DESELECTED, MIXED)
131    */
132   public void setState(int state) {
133     defaultEvent_ = false;
134     int vp = state_;
135     state_ = state;
136     switch (state) {
137      case STATE_SELECTED:
138       setIcon(iconDefault);
139       setSelected(true);
140       break;
141      case STATE_DESELECTED:
142       setIcon(iconDefault);
143       setSelected(false);
144       break;
145      case STATE_MIXED:
146       setIcon(iconStateMixed);
147       fireItemStateChanged(new ItemEvent(this,ItemEvent.ITEM_STATE_CHANGED,this,
148                            state));
149       fireStateChanged();
150       break;
151     }
152     defaultEvent_ = true;
153   }
154 
155   /**
156    * Retourne le status (SELECTED, DESELECTED, MIXED)
157    */
158   public int getState() {
159     return state_;
160   }
161 
162   /**
163    * adds an ItemListener to the checkbox
164    */
165   public void addItemListener(ItemListener l) {
166     itemListenerList.addElement(l);
167   }
168 
169   /**
170    * removes an ItemListener from the button
171    */
172   public void removeItemListener(ItemListener l) {
173     itemListenerList.removeElement(l);
174   }
175 
176   protected void fireItemStateChanged(ItemEvent event) {
177     // Evenement en provenance des classes mères => On ne notifie pas les listeners, mais
178     // on remodifie l'état du bouton.
179     if (defaultEvent_) {
180       setState((state_+1)%3);
181       return;
182     }
183     ItemEvent evt = new ItemEvent(this,event.getID(),event.getItem(),event.getStateChange());
184     if (itemListenerList != null)
185     for (int i=0; i<itemListenerList.size(); i++) {
186       ((ItemListener) itemListenerList.elementAt(i)).itemStateChanged(evt);
187     }
188   }
189   /**
190    * Doit être remplacé par <I>setState</I>
191    */
192 //  public void setSelected(boolean state) {
193 //    throw new RuntimeException("BuCheckBox3States : utilisez 'setState' a la place de 'setSelected'");
194 //  }
195 
196   /**
197    * Doit être remplacé par <I>getState</I>
198    */
199 //  public boolean isSelected() {
200 //    throw new RuntimeException("BuCheckBox3States : utilisez 'getState' a la place de 'isSelected'");
201 //  }
202 
203   // Classe de trace de l'icon pour un status STATE_MIXED
204 
205   private static final class IconStateMixed implements Icon {
206 
207     public int getIconHeight() {
208       return 13;
209     }
210 
211     public int getIconWidth() {
212       return 13;
213     }
214 
215     public void paintIcon(Component _c, Graphics _g, int _x, int _y) {
216       int w=getIconWidth();
217       int h=getIconHeight();
218 
219       _g.setColor(Color.lightGray);
220       _g.fillRect(_x,_y,w-2,h-1);
221       _g.setColor(Color.gray);
222       _g.drawLine(_x+w-1,_y,_x,_y);
223       _g.drawLine(_x,_y,_x,_y+h-1);
224       _g.setColor(Color.white);
225       _g.drawLine(_x,_y+h-1,_x+w-1,_y+h-1);
226       _g.drawLine(_x+w-1,_y+h-1,_x+w-1,_y);
227 
228       _g.setColor(Color.gray);
229       _g.drawLine(_x+3,_y+3,_x+w-5,_y+h-5);
230       _g.drawLine(_x+4,_y+3,_x+w-5,_y+h-6);
231       _g.drawLine(_x+3,_y+4,_x+w-6,_y+h-5);
232       _g.drawLine(_x+3,_y+h-5,_x+w-5,_y+3);
233       _g.drawLine(_x+3,_y+h-6,_x+w-6,_y+3);
234       _g.drawLine(_x+4,_y+h-5,_x+w-5,_y+4);
235     }
236   }
237 }