| 1 | package de.uka.ipd.sdq.sensorframework.visualisation.views; |
| 2 | |
| 3 | import java.util.Iterator; |
| 4 | import java.util.LinkedList; |
| 5 | import java.util.List; |
| 6 | |
| 7 | import org.eclipse.jface.action.Action; |
| 8 | import org.eclipse.jface.action.IMenuManager; |
| 9 | import org.eclipse.jface.action.IToolBarManager; |
| 10 | import org.eclipse.jface.dialogs.IInputValidator; |
| 11 | import org.eclipse.jface.dialogs.InputDialog; |
| 12 | import org.eclipse.jface.viewers.ISelectionChangedListener; |
| 13 | import org.eclipse.jface.viewers.IStructuredSelection; |
| 14 | import org.eclipse.jface.viewers.SelectionChangedEvent; |
| 15 | import org.eclipse.jface.viewers.TreeViewer; |
| 16 | import org.eclipse.jface.window.Window; |
| 17 | import org.eclipse.swt.widgets.Display; |
| 18 | |
| 19 | import de.uka.ipd.sdq.sensorframework.entities.Experiment; |
| 20 | import de.uka.ipd.sdq.sensorframework.entities.dao.IExperimentDAO; |
| 21 | |
| 22 | /** |
| 23 | * Adapter class that supplies functionality applicable to Experiments |
| 24 | */ |
| 25 | class ExperimentsAdapter implements ISelectionChangedListener { |
| 26 | |
| 27 | private TreeViewer myChildTree; |
| 28 | |
| 29 | private Action myDeleteAction; |
| 30 | private Action myRenameAction; |
| 31 | |
| 32 | public ExperimentsAdapter(TreeViewer childTree) { |
| 33 | assert (childTree != null ); |
| 34 | myChildTree = childTree; |
| 35 | createActions(); |
| 36 | myChildTree.addSelectionChangedListener(this); |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Adds actions to the menu if the selection is a single experiment. |
| 41 | * This method is called every time the user right clicks to open |
| 42 | * a popup menu. |
| 43 | * @param manager the menu manager to add the actions to |
| 44 | */ |
| 45 | public void addNavigationActions(IMenuManager manager) { |
| 46 | if (getSelectedExperiments().size() < 1){ |
| 47 | return; |
| 48 | } |
| 49 | |
| 50 | //actions which only apply to selections containing experiments |
| 51 | |
| 52 | if (getCurrentSelection().size() == 1) { |
| 53 | //action which only apply to a single distinctly selected experiment |
| 54 | manager.add(myRenameAction); |
| 55 | } |
| 56 | |
| 57 | |
| 58 | manager.add(myDeleteAction); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Adds actions to the toolbar which should then be enabled |
| 63 | * and disabled when the selection changes. |
| 64 | * @param toolbar the toolbar to add actions to |
| 65 | */ |
| 66 | public void addNavigationActions(IToolBarManager toolbar) { |
| 67 | // don't add anything to the toolbar |
| 68 | } |
| 69 | |
| 70 | public void selectionChanged(SelectionChangedEvent event) { |
| 71 | updateNavigationButtons(); |
| 72 | } |
| 73 | |
| 74 | private void createActions() { |
| 75 | myRenameAction = new RenameAction(); |
| 76 | myDeleteAction = new DeleteAction(); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * enables or disables navigation buttons depending on the selection |
| 81 | * @param selection the current selection |
| 82 | */ |
| 83 | private void updateNavigationButtons() { |
| 84 | // no toolbar items or context menu items that need to be updated |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * returns the current selection as a structured selection |
| 89 | * @return the current selection or null if no elements are contained |
| 90 | */ |
| 91 | private IStructuredSelection getCurrentSelection() { |
| 92 | //check if selection has objects |
| 93 | if ( ! (myChildTree.getSelection() instanceof IStructuredSelection)) { |
| 94 | return null; |
| 95 | } |
| 96 | |
| 97 | return (IStructuredSelection)myChildTree.getSelection(); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * retrieves all currently selected Experiments |
| 102 | * @return a list of all currently selected experiments |
| 103 | */ |
| 104 | private List<Experiment> getSelectedExperiments() { |
| 105 | List<Experiment> experiments = new LinkedList<Experiment>(); |
| 106 | |
| 107 | //check if selection has objects |
| 108 | if ( ! (myChildTree.getSelection() instanceof IStructuredSelection)) { |
| 109 | return experiments; |
| 110 | } |
| 111 | |
| 112 | IStructuredSelection selection = |
| 113 | (IStructuredSelection)myChildTree.getSelection(); |
| 114 | |
| 115 | Iterator<?> it = selection.iterator(); |
| 116 | while (it.hasNext()) { |
| 117 | //add experiment to list, if we find one |
| 118 | Object selectedObject = it.next(); |
| 119 | if (selectedObject instanceof ExperimentAndDAO) { |
| 120 | experiments.add(((ExperimentAndDAO) selectedObject).getExperiment()); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | return experiments; |
| 125 | } |
| 126 | |
| 127 | private List<ExperimentAndDAO> getSelectedExperimentAndDAOs() { |
| 128 | List<ExperimentAndDAO> exAndDAOs = new LinkedList<ExperimentAndDAO>(); |
| 129 | |
| 130 | //check if selection has objects |
| 131 | if ( ! (myChildTree.getSelection() instanceof IStructuredSelection)) { |
| 132 | return exAndDAOs; |
| 133 | } |
| 134 | |
| 135 | IStructuredSelection selection = |
| 136 | (IStructuredSelection)myChildTree.getSelection(); |
| 137 | |
| 138 | Iterator<?> it = selection.iterator(); |
| 139 | while (it.hasNext()) { |
| 140 | //add experiment to list, if we find one |
| 141 | Object selectedObject = it.next(); |
| 142 | if (selectedObject instanceof ExperimentAndDAO) { |
| 143 | exAndDAOs.add((ExperimentAndDAO) selectedObject); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | return exAndDAOs; |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Action that deletes all selected Experiments and their contents. |
| 152 | * Note: Currently Experiments are stored as part of ExperimentAndDAO, so |
| 153 | * these will be deleted instead of just the experiment. |
| 154 | */ |
| 155 | private class DeleteAction extends Action { |
| 156 | public DeleteAction() { |
| 157 | super(); |
| 158 | setText("Delete"); |
| 159 | setToolTipText("Delete Experiment"); |
| 160 | setEnabled(true); |
| 161 | } |
| 162 | |
| 163 | @Override |
| 164 | public void run() { |
| 165 | List<ExperimentAndDAO> exAndDAOs = getSelectedExperimentAndDAOs(); |
| 166 | |
| 167 | //delete all selected exAndDAOs |
| 168 | Iterator<ExperimentAndDAO> it = exAndDAOs.iterator(); |
| 169 | while (it.hasNext()) { |
| 170 | ExperimentAndDAO toDel = it.next(); |
| 171 | IExperimentDAO dao = toDel.getDatasource().createExperimentDAO(); |
| 172 | dao.removeExperiment(toDel.getExperiment(), true); |
| 173 | myChildTree.remove(toDel); |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Action that pops up a dialog to rename an experiment. |
| 180 | * Only runs, if exacly one experiment and nothing else is selected. |
| 181 | */ |
| 182 | private class RenameAction extends Action { |
| 183 | public RenameAction() { |
| 184 | super(); |
| 185 | setText("Rename"); |
| 186 | setToolTipText("Rename Experiment"); |
| 187 | setEnabled(true); |
| 188 | } |
| 189 | |
| 190 | @Override |
| 191 | public void run() { |
| 192 | //check if only a single element has been selected |
| 193 | if (getCurrentSelection().size() != 1) { |
| 194 | return; |
| 195 | } |
| 196 | |
| 197 | List<ExperimentAndDAO> expAndDAOs = getSelectedExperimentAndDAOs(); |
| 198 | if (expAndDAOs.isEmpty()) { |
| 199 | return; |
| 200 | } |
| 201 | |
| 202 | //get the first (and only) selected experiment |
| 203 | Experiment experiment = expAndDAOs.get(0).getExperiment(); |
| 204 | |
| 205 | //ask the user for a new name |
| 206 | InputDialog dlg = new InputDialog(Display.getCurrent().getActiveShell(), |
| 207 | "Rename experiment", "Enter the new experiment name!", experiment.getExperimentName(), new NameValidator()); |
| 208 | if (dlg.open() == Window.OK) { |
| 209 | experiment.setExperimentName(dlg.getValue()); |
| 210 | |
| 211 | myChildTree.refresh(); |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | private class NameValidator implements IInputValidator { |
| 216 | |
| 217 | public String isValid(String newText) { |
| 218 | if ( newText == "" ) { |
| 219 | return "Please enter a name!"; |
| 220 | } |
| 221 | |
| 222 | return null; |
| 223 | } |
| 224 | |
| 225 | } |
| 226 | } |
| 227 | } |