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.Iterator;
032:
033: import jfb.tools.activitymgr.core.ModelMgr;
034: import jfb.tools.activitymgr.core.beans.Task;
035: import jfb.tools.activitymgr.core.beans.TaskSearchFilter;
036: import jfb.tools.activitymgr.ui.dialogs.TaskChooserDialog;
037:
038: import org.eclipse.jface.dialogs.Dialog;
039: import org.eclipse.jface.dialogs.MessageDialog;
040: import org.eclipse.swt.SWT;
041: import org.eclipse.swt.events.KeyAdapter;
042: import org.eclipse.swt.events.KeyEvent;
043: import org.eclipse.swt.events.KeyListener;
044: import org.eclipse.swt.layout.GridData;
045: import org.eclipse.swt.layout.GridLayout;
046: import org.eclipse.swt.widgets.Button;
047: import org.eclipse.swt.widgets.Combo;
048: import org.eclipse.swt.widgets.Composite;
049: import org.eclipse.swt.widgets.Group;
050: import org.eclipse.swt.widgets.Text;
051:
052: /**
053: * Paneau offrant la possibilité de rechercher une tache par son nom ou son code.
054: */
055: public class TaskFinderPanel extends Composite {
056:
057: /** Liste de valeurs pour la liste de sélection du nom de l'attribut utilisé pour la recherche */
058: public static final String TASK_NAME_SEARCH_FIELD_LABEL = "Task name";
059: public static final String TASK_CODE_SEARCH_FIELD_LABEL = "Task code"; // @jve:decl-index=0:
060:
061: /** Liste de valeurs pour la liste de sélection du type de critère utilisé pour la recherche */
062: public static final String IS_EQUAL_TO_CRITERIA_LABEL = "is equal to"; // @jve:decl-index=0:
063: public static final String STARTS_WITH_CRITERIA_LABEL = "starts with"; // @jve:decl-index=0:
064: public static final String ENDS_WITH_CRITERIA_LABEL = "ends with";
065: public static final String CONTAINS_CRITERIA_LABEL = "contains"; // @jve:decl-index=0:
066:
067: /** Group contenant les controles */
068: private Group group = null;
069:
070: /** Liste des noms d'attributs utilisables pour la recherche */
071: private Combo searchFieldCombo = null;
072:
073: /** Liste de critères de recherche */
074: private Combo searchCriteriaCombo = null;
075:
076: /** Valeur utilisée pour la recherche */
077: private Text searchText = null;
078:
079: /** Bouton de recherche */
080: private Button findButton = null;
081:
082: /** Objet à l'écoute de la sélectiond d'une tache */
083: private ArrayList taskSelectionListeners = new ArrayList(); // @jve:decl-index=0:
084:
085: /** Dialogue permettant de sélectionner une tache parmi une liste */
086: private TaskChooserDialog taskChooserDialog = null;
087:
088: /**
089: * Constructeur par défaut.
090: * @param parent le composant parent.
091: */
092: public TaskFinderPanel(Composite parent) {
093: this (parent, SWT.NONE);
094: }
095:
096: /**
097: * Constructeur par défaut.
098: * @param parent le composant parent.
099: * @param style le style du panneau.
100: */
101: public TaskFinderPanel(Composite parent, int style) {
102: super (parent, style);
103: initialize();
104: // Initialisation de la liste de choix du champ utilisé pour la recherche (nom, code)
105: searchFieldCombo.add(TASK_NAME_SEARCH_FIELD_LABEL,
106: TaskSearchFilter.TASK_NAME_FIELD_IDX);
107: searchFieldCombo.add(TASK_CODE_SEARCH_FIELD_LABEL,
108: TaskSearchFilter.TASK_CODE_FIELD_IDX);
109: searchFieldCombo.setText(TASK_NAME_SEARCH_FIELD_LABEL);
110:
111: // Initialisation de la liste de choix du critère de recherche
112: searchCriteriaCombo.add(IS_EQUAL_TO_CRITERIA_LABEL,
113: TaskSearchFilter.IS_EQUAL_TO_CRITERIA_IDX);
114: searchCriteriaCombo.add(STARTS_WITH_CRITERIA_LABEL,
115: TaskSearchFilter.STARTS_WITH_CRITERIA_IDX);
116: searchCriteriaCombo.add(ENDS_WITH_CRITERIA_LABEL,
117: TaskSearchFilter.ENDS_WITH_CRITERIA_IDX);
118: searchCriteriaCombo.add(CONTAINS_CRITERIA_LABEL,
119: TaskSearchFilter.CONTAINS_WITH_CRITERIA_IDX);
120: searchCriteriaCombo.setText(CONTAINS_CRITERIA_LABEL);
121:
122: // Ajout du KeyListener permettant de valider la saisie sur l'utilisation de
123: // la touche entrée
124: KeyListener listener = new KeyAdapter() {
125: public void keyReleased(KeyEvent e) {
126: if (e.keyCode == SWT.CR)
127: buttonPressed();
128: }
129: };
130: searchFieldCombo.addKeyListener(listener);
131: searchCriteriaCombo.addKeyListener(listener);
132: searchText.addKeyListener(listener);
133:
134: // Initialisation du dialogue de coix de tache
135: taskChooserDialog = new TaskChooserDialog(getShell());
136:
137: }
138:
139: /**
140: * Initialise l'IHM.
141: */
142: private void initialize() {
143: GridLayout gridLayout = new GridLayout();
144: this .setLayout(gridLayout);
145: createGroup();
146: }
147:
148: /**
149: * Lance le traitement de recherche de tache à partir du filtre spécifié.
150: */
151: private void buttonPressed() {
152: // Chargement des données
153: SafeRunner safeRunner = new SafeRunner() {
154: public Object runUnsafe() throws Exception {
155: // Recherche en base de données
156: TaskSearchFilter filter = new TaskSearchFilter();
157: filter.setFieldIndex(searchFieldCombo
158: .getSelectionIndex());
159: filter.setCriteriaIndex(searchCriteriaCombo
160: .getSelectionIndex());
161: filter.setFieldValue(searchText.getText());
162: Task[] tasks = ModelMgr.getTasks(filter);
163:
164: // Traitement du résultat
165: Task selectedTask = null;
166: switch (tasks.length) {
167: case 0:
168: MessageDialog.openInformation(getShell(),
169: "Search status",
170: "Nothing matches your searc criterias");
171: break;
172: case 1:
173: selectedTask = tasks[0];
174: break;
175: default:
176: taskChooserDialog.setTasks(tasks);
177: if (taskChooserDialog.open() == Dialog.OK) {
178: selectedTask = (Task) taskChooserDialog
179: .getValue();
180: }
181: break;
182: }
183:
184: // Notification de la sélection
185: if (selectedTask != null) {
186: Iterator it = taskSelectionListeners.iterator();
187: while (it.hasNext()) {
188: ITaskSelectionListener listener = (ITaskSelectionListener) it
189: .next();
190: listener.taskSelected(selectedTask);
191: }
192: }
193:
194: // Pas de retour
195: return null;
196: }
197: };
198: // Exécution
199: safeRunner.run(getShell());
200: }
201:
202: /**
203: * This method initializes searchCriteriaCombo
204: *
205: */
206: private void createSearchCriteriaCombo() {
207: searchCriteriaCombo = new Combo(group, SWT.READ_ONLY);
208: }
209:
210: /**
211: * This method initializes searchFieldCombo
212: *
213: */
214: private void createSearchFieldCombo() {
215: searchFieldCombo = new Combo(group, SWT.READ_ONLY);
216: }
217:
218: /**
219: * Ajoute un objet à l'écoute de la sélectiond d'une tache.
220: * @param listener le nouveau listener.
221: */
222: public void addTaskListener(ITaskSelectionListener listener) {
223: taskSelectionListeners.add(listener);
224: }
225:
226: /**
227: * Supprime un listener.
228: * @param listener le listener.
229: */
230: public void removeTaskListener(ITaskSelectionListener listener) {
231: taskSelectionListeners.remove(listener);
232: }
233:
234: /**
235: * This method initializes group
236: *
237: */
238: private void createGroup() {
239: GridData gridData1 = new GridData();
240: gridData1.grabExcessHorizontalSpace = true;
241: gridData1.horizontalAlignment = org.eclipse.swt.layout.GridData.FILL;
242: GridData gridData = new GridData();
243: gridData.horizontalAlignment = org.eclipse.swt.layout.GridData.FILL;
244: gridData.grabExcessHorizontalSpace = true;
245: GridLayout gridLayout1 = new GridLayout();
246: gridLayout1.numColumns = 4;
247: group = new Group(this , SWT.NONE);
248: group.setText("Search filter");
249: group.setLayout(gridLayout1);
250: group.setLayoutData(gridData1);
251: createSearchFieldCombo();
252: createSearchCriteriaCombo();
253: searchText = new Text(group, SWT.BORDER);
254: searchText.setLayoutData(gridData);
255: findButton = new Button(group, SWT.NONE);
256: findButton.setText("Find...");
257: findButton
258: .addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
259: public void widgetSelected(
260: org.eclipse.swt.events.SelectionEvent e) {
261: buttonPressed();
262: }
263: });
264: }
265:
266: } // @jve:decl-index=0:visual-constraint="10,10"
|