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: package javax.swing;
019:
020: import java.awt.Component;
021: import java.awt.Image;
022: import java.awt.Point;
023: import java.awt.Rectangle;
024: import java.awt.event.ActionEvent;
025: import java.awt.event.InputEvent;
026: import javax.accessibility.Accessible;
027: import javax.accessibility.AccessibleContext;
028: import javax.accessibility.AccessibleRole;
029: import javax.accessibility.AccessibleText;
030: import javax.swing.plaf.LabelUI;
031: import javax.swing.text.AttributeSet;
032: import org.apache.harmony.luni.util.NotImplementedException;
033: import org.apache.harmony.x.swing.StringConstants;
034: import org.apache.harmony.x.swing.Utilities;
035:
036: import org.apache.harmony.x.swing.internal.nls.Messages;
037:
038: /**
039: * <p>
040: * <i>JLabel</i>
041: * </p>
042: * <h3>Implementation Notes:</h3>
043: * <ul>
044: * <li>The <code>serialVersionUID</code> fields are explicitly declared as a performance
045: * optimization, not as a guarantee of serialization compatibility.</li>
046: * </ul>
047: */
048: public class JLabel extends JComponent implements SwingConstants,
049: Accessible {
050: private static final long serialVersionUID = 1992522068352176692L;
051:
052: protected Component labelFor;
053:
054: private String text;
055:
056: private Icon icon;
057:
058: private Icon disabledIcon;
059:
060: private Icon defaultDisabledIcon;
061:
062: private int horizontalAlignment;
063:
064: private int horizontalTextPosition = TRAILING;
065:
066: private int verticalAlignment = CENTER;
067:
068: private int verticalTextPosition = CENTER;
069:
070: private int iconTextGap = 4;
071:
072: private int displayedMnemonicIndex = -1;
073:
074: private int displayedMnemonic;
075:
076: private AbstractAction labelForAction;
077:
078: private static final String UI_CLASS_ID = "LabelUI";
079:
080: private static final String DISPLAYED_MNEMONIC_CHANGED_PROPERTY = "displayedMnemonic";
081:
082: private static final String LABEL_FOR_CHANGED_PROPERTY = "labelFor";
083:
084: //TODO: implement
085: protected class AccessibleJLabel extends AccessibleJComponent
086: implements AccessibleText {
087: private static final long serialVersionUID = -3843546598023238741L;
088:
089: @Override
090: public AccessibleRole getAccessibleRole() {
091: return AccessibleRole.LABEL;
092: }
093:
094: public String getAfterIndex(final int part, final int index)
095: throws NotImplementedException {
096: throw new NotImplementedException();
097: }
098:
099: public String getAtIndex(final int part, final int index)
100: throws NotImplementedException {
101: throw new NotImplementedException();
102: }
103:
104: public String getBeforeIndex(final int part, final int index)
105: throws NotImplementedException {
106: throw new NotImplementedException();
107: }
108:
109: public int getCaretPosition() throws NotImplementedException {
110: throw new NotImplementedException();
111: }
112:
113: public AttributeSet getCharacterAttribute(final int i)
114: throws NotImplementedException {
115: throw new NotImplementedException();
116: }
117:
118: public Rectangle getCharacterBounds(final int i)
119: throws NotImplementedException {
120: throw new NotImplementedException();
121: }
122:
123: public int getCharCount() throws NotImplementedException {
124: throw new NotImplementedException();
125: }
126:
127: public int getIndexAtPoint(final Point p)
128: throws NotImplementedException {
129: throw new NotImplementedException();
130: }
131:
132: public String getSelectedText() throws NotImplementedException {
133: throw new NotImplementedException();
134: }
135:
136: public int getSelectionEnd() throws NotImplementedException {
137: throw new NotImplementedException();
138: }
139:
140: public int getSelectionStart() throws NotImplementedException {
141: throw new NotImplementedException();
142: }
143: }
144:
145: public JLabel() {
146: this ("");
147: }
148:
149: public JLabel(final Icon icon) {
150: this (icon, CENTER);
151: }
152:
153: public JLabel(final Icon icon, final int horizontalAlignment) {
154: this (null, icon, horizontalAlignment);
155: }
156:
157: public JLabel(final String text) {
158: this (text, LEADING);
159: }
160:
161: public JLabel(final String text, final int horizontalAlignment) {
162: this (text, null, horizontalAlignment);
163: }
164:
165: public JLabel(final String text, final Icon icon,
166: final int horizontalAlignment) {
167: checkHorizontalAligment(horizontalAlignment);
168: this .text = text;
169: this .icon = icon;
170: this .horizontalAlignment = horizontalAlignment;
171: updateUI();
172: }
173:
174: public void setUI(final LabelUI ui) {
175: super .setUI(ui);
176: }
177:
178: public LabelUI getUI() {
179: return (LabelUI) ui;
180: }
181:
182: @Override
183: public String getUIClassID() {
184: return UI_CLASS_ID;
185: }
186:
187: @Override
188: public void updateUI() {
189: setUI((LabelUI) UIManager.getUI(this ));
190: }
191:
192: public void setIcon(final Icon icon) {
193: Icon oldValue = this .icon;
194: this .icon = icon;
195: this .defaultDisabledIcon = null;
196: firePropertyChange(AbstractButton.ICON_CHANGED_PROPERTY,
197: oldValue, icon);
198: }
199:
200: public Icon getIcon() {
201: return icon;
202: }
203:
204: public void setDisabledIcon(final Icon icon) {
205: Icon oldValue = this .disabledIcon;
206: this .disabledIcon = icon;
207: firePropertyChange(
208: AbstractButton.DISABLED_ICON_CHANGED_PROPERTY,
209: oldValue, icon);
210: }
211:
212: public Icon getDisabledIcon() {
213: if (disabledIcon != null) {
214: return disabledIcon;
215: }
216: if (defaultDisabledIcon == null && icon instanceof ImageIcon) {
217: defaultDisabledIcon = new ImageIcon(GrayFilter
218: .createDisabledImage(((ImageIcon) icon).getImage()));
219: }
220: return defaultDisabledIcon;
221: }
222:
223: public void setText(final String text) {
224: String oldValue = this .text;
225: this .text = text;
226: firePropertyChange(AbstractButton.TEXT_CHANGED_PROPERTY,
227: oldValue, text);
228: setDisplayedMnemonicIndex(Utilities.getDisplayedMnemonicIndex(
229: text, Utilities
230: .keyCodeToKeyChar(getDisplayedMnemonic())));
231: }
232:
233: public String getText() {
234: return text;
235: }
236:
237: public void setLabelFor(final Component c) {
238: Component oldValue = this .labelFor;
239: this .labelFor = c;
240: firePropertyChange(LABEL_FOR_CHANGED_PROPERTY, oldValue, c);
241: }
242:
243: public Component getLabelFor() {
244: return labelFor;
245: }
246:
247: public void setIconTextGap(final int iconTextGap) {
248: LookAndFeel.markPropertyNotInstallable(this ,
249: StringConstants.ICON_TEXT_GAP_PROPERTY_CHANGED);
250: int oldValue = this .iconTextGap;
251: this .iconTextGap = iconTextGap;
252: firePropertyChange(
253: StringConstants.ICON_TEXT_GAP_PROPERTY_CHANGED,
254: oldValue, iconTextGap);
255: }
256:
257: public int getIconTextGap() {
258: return iconTextGap;
259: }
260:
261: public void setVerticalTextPosition(final int textPosition) {
262: checkVerticalKey(textPosition,
263: "incorrect vertical text position specified");
264: int oldValue = this .verticalTextPosition;
265: this .verticalTextPosition = textPosition;
266: firePropertyChange(
267: AbstractButton.VERTICAL_TEXT_POSITION_CHANGED_PROPERTY,
268: oldValue, textPosition);
269: }
270:
271: public int getVerticalTextPosition() {
272: return verticalTextPosition;
273: }
274:
275: public void setVerticalAlignment(final int alignment) {
276: checkVerticalKey(alignment,
277: "incorrect vertical aligment specified");
278: int oldValue = this .verticalAlignment;
279: this .verticalAlignment = alignment;
280: firePropertyChange(
281: AbstractButton.VERTICAL_ALIGNMENT_CHANGED_PROPERTY,
282: oldValue, alignment);
283: }
284:
285: public int getVerticalAlignment() {
286: return verticalAlignment;
287: }
288:
289: public void setHorizontalTextPosition(final int textPosition) {
290: checkHorizontalKey(textPosition,
291: "incorrect horizontal text position specified");
292: int oldValue = this .horizontalTextPosition;
293: this .horizontalTextPosition = textPosition;
294: firePropertyChange(
295: AbstractButton.HORIZONTAL_TEXT_POSITION_CHANGED_PROPERTY,
296: oldValue, textPosition);
297: }
298:
299: public int getHorizontalTextPosition() {
300: return horizontalTextPosition;
301: }
302:
303: public void setHorizontalAlignment(final int alignment) {
304: checkHorizontalAligment(alignment);
305: int oldValue = this .horizontalAlignment;
306: this .horizontalAlignment = alignment;
307: firePropertyChange(
308: AbstractButton.HORIZONTAL_ALIGNMENT_CHANGED_PROPERTY,
309: oldValue, alignment);
310: }
311:
312: public int getHorizontalAlignment() {
313: return horizontalAlignment;
314: }
315:
316: public void setDisplayedMnemonicIndex(final int index) {
317: if (index < -1 || index >= 0
318: && (text == null || index >= text.length())) {
319: throw new IllegalArgumentException(Messages
320: .getString("swing.14")); //$NON-NLS-1$
321: }
322: int oldValue = this .displayedMnemonicIndex;
323: this .displayedMnemonicIndex = index;
324: firePropertyChange(
325: StringConstants.MNEMONIC_INDEX_PROPERTY_CHANGED,
326: oldValue, index);
327: }
328:
329: public int getDisplayedMnemonicIndex() {
330: return displayedMnemonicIndex;
331: }
332:
333: public void setDisplayedMnemonic(final int key) {
334: setDisplayedMnemonic(key, Utilities.keyCodeToKeyChar(key));
335: }
336:
337: public void setDisplayedMnemonic(final char keyChar) {
338: setDisplayedMnemonic(Utilities.keyCharToKeyCode(keyChar),
339: keyChar);
340: }
341:
342: public int getDisplayedMnemonic() {
343: return displayedMnemonic;
344: }
345:
346: @Override
347: public AccessibleContext getAccessibleContext() {
348: if (accessibleContext == null) {
349: accessibleContext = new AccessibleJLabel();
350: }
351: return accessibleContext;
352: }
353:
354: @Override
355: public boolean imageUpdate(final Image image, final int infoflags,
356: final int x, final int y, final int w, final int h) {
357: Icon currentIcon = isEnabled() ? getIcon() : getDisabledIcon();
358: if (image != null && (currentIcon instanceof ImageIcon)
359: && !image.equals(((ImageIcon) currentIcon).getImage())) {
360: return false;
361: }
362: return super .imageUpdate(image, infoflags, x, y, w, h);
363: }
364:
365: protected int checkVerticalKey(final int key, final String message) {
366: return Utilities.checkVerticalKey(key, message);
367: }
368:
369: protected int checkHorizontalKey(final int key, final String message) {
370: return Utilities.checkHorizontalKey(key, message);
371: }
372:
373: private void setDisplayedMnemonic(final int keyCode,
374: final char keyChar) {
375: int oldValue = this .displayedMnemonic;
376: if (oldValue != keyCode) {
377: if (displayedMnemonic != 0) {
378: getInputMap(WHEN_IN_FOCUSED_WINDOW, true).remove(
379: KeyStroke.getKeyStroke(displayedMnemonic,
380: InputEvent.ALT_DOWN_MASK));
381: }
382: this .displayedMnemonic = keyCode;
383: String mnemonic = "mnemonic";
384: getInputMap(WHEN_IN_FOCUSED_WINDOW, true).put(
385: KeyStroke.getKeyStroke(keyCode,
386: InputEvent.ALT_DOWN_MASK), mnemonic);
387: getActionMap(true).put(mnemonic, getLabelForAction());
388: firePropertyChange(DISPLAYED_MNEMONIC_CHANGED_PROPERTY,
389: oldValue, keyCode);
390: setDisplayedMnemonicIndex(Utilities
391: .getDisplayedMnemonicIndex(text, keyChar));
392: }
393: }
394:
395: private void checkHorizontalAligment(final int alignment) {
396: checkHorizontalKey(alignment,
397: "Incorrect horizontal alignment is specified");
398: }
399:
400: private AbstractAction getLabelForAction() {
401: if (labelForAction == null) {
402: labelForAction = new AbstractAction() {
403: private static final long serialVersionUID = 1L;
404:
405: public void actionPerformed(final ActionEvent e) {
406: if (isVisible() && isEnabled() && labelFor != null
407: && labelFor.isVisible()
408: && labelFor.isEnabled()) {
409: labelFor.requestFocus();
410: }
411: }
412: };
413: }
414: return labelForAction;
415: }
416: }
|