001: package org.osbl.issue.gui;
002:
003: import org.conform.wings.Editor;
004: import org.conform.wings.PropertyAdapter;
005: import org.conform.wings.editor.DefaultEditorStyles;
006: import org.conform.wings.editor.ComponentInvalidator;
007: import org.conform.*;
008: import org.wings.SComponent;
009: import org.wings.session.SessionManager;
010: import org.osbl.issue.model.Link;
011: import org.osbl.client.wings.XLabel;
012: import org.osbl.client.wings.action.ShowEditorAction;
013: import org.osbl.client.wings.shell.Client;
014: import org.osbl.client.wings.shell.Window;
015: import org.apache.commons.logging.LogFactory;
016:
017: import java.util.*;
018:
019: public class ClickableLinksEditor implements Editor {
020: private static org.apache.commons.logging.Log LOG = LogFactory
021: .getLog(ClickableLinksEditor.class);
022:
023: public SComponent createComponent(PropertyMeta propertyMeta) {
024: PropertyDataXLabel propertyDataLabel = new PropertyDataXLabel(
025: propertyMeta);
026: configureComponent(propertyMeta, propertyDataLabel, false);
027: return propertyDataLabel;
028: }
029:
030: public void configureComponent(PropertyMeta propertyMeta,
031: SComponent component, boolean erroneous) {
032: PropertyDataXLabel propertyDataLabel = (PropertyDataXLabel) component;
033: propertyDataLabel.setVisible(propertyMeta.isReadable());
034: DefaultEditorStyles.applyEditorAlignment(propertyMeta,
035: component);
036: //DefaultEditorStyles.applyEditorState(propertyMeta, component, erroneous);
037: }
038:
039: public void setPropertyData(SComponent component,
040: PropertyData propertyData) {
041: BeanData beanData = propertyData.getBeanData();
042: if (beanData != null)
043: beanData.addPropertyChangeListener(propertyData
044: .getPropertyMeta().getName(),
045: new ComponentInvalidator(component));
046: ((PropertyDataXLabel) component).setPropertyData(propertyData);
047: }
048:
049: public PropertyData getPropertyData(SComponent component) {
050: return ((PropertyDataXLabel) component).getPropertyData();
051: }
052:
053: static class PropertyDataXLabel extends XLabel implements
054: PropertyAdapter {
055: private PropertyData propertyData;
056: private PropertyMeta propertyMeta;
057:
058: public PropertyDataXLabel(PropertyMeta propertyMeta) {
059: this .propertyMeta = propertyMeta;
060: setAjaxLinking(true);
061: }
062:
063: public void setPropertyData(PropertyData propertyData) {
064: this .propertyData = propertyData;
065: }
066:
067: public PropertyData getPropertyData() {
068: return propertyData;
069: }
070:
071: public void setText(String string) {
072: }
073:
074: public String getText() {
075: List<Link> links = (List<Link>) propertyData.getValue();
076: if (links == null)
077: return null;
078:
079: StringBuilder builder = new StringBuilder();
080: for (Link link : links) {
081: if (builder.length() != 0)
082: builder.append(", ");
083: builder.append('[');
084: builder.append(link(link));
085: builder.append(' ');
086: builder.append(link.getName());
087: builder.append(']');
088: }
089: return parse(builder.toString());
090: }
091:
092: private String link(Link link) {
093: String string = link.getLink();
094: if (string.startsWith("?edit"))
095: string = "?push" + string.substring(5);
096: else if (string.startsWith("http"))
097: string = "redirect=" + string;
098: return string;
099: }
100:
101: protected void handleEvent() {
102: String push = SessionManager.getSession()
103: .getServletRequest().getParameter("push");
104: if (push != null) {
105: try {
106: ClassLoader classLoader = Thread.currentThread()
107: .getContextClassLoader();
108: Window parent = org.osbl.client.wings.shell.Window
109: .currentWindow(this );
110: ShowEditorAction action = new ShowEditorAction(
111: parent, classLoader.loadClass(push), push);
112: action = (ShowEditorAction) Client.getInstance()
113: .getActionProvider().configure(action);
114: if (action.isEnabled()) {
115: action.navigate(query());
116: }
117: } catch (ClassNotFoundException e) {
118: LOG.warn(push, e);
119: }
120: return;
121: }
122:
123: String redirect = SessionManager.getSession()
124: .getServletRequest().getParameter("redirect");
125: if (redirect != null) {
126: SessionManager.getSession()
127: .setRedirectAddress(redirect);
128: }
129: }
130:
131: private Map<String, String> query() {
132: Map<String, String[]> map = SessionManager.getSession()
133: .getServletRequest().getParameterMap();
134: Map<String, String> query = new HashMap<String, String>();
135: for (Map.Entry<String, String[]> entry : map.entrySet()) {
136: String key = entry.getKey();
137: String[] value = entry.getValue();
138: if (value != null && value.length == 1)
139: query.put(key, value[0]);
140: }
141: query.put("locale", SessionManager.getSession().getLocale()
142: .toString());
143: query.put("user", SessionManager.getSession()
144: .getServletRequest().getUserPrincipal().getName());
145: return query;
146: }
147: }
148: }
|