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

fix

refs 2061
parent 3c33983c
Branches
Tags
No related merge requests found
......@@ -23,6 +23,7 @@ import static org.eclipse.core.databinding.conversion.StringToNumberConverter.to
import static org.eclipse.core.databinding.conversion.StringToNumberConverter.toInteger;
import static org.fortiss.tooling.kernel.ui.util.DataBindingUtils.DECORATION_KEY;
import static org.fortiss.tooling.kernel.ui.util.DataBindingUtils.performComplexTextBinding;
import static org.fortiss.tooling.kernel.ui.util.WidgetsFactory.createTextWithUndo;
import org.conqat.ide.commons.ui.databinding.validate.NumberPositiveValidator;
import org.conqat.ide.commons.ui.databinding.validate.TextToDoubleValidator;
......@@ -60,7 +61,7 @@ import org.fortiss.tooling.kernel.service.ILibraryService;
* @author hoelzl
* @author $Author$
* @version $Rev$
* @ConQAT.Rating YELLOW Hash: B9304656B01A96F3DC2E818F1A8DA952
* @ConQAT.Rating YELLOW Hash: B8CA0F1AF887AD482862C108C1857261
*/
public abstract class PropertySectionBase extends AbstractPropertySection {
......@@ -159,7 +160,7 @@ public abstract class PropertySectionBase extends AbstractPropertySection {
/** Creates form text field. */
protected Text createFormText(String labelText) {
Text text = getWidgetFactory().createText(composite, "");
Text text = createTextWithUndo(getWidgetFactory(), composite, "");
createFormEntry(text, labelText);
return text;
}
......
/*--------------------------------------------------------------------------+
$Id$
| |
| Copyright 2014 ForTISS GmbH |
| |
| Licensed under the Apache License, Version 2.0 (the "License"); |
| you may not use this file except in compliance with the License. |
| You may obtain a copy of the License at |
| |
| http://www.apache.org/licenses/LICENSE-2.0 |
| |
| Unless required by applicable law or agreed to in writing, software |
| distributed under the License is distributed on an "AS IS" BASIS, |
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| See the License for the specific language governing permissions and |
| limitations under the License. |
+--------------------------------------------------------------------------*/
package org.fortiss.tooling.kernel.ui.util;
import java.util.Stack;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.widgets.Text;
/**
* Hack class to implement undo/redo key shortcuts for swt Text fields.
*
* @author aravantinos
* @author $Author$
* @version $Rev$
* @ConQAT.Rating YELLOW Hash: 914426506C210C61997138D714B7B4EA
*/
public class UndoRedoImpl implements KeyListener, ModifyListener {
/**
* Encapsulation of the Undo and Redo stack(s).
*/
private static class UndoRedoStack<T> {
private Stack<T> undo;
private Stack<T> redo;
public UndoRedoStack() {
undo = new Stack<T>();
redo = new Stack<T>();
}
public void pushUndo(T delta) {
undo.add(delta);
}
public void pushRedo(T delta) {
redo.add(delta);
}
public T popUndo() {
T res = undo.pop();
return res;
}
public T popRedo() {
T res = redo.pop();
return res;
}
public void clearRedo() {
redo.clear();
}
public boolean hasUndo() {
return !undo.isEmpty();
}
public boolean hasRedo() {
return !redo.isEmpty();
}
}
private static class StringAndPosition {
public String string;
public int position;
public StringAndPosition(String str, int pos) {
string = str;
position = pos;
}
}
private Text editor;
private UndoRedoStack<StringAndPosition> stack;
private boolean isUndo;
private boolean isRedo;
private StringAndPosition previousTextAndPosition;
/**
* Creates a new instance of this class. Automatically starts listening to
* corresponding key and modify events coming from the given
* <var>editor</var>.
*
* @param editor
* the text field to which the Undo-Redo functionality should be
* added
*/
public UndoRedoImpl(Text editor) {
editor.addModifyListener(this);
editor.addKeyListener(this);
this.editor = editor;
stack = new UndoRedoStack<StringAndPosition>();
}
private void storeTextAndPos() {
previousTextAndPosition = new StringAndPosition(editor.getText(), editor.getSelection().x);
}
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.
*/
public void keyPressed(KeyEvent e) {
storeTextAndPos();
if((e.stateMask & SWT.MOD1) > 0 && !((e.stateMask & SWT.ALT) > 0)) {
boolean isShift = (e.stateMask & SWT.SHIFT) > 0;
if(!isShift && e.keyCode == 'y' || isShift && e.keyCode == 'z') {
redo();
}
}
}
/*
* 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.
*/
public void keyReleased(KeyEvent e) {
if((e.stateMask & SWT.MOD1) > 0 && !((e.stateMask & SWT.ALT) > 0)) {
boolean isShift = (e.stateMask & SWT.SHIFT) > 0;
if(!isShift && e.keyCode == 'z') {
undo();
} else if(isShift && e.keyCode == 'z') {
redo();
}
}
}
/**
* Creates a corresponding Undo or Redo step from the given event and pushes
* it to the stack. The Redo stack is, logically, emptied if the event comes
* from a normal user action.
*
* @param event
* @see org.eclipse.swt.custom.ExtendedModifyListener#modifyText(org.eclipse.
* swt.custom.ExtendedModifyEvent)
*/
public void modifyText(ModifyEvent event) {
if(isUndo) {
stack.pushRedo(previousTextAndPosition);
} else { // is Redo or a normal user action
stack.pushUndo(previousTextAndPosition);
if(!isRedo) {
stack.clearRedo();
}
}
}
/**
* Performs the Undo action. A new corresponding Redo step is automatically
* pushed to the stack.
*/
private void undo() {
if(stack.hasUndo()) {
storeTextAndPos();
isUndo = true;
setTextAndPos(stack.popUndo());
isUndo = false;
}
}
/**
* Performs the Redo action. A new corresponding Undo step is automatically
* pushed to the stack.
*/
private void redo() {
if(stack.hasRedo()) {
storeTextAndPos();
isRedo = true;
setTextAndPos(stack.popRedo());
isRedo = false;
}
}
}
......@@ -15,7 +15,7 @@ $Id: codetemplates.xml 1 2011-01-01 00:00:01Z hoelzl $
| See the License for the specific language governing permissions and |
| limitations under the License. |
+--------------------------------------------------------------------------*/
package org.fortiss.tooling.base.ui.utils;
package org.fortiss.tooling.kernel.ui.util;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.FontMetrics;
......@@ -24,6 +24,7 @@ import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Spinner;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.forms.widgets.FormToolkit;
/**
* Factory class for easier creation of different widgets.
......@@ -31,7 +32,7 @@ import org.eclipse.swt.widgets.Text;
* @author ratiu
* @author $Author: hoelzl $
* @version $Rev: 18709 $
* @ConQAT.Rating GREEN Hash: 4DC463FB75AF7997C1D245EFF5E3C5D5
* @ConQAT.Rating YELLOW Hash: 7C250F948EE999BACED7B389BF926573
*/
public class WidgetsFactory {
......@@ -73,6 +74,7 @@ public class WidgetsFactory {
*/
public static Text createText(Composite parent, int style, String initialText, int charsNumber) {
Text text = new Text(parent, style);
new UndoRedoImpl(text);
text.setText(initialText);
GC gc = new GC(text);
FontMetrics fm = gc.getFontMetrics();
......@@ -83,4 +85,11 @@ public class WidgetsFactory {
parent.pack();
return text;
}
/** Substitute to FormToolkit.createText in order to add undo/redo functionality. */
public static Text createTextWithUndo(FormToolkit toolkit, Composite parent, String value) {
Text res = toolkit.createText(parent, value);
new UndoRedoImpl(res);
return res;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment