Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
MatrixStatusBar.java
1 
16 package net.squiz.matrix.ui;
17 
18 import javax.swing.*;
19 import java.awt.*;
20 import java.util.*;
21 import java.awt.event.*;
22 import net.squiz.matrix.core.*;
23 import javax.swing.border.*;
24 
25 
31 public class MatrixStatusBar {
32 
33  private static java.util.List elements = new ArrayList();
34  private static MatrixStatusBarElement[] elementsArr;
35 
36  /* the text that was last set globally */
37  private static String statusText = "";
38 
39  // cannot instantiate
40  private MatrixStatusBar() {}
41 
46  public static MatrixStatusBarElement createStatusBar() {
47  MatrixStatusBarElement element = new MatrixStatusBarElement(statusText);
48  synchronized(elements) {
49  elements.add(element);
50  }
51  return element;
52  }
53 
61  private static MatrixStatusBarElement[] getElements() {
62  synchronized(elements) {
63  if (elementsArr == null) {
64  elementsArr = (MatrixStatusBarElement[]) elements.toArray(
65  new MatrixStatusBarElement[elements.size()]);
66  }
67  return elementsArr;
68  }
69  }
70 
76  public static void setStatus(String status) {
77  statusText = status;
78  MatrixStatusBarElement[] elements = getElements();
79  for (int i = 0; i < elements.length; i++)
80  elements[i].setStatus(status);
81  }
82 
87  public static void clearStatus() {
88  setStatus("");
89  }
90 
97  public static String getStatus() {
98  return statusText;
99  }
100 
109  public static void setStatusAndClear(String status, int time) {
110  // dont call MatrixStatusBarElement.setStatusAndClear as it will
111  // create an action listener and timer for all status bars and cause
112  // undesirable results.
113  ActionListener listener = new ActionListener() {
114  public void actionPerformed(ActionEvent evt) {
115  setStatus("");
116  }
117  };
118  setStatus(status);
119  javax.swing.Timer t = new javax.swing.Timer(time, listener);
120  t.setRepeats(false);
121  t.start();
122  }
123 
130  private static class MatrixStatusBarElement extends JPanel
131  implements MatrixConstants, MouseListener {
132 
133  private JLabel label;
134  private Spinner spinner;
135  private javax.swing.Timer clearTimer;
136 
137  // only MatrixStatusBar should be able to create individual elements
138  private MatrixStatusBarElement(String status) {
139  setLayout(new FlowLayout(FlowLayout.LEFT));
140 
141  spinner = new Spinner();
142  label = new JLabel(statusText);
143 
144  label.setFont(PLAIN_FONT_10);
145 
146  add(spinner);
147  add(label);
148 
149  setBackground(UIManager.getColor("StatusBar.background"));
150 
151  // create an action and a timer to clear the status
152  // which will also stop the spinner
153  ActionListener listener = new ActionListener() {
154  public void actionPerformed(ActionEvent evt) {
155  setStatus("");
156  }
157  };
158  clearTimer = new javax.swing.Timer(1000, listener);
159  clearTimer.setRepeats(false);
160  addMouseListener(this);
161  }
162 
168  private void setStatus(String status) {
169  if (status.equals("") && spinner.isStarted())
170  spinner.stop();
171  else if ((!status.equals("")) && !spinner.isStarted())
172  spinner.start();
173  label.setText(status);
174  }
175 
180  private void clearStatus() {
181  setStatus("");
182  }
183 
192  private void setStatusAndClear(String status, int time) {
193  setStatus(status);
194  clearTimer.setDelay(time);
195  clearTimer.start();
196  }
197 
203  private String getStatus() {
204  return label.getText();
205  }
206 
207  public void mouseEntered(MouseEvent evt) {}
208  public void mouseExited(MouseEvent evt) {}
209  public void mousePressed(MouseEvent evt) {}
210  public void mouseReleased(MouseEvent evt) {}
211 
212  public void mouseClicked(MouseEvent evt) {
213  if (evt.getClickCount() != 2)
214  return;
215  /*Runtime rt = Runtime.getRuntime();
216  long preFree = rt.freeMemory();
217  rt.gc();
218  long postFree = rt.freeMemory();
219  long freed = (postFree - preFree) / 1000;
220  GUIUtilities.error(freed + "Kb Released", "Memory Released");*/
221  net.squiz.matrix.debug.Log.openLogs();
222  }
223  }
224 }