001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Dmitry A. Durnev
019: * @version $Revision$
020: */package java.awt;
021:
022: import javax.accessibility.Accessible;
023: import javax.accessibility.AccessibleContext;
024: import javax.accessibility.AccessibleRole;
025:
026: import org.apache.harmony.awt.internal.nls.Messages;
027: import org.apache.harmony.awt.state.LabelState;
028:
029: public class Label extends Component implements Accessible {
030:
031: private static final long serialVersionUID = 3094126758329070636L;
032:
033: protected class AccessibleAWTLabel extends
034: Component.AccessibleAWTComponent {
035:
036: private static final long serialVersionUID = -3568967560160480438L;
037:
038: public AccessibleAWTLabel() {
039: // define default constructor explicitly just to make it public
040: }
041:
042: @Override
043: public String getAccessibleName() {
044: return Label.this .getText();
045: }
046:
047: @Override
048: public AccessibleRole getAccessibleRole() {
049: return AccessibleRole.LABEL;
050: }
051: }
052:
053: class State extends Component.ComponentState implements LabelState {
054:
055: final Dimension textSize = new Dimension();
056:
057: public String getText() {
058: return text;
059: }
060:
061: public Dimension getTextSize() {
062: return textSize;
063: }
064:
065: public void setTextSize(Dimension size) {
066: textSize.width = size.width;
067: textSize.height = size.height;
068: }
069:
070: public int getAlignment() {
071: return alignment;
072: }
073:
074: @Override
075: public void calculate() {
076: toolkit.theme.calculateLabel(state);
077: }
078:
079: }
080:
081: public static final int LEFT = 0;
082:
083: public static final int CENTER = 1;
084:
085: public static final int RIGHT = 2;
086:
087: private String text = null;
088:
089: private int alignment = LEFT;
090:
091: Color savedBackground;
092:
093: final State state = new State();
094:
095: public Label(String text) throws HeadlessException {
096: this (text, LEFT);
097: toolkit.lockAWT();
098: try {
099: } finally {
100: toolkit.unlockAWT();
101: }
102: }
103:
104: public Label() throws HeadlessException {
105: this (new String(""), LEFT); //$NON-NLS-1$
106: toolkit.lockAWT();
107: try {
108: } finally {
109: toolkit.unlockAWT();
110: }
111: }
112:
113: public Label(String text, int alignment) throws HeadlessException {
114: toolkit.lockAWT();
115: try {
116: this .text = text;
117: setAlignmentImpl(alignment);
118: } finally {
119: toolkit.unlockAWT();
120: }
121: }
122:
123: @Override
124: protected String paramString() {
125: /* The format is based on 1.5 release behavior
126: * which can be revealed by the following code:
127: * System.out.println(new Label());
128: */
129:
130: toolkit.lockAWT();
131: try {
132: return (super .paramString() + ",align=" + getAlignString() + //$NON-NLS-1$
133: ",text=" + text); //$NON-NLS-1$
134: } finally {
135: toolkit.unlockAWT();
136: }
137: }
138:
139: private String getAlignString() {
140: String alignStr = "left"; //$NON-NLS-1$
141: switch (alignment) {
142: case CENTER:
143: alignStr = "center"; //$NON-NLS-1$
144: break;
145: case RIGHT:
146: alignStr = "right"; //$NON-NLS-1$
147: break;
148: }
149: return alignStr;
150: }
151:
152: @Override
153: public void addNotify() {
154: toolkit.lockAWT();
155: try {
156: super .addNotify();
157: } finally {
158: toolkit.unlockAWT();
159: }
160: }
161:
162: @Override
163: public AccessibleContext getAccessibleContext() {
164: toolkit.lockAWT();
165: try {
166: return super .getAccessibleContext();
167: } finally {
168: toolkit.unlockAWT();
169: }
170: }
171:
172: public int getAlignment() {
173: toolkit.lockAWT();
174: try {
175: return alignment;
176: } finally {
177: toolkit.unlockAWT();
178: }
179: }
180:
181: public String getText() {
182: toolkit.lockAWT();
183: try {
184: return text;
185: } finally {
186: toolkit.unlockAWT();
187: }
188: }
189:
190: public void setAlignment(int alignment) {
191: toolkit.lockAWT();
192: try {
193: setAlignmentImpl(alignment);
194: doRepaint();
195: } finally {
196: toolkit.unlockAWT();
197: }
198: }
199:
200: private void setAlignmentImpl(int alignment) {
201: if ((alignment < LEFT) || (alignment > RIGHT)) {
202: // awt.10F=improper alignment: {0}
203: throw new IllegalArgumentException(Messages.getString(
204: "awt.10F", //$NON-NLS-1$
205: alignment));
206: }
207: this .alignment = alignment;
208: }
209:
210: public void setText(String text) {
211: toolkit.lockAWT();
212: try {
213: this .text = text;
214: doRepaint();
215: } finally {
216: toolkit.unlockAWT();
217: }
218: }
219:
220: private void doRepaint() {
221: if (isDisplayable()) {
222: invalidate();
223: if (isShowing()) {
224: repaint();
225: }
226: }
227: }
228:
229: @Override
230: void prepaint(Graphics g) {
231: toolkit.theme.drawLabel(g, state);
232: }
233:
234: @Override
235: boolean isPrepainter() {
236: return true;
237: }
238:
239: @Override
240: String autoName() {
241: return ("label" + toolkit.autoNumber.nextLabel++); //$NON-NLS-1$
242: }
243:
244: @Override
245: void validateImpl() {
246: super .validateImpl();
247: toolkit.theme.calculateLabel(state);
248: }
249:
250: @Override
251: void setEnabledImpl(boolean value) {
252: if (value != isEnabled()) { // to avoid dead loop in repaint()
253: super .setEnabledImpl(value);
254: repaint();
255: }
256: }
257:
258: @Override
259: ComponentBehavior createBehavior() {
260: return new HWBehavior(this );
261: }
262:
263: @Override
264: Dimension getDefaultMinimumSize() {
265: if (getFont() == null) {
266: return new Dimension(0, 0);
267: }
268: return state.getDefaultMinimumSize();
269: }
270:
271: @Override
272: void resetDefaultSize() {
273: state.reset();
274: }
275:
276: @Override
277: AccessibleContext createAccessibleContext() {
278: return new AccessibleAWTLabel();
279: }
280: }
|