KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > mobilitools > util > gui > FormFrame


1 /*
2 * MobiliTools: an implementation of the Object Management Group's
3 * Mobile Agent Facility specification.
4 * Copyright (C) 2003 France Telecom R&D
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 * MobiliTools $Name: $
21 *
22 * Contact: mobilitools-smi@lists.debian-sf.objectweb.org
23 *
24 * Authors: Bruno Dillenseger
25 */

26
27
28 package org.objectweb.mobilitools.util.gui;
29
30
31 import java.awt.*;
32 import java.awt.event.*;
33
34
35 public class FormFrame extends Frame implements ActionListener
36 {
37     Button[] boutonsBtn;
38     TextField[] fields;
39     FormListener listener; // object to call back for returning results
40

41
42     public FormFrame(String JavaDoc titre, String JavaDoc comment, String JavaDoc[] champs, String JavaDoc[] boutons)
43     {
44         this(titre, comment, champs, null, null, boutons);
45     }
46
47
48     public FormFrame(String JavaDoc titre, String JavaDoc comment, String JavaDoc[] champs, String JavaDoc[] valeurs, String JavaDoc[] boutons)
49     {
50         this(titre, comment, champs, valeurs, null, boutons);
51     }
52
53
54     public FormFrame(String JavaDoc titre, String JavaDoc comment, String JavaDoc[] champs, boolean[] secret, String JavaDoc[] boutons)
55     {
56         this(titre, comment, champs, null, secret, boutons);
57     }
58
59
60     public FormFrame(String JavaDoc titre, String JavaDoc comment, String JavaDoc[] champs, String JavaDoc[] valeurs, boolean[] secret, String JavaDoc[] boutons)
61     {
62         super(titre);
63         setLayout(new BorderLayout());
64
65         // zone des explications
66
Label texte = new Label(comment);
67         add("North", texte);
68
69         // zone des champs du formulaire
70
Panel labelPanel = new Panel(), fieldPanel = new Panel();
71         labelPanel.setLayout(new GridLayout(champs.length, 1));
72         fieldPanel.setLayout(new GridLayout(champs.length, 1));
73         fields = new TextField[champs.length];
74         for (int i=0 ; i<champs.length ; ++i)
75         {
76             labelPanel.add(new Label(champs[i]));
77             fieldPanel.add(fields[i] = new TextField());
78             if (valeurs != null && valeurs[i] != null && valeurs[i].length() > 0)
79             {
80                 fields[i].setText(valeurs[i]);
81             }
82             if (secret != null && secret[i])
83             {
84                 fields[i].setEchoChar('*');
85             }
86         }
87         add("West", labelPanel);
88         add("Center", fieldPanel);
89
90         // zone des boutons
91
Panel boutonsPnl = new Panel();
92         boutonsPnl.setLayout(new FlowLayout());
93         boutonsBtn = new Button[boutons.length];
94         for (int i=0 ; i<boutons.length ; ++i)
95         {
96             boutonsPnl.add(boutonsBtn[i] = new Button(boutons[i]));
97             boutonsBtn[i].addActionListener(this);
98         }
99         add("South", boutonsPnl);
100         addWindowListener(new OnWindowClosing());
101         pack();
102     }
103
104
105     public void run(FormListener the_listener)
106     {
107         listener = the_listener;
108         show();
109     }
110
111
112     ////////////////////////////////////////////////
113
// Implementation of interface ActionListener //
114
////////////////////////////////////////////////
115

116
117     public void actionPerformed(ActionEvent evt)
118     {
119         int i;
120         String JavaDoc[] values = new String JavaDoc[1+fields.length];
121         for (i=0 ; i<fields.length ; ++i)
122         {
123             values[i] = fields[i].getText();
124         }
125         values[i] = ((Button)evt.getSource()).getLabel();
126         listener.formResult(values);
127         dispose();
128     }
129
130
131     ///////////////////////////////////////
132
// inner WindowAdapter-derived class //
133
///////////////////////////////////////
134

135
136     class OnWindowClosing extends WindowAdapter
137     {
138         public void windowClosing(WindowEvent e)
139         {
140             if (listener != null)
141             {
142                 FormFrame.this.listener.formResult(null);
143             }
144             FormFrame.this.dispose();
145         }
146     }
147 }
148
Popular Tags