001: /**
002: * History.java
003: * Created on 23.02.2003, 0:27:33 Alex
004: * Package: net.sf.memoranda
005: *
006: * @author Alex V. Alishevskikh, alex@openmechanics.net
007: * Copyright (c) 2003 Memoranda Team. http://memoranda.sf.net
008: */package net.sf.memoranda;
009:
010: import java.awt.event.ActionEvent;
011: import java.awt.event.KeyEvent;
012:
013: import java.util.Vector;
014: import java.text.DateFormat;
015: import java.text.SimpleDateFormat;
016: import java.util.Date;
017:
018: import javax.swing.AbstractAction;
019: import javax.swing.Action;
020: import javax.swing.ImageIcon;
021: import javax.swing.KeyStroke;
022:
023: import net.sf.memoranda.util.Local;
024:
025: /**
026: *
027: */
028: /*$Id: History.java,v 1.7 2006/10/31 15:34:14 hglas Exp $*/
029: public class History {
030:
031: static Vector _list = new Vector();
032: static int p = -1;
033: static Vector historyListeners = new Vector();
034: static Object next = null;
035: static Object prev = null;
036:
037: public static void add(HistoryItem item) {
038: if (prev != null)
039: if (item.equals(prev))
040: return;
041: if (p < _list.size() - 1)
042: _list.setSize(p + 1);
043: _list.add(item);
044: p = _list.size() - 1;
045: if (p > 0)
046: prev = _list.get(p - 1);
047: else
048: prev = null;
049: next = null;
050: historyBackAction.update();
051: historyForwardAction.update();
052: /*System.out.println();
053: for (int i = 0; i < _list.size(); i++)
054: System.out.println(((HistoryItem)_list.get(i)).getDate().toString());
055: System.out.println(item.getDate().toShortString()+ " added");*/
056: if (_list.size() > 99)
057: _list.remove(0);
058: }
059:
060: public static HistoryItem rollBack() {
061: Object n = prev;
062: if (p > 1) {
063: p--;
064: prev = _list.get(p - 1);
065: } else if (p > 0) {
066: p--;
067: prev = null;
068: } else
069: prev = null;
070: if (p < _list.size() - 1)
071: next = _list.get(p + 1);
072: else
073: next = null;
074: return (HistoryItem) n;
075: }
076:
077: public static HistoryItem rollForward() {
078: Object n = next;
079: if (p < _list.size() - 1) {
080: p++;
081: if (p == 1)
082: p++;
083: next = _list.get(p);
084: } else
085: next = null;
086: if (p > 0)
087: prev = _list.get(p - 1);
088: else
089: prev = null;
090: return (HistoryItem) n;
091: }
092:
093: public static boolean canRollBack() {
094: return prev != null;
095: }
096:
097: public static boolean canRollForward() {
098: return next != null;
099: }
100:
101: public static void addHistoryListener(HistoryListener hl) {
102: historyListeners.add(hl);
103: }
104:
105: public static void removeProjectHistory(Project prj) {
106: Vector list = new Vector();
107: String id;
108:
109: for (int i = 0; i < _list.size(); i++) {
110: id = (((HistoryItem) _list.elementAt(i)).getProject())
111: .getID();
112: if (id.equals(prj.getID())) {
113: list.add(_list.elementAt(i));
114: p--;
115: if (_list.elementAt(i).equals(prev)) {
116: if (p > 0)
117: prev = _list.get(p - 1);
118: else
119: prev = null;
120: }
121: }
122: }
123: if (!list.isEmpty()) {
124: _list.removeAll(list);
125: if (p < 0) {
126: p = 0;
127: }
128: _list.setSize(p);
129: next = null;
130: historyBackAction.update();
131: historyForwardAction.update();
132: }
133: }
134:
135: private static void notifyListeners(HistoryItem n) {
136: for (int i = 0; i < historyListeners.size(); i++)
137: ((HistoryListener) historyListeners.get(i))
138: .historyWasRolledTo(n);
139: }
140:
141: public static HistoryBackAction historyBackAction = new HistoryBackAction();
142: public static HistoryForwardAction historyForwardAction = new HistoryForwardAction();
143:
144: static class HistoryBackAction extends AbstractAction {
145:
146: public HistoryBackAction() {
147: super (
148: Local.getString("History back"),
149: new ImageIcon(
150: net.sf.memoranda.ui.AppFrame.class
151: .getResource("resources/icons/hist_back.png")));
152: putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(
153: KeyEvent.VK_LEFT, KeyEvent.ALT_MASK));
154: setEnabled(false);
155: }
156:
157: public void actionPerformed(ActionEvent e) {
158: notifyListeners(rollBack());
159: update();
160: historyForwardAction.update();
161: }
162:
163: /*public boolean isEnabled() {
164: return canRollBack();
165: }*/
166:
167: void update() {
168: if (canRollBack()) {
169: setEnabled(true);
170:
171: SimpleDateFormat sdf = new SimpleDateFormat();
172: sdf = (SimpleDateFormat) DateFormat
173: .getDateInstance(DateFormat.SHORT);
174: Date date = ((HistoryItem) prev).getDate().getDate();
175: putValue(Action.SHORT_DESCRIPTION, Local
176: .getString("Back to")
177: + " " + sdf.format(date));
178:
179: // putValue(Action.SHORT_DESCRIPTION, Local.getString("Back to") + " " + ((HistoryItem) prev).getDate().toString());
180: } else {
181: setEnabled(false);
182: putValue(Action.SHORT_DESCRIPTION, Local
183: .getString("Back"));
184: }
185: }
186: }
187:
188: static class HistoryForwardAction extends AbstractAction {
189:
190: public HistoryForwardAction() {
191: super (
192: Local.getString("History forward"),
193: new ImageIcon(
194: net.sf.memoranda.ui.AppFrame.class
195: .getResource("resources/icons/hist_forward.png")));
196: putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(
197: KeyEvent.VK_RIGHT, KeyEvent.ALT_MASK));
198: setEnabled(false);
199: }
200:
201: public void actionPerformed(ActionEvent e) {
202: notifyListeners(rollForward());
203: update();
204: historyBackAction.update();
205: }
206:
207: /*public boolean isEnabled() {
208: return canRollForward();
209: }*/
210:
211: void update() {
212: if (canRollForward()) {
213: setEnabled(true);
214:
215: SimpleDateFormat sdf = new SimpleDateFormat();
216: sdf = (SimpleDateFormat) DateFormat
217: .getDateInstance(DateFormat.SHORT);
218: Date date = ((HistoryItem) next).getDate().getDate();
219:
220: putValue(Action.SHORT_DESCRIPTION,
221: // Local.getString("Forward to") + " " + ((HistoryItem) next).getDate().toString());
222: Local.getString("Forward to") + " "
223: + sdf.format(date));
224: } else {
225: setEnabled(false);
226: putValue(Action.SHORT_DESCRIPTION, Local
227: .getString("Forward"));
228: }
229: }
230: }
231:
232: }
|