| 1 | package de.uka.ipd.sdq.sensorframework.visualisation.editor; |
| 2 | |
| 3 | import java.util.ArrayList; |
| 4 | import java.util.List; |
| 5 | import java.util.Observable; |
| 6 | import java.util.Observer; |
| 7 | |
| 8 | import org.eclipse.core.runtime.IProgressMonitor; |
| 9 | import org.eclipse.jface.dialogs.MessageDialog; |
| 10 | import org.eclipse.jface.util.LocalSelectionTransfer; |
| 11 | import org.eclipse.jface.viewers.ISelection; |
| 12 | import org.eclipse.jface.viewers.ISelectionChangedListener; |
| 13 | import org.eclipse.jface.viewers.ISelectionProvider; |
| 14 | import org.eclipse.jface.viewers.StructuredSelection; |
| 15 | import org.eclipse.swt.dnd.DND; |
| 16 | import org.eclipse.swt.dnd.DropTarget; |
| 17 | import org.eclipse.swt.dnd.Transfer; |
| 18 | import org.eclipse.swt.widgets.Composite; |
| 19 | import org.eclipse.swt.widgets.Control; |
| 20 | import org.eclipse.ui.IEditorInput; |
| 21 | import org.eclipse.ui.IEditorSite; |
| 22 | import org.eclipse.ui.PartInitException; |
| 23 | import org.eclipse.ui.part.EditorPart; |
| 24 | import org.eclipse.ui.views.properties.IPropertySheetPage; |
| 25 | import org.eclipse.ui.views.properties.tabbed.ITabbedPropertySheetPageContributor; |
| 26 | import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage; |
| 27 | |
| 28 | import de.uka.ipd.sdq.sensorframework.adapter.AdapterRegistry; |
| 29 | import de.uka.ipd.sdq.sensorframework.adapter.DataAdapter; |
| 30 | import de.uka.ipd.sdq.sensorframework.adapter.IAdapterFactory; |
| 31 | import de.uka.ipd.sdq.sensorframework.entities.Sensor; |
| 32 | import de.uka.ipd.sdq.sensorframework.entities.SensorAndMeasurements; |
| 33 | import de.uka.ipd.sdq.sensorframework.visualisation.views.ViewDropTargetListener; |
| 34 | |
| 35 | public abstract class AbstractReportView extends EditorPart implements |
| 36 | ITabbedPropertySheetPageContributor, Observer { |
| 37 | |
| 38 | /** Contributor identifier. */ |
| 39 | public static String ABSTRACT_EDITOR_ID = "de.uka.ipd.sdq.sensorframework.visualisation.AbstractReportView"; |
| 40 | |
| 41 | /** Editor input. */ |
| 42 | protected ConfigEditorInput myInput; |
| 43 | |
| 44 | /** List of all data adapters used by this report. */ |
| 45 | private ArrayList<DataAdapter> dataAdapters = null; |
| 46 | |
| 47 | /**Marks if the SettingsChanged event from DataAdapters are ignored. */ |
| 48 | private boolean ignoreDataSettingsChanged; |
| 49 | |
| 50 | public AbstractReportView() { |
| 51 | super(); |
| 52 | ignoreDataSettingsChanged = false; |
| 53 | } |
| 54 | |
| 55 | /* (non-Javadoc) |
| 56 | * @see org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite) |
| 57 | */ |
| 58 | @Override |
| 59 | public void createPartControl(Composite parent) { |
| 60 | getSite().setSelectionProvider(new ISelectionProvider() { |
| 61 | |
| 62 | public void addSelectionChangedListener( |
| 63 | ISelectionChangedListener listener) { |
| 64 | } |
| 65 | |
| 66 | public ISelection getSelection() { |
| 67 | return new StructuredSelection(this); |
| 68 | } |
| 69 | |
| 70 | public void removeSelectionChangedListener( |
| 71 | ISelectionChangedListener listener) { |
| 72 | } |
| 73 | |
| 74 | public void setSelection(ISelection selection) { |
| 75 | } |
| 76 | }); |
| 77 | |
| 78 | addDropSupport(parent); |
| 79 | |
| 80 | createReportControls(parent); |
| 81 | changedInputData(); |
| 82 | } |
| 83 | |
| 84 | /** The method define a 'Template method' pattern. */ |
| 85 | protected abstract void createReportControls(Composite parent); |
| 86 | |
| 87 | private void addDropSupport(Control control) { |
| 88 | |
| 89 | int operations = DND.DROP_COPY | DND.DROP_DEFAULT; |
| 90 | DropTarget target = new DropTarget(control, operations); |
| 91 | |
| 92 | Transfer[] transferTypes = new Transfer[] { |
| 93 | LocalSelectionTransfer.getTransfer() |
| 94 | }; |
| 95 | target.setTransfer(transferTypes); |
| 96 | target.addDropListener(new ViewDropTargetListener( |
| 97 | this.getEditorInput())); |
| 98 | } |
| 99 | |
| 100 | /* (non-Javadoc) |
| 101 | * @see org.eclipse.ui.part.WorkbenchPart#getAdapter(java.lang.Class) |
| 102 | */ |
| 103 | @SuppressWarnings("unchecked") |
| 104 | @Override |
| 105 | public Object getAdapter(Class adapter) { |
| 106 | if (adapter == IPropertySheetPage.class) |
| 107 | return new TabbedPropertySheetPage(this); |
| 108 | return super.getAdapter(adapter); |
| 109 | } |
| 110 | |
| 111 | /** The method return the contributor identifier for 'ConfigEditorInput'. */ |
| 112 | public String getContributorId() { |
| 113 | return ABSTRACT_EDITOR_ID; |
| 114 | } |
| 115 | |
| 116 | |
| 117 | /* (non-Javadoc) |
| 118 | * @see org.eclipse.ui.part.EditorPart#init(org.eclipse.ui.IEditorSite, org.eclipse.ui.IEditorInput) |
| 119 | */ |
| 120 | @Override |
| 121 | public void init(IEditorSite site, IEditorInput input) |
| 122 | throws PartInitException { |
| 123 | setSite(site); |
| 124 | setInput(input); |
| 125 | } |
| 126 | |
| 127 | /** {@inheritDoc} |
| 128 | */ |
| 129 | @Override |
| 130 | protected void setInput(IEditorInput input) { |
| 131 | super.setInput(input); |
| 132 | myInput = (ConfigEditorInput) input; |
| 133 | myInput.addObserver(this); |
| 134 | } |
| 135 | |
| 136 | /** {@inheritDoc} |
| 137 | */ |
| 138 | public void update(Observable o, Object arg) { |
| 139 | getSite().getPage().activate(this); |
| 140 | if (arg != null) { |
| 141 | if (arg == DataAdapter.SETTINGS_CHANGED) |
| 142 | if (!ignoreDataSettingsChanged) |
| 143 | generateVisualization(dataAdapters); |
| 144 | if (ConfigEditorInput.class.isInstance(arg)) |
| 145 | changedInputData(); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | private void changedInputData() { |
| 150 | ArrayList<SensorAndMeasurements> list = new ArrayList<SensorAndMeasurements>(); |
| 151 | |
| 152 | for (ConfigEntry re : myInput.getConfigEntrys()) { |
| 153 | for (Sensor s : re.getSensors()) { |
| 154 | try { |
| 155 | list.add(re.getExperimentRun().getMeasurementsOfSensor(s)); |
| 156 | } catch (Exception e1) { |
| 157 | showMessage(s.getSensorName(), |
| 158 | "Missing the Measurements of sensor!"); |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | // get the adapter factory for input |
| 163 | IAdapterFactory adapterFactory = AdapterRegistry.singleton() |
| 164 | .getFactoryByID(myInput.getAdapterFactoryID()); |
| 165 | |
| 166 | /* Add the data sources to this report an observe changes at |
| 167 | * the adapters and their settings. |
| 168 | */ |
| 169 | ArrayList<DataAdapter> newDataAdapters = new ArrayList<DataAdapter>(); |
| 170 | DataAdapter usedAdapter = null; |
| 171 | for (SensorAndMeasurements sam : list) { |
| 172 | usedAdapter = adapterFactory.getAdapter(myInput |
| 173 | // set the empty filter of the measurements |
| 174 | .getFiltersManager().getFilteredMeasurements(sam)); |
| 175 | usedAdapter.addObserver(this); |
| 176 | newDataAdapters.add(usedAdapter); |
| 177 | } |
| 178 | generateVisualization(newDataAdapters); |
| 179 | for (DataAdapter adapter : newDataAdapters) { |
| 180 | adapter.addObserver(this); |
| 181 | } |
| 182 | dataAdapters = newDataAdapters; |
| 183 | } |
| 184 | |
| 185 | protected abstract void generateVisualization(List<DataAdapter> list); |
| 186 | |
| 187 | /** Show exception message. */ |
| 188 | private void showMessage(String title, String message) { |
| 189 | MessageDialog.openInformation(this.getEditorSite().getShell(), title, |
| 190 | message); |
| 191 | } |
| 192 | |
| 193 | /* (non-Javadoc) |
| 194 | * @see org.eclipse.ui.part.EditorPart#doSave(org.eclipse.core.runtime.IProgressMonitor) |
| 195 | */ |
| 196 | @Override |
| 197 | public void doSave(IProgressMonitor monitor) { |
| 198 | // The implementation is not necessary. |
| 199 | } |
| 200 | |
| 201 | /* (non-Javadoc) |
| 202 | * @see org.eclipse.ui.part.EditorPart#doSaveAs() |
| 203 | */ |
| 204 | @Override |
| 205 | public void doSaveAs() { |
| 206 | // The implementation is not necessary. |
| 207 | } |
| 208 | |
| 209 | /* (non-Javadoc) |
| 210 | * @see org.eclipse.ui.part.EditorPart#isDirty() |
| 211 | */ |
| 212 | @Override |
| 213 | public boolean isDirty() { |
| 214 | return false; |
| 215 | } |
| 216 | |
| 217 | /* (non-Javadoc) |
| 218 | * @see org.eclipse.ui.part.EditorPart#isSaveAsAllowed() |
| 219 | */ |
| 220 | @Override |
| 221 | public boolean isSaveAsAllowed() { |
| 222 | return false; |
| 223 | } |
| 224 | |
| 225 | public boolean isIgnoreDataSettingsChanged() { |
| 226 | return ignoreDataSettingsChanged; |
| 227 | } |
| 228 | |
| 229 | public void setIgnoreDataSettingsChanged(boolean ignoreDataSettingsChanged) { |
| 230 | this.ignoreDataSettingsChanged = ignoreDataSettingsChanged; |
| 231 | } |
| 232 | } |