001: package org.osbl.client.wings.concern;
002:
003: import org.concern.*;
004: import org.osbl.client.wings.form.ObjectTableModel;
005: import org.osbl.client.wings.form.StringEditableCellRenderer;
006: import org.osbl.client.wings.shell.Client;
007: import org.conform.BeanMeta;
008: import org.conform.wings.ComponentFactory;
009: import org.wings.session.SessionManager;
010: import org.wings.table.SDefaultTableCellRenderer;
011: import org.wings.SComponent;
012: import org.wings.STable;
013: import org.wingx.table.*;
014:
015: import java.awt.event.ActionEvent;
016: import java.awt.event.ActionListener;
017: import java.util.*;
018: import java.text.DateFormat;
019:
020: /**
021: * @author hengels
022: * @version $Revision$
023: */
024: public class TaskTableModel extends XTableModel implements
025: ObjectTableModel {
026: private final static String[] propertyNames = new String[] {
027: "subjectLine", "process", "activity", "originator", "due", };
028: private final static String[] filterColumnNames = new String[] {
029: "line.line", "subject.process", "enlistment.activity",
030: "subject.originator", "enlistment.timeout", };
031:
032: private BeanMeta beanMeta;
033: private XTableColumnModel columnModel = new XDefaultTableColumnModel();
034:
035: List works = Collections.EMPTY_LIST;
036:
037: protected Worklist worklist;
038: protected String user;
039: private String[] processes;
040: private String[] activities;
041: private String[] orders;
042: private String[] keywords;
043:
044: public TaskTableModel(BeanMeta beanMeta) {
045: this .beanMeta = beanMeta;
046: worklist = ControllerLookup.getInstance().getWorklist();
047:
048: for (int i = 0; i < TaskTableModel.propertyNames.length; i++) {
049: String columnName = TaskTableModel.propertyNames[i];
050: columnModel.addColumn(ComponentFactory.INSTANCE
051: .createTableColumn(beanMeta, columnName, i));
052: }
053: columnModel.getColumn("process").setSortable(true);
054: columnModel.getColumn("activity").setSortable(true);
055: columnModel.getColumn("originator").setSortable(true);
056: columnModel.getColumn("due").setSortable(true);
057:
058: columnModel.getColumn("subjectLine").setFilterable(true);
059: columnModel.getColumn("subjectLine").setFilterRenderer(
060: new StringEditableCellRenderer());
061: columnModel.getColumn("process").setFilterable(true);
062: columnModel.getColumn("process").setFilterRenderer(
063: new ProcessFilterRenderer());
064: columnModel.getColumn("activity").setFilterable(true);
065: columnModel.getColumn("activity").setFilterRenderer(
066: new StringEditableCellRenderer());
067:
068: columnModel.getColumn("subjectLine").setCellRenderer(
069: new TaskTableCellRenderer());
070: columnModel.getColumn("process").setCellRenderer(
071: new TaskTableCellRenderer());
072: columnModel.getColumn("activity").setCellRenderer(
073: new TaskTableCellRenderer());
074: }
075:
076: public String[] getKeywords() {
077: return keywords;
078: }
079:
080: public void setKeywords(String[] keywords) {
081: this .keywords = keywords;
082: }
083:
084: public void setProcesses(String[] processes) {
085: this .processes = processes;
086: }
087:
088: public String getUser() {
089: return user;
090: }
091:
092: public void setUser(String user) {
093: this .user = user;
094: }
095:
096: public String[] getActivities() {
097: return activities;
098: }
099:
100: public void setActivities(String[] activities) {
101: this .activities = activities;
102: }
103:
104: public void refresh() {
105: String locale = SessionManager.getSession().getLocale()
106: .toString();
107: works = worklist.listWork(new String[] { "*", user },
108: processes, activities, keywords, getOrders(), locale,
109: 200);
110: fireTableDataChanged();
111: }
112:
113: public void setFilter(int col, Object value) {
114: super .setFilter(col, value);
115: switch (col) {
116: case 0:
117: keywords = tokenize((String) value);
118: break;
119: case 1:
120: processes = (String[]) value;
121: break;
122: case 2:
123: activities = tokenize((String) value);
124: break;
125: }
126: }
127:
128: protected String[] tokenize(String string) {
129: if (string == null)
130: return new String[0];
131:
132: StringTokenizer tokens = new StringTokenizer(string, " ,");
133: String[] strings = new String[tokens.countTokens()];
134: for (int i = 0; tokens.hasMoreTokens(); i++)
135: strings[i] = tokens.nextToken();
136:
137: return strings;
138: }
139:
140: public void setSort(int col, int order) {
141: super .setSort(col, order);
142: orders = null;
143: }
144:
145: public String[] getOrders() {
146: if (orders == null) {
147: List<String> orders = new LinkedList<String>();
148: for (int i = 0; i < getColumnCount(); i++) {
149: int sort = getSort(i);
150: if (sort == SORT_NONE)
151: continue;
152:
153: orders.add((sort == SORT_ASCENDING ? "" : "-")
154: + TaskTableModel.filterColumnNames[i]);
155: }
156: this .orders = orders.toArray(new String[orders.size()]);
157: }
158: return orders;
159: }
160:
161: public XTableColumnModel getColumnModel() {
162: return columnModel;
163: }
164:
165: public int getRowCount() {
166: return works.size();
167: }
168:
169: public int getColumnCount() {
170: return columnModel.getColumnCount();
171: }
172:
173: public Object getRowId(int rowIndex) {
174: Work work = getRow(rowIndex);
175: return id(work);
176: }
177:
178: static String id(Work work) {
179: return work.getProcess() + "_" + work.getActivity() + "_"
180: + work.getSubjectId();
181: }
182:
183: public Object getValueAt(int rowIndex, int columnIndex) {
184: Work work = (Work) works.get(rowIndex);
185:
186: switch (columnIndex) {
187: case 0:
188: String subjectLine = work.getSubjectLine();
189: if (subjectLine != null && subjectLine.length() > 60)
190: subjectLine = subjectLine.substring(0, 56) + " ...";
191: return subjectLine;
192: case 1:
193: return Client.getInstance().getResourceProvider()
194: .getMessage("process." + work.getProcess());
195: case 2:
196: return Client.getInstance().getResourceProvider()
197: .getMessage(
198: "process." + work.getProcess() + "."
199: + work.getActivity());
200: case 3:
201: return work.getOriginator();
202: case 4:
203: return work.getDue();
204: }
205: return "?";
206: }
207:
208: public String getColumnName(int column) {
209: return "" + columnModel.getColumn(column).getHeaderValue();
210: }
211:
212: public Class<?> getColumnClass(int columnIndex) {
213: return beanMeta.getProperties()[columnIndex].getType();
214: }
215:
216: Work getRow(int row) {
217: return (Work) works.get(row);
218: }
219:
220: // Attach an action listener in order to have the component autosubmit onchange
221: class NoAction implements ActionListener {
222: public void actionPerformed(ActionEvent e) {
223: }
224: }
225:
226: class TaskTableCellRenderer extends SDefaultTableCellRenderer {
227: DateFormat dateFormat = DateFormat.getTimeInstance(
228: DateFormat.MEDIUM, SessionManager.getSession()
229: .getLocale());
230: String user = SessionManager.getSession().getServletRequest()
231: .getUserPrincipal().getName();
232:
233: public SComponent getTableCellRendererComponent(STable table,
234: Object value, boolean selected, int row, int col) {
235: super .getTableCellRendererComponent(table, value, selected,
236: row, col);
237:
238: TaskTableModel model = (TaskTableModel) table.getModel();
239: Work work = model.getRow(row);
240:
241: if (work.getLockedBy() != null
242: && !user.equals(work.getLockedBy())
243: && work.getLockedUntil().after(new Date())) {
244: setToolTipText("Locked by " + work.getLockedBy()
245: + " until "
246: + dateFormat.format(work.getLockedUntil()));
247: setStyle("locked_work");
248: } else {
249: setToolTipText(null);
250: setStyle("SLabel");
251: }
252: return this;
253: }
254: }
255: }
|