FilterMap.java
Go to the documentation of this file.00001 package edu.rice.cs.hpc.filter.service;
00002
00003 import java.util.Map.Entry;
00004
00005 import org.eclipse.core.runtime.IPath;
00006 import org.eclipse.core.runtime.Platform;
00007 import org.eclipse.core.runtime.Assert;
00008 import org.eclipse.ui.IWorkbenchWindow;
00009 import org.eclipse.ui.services.ISourceProviderService;
00010
00011 import edu.rice.cs.hpc.common.ui.Util;
00012 import edu.rice.cs.hpc.common.util.AliasMap;
00013 import edu.rice.cs.hpc.data.filter.FilterAttribute;
00014 import edu.rice.cs.hpc.data.filter.IFilterData;
00015
00016
00017
00018
00019
00020
00021
00022 public class FilterMap extends AliasMap<String, FilterAttribute>
00023 implements IFilterData
00024 {
00025
00026 static private final String FILE_NAME = "filter.map";
00027 static private final FilterMap filterMap = new FilterMap();
00028
00029 private FilterStateProvider filterStateProvider = null;
00030
00031 public static FilterMap getInstance()
00032 {
00033 return filterMap;
00034 }
00035
00036 @Override
00037 public String getFilename() {
00038
00039 IPath path = Platform.getLocation().makeAbsolute();
00040 return path.append(FILE_NAME).makeAbsolute().toString();
00041 }
00042
00043 @Override
00044 public void initDefault() {
00045 }
00046
00047
00048
00049
00050
00051
00052 public Object[] getEntrySet() {
00053 checkData();
00054 return data.entrySet().toArray();
00055 }
00056
00057 @Override
00058
00059
00060
00061
00062 public void put(String filter, FilterAttribute state)
00063 {
00064 super.put(filter, state);
00065 save();
00066 }
00067
00068 @Override
00069
00070
00071
00072
00073 public boolean select(String element)
00074 {
00075 return getFilterAttribute(element) != null;
00076 }
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090 @Override
00091 public FilterAttribute getFilterAttribute(String element)
00092 {
00093 if (!isFilterEnabled())
00094 {
00095 return null;
00096 }
00097 Object []entries = getEntrySet();
00098
00099
00100
00101
00102
00103
00104 for (Object entry : entries)
00105 {
00106 Entry<String, FilterAttribute> pattern = (Entry<String, FilterAttribute>) entry;
00107 FilterAttribute toFilter = pattern.getValue();
00108 if (toFilter.enable)
00109 {
00110 final String key = pattern.getKey().replace("*", ".*").replace("?", ".?");
00111 if (element.matches(key)) {
00112 return toFilter;
00113 }
00114 }
00115 }
00116 return null;
00117 }
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127 public boolean update(String oldKey, String newKey)
00128 {
00129 FilterAttribute val = get(oldKey);
00130 if (val != null)
00131 {
00132 remove(oldKey);
00133 put(newKey, val);
00134 save();
00135 return true;
00136 }
00137 return false;
00138 }
00139
00140 @Override
00141
00142
00143
00144
00145 public boolean isFilterEnabled()
00146 {
00147 if (filterStateProvider == null)
00148 {
00149 IWorkbenchWindow window = Util.getActiveWindow();
00150 Assert.isNotNull(window);
00151 ISourceProviderService service = (ISourceProviderService) window.getService(ISourceProviderService.class);
00152 filterStateProvider = (FilterStateProvider) service.getSourceProvider(FilterStateProvider.FILTER_REFRESH_PROVIDER);
00153 }
00154
00155 return filterStateProvider.isEnabled();
00156 }
00157 }