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  * Map to filter a scope either exclusively on inclusively
00019  * @see FilterAttribute
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      * retrieve a list of filters
00049      * 
00050      * @return
00051      */
00052     public Object[] getEntrySet() {
00053         checkData();
00054         return data.entrySet().toArray();
00055     }
00056     
00057     @Override
00058     /*
00059      * (non-Javadoc)
00060      * @see edu.rice.cs.hpc.common.util.AliasMap#put(java.lang.Object, java.lang.Object)
00061      */
00062     public void put(String filter, FilterAttribute state)
00063     {
00064         super.put(filter, state);
00065         save();
00066     }
00067 
00068     @Override
00069     /*
00070      * (non-Javadoc)
00071      * @see edu.rice.cs.hpc.data.filter.IFilterData#select(java.lang.String)
00072      */
00073     public boolean select(String element)
00074     {
00075         return getFilterAttribute(element) != null;
00076     }
00077     
00078     /*****
00079      * Check whether a string can be filtered or not.
00080      * If the string match a filter, returns the filter attribute
00081      * Otherwise returns null;
00082      * 
00083      * <p>See {@link edu.rice.cs.hpc.data.filter.FilterAttribute}
00084      * </p>
00085      * 
00086      * @param element
00087      * @return {@link FilterAttribute} 
00088      * if the element matches to a filter pattern. Return null otherwise.
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         // this is a bad bad bad practice.
00101         // the complexity is O(NM) where N is the number of nodes and M is the number of patterns
00102         // we know this is not quick, but assuming M is very small, the order should be linear
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      * rename the filter
00121      * 
00122      * @param oldKey : old filter
00123      * @param newKey : new filter
00124      * 
00125      * @return true if the update successful, false otherwise
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      * (non-Javadoc)
00143      * @see edu.rice.cs.hpc.data.filter.IFilterData#isFilterEnabled()
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 }

Generated on 5 May 2015 for HPCVIEWER by  doxygen 1.6.1