|
Added
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2005 IBM Corporation and others. |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* http://www.eclipse.org/legal/epl-v10.html |
| 7 |
* |
| 8 |
* Contributors: |
| 9 |
* IBM Corporation - initial API and implementation |
| 10 |
*******************************************************************************/ |
| 11 |
package org.eclipse.jface.internal.databinding.swt; |
| 12 |
|
| 13 |
import org.eclipse.jface.internal.provisional.databinding.ChangeEvent; |
| 14 |
import org.eclipse.jface.internal.provisional.databinding.UpdatableValue; |
| 15 |
import org.eclipse.swt.SWT; |
| 16 |
import org.eclipse.swt.custom.StyledText; |
| 17 |
import org.eclipse.swt.events.KeyEvent; |
| 18 |
import org.eclipse.swt.events.KeyListener; |
| 19 |
import org.eclipse.swt.events.VerifyEvent; |
| 20 |
import org.eclipse.swt.events.VerifyListener; |
| 21 |
import org.eclipse.swt.widgets.Event; |
| 22 |
import org.eclipse.swt.widgets.Listener; |
| 23 |
|
| 24 |
/** |
| 25 |
* @since 3.2 |
| 26 |
* |
| 27 |
*/ |
| 28 |
public class StyledTextUpdatableValue extends UpdatableValue { |
| 29 |
|
| 30 |
private final StyledText text; |
| 31 |
|
| 32 |
private boolean updating = false; |
| 33 |
|
| 34 |
private int updatePolicy; |
| 35 |
|
| 36 |
private String bufferedValue; |
| 37 |
|
| 38 |
private Listener validateListener = new Listener() { |
| 39 |
public void handleEvent(Event event) { |
| 40 |
if (!updating) { |
| 41 |
fireChangeEvent(ChangeEvent.CHANGE, null, text.getText()); |
| 42 |
} |
| 43 |
} |
| 44 |
}; |
| 45 |
|
| 46 |
private Listener updateListener = new Listener() { |
| 47 |
public void handleEvent(Event event) { |
| 48 |
if (!updating) { |
| 49 |
String oldValue = bufferedValue; |
| 50 |
String newValue = text.getText(); |
| 51 |
|
| 52 |
// If we are updating on focus lost then when we fire the change event change the buffered value |
| 53 |
if (updatePolicy == SWT.FocusOut){ |
| 54 |
bufferedValue = text.getText(); |
| 55 |
if (!oldValue.equals(newValue)) { |
| 56 |
fireChangeEvent(ChangeEvent.CHANGE, null, text.getText()); |
| 57 |
} |
| 58 |
} else { |
| 59 |
fireChangeEvent(ChangeEvent.CHANGE, null, text.getText()); |
| 60 |
} |
| 61 |
} |
| 62 |
} |
| 63 |
}; |
| 64 |
|
| 65 |
private VerifyListener verifyListener; |
| 66 |
|
| 67 |
private KeyListener keyListener; |
| 68 |
|
| 69 |
/** |
| 70 |
* @param text |
| 71 |
* @param updatePolicy |
| 72 |
*/ |
| 73 |
public StyledTextUpdatableValue(final StyledText text, int updatePolicy) { |
| 74 |
this.text = text; |
| 75 |
this.updatePolicy = updatePolicy; |
| 76 |
if (updatePolicy != SWT.None) { |
| 77 |
text.addListener(updatePolicy, updateListener); |
| 78 |
} |
| 79 |
// If the update policy is TIME_EARLY then the model is notified of changed on key stroke by key stroke |
| 80 |
// When escape is pressed we need to rollback to the previous value which is done with a key listener, however |
| 81 |
// the bufferedValue (the last remembered change value) must be changed on focus lost |
| 82 |
if(updatePolicy == SWT.Modify){ |
| 83 |
text.addListener(SWT.FocusOut, new Listener(){ |
| 84 |
public void handleEvent(Event event){ |
| 85 |
if(!updating){ |
| 86 |
bufferedValue = text.getText(); |
| 87 |
} |
| 88 |
} |
| 89 |
}); |
| 90 |
} |
| 91 |
verifyListener = new VerifyListener() { |
| 92 |
public void verifyText(VerifyEvent e) { |
| 93 |
if (!updating) { |
| 94 |
String currentText = StyledTextUpdatableValue.this.text.getText(); |
| 95 |
String newText = currentText.substring(0, e.start) + e.text |
| 96 |
+ currentText.substring(e.end); |
| 97 |
ChangeEvent changeEvent = fireChangeEvent( |
| 98 |
ChangeEvent.VERIFY, currentText, newText); |
| 99 |
if (changeEvent.getVeto()) { |
| 100 |
e.doit = false; |
| 101 |
} |
| 102 |
} |
| 103 |
} |
| 104 |
}; |
| 105 |
text.addVerifyListener(verifyListener); |
| 106 |
keyListener = new KeyListener(){ |
| 107 |
public void keyPressed(KeyEvent e) { |
| 108 |
if(e.character == SWT.ESC && bufferedValue != null){ |
| 109 |
// Revert the value in the text field to the model value |
| 110 |
text.setText(bufferedValue); |
| 111 |
} |
| 112 |
} |
| 113 |
public void keyReleased(KeyEvent e) { |
| 114 |
} |
| 115 |
}; |
| 116 |
text.addKeyListener(keyListener); |
| 117 |
} |
| 118 |
|
| 119 |
public void setValue(final Object value) { |
| 120 |
|
| 121 |
AsyncRunnable runnable = new AsyncRunnable(){ |
| 122 |
public void run(){ |
| 123 |
String oldValue = text.getText(); |
| 124 |
try { |
| 125 |
updating = true; |
| 126 |
bufferedValue = (String)value; |
| 127 |
text.setText(value == null ? "" : value.toString()); //$NON-NLS-1$ |
| 128 |
} finally { |
| 129 |
updating = false; |
| 130 |
} |
| 131 |
fireChangeEvent(ChangeEvent.CHANGE, oldValue, text.getText()); |
| 132 |
} |
| 133 |
}; |
| 134 |
runnable.runOn(text.getDisplay()); |
| 135 |
} |
| 136 |
|
| 137 |
public Object computeValue() { |
| 138 |
SyncRunnable runnable = new SyncRunnable(){ |
| 139 |
public Object run() { |
| 140 |
return text.getText(); |
| 141 |
} |
| 142 |
}; |
| 143 |
return runnable.runOn(text.getDisplay()); |
| 144 |
} |
| 145 |
|
| 146 |
public Class getValueType() { |
| 147 |
return String.class; |
| 148 |
} |
| 149 |
|
| 150 |
public void dispose() { |
| 151 |
if (!text.isDisposed()) { |
| 152 |
if (updatePolicy != SWT.None) { |
| 153 |
text.removeListener(updatePolicy, updateListener); |
| 154 |
} |
| 155 |
text.removeVerifyListener(verifyListener); |
| 156 |
} |
| 157 |
super.dispose(); |
| 158 |
} |
| 159 |
} |