FilterView.java
Go to the documentation of this file.00001 package edu.rice.cs.hpc.filter.view;
00002
00003 import java.util.Map;
00004 import java.util.Map.Entry;
00005
00006 import org.eclipse.jface.dialogs.InputDialog;
00007 import org.eclipse.jface.layout.GridDataFactory;
00008 import org.eclipse.jface.layout.GridLayoutFactory;
00009 import org.eclipse.jface.viewers.CheckStateChangedEvent;
00010 import org.eclipse.jface.viewers.CheckboxTableViewer;
00011 import org.eclipse.jface.viewers.DoubleClickEvent;
00012 import org.eclipse.jface.viewers.ICheckStateListener;
00013 import org.eclipse.jface.viewers.IDoubleClickListener;
00014 import org.eclipse.jface.viewers.ISelection;
00015 import org.eclipse.jface.viewers.ISelectionChangedListener;
00016 import org.eclipse.jface.viewers.SelectionChangedEvent;
00017 import org.eclipse.jface.viewers.StructuredSelection;
00018 import org.eclipse.jface.window.Window;
00019 import org.eclipse.swt.widgets.Composite;
00020 import org.eclipse.swt.widgets.Shell;
00021 import org.eclipse.swt.widgets.Table;
00022 import org.eclipse.swt.widgets.TableItem;
00023 import org.eclipse.ui.ISourceProviderListener;
00024 import org.eclipse.ui.IWorkbenchWindow;
00025 import org.eclipse.ui.part.ViewPart;
00026 import org.eclipse.ui.services.ISourceProviderService;
00027
00028 import edu.rice.cs.hpc.common.ui.Util;
00029 import edu.rice.cs.hpc.data.filter.FilterAttribute;
00030 import edu.rice.cs.hpc.filter.pattern.PatternValidator;
00031 import edu.rice.cs.hpc.filter.service.FilterMap;
00032 import edu.rice.cs.hpc.filter.service.FilterStateProvider;
00033 import edu.rice.cs.hpc.filter.view.FilterTableViewerFactory;
00034 import edu.rice.cs.hpc.filter.view.IFilterView;
00035
00036
00037
00038
00039
00040
00041 public class FilterView extends ViewPart implements IFilterView
00042 {
00043 final static public String ID = "edu.rice.cs.hpc.filter.view.FilterView";
00044 private CheckboxTableViewer tableViewer;
00045 private ISourceProviderListener sourceProviderListener;
00046 private FilterStateProvider serviceProvider;
00047 private ICheckStateListener checkStateListener;
00048 private SelectionChangedListener selectionChangedListener;
00049
00050 public FilterView() {
00051 new FilterMap();
00052 }
00053
00054 @Override
00055 public void createPartControl(Composite parent) {
00056
00057
00058
00059
00060 tableViewer = FilterTableViewerFactory.getTable(parent);
00061
00062 final Table table = tableViewer.getTable();
00063 table.setLinesVisible(true);
00064
00065 GridDataFactory.fillDefaults().grab(true, true).applyTo(table);
00066 GridLayoutFactory.fillDefaults().numColumns(1).applyTo(table);
00067
00068 table.setVisible(true);
00069 table.pack();
00070
00071
00072
00073
00074 final ISourceProviderService service = (ISourceProviderService)getSite().getWorkbenchWindow().
00075 getService(ISourceProviderService.class);
00076 serviceProvider = (FilterStateProvider) service.getSourceProvider(FilterStateProvider.FILTER_REFRESH_PROVIDER);
00077
00078
00079
00080
00081
00082 checkStateListener = new ICheckStateListener() {
00083 @Override
00084 public void checkStateChanged(CheckStateChangedEvent event) {
00085 final Entry<String, FilterAttribute> element = (Entry<String, FilterAttribute>) event.getElement();
00086 final String key = element.getKey();
00087 FilterMap map = FilterMap.getInstance();
00088
00089
00090 FilterAttribute attribute = element.getValue();
00091 attribute.enable = event.getChecked();
00092
00093
00094 map.put(key, attribute);
00095 map.save();
00096
00097
00098 serviceProvider.refresh();
00099 }
00100 };
00101 tableViewer.addCheckStateListener( checkStateListener );
00102
00103
00104
00105
00106 selectionChangedListener = new SelectionChangedListener();
00107 tableViewer.addSelectionChangedListener(selectionChangedListener);
00108
00109
00110
00111
00112 tableViewer.addDoubleClickListener( new IDoubleClickListener() {
00113
00114 @Override
00115 public void doubleClick(DoubleClickEvent event) {
00116 ISelection selection = event.getSelection();
00117 if (selection != null && !selection.isEmpty())
00118 {
00119 final Shell shell = Util.getActiveShell();
00120 final StructuredSelection select = (StructuredSelection) selection;
00121 final Entry<String, FilterAttribute> item= (Entry<String, FilterAttribute>) select.getFirstElement();
00122
00123 InputDialog dialog = new InputDialog(shell, "Rename a filter",
00124 "Use a glob pattern to define a filter. For instance, a MPI* will filter all MPI routines",
00125 item.getKey(), new PatternValidator());
00126 if (dialog.open() == Window.OK)
00127 {
00128 final FilterMap filterMap = FilterMap.getInstance();
00129 filterMap.update(item.getKey(), dialog.getValue());
00130
00131
00132 serviceProvider.refresh();
00133 }
00134 }
00135 }
00136 });
00137
00138
00139
00140
00141 sourceProviderListener = new ISourceProviderListener() {
00142
00143 @Override
00144 public void sourceChanged(int sourcePriority, String sourceName,
00145 Object sourceValue)
00146 {
00147
00148 if (sourceName.equals(FilterStateProvider.FILTER_REFRESH_PROVIDER)) {
00149
00150 FilterMap map = FilterMap.getInstance();
00151 tableViewer.setInput(map.getEntrySet());
00152 tableViewer.refresh();
00153 }
00154 }
00155
00156 @Override
00157 public void sourceChanged(int sourcePriority, Map sourceValuesByName) {}
00158 };
00159 serviceProvider.addSourceProviderListener( sourceProviderListener );
00160 }
00161
00162 @Override
00163 public void setFocus() {
00164 tableViewer.getTable().setFocus();
00165 }
00166
00167
00168 @Override
00169 public void refresh() {
00170 FilterMap map = FilterMap.getInstance();
00171 tableViewer.setInput(map.getEntrySet());
00172 tableViewer.refresh();
00173 }
00174
00175 @Override
00176 public Object[] getSelectedElements() {
00177 final Table table = tableViewer.getTable();
00178 TableItem []items = table.getSelection();
00179
00180 if (items == null || items.length == 0)
00181 return null;
00182
00183 Entry<String, Boolean> []elements = new Entry [items.length];
00184 int i = 0;
00185 for (TableItem item : items)
00186 {
00187 elements[i] = (Entry<String, Boolean>) item.getData();
00188 i++;
00189 }
00190 return elements;
00191 }
00192
00193 @Override
00194
00195
00196
00197
00198 public void dispose()
00199 {
00200 tableViewer.removeCheckStateListener(checkStateListener);
00201 tableViewer.removeSelectionChangedListener(selectionChangedListener);
00202
00203 serviceProvider.removeSourceProviderListener(sourceProviderListener);
00204 serviceProvider.setSelection(null);
00205
00206 super.dispose();
00207 }
00208
00209 private static class SelectionChangedListener implements ISelectionChangedListener
00210 {
00211 private final FilterStateProvider provider;
00212
00213 public SelectionChangedListener()
00214 {
00215 IWorkbenchWindow winObj = Util.getActiveWindow();
00216 ISourceProviderService sourceProviderService = (ISourceProviderService) winObj.getService(
00217 ISourceProviderService.class);
00218 provider = (FilterStateProvider) sourceProviderService.getSourceProvider(FilterStateProvider.FILTER_STATE_PROVIDER);
00219 }
00220
00221 @Override
00222 public void selectionChanged(SelectionChangedEvent event) {
00223 ISelection selection = event.getSelection();
00224 provider.setSelection(selection);
00225 }
00226 }
00227
00228 }