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