Source code: org/fudaa/fudaa/prert/panel/PrertPnDialog.java
1 /*
2 * @file PrertPnDialog.java
3 * @creation 2002-09-03
4 * @modification $Date: 2001/09/11 14:24:42 $
5 * @license GNU General Public License 2
6 * @copyright (c)1998-2001 CETMEF 2 bd Gambetta F-60231 Compiegne
7 * @mail devel@fudaa.org
8 */
9 package org.fudaa.fudaa.prert.panel;
10 import java.awt.*;
11 import java.awt.event.*;
12 import javax.swing.*;
13 import javax.swing.border.*;
14 import com.memoire.bu.*;
15 import org.fudaa.fudaa.prert.*;
16 /**
17 * Un panneau qui peut etre facilement affichable dans une boite de dialogue.
18 * Cette classe reprend le mode de fonctionnement de JOptionPane.
19 * Les methodes valide(), actionApply() et actionCancel() peuvent être
20 * redefinies : valide() est appele avant d'appliquer les changements
21 * ( la methode actionApply() ).
22 *
23 * @version $Id$
24 * @author Bertrand Marchand,Fred Deniger
25 */
26 public class PrertPnDialog
27 extends BuPanel
28 implements ActionListener, WindowListener
29 {
30 /**
31 * Option : uniquement un bouton ok sera affiche. Reponse donnee par le
32 * dialogue si le bouton "ok" utilise.
33 */
34 public static final int OK_OPTION = 0;
35 public static final int OK_CANCEL_OPTION = 1;
36 public static final int OK_APPLY_OPTION = 2;
37 public static final int OK_CANCEL_APPLY_OPTION = 3;
38 protected int option_;
39 protected boolean modale_;
40 protected JDialog dialog_;
41 private int response_;
42 public PrertPnDialog()
43 {
44 this(OK_CANCEL_OPTION);
45 }
46 /**
47 * Les options possibles les variables statiques <code>OK_OPTION</code>
48 * (un bouton ok unique,
49 * <code>OK_CANCEL_OPTION</code>,...
50 */
51 public PrertPnDialog(int _option)
52 {
53 option_ = _option;
54 response_ = JOptionPane.CANCEL_OPTION;
55 }
56 public void setModale(boolean _modale)
57 {
58 modale_ = _modale;
59 }
60 public boolean valide()
61 {
62 if (Prert.DEBUG)
63 System.out.println("validation debut...");
64 return true;
65 }
66 /**
67 * Affiche le panel dans une boite de dialogue.
68 * @param _parent la boite de dialogue parent
69 * @param _titre le titre de la boite de dialogue
70 */
71 public void affiche(Dialog _parent, String _titre)
72 {
73 createDialog(_parent,_titre);
74 afficheDialog(_parent);
75 }
76
77 /**
78 * Affiche le panel dans une boite de dialogue.
79 * @param _parent la fenetre parent
80 * @param _titre le titre de la boite de dialogue
81 */
82 public void affiche(Frame _parent, String _titre)
83 {
84 createDialog(_parent,_titre);
85 afficheDialog(_parent);
86 }
87
88 private int createDialog(Frame _parent, String _titre)
89 {
90 if (dialog_ != null)
91 {
92 System.err.println("Cet element est deja visualise");
93 return JOptionPane.CANCEL_OPTION;
94 }
95 dialog_ = new JDialog(_parent, _titre);
96 return JOptionPane.OK_OPTION;
97 }
98
99
100 private int createDialog(Dialog _parent, String _titre)
101 {
102 if (dialog_ != null)
103 {
104 System.err.println("Cet element est deja visualise");
105 return JOptionPane.CANCEL_OPTION;
106 }
107 dialog_ = new JDialog(_parent, _titre);
108 return JOptionPane.OK_OPTION;
109 }
110
111
112
113
114 /**
115 * Affiche le panel dans une boite de dialogue modale.
116 * @param _parent la boite de dialogue parent
117 * @param _titre le titre de la boite de dialogue
118 */
119 public int afficheModale(Dialog _parent, String _titre)
120 {
121 int r=createDialog(_parent,_titre);
122 if(r==JOptionPane.CANCEL_OPTION) return r;
123 dialog_.setModal(true);
124 return afficheDialog(_parent);
125 }
126
127 /**
128 * Affiche le panel dans une boite de dialogue modale.
129 * @param _parent la fenetre parent
130 * @param _titre le titre de la boite de dialogue
131 */
132 public int afficheModale(Frame _parent, String _titre)
133 {
134 int r=createDialog(_parent,_titre);
135 if(r==JOptionPane.CANCEL_OPTION) return r;
136 dialog_.setModal(true);
137 return afficheDialog(_parent);
138 }
139
140 public void addEmptyBorder(int _b)
141 {
142 setBorder(BorderFactory.createEmptyBorder(_b, _b, _b, _b));
143 }
144 /**
145 * Affiche le dialogue et le positionne par rapport a <code>_parent</code>.
146 */
147 private int afficheDialog(Component _parent)
148 {
149 dialog_.setModal(modale_);
150 dialog_.addWindowListener(this);
151 dialog_.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
152 dialog_.setContentPane(construitDialogPanel());
153 Dimension dParent;
154 Point pParent;
155 Dimension dThis = dialog_.getPreferredSize();
156 Point pThis = new Point();
157 if (_parent == null)
158 {
159 dParent = Toolkit.getDefaultToolkit().getScreenSize();
160 pParent = new Point(0, 0);
161 }
162 else
163 {
164 dParent = _parent.getPreferredSize();
165 pParent = _parent.getLocation();
166 }
167 pThis.x = pParent.x + (dParent.width - dThis.width) / 2;
168 pThis.y = pParent.y + (dParent.height - dThis.height) / 2;
169 dialog_.setLocation(pThis);
170 dialog_.pack();
171 dialog_.show();
172 return response_;
173 }
174 /**
175 * Construit le panel de la boite de dialogue.
176 */
177 private BuPanel construitDialogPanel()
178 {
179 BuPanel princ = new BuPanel();
180 princ.setLayout(new BuBorderLayout());
181 princ.add(this, BuBorderLayout.CENTER);
182 BuPanel pnAction = new BuPanel();
183 pnAction.setLayout(new FlowLayout(FlowLayout.RIGHT, 5, 0));
184 pnAction.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
185 switch (option_)
186 {
187 case OK_OPTION :
188 construireOk(pnAction);
189 break;
190 case OK_CANCEL_OPTION :
191 construireOk(pnAction);
192 construireCancel(pnAction);
193 break;
194 case OK_APPLY_OPTION :
195 construireOk(pnAction);
196 construireApply(pnAction);
197 break;
198 case OK_CANCEL_APPLY_OPTION :
199 construireOk(pnAction);
200 construireApply(pnAction);
201 construireCancel(pnAction);
202 break;
203 }
204 princ.add(pnAction, BuBorderLayout.SOUTH);
205 princ.doLayout();
206 return princ;
207 }
208 /**
209 * Construit un bouton ayant comme label <code>_text</code> et comme
210 * "ActionCommand" <code>_action</code>.
211 */
212 private BuButton construireBuButton(String _text, String _action)
213 {
214 BuButton r = new BuButton(_text);
215 r.setActionCommand(_action);
216 r.addActionListener(this);
217 return r;
218 }
219 private void construireOk(BuPanel _p)
220 {
221 _p.add(construireBuButton("Continuer", "OK"));
222 }
223 private void construireCancel(BuPanel _p)
224 {
225 _p.add(construireBuButton("Annuler", "CANCEL"));
226 }
227 private void construireApply(BuPanel _p)
228 {
229 _p.add(construireBuButton("Appliquer", "APPLY"));
230 }
231 /**
232 * ajoute au panel un label dont la couleur de texte est rouge.
233 */
234 protected BuLabel addRedLabel()
235 {
236 BuLabel r = new BuLabel();
237 r.setForeground(Color.red);
238 add(r);
239 return r;
240 }
241 protected BuLabel addLabel(String _l)
242 {
243 BuLabel r = new BuLabel(_l);
244 add(r);
245 return r;
246 }
247 protected BuTextField addLabelDoubleText(String _label)
248 {
249 addLabel(_label);
250 return addDoubleText();
251 }
252 protected BuTextField addLabelIntegerText(String _label)
253 {
254 addLabel(_label);
255 return addIntegerText();
256 }
257 protected BuTextField addDoubleText()
258 {
259 BuTextField r = new BuTextField(20);
260 r.setCharValidator(BuCharValidator.DOUBLE);
261 r.setStringValidator(BuStringValidator.DOUBLE);
262 r.setValueValidator(BuValueValidator.DOUBLE);
263 add(r);
264 return r;
265 }
266 protected BuTextField addDoubleText(double _d)
267 {
268 BuTextField r = addDoubleText();
269 r.setText(_d + "");
270 return r;
271 }
272 protected BuTextField addIntegerText()
273 {
274 BuTextField r = new BuTextField(20);
275 r.setCharValidator(BuCharValidator.INTEGER);
276 r.setStringValidator(BuStringValidator.INTEGER);
277 r.setValueValidator(BuValueValidator.INTEGER);
278 add(r);
279 return r;
280 }
281 protected BuTextField addIntegerText(int _d)
282 {
283 BuTextField r = addIntegerText();
284 r.setText(_d + "");
285 return r;
286 }
287 /**
288 * Recupere les evt des boutons ok,cancel et apply.
289 * bouton OK : valide(), actionApply() et ferme le dialogue (fermerDialog() )<br/>
290 * bouton cancel : actionCancel() et ferme le dialogue<br/>
291 * bouton cancel : valide(), actionApply()<br/>
292 */
293 public void actionPerformed(ActionEvent _evt)
294 {
295 String com = _evt.getActionCommand();
296 if (Prert.DEBUG)
297 System.out.println("PrertPnDialog:commande " + com);
298 if ("OK".equals(com))
299 {
300 if (valide())
301 {
302 actionApply();
303 response_ = JOptionPane.OK_OPTION;
304 fermerDialog();
305 }
306 }
307 else if ("CANCEL".equals(com))
308 {
309 actionCancel();
310 response_ = JOptionPane.CANCEL_OPTION;
311 fermerDialog();
312 }
313 else if ("APPLY".equals(com))
314 if (valide())
315 actionApply();
316 }
317 private void fermerDialog()
318 {
319 System.out.println("fermer");
320 if (dialog_ != null)
321 {
322 dialog_.dispose();
323 }
324 dialog_ = null;
325 }
326 /**
327 * maj du layout et de la boite de dialogue.
328 */
329 public void actualiseAffichage()
330 {
331 doLayout();
332 if (dialog_ != null)
333 dialog_.pack();
334 }
335 /**
336 * Méthode à surcharger : elle est appelee si le bouton APPLY ou OK est
337 * active.
338 */
339 protected void actionApply()
340 {
341 }
342 /**
343 * Méthode action pour le bouton CANCEL à surcharger.
344 */
345 protected void actionCancel()
346 {
347 }
348 public void windowActivated(WindowEvent e)
349 {
350 }
351 public void windowClosed(WindowEvent e)
352 {
353 }
354 public void windowClosing(WindowEvent e)
355 {
356 }
357 public void windowDeactivated(WindowEvent e)
358 {
359 }
360 public void windowDeiconified(WindowEvent e)
361 {
362 }
363 public void windowIconified(WindowEvent e)
364 {
365 }
366 public void windowOpened(WindowEvent e)
367 {
368 }
369 }