001: /* $Id: StringEditor.java 746 2006-11-08 13:34:23Z hengels $ */
002: package org.conform.wings.editor;
003:
004: import javax.swing.text.BadLocationException;
005:
006: import org.conform.*;
007: import org.conform.wings.PropertyAdapter;
008: import org.conform.wings.Editor;
009: import org.wings.*;
010: import org.wings.event.SDocumentListener;
011: import org.wings.event.SDocumentEvent;
012: import org.wings.text.DefaultDocument;
013:
014: import java.util.Arrays;
015:
016: public class StringEditor implements Editor {
017: /**
018: * Integer rows for STextArea.
019: */
020: public static final String ROWS = "editor.string.rows";
021: /**
022: * Integer cols for STextField / SPasswordField / STextArea.
023: */
024: public static final String COLS = "editor.string.cols";
025: /**
026: * Boolean flag require a SPasswordField.
027: */
028: public static final String PASSWORD = "editor.string.password";
029:
030: private static final SDocumentListener NO_DOCUMENT_LISTENER = new SDocumentListener() {
031: public void insertUpdate(SDocumentEvent e) {
032: }
033:
034: public void removeUpdate(SDocumentEvent e) {
035: }
036:
037: public void changedUpdate(SDocumentEvent e) {
038: }
039: };
040:
041: public SComponent createComponent(PropertyMeta propertyMeta) {
042: Integer rows = (Integer) propertyMeta.getAttribute(ROWS);
043: boolean password = Boolean.TRUE.equals(propertyMeta
044: .getAttribute(PASSWORD));
045:
046: STextComponent textComponent;
047: if (rows != null && rows > 1) {
048: textComponent = new STextArea();
049: } else if (password) {
050: textComponent = new SPasswordField();
051: } else {
052: textComponent = new STextField();
053: }
054:
055: textComponent.setDocument(new DocumentPropertyAdapter(
056: propertyMeta));
057: configureComponent(propertyMeta, textComponent, false);
058: return textComponent;
059: }
060:
061: public void configureComponent(PropertyMeta propertyMeta,
062: SComponent component, boolean erroneous) {
063: STextComponent textComponent = (STextComponent) component;
064:
065: Integer rows = (Integer) propertyMeta.getAttribute(ROWS);
066: Integer cols = (Integer) propertyMeta.getAttribute(COLS);
067: Integer max = (Integer) propertyMeta
068: .getAttribute(PropertyMeta.ATTRIBUTE_LENGTH);
069: boolean password = Boolean.TRUE.equals(propertyMeta
070: .getAttribute(PASSWORD));
071:
072: if (rows != null && rows > 1) {
073: ((STextArea) textComponent).setRows(rows);
074:
075: if (cols != null && cols > 1)
076: ((STextArea) textComponent).setColumns(cols);
077: } else if (password) {
078: if (cols != null && cols > 1)
079: ((SPasswordField) textComponent).setColumns(cols);
080:
081: if (max != null && max > 1)
082: ((SPasswordField) textComponent).setMaxColumns(max);
083: } else {
084: if (cols != null && cols > 1)
085: ((STextField) textComponent).setColumns(cols);
086:
087: if (max != null && max > 0)
088: ((STextField) textComponent).setMaxColumns(max);
089: }
090:
091: textComponent.setEditable(propertyMeta.isWritable());
092: textComponent.setVisible(propertyMeta.isReadable());
093: DefaultEditorStyles.applyEditorAlignment(propertyMeta,
094: component);
095: DefaultEditorStyles.applyEditorState(propertyMeta, component,
096: erroneous);
097:
098: if (propertyMeta
099: .getAttribute(PropertyMeta.ATTRIBUTE_CHANGE_LISTENER) != null
100: && !Arrays.asList(textComponent.getDocumentListeners())
101: .contains(NO_DOCUMENT_LISTENER))
102: textComponent.addDocumentListener(NO_DOCUMENT_LISTENER);
103: }
104:
105: public void setPropertyData(SComponent component,
106: PropertyData propertyData) {
107: BeanData beanData = propertyData.getBeanData();
108: if (beanData != null)
109: beanData.addPropertyChangeListener(propertyData
110: .getPropertyMeta().getName(),
111: new ComponentInvalidator(component));
112: STextComponent textComponent = (STextComponent) component;
113: ((DocumentPropertyAdapter) textComponent.getDocument())
114: .setPropertyData(propertyData);
115: }
116:
117: public PropertyData getPropertyData(SComponent component) {
118: STextComponent textComponent = (STextComponent) component;
119: return ((DocumentPropertyAdapter) textComponent.getDocument())
120: .getPropertyData();
121: }
122:
123: protected static class DocumentPropertyAdapter extends
124: DefaultDocument implements PropertyAdapter {
125: private PropertyData propertyData;
126: private PropertyMeta field;
127:
128: public DocumentPropertyAdapter(PropertyMeta field) {
129: this .field = field;
130: }
131:
132: public void setPropertyData(PropertyData propertyData) {
133: this .propertyData = propertyData;
134: //fireChangeUpdate(0, getLength());
135: }
136:
137: public PropertyData getPropertyData() {
138: return propertyData;
139: }
140:
141: public void setText(String text) {
142: int length = (text == null) ? 0 : text.length();
143: if (length == 0)
144: text = null;
145:
146: propertyData.setValue(text);
147: fireChangeUpdate(0, length);
148: }
149:
150: public String getText() {
151: return (String) propertyData.getValue();
152: }
153:
154: public int getLength() {
155: return (getText() == null) ? 0 : getText().length();
156: }
157:
158: public String getText(int offset, int length)
159: throws BadLocationException {
160: throw new UnsupportedOperationException(
161: "this meant to be an adapter");
162: }
163:
164: public void remove(int offset, int length)
165: throws BadLocationException {
166: throw new UnsupportedOperationException(
167: "this meant to be an adapter");
168: }
169:
170: public void insert(int pos, String s)
171: throws BadLocationException {
172: throw new UnsupportedOperationException(
173: "this meant to be an adapter");
174: }
175: }
176: }
|