|
Added
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2008 Code 9 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 |
* Code 9 Corporation - initial API and implementation |
| 10 |
* Chris Aniszczyk <zx@code9.com> - bug 131435 |
| 11 |
*******************************************************************************/ |
| 12 |
package org.eclipse.jface.internal.databinding.swt; |
| 13 |
|
| 14 |
import org.eclipse.core.databinding.observable.Diffs; |
| 15 |
import org.eclipse.core.databinding.observable.IObservable; |
| 16 |
import org.eclipse.core.databinding.observable.Realm; |
| 17 |
import org.eclipse.jface.databinding.swt.SWTObservables; |
| 18 |
import org.eclipse.jface.internal.databinding.provisional.swt.AbstractSWTVetoableValue; |
| 19 |
import org.eclipse.swt.SWT; |
| 20 |
import org.eclipse.swt.custom.StyledText; |
| 21 |
import org.eclipse.swt.events.VerifyEvent; |
| 22 |
import org.eclipse.swt.events.VerifyListener; |
| 23 |
import org.eclipse.swt.widgets.Event; |
| 24 |
import org.eclipse.swt.widgets.Listener; |
| 25 |
|
| 26 |
/** |
| 27 |
* {@link IObservable} implementation that wraps a {@link StyledText} widget. The time |
| 28 |
* at which listeners should be notified about changes to the text is specified |
| 29 |
* on construction. |
| 30 |
* |
| 31 |
* <dl> |
| 32 |
* <dt>Events:</dt> |
| 33 |
* <dd> If the update event type (specified on construction) is |
| 34 |
* <code>SWT.Modify</code> a value change event will be fired on every key |
| 35 |
* stroke. If the update event type is <code>SWT.FocusOut</code> a value |
| 36 |
* change event will be fired on focus out. When in either mode if the user is |
| 37 |
* entering text and presses [Escape] the value will be reverted back to the |
| 38 |
* last value set using doSetValue(). Regardless of the update event type a |
| 39 |
* value changing event will fire on verify to enable vetoing of changes.</dd> |
| 40 |
* </dl> |
| 41 |
* |
| 42 |
* @since 1.3 |
| 43 |
*/ |
| 44 |
public class StyledTextObservableValue extends AbstractSWTVetoableValue { |
| 45 |
|
| 46 |
/** |
| 47 |
* {@link StyledText} widget that this is being observed. |
| 48 |
*/ |
| 49 |
private final StyledText text; |
| 50 |
|
| 51 |
/** |
| 52 |
* Flag to track when the model is updating the widget. When |
| 53 |
* <code>true</code> the handlers for the SWT events should not process |
| 54 |
* the event as this would cause an infinite loop. |
| 55 |
*/ |
| 56 |
private boolean updating = false; |
| 57 |
|
| 58 |
/** |
| 59 |
* SWT event that on firing this observable will fire change events to its |
| 60 |
* listeners. |
| 61 |
*/ |
| 62 |
private final int updateEventType; |
| 63 |
|
| 64 |
/** |
| 65 |
* Valid types for the {@link #updateEventType}. |
| 66 |
*/ |
| 67 |
private static final int[] validUpdateEventTypes = new int[] { SWT.Modify, |
| 68 |
SWT.FocusOut, SWT.None }; |
| 69 |
|
| 70 |
/** |
| 71 |
* Previous value of the Text. |
| 72 |
*/ |
| 73 |
private String oldValue; |
| 74 |
|
| 75 |
private Listener updateListener = new Listener() { |
| 76 |
public void handleEvent(Event event) { |
| 77 |
if (!updating) { |
| 78 |
String newValue = text.getText(); |
| 79 |
|
| 80 |
if (!newValue.equals(oldValue)) { |
| 81 |
fireValueChange(Diffs.createValueDiff(oldValue, newValue)); |
| 82 |
oldValue = newValue; |
| 83 |
} |
| 84 |
} |
| 85 |
} |
| 86 |
}; |
| 87 |
|
| 88 |
private VerifyListener verifyListener; |
| 89 |
|
| 90 |
/** |
| 91 |
* Constructs a new instance bound to the given <code>StyledText</code> widget |
| 92 |
* and configured to fire change events to its listeners at the time of the |
| 93 |
* <code>updateEventType</code>. |
| 94 |
* |
| 95 |
* @param text |
| 96 |
* @param updateEventType |
| 97 |
* SWT event constant as to what SWT event to update the model in |
| 98 |
* response to. Appropriate values are: <code>SWT.Modify</code>, |
| 99 |
* <code>SWT.FocusOut</code>, <code>SWT.None</code>. |
| 100 |
* @throws IllegalArgumentException |
| 101 |
* if <code>updateEventType</code> is an incorrect type. |
| 102 |
*/ |
| 103 |
public StyledTextObservableValue(final StyledText text, int updateEventType) { |
| 104 |
this(SWTObservables.getRealm(text.getDisplay()), text, updateEventType); |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Constructs a new instance. |
| 109 |
* |
| 110 |
* @param realm can not be <code>null</code> |
| 111 |
* @param text |
| 112 |
* @param updateEventType |
| 113 |
*/ |
| 114 |
public StyledTextObservableValue(final Realm realm, StyledText text, int updateEventType) { |
| 115 |
super(realm, text); |
| 116 |
|
| 117 |
boolean eventValid = false; |
| 118 |
for (int i = 0; !eventValid && i < validUpdateEventTypes.length; i++) { |
| 119 |
eventValid = (updateEventType == validUpdateEventTypes[i]); |
| 120 |
} |
| 121 |
if (!eventValid) { |
| 122 |
throw new IllegalArgumentException( |
| 123 |
"UpdateEventType [" + updateEventType + "] is not supported."); //$NON-NLS-1$//$NON-NLS-2$ |
| 124 |
} |
| 125 |
this.text = text; |
| 126 |
this.updateEventType = updateEventType; |
| 127 |
if (updateEventType != SWT.None) { |
| 128 |
text.addListener(updateEventType, updateListener); |
| 129 |
} |
| 130 |
|
| 131 |
oldValue = text.getText(); |
| 132 |
|
| 133 |
verifyListener = new VerifyListener() { |
| 134 |
public void verifyText(VerifyEvent e) { |
| 135 |
if (!updating) { |
| 136 |
String currentText = StyledTextObservableValue.this.text |
| 137 |
.getText(); |
| 138 |
String newText = currentText.substring(0, e.start) + e.text |
| 139 |
+ currentText.substring(e.end); |
| 140 |
if (!fireValueChanging(Diffs.createValueDiff(currentText, |
| 141 |
newText))) { |
| 142 |
e.doit = false; |
| 143 |
} |
| 144 |
} |
| 145 |
} |
| 146 |
}; |
| 147 |
text.addVerifyListener(verifyListener); |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Sets the bound {@link StyledText Text's} text to the passed <code>value</code>. |
| 152 |
* |
| 153 |
* @param value |
| 154 |
* new value, String expected |
| 155 |
* @see org.eclipse.core.databinding.observable.value.AbstractVetoableValue#doSetApprovedValue(java.lang.Object) |
| 156 |
* @throws ClassCastException |
| 157 |
* if the value is anything other than a String |
| 158 |
*/ |
| 159 |
protected void doSetApprovedValue(final Object value) { |
| 160 |
try { |
| 161 |
updating = true; |
| 162 |
text.setText(value == null ? "" : value.toString()); //$NON-NLS-1$ |
| 163 |
oldValue = text.getText(); |
| 164 |
} finally { |
| 165 |
updating = false; |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
/** |
| 170 |
* Returns the current value of the {@link StyledText}. |
| 171 |
* |
| 172 |
* @see org.eclipse.core.databinding.observable.value.AbstractVetoableValue#doGetValue() |
| 173 |
*/ |
| 174 |
public Object doGetValue() { |
| 175 |
return oldValue = text.getText(); |
| 176 |
} |
| 177 |
|
| 178 |
/** |
| 179 |
* Returns the type of the value from {@link #doGetValue()}, i.e. |
| 180 |
* String.class |
| 181 |
* |
| 182 |
* @see org.eclipse.core.databinding.observable.value.IObservableValue#getValueType() |
| 183 |
*/ |
| 184 |
public Object getValueType() { |
| 185 |
return String.class; |
| 186 |
} |
| 187 |
|
| 188 |
public void dispose() { |
| 189 |
if (!text.isDisposed()) { |
| 190 |
if (updateEventType != SWT.None) { |
| 191 |
text.removeListener(updateEventType, updateListener); |
| 192 |
} |
| 193 |
text.removeVerifyListener(verifyListener); |
| 194 |
} |
| 195 |
super.dispose(); |
| 196 |
} |
| 197 |
} |