1 /*
2 * Copyright 2006-2016 The JGUIraffe Team.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License")
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package net.sf.jguiraffe.gui.platform.swing.builder.components;
17
18 import javax.swing.Icon;
19 import javax.swing.JLabel;
20
21 import net.sf.jguiraffe.gui.builder.components.model.StaticTextData;
22 import net.sf.jguiraffe.gui.builder.components.model.StaticTextHandler;
23 import net.sf.jguiraffe.gui.builder.components.model.TextIconAlignment;
24 import net.sf.jguiraffe.gui.builder.components.tags.StaticTextDataImpl;
25
26 /**
27 * <p>
28 * A concrete <code>ComponentHandler</code> implementation for dealing with
29 * static text elements.
30 * </p>
31 * <p>
32 * This implementation maintains a label component whose properties can be
33 * queried or altered using
34 * {@link net.sf.jguiraffe.gui.builder.components.model.StaticTextData
35 * StaticTextData} objects.
36 * </p>
37 *
38 * @author Oliver Heger
39 * @version $Id: SwingStaticTextComponentHandler.java 205 2012-01-29 18:29:57Z oheger $
40 */
41 class SwingStaticTextComponentHandler extends
42 SwingComponentHandler<StaticTextData> implements StaticTextHandler
43 {
44 /**
45 * Creates a new instance of <code>SwingStaticTextComponentHandler</code>
46 * that wraps the passed in label component.
47 *
48 * @param label the component to be managed by this handler
49 */
50 public SwingStaticTextComponentHandler(JLabel label)
51 {
52 super(label);
53 }
54
55 /**
56 * Returns the internally wrapped label component.
57 *
58 * @return the underlying label
59 */
60 public JLabel getLabel()
61 {
62 return (JLabel) getJComponent();
63 }
64
65 /**
66 * Returns a data object for this component. This is a
67 * <code>StaticTextData</code> object in this case.
68 *
69 * @return a data object for this component
70 */
71 public StaticTextData getData()
72 {
73 StaticTextData data = new StaticTextDataImpl();
74 data.setAlignment(getAlignment());
75 data.setIcon(getIcon());
76 data.setText(getText());
77 return data;
78 }
79
80 /**
81 * Returns the data type of this component handler.
82 *
83 * @return the data type
84 */
85 public Class<?> getType()
86 {
87 return StaticTextData.class;
88 }
89
90 /**
91 * Sets data for this component. The passed in data object can be of various
92 * types.
93 *
94 * @param data the data object
95 */
96 public void setData(StaticTextData data)
97 {
98 if (data == null)
99 {
100 setText(null);
101 setIcon(null);
102 setAlignment(TextIconAlignment.LEFT);
103 }
104 else
105 {
106 setAlignment(data.getAlignment());
107 setIcon(data.getIcon());
108 setText(data.getText());
109 }
110 }
111
112 /**
113 * Returns the alignment of the managed label.
114 *
115 * @return the alignment
116 */
117 public TextIconAlignment getAlignment()
118 {
119 try
120 {
121 return SwingComponentManager.transformSwingAlign(getLabel()
122 .getHorizontalAlignment());
123 }
124 catch (IllegalArgumentException iex)
125 {
126 // obviously no standard alignment => set default
127 return TextIconAlignment.LEFT;
128 }
129 }
130
131 /**
132 * Returns the label's icon.
133 *
134 * @return the icon
135 */
136 public Object getIcon()
137 {
138 return getLabel().getIcon();
139 }
140
141 /**
142 * Returns the text of the managed label.
143 *
144 * @return the text
145 */
146 public String getText()
147 {
148 return getLabel().getText();
149 }
150
151 /**
152 * Sets the alignment of the managed label.
153 *
154 * @param alignment the new alignment
155 */
156 public void setAlignment(TextIconAlignment alignment)
157 {
158 getLabel().setHorizontalAlignment(
159 SwingComponentManager.transformAlign(alignment));
160 }
161
162 /**
163 * Sets the icon of the managed label. The passed in object must implement
164 * the <code>Icon</code> interface of Swing.
165 *
166 * @param icon the new icon
167 */
168 public void setIcon(Object icon)
169 {
170 getLabel().setIcon((Icon) icon);
171 }
172
173 /**
174 * Sets the text of the managed label.
175 *
176 * @param s the new text
177 */
178 public void setText(String s)
179 {
180 getLabel().setText(s);
181 }
182 }