001: package org.conform.wings.editor;
002:
003: import org.conform.wings.Editor;
004: import org.conform.wings.PropertyAdapter;
005: import org.conform.*;
006: import org.wings.*;
007: import org.wings.text.DefaultDocument;
008:
009: import javax.swing.text.BadLocationException;
010: import java.util.*;
011:
012: public class StringsEditor implements Editor {
013: public static final String ROWS = "editor.strings.rows";
014: public static final String COLS = "editor.strings.cols";
015: public static final String SORT = "editor.strings.sort";
016:
017: public SComponent createComponent(PropertyMeta propertyMeta) {
018: STextComponent textComponent = new STextArea();
019: textComponent.setDocument(new DocumentPropertyAdapter(
020: propertyMeta));
021: configureComponent(propertyMeta, textComponent, false);
022: return textComponent;
023: }
024:
025: public void configureComponent(PropertyMeta propertyMeta,
026: SComponent component, boolean erroneous) {
027: STextArea textArea = (STextArea) component;
028:
029: Integer rows = (Integer) propertyMeta
030: .getAttribute(StringsEditor.ROWS);
031: if (rows == null)
032: rows = 4;
033: Integer cols = (Integer) propertyMeta
034: .getAttribute(StringsEditor.COLS);
035: if (cols == null)
036: cols = 80;
037:
038: textArea.setRows(rows);
039: textArea.setColumns(cols);
040:
041: textArea.setEditable(propertyMeta.isWritable());
042: textArea.setVisible(propertyMeta.isReadable());
043: DefaultEditorStyles.applyEditorAlignment(propertyMeta,
044: component);
045: DefaultEditorStyles.applyEditorState(propertyMeta, component,
046: erroneous);
047: }
048:
049: public void setPropertyData(SComponent component,
050: PropertyData propertyData) {
051: BeanData beanData = propertyData.getBeanData();
052: if (beanData != null)
053: beanData.addPropertyChangeListener(propertyData
054: .getPropertyMeta().getName(),
055: new ComponentInvalidator(component));
056: STextComponent textComponent = (STextComponent) component;
057: ((DocumentPropertyAdapter) textComponent.getDocument())
058: .setPropertyData(propertyData);
059: }
060:
061: public PropertyData getPropertyData(SComponent component) {
062: STextComponent textComponent = (STextComponent) component;
063: return ((DocumentPropertyAdapter) textComponent.getDocument())
064: .getPropertyData();
065: }
066:
067: protected static class DocumentPropertyAdapter extends
068: DefaultDocument implements PropertyAdapter {
069: private PropertyData propertyData;
070: private PropertyMeta field;
071: private StringBuilder builder = new StringBuilder();
072: private Comparator sort;
073:
074: public DocumentPropertyAdapter(PropertyMeta field) {
075: this .field = field;
076: }
077:
078: public void setPropertyData(PropertyData propertyData) {
079: this .propertyData = propertyData;
080: sort = (Comparator) propertyData.getPropertyMeta()
081: .getAttribute(SORT);
082: }
083:
084: public PropertyData getPropertyData() {
085: return propertyData;
086: }
087:
088: public void setText(String text) {
089: int length = (text == null) ? 0 : text.length();
090: if (length == 0)
091: text = null;
092:
093: if (text != null) {
094: StringTokenizer stringTokenizer = new StringTokenizer(
095: text, "\n");
096: Collection<String> strings = newCollection(stringTokenizer
097: .countTokens());
098: for (; stringTokenizer.hasMoreTokens();) {
099: String line = stringTokenizer.nextToken();
100: if (line.trim().length() > 0)
101: strings.add(line);
102: }
103: propertyData.setValue(strings);
104: } else
105: propertyData.setValue(null);
106:
107: fireChangeUpdate(0, length);
108: }
109:
110: private Collection<String> newCollection(int n) {
111: try {
112: Class type = propertyData.getPropertyMeta().getType();
113: if (Set.class.isAssignableFrom(type))
114: return new HashSet<String>();
115: else if (List.class.isAssignableFrom(type))
116: return new ArrayList<String>();
117: } catch (Exception e) {
118: throw new RuntimeException(e);
119: }
120: throw new IllegalArgumentException(
121: "Only Set and List is supported.");
122: }
123:
124: public String getText() {
125: Collection<String> strings = (Collection<String>) propertyData
126: .getValue();
127: if (strings != null) {
128: if (sort != null) {
129: List<String> list = new ArrayList<String>(strings);
130: Collections.sort(list, sort);
131: strings = list;
132: }
133: builder.setLength(0);
134: for (String line : strings) {
135: builder.append(line);
136: builder.append('\n');
137: }
138: return builder.toString();
139: } else
140: return null;
141: }
142:
143: public int getLength() {
144: return (getText() == null) ? 0 : getText().length();
145: }
146:
147: public String getText(int offset, int length)
148: throws BadLocationException {
149: throw new UnsupportedOperationException(
150: "this meant to be an adapter");
151: }
152:
153: public void remove(int offset, int length)
154: throws BadLocationException {
155: throw new UnsupportedOperationException(
156: "this meant to be an adapter");
157: }
158:
159: public void insert(int pos, String s)
160: throws BadLocationException {
161: throw new UnsupportedOperationException(
162: "this meant to be an adapter");
163: }
164: }
165: }
|