001: /*******************************************************************************
002: * Copyright (c) 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: ******************************************************************************/package org.eclipse.ui.internal.provisional.views.markers;
011:
012: import java.util.ArrayList;
013: import java.util.Collection;
014: import java.util.Iterator;
015:
016: import org.eclipse.jface.dialogs.Dialog;
017: import org.eclipse.jface.dialogs.IInputValidator;
018: import org.eclipse.jface.dialogs.InputDialog;
019: import org.eclipse.jface.viewers.CheckboxTableViewer;
020: import org.eclipse.jface.viewers.ISelection;
021: import org.eclipse.jface.viewers.ISelectionChangedListener;
022: import org.eclipse.jface.viewers.IStructuredContentProvider;
023: import org.eclipse.jface.viewers.IStructuredSelection;
024: import org.eclipse.jface.viewers.LabelProvider;
025: import org.eclipse.jface.viewers.SelectionChangedEvent;
026: import org.eclipse.jface.viewers.StructuredSelection;
027: import org.eclipse.jface.viewers.Viewer;
028: import org.eclipse.jface.window.IShellProvider;
029: import org.eclipse.osgi.util.NLS;
030: import org.eclipse.swt.SWT;
031: import org.eclipse.swt.events.DisposeEvent;
032: import org.eclipse.swt.events.DisposeListener;
033: import org.eclipse.swt.events.SelectionAdapter;
034: import org.eclipse.swt.events.SelectionEvent;
035: import org.eclipse.swt.layout.GridData;
036: import org.eclipse.swt.layout.GridLayout;
037: import org.eclipse.swt.widgets.Button;
038: import org.eclipse.swt.widgets.Composite;
039: import org.eclipse.swt.widgets.Control;
040: import org.eclipse.swt.widgets.Label;
041: import org.eclipse.ui.forms.events.ExpansionEvent;
042: import org.eclipse.ui.forms.events.IExpansionListener;
043: import org.eclipse.ui.forms.widgets.ExpandableComposite;
044: import org.eclipse.ui.forms.widgets.FormToolkit;
045: import org.eclipse.ui.forms.widgets.ScrolledForm;
046: import org.eclipse.ui.internal.provisional.views.markers.api.FilterConfigurationArea;
047: import org.eclipse.ui.views.markers.internal.MarkerMessages;
048:
049: /**
050: * FiltersConfigurationDialog is the dialog for configuring the filters for the
051: *
052: * @since 3.3
053: *
054: */
055: public class FiltersConfigurationDialog extends Dialog {
056:
057: private Collection filterAreas;
058:
059: private Collection filterGroups;
060:
061: private CheckboxTableViewer filtersList;
062:
063: private GroupFilterConfigurationArea scopeArea = new ScopeArea();
064:
065: private MarkerFieldFilterGroup selectedFilterGroup;
066:
067: private ScrolledForm form;
068:
069: /**
070: * Create a new instance of the receiver on group.
071: *
072: * @param parentShell
073: * @param groups
074: * Collection of MarkerFieldFilterGroup
075: * @param fieldFilterAreas -
076: * Collection of FilterConfigurationArea
077: */
078: public FiltersConfigurationDialog(IShellProvider parentShell,
079: Collection groups, Collection fieldFilterAreas) {
080: super (parentShell);
081: filterGroups = makeWorkingCopy(groups);
082: filterAreas = fieldFilterAreas;
083: }
084:
085: /*
086: * (non-Javadoc)
087: *
088: * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
089: */
090: protected Control createDialogArea(Composite parent) {
091:
092: Composite top = (Composite) super .createDialogArea(parent);
093:
094: initializeDialogUnits(top);
095:
096: GridLayout layout = new GridLayout();
097: layout.numColumns = 2;
098: layout.makeColumnsEqualWidth = false;
099: top.setLayout(layout);
100:
101: createFilterSelectionArea(top);
102:
103: final FormToolkit toolkit = new FormToolkit(top.getDisplay());
104: parent.addDisposeListener(new DisposeListener() {
105:
106: public void widgetDisposed(DisposeEvent e) {
107: toolkit.dispose();
108:
109: }
110: });
111: form = toolkit.createScrolledForm(top);
112: form.setBackground(parent.getBackground());
113:
114: GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);
115: form.setLayoutData(data);
116: form.getBody().setLayout(new GridLayout());
117:
118: // Expand all of the filter areas if the choices are small
119: boolean expand = filterAreas.size() < 3;
120: createFieldArea(toolkit, form, scopeArea, expand);
121: Iterator areas = filterAreas.iterator();
122: while (areas.hasNext()) {
123: createFieldArea(toolkit, form,
124: (FilterConfigurationArea) areas.next(), expand);
125:
126: }
127:
128: if (filterGroups.isEmpty()) {
129: setFieldsVisible(false);
130: } else {
131: filtersList.setSelection(new StructuredSelection(
132: filterGroups.iterator().next()));
133: }
134:
135: return top;
136: }
137:
138: /**
139: * Set the visibility of the fields to visible.
140: */
141: private void setFieldsVisible(boolean visible) {
142: Control[] children = form.getChildren();
143: for (int i = 0; i < children.length; i++) {
144: children[i].setVisible(visible);
145: }
146: }
147:
148: /**
149: * Create a field area in the form for the FilterConfigurationArea
150: *
151: * @param toolkit
152: * @param form
153: * @param area
154: * @param expand
155: * <code>true</code> if the area should be expanded by default
156: */
157: private void createFieldArea(final FormToolkit toolkit,
158: final ScrolledForm form,
159: final FilterConfigurationArea area, boolean expand) {
160: final ExpandableComposite expandable = toolkit
161: .createExpandableComposite(form.getBody(),
162: ExpandableComposite.TWISTIE);
163: expandable.setText(area.getTitle());
164: expandable.setBackground(form.getBackground());
165: expandable.setLayout(new GridLayout());
166: expandable.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
167: true));
168: expandable.addExpansionListener(new IExpansionListener() {
169: /*
170: * (non-Javadoc)
171: *
172: * @see org.eclipse.ui.forms.events.IExpansionListener#expansionStateChanged(org.eclipse.ui.forms.events.ExpansionEvent)
173: */
174: public void expansionStateChanged(ExpansionEvent e) {
175: expandable.getParent().layout(true);
176:
177: }
178:
179: /*
180: * (non-Javadoc)
181: *
182: * @see org.eclipse.ui.forms.events.IExpansionListener#expansionStateChanging(org.eclipse.ui.forms.events.ExpansionEvent)
183: */
184: public void expansionStateChanging(ExpansionEvent e) {
185:
186: }
187: });
188:
189: Composite sectionClient = toolkit.createComposite(expandable);
190: sectionClient.setLayout(new GridLayout());
191: sectionClient.setLayoutData(new GridData(SWT.FILL, SWT.FILL,
192: true, true));
193: sectionClient.setBackground(form.getBackground());
194: area.createContents(sectionClient);
195: expandable.setClient(sectionClient);
196: expandable.setExpanded(expand);
197: }
198:
199: /**
200: * Create the area for selecting the filters and enabling/disabling them.
201: *
202: * @param top
203: */
204: private void createFilterSelectionArea(Composite top) {
205:
206: Composite filtersComposite = new Composite(top, SWT.NONE);
207: filtersComposite.setLayout(new GridLayout(2, false));
208: filtersComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL,
209: true, true));
210:
211: Label title = new Label(filtersComposite, SWT.NONE);
212: title.setText(MarkerMessages.filtersDialog_title);
213: GridData titleData = new GridData();
214: titleData.horizontalSpan = 2;
215: title.setLayoutData(titleData);
216:
217: filtersList = CheckboxTableViewer.newCheckList(
218: filtersComposite, SWT.BORDER);
219: filtersList
220: .setContentProvider(new IStructuredContentProvider() {
221: /*
222: * (non-Javadoc)
223: *
224: * @see org.eclipse.jface.viewers.IContentProvider#dispose()
225: */
226: public void dispose() {
227: // Do nothing
228: }
229:
230: /*
231: * (non-Javadoc)
232: *
233: * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
234: */
235: public Object[] getElements(Object inputElement) {
236: return filterGroups.toArray();
237: }
238:
239: /*
240: * (non-Javadoc)
241: *
242: * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer,
243: * java.lang.Object, java.lang.Object)
244: */
245: public void inputChanged(Viewer viewer,
246: Object oldInput, Object newInput) {
247: // Do nothing
248: }
249: });
250:
251: filtersList.setLabelProvider(new LabelProvider() {
252: /*
253: * (non-Javadoc)
254: *
255: * @see org.eclipse.jface.viewers.ILabelProvider#getText(java.lang.Object)
256: */
257: public String getText(Object element) {
258: return ((MarkerFieldFilterGroup) element).getName();
259: }
260: });
261:
262: if (selectedFilterGroup != null)
263: filtersList.setSelection(new StructuredSelection(
264: selectedFilterGroup));
265:
266: filtersList
267: .addSelectionChangedListener(new ISelectionChangedListener() {
268:
269: /*
270: * (non-Javadoc)
271: *
272: * @see org.eclipse.jface.viewers.ISelectionChangedListener#selectionChanged(org.eclipse.jface.viewers.SelectionChangedEvent)
273: */
274: public void selectionChanged(
275: SelectionChangedEvent event) {
276: setSelectedFilter((MarkerFieldFilterGroup) ((IStructuredSelection) event
277: .getSelection()).getFirstElement());
278:
279: }
280: });
281:
282: filtersList.setInput(this );
283:
284: Iterator filterIterator = filterGroups.iterator();
285: while (filterIterator.hasNext()) {
286: MarkerFieldFilterGroup group = (MarkerFieldFilterGroup) filterIterator
287: .next();
288: filtersList.setChecked(group, group.isEnabled());
289: }
290:
291: GridData listData = new GridData(SWT.FILL, SWT.FILL, true, true);
292: listData.widthHint = convertHorizontalDLUsToPixels(100);
293: filtersList.getControl().setLayoutData(listData);
294:
295: Composite buttons = new Composite(filtersComposite, SWT.NONE);
296: GridLayout buttonLayout = new GridLayout();
297: buttonLayout.marginWidth = 0;
298: buttons.setLayout(buttonLayout);
299: GridData buttonsData = new GridData();
300: buttonsData.verticalAlignment = GridData.BEGINNING;
301: buttons.setLayoutData(buttonsData);
302:
303: Button addNew = new Button(buttons, SWT.PUSH);
304: addNew.setText(MarkerMessages.MarkerFilter_addFilterName);
305: addNew.addSelectionListener(new SelectionAdapter() {
306: public void widgetSelected(SelectionEvent e) {
307: InputDialog newDialog = new InputDialog(getShell(),
308: MarkerMessages.MarkerFilterDialog_title,
309: MarkerMessages.MarkerFilterDialog_message,
310: MarkerMessages.MarkerFilter_newFilterName,
311: new IInputValidator() {
312: /*
313: * (non-Javadoc)
314: *
315: * @see org.eclipse.jface.dialogs.IInputValidator#isValid(java.lang.String)
316: */
317: public String isValid(String newText) {
318: if (newText.length() == 0)
319: return MarkerMessages.MarkerFilterDialog_emptyMessage;
320: Iterator filterIterator = filterGroups
321: .iterator();
322: while (filterIterator.hasNext()) {
323: if (((MarkerFieldFilterGroup) filterIterator
324: .next()).getName().equals(
325: newText))
326: return NLS
327: .bind(
328: MarkerMessages.filtersDialog_conflictingName,
329: newText);
330: }
331:
332: return null;
333: }
334: });
335: newDialog.open();
336: String newName = newDialog.getValue();
337: if (newName != null) {
338: createNewFilter(newName);
339: }
340: }
341: });
342: setButtonLayoutData(addNew);
343:
344: Button remove = new Button(buttons, SWT.PUSH);
345: remove.setText(MarkerMessages.MarkerFilter_deleteSelectedName);
346: remove.addSelectionListener(new SelectionAdapter() {
347: public void widgetSelected(SelectionEvent e) {
348: removeFilters(filtersList.getSelection());
349: }
350: });
351: setButtonLayoutData(remove);
352: }
353:
354: /**
355: * Create a new filter called newName
356: *
357: * @param newName
358: */
359: protected void createNewFilter(String newName) {
360: // TODO Create one
361:
362: }
363:
364: /**
365: * Return the filter groups modified by the receiver.
366: *
367: * @return Collection of {@link MarkerFieldFilterGroup}
368: */
369: public Collection getFilters() {
370: return filterGroups;
371: }
372:
373: /**
374: * Make a working copy of the groups.
375: *
376: * @param groups
377: * @return Collection of MarkerFieldFilterGroup
378: */
379: private Collection makeWorkingCopy(Collection groups) {
380: Iterator initialFiltersIterator = groups.iterator();
381: Collection returnFilters = new ArrayList(groups.size());
382: while (initialFiltersIterator.hasNext()) {
383: MarkerFieldFilterGroup group = (MarkerFieldFilterGroup) initialFiltersIterator
384: .next();
385: MarkerFieldFilterGroup copy = group.makeWorkingCopy();
386: if (copy != null)
387: returnFilters.add(copy);
388: }
389: return returnFilters;
390: }
391:
392: /*
393: * (non-Javadoc)
394: *
395: * @see org.eclipse.jface.dialogs.Dialog#okPressed()
396: */
397: protected void okPressed() {
398:
399: Iterator filterGroupIterator = filterGroups.iterator();
400: while (filterGroupIterator.hasNext()) {
401: MarkerFieldFilterGroup group = (MarkerFieldFilterGroup) filterGroupIterator
402: .next();
403: group.setEnabled(filtersList.getChecked(group));
404: }
405: if (selectedFilterGroup != null) {
406:
407: scopeArea.applyToGroup(selectedFilterGroup);
408: Iterator areas = filterAreas.iterator();
409: while (areas.hasNext()) {
410: FilterConfigurationArea area = (FilterConfigurationArea) areas
411: .next();
412:
413: // Handle the internal special cases
414: if (area instanceof GroupFilterConfigurationArea)
415: ((GroupFilterConfigurationArea) area)
416: .applyToGroup(selectedFilterGroup);
417: area.apply(selectedFilterGroup.getFilter(area
418: .getField()));
419: }
420: }
421: super .okPressed();
422:
423: }
424:
425: /**
426: * Remove the filters in selection.
427: *
428: * @param selection
429: */
430: protected void removeFilters(ISelection selection) {
431: // TODO Auto-generated method stub
432:
433: }
434:
435: /**
436: * Set the filter that is being worked on.
437: *
438: * @param markerFieldFilterGroup
439: */
440: void setSelectedFilter(MarkerFieldFilterGroup markerFieldFilterGroup) {
441: MarkerFieldFilterGroup old = selectedFilterGroup;
442: selectedFilterGroup = markerFieldFilterGroup;
443: if (old != null)
444: scopeArea.applyToGroup(old);
445:
446: if (selectedFilterGroup == null) {
447: setFieldsVisible(false);
448: return;
449: }
450: setFieldsVisible(true);
451: scopeArea.initializeFromGroup(selectedFilterGroup);
452: Iterator areas = filterAreas.iterator();
453: while (areas.hasNext()) {
454: FilterConfigurationArea area = (FilterConfigurationArea) areas
455: .next();
456: if (old != null) {
457: if (area instanceof GroupFilterConfigurationArea)
458: ((GroupFilterConfigurationArea) area)
459: .applyToGroup(old);
460: area.apply(old.getFilter(area.getField()));
461: }
462: if (area instanceof GroupFilterConfigurationArea)
463: ((GroupFilterConfigurationArea) area)
464: .initializeFromGroup(selectedFilterGroup);
465: area.initialize(selectedFilterGroup.getFilter(area
466: .getField()));
467: }
468: }
469:
470: }
|