Skip to content
Snippets Groups Projects
Commit e0ffd3c3 authored by Vincent Aravantinos's avatar Vincent Aravantinos
Browse files

YELLOW

refs 2093
parent f4d5c17a
No related branches found
No related tags found
No related merge requests found
......@@ -28,77 +28,107 @@ import org.eclipse.swt.widgets.Text;
/**
* Hack class to implement undo/redo key shortcuts for swt Text fields.
* TODO: yeah looks like tha warnings swarming all over the place => RED as hell fire
*
* @author aravantinos
* @author $Author$
* @version $Rev$
* @ConQAT.Rating YELLOW Hash: C6CFC71F876775529808379C84FC2615
* @ConQAT.Rating YELLOW Hash: 6B5E3B3D3FF977AAB4287A8F4FA17C12
*/
public class UndoRedoImpl implements KeyListener, ModifyListener {
/**
* Encapsulation of the Undo and Redo stack(s).
*/
/** Encapsulation of the generic undo and redo stack(s). */
private static class UndoRedoStack<T> {
/** Generic undo stack. */
private Stack<T> undo;
/** Generic redo stack. */
private Stack<T> redo;
/** Constructor. */
public UndoRedoStack() {
undo = new Stack<T>();
redo = new Stack<T>();
}
/** Push on the undo stack. */
public void pushUndo(T delta) {
undo.add(delta);
}
/** Push on the redo stack. */
public void pushRedo(T delta) {
redo.add(delta);
}
/** Pop from the undo stack. */
public T popUndo() {
T res = undo.pop();
return res;
}
/** Pop from the redo stack. */
public T popRedo() {
T res = redo.pop();
return res;
}
/** Clear the redo stack. */
public void clearRedo() {
redo.clear();
}
/** Test if the undo stack is not empty. */
public boolean hasUndo() {
return !undo.isEmpty();
}
/** Test if the redo stack is not empty. */
public boolean hasRedo() {
return !redo.isEmpty();
}
}
/**
* Class encapsulating a string and a cursor position.
* This is what is going to be stored in the {@link UndoRedoStack}.
*
* @author aravantinos
* @author $Author$
* @version $Rev$
* @ConQAT.Rating RED Hash:
*/
private static class StringAndPosition {
/** String content. */
public String string;
/** Cursor position. */
public int position;
/** Constructor. */
public StringAndPosition(String str, int pos) {
string = str;
position = pos;
}
}
/** Text widget for which the undo/redo feature is implemented. */
private Text editor;
/** The undo/redo stack. */
private UndoRedoStack<StringAndPosition> stack;
/** Flag to know if we are in the course of applying an undo. */
private boolean isUndo;
/** Flag to know if we are in the course of applying a redo. */
private boolean isRedo;
/**
* To store the latest string and position (useful when we are in the course of applying an
* undo/redo and the current string and position in the widget do not represent a "valid"
* value).
*/
private StringAndPosition previousTextAndPosition;
/**
......@@ -118,19 +148,22 @@ public class UndoRedoImpl implements KeyListener, ModifyListener {
stack = new UndoRedoStack<StringAndPosition>();
}
/** Stores the information from the widget to the previousTextAndPosition attribute. */
private void storeTextAndPos() {
previousTextAndPosition = new StringAndPosition(editor.getText(), editor.getSelection().x);
}
/** Updates the text and cursor position of the widget according to the argument. */
private void setTextAndPos(StringAndPosition x) {
editor.setText(x.string);
editor.setSelection(x.position);
}
/*
/**
* We use keyPressed for Redo and keyReleased for Undo, because, for some reason, Ctrl-z is
* captured only at release and Ctrl-y (resp. Ctrl-Shift-y) only at press.
*/
@Override
public void keyPressed(KeyEvent e) {
storeTextAndPos();
if((e.stateMask & SWT.MOD1) > 0 && !((e.stateMask & SWT.ALT) > 0)) {
......@@ -141,10 +174,11 @@ public class UndoRedoImpl implements KeyListener, ModifyListener {
}
}
/*
/**
* We use keyPressed for Redo and keyReleased for Undo, because, for some reason, Ctrl-z is
* captured only at release and Ctrl-y (resp. Ctrl-Shift-y) only at press.
*/
@Override
public void keyReleased(KeyEvent e) {
if((e.stateMask & SWT.MOD1) > 0 && !((e.stateMask & SWT.ALT) > 0)) {
boolean isShift = (e.stateMask & SWT.SHIFT) > 0;
......@@ -165,6 +199,7 @@ public class UndoRedoImpl implements KeyListener, ModifyListener {
* @see org.eclipse.swt.custom.ExtendedModifyListener#modifyText(org.eclipse.
* swt.custom.ExtendedModifyEvent)
*/
@Override
public void modifyText(ModifyEvent event) {
if(isUndo) {
stack.pushRedo(previousTextAndPosition);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment