| 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.IAdaptable; |
| 9 | import org.eclipse.core.runtime.IPath; |
| 10 | import org.eclipse.jface.resource.ImageDescriptor; |
| 11 | import org.eclipse.ui.IEditorInput; |
| 12 | import org.eclipse.ui.IMemento; |
| 13 | import org.eclipse.ui.IPersistableElement; |
| 14 | |
| 15 | import de.uka.ipd.sdq.sensorframework.entities.Experiment; |
| 16 | import de.uka.ipd.sdq.sensorframework.entities.ExperimentRun; |
| 17 | import de.uka.ipd.sdq.sensorframework.entities.Sensor; |
| 18 | import de.uka.ipd.sdq.sensorframework.entities.dao.IDAOFactory; |
| 19 | import de.uka.ipd.sdq.sensorframework.filter.FilteredCollectionsManager; |
| 20 | import de.uka.ipd.sdq.sensorframework.visualisation.VisualisationPlugin; |
| 21 | |
| 22 | /** @author Roman Andrej */ |
| 23 | public class ConfigEditorInput extends Observable |
| 24 | implements IEditorInput, IPersistableElement,IAdaptable, Observer { |
| 25 | |
| 26 | /** Create the filter manager instance. */ |
| 27 | private FilteredCollectionsManager filtersManager = new FilteredCollectionsManager(); |
| 28 | |
| 29 | private List<ConfigEntry> configEntrys; |
| 30 | private String adapterFactoryID; |
| 31 | |
| 32 | public ConfigEditorInput(String adapterFactoryID) { |
| 33 | this.configEntrys = new ArrayList<ConfigEntry>(); |
| 34 | this.adapterFactoryID = adapterFactoryID; |
| 35 | } |
| 36 | |
| 37 | public ConfigEditorInput(String adapterFactoryID, ConfigEntry configEntry) { |
| 38 | this.configEntrys = new ArrayList<ConfigEntry>(); |
| 39 | this.adapterFactoryID = adapterFactoryID; |
| 40 | addConfigEntry(configEntry); |
| 41 | } |
| 42 | |
| 43 | /** Edit command of ConfigEntry */ |
| 44 | public void editConfigEntry(IDAOFactory datasource, ExperimentRun run, Experiment experiment, Sensor sensor, String adapterFactoryID) { |
| 45 | ConfigEntry configEntry = getConfigEntryToRun(run); |
| 46 | |
| 47 | if (configEntry == null) { |
| 48 | ConfigEntry confEntry = new ConfigEntry(datasource,run,experiment,sensor); |
| 49 | confEntry.addObserver(this); |
| 50 | configEntrys.add(confEntry); |
| 51 | } else |
| 52 | configEntry.setSensorChecked(sensor); |
| 53 | notifyObserver(); |
| 54 | } |
| 55 | |
| 56 | /** Add new ConfigEntry */ |
| 57 | public void addConfigEntry(ConfigEntry configEntry){ |
| 58 | configEntry.addObserver(this); |
| 59 | configEntrys.add(configEntry); |
| 60 | notifyObserver(); |
| 61 | } |
| 62 | |
| 63 | /** Remove a ConfigEntry */ |
| 64 | public void removeConfigEntry(ConfigEntry entry){ |
| 65 | if (configEntrys.contains(entry)){ |
| 66 | entry.deleteObserver(this); |
| 67 | configEntrys.remove(entry); |
| 68 | notifyObserver(); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | public ConfigEntry getConfigEntryToRun(ExperimentRun run){ |
| 73 | for(ConfigEntry re : configEntrys){ |
| 74 | if (re.getExperimentRun().equals(run)) |
| 75 | return re; |
| 76 | } |
| 77 | return null; |
| 78 | } |
| 79 | |
| 80 | public List<ConfigEntry> getConfigEntrys() { |
| 81 | return configEntrys; |
| 82 | } |
| 83 | |
| 84 | public boolean isEmpty(){ |
| 85 | return configEntrys.isEmpty(); |
| 86 | } |
| 87 | |
| 88 | /* (non-Javadoc) |
| 89 | * @see org.eclipse.ui.IEditorInput#exists() |
| 90 | */ |
| 91 | public boolean exists() { |
| 92 | return false; |
| 93 | } |
| 94 | |
| 95 | /* (non-Javadoc) |
| 96 | * @see org.eclipse.ui.IEditorInput#getImageDescriptor() |
| 97 | */ |
| 98 | public ImageDescriptor getImageDescriptor() { |
| 99 | return ImageDescriptor.getMissingImageDescriptor(); |
| 100 | } |
| 101 | |
| 102 | /* (non-Javadoc) |
| 103 | * @see org.eclipse.ui.IEditorInput#getName() |
| 104 | */ |
| 105 | public String getName() { |
| 106 | return ""; |
| 107 | } |
| 108 | |
| 109 | /* (non-Javadoc) |
| 110 | * @see org.eclipse.ui.IEditorInput#getPersistable() |
| 111 | */ |
| 112 | public IPersistableElement getPersistable() { |
| 113 | return this; |
| 114 | } |
| 115 | |
| 116 | /* (non-Javadoc) |
| 117 | * @see org.eclipse.ui.IEditorInput#getToolTipText() |
| 118 | */ |
| 119 | public String getToolTipText() { |
| 120 | return ""; |
| 121 | } |
| 122 | |
| 123 | /* (non-Javadoc) |
| 124 | * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class) |
| 125 | */ |
| 126 | @SuppressWarnings("unchecked") |
| 127 | public Object getAdapter(Class adapter) { |
| 128 | if (adapter == ConfigEditorInput.class) |
| 129 | return this; |
| 130 | return null; |
| 131 | } |
| 132 | |
| 133 | |
| 134 | |
| 135 | /* (non-Javadoc) |
| 136 | * @see org.eclipse.ui.IPersistableElement#getFactoryId() |
| 137 | */ |
| 138 | public String getFactoryId() { |
| 139 | return ConfigEditorInputFactory.getFactoryId(); |
| 140 | } |
| 141 | |
| 142 | /* (non-Javadoc) |
| 143 | * @see org.eclipse.ui.IPersistable#saveState(org.eclipse.ui.IMemento) |
| 144 | */ |
| 145 | public void saveState(IMemento memento) { |
| 146 | try |
| 147 | { |
| 148 | ConfigEditorInputFactory.saveState(memento, this); |
| 149 | } catch (Exception e) { |
| 150 | /* ignore errors if they happen, most likely the corresponding |
| 151 | * sensor entities have been deleted meanwhile |
| 152 | */ |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | /**Set the status of this class to changed and notifies all |
| 157 | * listening observers. |
| 158 | */ |
| 159 | private void notifyObserver() { |
| 160 | setChanged(); |
| 161 | notifyObservers(this); |
| 162 | } |
| 163 | |
| 164 | /** {@inheritDoc} |
| 165 | */ |
| 166 | public void update(Observable o, Object arg) { |
| 167 | // call the local method. |
| 168 | notifyObserver(); |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Return absolute path of the config file. It develops out location in the |
| 173 | * local file system of the plug-in state area for this plug-in and defined |
| 174 | * name. |
| 175 | * |
| 176 | * @return path to configurations file. |
| 177 | */ |
| 178 | public static String getPathToConfigFile() { |
| 179 | IPath path = VisualisationPlugin.getDefault().getStateLocation(); |
| 180 | |
| 181 | return path.toPortableString() + "/" + "persistable_element.xml"; |
| 182 | } |
| 183 | |
| 184 | public String getAdapterFactoryID() { |
| 185 | return this.adapterFactoryID; |
| 186 | } |
| 187 | |
| 188 | public FilteredCollectionsManager getFiltersManager() { |
| 189 | return filtersManager; |
| 190 | } |
| 191 | } |