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.util;
029:
030: import java.util.ArrayList;
031: import java.util.Arrays;
032: import java.util.Iterator;
033:
034: import jfb.tools.activitymgr.core.ModelMgr;
035: import jfb.tools.activitymgr.core.beans.Collaborator;
036: import jfb.tools.activitymgr.ui.CollaboratorsUI.ICollaboratorListener;
037: import jfb.tools.activitymgr.ui.DatabaseUI.IDbStatusListener;
038: import jfb.tools.activitymgr.ui.images.ImagesDatas;
039:
040: import org.apache.log4j.Logger;
041: import org.eclipse.jface.viewers.StructuredSelection;
042: import org.eclipse.jface.viewers.TableViewer;
043: import org.eclipse.swt.SWT;
044: import org.eclipse.swt.events.SelectionEvent;
045: import org.eclipse.swt.events.SelectionListener;
046: import org.eclipse.swt.graphics.Image;
047: import org.eclipse.swt.widgets.Composite;
048: import org.eclipse.swt.widgets.Event;
049: import org.eclipse.swt.widgets.Listener;
050: import org.eclipse.swt.widgets.Table;
051: import org.eclipse.swt.widgets.TableColumn;
052: import org.eclipse.swt.widgets.TableItem;
053:
054: /**
055: * IHM de gestion des collaborateurs.
056: */
057: public class SelectableCollaboratorPanel extends AbstractTableMgr
058: implements IDbStatusListener, ICollaboratorListener {
059:
060: /** Logger */
061: private static Logger log = Logger
062: .getLogger(SelectableCollaboratorPanel.class);
063:
064: /** Constantes associées aux colonnes */
065: public static final int FIRST_NAME_COLUMN_IDX = 0;
066: public static final int LAST_NAME_COLUMN_IDX = 1;
067: private static TableOrTreeColumnsMgr tableColsMgr;
068:
069: /** Viewer */
070: private TableViewer tableViewer;
071:
072: /** Composant parent */
073: private Composite parent;
074:
075: /** Index de la colonne utilisé pour trier les collaborateurs */
076: private int sortColumnIndex = LAST_NAME_COLUMN_IDX;
077:
078: /** Icone utilisé pour marquer le collaborateur sélectionné */
079: private Image selectedItemIcon;
080:
081: /** Icone utilisé pour les collaborateurs non sélectionnés */
082: private Image unselectedItemIcon;
083:
084: /** Liste des listeners */
085: private ArrayList listeners = new ArrayList();
086:
087: /** Collaborateur sélectionné */
088: private Collaborator selectedCollaborator;
089:
090: /**
091: * Constructeur par défaut.
092: * @param parentComposite composant parent.
093: */
094: public SelectableCollaboratorPanel(Composite parentComposite,
095: Object layoutData) {
096: parent = parentComposite;
097:
098: // Table
099: final Table table = new Table(parent, SWT.MULTI
100: | SWT.FULL_SELECTION | SWT.BORDER | SWT.HIDE_SELECTION);
101: table.setLayoutData(layoutData);
102: table.setLinesVisible(true);
103: table.setHeaderVisible(true);
104: table.setEnabled(true);
105: table.addSelectionListener(new SelectionListener() {
106: public void widgetDefaultSelected(SelectionEvent e) {
107: }
108:
109: public void widgetSelected(SelectionEvent e) {
110: Iterator it = listeners.iterator();
111: while (it.hasNext()) {
112: Collaborator lastSelectedCollaborator = selectedCollaborator;
113: StructuredSelection selection = (StructuredSelection) tableViewer
114: .getSelection();
115: if (selection != null && !selection.isEmpty())
116: selectedCollaborator = (Collaborator) selection
117: .getFirstElement();
118: ICollaboratorSelectionListener listener = (ICollaboratorSelectionListener) it
119: .next();
120: listener.collaboratorSelected(selectedCollaborator);
121: tableViewer.refresh(selectedCollaborator);
122: if (lastSelectedCollaborator != null)
123: tableViewer.refresh(lastSelectedCollaborator);
124: }
125: }
126: });
127:
128: // Création du viewer
129: tableViewer = new TableViewer(table);
130: tableViewer.setContentProvider(this );
131: tableViewer.setLabelProvider(this );
132:
133: // Configuration des colonnes
134: tableColsMgr = new TableOrTreeColumnsMgr();
135: tableColsMgr.addColumn("FIRST_NAME", "First name", 100,
136: SWT.LEFT);
137: tableColsMgr.addColumn("LAST_NAME", "Last name", 100, SWT.LEFT);
138: tableColsMgr.configureTable(tableViewer);
139:
140: // Ajout du listener de gestion du tri des colonnes
141: // Add sort indicator and sort data when column selected
142: Listener sortListener = new Listener() {
143: public void handleEvent(Event e) {
144: log.debug("handleEvent(" + e + ")");
145: TableColumn previousSortColumn = table.getSortColumn();
146: TableColumn newSortColumn = (TableColumn) e.widget;
147: int dir = table.getSortDirection();
148: if (previousSortColumn == newSortColumn) {
149: dir = dir == SWT.UP ? SWT.DOWN : SWT.UP;
150: } else {
151: table.setSortColumn(newSortColumn);
152: dir = SWT.UP;
153: }
154: table.setSortDirection(dir);
155: sortColumnIndex = Arrays.asList(table.getColumns())
156: .indexOf(newSortColumn);
157: // Rafraichissement des données
158: tableViewer.refresh();
159: }
160: };
161: table.getColumns()[FIRST_NAME_COLUMN_IDX].addListener(
162: SWT.Selection, sortListener);
163: table.getColumns()[LAST_NAME_COLUMN_IDX].addListener(
164: SWT.Selection, sortListener);
165: table.setSortColumn(table.getColumns()[sortColumnIndex]);
166: table.setSortDirection(SWT.UP);
167:
168: // Chargement des icones
169: selectedItemIcon = new Image(parentComposite.getDisplay(),
170: ImagesDatas.SELECTED_ITEM_ICON);
171: unselectedItemIcon = new Image(parentComposite.getDisplay(),
172: ImagesDatas.UNSELECTED_ITEM_ICON);
173:
174: }
175:
176: /**
177: * Retourne le nombre de collaborateurs présentés dans le tableau.
178: * @return le nombre de collaborateurs présentés dans le tableau.
179: */
180: public int getCollaboratorsCount() {
181: return tableViewer.getTable().getItemCount();
182: }
183:
184: /**
185: * Définit le collaborateur sélectionné.
186: * @param idx index du collaborateur sélectionné.
187: */
188: public void setSelectedIndex(int idx) {
189: Collaborator c = (Collaborator) tableViewer.getElementAt(idx);
190: setSelectedCollaborator(c);
191: }
192:
193: /**
194: * Définit le collaborateur sélectionné.
195: * @param collaborator le collaborateur.
196: */
197: public void setSelectedCollaborator(Collaborator collaborator) {
198: Collaborator lastSelectedCollaborator = selectedCollaborator;
199: selectedCollaborator = collaborator;
200: if (collaborator != null) {
201: tableViewer.setSelection(new StructuredSelection(
202: selectedCollaborator));
203: tableViewer.refresh(selectedCollaborator);
204: }
205: if (lastSelectedCollaborator != null)
206: tableViewer.refresh(lastSelectedCollaborator);
207: }
208:
209: /**
210: * Retourne le collaborateur sélectionné.
211: * @return le collaborateur sélectionné.
212: */
213: public Collaborator getSelectedCollaborator() {
214: return selectedCollaborator;
215: }
216:
217: /* (non-Javadoc)
218: * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
219: */
220: public Object[] getElements(Object inputElement) {
221: // Chargement des données
222: SafeRunner safeRunner = new SafeRunner() {
223: public Object runUnsafe() throws Exception {
224: // Recherche des collaborateurs
225: int orderByFieldIndex = -1;
226: switch (sortColumnIndex) {
227: case FIRST_NAME_COLUMN_IDX:
228: orderByFieldIndex = Collaborator.FIRST_NAME_FIELD_IDX;
229: break;
230: case LAST_NAME_COLUMN_IDX:
231: default:
232: orderByFieldIndex = Collaborator.LAST_NAME_FIELD_IDX;
233: break;
234: }
235: // Récupération
236: return ModelMgr.getActiveCollaborators(
237: orderByFieldIndex, tableViewer.getTable()
238: .getSortDirection() == SWT.UP);
239: }
240: };
241: // Exécution
242: Object result = (Object) safeRunner.run(parent.getShell());
243: return (Collaborator[]) (result != null ? result
244: : new Collaborator[] {});
245: }
246:
247: /* (non-Javadoc)
248: * @see org.eclipse.jface.viewers.ITableLabelProvider#getColumnText(java.lang.Object, int)
249: */
250: public String getColumnText(final Object element,
251: final int columnIndex) {
252: log.debug("ITableLabelProvider.getColumnText(" + element + ", "
253: + columnIndex + ")");
254: SafeRunner safeRunner = new SafeRunner() {
255: public Object runUnsafe() throws Exception {
256: Collaborator collaborator = (Collaborator) element;
257: String text = null;
258: switch (columnIndex) {
259: case (FIRST_NAME_COLUMN_IDX):
260: text = collaborator.getFirstName();
261: break;
262: case (LAST_NAME_COLUMN_IDX):
263: text = collaborator.getLastName();
264: break;
265: default:
266: throw new Error("Colonne inconnue");
267: }
268: return text;
269: }
270: };
271: // Exécution
272: return (String) safeRunner.run(parent.getShell(), "");
273: }
274:
275: public Image getColumnImage(Object element, int columnIndex) {
276: Image image = null;
277: if (columnIndex == 0) {
278: Collaborator collaborator = (Collaborator) element;
279: image = collaborator.equals(selectedCollaborator) ? selectedItemIcon
280: : unselectedItemIcon;
281: }
282: return image;
283: }
284:
285: /**
286: * Initialise l'IHM.
287: */
288: public void initialize() {
289: // Création d'une racine fictive
290: tableViewer.setInput(ROOT_NODE);
291: }
292:
293: /* (non-Javadoc)
294: * @see jfb.tools.activitymgr.ui.DatabaseUI.DbStatusListener#databaseOpened()
295: */
296: public void databaseOpened() {
297: initialize();
298: }
299:
300: /* (non-Javadoc)
301: * @see jfb.tools.activitymgr.ui.DatabaseUI.DbStatusListener#databaseClosed()
302: */
303: public void databaseClosed() {
304: Table table = tableViewer.getTable();
305: TableItem[] items = table.getItems();
306: for (int i = 0; i < items.length; i++) {
307: items[i].dispose();
308: }
309: selectedCollaborator = null;
310: }
311:
312: /* (non-Javadoc)
313: * @see jfb.tools.activitymgr.ui.CollaboratorsUI.ICollaboratorListener#collaboratorAdded(jfb.tools.activitymgr.core.beans.Collaborator)
314: */
315: public void collaboratorAdded(Collaborator collaborator) {
316: tableViewer.refresh();
317: }
318:
319: /* (non-Javadoc)
320: * @see jfb.tools.activitymgr.ui.CollaboratorsUI.ICollaboratorListener#collaboratorRemoved(jfb.tools.activitymgr.core.beans.Collaborator)
321: */
322: public void collaboratorRemoved(Collaborator collaborator) {
323: // Si le collaborateur supprimé est celui qui est actuellement sélectionné => on supprime la sélection
324: if (collaborator.equals(selectedCollaborator))
325: selectedCollaborator = null;
326: tableViewer.refresh();
327: }
328:
329: /* (non-Javadoc)
330: * @see jfb.tools.activitymgr.ui.CollaboratorsUI.ICollaboratorListener#collaboratorUpdated(jfb.tools.activitymgr.core.beans.Collaborator)
331: */
332: public void collaboratorUpdated(Collaborator collaborator) {
333: tableViewer.refresh(collaborator);
334: }
335:
336: /* (non-Javadoc)
337: * @see jfb.tools.activitymgr.ui.CollaboratorsUI.ICollaboratorListener#collaboratorActivationStatusChanged(jfb.tools.activitymgr.core.beans.Collaborator)
338: */
339: public void collaboratorActivationStatusChanged(
340: Collaborator collaborator) {
341: // C'est soit comme une suppression ou comme un ajout
342: if (collaborator.getIsActive())
343: collaboratorAdded(collaborator);
344: else
345: collaboratorRemoved(collaborator);
346: }
347:
348: /**
349: * Ajoute un listener de sélection.
350: * @param listener le nouveau listener.
351: */
352: public void addSelectionListener(
353: ICollaboratorSelectionListener listener) {
354: listeners.add(listener);
355: }
356:
357: /**
358: * Supprime un listener de sélection.
359: * @param listener le listener.
360: */
361: public void removeSelectionListener(SelectionListener listener) {
362: listeners.remove(listener);
363: }
364:
365: }
|