KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > jdnc > markup > attr > LabelAttributes


1 /*
2  * $Id: LabelAttributes.java,v 1.2 2004/09/02 00:50:35 aim Exp $
3  *
4  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
5  * Santa Clara, California 95054, U.S.A. All rights reserved.
6  */

7
8 package org.jdesktop.jdnc.markup.attr;
9
10 import java.net.URL JavaDoc;
11
12 import javax.swing.Icon JavaDoc;
13 import javax.swing.ImageIcon JavaDoc;
14 import javax.swing.JLabel JavaDoc;
15 import javax.swing.SwingConstants JavaDoc;
16
17 import net.openmarkup.ApplierException;
18 import net.openmarkup.AttributeApplier;
19 import net.openmarkup.Realizable;
20
21 import org.jdesktop.jdnc.markup.Namespace;
22
23 /**
24  * @author Ramesh Gupta
25  */

26 public class LabelAttributes {
27
28     public static void applyHorizontalAlignment(JLabel JavaDoc label, String JavaDoc attributeValue) {
29         // Per W3C Internationalization guidelines, we now support "before" as a
30
// synonym for "leading", and "after" as a synonym for "trailing"
31

32         int value = Decoder.decodeConstant(attributeValue);
33         if (value != -1) {
34             label.setHorizontalAlignment(value);
35         }
36         /**@remind:aim: warning if value not recognized? */
37
38     }
39
40     public static void applyHorizontalTextPosition(JLabel JavaDoc label, String JavaDoc attributeValue) {
41         // Per W3C Internationalization guidelines, we now support "before" as a
42
// synonym for "leading", and "after" as a synonym for "trailing"
43

44         int value = Decoder.decodeConstant(attributeValue);
45         if (value != -1) {
46             label.setHorizontalTextPosition(value);
47         }
48     }
49
50     public static void applyIcon(Realizable target, JLabel JavaDoc label, String JavaDoc attributeValue) {
51         try {
52             URL JavaDoc url = target.getResolvedURL(attributeValue);
53             if (url == null) {
54                 System.out.println("Could not resolve URL: " + attributeValue);
55             }
56             else {
57                 //System.out.println("Get Icon from: " + url);
58
Icon JavaDoc icon = new ImageIcon JavaDoc(url);
59                 label.setIcon(icon);
60             }
61         }
62         catch (Exception JavaDoc ex) {
63             System.out.println("LabelAttributes.iconApplier: " + ex);
64         }
65     }
66
67     public static void applyIconTextGap(JLabel JavaDoc label, String JavaDoc attributeValue) {
68         try {
69             int value = Integer.parseInt(attributeValue);
70             label.setIconTextGap(value);
71         }
72         catch (Exception JavaDoc ex) {
73             System.out.println("LabelAttributes.iconTextGapApplier: " + ex);
74         }
75     }
76
77     public static void applyVerticalAlignment(JLabel JavaDoc label, String JavaDoc attributeValue) {
78         int value = Decoder.decodeConstant(attributeValue);
79         if (value != -1) {
80             label.setVerticalAlignment(value);
81         }
82     }
83
84     public static void applyVerticalTextPosition(JLabel JavaDoc label, String JavaDoc attributeValue) {
85         int value = Decoder.decodeConstant(attributeValue);
86         if (value != -1) {
87             label.setVerticalTextPosition(value);
88         }
89     }
90
91     public static final AttributeApplier horizontalAlignmentApplier = new AttributeApplier() {
92         public void apply(Realizable target, String JavaDoc namespaceURI,
93                           String JavaDoc attributeName, String JavaDoc attributeValue) {
94             applyHorizontalAlignment((JLabel JavaDoc)target.getObject(), attributeValue);
95         }
96     };
97
98     public static final AttributeApplier horizontalTextPositionApplier = new AttributeApplier() {
99         public void apply(Realizable target, String JavaDoc namespaceURI,
100                           String JavaDoc attributeName, String JavaDoc attributeValue) {
101             applyHorizontalTextPosition((JLabel JavaDoc)target.getObject(), attributeValue);
102         }
103     };
104
105     public static final AttributeApplier iconApplier = new AttributeApplier() {
106         public void apply(Realizable target, String JavaDoc namespaceURI,
107                           String JavaDoc attributeName, String JavaDoc attributeValue) throws ApplierException {
108             applyIcon(target, (JLabel JavaDoc)target.getObject(), attributeValue);
109         }
110     };
111
112     public static final AttributeApplier iconTextGapApplier = new AttributeApplier() {
113         public void apply(Realizable target, String JavaDoc namespaceURI,
114                           String JavaDoc attributeName, String JavaDoc attributeValue) throws
115             ApplierException {
116             applyIconTextGap((JLabel JavaDoc)target.getObject(), attributeValue);
117         }
118     };
119
120     public static final AttributeApplier titleApplier = new AttributeApplier() {
121         public void apply(Realizable target, String JavaDoc namespaceURI,
122                           String JavaDoc attributeName, String JavaDoc attributeValue) {
123             ((JLabel JavaDoc)target.getObject()).setText(attributeValue);
124         }
125     };
126
127     public static final AttributeApplier verticalAlignmentApplier = new AttributeApplier() {
128         public void apply(Realizable target, String JavaDoc namespaceURI,
129                           String JavaDoc attributeName, String JavaDoc attributeValue) {
130             applyVerticalAlignment((JLabel JavaDoc)target.getObject(), attributeValue);
131         }
132     };
133
134     public static final AttributeApplier verticalTextPositionApplier = new AttributeApplier() {
135         public void apply(Realizable target, String JavaDoc namespaceURI,
136                           String JavaDoc attributeName, String JavaDoc attributeValue) {
137             applyVerticalTextPosition( (JLabel JavaDoc) target.getObject(),
138                                       attributeValue);
139         }
140     };
141
142 }
143
Popular Tags