View Javadoc

1   /*
2    * AI Soccer Project - network gaming environment for AI warriors.
3    * Copyright (C) 2001-2004  Marcin Werla, Pawel Widera
4    *
5    * This program is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU General Public License
7    * as published by the Free Software Foundation; either version 2
8    * of the License, or (at your option) any later version.
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with this program; if not, you can find it here:
17   * http://www.gnu.org/licenses/gpl.html
18   */
19  
20  package aigames.soccer.observer;
21  
22  import java.awt.BorderLayout;
23  import java.awt.Component;
24  import java.awt.Dimension;
25  import java.net.InetAddress;
26  import java.net.UnknownHostException;
27  
28  import javax.swing.Box;
29  import javax.swing.BoxLayout;
30  import javax.swing.ButtonGroup;
31  import javax.swing.ImageIcon;
32  import javax.swing.JList;
33  import javax.swing.JTabbedPane;
34  import javax.swing.ListSelectionModel;
35  
36  
37  /***
38   * The observer options tabbed pane class.
39   * @version $Id: OptionsPane.java,v 1.11 2004/05/17 22:26:39 momat Exp $
40   */
41  public class OptionsPane extends JTabbedPane {
42      private static java.awt.Color darkestGreen = new java.awt.Color(0, 170, 0);
43      private static java.awt.Color darkGreen = new java.awt.Color(0, 187, 0);
44      private static java.awt.Color lightGreen = new java.awt.Color(0, 221, 0);
45      private static java.awt.Color lightestGreen = new java.awt.Color(0, 238, 0);
46      private boolean connected;
47      private boolean selected;
48      private javax.swing.JPanel jPanel = null;
49      private javax.swing.JLabel jLabel = null;
50      private javax.swing.JTextField jTextField = null;
51      private javax.swing.JLabel jLabel1 = null;
52      private javax.swing.JTextField jTextField1 = null;
53      private javax.swing.JButton jButton = null;
54      private javax.swing.JPanel jPanel1 = null;
55      private javax.swing.JScrollPane jScrollPane = null;
56      private javax.swing.JPanel jPanel2 = null;
57      private javax.swing.JList jList = null;
58      private javax.swing.DefaultListModel listModel = null;
59      private javax.swing.JButton jButton1 = null;
60      private javax.swing.JPanel jPanel3 = null;
61      private javax.swing.ButtonGroup buttonGroup = null;
62      private javax.swing.JRadioButton jRadioButton = null;
63      private javax.swing.JRadioButton jRadioButton1 = null;
64      private javax.swing.JPanel jPanel4 = null;
65      private javax.swing.JLabel jLabel2 = null;
66  
67      /***
68       * This is the default constructor.
69       */
70      public OptionsPane() {
71          super();
72          initialize();
73          setConnected(false);
74          setSelected(false);
75      }
76  
77      /***
78       * This method initializes this.
79       */
80      private void initialize() {
81          this.setTabPlacement(JTabbedPane.BOTTOM);
82          this.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
83          this.addTab("Server", null, getJPanel(), null);
84          this.addTab("Games", null, getJPanel1(), null);
85          this.addTab("View", null, getJPanel3(), null);
86  
87          Dimension size = this.getPreferredSize();
88          this.setBounds(0, 0, size.width, size.height);
89          this.setBackground(darkGreen);
90      }
91  
92      /***
93       * This method initializes jPanel.
94       * @return javax.swing.JPanel
95       */
96      private javax.swing.JPanel getJPanel() {
97          if (jPanel == null) {
98              jPanel = new javax.swing.JPanel();
99              jPanel.setLayout(new BoxLayout(jPanel, BoxLayout.PAGE_AXIS));
100             jPanel.add(getJLabel(), null);
101             jPanel.add(getJTextField(), null);
102             jPanel.add(Box.createRigidArea(new Dimension(0, 5)));
103             jPanel.add(getJLabel1(), null);
104             jPanel.add(getJTextField1(), null);
105             jPanel.add(Box.createRigidArea(new Dimension(0, 10)));
106             jPanel.add(getJButton(), null);
107             jPanel.setBackground(lightGreen);
108         }
109 
110         return jPanel;
111     }
112 
113     /***
114      * This method initializes jTextField.
115      * @return javax.swing.JTextField
116      */
117     private javax.swing.JTextField getJTextField() {
118         if (jTextField == null) {
119             jTextField = new javax.swing.JTextField();
120             String localIP;
121 			try {
122 				localIP = InetAddress.getLocalHost().getHostAddress();
123 			} catch (UnknownHostException e) {
124 				localIP = "127.0.0.1";
125 			}
126 			jTextField.setText(localIP);
127             jTextField.setHorizontalAlignment(javax.swing.JTextField.CENTER);
128 
129             Dimension size = jTextField.getPreferredSize();
130             size.width += 20;
131             jTextField.setMaximumSize(size);
132             jTextField.setAlignmentX(Component.CENTER_ALIGNMENT);
133         }
134 
135         return jTextField;
136     }
137 
138     /***
139      * This method initializes jLabel.
140      * @return javax.swing.JLabel
141      */
142     private javax.swing.JLabel getJLabel() {
143         if (jLabel == null) {
144             jLabel = new javax.swing.JLabel();
145             jLabel.setText("server IP");
146             jLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
147         }
148 
149         return jLabel;
150     }
151 
152     /***
153      * This method initializes jLabel1.
154      * @return javax.swing.JLabel
155      */
156     private javax.swing.JLabel getJLabel1() {
157         if (jLabel1 == null) {
158             jLabel1 = new javax.swing.JLabel();
159             jLabel1.setText("server port");
160             jLabel1.setAlignmentX(Component.CENTER_ALIGNMENT);
161         }
162 
163         return jLabel1;
164     }
165 
166     /***
167      * This method initializes jTextField1.
168      * @return javax.swing.JTextField
169      */
170     private javax.swing.JTextField getJTextField1() {
171         if (jTextField1 == null) {
172             jTextField1 = new javax.swing.JTextField();
173             jTextField1.setText("9669");
174             jTextField1.setHorizontalAlignment(javax.swing.JTextField.CENTER);
175 
176             Dimension size = jTextField1.getPreferredSize();
177             size.width += 20;
178             jTextField1.setMaximumSize(size);
179             jTextField1.setAlignmentX(Component.CENTER_ALIGNMENT);
180         }
181 
182         return jTextField1;
183     }
184 
185     /***
186      * This method initializes jButton.
187      * @return javax.swing.JButton
188      */
189     private javax.swing.JButton getJButton() {
190         if (jButton == null) {
191             jButton = new javax.swing.JButton();
192             jButton.setText("Disconnect");
193             jButton.setBackground(darkGreen);
194             jButton.setAlignmentX(Component.CENTER_ALIGNMENT);
195 
196             Dimension size = jButton.getPreferredSize();
197             size.height -= 5;
198             jButton.setMaximumSize(size);
199             jButton.addActionListener(new java.awt.event.ActionListener() {
200                     public void actionPerformed(java.awt.event.ActionEvent e) {
201                         firePropertyChange("connected", connected, !connected);
202                     }
203                 });
204         }
205 
206         return jButton;
207     }
208 
209     /***
210      * This method initializes jPanel1.
211      * @return javax.swing.JPanel
212      */
213     private javax.swing.JPanel getJPanel1() {
214         if (jPanel1 == null) {
215             jPanel1 = new javax.swing.JPanel();
216             jPanel1.setLayout(new BorderLayout());
217             jPanel1.add(getJScrollPane(), BorderLayout.CENTER);
218             jPanel1.add(getJPanel2(), BorderLayout.SOUTH);
219             jPanel1.setBackground(lightGreen);
220         }
221 
222         return jPanel1;
223     }
224 
225     /***
226      * This method initializes jScrollPane.
227      * @return javax.swing.JScrollPane
228      */
229     private javax.swing.JScrollPane getJScrollPane() {
230         if (jScrollPane == null) {
231             jScrollPane = new javax.swing.JScrollPane();
232             jScrollPane.setViewportView(getJList());
233         }
234 
235         return jScrollPane;
236     }
237 
238     /***
239      * This method initializes jPanel2.
240      * @return javax.swing.JPanel
241      */
242     private javax.swing.JPanel getJPanel2() {
243         if (jPanel2 == null) {
244             jPanel2 = new javax.swing.JPanel();
245             jPanel2.add(getJButton1(), null);
246             jPanel2.setBackground(lightGreen);
247         }
248 
249         return jPanel2;
250     }
251 
252     /***
253      * This method initializes jList.
254      * @return javax.swing.JList
255      */
256     private javax.swing.JList getJList() {
257         if (jList == null) {
258             jList = new javax.swing.JList();
259             jList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
260             jList.setLayoutOrientation(JList.VERTICAL);
261             jList.setVisibleRowCount(4);
262 
263             listModel = new javax.swing.DefaultListModel();
264 
265             //@TODO use status bar for such messages
266             listModel.addElement("Reading games...");
267             jList.setModel(listModel);
268             jList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
269                     public void valueChanged(
270                         javax.swing.event.ListSelectionEvent e) {
271                         jButton1.setEnabled(true);
272                     }
273                 });
274         }
275 
276         return jList;
277     }
278 
279     /***
280      * This method initializes jButton1.
281      * @return javax.swing.JButton
282      */
283     private javax.swing.JButton getJButton1() {
284         if (jButton1 == null) {
285             jButton1 = new javax.swing.JButton();
286             jButton1.setText("Select");
287             jButton1.setBackground(darkGreen);
288             jButton1.setAlignmentX(Component.CENTER_ALIGNMENT);
289 
290             Dimension size = jButton1.getPreferredSize();
291             size.height -= 5;
292             jButton1.setMaximumSize(size);
293             jButton1.addActionListener(new java.awt.event.ActionListener() {
294                     public void actionPerformed(java.awt.event.ActionEvent e) {
295                         firePropertyChange("selected", true, selected);
296                     }
297                 });
298         }
299 
300         return jButton1;
301     }
302 
303     /***
304      * This method initializes jPanel3.
305      * @return javax.swing.JPanel
306      */
307     private javax.swing.JPanel getJPanel3() {
308         if (jPanel3 == null) {
309             jPanel3 = new javax.swing.JPanel();
310             jPanel3.setLayout(new BorderLayout());
311             jPanel3.add(getJPanel4(), BorderLayout.NORTH);
312             jPanel3.add(getJLabel2(), BorderLayout.CENTER);
313             jPanel3.setBackground(lightGreen);
314         }
315 
316         return jPanel3;
317     }
318 
319     /***
320      * This method initializes jPanel4.
321      * @return javax.swing.JPanel
322      */
323     private javax.swing.JPanel getJPanel4() {
324         if (jPanel4 == null) {
325             jPanel4 = new javax.swing.JPanel();
326             buttonGroup = new ButtonGroup();
327             buttonGroup.add(getJRadioButton());
328             buttonGroup.add(getJRadioButton1());
329             jPanel4.add(getJRadioButton(), null);
330             jPanel4.add(getJRadioButton1(), null);
331             jPanel4.setBackground(lightGreen);
332         }
333 
334         return jPanel4;
335     }
336 
337     /***
338      * This method initializes jLabel2.
339      * @return javax.swing.JLabel
340      */
341     private javax.swing.JLabel getJLabel2() {
342         if (jLabel2 == null) {
343             jLabel2 = new javax.swing.JLabel();
344 
345             ImageIcon icon = new ImageIcon("/src/xdocs/aisoccer.jpg");
346             jLabel2.setIcon(icon);
347         }
348 
349         return jLabel2;
350     }
351 
352     /***
353      * This method initializes jRadioButton.
354      * @return javax.swing.JRadioButton
355      */
356     private javax.swing.JRadioButton getJRadioButton() {
357         if (jRadioButton == null) {
358             jRadioButton = new javax.swing.JRadioButton();
359             jRadioButton.setText("horizontal");
360             jRadioButton.setSelected(true);
361             jRadioButton.setBackground(lightGreen);
362         }
363 
364         return jRadioButton;
365     }
366 
367     /***
368      * This method initializes jRadioButton1.
369      * @return javax.swing.JRadioButton
370      */
371     private javax.swing.JRadioButton getJRadioButton1() {
372         if (jRadioButton1 == null) {
373             jRadioButton1 = new javax.swing.JRadioButton();
374             jRadioButton1.setText("vertical");
375             jRadioButton1.setBackground(lightGreen);
376         }
377 
378         return jRadioButton1;
379     }
380 
381     /***
382      * Connected property getter.
383      * @return true if an observer is connected to a server
384      */
385     public boolean isConnected() {
386         return connected;
387     }
388 
389     /***
390      * Connected property setter.
391      * @param b
392      */
393     public void setConnected(boolean b) {
394         connected = b;
395 
396         if (b) {
397             this.setEnabledAt(1, true);
398             jButton.setText("Disonnect");
399         } else {
400             this.setEnabledAt(1, false);
401             jButton.setText("Connect");
402             setSelected(false);
403             this.setSelectedIndex(0);
404         }
405     }
406 
407     /***
408      * Selected property getter.
409      * @return true if a game is selected
410      */
411     public boolean isSelected() {
412         return selected;
413     }
414 
415     /***
416      * Selected property setter.
417      * @param b
418      */
419     public void setSelected(boolean b) {
420         selected = b;
421 
422         if (b) {
423             this.setEnabledAt(2, true);
424             this.setSelectedIndex(2);
425             this.setEnabledAt(1, false);
426         } else {
427             this.setEnabledAt(2, false);
428             jButton1.setEnabled(false);
429             jList.clearSelection();
430         }
431     }
432 
433     /***
434      * Setter for a list of games on a server.
435      * @param list
436      */
437     public void setGamesList(String[] list) {
438         listModel.clear();
439 
440         for (int i = 0; i < list.length; ++i) {
441             listModel.addElement(list[i]);
442         }
443 
444         this.setSelectedIndex(1);
445     }
446 
447     /***
448      * Getter for the selected name of a game.
449      * @return String gameName
450      */
451     public String getGame() {
452         return (String) jList.getSelectedValue();
453     }
454 
455     /***
456     * Getter for the server ip address.
457     * @return String ip
458     */
459     public String getIp() {
460         return jTextField.getText();
461     }
462 
463     /***
464      * Getter for the server port.
465      * @return int port
466      */
467     public int getPort() {
468         return Integer.parseInt(jTextField1.getText());
469     }
470 }