1 package fr.ove.openmath.mathematica;
2
3 import java.awt.*;
4 import java.awt.event.*;
5 import javax.swing.*;
6 import javax.swing.event.*;
7 import javax.swing.border.*;
8 import fr.ove.utils.Connection;
9
10 /***
11 * A Status Bar
12 */
13 public class StatusBar extends JPanel implements ChangeListener {
14 /***
15 * The status to display.
16 */
17 private JLabel status;
18
19 /***
20 * The connection status.
21 */
22 private JLabel connStatus;
23
24 /***
25 * A blank icon.
26 */
27 private ImageIcon blank;
28
29 /***
30 * The connexion satus : online
31 */
32 private ImageIcon online;
33
34 /***
35 * The connexion satus : offline
36 */
37 private ImageIcon offline;
38
39 /***
40 * A connection handler dialog
41 */
42 private ConnectionHandlerDialog connectionHandlerDialog;
43
44 /***
45 * The constructor.
46 */
47 public StatusBar(JFrame parent) {
48 super(new GridBagLayout());
49
50 connectionHandlerDialog = new ConnectionHandlerDialog(parent, this);
51
52 BevelBorder border = new BevelBorder(BevelBorder.LOWERED);
53
54 GridBagConstraints constraints = new GridBagConstraints();
55 constraints.weightx = 1.0;
56 constraints.anchor = GridBagConstraints.WEST;
57 constraints.fill = GridBagConstraints.HORIZONTAL;
58 constraints.ipady = 2;
59
60 blank = getIcon("fr/ove/openmath/mathematica/images/blank.gif");
61 status = new JLabel(blank);
62 status.setHorizontalAlignment(JLabel.LEFT);
63 status.setBorder(border);
64 status.setIconTextGap(5);
65 add(status, constraints);
66
67 constraints.gridx = 1;
68 constraints.weightx = 0.0;
69 constraints.anchor = GridBagConstraints.CENTER;
70 constraints.fill = GridBagConstraints.HORIZONTAL;
71 constraints.ipadx = 10;
72
73 connStatus = new JLabel(blank);
74 connStatus.setToolTipText("");
75 connStatus.addMouseListener(
76 new MouseAdapter() {
77 public void mousePressed(MouseEvent me) {
78 if (!connStatus.getToolTipText().equals("")) {
79 connectionHandlerDialog.show();
80 }
81 }
82 }
83 );
84 connStatus.setHorizontalAlignment(JLabel.CENTER);
85 connStatus.setBorder(border);
86 add(connStatus, constraints);
87
88 online = getIcon("fr/ove/openmath/mathematica/images/online.gif");
89 offline = getIcon("fr/ove/openmath/mathematica/images/offline.gif");
90 }
91
92 /***
93 * Sets the text for the status.
94 * @param the tex for the status.
95 */
96 public void setStatus(String text) {
97 status.setText(text);
98 status.setHorizontalAlignment(JLabel.LEFT);
99 }
100
101 /***
102 * Sets the status.
103 */
104 public void setStatus(String text, ImageIcon icon) {
105 status.setText(text);
106 status.setIcon(icon);
107 status.setHorizontalAlignment(JLabel.LEFT);
108 }
109
110 /***
111 * Clears the status.
112 */
113 public void clear() {
114 setStatus("", blank);
115 }
116
117 /***
118 * Sets the connection status.
119 * @param state <CODE>true</CODE> if state. <CODE>false</CODE> otherwise.
120 */
121 public void setConnectionStatus(boolean state) {
122 if (state) {
123 connStatus.setIcon(online);
124 connStatus.setToolTipText("Connected to the server");
125 }
126 else {
127 connStatus.setIcon(offline);
128 connStatus.setToolTipText("Not connected to the server");
129 }
130 }
131
132 /***
133 * Returns the icon with the specified image filename.
134 * @param filename the image filename
135 */
136 private ImageIcon getIcon(String filename) {
137 ImageIcon icon = null;
138
139 java.net.URL iconURL = ClassLoader.getSystemResource(filename);
140 if (iconURL != null)
141 icon = new ImageIcon(iconURL);
142
143 return icon;
144 }
145
146
147
148 /***
149 * Invoked when the target of the listener has changed its state.
150 * @param e a ChangeEvent object
151 */
152 public void stateChanged(ChangeEvent e) {
153 WorkBook workBook = (WorkBook) e.getSource();
154
155 if (workBook.getTabCount() == 0) {
156 clear();
157 connStatus.setIcon(blank);
158 connStatus.setToolTipText("");
159 }
160 else {
161 setStatus("", getIcon("fr/ove/openmath/mathematica/images/worksheet.gif"));
162
163 JInternalFrame jif = workBook.getActiveInternalFrame();
164 OMWorkSheet workSheet = (OMWorkSheet) jif.getContentPane();
165 Connection connection = workSheet.getConnection();
166
167 setConnectionStatus(connection.isOpened());
168 connectionHandlerDialog.setConnection(connection);
169 }
170 }
171 }