001: package org.gui4j.component.factory;
002:
003: import java.util.ArrayList;
004: import java.util.HashMap;
005: import java.util.List;
006: import java.util.Map;
007:
008: import javax.swing.ScrollPaneConstants;
009: import javax.swing.border.Border;
010:
011: import org.dom4j.LElement;
012: import org.gui4j.component.Gui4jJComponent;
013: import org.gui4j.component.Gui4jScrollPane;
014: import org.gui4j.core.Gui4jComponentContainer;
015: import org.gui4j.core.Gui4jComponentContainerManager;
016: import org.gui4j.core.Gui4jMap1;
017: import org.gui4j.core.Gui4jQualifiedComponent;
018: import org.gui4j.core.definition.Attribute;
019: import org.gui4j.core.definition.AttributeTypeEnumeration;
020: import org.gui4j.core.definition.AttributeTypeID;
021: import org.gui4j.core.definition.AttributeTypeMethodCall;
022: import org.gui4j.core.definition.Param;
023: import org.gui4j.exception.Gui4jUncheckedException;
024: import org.gui4j.util.Filter;
025:
026: public final class Gui4jScrollPaneFactory extends
027: Gui4jJComponentFactory {
028: private static final String NAME = "scrollPane";
029: private static final String ID = "id";
030: private static final String HSCROLLPOLICY = "hScrollPolicy";
031: private static final String VSCROLLPOLICY = "vScrollPolicy";
032: private static final String VIEWPORTBORDER = "viewportBorder";
033:
034: private static final String ALWAYS = "always";
035: private static final String NEVER = "never";
036: private static final String AUTO = "auto";
037:
038: private static final Map mHScrollPolicy = new HashMap();
039: private static final Map mVScrollPolicy = new HashMap();
040:
041: static {
042: mHScrollPolicy.put(ALWAYS, new Integer(
043: ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS));
044: mHScrollPolicy.put(NEVER, new Integer(
045: ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER));
046: mHScrollPolicy.put(AUTO, new Integer(
047: ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED));
048:
049: mVScrollPolicy.put(ALWAYS, new Integer(
050: ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS));
051: mVScrollPolicy.put(NEVER, new Integer(
052: ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER));
053: mVScrollPolicy.put(AUTO, new Integer(
054: ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED));
055: }
056:
057: /* (non-Javadoc)
058: * @see org.gui4j.component.factory.Gui4jJComponentFactory#defineGui4jJComponentBy(org.gui4j.core.Gui4jComponentContainer, java.lang.String, org.dom4j.LElement)
059: */
060: protected Gui4jJComponent defineGui4jJComponentBy(
061: Gui4jComponentContainer gui4jComponentContainer, String id,
062: LElement e) {
063: String gui4jId = e.attributeValue(ID);
064: Gui4jQualifiedComponent gui4jComponentInPath;
065: if (gui4jId == null) {
066: if (e.elements().isEmpty()) {
067: Object[] args = { getName() };
068: throw new Gui4jUncheckedException.ResourceError(
069: gui4jComponentContainer.getConfigurationName(),
070: Gui4jComponentContainerManager.getLineNumber(e),
071: RESOURCE_ERROR_element_must_contain_gui4jComponent,
072: args);
073: }
074: LElement gui4jElement = (LElement) e.elements().iterator()
075: .next();
076: gui4jComponentInPath = gui4jComponentContainer
077: .extractGui4jComponent(gui4jElement);
078: } else {
079: gui4jComponentInPath = gui4jComponentContainer
080: .getGui4jQualifiedComponent(gui4jId);
081: }
082:
083: int hScrollPolicy = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
084: int vScrollPolicy = ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
085: String hScrollPolicyStr = e.attributeValue(HSCROLLPOLICY);
086: String vScrollPolicyStr = e.attributeValue(VSCROLLPOLICY);
087: if (hScrollPolicyStr != null) {
088: hScrollPolicy = ((Integer) mHScrollPolicy
089: .get(hScrollPolicyStr)).intValue();
090: }
091: if (vScrollPolicyStr != null) {
092: vScrollPolicy = ((Integer) mVScrollPolicy
093: .get(vScrollPolicyStr)).intValue();
094: }
095:
096: Gui4jScrollPane gui4jScrollPane = new Gui4jScrollPane(
097: gui4jComponentContainer, id, gui4jComponentInPath,
098: hScrollPolicy, vScrollPolicy);
099: return gui4jScrollPane;
100: }
101:
102: protected void defineProperties(Gui4jJComponent gui4jJComponent,
103: LElement e) {
104: super .defineProperties(gui4jJComponent, e);
105: gui4jJComponent.definePropertySetter(VIEWPORTBORDER,
106: getGui4jAccessInstance(Border.class, new Gui4jMap1(
107: CONTEXT, Object.class), gui4jJComponent, e,
108: VIEWPORTBORDER), true);
109: }
110:
111: public SubElement getSubElement(String elementName) {
112: if (NAME.equals(elementName)) {
113: return SubElement.optional(SubElement.gui4jComponent());
114: }
115: return null;
116: }
117:
118: public void addInnerAttributes(String elementName, List list) {
119: }
120:
121: public void addToplevelAttributes(List attrList, Filter filter) {
122: super .addToplevelAttributes(attrList, filter);
123: if (filter == null
124: || filter.takeIt(Gui4jScrollPaneFactory.class)) {
125: List l = new ArrayList();
126: l.add(new Param(CONTEXT));
127:
128: attrList.add(new Attribute(ID, new AttributeTypeID(),
129: IMPLIED, false));
130: attrList.add(new Attribute(HSCROLLPOLICY,
131: new AttributeTypeEnumeration(mHScrollPolicy, AUTO),
132: IMPLIED, false));
133: attrList.add(new Attribute(VSCROLLPOLICY,
134: new AttributeTypeEnumeration(mVScrollPolicy, AUTO),
135: IMPLIED, false));
136: attrList.add(new Attribute(VIEWPORTBORDER,
137: new AttributeTypeMethodCall(Border.class, l,
138: EVENT_AWARE), IMPLIED, false));
139:
140: }
141: }
142:
143: /**
144: * @see org.gui4j.core.Gui4jComponentFactory#getName()
145: */
146: public String getName() {
147: return NAME;
148: }
149:
150: }
|