1
2
3
4
5
6 package net.sf.navigator.displayer;
7
8 import net.sf.navigator.menu.MenuComponent;
9 import org.apache.commons.logging.Log;
10 import org.apache.commons.logging.LogFactory;
11 import org.apache.struts.util.MessageResources;
12
13 import javax.servlet.jsp.JspException;
14 import java.io.IOException;
15 import java.util.Locale;
16 import java.util.MissingResourceException;
17 import java.util.ResourceBundle;
18
19
20 /**
21 *
22 * @author ssayles
23 * @version
24 */
25 public abstract class MessageResourcesMenuDisplayer extends AbstractMenuDisplayer {
26 protected final transient Log log = LogFactory.getLog(getClass());
27
28
29
30 protected Object messages = null;
31 protected Locale locale = null;
32
33
34
35 public Locale getLocale() {
36 return locale;
37 }
38
39 public void setLocale(Locale locale) {
40 this.locale = locale;
41 }
42
43 public Object getMessageResources() {
44 return messages;
45 }
46
47 public void setMessageResources(Object messages) {
48 this.messages = messages;
49 }
50
51 /**
52 * Get the title key from the bundle (if it exists). This method
53 * is public to expose it to Velocity.
54 *
55 * @param key the key
56 */
57 public String getMessage(String key) {
58 String message = null;
59
60 if (messages != null && messages instanceof ResourceBundle) {
61 if (log.isDebugEnabled()) {
62 log.debug("Looking up string '" + key + "' in ResourceBundle");
63 }
64 ResourceBundle bundle = (ResourceBundle) messages;
65 try {
66 message = bundle.getString(key);
67 } catch (MissingResourceException mre) {
68 message = null;
69 }
70 } else if (messages != null) {
71 if (log.isDebugEnabled()) {
72 log.debug("Looking up message '" + key + "' in Struts' MessageResources");
73 }
74
75 if ("org.apache.struts.util.PropertyMessageResources".equals(messages.getClass().getName())) {
76 MessageResources resources = (MessageResources) messages;
77 try {
78 if (locale != null) {
79
80 message = resources.getMessage(locale, key);
81 } else {
82 message = resources.getMessage(key);
83 }
84 } catch (Throwable t) {
85 message = null;
86 }
87 }
88 } else {
89 message = key;
90 }
91
92 if (message == null) {
93 message = key;
94 }
95
96 return message;
97 }
98
99 /**
100 * Get the menu's target (if it exists). This method
101 * is public to expose it to Velocity.
102 *
103 * @param menu
104 */
105 public String getMenuTarget(MenuComponent menu) {
106 String menuTarget;
107
108 if (this.target != null) {
109 menuTarget = target;
110 } else {
111 if (menu.getTarget() != null) {
112 menuTarget = menu.getTarget();
113 } else {
114 menuTarget = MenuDisplayer._SELF;
115 }
116 }
117
118 return menuTarget;
119 }
120
121 /**
122 * Get the menu's tooltip (if it exists). This method
123 * is public to expose it to Velocity.
124 *
125 * @param menu
126 */
127 public String getMenuToolTip(MenuComponent menu) {
128 String tooltip;
129
130 if (menu.getToolTip() != null) {
131 tooltip = this.getMessage(menu.getToolTip());
132 } else {
133 tooltip = this.getMessage(menu.getTitle());
134 }
135
136 return tooltip;
137 }
138
139 public String getMenuOnClick(MenuComponent menu) {
140 if (menu.getOnclick() != null) {
141 return " onclick=\"" + menu.getOnclick() + "\"";
142 }
143 return "";
144 }
145
146 public abstract void display(MenuComponent menu)
147 throws JspException, IOException;
148 }