KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: ComponentAttributes.java,v 1.1.1.1 2004/06/16 01:43:40 davidson1 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.awt.Color JavaDoc;
11 import java.awt.Component JavaDoc;
12 import java.awt.Dimension JavaDoc;
13 import java.awt.Font JavaDoc;
14
15 import javax.swing.JComponent JavaDoc;
16
17 import net.openmarkup.AttributeApplier;
18 import net.openmarkup.Realizable;
19
20 import org.jdesktop.jdnc.markup.Attributes;
21 import org.jdesktop.jdnc.markup.Namespace;
22
23 /**
24  * @author Ramesh Gupta
25  * @author Amy Fowler
26  */

27 public class ComponentAttributes {
28
29     public static void applyBackground(Component JavaDoc component, String JavaDoc attributeValue) {
30         Color JavaDoc value = Decoder.decodeColor(attributeValue);
31         if (value != null) {
32             component.setBackground(value);
33         }
34     }
35
36     public static void applyForeground(Component JavaDoc component, String JavaDoc attributeValue) {
37         Color JavaDoc value = Decoder.decodeColor(attributeValue);
38          if (value != null) {
39              component.setForeground(value);
40          }
41     }
42
43     // ...
44

45     public static final AttributeApplier backgroundApplier = new AttributeApplier() {
46         public void apply(Realizable target, String JavaDoc namespaceURI,
47                           String JavaDoc attributeName, String JavaDoc attributeValue) {
48             applyBackground((Component JavaDoc)target.getObject(), attributeValue);
49         }
50     };
51
52     public static final AttributeApplier isEnabledApplier = new AttributeApplier() {
53         public void apply(Realizable target, String JavaDoc namespaceURI,
54                           String JavaDoc attributeName, String JavaDoc attributeValue) {
55             Component JavaDoc component = (Component JavaDoc) target.getObject();
56             boolean isEnabled = Boolean.valueOf(attributeValue).booleanValue();
57             component.setEnabled(isEnabled);
58         }
59     };
60
61     public static final AttributeApplier isVisibleApplier = new AttributeApplier() {
62         public void apply(Realizable target, String JavaDoc namespaceURI,
63                           String JavaDoc attributeName, String JavaDoc attributeValue) {
64             Component JavaDoc component = (Component JavaDoc) target.getObject();
65             boolean isVisible = Boolean.valueOf(attributeValue).booleanValue();
66             component.setVisible(isVisible);
67         }
68     };
69
70     public static final AttributeApplier fontApplier = new AttributeApplier() {
71         public void apply(Realizable target, String JavaDoc namespaceURI,
72                           String JavaDoc attributeName, String JavaDoc attributeValue) {
73             Component JavaDoc component = (Component JavaDoc) target.getObject();
74             Font JavaDoc font = (Font JavaDoc)BaseAttribute.getReferencedObject(target, attributeValue);
75             component.setFont(font);
76         }
77     };
78
79     public static final AttributeApplier foregroundApplier = new
80         AttributeApplier() {
81         public void apply(Realizable target, String JavaDoc namespaceURI,
82                           String JavaDoc attributeName, String JavaDoc attributeValue) {
83             applyForeground( (Component JavaDoc) target.getObject(), attributeValue);
84         }
85     };
86
87     public static final AttributeApplier nameApplier = new AttributeApplier() {
88         public void apply(Realizable target, String JavaDoc namespaceURI,
89                           String JavaDoc attributeName, String JavaDoc attributeValue) {
90             JComponent JavaDoc component = (JComponent JavaDoc) target.getObject();
91             component.setName(attributeValue);
92         }
93      };
94
95      public static final AttributeApplier preferredSizeApplier = new AttributeApplier() {
96          public void apply(Realizable target, String JavaDoc namespaceURI,
97                            String JavaDoc attributeName, String JavaDoc attributeValue) {
98              String JavaDoc[] args = attributeValue.split("\\s");
99              if (args.length == 2) {
100                  Dimension JavaDoc size = new Dimension JavaDoc(
101                      Integer.parseInt(args[0]), Integer.parseInt(args[1]));
102                  JComponent JavaDoc component = (JComponent JavaDoc) target.getObject();
103                  component.setPreferredSize(size);
104              }
105          }
106     };
107
108     public static final AttributeApplier sizeApplier = new AttributeApplier() {
109         public void apply(Realizable target, String JavaDoc namespaceURI,
110                           String JavaDoc attributeName, String JavaDoc attributeValue) {
111             String JavaDoc[] args = attributeValue.split("\\s");
112             if (args.length == 2) {
113                 Dimension JavaDoc size = new Dimension JavaDoc(
114                     Integer.parseInt(args[0]), Integer.parseInt(args[1]));
115                 JComponent JavaDoc component = (JComponent JavaDoc) target.getObject();
116                 component.setSize(size);
117             }
118         }
119     };
120
121     // ...
122
}
123
Popular Tags