001: /*
002: * Copyright (c) 2004-2006, Jean-François Brazeau. All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * 1. Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * 2. Redistributions in binary form must reproduce the above copyright
011: * notice, this list of conditions and the following disclaimer in the
012: * documentation and/or other materials provided with the distribution.
013: *
014: * 3. The name of the author may not be used to endorse or promote products
015: * derived from this software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
018: * IMPLIEDWARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
019: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
020: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
021: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
022: * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
023: * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
024: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
025: * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
026: * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027: */
028: package jfb.tools.activitymgr.ui.dialogs;
029:
030: import java.text.SimpleDateFormat;
031: import java.util.HashMap;
032:
033: import jfb.tools.activitymgr.core.DbException;
034: import jfb.tools.activitymgr.core.ModelException;
035: import jfb.tools.activitymgr.core.ModelMgr;
036: import jfb.tools.activitymgr.core.beans.Collaborator;
037: import jfb.tools.activitymgr.core.beans.Contribution;
038: import jfb.tools.activitymgr.core.beans.Task;
039: import jfb.tools.activitymgr.core.util.StringHelper;
040: import jfb.tools.activitymgr.ui.util.AbstractTableMgr;
041: import jfb.tools.activitymgr.ui.util.SWTHelper;
042: import jfb.tools.activitymgr.ui.util.SafeRunner;
043: import jfb.tools.activitymgr.ui.util.TableOrTreeColumnsMgr;
044:
045: import org.apache.log4j.Logger;
046: import org.eclipse.jface.viewers.TableViewer;
047: import org.eclipse.swt.SWT;
048: import org.eclipse.swt.events.MenuEvent;
049: import org.eclipse.swt.events.MenuListener;
050: import org.eclipse.swt.events.SelectionEvent;
051: import org.eclipse.swt.events.SelectionListener;
052: import org.eclipse.swt.layout.GridData;
053: import org.eclipse.swt.layout.GridLayout;
054: import org.eclipse.swt.widgets.Composite;
055: import org.eclipse.swt.widgets.Menu;
056: import org.eclipse.swt.widgets.MenuItem;
057: import org.eclipse.swt.widgets.Table;
058:
059: public class ContributionsViewerTable extends AbstractTableMgr
060: implements SelectionListener, MenuListener {
061:
062: /** Logger */
063: private static Logger log = Logger
064: .getLogger(ContributionsViewerTable.class);
065:
066: /** Filtre de recherche */
067: private Task task;
068: private Collaborator contributor;
069: private Integer day;
070: private Integer month;
071: private Integer year;
072:
073: /** Constantes associées aux colonnes */
074: public static final int DATE_COLUMN_IDX = 0;
075: public static final int COLLABORATOR_COLUMN_IDX = 1;
076: public static final int TASK_CODE_PATH_COLUMN_IDX = 2;
077: public static final int TASK_NAME_COLUMN_IDX = 3;
078: public static final int DURATION_COLUMN_IDX = 4;
079: private static TableOrTreeColumnsMgr tableColsMgr;
080:
081: /** Formatteur de date */
082: private SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
083:
084: /** Viewer */
085: private TableViewer tableViewer;
086:
087: /** Composant parent */
088: private Composite parent;
089:
090: /** Items de menu */
091: private MenuItem exportItem;
092:
093: /** Cache de taches */
094: private HashMap tasksCache = new HashMap();
095:
096: /** Cache de chemins de tache */
097: private HashMap taskCodePathsCache = new HashMap();
098:
099: /** Cache de collaborateurs */
100: private HashMap collaboratorsCache = new HashMap();
101:
102: /**
103: * Constructeur par défaut.
104: * @param parentComposite composant parent.
105: * @param layoutData données associées au layout.
106: */
107: public ContributionsViewerTable(Composite parentComposite,
108: Object layoutData) {
109: log.debug("new ContributionsViewerTable()");
110: // Création du composite parent
111: parent = new Composite(parentComposite, SWT.NONE);
112: parent.setLayoutData(layoutData);
113: parent.setLayout(new GridLayout(1, false));
114:
115: // Arbre tableau
116: final Table table = new Table(parent, SWT.FULL_SELECTION
117: | SWT.BORDER | SWT.HIDE_SELECTION | SWT.MULTI);
118: GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, true);
119: gridData.heightHint = 300;
120: table.setLayoutData(gridData);
121: table.setLinesVisible(true);
122: table.setHeaderVisible(true);
123: table.setEnabled(true);
124:
125: // Création du viewer
126: tableViewer = new TableViewer(table);
127: tableViewer.setContentProvider(this );
128: tableViewer.setLabelProvider(this );
129:
130: // Configuration des colonnes
131: tableColsMgr = new TableOrTreeColumnsMgr();
132: tableColsMgr.addColumn("DATE", "Date", 70, SWT.LEFT);
133: tableColsMgr.addColumn("COLLABORATOR", "Collaborator", 100,
134: SWT.LEFT);
135: tableColsMgr.addColumn("TASK_NAME", "Task path", 170, SWT.LEFT);
136: tableColsMgr.addColumn("TASK_NAME", "Task name", 170, SWT.LEFT);
137: tableColsMgr.addColumn("DURATION", "Duration", 50, SWT.LEFT);
138: tableColsMgr.configureTable(tableViewer);
139:
140: // Configuration du menu popup
141: final Menu menu = new Menu(table);
142: menu.addMenuListener(this );
143: exportItem = new MenuItem(menu, SWT.CASCADE);
144: exportItem.setText("Export");
145: exportItem.addSelectionListener(this );
146: table.setMenu(menu);
147: }
148:
149: /**
150: * Initialise le filtre de recherche des contributions.
151: * @param task la tache.
152: * @param contributor le collaborateur.
153: * @param year l'année.
154: * @param month le mois.
155: * @param day le jour.
156: */
157: public void setFilter(Task task, Collaborator contributor,
158: Integer year, Integer month, Integer day) {
159: // Initialisation du filtre de recherche
160: this .task = task;
161: this .contributor = contributor;
162: this .year = year;
163: this .month = month;
164: this .day = day;
165: // Création d'une racine fictive
166: tableViewer.setInput(ROOT_NODE);
167: }
168:
169: /* (non-Javadoc)
170: * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
171: */
172: public Object[] getElements(Object inputElement) {
173: // Chargement des données
174: SafeRunner safeRunner = new SafeRunner() {
175: public Object runUnsafe() throws Exception {
176: // Recherche des collaborateurs
177: return ModelMgr.getContributions(task, contributor,
178: year, month, day);
179: }
180: };
181: // Exécution
182: Object result = (Object) safeRunner.run(parent.getShell());
183: return (Contribution[]) (result != null ? result
184: : new Contribution[] {});
185: }
186:
187: /* (non-Javadoc)
188: * @see org.eclipse.jface.viewers.ITableLabelProvider#getColumnText(java.lang.Object, int)
189: */
190: public String getColumnText(final Object element,
191: final int columnIndex) {
192: SafeRunner safeRunner = new SafeRunner() {
193: public Object runUnsafe() throws Exception {
194: Contribution c = (Contribution) element;
195: String text = null;
196: switch (columnIndex) {
197: case (DATE_COLUMN_IDX):
198: text = sdf.format(c.getDate().getTime());
199: break;
200: case (COLLABORATOR_COLUMN_IDX):
201: Collaborator collaborator = getCachedCollaborator(c
202: .getContributorId());
203: text = collaborator.getFirstName() + " "
204: + collaborator.getLastName();
205: break;
206: case (TASK_CODE_PATH_COLUMN_IDX):
207: text = getCachedTaskCodePath(c.getTaskId());
208: break;
209: case (TASK_NAME_COLUMN_IDX):
210: text = getCachedTask(c.getTaskId()).getName();
211: break;
212: case (DURATION_COLUMN_IDX):
213: text = StringHelper.hundredthToEntry(c
214: .getDurationId());
215: break;
216: default:
217: throw new Error("Colonne inconnue");
218: }
219: return text;
220: }
221: };
222: // Exécution
223: return (String) safeRunner.run(parent.getShell(), "");
224: }
225:
226: /**
227: * Retourne le chemin de la tache associée à l'identifiant spécifié.
228: * @param taskId l'identifiant de la tache.
229: * @return le chemin.
230: * @throws ModelException levé en cas de viloation du modèle.
231: * @throws DbException levé en cas d'incident associé à l'accès à la base de données.
232: */
233: private String getCachedTaskCodePath(long taskId)
234: throws ModelException, DbException {
235: Long _taskId = new Long(taskId);
236: String taskCodePath = (String) taskCodePathsCache.get(_taskId);
237: if (taskCodePath == null) {
238: log.debug("Registering in cache task code path for taskId="
239: + taskId);
240: Task task = getCachedTask(taskId);
241: taskCodePath = ModelMgr.getTaskCodePath(task);
242: taskCodePathsCache.put(_taskId, taskCodePath);
243: }
244: return taskCodePath;
245: }
246:
247: /**
248: * Retourne la tache associée à l'identifiant spécifié.
249: * @param taskId l'identifiant de la tache.
250: * @return la tache.
251: * @throws DbException levé en cas d'incident associé à l'accès à la base de données.
252: */
253: private Task getCachedTask(long taskId) throws DbException {
254: Long _taskId = new Long(taskId);
255: Task task = (Task) tasksCache.get(_taskId);
256: if (task == null) {
257: log.debug("Registering in cache task for taskId=" + taskId);
258: task = ModelMgr.getTask(taskId);
259: tasksCache.put(_taskId, task);
260: }
261: return task;
262: }
263:
264: /**
265: * Retourne le collaborateur associée à l'identifiant spécifié.
266: * @param collaboratorId l'identifiant du collaborateur.
267: * @return le collaborateur.
268: * @throws DbException levé en cas d'incident associé à l'accès à la base de données.
269: */
270: private Collaborator getCachedCollaborator(long collaboratorId)
271: throws DbException {
272: Long _collaboratorId = new Long(collaboratorId);
273: Collaborator collaborator = (Collaborator) collaboratorsCache
274: .get(_collaboratorId);
275: if (collaborator == null) {
276: log
277: .debug("Registering in cache collaborator for collaboratorId="
278: + collaboratorId);
279: collaborator = ModelMgr.getCollaborator(collaboratorId);
280: collaboratorsCache.put(_collaboratorId, collaborator);
281: }
282: return collaborator;
283: }
284:
285: /* (non-Javadoc)
286: * @see org.eclipse.swt.events.MenuListener#menuShown(org.eclipse.swt.events.MenuEvent)
287: */
288: public void menuShown(MenuEvent e) {
289: log.debug("menuShown(" + e + ")");
290: // TableItem[] selection = tableViewer.getTable().getSelection();
291: // boolean emptySelection = selection.length==0;
292: // boolean singleSelection = selection.length==1;
293: exportItem.setEnabled(true);
294: }
295:
296: /* (non-Javadoc)
297: * @see org.eclipse.swt.events.MenuListener#menuHidden(org.eclipse.swt.events.MenuEvent)
298: */
299: public void menuHidden(MenuEvent e) {
300: // Do nothing...
301: }
302:
303: /* (non-Javadoc)
304: * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
305: */
306: public void widgetSelected(final SelectionEvent e) {
307: log.debug("SelectionListener.widgetSelected(" + e + ")");
308: final Object source = e.getSource();
309: SafeRunner safeRunner = new SafeRunner() {
310: public Object runUnsafe() throws Exception {
311: //TableItem[] selection = tableViewer.getTable().getSelection();
312: // Cas d'une demande d'export du tableau
313: if (exportItem.equals(source)) {
314: // Export du tableau
315: SWTHelper.exportToWorkBook(tableViewer.getTable());
316: }
317: return null;
318: }
319: };
320: // Exécution
321: safeRunner.run(parent.getShell());
322: }
323:
324: /* (non-Javadoc)
325: * @see org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent)
326: */
327: public void widgetDefaultSelected(SelectionEvent e) {
328: widgetSelected(e);
329: }
330:
331: }
|