001: /*
002: * Copyright (c) 2004 JETA Software, Inc. All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without modification,
005: * are permitted provided that the following conditions are met:
006: *
007: * o Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * o Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * o Neither the name of JETA Software nor the names of its contributors may
015: * be used to endorse or promote products derived from this software without
016: * specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
021: * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
022: * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
023: * INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
024: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
025: * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
026: * INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
027: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
028: */
029:
030: package com.jeta.forms.gui.beans.factories;
031:
032: import java.awt.Component;
033:
034: import javax.swing.JLabel;
035: import javax.swing.SwingConstants;
036:
037: import com.jeta.forms.components.label.JETALabel;
038: import com.jeta.forms.gui.beans.BeanProperties;
039: import com.jeta.forms.gui.beans.JETABean;
040: import com.jeta.forms.gui.common.FormException;
041: import com.jeta.forms.store.properties.TransformOptionsProperty;
042: import com.jeta.forms.store.properties.effects.PaintProperty;
043: import com.jeta.forms.support.UserProperties;
044: import com.jeta.open.registry.JETARegistry;
045:
046: /**
047: * Factory for instantiating a JLabel bean.
048: *
049: * @author Jeff Tassin
050: */
051: public class LabelBeanFactory extends JComponentBeanFactory {
052: /**
053: * User property for default horizontal alignment.
054: */
055: public static final String ID_DEFAULT_HORIZONTAL_ALIGNMENT = "label.h.align";
056:
057: /**
058: * ctor
059: */
060: public LabelBeanFactory() {
061: super (JETALabel.class);
062: }
063:
064: /**
065: * Override to set custom properties for your factory
066: */
067: public void defineProperties(BeanProperties props) {
068: super .defineProperties(props);
069: TransformOptionsProperty hprop = new TransformOptionsProperty(
070: "horizontalAlignment",
071: "getHorizontalAlignment",
072: "setHorizontalAlignment",
073: new Object[][] {
074: { "LEFT", new Integer(SwingConstants.LEFT) },
075: { "CENTER", new Integer(SwingConstants.CENTER) },
076: { "RIGHT", new Integer(SwingConstants.RIGHT) },
077: { "LEADING",
078: new Integer(SwingConstants.LEADING) },
079: { "TRAILING",
080: new Integer(SwingConstants.TRAILING) } });
081:
082: TransformOptionsProperty htprop = new TransformOptionsProperty(
083: "horizontalTextPosition",
084: "getHorizontalTextPosition",
085: "setHorizontalTextPosition",
086: new Object[][] {
087: { "LEFT", new Integer(SwingConstants.LEFT) },
088: { "CENTER", new Integer(SwingConstants.CENTER) },
089: { "RIGHT", new Integer(SwingConstants.RIGHT) },
090: { "LEADING",
091: new Integer(SwingConstants.LEADING) },
092: { "TRAILING",
093: new Integer(SwingConstants.TRAILING) } });
094:
095: TransformOptionsProperty vprop = new TransformOptionsProperty(
096: "verticalAlignment",
097: "getVerticalAlignment",
098: "setVerticalAlignment",
099: new Object[][] {
100: { "TOP", new Integer(SwingConstants.TOP) },
101: { "CENTER", new Integer(SwingConstants.CENTER) },
102: { "BOTTOM", new Integer(SwingConstants.BOTTOM) } });
103:
104: TransformOptionsProperty vtprop = new TransformOptionsProperty(
105: "verticalTextPosition",
106: "getVerticalTextPosition",
107: "setVerticalTextPosition",
108: new Object[][] {
109: { "TOP", new Integer(SwingConstants.TOP) },
110: { "CENTER", new Integer(SwingConstants.CENTER) },
111: { "BOTTOM", new Integer(SwingConstants.BOTTOM) } });
112: htprop.setPreferred(false);
113: vtprop.setPreferred(false);
114:
115: props.register(hprop);
116: props.register(htprop);
117: props.register(vprop);
118: props.register(vtprop);
119: props.register(new PaintProperty());
120:
121: /** make font preferred since it is used often in labels */
122: props.setPreferred("font", true);
123:
124: }
125:
126: public JETABean createBean(String compName,
127: boolean instantiateBean, boolean setDefaults)
128: throws FormException {
129: JETABean jbean = super .createBean(compName, instantiateBean,
130: setDefaults);
131: Component comp = jbean.getDelegate();
132: if (comp instanceof JLabel) {
133: JLabel label = (JLabel) comp;
134: String bname = getShortBeanClassName();
135: if ("JETALabel".equals(bname))
136: bname = "JLabel";
137:
138: if (setDefaults)
139: label.setText(bname);
140:
141: try {
142: UserProperties props = (UserProperties) JETARegistry
143: .lookup(UserProperties.COMPONENT_ID);
144: if (props != null) {
145: String default_halign = props.getProperty(
146: ID_DEFAULT_HORIZONTAL_ALIGNMENT, null);
147: if (default_halign != null && setDefaults) {
148: label
149: .setHorizontalAlignment(getHorizontalAlignment(default_halign));
150: }
151: }
152: } catch (Exception e) {
153: // ignore here
154: }
155: }
156: return jbean;
157: }
158:
159: /**
160: * @return the JLabel horizontal alignment string from the given integer
161: * value
162: */
163: public static String getHorizontalAlignmentString(int halign) {
164: if (halign == SwingConstants.LEFT)
165: return "LEFT";
166: else if (halign == SwingConstants.CENTER)
167: return "CENTER";
168: else if (halign == SwingConstants.RIGHT)
169: return "RIGHT";
170: else if (halign == SwingConstants.TRAILING)
171: return "TRAILING";
172: else if (halign == SwingConstants.LEADING)
173: return "LEADING";
174: else
175: return "LEFT";
176: }
177:
178: /**
179: * @return the JLabel horizontal alignment string from the given integer
180: * value
181: */
182: public static int getHorizontalAlignment(String halign) {
183: if ("LEFT".equalsIgnoreCase(halign))
184: return SwingConstants.LEFT;
185: else if ("CENTER".equalsIgnoreCase(halign))
186: return SwingConstants.CENTER;
187: else if ("RIGHT".equalsIgnoreCase(halign))
188: return SwingConstants.RIGHT;
189: else if ("TRAILING".equalsIgnoreCase(halign))
190: return SwingConstants.TRAILING;
191: else if ("LEADING".equalsIgnoreCase(halign))
192: return SwingConstants.LEADING;
193: else
194: return SwingConstants.LEFT;
195: }
196:
197: }
|