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.Collection;
013: import java.util.HashMap;
014: import java.util.HashSet;
015: import java.util.Iterator;
016:
017: import org.eclipse.core.resources.IMarker;
018: import org.eclipse.core.runtime.CoreException;
019: import org.eclipse.ui.IMemento;
020: import org.eclipse.ui.internal.provisional.views.markers.api.MarkerFieldFilter;
021: import org.eclipse.ui.views.markers.internal.MarkerType;
022: import org.eclipse.ui.views.markers.internal.MarkerTypesModel;
023:
024: /**
025: * MarkerTypeFieldFilter is the field filter for filtering on types.
026: *
027: * @since 3.4
028: *
029: */
030: public class MarkerTypeFieldFilter extends MarkerFieldFilter {
031:
032: private static final String TAG_TYPES_DELIMITER = ":"; //$NON-NLS-1$
033: private static final String TAG_SELECTED_TYPES = "selectedTypes"; //$NON-NLS-1$
034: Collection selectedTypes;
035: private HashMap allTypes;
036:
037: /*
038: * (non-Javadoc)
039: *
040: * @see org.eclipse.ui.internal.provisional.views.markers.MarkerFieldFilter#select(org.eclipse.core.resources.IMarker)
041: */
042: public boolean select(IMarker marker) {
043:
044: try {
045: return selectedTypes.contains(MarkerTypesModel
046: .getInstance().getType(marker.getType()));
047: } catch (CoreException e) {
048: return false;
049: }
050: }
051:
052: /**
053: * Set the selected types in the receiver.
054: *
055: * @param markerTypes
056: * Collection of MarkerType
057: */
058: public void setSelectedTypes(Collection markerTypes) {
059: selectedTypes = markerTypes;
060:
061: }
062:
063: /**
064: * Return the selectedTypes.
065: *
066: * @return Collection of MarkerType
067: */
068: public Collection getSelectedTypes() {
069: return selectedTypes;
070: }
071:
072: /*
073: * (non-Javadoc)
074: *
075: * @see org.eclipse.ui.internal.provisional.views.markers.MarkerFieldFilter#saveSettings(org.eclipse.ui.IMemento)
076: */
077: public void saveSettings(IMemento memento) {
078:
079: Iterator selected = selectedTypes.iterator();
080:
081: StringBuffer settings = new StringBuffer();
082: while (selected.hasNext()) {
083: MarkerType markerType = (MarkerType) selected.next();
084: settings.append(markerType.getId());
085: settings.append(TAG_TYPES_DELIMITER);
086: }
087:
088: memento.putString(TAG_SELECTED_TYPES, settings.toString());
089: }
090:
091: /*
092: * (non-Javadoc)
093: *
094: * @see org.eclipse.ui.internal.provisional.views.markers.MarkerFieldFilter#loadSettings(org.eclipse.ui.IMemento)
095: */
096: public void loadSettings(IMemento memento) {
097:
098: String types = memento.getString(TAG_SELECTED_TYPES);
099: selectedTypes.clear();
100:
101: int start = 0;
102: int nextSpace = types.indexOf(TAG_TYPES_DELIMITER, 0);
103: while (nextSpace > 0) {
104: String typeId = types.substring(start, nextSpace);
105: start = nextSpace + 1;
106: nextSpace = types.indexOf(TAG_TYPES_DELIMITER, start);
107:
108: if (allTypes.containsKey(typeId))
109: selectedTypes.add(allTypes.get(typeId));
110: }
111:
112: }
113:
114: /**
115: * Set the set of all types to markerTypes. Select all of them by default.
116: *
117: * @param markerTypes
118: */
119: public void setAndSelectAllTypes(Collection markerTypes) {
120: allTypes = new HashMap();
121: selectedTypes = new HashSet(markerTypes);
122: Iterator allIterator = markerTypes.iterator();
123: while (allIterator.hasNext()) {
124: MarkerType next = (MarkerType) allIterator.next();
125: allTypes.put(next.getId(), next);
126: }
127:
128: }
129:
130: /*
131: * (non-Javadoc)
132: *
133: * @see org.eclipse.ui.internal.provisional.views.markers.MarkerFieldFilter#populateWorkingCopy(org.eclipse.ui.internal.provisional.views.markers.MarkerFieldFilter)
134: */
135: public void populateWorkingCopy(MarkerFieldFilter copy) {
136: super .populateWorkingCopy(copy);
137: ((MarkerTypeFieldFilter) copy).selectedTypes = new HashSet(
138: selectedTypes);
139: }
140: }
|