KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: ActionAttributes.java,v 1.2 2004/07/23 04:15:07 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.net.URL JavaDoc;
11
12 import javax.swing.Action JavaDoc;
13 import javax.swing.Icon JavaDoc;
14 import javax.swing.ImageIcon JavaDoc;
15 import javax.swing.KeyStroke JavaDoc;
16
17 import org.jdesktop.swing.Application;
18 import org.jdesktop.swing.actions.AbstractActionExt;
19
20 import net.openmarkup.ApplierException;
21 import net.openmarkup.AttributeApplier;
22 import net.openmarkup.Realizable;
23 import net.openmarkup.Scribe;
24
25 /**
26  * Attribute appliers for the Action element
27  *
28  * @author Ramesh Gupta
29  * @author Mark Davidson
30  */

31 public class ActionAttributes {
32 /*
33     public static final AttributeApplier idApplier = new AttributeApplier() {
34         public void apply(Realizable target, String namespaceURI,
35                           String attributeName, String attributeValue) throws
36             ApplierException {
37             Action action = (Action) target.getObject();
38             action.putValue(Action.ACTION_COMMAND_KEY, attributeValue);
39         }
40     };
41 */

42     public static final AttributeApplier titleApplier = new AttributeApplier() {
43         public void apply(Realizable target, String JavaDoc namespaceURI,
44                           String JavaDoc attributeName, String JavaDoc attributeValue) throws
45             ApplierException {
46             Action JavaDoc action = (Action JavaDoc) target.getObject();
47             action.putValue(Action.NAME, attributeValue);
48         }
49     };
50
51     public static final AttributeApplier isToggleApplier = new AttributeApplier() {
52         public void apply(Realizable target, String JavaDoc namespaceURI,
53                           String JavaDoc attributeName, String JavaDoc attributeValue) throws
54             ApplierException {
55             AbstractActionExt action = (AbstractActionExt) target.getObject();
56             action.setStateAction(Boolean.valueOf(attributeValue).booleanValue());
57         }
58     };
59
60     public static final AttributeApplier groupApplier = new AttributeApplier() {
61         public void apply(Realizable target, String JavaDoc namespaceURI,
62                           String JavaDoc attributeName, String JavaDoc attributeValue) throws
63             ApplierException {
64             AbstractActionExt action = (AbstractActionExt) target.getObject();
65             action.setGroup(attributeValue);
66         }
67     };
68
69     public static final AttributeApplier mnemonicApplier = new AttributeApplier() {
70         public void apply(Realizable target, String JavaDoc namespaceURI,
71                           String JavaDoc attributeName, String JavaDoc attributeValue) throws
72             ApplierException {
73             Action JavaDoc action = (Action JavaDoc) target.getObject();
74             action.putValue(Action.MNEMONIC_KEY,
75                             new Integer JavaDoc(attributeValue.charAt(0)));
76         }
77     };
78
79     public static final AttributeApplier iconApplier = new AttributeApplier() {
80         public void apply(Realizable target, String JavaDoc namespaceURI,
81                           String JavaDoc attributeName, String JavaDoc attributeValue) throws
82             ApplierException {
83             Action JavaDoc action = (Action JavaDoc) target.getObject();
84
85             Icon JavaDoc icon = ActionAttributes.getIcon(target, attributeValue);
86             action.putValue(AbstractActionExt.LARGE_ICON, icon);
87         }
88     };
89
90     // If the URI represents a valid URL or classpath then create an
91
// ImageIcon. Otherwise, return null.
92
public static Icon JavaDoc getIcon(Realizable target, String JavaDoc uri) {
93         URL JavaDoc url = ActionAttributes.class.getResource(uri);
94         if (url == null) {
95             url = target.getResolvedURL(uri);
96         }
97         Icon JavaDoc icon = null;
98         try {
99             // test to see if the url is valid
100
url.openStream();
101             // icon is in a place which is relative to the document
102
icon = new ImageIcon JavaDoc(url);
103         }
104         catch (Exception JavaDoc ex) {
105         Scribe.getLogger().warning("Error getting icon: " + url.toExternalForm());
106             // XXX - this should be consolidated with getResolvedURL.
107
// getResolvedURL should produce a URL from a
108
// classpath: /com/sun/foo/resources/Foo/gif
109
// It doesn't make sense to use the Action as the basis for
110
// the url
111
icon = Application.getIcon(uri, (Action JavaDoc) target.getObject());
112         }
113         return icon;
114     }
115
116     public static final AttributeApplier smallIconApplier = new
117         AttributeApplier() {
118         public void apply(Realizable target, String JavaDoc namespaceURI,
119                           String JavaDoc attributeName, String JavaDoc attributeValue) throws
120             ApplierException {
121             Action JavaDoc action = (Action JavaDoc) target.getObject();
122             action.putValue(Action.SMALL_ICON,
123                             ActionAttributes.getIcon(target, attributeValue));
124         }
125     };
126
127     public static final AttributeApplier acceleratorApplier = new
128         AttributeApplier() {
129         public void apply(Realizable target, String JavaDoc namespaceURI,
130                           String JavaDoc attributeName, String JavaDoc attributeValue) throws
131             ApplierException {
132             Action JavaDoc action = (Action JavaDoc) target.getObject();
133             action.putValue(Action.ACCELERATOR_KEY,
134                             KeyStroke.getKeyStroke(attributeValue));
135         }
136     };
137
138     public static final AttributeApplier descriptionApplier = new
139         AttributeApplier() {
140         public void apply(Realizable target, String JavaDoc namespaceURI,
141                           String JavaDoc attributeName, String JavaDoc attributeValue) throws
142             ApplierException {
143             Action JavaDoc action = (Action JavaDoc) target.getObject();
144             action.putValue(Action.SHORT_DESCRIPTION, attributeValue);
145             // TODO: Perhaps add a long description attribute
146
action.putValue(Action.LONG_DESCRIPTION, attributeValue);
147         }
148     };
149
150     // TODO: The delegate attribute will probably go away.
151
public static final AttributeApplier delegateApplier = new AttributeApplier() {
152         public void apply(Realizable target, String JavaDoc namespaceURI,
153                           String JavaDoc attributeName, String JavaDoc attributeValue) throws
154             ApplierException {
155             Action JavaDoc action = (Action JavaDoc) target.getObject();
156             Object JavaDoc delegate = BaseAttribute.getReferencedObject(target,
157                 attributeValue);
158             action.putValue("delegate", delegate);
159         }
160     };
161 }
162
Popular Tags