KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > dotcom > GUI


1 /*
2  * JORAM: Java(TM) Open Reliable Asynchronous Messaging
3  * Copyright (C) 2001 - ScalAgent Distributed Technologies
4  * Copyright (C) 1996 - Dyade
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.1 of the License, or 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
19  * USA.
20  *
21  * Initial developer(s): Frederic Maistre (INRIA)
22  * Contributor(s):
23  */

24 package dotcom;
25
26 import javax.swing.* ;
27 import java.awt.* ;
28 import java.awt.event.* ;
29
30 /**
31  * Defines the GUI type used to illustrate the demo.<br>
32  * General look: 3 panes, labels, radioButtons, buttons.
33  *
34  * @author Maistre Frederic
35  *
36  * @see WebOrdering
37  * @see CustomerTreatment
38  * @see InventoryTreatment
39  * @see ControlTreatment
40  * @see DeliveryTreatment
41  * @see Servers
42  */

43 class GUI {
44     
45   // reference to the calling thread
46
Servers server ;
47
48   // graphical attributes
49
JFrame mainWindow ;
50   JPanel mainPane ;
51   JPanel topPane ;
52   JPanel middlePane ;
53   JPanel bottomPane ;
54   
55   JLabel idLabel1 ;
56   JLabel idLabel2 ;
57   JLabel itemLabel1 ;
58   JLabel itemLabel2 ;
59   JLabel infoLabel ;
60   
61   // radio buttons
62
JRadioButton shoesButton ;
63   JRadioButton socksButton ;
64   JRadioButton trousersButton ;
65   JRadioButton shirtButton ;
66   JRadioButton hatButton ;
67   ButtonGroup buttonGroup ;
68   
69   // buttons
70
JButton otherButton ;
71   JButton sendButton ;
72   JButton cancelButton ;
73   JButton quitButton ;
74   JButton okButton ;
75   JButton noButton ;
76   JButton closeButton ;
77   
78   /**
79    * Creates a GUI used to order items (instanciated by WebOrdering).
80    *
81    * @param title title of the frame
82    * @param ser calling thread
83    * @param x window's top left corner x location
84    * @param y window's top left corner y location
85    */

86   GUI(String JavaDoc title, Servers ser, int x, int y) {
87
88     this.server = ser ;
89  
90     // setting the window
91
idLabel1 = new JLabel("Order number: ", SwingConstants.CENTER) ;
92     idLabel2 = new JLabel("1", SwingConstants.CENTER) ;
93     
94     shoesButton = new JRadioButton("Shoes") ;
95     shoesButton.setMnemonic(KeyEvent.VK_1) ;
96     shoesButton.setActionCommand("Shoes") ;
97     shoesButton.setSelected(true) ;
98     socksButton = new JRadioButton("Socks") ;
99     socksButton.setMnemonic(KeyEvent.VK_2) ;
100     socksButton.setActionCommand("Socks") ;
101     trousersButton = new JRadioButton("Trousers") ;
102     trousersButton.setMnemonic(KeyEvent.VK_3) ;
103     trousersButton.setActionCommand("Trousers") ;
104     shirtButton = new JRadioButton("Shirt") ;
105     shirtButton.setMnemonic(KeyEvent.VK_4) ;
106     shirtButton.setActionCommand("Shirt") ;
107     hatButton = new JRadioButton("Hat") ;
108     hatButton.setMnemonic(KeyEvent.VK_5) ;
109     hatButton.setActionCommand("Hat") ;
110     
111     buttonGroup = new ButtonGroup() ;
112     buttonGroup.add(shoesButton) ;
113     buttonGroup.add(socksButton) ;
114     buttonGroup.add(trousersButton) ;
115     buttonGroup.add(shirtButton) ;
116     buttonGroup.add(hatButton) ;
117     
118     otherButton = new JButton("Other order") ;
119     sendButton = new JButton("Send order(s)") ;
120     cancelButton = new JButton("Cancel order(s)") ;
121     quitButton = new JButton("Quit") ;
122     
123     mainPane = new JPanel() ;
124     topPane = new JPanel() ;
125     middlePane = new JPanel() ;
126     bottomPane = new JPanel() ;
127     
128     mainPane.setLayout(new GridLayout(3,1)) ;
129     topPane.setLayout(new GridLayout(1,2)) ;
130     middlePane.setLayout(new GridLayout(1,5)) ;
131     bottomPane.setLayout(new GridLayout(1,4)) ;
132   
133     topPane.add(idLabel1) ;
134     topPane.add(idLabel2) ;
135     middlePane.add(shoesButton) ;
136     middlePane.add(socksButton) ;
137     middlePane.add(trousersButton) ;
138     middlePane.add(shirtButton) ;
139     middlePane.add(hatButton) ;
140     bottomPane.add(otherButton) ;
141     bottomPane.add(sendButton);
142     bottomPane.add(cancelButton) ;
143     bottomPane.add(quitButton) ;
144     mainPane.add(topPane) ;
145     mainPane.add(middlePane) ;
146     mainPane.add(bottomPane) ;
147   
148     mainWindow = new JFrame(title) ;
149     mainWindow.getContentPane().add(mainPane) ;
150   
151     // registering a listener for the radio buttons
152
RadioButtonListener radioBListener = new RadioButtonListener(server) ;
153     shoesButton.addActionListener(radioBListener) ;
154     socksButton.addActionListener(radioBListener) ;
155     trousersButton.addActionListener(radioBListener) ;
156     shirtButton.addActionListener(radioBListener) ;
157     hatButton.addActionListener(radioBListener) ;
158     
159     /** Method called when pressing the otherButton. */
160     otherButton.addActionListener(new ActionListener() {
161       public void actionPerformed(ActionEvent e) {
162         server.otherMethod() ;
163       }
164     }) ;
165   
166     /** Method called when pressing the sendButton. */
167     sendButton.addActionListener(new ActionListener() {
168       public void actionPerformed(ActionEvent e) {
169         server.sendMethod() ;
170       }
171     }) ;
172     
173     /** Method called when pressing the cancelButton. */
174     cancelButton.addActionListener(new ActionListener() {
175       public void actionPerformed(ActionEvent e) {
176         server.cancelMethod() ;
177       }
178     }) ;
179     
180     /** Method called when pressing the quitButton. */
181     quitButton.addActionListener(new ActionListener() {
182       public void actionPerformed(ActionEvent e) {
183         server.quitMethod() ;
184       }
185     }) ;
186
187     // last settings
188
mainWindow.pack() ;
189     mainWindow.setLocation(x,y);
190   }
191   
192   /**
193    * Creates a GUI used to validate incoming OrderMessages
194    * (instanciated by CustomerTreatment, StockTreatment, ControlTreatment).
195    *
196    * @param title title of the frame
197    * @param okBut label of okButton
198    * @param noButt label of noButton
199    * @param ser calling thread
200    * @param x window's top left corner x location
201    * @param y window's top left corner y location
202    */

203   GUI(String JavaDoc title, String JavaDoc okBut, String JavaDoc noBut, Servers ser, int x, int y) {
204     
205     this.server = ser ;
206  
207     // setting the window
208
idLabel1 = new JLabel("ORDER ID: ", SwingConstants.CENTER) ;
209     idLabel2 = new JLabel();
210     itemLabel1 = new JLabel("ITEM: ", SwingConstants.CENTER) ;
211     itemLabel2 = new JLabel();
212     okButton = new JButton(okBut) ;
213     noButton = new JButton(noBut) ;
214     
215     mainPane = new JPanel() ;
216     topPane = new JPanel() ;
217     middlePane = new JPanel() ;
218     bottomPane = new JPanel() ;
219     
220     mainPane.setLayout(new GridLayout(3,1)) ;
221     topPane.setLayout(new GridLayout(1,4)) ;
222     middlePane.setLayout(new GridLayout(1,1)) ;
223     bottomPane.setLayout(new GridLayout(1,2)) ;
224   
225     topPane.add(idLabel1) ;
226     topPane.add(idLabel2) ;
227     topPane.add(itemLabel1) ;
228     topPane.add(itemLabel2) ;
229     bottomPane.add(okButton);
230     bottomPane.add(noButton);
231     mainPane.add(topPane) ;
232     mainPane.add(middlePane) ;
233     mainPane.add(bottomPane) ;
234   
235     mainWindow = new JFrame(title) ;
236     mainWindow.getContentPane().add(mainPane) ;
237   
238     /** Method called when pressing the okButton. */
239     okButton.addActionListener(new ActionListener() {
240       public void actionPerformed(ActionEvent e) {
241         server.okMethod() ;
242       }
243     }) ;
244   
245     /** Method called when pressing the noButton. */
246     noButton.addActionListener(new ActionListener() {
247       public void actionPerformed(ActionEvent e) {
248         server.noMethod() ;
249       }
250     }) ;
251
252     // last settings
253
mainWindow.pack() ;
254     mainWindow.setLocation(x,y);
255   }
256   
257   
258   
259   /**
260    * Creates a GUI used to display a message reception
261    * (instanciated by CustomerTreatement, DeliveryTreatement).
262    *
263    * @param title title of the frame
264    * @param info info to display
265    * @param ser calling thread
266    * @param x window's top left corner x location
267    * @param y window's top left corner y location
268    */

269   GUI(String JavaDoc title, String JavaDoc info, Servers ser, int x, int y) {
270     
271     this.server = ser ;
272     
273     // setting the window
274
idLabel1 = new JLabel("ORDER ID: ", SwingConstants.CENTER) ;
275     idLabel2 = new JLabel() ;
276     itemLabel1 = new JLabel("ITEM: ", SwingConstants.CENTER) ;
277     itemLabel2 = new JLabel() ;
278     infoLabel = new JLabel(info, SwingConstants.CENTER) ;
279     closeButton = new JButton("Close") ;
280     
281     mainPane = new JPanel() ;
282     topPane = new JPanel() ;
283     middlePane = new JPanel() ;
284     bottomPane = new JPanel() ;
285     
286     mainPane.setLayout(new GridLayout(3,1)) ;
287     topPane.setLayout(new GridLayout(1,4)) ;
288     middlePane.setLayout(new GridLayout(1,1)) ;
289     bottomPane.setLayout(new GridLayout(1,1)) ;
290   
291     topPane.add(idLabel1) ;
292     topPane.add(idLabel2) ;
293     topPane.add(itemLabel1) ;
294     topPane.add(itemLabel2) ;
295     middlePane.add(infoLabel) ;
296     bottomPane.add(closeButton) ;
297     mainPane.add(topPane) ;
298     mainPane.add(middlePane) ;
299     mainPane.add(bottomPane) ;
300   
301     mainWindow = new JFrame(title) ;
302     mainWindow.getContentPane().add(mainPane) ;
303   
304     /** Method called when pressing the closeButton. */
305     closeButton.addActionListener(new ActionListener() {
306       public void actionPerformed(ActionEvent e) {
307         server.closeMethod() ;
308       }
309     }) ;
310   
311     // last settings
312
mainWindow.pack() ;
313     mainWindow.setLocation(x,y);
314   }
315   
316   /**
317    * Method called to set the GUI's visibility.
318    *
319    * @param bool true or false.
320    */

321   public void setVisible(boolean bool) {
322     mainWindow.setVisible(bool) ;
323   }
324   
325   /**
326    * Method called to update idLabel2.
327    *
328    * @param id order id.
329    */

330   public void updateId(int id) {
331     idLabel2.setText(Integer.toString(id)) ;
332   }
333   
334   /**
335    * Method called to update itemLabel2.
336    *
337    * @param item item ordered.
338    */

339   public void updateItem(String JavaDoc item) {
340     itemLabel2.setText(item) ;
341   }
342 }
343  
344   
345 /**
346  * Listener getting RadioButtons selections.<br>
347  * Used by WebOrdering's GUI.
348  *
349  * @author Maistre Frederic
350  *
351  * @see GUI
352  * @see WebServer
353  */

354 class RadioButtonListener implements ActionListener {
355   /** Thread instanciating the GUI. */
356   Servers server ;
357
358   /**
359    * Creates a RadioButtonListener.
360    *
361    * @param server calling thread.
362    */

363   RadioButtonListener(Servers ser) {
364     this.server = ser ;
365   }
366
367   /**
368    * Method called when selecting a RadioButton.
369    */

370   public void actionPerformed(ActionEvent e) {
371     server.choiceMethod(e.getActionCommand()) ;
372   }
373 }
374
Popular Tags