Commit | Line | Data |
---|---|---|
73005152 | 1 | /********************************************************************** |
c8422608 | 2 | * Copyright (c) 2005, 2012 IBM Corporation, Ericsson |
73005152 BH |
3 | * All rights reserved. This program and the accompanying materials |
4 | * are made available under the terms of the Eclipse Public License v1.0 | |
5 | * which accompanies this distribution, and is available at | |
6 | * http://www.eclipse.org/legal/epl-v10.html | |
013a5f1c AM |
7 | * |
8 | * Contributors: | |
c8422608 AM |
9 | * IBM - Initial API and implementation |
10 | * Bernd Hufmann - Updated for TMF | |
73005152 | 11 | **********************************************************************/ |
c8422608 | 12 | |
df0b8ff4 | 13 | package org.eclipse.linuxtools.tmf.ui.views.uml2sd.dialogs; |
73005152 BH |
14 | |
15 | import java.util.ArrayList; | |
16 | import java.util.List; | |
17 | ||
18 | import org.eclipse.jface.dialogs.Dialog; | |
19 | import org.eclipse.jface.dialogs.DialogSettings; | |
20 | import org.eclipse.jface.dialogs.IDialogConstants; | |
8fd82db5 | 21 | import org.eclipse.linuxtools.internal.tmf.ui.Activator; |
73005152 BH |
22 | import org.eclipse.linuxtools.tmf.ui.views.uml2sd.SDView; |
23 | import org.eclipse.linuxtools.tmf.ui.views.uml2sd.core.AsyncMessage; | |
24 | import org.eclipse.linuxtools.tmf.ui.views.uml2sd.core.AsyncMessageReturn; | |
25 | import org.eclipse.linuxtools.tmf.ui.views.uml2sd.core.GraphNode; | |
26 | import org.eclipse.linuxtools.tmf.ui.views.uml2sd.core.Lifeline; | |
27 | import org.eclipse.linuxtools.tmf.ui.views.uml2sd.core.Stop; | |
28 | import org.eclipse.linuxtools.tmf.ui.views.uml2sd.core.SyncMessage; | |
29 | import org.eclipse.linuxtools.tmf.ui.views.uml2sd.core.SyncMessageReturn; | |
30 | import org.eclipse.linuxtools.tmf.ui.views.uml2sd.handlers.provider.ISDFindProvider; | |
31 | import org.eclipse.linuxtools.tmf.ui.views.uml2sd.handlers.provider.ISDGraphNodeSupporter; | |
32 | import org.eclipse.linuxtools.tmf.ui.views.uml2sd.util.SDMessages; | |
33 | import org.eclipse.swt.SWT; | |
34 | import org.eclipse.swt.widgets.Button; | |
35 | import org.eclipse.swt.widgets.Composite; | |
36 | import org.eclipse.swt.widgets.Control; | |
37 | import org.eclipse.swt.widgets.TabFolder; | |
38 | ||
39 | /** | |
40 | * This is the common dialog to define Find and/or Filter Criteria(s) | |
013a5f1c AM |
41 | * |
42 | * @version 1.0 | |
43 | * @author Bernd Hufmann | |
73005152 BH |
44 | */ |
45 | public class SearchFilterDialog extends Dialog { | |
46 | ||
df0b8ff4 BH |
47 | // ------------------------------------------------------------------------ |
48 | // Constants | |
49 | // ------------------------------------------------------------------------ | |
50 | /** | |
51 | * The find criteria property name | |
52 | */ | |
73005152 | 53 | protected static final String FIND_CRITERIA = "findCriteria"; //$NON-NLS-1$ |
df0b8ff4 BH |
54 | /** |
55 | * The find expression list property name | |
56 | */ | |
73005152 | 57 | protected static final String FIND_EXPRESSION_LIST = "findExpressionList"; //$NON-NLS-1$ |
df0b8ff4 | 58 | /** |
013a5f1c | 59 | * The filter criteria poperty name |
df0b8ff4 | 60 | */ |
73005152 | 61 | protected static final String FILTER_CRITERIA = "filterCriteria"; //$NON-NLS-1$ |
df0b8ff4 BH |
62 | /** |
63 | * The filter expression list property name | |
64 | */ | |
73005152 | 65 | protected static final String FILTER_EXPRESSION_LIST = "filterExpressionList"; //$NON-NLS-1$ |
df0b8ff4 BH |
66 | /** |
67 | * The maximum number of expressions stored. | |
68 | */ | |
73005152 BH |
69 | protected static final int MAX_EXPRESSION_LIST = 7; |
70 | ||
df0b8ff4 BH |
71 | // ------------------------------------------------------------------------ |
72 | // Attributes | |
73 | // ------------------------------------------------------------------------ | |
74 | ||
73005152 | 75 | /** |
df0b8ff4 | 76 | * The sequence diagram view reference. |
73005152 | 77 | */ |
eb63f5ff | 78 | protected SDView fSdView = null; |
73005152 BH |
79 | |
80 | /** | |
df0b8ff4 | 81 | * The tab with the controls for a Criteria |
73005152 | 82 | */ |
eb63f5ff | 83 | protected TabFolder fTabFolder = null; |
73005152 BH |
84 | |
85 | /** | |
df0b8ff4 | 86 | * The Criteria updated by this dialog |
73005152 | 87 | */ |
eb63f5ff | 88 | protected Criteria fCriteria = null; |
73005152 BH |
89 | |
90 | /** | |
df0b8ff4 | 91 | * The find/filter provider telling which graph nodes are supported |
73005152 | 92 | */ |
eb63f5ff | 93 | protected ISDGraphNodeSupporter fProvider = null; |
73005152 BH |
94 | |
95 | /** | |
df0b8ff4 | 96 | * The okText is the text for the Ok button and title is the title of the dialog.<br> |
013a5f1c | 97 | * Both depend (okText and title (below)) on the usage that is done of this dialog |
df0b8ff4 | 98 | * (find or filter). |
73005152 | 99 | */ |
eb63f5ff | 100 | protected String fOkText; |
013a5f1c | 101 | |
df0b8ff4 BH |
102 | /** |
103 | * The title is the title of the dialog.<br> | |
013a5f1c | 104 | * Both depend (okText and title) on the usage that is done of this dialog |
df0b8ff4 BH |
105 | * (find or filter). |
106 | */ | |
eb63f5ff | 107 | protected String fTitle; |
73005152 BH |
108 | |
109 | /** | |
110 | * List of string expressions that have been searched already | |
111 | */ | |
eb63f5ff | 112 | protected String[] fExpressionList; |
73005152 BH |
113 | |
114 | /** | |
115 | * find is true if the dialog is for the find feature and false for filter feature | |
116 | */ | |
eb63f5ff | 117 | protected boolean fIsFind; |
73005152 | 118 | |
df0b8ff4 BH |
119 | // ------------------------------------------------------------------------ |
120 | // Constructors | |
121 | // ------------------------------------------------------------------------ | |
122 | /** | |
123 | * Standard constructor | |
013a5f1c | 124 | * |
eb63f5ff BH |
125 | * @param view A sequence diagram view reference |
126 | * @param provider A graph node supporter provider | |
013a5f1c | 127 | * @param filter A flag to indicate filtering (true) or finding (false) |
df0b8ff4 BH |
128 | * @param style Style bits |
129 | */ | |
eb63f5ff BH |
130 | public SearchFilterDialog(SDView view, ISDGraphNodeSupporter provider, boolean filter, int style) { |
131 | super(view.getSDWidget().getShell()); | |
df0b8ff4 | 132 | setShellStyle(SWT.DIALOG_TRIM | style); |
eb63f5ff BH |
133 | fProvider = provider; |
134 | fSdView = view; | |
135 | fIsFind = !filter; | |
df0b8ff4 | 136 | } |
013a5f1c | 137 | |
df0b8ff4 BH |
138 | // ------------------------------------------------------------------------ |
139 | // Methods | |
140 | // ------------------------------------------------------------------------ | |
141 | ||
73005152 BH |
142 | /* |
143 | * (non-Javadoc) | |
144 | * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite) | |
145 | */ | |
146 | @Override | |
147 | public Control createDialogArea(Composite arg0) { | |
eb63f5ff | 148 | if (fIsFind) { |
8fd82db5 | 149 | fExpressionList = Activator.getDefault().getDialogSettings().getArray(FIND_EXPRESSION_LIST); |
df0b8ff4 | 150 | } else { |
8fd82db5 | 151 | fExpressionList = Activator.getDefault().getDialogSettings().getArray(FILTER_EXPRESSION_LIST); |
df0b8ff4 | 152 | } |
eb63f5ff BH |
153 | if (fExpressionList == null) { |
154 | fExpressionList = new String[0]; | |
73005152 | 155 | } |
eb63f5ff | 156 | return new TabContents(arg0, fProvider, getButton(IDialogConstants.OK_ID), fExpressionList); |
73005152 BH |
157 | } |
158 | ||
73005152 BH |
159 | /** |
160 | * Open the dialog box | |
161 | */ | |
162 | @Override | |
163 | public int open() { | |
164 | create(); | |
165 | ||
eb63f5ff | 166 | if (fCriteria == null) { |
73005152 | 167 | loadCriteria(); |
df0b8ff4 BH |
168 | } |
169 | ||
eb63f5ff BH |
170 | if (fCriteria == null) { |
171 | fCriteria = new Criteria(); | |
172 | fCriteria.setLifeLineSelected(fProvider.isNodeSupported(ISDGraphNodeSupporter.LIFELINE)); | |
173 | fCriteria.setSyncMessageSelected(fProvider.isNodeSupported(ISDGraphNodeSupporter.SYNCMESSAGE)); | |
174 | fCriteria.setSyncMessageReturnSelected(fProvider.isNodeSupported(ISDGraphNodeSupporter.SYNCMESSAGERETURN)); | |
175 | fCriteria.setAsyncMessageSelected(fProvider.isNodeSupported(ISDGraphNodeSupporter.ASYNCMESSAGE)); | |
176 | fCriteria.setAsyncMessageReturnSelected(fProvider.isNodeSupported(ISDGraphNodeSupporter.ASYNCMESSAGERETURN)); | |
177 | fCriteria.setStopSelected(fProvider.isNodeSupported(ISDGraphNodeSupporter.STOP)); | |
73005152 | 178 | } |
eb63f5ff | 179 | copyFromCriteria(fCriteria); |
73005152 | 180 | |
eb63f5ff BH |
181 | if (fOkText != null) { |
182 | getButton(IDialogConstants.OK_ID).setText(fOkText); | |
73005152 BH |
183 | } else { |
184 | getButton(IDialogConstants.OK_ID).setText(SDMessages._21); | |
185 | } | |
186 | ||
eb63f5ff | 187 | if (fIsFind) { |
73005152 BH |
188 | getButton(IDialogConstants.CANCEL_ID).setText(SDMessages._22); |
189 | } | |
190 | ||
191 | Button okButton = getButton(IDialogConstants.OK_ID); | |
192 | ((TabContents) getDialogArea()).setOkButton(okButton); | |
eb63f5ff BH |
193 | if (fCriteria == null || !((fCriteria.getExpression() != null && !fCriteria.getExpression().equals("")) && //$NON-NLS-1$ |
194 | (fCriteria.isAsyncMessageReturnSelected() || fCriteria.isAsyncMessageSelected() || fCriteria.isLifeLineSelected() || fCriteria.isStopSelected() || fCriteria.isSyncMessageReturnSelected() || fCriteria.isSyncMessageSelected()))) { | |
73005152 BH |
195 | okButton.setEnabled(false); |
196 | } | |
197 | ||
eb63f5ff BH |
198 | if (fTitle != null) { |
199 | getShell().setText(fTitle); | |
73005152 BH |
200 | } else { |
201 | getShell().setText(SDMessages._24); | |
202 | } | |
203 | ||
204 | getShell().pack(); | |
205 | getShell().setLocation(getShell().getDisplay().getCursorLocation()); | |
206 | ||
eb63f5ff | 207 | fCriteria = null; |
73005152 BH |
208 | return super.open(); |
209 | } | |
210 | ||
211 | /** | |
df0b8ff4 | 212 | * Loads criteria from the dialog settings which are saved in the workspace. |
73005152 BH |
213 | */ |
214 | @SuppressWarnings("rawtypes") | |
215 | protected void loadCriteria() { | |
216 | ||
217 | String CRITERIA = FIND_CRITERIA; | |
eb63f5ff | 218 | if (!fIsFind) { |
73005152 | 219 | CRITERIA = FILTER_CRITERIA; |
df0b8ff4 | 220 | } |
73005152 | 221 | |
8fd82db5 | 222 | DialogSettings section = (DialogSettings) Activator.getDefault().getDialogSettings().getSection(CRITERIA); |
eb63f5ff BH |
223 | List selection = fSdView.getSDWidget().getSelection(); |
224 | if ((selection == null || selection.size() != 1) || (!fIsFind)) { | |
73005152 | 225 | if (section != null) { |
eb63f5ff BH |
226 | fCriteria = new Criteria(); |
227 | fCriteria.load(section); | |
73005152 BH |
228 | } |
229 | } else { | |
230 | GraphNode gn = (GraphNode) selection.get(0); | |
eb63f5ff BH |
231 | fCriteria = new Criteria(); |
232 | fCriteria.setExpression(gn.getName()); | |
233 | fCriteria.setCaseSenstiveSelected(true); | |
234 | if (gn instanceof Lifeline && fProvider.isNodeSupported(ISDGraphNodeSupporter.LIFELINE)) { | |
235 | fCriteria.setLifeLineSelected(true); | |
236 | } else if (gn instanceof SyncMessageReturn && fProvider.isNodeSupported(ISDGraphNodeSupporter.SYNCMESSAGERETURN)) { | |
237 | fCriteria.setSyncMessageReturnSelected(true); | |
238 | } else if ((gn instanceof SyncMessageReturn || gn instanceof SyncMessage) && fProvider.isNodeSupported(ISDGraphNodeSupporter.SYNCMESSAGE)) { | |
239 | fCriteria.setSyncMessageSelected(true); | |
240 | } else if (gn instanceof AsyncMessageReturn && fProvider.isNodeSupported(ISDGraphNodeSupporter.ASYNCMESSAGERETURN)) { | |
241 | fCriteria.setAsyncMessageReturnSelected(true); | |
242 | } else if ((gn instanceof AsyncMessageReturn || gn instanceof AsyncMessage) && fProvider.isNodeSupported(ISDGraphNodeSupporter.ASYNCMESSAGE)) { | |
243 | fCriteria.setAsyncMessageSelected(true); | |
244 | } else if (gn instanceof Stop && fProvider.isNodeSupported(ISDGraphNodeSupporter.STOP)) { | |
245 | fCriteria.setStopSelected(true); | |
73005152 BH |
246 | } |
247 | } | |
248 | } | |
249 | ||
250 | /** | |
013a5f1c | 251 | * Called when the dialog box ok button is pressed and calls back |
df0b8ff4 | 252 | * the appropriate action provider (ISDFilterProvider or ISDFindProvider). |
73005152 BH |
253 | */ |
254 | @Override | |
255 | public void okPressed() { | |
256 | copyToCriteria(); | |
eb63f5ff | 257 | if (!fIsFind) { |
73005152 BH |
258 | saveCriteria(); |
259 | super.close(); // Filter is modal | |
260 | } | |
eb63f5ff BH |
261 | if ((fProvider != null) && (fProvider instanceof ISDFindProvider) && fIsFind) { |
262 | boolean result = ((ISDFindProvider) fProvider).find(fCriteria); | |
73005152 BH |
263 | TabContents content = getTabContents(); |
264 | content.setResult(result); | |
265 | } | |
266 | } | |
267 | ||
df0b8ff4 BH |
268 | /* |
269 | * (non-Javadoc) | |
270 | * @see org.eclipse.jface.dialogs.Dialog#cancelPressed() | |
271 | */ | |
73005152 BH |
272 | @Override |
273 | public void cancelPressed() { | |
eb63f5ff | 274 | if (fIsFind) { |
73005152 | 275 | copyToCriteria(); |
eb63f5ff BH |
276 | if (fProvider instanceof ISDFindProvider) { |
277 | ((ISDFindProvider) fProvider).cancel(); | |
73005152 BH |
278 | } |
279 | saveCriteria(); | |
280 | } | |
281 | super.cancelPressed(); | |
282 | } | |
283 | ||
284 | /** | |
df0b8ff4 | 285 | * Saves the criteria to the dialog settings within the workspace. |
73005152 BH |
286 | */ |
287 | public void saveCriteria() { | |
288 | String CRITERIA = FIND_CRITERIA; | |
289 | String EXPRESSION_LIST = FIND_EXPRESSION_LIST; | |
eb63f5ff | 290 | if (!fIsFind) { |
73005152 BH |
291 | CRITERIA = FILTER_CRITERIA; |
292 | EXPRESSION_LIST = FILTER_EXPRESSION_LIST; | |
293 | } | |
8fd82db5 | 294 | DialogSettings settings = (DialogSettings) Activator.getDefault().getDialogSettings(); |
73005152 BH |
295 | DialogSettings section = (DialogSettings) settings.getSection(CRITERIA); |
296 | if (section == null) { | |
297 | section = (DialogSettings) settings.addNewSection(CRITERIA); | |
298 | } | |
eb63f5ff | 299 | fCriteria.save(section); |
73005152 | 300 | |
eb63f5ff | 301 | if (fCriteria.getExpression().length() > 0) { |
73005152 | 302 | ArrayList<String> list = new ArrayList<String>(); |
eb63f5ff BH |
303 | for (int i = 0; i < fExpressionList.length; i++) { |
304 | list.add(fExpressionList[i]); | |
73005152 BH |
305 | } |
306 | // Remove the used expression if one from the dropdown list | |
eb63f5ff | 307 | list.remove(fCriteria.getExpression()); |
73005152 | 308 | // Put the new expression at the beginning |
eb63f5ff | 309 | list.add(0, fCriteria.getExpression()); |
73005152 BH |
310 | // Fill in the expressionList, truncating to MAX_EXPRESSION_LIST |
311 | int size = Math.min(list.size(), MAX_EXPRESSION_LIST); | |
312 | String[] temp = new String[size]; | |
313 | for (int i = 0; i < size; i++) { | |
abbdd66a | 314 | temp[i] = list.get(i); |
73005152 | 315 | } |
eb63f5ff BH |
316 | fExpressionList = temp; |
317 | settings.put(EXPRESSION_LIST, fExpressionList); | |
73005152 BH |
318 | } |
319 | } | |
320 | ||
321 | /** | |
df0b8ff4 | 322 | * Returns the criteria |
013a5f1c | 323 | * |
df0b8ff4 | 324 | * @return the criteria |
73005152 BH |
325 | */ |
326 | public Criteria getCriteria() { | |
eb63f5ff | 327 | return fCriteria; |
73005152 BH |
328 | } |
329 | ||
330 | /** | |
df0b8ff4 | 331 | * Sets the criteria. |
013a5f1c | 332 | * |
eb63f5ff | 333 | * @param criteria the criteria to set. |
73005152 | 334 | */ |
eb63f5ff BH |
335 | public void setCriteria(Criteria criteria) { |
336 | fCriteria = criteria; | |
73005152 BH |
337 | } |
338 | ||
339 | /** | |
340 | * Get the current end-user settings from the dialog to a Criteria | |
73005152 BH |
341 | */ |
342 | public void copyToCriteria() { | |
eb63f5ff | 343 | fCriteria = new Criteria(); |
73005152 | 344 | TabContents content = getTabContents(); |
eb63f5ff BH |
345 | fCriteria.setLifeLineSelected(content.isLifelineButtonSelected()); |
346 | fCriteria.setSyncMessageSelected(content.isSynMessageButtonSelected()); | |
347 | fCriteria.setSyncMessageReturnSelected(content.isSynMessageReturnButtonSelected()); | |
348 | fCriteria.setAsyncMessageSelected(content.isAsynMessageButtonSelected()); | |
349 | fCriteria.setAsyncMessageReturnSelected(content.isAsynMessageReturnButtonSelected()); | |
350 | fCriteria.setStopSelected(content.isStopButtonSelected()); | |
351 | fCriteria.setCaseSenstiveSelected(content.isCaseSensitiveSelected()); | |
352 | fCriteria.setExpression(content.getSearchText()); | |
73005152 BH |
353 | } |
354 | ||
355 | /** | |
df0b8ff4 | 356 | * Returns the tab content reference. |
013a5f1c | 357 | * |
0d9a6d76 | 358 | * @return the tab content |
73005152 BH |
359 | */ |
360 | protected TabContents getTabContents() { | |
361 | TabContents content = null; | |
eb63f5ff | 362 | if (fTabFolder == null) { |
73005152 | 363 | content = (TabContents) getDialogArea(); |
df0b8ff4 | 364 | } else { |
eb63f5ff | 365 | content = (TabContents) fTabFolder.getSelection()[0].getControl(); |
df0b8ff4 | 366 | } |
73005152 BH |
367 | return content; |
368 | } | |
369 | ||
370 | /** | |
371 | * Initialize the dialog with the settings of an existing Criteria<br> | |
372 | * Criteria must not be null and the TabContents must have been created | |
013a5f1c | 373 | * |
df0b8ff4 | 374 | * @param from the criteria to copy from |
73005152 BH |
375 | */ |
376 | public void copyFromCriteria(Criteria from) { | |
377 | TabContents content = getTabContents(); | |
378 | content.setLifelineButtonSelection(from.isLifeLineSelected()); | |
379 | content.setSynMessageButtonSelection(from.isSyncMessageSelected()); | |
380 | content.setSynMessageReturnButtonSelection(from.isSyncMessageReturnSelected()); | |
381 | content.setAsynMessageButtonSelection(from.isAsyncMessageSelected()); | |
382 | content.setAsynMessageReturnButtonSelection(from.isSyncMessageReturnSelected()); | |
383 | content.setStopButtonSelection(from.isStopSelected()); | |
384 | content.setCaseSensitiveSelection(from.isCaseSenstiveSelected()); | |
385 | if (from.getExpression() != null) { | |
386 | content.setSearchText(from.getExpression()); | |
387 | } | |
388 | } | |
389 | ||
390 | /** | |
df0b8ff4 | 391 | * Sets the text to be used for the ok button |
013a5f1c | 392 | * |
eb63f5ff | 393 | * @param okText text to set |
73005152 | 394 | */ |
eb63f5ff BH |
395 | public void setOkText(String okText) { |
396 | fOkText = okText; | |
73005152 BH |
397 | } |
398 | ||
399 | /** | |
df0b8ff4 | 400 | * Sets the title to be used for the dialog box. |
013a5f1c | 401 | * |
eb63f5ff | 402 | * @param title The title to set |
73005152 | 403 | */ |
eb63f5ff BH |
404 | public void setTitle(String title) { |
405 | fTitle = title; | |
73005152 BH |
406 | } |
407 | } |