001: package fr.aliacom.form.swt.ui;
002:
003: import java.beans.PropertyChangeEvent;
004: import java.beans.PropertyChangeListener;
005:
006: import org.apache.log4j.Logger;
007: import org.eclipse.swt.SWT;
008: import org.eclipse.swt.events.KeyAdapter;
009: import org.eclipse.swt.events.KeyEvent;
010: import org.eclipse.swt.graphics.FontMetrics;
011: import org.eclipse.swt.graphics.GC;
012: import org.eclipse.swt.widgets.Text;
013:
014: import fr.aliacom.bean.BeanUtils;
015: import fr.aliacom.commands.Command;
016: import fr.aliacom.form.swt.events.TextModifyListener;
017: import fr.aliacom.form.swt.utils.SWTUtils;
018:
019: /**
020: * @author tom
021: *
022: * (C) 2001, 2003 Thomas Cataldo
023: */
024: public class SWTAbstractText implements ISWTText,
025: PropertyChangeListener {
026:
027: private Object javaBean;
028: private TextModifyListener listener;
029: private boolean locked;
030: protected String property;
031: protected Text text;
032:
033: private static final Logger log = Logger
034: .getLogger(SWTAbstractText.class);
035:
036: protected void sizeControl(int length) {
037: SWTUtils.setMonospacedFont(text);
038: GC gc = new GC(text);
039: FontMetrics fm = gc.getFontMetrics();
040: int width = length * fm.getAverageCharWidth();
041: int height = fm.getHeight();
042: gc.dispose();
043: text.setSize(text.computeSize(width, height));
044: log.debug("Setting text field size to (" + width + ", "
045: + height + ")");
046: }
047:
048: /**
049: * @see fr.aliacom.form.common.IFormComponent#getNativeWidget()
050: */
051: public Object getNativeWidget() {
052: return text;
053: }
054:
055: /**
056: * @see fr.aliacom.common.ui.IText#getText()
057: */
058: public String getText() {
059: return text.getText();
060: }
061:
062: /**
063: * Method lock.
064: */
065: public void lock() {
066: locked = true;
067: }
068:
069: /**
070: * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
071: */
072: public void propertyChange(PropertyChangeEvent evt) {
073: if (!locked) {
074: listener.lock();
075: text.setText(evt.getNewValue().toString());
076: listener.unlock();
077: }
078: }
079:
080: /**
081: * @see fr.aliacom.form.common.IFormComponent#reset()
082: */
083: public void reset() {
084: }
085:
086: /**
087: * @see fr.aliacom.common.ui.IText#setEditable(boolean)
088: */
089: public void setEditable(boolean editable) {
090: text.setEditable(editable);
091: }
092:
093: /**
094: * @see fr.aliacom.common.ui.IText#setEnabled(boolean)
095: */
096: public void setEnabled(boolean enabled) {
097: text.setEnabled(enabled);
098: }
099:
100: /**
101: * @see fr.aliacom.common.ui.IText#setText(java.lang.String)
102: */
103: public void setText(String txt) {
104: text.setText(txt);
105: }
106:
107: /**
108: * @see fr.aliacom.form.common.IFormComponent#setValueBean(java.lang.Object)
109: */
110: public void setValueBean(Object bean) {
111: locked = false;
112: String value = (String) BeanUtils.getValue(bean, property);
113: value = (value == null ? "" : value);
114: setText(value);
115: if (javaBean != null) {
116: text.removeModifyListener(listener);
117: BeanUtils
118: .removePropertyChangeListener(bean, property, this );
119: }
120: text.addModifyListener(listener = new TextModifyListener(this ,
121: bean, property));
122: javaBean = bean;
123: BeanUtils.addPropertyChangeListener(javaBean, property, this );
124: }
125:
126: public void unlock() {
127: locked = false;
128: }
129:
130: /**
131: * @see fr.aliacom.common.ui.ITextField#setOnCrAction(fr.aliacom.commands.Command)
132: */
133: public void setOnCrAction(final Command c) {
134: text.addKeyListener(new KeyAdapter() {
135: public void keyPressed(KeyEvent e) {
136: if (e.character == SWT.CR) {
137: c.run();
138: }
139: }
140: });
141: }
142:
143: /**
144: * @see fr.aliacom.common.ui.ITextField#setOnDownAction(fr.aliacom.commands.Command)
145: */
146: public void setOnDownAction(final Command c) {
147: text.addKeyListener(new KeyAdapter() {
148: public void keyPressed(KeyEvent e) {
149: if (e.keyCode == SWT.ARROW_DOWN) {
150: c.run();
151: }
152: }
153: });
154: }
155:
156: /**
157: * @see fr.aliacom.common.ui.ITextField#setOnUpAction(fr.aliacom.commands.Command)
158: */
159: public void setOnUpAction(final Command c) {
160: text.addKeyListener(new KeyAdapter() {
161: public void keyPressed(KeyEvent e) {
162: if (e.keyCode == SWT.ARROW_UP) {
163: c.run();
164: }
165: }
166: });
167: }
168:
169: /* (non-Javadoc)
170: * @see fr.aliacom.common.ui.IText#focus()
171: */
172: public void focus() {
173: text.setFocus();
174: }
175:
176: }
|