View Javadoc

1   package fr.ove.openmath.mathematica;
2   
3   import javax.swing.*;
4   import javax.swing.border.*;
5   import javax.swing.event.*;
6   import java.awt.*;
7   import java.awt.event.*;
8   import java.io.*;
9   import java.util.*;
10  import fr.ove.utils.Connection;
11  import fr.ove.utils.ConnectionConfigurable;
12  
13  /***
14  * A connection parameter setter.
15  */
16  public class ConnectionHandlerDialog extends JDialog {
17      /***
18      * The connection.
19      */
20      private Connection connection; 
21      
22      /***
23      * A connection configurable dialog.
24      */
25      private ConnectionConfigDialog connectionConfigDialog;
26      
27      /***
28      * A message status for the connection.
29      */
30      private JLabel status;
31      
32      /***
33      * The open connection button.
34      */
35      private JButton buttonConnect;
36      
37      /***
38      * The close connection button.
39      */
40      private JButton buttonDisconnect;
41      
42      /***
43      * The configure connection button.
44      */
45      private JButton buttonConfig;
46      
47      /***
48      * The connexion satus : online
49      */
50      private ImageIcon online;
51      
52      /***
53      * The connexion satus : offline
54      */
55      private ImageIcon offline;
56      
57      private JFrame frame;
58      
59      private StatusBar statusBar;
60      
61      /***
62      * The Constructor.
63      * @param configurableObj a connection configurable object.
64      */
65      public ConnectionHandlerDialog(JFrame parentFrame, StatusBar statusBar) {
66          super(parentFrame, "Connection Handler", true);
67          
68          frame = parentFrame;
69          this.statusBar = statusBar;
70          
71          connectionConfigDialog = new ConnectionConfigDialog(frame, null);
72          
73          JPanel contentPane = new JPanel(new GridBagLayout());
74          
75          EtchedBorder border = (EtchedBorder) BorderFactory.createEtchedBorder();
76          contentPane.setBorder(border);
77          
78          online = getIcon("fr/ove/openmath/mathematica/images/online.gif");
79          offline = getIcon("fr/ove/openmath/mathematica/images/offline.gif");
80          
81  		GridBagConstraints constraints = new GridBagConstraints();
82  		constraints.weightx = 1.0;
83  		constraints.weighty = 1.0;
84  		constraints.fill = GridBagConstraints.HORIZONTAL;
85  		
86          status = new JLabel();
87          status.setIconTextGap(15);
88          status.setHorizontalAlignment(SwingConstants.CENTER);
89          constraints.gridwidth = 3;
90          constraints.ipadx = 40;
91          constraints.ipady = 40;
92          contentPane.add(status, constraints);
93          
94          buttonConnect = new JButton("Connect");
95          buttonConnect.setToolTipText("Open the connection");
96          buttonConnect.addActionListener(
97              new ActionListener() {
98                  public void actionPerformed(ActionEvent ae) {
99                      try {
100                         connection.open();
101                         ConnectionHandlerDialog.this.statusBar.setConnectionStatus(true);
102                         String text = "Connected to [" + connection.getHostName() + ", " +
103                                         connection.getPortNumber() + "]";
104                         status.setText(text);
105                         status.setIcon(online);
106                         
107                         buttonConnect.setEnabled(false);
108                         buttonDisconnect.setEnabled(true);
109                         buttonConfig.setEnabled(false);
110                     }
111                     catch (IOException ioe) {
112                         String errorMsg = "failed to open a connection to [" + 
113                                         connection.getHostName() + ", " + connection.getPortNumber() +
114                                         "]";
115                         JOptionPane jop = new JOptionPane(errorMsg, JOptionPane.ERROR_MESSAGE);
116                         JDialog dialog = jop.createDialog(frame, "Connection Failed");
117                         dialog.show();
118                     }
119                 }
120             }
121         );
122         constraints.gridwidth = 1;
123         constraints.gridy = 1;
124         constraints.ipadx = 0;
125         constraints.ipady = 0;
126         constraints.insets = new Insets(0, 10, 10, 2);
127         contentPane.add(buttonConnect, constraints);
128         
129         buttonDisconnect = new JButton("Disconnect");
130         buttonDisconnect.setToolTipText("Close the connection");
131         buttonDisconnect.addActionListener(
132             new ActionListener() {
133                 public void actionPerformed(ActionEvent ae) {
134                     try {
135                         connection.close();
136                         ConnectionHandlerDialog.this.statusBar.setConnectionStatus(false);
137                         String text = "Disconnected from [" + connection.getHostName() + ", " +
138                                         connection.getPortNumber() + "]";
139                         status.setText(text);
140                         status.setIcon(offline);
141                         
142                         buttonConnect.setEnabled(true);
143                         buttonDisconnect.setEnabled(false);
144                         buttonConfig.setEnabled(true);
145                     }
146                     catch (IOException ioe) {
147                     }
148                 }
149             }
150         );
151         constraints.gridx = 1;
152         constraints.insets = new Insets(0, 0, 10, 0);
153         contentPane.add(buttonDisconnect, constraints);
154         
155         buttonConfig = new JButton("Configure");
156         buttonConfig.setToolTipText("Configure the connection");
157         buttonConfig.addActionListener(
158             new ActionListener() {
159                 public void actionPerformed(ActionEvent ae) {
160                     connectionConfigDialog.setConnectionConfigurable(connection);
161                     connectionConfigDialog.setLocationRelativeTo(ConnectionHandlerDialog.this);
162                     connectionConfigDialog.show();
163                     String text = "Disconnected from [" + connection.getHostName() + ", " +
164                             connection.getPortNumber() + "]";
165                     status.setText(text);
166                 }
167             }
168         );
169         constraints.gridx = 2;
170         constraints.insets = new Insets(2, 0, 10, 10);
171         contentPane.add(buttonConfig, constraints);
172         
173 		setContentPane(contentPane);
174     }
175     
176     /***
177     * sets the connection to handle.
178     * @param connection the connection to handle.
179     */
180     public void setConnection(Connection connection) {
181         this.connection = connection;
182     }
183     
184     /***
185     * Shows the dialog
186     */
187     public void show() {
188         if (connection.isOpened()) {
189             String text = "Connected to [" + connection.getHostName() + ", " +
190                             connection.getPortNumber() + "]";
191             status.setText(text);
192             status.setIcon(online);
193             
194             buttonConnect.setEnabled(false);
195             buttonDisconnect.setEnabled(true);
196             buttonConfig.setEnabled(false);
197         }
198         else {
199             String text = "Disconnected from [" + connection.getHostName() + ", " +
200                             connection.getPortNumber() + "]";
201             status.setText(text);
202             status.setIcon(offline);
203             
204             buttonConnect.setEnabled(true);
205             buttonDisconnect.setEnabled(false);
206             buttonConfig.setEnabled(true);
207         }
208         
209         pack();
210         setLocationRelativeTo(frame);
211         super.show();
212     }
213     
214     /***
215     * Returns the icon with the specified image filename.
216     * @param filename the image filename
217     */
218     private ImageIcon getIcon(String filename) {
219         ImageIcon icon = null;
220         
221         java.net.URL iconURL = ClassLoader.getSystemResource(filename);
222         if (iconURL != null)
223             icon = new ImageIcon(iconURL);
224             
225         return icon;
226     }
227 }