001: package org.gui4j.component.factory;
002:
003: import java.util.ArrayList;
004: import java.util.HashSet;
005: import java.util.List;
006: import java.util.Set;
007:
008: import javax.swing.KeyStroke;
009: import javax.swing.border.Border;
010:
011: import org.apache.commons.logging.Log;
012: import org.apache.commons.logging.LogFactory;
013: import org.dom4j.LElement;
014: import org.gui4j.component.Gui4jJComponent;
015: import org.gui4j.core.Gui4jCall;
016: import org.gui4j.core.Gui4jComponent;
017: import org.gui4j.core.Gui4jComponentContainer;
018: import org.gui4j.core.Gui4jComponentContainerManager;
019: import org.gui4j.core.Gui4jComponentFactory;
020: import org.gui4j.core.Gui4jMap1;
021: import org.gui4j.core.Gui4jTypeCheck;
022: import org.gui4j.core.definition.Attribute;
023: import org.gui4j.core.definition.AttributeTypeEnumeration;
024: import org.gui4j.core.definition.AttributeTypeID;
025: import org.gui4j.core.definition.AttributeTypeMethodCall;
026: import org.gui4j.core.definition.AttributeTypeString;
027: import org.gui4j.core.definition.Param;
028: import org.gui4j.exception.Gui4jUncheckedException;
029: import org.gui4j.util.Filter;
030:
031: abstract public class Gui4jJComponentFactory extends
032: Gui4jComponentFactory {
033: private static final String BORDER = "border";
034: private static final String TOOLTIP = "tooltip";
035: private static final String OPAQUE = "opaque";
036: private static final String GRABFOCUS = "grabFocus";
037: private static final String HANDLE_READ_ONLY = "handleReadOnly";
038: private static final String KEYMAP = "keyMapId";
039: private static final String FOCUS_KEYS_FORWARD = "focusKeysForward";
040: private static final String FOCUS_KEYS_BACKWARD = "focusKeysBackward";
041:
042: private static final Log mLogger = LogFactory
043: .getLog(Gui4jJComponentFactory.class);
044:
045: protected void defineProperties(Gui4jJComponent gui4jJComponent,
046: LElement e) {
047: super .defineProperties(gui4jJComponent, e);
048: gui4jJComponent.definePropertySetter(OPAQUE,
049: getGui4jAccessInstance(Boolean.TYPE, gui4jJComponent,
050: e, OPAQUE), false);
051: gui4jJComponent.definePropertySetter(BORDER,
052: getGui4jAccessInstance(Border.class, new Gui4jMap1(
053: CONTEXT, Object.class), gui4jJComponent, e,
054: BORDER), true);
055:
056: gui4jJComponent.definePropertySetter(TOOLTIP,
057: getGui4jAccessInstance(String.class, new Gui4jMap1(
058: CONTEXT, Object.class), gui4jJComponent, e,
059: TOOLTIP), true);
060:
061: Gui4jCall grabFocus = getGui4jAccessInstance(null,
062: new Gui4jMap1(CONTEXT, Object.class), gui4jJComponent,
063: e, GRABFOCUS);
064: if (grabFocus != null) {
065: Gui4jCall[] dependantProperties = grabFocus
066: .getDependantProperties();
067: if (dependantProperties == null
068: || dependantProperties.length == 0) {
069: mLogger.warn("Set of dependant events is empty");
070: }
071: gui4jJComponent.setGrabFocus(grabFocus);
072: if (grabFocus.getResultClass() != null) {
073: Gui4jTypeCheck.ensureType(Boolean.TYPE, grabFocus
074: .getResultClass(), gui4jJComponent
075: .getConfigurationName(), e
076: .attributeValue(GRABFOCUS));
077: }
078: }
079:
080: Set focusKeysForward = extractKeyStrokes(gui4jJComponent, e,
081: FOCUS_KEYS_FORWARD);
082: if (focusKeysForward != null) {
083: gui4jJComponent
084: .setFocusTraversalKeysForward(focusKeysForward);
085: }
086:
087: Set focusKeysBackward = extractKeyStrokes(gui4jJComponent, e,
088: FOCUS_KEYS_BACKWARD);
089: if (focusKeysBackward != null) {
090: gui4jJComponent
091: .setFocusTraversalKeysBackward(focusKeysBackward);
092: }
093:
094: gui4jJComponent.setKeyMapId(e.attributeValue(KEYMAP));
095: String handleReadOnly = e.attributeValue(HANDLE_READ_ONLY);
096: gui4jJComponent.setHandleReadOnly(handleReadOnly != null
097: && handleReadOnly.equalsIgnoreCase("true"));
098: }
099:
100: /**
101: * Returns a Set of KeyStroke objects created from the specified attribute. The attribute value is
102: * expected to be a comma separated list of individual KeyStroke definitions. Each definition is converted
103: * to a KeyStroke using {@link KeyStroke#getKeyStroke(java.lang.String)}.
104: * @param gui4jJComponent
105: * @param e element
106: * @param attr name of the attribute containing the keystroke defintions
107: * @return Set(KeyStroke)
108: */
109: private Set extractKeyStrokes(Gui4jJComponent gui4jJComponent,
110: LElement e, String attr) {
111: String value = e.attributeValue(attr);
112: if (value == null) {
113: return null;
114: } else {
115: Set keyStrokes = new HashSet();
116: String[] keyStrings = value.split(",");
117: for (int i = 0; i < keyStrings.length; i++) {
118: String keyString = keyStrings[i].trim();
119: if (keyString.length() > 0) {
120: KeyStroke stroke = KeyStroke
121: .getKeyStroke(keyString);
122: if (stroke == null) {
123: throw new Gui4jUncheckedException.ResourceError(
124: gui4jJComponent.getConfigurationName(),
125: Gui4jComponentContainerManager
126: .getLineNumber(e),
127: RESOURCE_ERROR_invalid_keystroke,
128: new Object[] { keyString });
129: } else {
130: keyStrokes.add(stroke);
131: }
132: }
133: }
134: return keyStrokes;
135: }
136: }
137:
138: public void addToplevelAttributes(List attrList, Filter filter) {
139: super .addToplevelAttributes(attrList, filter);
140: if (filter == null
141: || filter.takeIt(Gui4jJComponentFactory.class)) {
142: List l = new ArrayList();
143: l.add(new Param(CONTEXT));
144: attrList.add(new Attribute(BORDER,
145: new AttributeTypeMethodCall(Border.class, l,
146: EVENT_AWARE), IMPLIED, false));
147: attrList.add(new Attribute(TOOLTIP,
148: new AttributeTypeMethodCall(String.class, l,
149: EVENT_AWARE), IMPLIED, false));
150: attrList.add(new Attribute(OPAQUE,
151: new AttributeTypeMethodCall(Boolean.TYPE), IMPLIED,
152: false));
153: attrList.add(new Attribute(GRABFOCUS,
154: new AttributeTypeMethodCall(Boolean.TYPE, l,
155: EVENT_AWARE), IMPLIED, false));
156: attrList.add(new Attribute(FOCUS_KEYS_FORWARD,
157: new AttributeTypeString(), IMPLIED, false));
158: attrList.add(new Attribute(FOCUS_KEYS_BACKWARD,
159: new AttributeTypeString(), IMPLIED, false));
160: attrList.add(new Attribute(HANDLE_READ_ONLY,
161: AttributeTypeEnumeration.getBooleanInstance(),
162: false, false));
163: attrList.add(new Attribute(KEYMAP, new AttributeTypeID(),
164: false, false));
165: }
166: }
167:
168: final public Gui4jComponent defineBy(
169: Gui4jComponentContainer gui4jComponentContainer, String id,
170: LElement e) {
171: Gui4jJComponent gui4jJComponent = defineGui4jJComponentBy(
172: gui4jComponentContainer, id, e);
173: defineProperties(gui4jJComponent, e);
174: return gui4jJComponent;
175: }
176:
177: abstract protected Gui4jJComponent defineGui4jJComponentBy(
178: Gui4jComponentContainer gui4jComponentContainer, String id,
179: LElement e);
180: }
|