00001 package cz.vutbr.fit.dudka.SGVis.Visual;
00002
00003 import java.awt.event.ActionEvent;
00004 import java.awt.event.ActionListener;
00005 import java.util.ArrayList;
00006
00007 import javax.swing.Timer;
00008 import javax.swing.event.ChangeEvent;
00009 import javax.swing.event.ChangeListener;
00010
00016 class StatusManager {
00020 public static int DEF_TIMEOUT = 2500;
00021
00022 private String statusText;
00023 private boolean statusErr;
00024 private ArrayList<ChangeListener> statusListeners;
00025 private final GraphView gview;
00026 private int timeout;
00027 private Timer timer;
00028
00029 public StatusManager(GraphView gview) {
00030 this.gview = gview;
00031 this.timeout = DEF_TIMEOUT;
00032 this.timer = null;
00033 statusListeners = new ArrayList<ChangeListener>();
00034 }
00035
00039 public synchronized int getTimeout() {
00040 return timeout;
00041 }
00042
00047 public synchronized void setTimeout(int timeout) {
00048 this.timeout = timeout;
00049 }
00050
00055 public synchronized void addListener(ChangeListener listener) {
00056 this.statusListeners.add(listener);
00057 }
00058
00062 public synchronized void setStatus() {
00063 this.destroyTimer();
00064 this.setStatus(" ");
00065 }
00066
00071 public synchronized void setStatus(String status) {
00072 this.destroyTimer();
00073 this.setStatus(status, false);
00074 }
00075
00080 public synchronized void showStatus(String status) {
00081 this.showStatus(status, this.timeout);
00082 }
00083
00089 public synchronized void showStatus(String status, int timeout) {
00090 this.destroyTimer();
00091 this.setStatus(status);
00092 this.initTimer(timeout);
00093 }
00094
00099 public synchronized void showErrror(String text) {
00100 this.destroyTimer();
00101 this.setStatus(text, true);
00102 }
00103
00107 public synchronized String getStatus() {
00108 return this.statusText;
00109 }
00110
00114 public synchronized boolean isStatusError() {
00115 return this.statusErr;
00116 }
00117
00118 private synchronized void setStatus(String status, boolean err) {
00119 this.statusText = status;
00120 this.statusErr = err;
00121 for (ChangeListener listener: statusListeners)
00122 listener.stateChanged(new ChangeEvent(gview));
00123 }
00124 private synchronized void destroyTimer() {
00125 if (null==timer)
00126 return;
00127 timer.stop();
00128 timer = null;
00129 }
00130 private synchronized void initTimer(int timeout) {
00131 final StatusManager sm = this;
00132 ActionListener timerListener = new ActionListener() {
00133 public void actionPerformed(ActionEvent e) {
00134 sm.setStatus();
00135 }
00136 };
00137 timer = new Timer(timeout, timerListener);
00138 timer.start();
00139 }
00140 }