]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.message.ui/src/org/simantics/message/ui/FilterDialog.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.message.ui / src / org / simantics / message / ui / FilterDialog.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.message.ui;
13
14 import java.util.StringTokenizer;
15 import org.eclipse.jface.dialogs.*;
16 import org.eclipse.jface.dialogs.Dialog;
17 import org.eclipse.jface.window.Window;
18 import org.eclipse.swt.SWT;
19 import org.eclipse.swt.events.*;
20 import org.eclipse.swt.layout.GridData;
21 import org.eclipse.swt.layout.GridLayout;
22 import org.eclipse.swt.widgets.*;
23 import org.eclipse.swt.widgets.Group;
24 import org.eclipse.ui.IMemento;
25
26 public class FilterDialog extends TrayDialog {
27
28         Button okButton;
29
30         // entries count limit
31         private Button limit;
32         Text limitText;
33
34         // entry types filter
35         private Button errorButton;
36         private Button warningButton;
37         private Button infoButton;
38
39         // show all sessions
40         private Button showAllButton;
41
42         // filter stack trace elements in EventDetailsDialog
43         private Button filterEnabled;
44         private Button addFilter;
45         private Button removeFilter;
46         private List filterList;
47
48         private IMemento memento;
49
50         public FilterDialog(Shell parentShell, IMemento memento) {
51                 super(parentShell);
52                 this.memento = memento;
53         }
54
55         protected Control createDialogArea(Composite parent) {
56                 Composite container = (Composite) super.createDialogArea(parent);
57                 createEventTypesGroup(container);
58                 createLimitSection(container);
59                 createSessionSection(container);
60                 createFilterSection(container);
61
62                 Dialog.applyDialogFont(container);
63                 return container;
64         }
65
66         private void createEventTypesGroup(Composite parent) {
67                 Group group = new Group(parent, SWT.NONE);
68                 group.setLayout(new GridLayout());
69                 GridData gd = new GridData(GridData.FILL_HORIZONTAL);
70                 gd.widthHint = 275;
71                 group.setLayoutData(gd);
72                 group.setText(Messages.LogView_FilterDialog_eventTypes);
73
74                 infoButton = new Button(group, SWT.CHECK);
75                 infoButton.setText(Messages.LogView_FilterDialog_information);
76                 infoButton.setSelection(memento.getString(LogView.P_LOG_INFO).equals("true")); //$NON-NLS-1$
77
78                 warningButton = new Button(group, SWT.CHECK);
79                 warningButton.setText(Messages.LogView_FilterDialog_warning);
80                 warningButton.setSelection(memento.getString(LogView.P_LOG_WARNING).equals("true")); //$NON-NLS-1$
81
82                 errorButton = new Button(group, SWT.CHECK);
83                 errorButton.setText(Messages.LogView_FilterDialog_error);
84                 errorButton.setSelection(memento.getString(LogView.P_LOG_ERROR).equals("true")); //$NON-NLS-1$
85         }
86
87         private void createLimitSection(Composite parent) {
88                 Composite comp = new Composite(parent, SWT.NONE);
89                 GridLayout layout = new GridLayout();
90                 layout.numColumns = 2;
91                 comp.setLayout(layout);
92                 comp.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
93
94                 limit = new Button(comp, SWT.CHECK);
95                 limit.setText(Messages.LogView_FilterDialog_limitTo);
96                 limit.setSelection(memento.getString(LogView.P_USE_LIMIT).equals("true")); //$NON-NLS-1$
97                 limit.addSelectionListener(new SelectionAdapter() {
98                         public void widgetSelected(SelectionEvent e) {
99                                 limitText.setEnabled(((Button) e.getSource()).getSelection());
100                         }
101                 });
102
103                 limitText = new Text(comp, SWT.BORDER);
104                 limitText.addModifyListener(new ModifyListener() {
105                         public void modifyText(ModifyEvent e) {
106                                 try {
107                                         if (okButton == null)
108                                                 return;
109                                         Integer.parseInt(limitText.getText());
110                                         okButton.setEnabled(true);
111                                 } catch (NumberFormatException e1) {
112                                         okButton.setEnabled(false);
113                                 }
114                         }
115                 });
116                 limitText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
117                 limitText.setText(memento.getString(LogView.P_LOG_LIMIT));
118                 limitText.setEnabled(limit.getSelection());
119
120         }
121
122         private void createSessionSection(Composite parent) {
123                 Composite container = new Composite(parent, SWT.NONE);
124                 container.setLayout(new GridLayout());
125                 container.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
126
127                 Label label = new Label(container, SWT.NONE);
128                 label.setText(Messages.LogView_FilterDialog_eventsLogged);
129
130                 showAllButton = new Button(container, SWT.RADIO);
131                 showAllButton.setText(Messages.LogView_FilterDialog_allSessions);
132                 GridData gd = new GridData();
133                 gd.horizontalIndent = 20;
134                 showAllButton.setLayoutData(gd);
135
136                 Button button = new Button(container, SWT.RADIO);
137                 button.setText(Messages.LogView_FilterDialog_recentSession);
138                 gd = new GridData();
139                 gd.horizontalIndent = 20;
140                 button.setLayoutData(gd);
141
142                 if (memento.getString(LogView.P_SHOW_ALL_SESSIONS).equals("true")) { //$NON-NLS-1$
143                         showAllButton.setSelection(true);
144                 } else {
145                         button.setSelection(true);
146                 }
147         }
148
149         private void createFilterSection(Composite parent) {
150                 Composite comp = new Composite(parent, SWT.NONE);
151                 GridLayout layout = new GridLayout(2, false);
152                 comp.setLayout(layout);
153                 comp.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
154
155                 filterEnabled = new Button(comp, SWT.CHECK);
156                 filterEnabled.setText(Messages.FilterDialog_EnableFiltersCheckbox);
157                 GridData gd = new GridData();
158                 gd.horizontalSpan = 2;
159                 filterEnabled.setLayoutData(gd);
160                 filterEnabled.addSelectionListener(new SelectionAdapter() {
161                         public void widgetSelected(SelectionEvent e) {
162                                 setStackTraceFilterEnabled(filterEnabled.getSelection());
163                         }
164
165                 });
166
167                 filterList = new List(comp, SWT.BORDER);
168                 gd = new GridData(GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL);
169                 gd.verticalSpan = 3;
170                 gd.widthHint = 280;
171                 gd.horizontalIndent = 20;
172                 filterList.setLayoutData(gd);
173                 filterList.addSelectionListener(new SelectionAdapter() {
174                         public void widgetSelected(SelectionEvent e) {
175                                 removeFilter.setEnabled(true);
176                         }
177                 });
178
179                 addFilter = new Button(comp, SWT.NONE);
180                 gd = new GridData(GridData.FILL_HORIZONTAL);
181                 addFilter.setLayoutData(gd);
182                 addFilter.setText(Messages.FilterDialog_Add);
183                 addFilter.addSelectionListener(new SelectionAdapter() {
184                         public void widgetSelected(SelectionEvent e) {
185                                 addFilter();
186                         }
187                 });
188
189                 removeFilter = new Button(comp, SWT.NONE);
190                 gd = new GridData(GridData.FILL_HORIZONTAL);
191                 removeFilter.setLayoutData(gd);
192                 removeFilter.setText(Messages.FilterDialog_Remove);
193                 removeFilter.setEnabled(false);
194                 removeFilter.addSelectionListener(new SelectionAdapter() {
195                         public void widgetSelected(SelectionEvent e) {
196                                 removeFilter();
197                         }
198                 });
199
200                 // load preferences
201                 Boolean enable = memento.getBoolean(EventDetailsDialog.FILTER_ENABLED);
202                 enable = enable == null ? Boolean.FALSE : enable;
203
204                 filterEnabled.setSelection(enable.booleanValue());
205                 setStackTraceFilterEnabled(enable.booleanValue());
206
207                 String filters = memento.getString(EventDetailsDialog.FILTER_LIST);
208                 if (filters != null) {
209                         StringTokenizer st = new StringTokenizer(filters, ";"); //$NON-NLS-1$
210                         while (st.hasMoreElements()) {
211                                 filterList.add(st.nextToken());
212                         }
213                 }
214         }
215
216         private void addFilter() {
217                 IInputValidator validator = new IInputValidator() {
218
219                         public String isValid(String newText) {
220                                 return newText.indexOf(';') >= 0 ? Messages.FilterDialog_FilterShouldntContainSemicolon : null;
221                         }
222
223                 };
224                 InputDialog dialog = new InputDialog(getShell(), Messages.FilterDialog_AddFilterTitle, Messages.FilterDialog_AddFliterLabel, null, validator);
225                 if (dialog.open() == Window.OK) {
226                         String value = dialog.getValue().trim();
227
228                         if (value.length() > 0) {
229                                 filterList.add(value);
230                         }
231                 }
232         }
233
234         private void removeFilter() {
235                 int index = filterList.getSelectionIndex();
236                 if (index != -1) {
237                         filterList.remove(index);
238                 }
239
240                 removeFilter.setEnabled(false);
241         }
242
243         private void setStackTraceFilterEnabled(boolean enabled) {
244                 filterList.setEnabled(enabled);
245                 addFilter.setEnabled(enabled);
246                 removeFilter.setEnabled(enabled && filterList.getSelectionIndex() != -1);
247         }
248
249         protected void createButtonsForButtonBar(Composite parent) {
250                 okButton = createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, true);
251                 createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false);
252         }
253
254         protected void okPressed() {
255                 memento.putString(LogView.P_LOG_INFO, infoButton.getSelection() ? "true" : "false"); //$NON-NLS-1$ //$NON-NLS-2$
256                 memento.putString(LogView.P_LOG_WARNING, warningButton.getSelection() ? "true" : "false"); //$NON-NLS-1$ //$NON-NLS-2$
257                 memento.putString(LogView.P_LOG_ERROR, errorButton.getSelection() ? "true" : "false"); //$NON-NLS-1$ //$NON-NLS-2$
258                 memento.putString(LogView.P_LOG_LIMIT, limitText.getText());
259                 memento.putString(LogView.P_USE_LIMIT, limit.getSelection() ? "true" : "false"); //$NON-NLS-1$ //$NON-NLS-2$
260                 memento.putString(LogView.P_SHOW_ALL_SESSIONS, showAllButton.getSelection() ? "true" : "false"); //$NON-NLS-1$ //$NON-NLS-2$
261
262                 // store Event Dialog stack trace filter preferences
263                 memento.putBoolean(EventDetailsDialog.FILTER_ENABLED, filterEnabled.getSelection());
264
265                 StringBuffer sb = new StringBuffer();
266                 String[] items = filterList.getItems();
267                 for (int i = 0; i < items.length; i++) {
268                         sb.append(items[i]);
269                         if (i < items.length - 1) {
270                                 sb.append(";"); //$NON-NLS-1$
271                         }
272                 }
273                 memento.putString(EventDetailsDialog.FILTER_LIST, sb.toString());
274
275                 super.okPressed();
276         }
277
278 }