FilterStateProvider.java

Go to the documentation of this file.
00001 package edu.rice.cs.hpc.filter.service;
00002 
00003 import java.util.HashMap;
00004 import java.util.Map;
00005 import java.util.Properties;
00006 
00007 import org.eclipse.core.commands.Command;
00008 import org.eclipse.core.commands.State;
00009 import org.eclipse.core.runtime.Assert;
00010 import org.eclipse.jface.viewers.ISelection;
00011 import org.eclipse.jface.viewers.StructuredSelection;
00012 import org.eclipse.ui.AbstractSourceProvider;
00013 import org.eclipse.ui.ISources;
00014 import org.eclipse.ui.IWorkbenchWindow;
00015 import org.eclipse.ui.PlatformUI;
00016 import org.eclipse.ui.commands.ICommandService;
00017 
00018 import edu.rice.cs.hpc.filter.action.FilterApply;
00019 
00020 /**************************************************************
00021  * 
00022  * Class to manage filter state, whether it needs to be refreshed
00023  * or there's a filter selection in the view
00024  *
00025  **************************************************************/
00026 public class FilterStateProvider extends AbstractSourceProvider 
00027 {
00028     final static public String FILTER_STATE_PROVIDER = "edu.rice.cs.hpc.filter.selection";
00029     final static public String FILTER_REFRESH_PROVIDER = "edu.rice.cs.hpc.filter.update";
00030     final static public String FILTER_ENABLE_PROVIDER = "edu.rice.cs.hpc.filter.enable";
00031     
00032     final static public String TOGGLE_COMMAND = "org.eclipse.ui.commands.toggleState";
00033     final static public String SELECTED_STATE = "SELECTED";
00034     
00035     private boolean isSelected = false;
00036     private Object []elements;
00037     private Boolean enable = null;
00038     
00039     public FilterStateProvider() {
00040         // TODO Auto-generated constructor stub
00041     }
00042 
00043     @Override
00044     public void dispose() {
00045         // TODO Auto-generated method stub
00046 
00047     }
00048 
00049     @Override
00050     public Map<String, Object> getCurrentState() {
00051         Map<String, Object> map = new HashMap<String, Object>(1);
00052         map.put(FILTER_STATE_PROVIDER, getSelectedValue());
00053         map.put(FILTER_REFRESH_PROVIDER, FilterMap.getInstance());
00054         
00055         final String filterVal = System.getenv("FILTER");
00056         map.put(FILTER_ENABLE_PROVIDER, filterVal);
00057         
00058         return map;
00059     }
00060 
00061     @Override
00062     public String[] getProvidedSourceNames() {
00063         return new String[] {FILTER_STATE_PROVIDER, FILTER_REFRESH_PROVIDER, FILTER_ENABLE_PROVIDER};
00064     }
00065 
00066     
00067     public void setSelection(ISelection selection)
00068     {
00069         isSelected = selection != null && !selection.isEmpty();
00070         if (isSelected) {
00071             final StructuredSelection elements = (StructuredSelection) selection;
00072             this.elements = elements.toArray();
00073         } else {
00074             this.elements = null;
00075         }
00076         fireSourceChanged(ISources.WORKBENCH, FILTER_STATE_PROVIDER, getSelectedValue());
00077     }
00078     
00079     public Object[] getSelections()
00080     {
00081         return elements;
00082     }
00083     
00084     public boolean isEnabled()
00085     {
00086         if (enable == null) 
00087         {
00088             IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
00089             Assert.isNotNull(window);
00090             
00091             ICommandService service = (ICommandService) window.getService(ICommandService.class);
00092             Command command = service.getCommand(FilterApply.ID);
00093             State   state   = command.getState(TOGGLE_COMMAND);
00094             enable = (Boolean) state.getValue();
00095 
00096         }
00097         return enable.booleanValue();
00098     }
00099     /*****
00100      * Notify the views that we may turn on/off the filter
00101      * 
00102      * @param enableFilter
00103      */
00104     public void refresh(Boolean enableFilter)
00105     {
00106         this.enable = enableFilter;
00107         Properties properties = new Properties();
00108         properties.put(FILTER_REFRESH_PROVIDER, enableFilter);
00109 
00110         fireSourceChanged(ISources.WORKBENCH, FILTER_REFRESH_PROVIDER, enableFilter);
00111     }
00112     
00113     /*****
00114      * refresh the table as the filter pattern may change
00115      * Usually called by FilterAdd and FilterDelete 
00116      */
00117     public void refresh()
00118     {
00119         refresh(isEnabled());
00120     }
00121     
00122     private String getSelectedValue()
00123     {
00124         return (isSelected ? SELECTED_STATE : "");
00125     }
00126 }

Generated on 5 May 2015 for HPCVIEWER by  doxygen 1.6.1