1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.message.ui;
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;
26 public class FilterDialog extends TrayDialog {
30 // entries count limit
35 private Button errorButton;
36 private Button warningButton;
37 private Button infoButton;
40 private Button showAllButton;
42 // filter stack trace elements in EventDetailsDialog
43 private Button filterEnabled;
44 private Button addFilter;
45 private Button removeFilter;
46 private List filterList;
48 private IMemento memento;
50 public FilterDialog(Shell parentShell, IMemento memento) {
52 this.memento = memento;
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);
62 Dialog.applyDialogFont(container);
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);
71 group.setLayoutData(gd);
72 group.setText(Messages.LogView_FilterDialog_eventTypes);
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$
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$
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$
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));
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());
103 limitText = new Text(comp, SWT.BORDER);
104 limitText.addModifyListener(new ModifyListener() {
105 public void modifyText(ModifyEvent e) {
107 if (okButton == null)
109 Integer.parseInt(limitText.getText());
110 okButton.setEnabled(true);
111 } catch (NumberFormatException e1) {
112 okButton.setEnabled(false);
116 limitText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
117 limitText.setText(memento.getString(LogView.P_LOG_LIMIT));
118 limitText.setEnabled(limit.getSelection());
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));
127 Label label = new Label(container, SWT.NONE);
128 label.setText(Messages.LogView_FilterDialog_eventsLogged);
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);
136 Button button = new Button(container, SWT.RADIO);
137 button.setText(Messages.LogView_FilterDialog_recentSession);
139 gd.horizontalIndent = 20;
140 button.setLayoutData(gd);
142 if (memento.getString(LogView.P_SHOW_ALL_SESSIONS).equals("true")) { //$NON-NLS-1$
143 showAllButton.setSelection(true);
145 button.setSelection(true);
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));
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());
167 filterList = new List(comp, SWT.BORDER);
168 gd = new GridData(GridData.FILL_HORIZONTAL | GridData.FILL_VERTICAL);
171 gd.horizontalIndent = 20;
172 filterList.setLayoutData(gd);
173 filterList.addSelectionListener(new SelectionAdapter() {
174 public void widgetSelected(SelectionEvent e) {
175 removeFilter.setEnabled(true);
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) {
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) {
201 Boolean enable = memento.getBoolean(EventDetailsDialog.FILTER_ENABLED);
202 enable = enable == null ? Boolean.FALSE : enable;
204 filterEnabled.setSelection(enable.booleanValue());
205 setStackTraceFilterEnabled(enable.booleanValue());
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());
216 private void addFilter() {
217 IInputValidator validator = new IInputValidator() {
219 public String isValid(String newText) {
220 return newText.indexOf(';') >= 0 ? Messages.FilterDialog_FilterShouldntContainSemicolon : null;
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();
228 if (value.length() > 0) {
229 filterList.add(value);
234 private void removeFilter() {
235 int index = filterList.getSelectionIndex();
237 filterList.remove(index);
240 removeFilter.setEnabled(false);
243 private void setStackTraceFilterEnabled(boolean enabled) {
244 filterList.setEnabled(enabled);
245 addFilter.setEnabled(enabled);
246 removeFilter.setEnabled(enabled && filterList.getSelectionIndex() != -1);
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);
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$
262 // store Event Dialog stack trace filter preferences
263 memento.putBoolean(EventDetailsDialog.FILTER_ENABLED, filterEnabled.getSelection());
265 StringBuffer sb = new StringBuffer();
266 String[] items = filterList.getItems();
267 for (int i = 0; i < items.length; i++) {
269 if (i < items.length - 1) {
270 sb.append(";"); //$NON-NLS-1$
273 memento.putString(EventDetailsDialog.FILTER_LIST, sb.toString());