001: package org.osbl.client.wings.navigation;
002:
003: import org.osbl.client.wings.shell.*;
004: import org.osbl.client.wings.form.FormTool;
005: import org.osbl.client.wings.form.ObjectForm;
006: import org.osbl.client.wings.XLabel;
007: import org.osbl.client.wings.XIcons;
008: import org.osbl.client.wings.action.ShowEditorAction;
009: import org.osbl.client.ClientResourceProvider;
010: import org.osbl.*;
011: import org.wings.*;
012: import org.wings.session.SessionManager;
013: import org.wings.border.SEmptyBorder;
014: import org.wingx.XDivision;
015: import org.concern.LoaderException;
016: import org.conform.*;
017: import org.conform.format.Format;
018: import org.conform.format.NoFormat;
019: import org.apache.commons.logging.LogFactory;
020:
021: import javax.swing.*;
022: import java.util.*;
023: import java.net.URLEncoder;
024: import java.io.UnsupportedEncodingException;
025: import java.awt.event.ActionEvent;
026:
027: public class ReferenceTool extends AbstractTool implements FormTool {
028: private static org.apache.commons.logging.Log LOG = LogFactory
029: .getLog(ReferenceTool.class);
030:
031: XDivision panel;
032: private ObjectForm objectForm;
033: private XLabel links = new ReferencesLabel();
034: private ObjectLocators objectLocators;
035:
036: public ReferenceTool() {
037: putValue(Tool.NAME, Client.getInstance().getResourceProvider()
038: .getMessage("tool.references"));
039: }
040:
041: public void setForm(ObjectForm objectForm) {
042: this .objectForm = objectForm;
043: putValue(Tool.KEY, "Reference: "
044: + objectForm.getType().getName());
045: }
046:
047: public SComponent getComponent() {
048: if (panel == null) {
049: panel = new XDivision(new SGridLayout(2));
050: panel.setTitle((String) getValue(NAME));
051: panel.setBorder(new SEmptyBorder(8, 8, 8, 8));
052: panel.setPreferredSize(SDimension.FULLWIDTH);
053:
054: SButton button = new SButton(new RefreshAction());
055: button.setVerticalAlignment(SConstants.TOP_ALIGN);
056: button.setStyle("borderless");
057: panel.add(button);
058:
059: links.setVerticalAlignment(SConstants.TOP_ALIGN);
060: links.setAjaxLinking(true);
061: links.setWordWrap(true);
062: panel.add(links);
063: }
064: return panel;
065: }
066:
067: private void refresh() {
068: ReferenceCheckers referenceCheckers = (ReferenceCheckers) ServiceProvider
069: .getInstance().getService("ReferenceCheckers");
070: List references = referenceCheckers.referenceChecks(objectForm
071: .getType(), Collections.singleton(objectForm
072: .getObject()));
073: if (references != null && references.size() != 0) {
074: StringBuilder builder = new StringBuilder();
075: for (Object object : references) {
076: if (builder.length() != 0)
077: builder.append(", ");
078:
079: builder.append('[');
080: builder.append(query(object));
081: builder.append(' ');
082: builder.append(format(object));
083: builder.append(']');
084: }
085: links.setXText(builder.toString());
086: } else
087: links.setText(null);
088: }
089:
090: private String query(Object object) {
091: Class type = object.getClass();
092: ObjectLocator objectLocator = getObjectLocators().get(type);
093: if (objectLocator == null) {
094: LOG.warn("No ObjectLocator for " + type.getName());
095: return null;
096: }
097:
098: Map<String, String> query = objectLocator.queryByObject(object);
099:
100: StringBuilder builder = new StringBuilder();
101: try {
102: builder.append('?');
103: builder.append("push=");
104: builder.append(type.getName());
105:
106: for (Map.Entry<String, String> entry : query.entrySet()) {
107: builder.append('&');
108: builder.append(entry.getKey());
109: builder.append('=');
110: builder.append(URLEncoder.encode(entry.getValue(),
111: "UTF-8"));
112: }
113: } catch (UnsupportedEncodingException e) {
114: throw new RuntimeException(e); // won't happen
115: }
116:
117: return builder.toString();
118: }
119:
120: public String format(Object object) throws LoaderException {
121: Client client = Client.getInstance();
122: ClientResourceProvider resourceProvider = client
123: .getResourceProvider();
124: BeanMetaProvider beanMetaProvider = client
125: .getBeanMetaProvider();
126: BeanMeta beanMeta = beanMetaProvider.getBeanMeta(object
127: .getClass());
128:
129: String patternKey = (String) beanMeta
130: .getAttribute(PropertyMeta.ATTRIBUTE_PATTERN_KEY);
131: Format subjectFormat = beanMeta.getFormat();
132: if (subjectFormat == null)
133: subjectFormat = new NoFormat();
134:
135: if (patternKey != null)
136: subjectFormat.setPattern(resourceProvider.getMessage(client
137: .getLocale(), patternKey));
138:
139: return subjectFormat.format(object);
140: }
141:
142: public ObjectLocators getObjectLocators() {
143: if (objectLocators == null)
144: objectLocators = (ObjectLocators) ServiceProvider
145: .getInstance().getService("ObjectLocators");
146: return objectLocators;
147: }
148:
149: class RefreshAction extends AbstractAction {
150: public RefreshAction() {
151: putValue(SMALL_ICON, XIcons.REFRESH);
152: }
153:
154: public void actionPerformed(ActionEvent e) {
155: refresh();
156: }
157: }
158:
159: private class ReferencesLabel extends XLabel {
160: public ReferencesLabel() {
161: setAjaxLinking(true);
162: }
163:
164: protected void handleEvent() {
165: String push = SessionManager.getSession()
166: .getServletRequest().getParameter("push");
167: if (push != null) {
168: try {
169: ClassLoader classLoader = Thread.currentThread()
170: .getContextClassLoader();
171: Window parent = Window.currentWindow(objectForm
172: .getEnvironment());
173: ShowEditorAction action = new ShowEditorAction(
174: parent, classLoader.loadClass(push), push);
175: action = (ShowEditorAction) Client.getInstance()
176: .getActionProvider().configure(action);
177: if (action.isEnabled()) {
178: action.navigate(query());
179: }
180: } catch (ClassNotFoundException e) {
181: LOG.warn(push, e);
182: }
183: return;
184: }
185: }
186:
187: private Map<String, String> query() {
188: Map<String, String[]> map = SessionManager.getSession()
189: .getServletRequest().getParameterMap();
190: Map<String, String> query = new HashMap<String, String>();
191: for (Map.Entry<String, String[]> entry : map.entrySet()) {
192: String key = entry.getKey();
193: String[] value = entry.getValue();
194: if (value != null && value.length == 1)
195: query.put(key, value[0]);
196: }
197: query.put("locale", SessionManager.getSession().getLocale()
198: .toString());
199: query.put("user", SessionManager.getSession()
200: .getServletRequest().getUserPrincipal().getName());
201: return query;
202: }
203: }
204: }
|