1 /*******************************************************************************
2 * Copyright (c) 2000, 2004 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.ui.actions;
13 import java.util.ArrayList;
14 import java.util.Arrays;
15 import java.util.HashMap;
16 import java.util.HashSet;
17 import java.util.Iterator;
18 import java.util.List;
21 import java.util.SortedSet;
22 import java.util.Stack;
23 import java.util.StringTokenizer;
24 import java.util.TreeSet;
26 import net.sourceforge.phpdt.core.IJavaModel;
27 import net.sourceforge.phpdt.internal.ui.PHPUiImages;
28 import net.sourceforge.phpdt.internal.ui.filters.CustomFiltersDialog;
29 import net.sourceforge.phpdt.internal.ui.filters.FilterDescriptor;
30 import net.sourceforge.phpdt.internal.ui.filters.FilterMessages;
31 import net.sourceforge.phpdt.internal.ui.filters.NamePatternFilter;
32 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
33 import net.sourceforge.phpeclipse.ui.WebUI;
35 import org.eclipse.jface.action.Action;
36 import org.eclipse.jface.action.ContributionItem;
37 import org.eclipse.jface.action.GroupMarker;
38 import org.eclipse.jface.action.IContributionItem;
39 import org.eclipse.jface.action.IMenuListener;
40 import org.eclipse.jface.action.IMenuManager;
41 import org.eclipse.jface.action.IToolBarManager;
42 import org.eclipse.jface.action.Separator;
43 import org.eclipse.jface.preference.IPreferenceStore;
44 import org.eclipse.jface.util.Assert;
45 import org.eclipse.jface.viewers.IContentProvider;
46 import org.eclipse.jface.viewers.ITreeContentProvider;
47 import org.eclipse.jface.viewers.StructuredViewer;
48 import org.eclipse.jface.viewers.ViewerFilter;
49 import org.eclipse.jface.window.Window;
50 import org.eclipse.swt.SWT;
51 import org.eclipse.swt.events.SelectionAdapter;
52 import org.eclipse.swt.events.SelectionEvent;
53 import org.eclipse.swt.widgets.Menu;
54 import org.eclipse.swt.widgets.MenuItem;
55 import org.eclipse.ui.IActionBars;
56 import org.eclipse.ui.IMemento;
57 import org.eclipse.ui.IViewPart;
58 import org.eclipse.ui.actions.ActionGroup;
61 * Action group to add the filter action to a view part's tool bar menu.
63 * This class may be instantiated; it is not intended to be subclassed.
68 public class CustomFiltersActionGroup extends ActionGroup {
70 class ShowFilterDialogAction extends Action {
71 ShowFilterDialogAction() {
72 setText(FilterMessages
73 .getString("OpenCustomFiltersDialogAction.text")); //$NON-NLS-1$
74 setImageDescriptor(PHPUiImages.DESC_ELCL_FILTER);
75 setDisabledImageDescriptor(PHPUiImages.DESC_DLCL_FILTER);
84 * Menu contribution item which shows and lets check and uncheck filters.
88 class FilterActionMenuContributionItem extends ContributionItem {
90 private int fItemNumber;
92 private boolean fState;
94 private String fFilterId;
96 private String fFilterName;
98 private CustomFiltersActionGroup fActionGroup;
101 * Constructor for FilterActionMenuContributionItem.
106 * the id of the filter
108 * the name of the filter
110 * the initial state of the filter
112 * the menu item index
114 public FilterActionMenuContributionItem(
115 CustomFiltersActionGroup actionGroup, String filterId,
116 String filterName, boolean state, int itemNumber) {
118 Assert.isNotNull(actionGroup);
119 Assert.isNotNull(filterId);
120 Assert.isNotNull(filterName);
121 fActionGroup = actionGroup;
122 fFilterId = filterId;
123 fFilterName = filterName;
125 fItemNumber = itemNumber;
129 * Overrides method from ContributionItem.
131 public void fill(Menu menu, int index) {
132 MenuItem mi = new MenuItem(menu, SWT.CHECK, index);
133 mi.setText("&" + fItemNumber + " " + fFilterName); //$NON-NLS-1$ //$NON-NLS-2$
135 * XXX: Don't set the image - would look bad because other menu
136 * items don't provide image XXX: Get working set specific image
137 * name from XML - would need to cache icons
139 // mi.setImage(JavaPluginImages.get(JavaPluginImages.IMG_OBJS_JAVA_WORKING_SET));
140 mi.setSelection(fState);
141 mi.addSelectionListener(new SelectionAdapter() {
142 public void widgetSelected(SelectionEvent e) {
144 fActionGroup.setFilter(fFilterId, fState);
150 * @see org.eclipse.jface.action.IContributionItem#isDynamic()
152 public boolean isDynamic() {
157 private static final String TAG_CUSTOM_FILTERS = "customFilters"; //$NON-NLS-1$
159 private static final String TAG_USER_DEFINED_PATTERNS_ENABLED = "userDefinedPatternsEnabled"; //$NON-NLS-1$
161 private static final String TAG_USER_DEFINED_PATTERNS = "userDefinedPatterns"; //$NON-NLS-1$
163 private static final String TAG_XML_DEFINED_FILTERS = "xmlDefinedFilters"; //$NON-NLS-1$
165 private static final String TAG_LRU_FILTERS = "lastRecentlyUsedFilters"; //$NON-NLS-1$
167 private static final String TAG_CHILD = "child"; //$NON-NLS-1$
169 private static final String TAG_PATTERN = "pattern"; //$NON-NLS-1$
171 private static final String TAG_FILTER_ID = "filterId"; //$NON-NLS-1$
173 private static final String TAG_IS_ENABLED = "isEnabled"; //$NON-NLS-1$
175 private static final String SEPARATOR = ","; //$NON-NLS-1$
177 private static final int MAX_FILTER_MENU_ENTRIES = 3;
179 private static final String RECENT_FILTERS_GROUP_NAME = "recentFiltersGroup"; //$NON-NLS-1$
181 private StructuredViewer fViewer;
183 private NamePatternFilter fPatternFilter;
185 private Map fInstalledBuiltInFilters;
187 private Map fEnabledFilterIds;
189 private boolean fUserDefinedPatternsEnabled;
191 private String[] fUserDefinedPatterns;
194 * Recently changed filter Ids stack with oldest on top (i.e. at the end).
198 private Stack fLRUFilterIdsStack;
201 * Handle to menu manager to dynamically update the last recently used
206 private IMenuManager fMenuManager;
209 * The menu listener which dynamically updates the last recently used
214 private IMenuListener fMenuListener;
217 * Filter Ids used in the last view menu invocation.
221 private String[] fFilterIdsUsedInLastViewMenu;
223 private HashMap fFilterDescriptorMap;
225 private String fTargetId;
228 * Creates a new <code>CustomFiltersActionGroup</code>.
231 * the view part that owns this action group
233 * the viewer to be filtered
235 public CustomFiltersActionGroup(IViewPart part, StructuredViewer viewer) {
236 this(part.getViewSite().getId(), viewer);
240 * Creates a new <code>CustomFiltersActionGroup</code>.
243 * the id of this action group's owner
245 * the viewer to be filtered
247 public CustomFiltersActionGroup(String ownerId, StructuredViewer viewer) {
248 Assert.isNotNull(ownerId);
249 Assert.isNotNull(viewer);
253 fLRUFilterIdsStack = new Stack();
255 initializeWithPluginContributions();
256 initializeWithViewDefaults();
262 * Method declared on ActionGroup.
264 public void fillActionBars(IActionBars actionBars) {
265 fillToolBar(actionBars.getToolBarManager());
266 fillViewMenu(actionBars.getMenuManager());
269 public String[] removeFiltersFor(Object parent, Object element,
270 IContentProvider contentProvider) {
271 String[] enabledFilters = getEnabledFilterIds();
272 Set newFilters = new HashSet();
273 for (int i = 0; i < enabledFilters.length; i++) {
274 String filterName = enabledFilters[i];
275 ViewerFilter filter = (ViewerFilter) fInstalledBuiltInFilters
278 newFilters.add(filterName);
279 else if (isSelected(parent, element, contentProvider, filter))
280 newFilters.add(filterName);
282 if (newFilters.size() == enabledFilters.length)
283 return new String[0];
284 return (String[]) newFilters.toArray(new String[newFilters.size()]);
287 public void setFilters(String[] newFilters) {
288 setEnabledFilterIds(newFilters);
289 updateViewerFilters(true);
292 private boolean isSelected(Object parent, Object element,
293 IContentProvider contentProvider, ViewerFilter filter) {
294 if (contentProvider instanceof ITreeContentProvider) {
295 // the element and all its parents have to be selected
296 ITreeContentProvider provider = (ITreeContentProvider) contentProvider;
297 while (element != null && !(element instanceof IJavaModel)) {
298 if (!filter.select(fViewer, parent, element))
300 element = provider.getParent(element);
304 return filter.select(fViewer, parent, element);
308 * Sets the enable state of the given filter.
311 * the id of the filter
315 private void setFilter(String filterId, boolean state) {
316 // Renew filter id in LRU stack
317 fLRUFilterIdsStack.remove(filterId);
318 fLRUFilterIdsStack.add(0, filterId);
320 fEnabledFilterIds.put(filterId, new Boolean(state));
323 updateViewerFilters(true);
326 private String[] getEnabledFilterIds() {
327 Set enabledFilterIds = new HashSet(fEnabledFilterIds.size());
328 Iterator iter = fEnabledFilterIds.entrySet().iterator();
329 while (iter.hasNext()) {
330 Map.Entry entry = (Map.Entry) iter.next();
331 String id = (String) entry.getKey();
332 boolean isEnabled = ((Boolean) entry.getValue()).booleanValue();
334 enabledFilterIds.add(id);
336 return (String[]) enabledFilterIds.toArray(new String[enabledFilterIds
340 private void setEnabledFilterIds(String[] enabledIds) {
341 Iterator iter = fEnabledFilterIds.keySet().iterator();
342 while (iter.hasNext()) {
343 String id = (String) iter.next();
344 fEnabledFilterIds.put(id, Boolean.FALSE);
346 for (int i = 0; i < enabledIds.length; i++)
347 fEnabledFilterIds.put(enabledIds[i], Boolean.TRUE);
350 private void setUserDefinedPatterns(String[] patterns) {
351 fUserDefinedPatterns = patterns;
352 cleanUpPatternDuplicates();
356 * Sets the recently changed filters.
358 * @param changeHistory
362 private void setRecentlyChangedFilters(Stack changeHistory) {
363 Stack oldestFirstStack = new Stack();
365 int length = Math.min(changeHistory.size(), MAX_FILTER_MENU_ENTRIES);
366 for (int i = 0; i < length; i++)
367 oldestFirstStack.push(((FilterDescriptor) changeHistory.pop())
370 length = Math.min(fLRUFilterIdsStack.size(), MAX_FILTER_MENU_ENTRIES
371 - oldestFirstStack.size());
373 for (int i = 0; i < length; i++) {
374 Object filter = fLRUFilterIdsStack.remove(NEWEST);
375 if (!oldestFirstStack.contains(filter))
376 oldestFirstStack.push(filter);
378 fLRUFilterIdsStack = oldestFirstStack;
381 private boolean areUserDefinedPatternsEnabled() {
382 return fUserDefinedPatternsEnabled;
385 private void setUserDefinedPatternsEnabled(boolean state) {
386 fUserDefinedPatternsEnabled = state;
389 private void fillToolBar(IToolBarManager tooBar) {
393 * Fills the given view menu with the entries managed by the group.
398 public void fillViewMenu(IMenuManager viewMenu) {
400 * Don't change the separator group name. Using this name ensures that
401 * other filters get contributed to the same group.
403 viewMenu.add(new Separator("filters")); //$NON-NLS-1$
404 viewMenu.add(new GroupMarker(RECENT_FILTERS_GROUP_NAME));
405 viewMenu.add(new ShowFilterDialogAction());
407 fMenuManager = viewMenu;
408 fMenuListener = new IMenuListener() {
409 public void menuAboutToShow(IMenuManager manager) {
410 removePreviousLRUFilterActions(manager);
411 addLRUFilterActions(manager);
414 fMenuManager.addMenuListener(fMenuListener);
417 private void removePreviousLRUFilterActions(IMenuManager mm) {
418 if (fFilterIdsUsedInLastViewMenu == null)
421 for (int i = 0; i < fFilterIdsUsedInLastViewMenu.length; i++)
422 mm.remove(fFilterIdsUsedInLastViewMenu[i]);
425 private void addLRUFilterActions(IMenuManager mm) {
426 if (fLRUFilterIdsStack.isEmpty()) {
427 fFilterIdsUsedInLastViewMenu = null;
431 SortedSet sortedFilters = new TreeSet(fLRUFilterIdsStack);
432 String[] recentlyChangedFilterIds = (String[]) sortedFilters
433 .toArray(new String[sortedFilters.size()]);
435 fFilterIdsUsedInLastViewMenu = new String[recentlyChangedFilterIds.length];
436 for (int i = 0; i < recentlyChangedFilterIds.length; i++) {
437 String id = recentlyChangedFilterIds[i];
438 fFilterIdsUsedInLastViewMenu[i] = id;
439 boolean state = fEnabledFilterIds.containsKey(id)
440 && ((Boolean) fEnabledFilterIds.get(id)).booleanValue();
441 FilterDescriptor filterDesc = (FilterDescriptor) fFilterDescriptorMap
443 if (filterDesc != null) {
444 IContributionItem item = new FilterActionMenuContributionItem(
445 this, id, filterDesc.getName(), state, i + 1);
446 mm.insertBefore(RECENT_FILTERS_GROUP_NAME, item);
452 * Method declared on ActionGroup.
454 public void dispose() {
455 if (fMenuManager != null)
456 fMenuManager.removeMenuListener(fMenuListener);
460 private void initializeWithPluginContributions() {
461 fUserDefinedPatterns = new String[0];
462 fUserDefinedPatternsEnabled = false;
464 FilterDescriptor[] filterDescs = FilterDescriptor
465 .getFilterDescriptors(fTargetId);
466 fFilterDescriptorMap = new HashMap(filterDescs.length);
467 fEnabledFilterIds = new HashMap(filterDescs.length);
468 for (int i = 0; i < filterDescs.length; i++) {
469 String id = filterDescs[i].getId();
470 Boolean isEnabled = new Boolean(filterDescs[i].isEnabled());
471 if (fEnabledFilterIds.containsKey(id))
473 .logErrorMessage("WARNING: Duplicate id for extension-point \"net.sourceforge.phpdt.ui.javaElementFilters\""); //$NON-NLS-1$
474 fEnabledFilterIds.put(id, isEnabled);
475 fFilterDescriptorMap.put(id, filterDescs[i]);
479 // ---------- viewer filter handling ----------
481 private void installFilters() {
482 fInstalledBuiltInFilters = new HashMap(fEnabledFilterIds.size());
483 fPatternFilter = new NamePatternFilter();
484 fPatternFilter.setPatterns(getUserAndBuiltInPatterns());
485 fViewer.addFilter(fPatternFilter);
486 updateBuiltInFilters();
489 private void updateViewerFilters(boolean refresh) {
490 String[] patterns = getUserAndBuiltInPatterns();
491 fPatternFilter.setPatterns(patterns);
492 fViewer.getControl().setRedraw(false);
493 updateBuiltInFilters();
496 fViewer.getControl().setRedraw(true);
499 private void updateBuiltInFilters() {
500 Set installedFilters = fInstalledBuiltInFilters.keySet();
501 Set filtersToAdd = new HashSet(fEnabledFilterIds.size());
502 Set filtersToRemove = new HashSet(fEnabledFilterIds.size());
503 Iterator iter = fEnabledFilterIds.entrySet().iterator();
504 while (iter.hasNext()) {
505 Map.Entry entry = (Map.Entry) iter.next();
506 String id = (String) entry.getKey();
507 boolean isEnabled = ((Boolean) entry.getValue()).booleanValue();
508 if (isEnabled && !installedFilters.contains(id))
509 filtersToAdd.add(id);
510 else if (!isEnabled && installedFilters.contains(id))
511 filtersToRemove.add(id);
514 // Install the filters
515 FilterDescriptor[] filterDescs = FilterDescriptor
516 .getFilterDescriptors(fTargetId);
517 for (int i = 0; i < filterDescs.length; i++) {
518 String id = filterDescs[i].getId();
519 // just to double check - id should denote a custom filter anyway
520 boolean isCustomFilter = filterDescs[i].isCustomFilter();
521 if (isCustomFilter) {
522 if (filtersToAdd.contains(id)) {
523 ViewerFilter filter = filterDescs[i].createViewerFilter();
524 if (filter != null) {
525 fViewer.addFilter(filter);
526 fInstalledBuiltInFilters.put(id, filter);
529 if (filtersToRemove.contains(id)) {
531 .removeFilter((ViewerFilter) fInstalledBuiltInFilters
533 fInstalledBuiltInFilters.remove(id);
539 private String[] getUserAndBuiltInPatterns() {
540 List patterns = new ArrayList(fUserDefinedPatterns.length);
541 if (areUserDefinedPatternsEnabled())
542 patterns.addAll(Arrays.asList(fUserDefinedPatterns));
543 FilterDescriptor[] filterDescs = FilterDescriptor
544 .getFilterDescriptors(fTargetId);
545 for (int i = 0; i < filterDescs.length; i++) {
546 String id = filterDescs[i].getId();
547 boolean isPatternFilter = filterDescs[i].isPatternFilter();
548 Object isEnabled = fEnabledFilterIds.get(id);
549 if (isEnabled != null && isPatternFilter
550 && ((Boolean) isEnabled).booleanValue())
551 patterns.add(filterDescs[i].getPattern());
553 return (String[]) patterns.toArray(new String[patterns.size()]);
556 // ---------- view kind/defaults persistency ----------
558 private void initializeWithViewDefaults() {
559 // get default values for view
560 IPreferenceStore store = WebUI.getDefault()
561 .getPreferenceStore();
563 // XXX: can be removed once bug 22533 is fixed.
564 if (!store.contains(getPreferenceKey("TAG_DUMMY_TO_TEST_EXISTENCE")))//$NON-NLS-1$
567 // XXX: Uncomment once bug 22533 is fixed.
569 // (!store.contains(getPreferenceKey(TAG_USER_DEFINED_PATTERNS_ENABLED)))
572 fUserDefinedPatternsEnabled = store
573 .getBoolean(getPreferenceKey(TAG_USER_DEFINED_PATTERNS_ENABLED));
574 setUserDefinedPatterns(CustomFiltersDialog.convertFromString(store
575 .getString(getPreferenceKey(TAG_USER_DEFINED_PATTERNS)),
578 Iterator iter = fEnabledFilterIds.keySet().iterator();
579 while (iter.hasNext()) {
580 String id = (String) iter.next();
581 Boolean isEnabled = new Boolean(store.getBoolean(id));
582 fEnabledFilterIds.put(id, isEnabled);
585 fLRUFilterIdsStack.clear();
586 String lruFilterIds = store.getString(TAG_LRU_FILTERS);
587 StringTokenizer tokenizer = new StringTokenizer(lruFilterIds, SEPARATOR);
588 while (tokenizer.hasMoreTokens()) {
589 String id = tokenizer.nextToken();
590 if (fFilterDescriptorMap.containsKey(id)
591 && !fLRUFilterIdsStack.contains(id))
592 fLRUFilterIdsStack.push(id);
596 private void storeViewDefaults() {
597 // get default values for view
598 IPreferenceStore store = WebUI.getDefault()
599 .getPreferenceStore();
601 // XXX: can be removed once bug 22533 is fixed.
604 getPreferenceKey("TAG_DUMMY_TO_TEST_EXISTENCE"), "storedViewPreferences");//$NON-NLS-1$//$NON-NLS-2$
606 store.setValue(getPreferenceKey(TAG_USER_DEFINED_PATTERNS_ENABLED),
607 fUserDefinedPatternsEnabled);
608 store.setValue(getPreferenceKey(TAG_USER_DEFINED_PATTERNS),
609 CustomFiltersDialog.convertToString(fUserDefinedPatterns,
612 Iterator iter = fEnabledFilterIds.entrySet().iterator();
613 while (iter.hasNext()) {
614 Map.Entry entry = (Map.Entry) iter.next();
615 String id = (String) entry.getKey();
616 boolean isEnabled = ((Boolean) entry.getValue()).booleanValue();
617 store.setValue(id, isEnabled);
620 StringBuffer buf = new StringBuffer(fLRUFilterIdsStack.size() * 20);
621 iter = fLRUFilterIdsStack.iterator();
622 while (iter.hasNext()) {
623 buf.append((String) iter.next());
624 buf.append(SEPARATOR);
626 store.setValue(TAG_LRU_FILTERS, buf.toString());
629 private String getPreferenceKey(String tag) {
630 return "CustomFiltersActionGroup." + fTargetId + '.' + tag; //$NON-NLS-1$
633 // ---------- view instance persistency ----------
636 * Saves the state of the custom filters in a memento.
639 * the memento into which the state is saved
641 public void saveState(IMemento memento) {
642 IMemento customFilters = memento.createChild(TAG_CUSTOM_FILTERS);
643 customFilters.putString(TAG_USER_DEFINED_PATTERNS_ENABLED, new Boolean(
644 fUserDefinedPatternsEnabled).toString());
645 saveUserDefinedPatterns(customFilters);
646 saveXmlDefinedFilters(customFilters);
647 saveLRUFilters(customFilters);
650 private void saveXmlDefinedFilters(IMemento memento) {
651 if (fEnabledFilterIds != null && !fEnabledFilterIds.isEmpty()) {
652 IMemento xmlDefinedFilters = memento
653 .createChild(TAG_XML_DEFINED_FILTERS);
654 Iterator iter = fEnabledFilterIds.entrySet().iterator();
655 while (iter.hasNext()) {
656 Map.Entry entry = (Map.Entry) iter.next();
657 String id = (String) entry.getKey();
658 Boolean isEnabled = (Boolean) entry.getValue();
659 IMemento child = xmlDefinedFilters.createChild(TAG_CHILD);
660 child.putString(TAG_FILTER_ID, id);
661 child.putString(TAG_IS_ENABLED, isEnabled.toString());
667 * Stores the last recently used filter Ids into the given memento
670 * the memento into which to store the LRU filter Ids
673 private void saveLRUFilters(IMemento memento) {
674 if (fLRUFilterIdsStack != null && !fLRUFilterIdsStack.isEmpty()) {
675 IMemento lruFilters = memento.createChild(TAG_LRU_FILTERS);
676 Iterator iter = fLRUFilterIdsStack.iterator();
677 while (iter.hasNext()) {
678 String id = (String) iter.next();
679 IMemento child = lruFilters.createChild(TAG_CHILD);
680 child.putString(TAG_FILTER_ID, id);
685 private void saveUserDefinedPatterns(IMemento memento) {
686 if (fUserDefinedPatterns != null && fUserDefinedPatterns.length > 0) {
687 IMemento userDefinedPatterns = memento
688 .createChild(TAG_USER_DEFINED_PATTERNS);
689 for (int i = 0; i < fUserDefinedPatterns.length; i++) {
690 IMemento child = userDefinedPatterns.createChild(TAG_CHILD);
691 child.putString(TAG_PATTERN, fUserDefinedPatterns[i]);
697 * Restores the state of the filter actions from a memento.
699 * Note: This method does not refresh the viewer.
703 * the memento from which the state is restored
705 public void restoreState(IMemento memento) {
708 IMemento customFilters = memento.getChild(TAG_CUSTOM_FILTERS);
709 if (customFilters == null)
711 String userDefinedPatternsEnabled = customFilters
712 .getString(TAG_USER_DEFINED_PATTERNS_ENABLED);
713 if (userDefinedPatternsEnabled == null)
716 fUserDefinedPatternsEnabled = Boolean.valueOf(
717 userDefinedPatternsEnabled).booleanValue();
718 restoreUserDefinedPatterns(customFilters);
719 restoreXmlDefinedFilters(customFilters);
720 restoreLRUFilters(customFilters);
722 updateViewerFilters(false);
725 private void restoreUserDefinedPatterns(IMemento memento) {
726 IMemento userDefinedPatterns = memento
727 .getChild(TAG_USER_DEFINED_PATTERNS);
728 if (userDefinedPatterns != null) {
729 IMemento children[] = userDefinedPatterns.getChildren(TAG_CHILD);
730 String[] patterns = new String[children.length];
731 for (int i = 0; i < children.length; i++)
732 patterns[i] = children[i].getString(TAG_PATTERN);
734 setUserDefinedPatterns(patterns);
736 setUserDefinedPatterns(new String[0]);
739 private void restoreXmlDefinedFilters(IMemento memento) {
740 IMemento xmlDefinedFilters = memento.getChild(TAG_XML_DEFINED_FILTERS);
741 if (xmlDefinedFilters != null) {
742 IMemento[] children = xmlDefinedFilters.getChildren(TAG_CHILD);
743 for (int i = 0; i < children.length; i++) {
744 String id = children[i].getString(TAG_FILTER_ID);
745 Boolean isEnabled = new Boolean(children[i]
746 .getString(TAG_IS_ENABLED));
747 fEnabledFilterIds.put(id, isEnabled);
752 private void restoreLRUFilters(IMemento memento) {
753 IMemento lruFilters = memento.getChild(TAG_LRU_FILTERS);
754 fLRUFilterIdsStack.clear();
755 if (lruFilters != null) {
756 IMemento[] children = lruFilters.getChildren(TAG_CHILD);
757 for (int i = 0; i < children.length; i++) {
758 String id = children[i].getString(TAG_FILTER_ID);
759 if (fFilterDescriptorMap.containsKey(id)
760 && !fLRUFilterIdsStack.contains(id))
761 fLRUFilterIdsStack.push(id);
766 private void cleanUpPatternDuplicates() {
767 if (!areUserDefinedPatternsEnabled())
769 List userDefinedPatterns = new ArrayList(Arrays
770 .asList(fUserDefinedPatterns));
771 FilterDescriptor[] filters = FilterDescriptor
772 .getFilterDescriptors(fTargetId);
774 for (int i = 0; i < filters.length; i++) {
775 if (filters[i].isPatternFilter()) {
776 String pattern = filters[i].getPattern();
777 if (userDefinedPatterns.contains(pattern)) {
778 fEnabledFilterIds.put(filters[i].getId(), Boolean.TRUE);
779 boolean hasMore = true;
781 hasMore = userDefinedPatterns.remove(pattern);
785 fUserDefinedPatterns = (String[]) userDefinedPatterns
786 .toArray(new String[userDefinedPatterns.size()]);
787 setUserDefinedPatternsEnabled(fUserDefinedPatternsEnabled
788 && fUserDefinedPatterns.length > 0);
791 // ---------- dialog related code ----------
793 private void openDialog() {
794 CustomFiltersDialog dialog = new CustomFiltersDialog(fViewer
795 .getControl().getShell(), fTargetId,
796 areUserDefinedPatternsEnabled(), fUserDefinedPatterns,
797 getEnabledFilterIds());
799 if (dialog.open() == Window.OK) {
800 setEnabledFilterIds(dialog.getEnabledFilterIds());
801 setUserDefinedPatternsEnabled(dialog
802 .areUserDefinedPatternsEnabled());
803 setUserDefinedPatterns(dialog.getUserDefinedPatterns());
804 setRecentlyChangedFilters(dialog.getFilterDescriptorChangeHistory());
808 updateViewerFilters(true);