1 | package de.uka.ipd.sdq.tcfmoop.views; |
2 | |
3 | import java.util.ArrayList; |
4 | import java.util.HashMap; |
5 | import java.util.List; |
6 | import java.util.Map; |
7 | |
8 | import org.eclipse.swt.widgets.Composite; |
9 | import org.eclipse.swt.widgets.Display; |
10 | import org.eclipse.swt.widgets.Menu; |
11 | import org.eclipse.ui.part.*; |
12 | import org.eclipse.jface.resource.ImageDescriptor; |
13 | import org.eclipse.jface.viewers.*; |
14 | import org.eclipse.swt.dnd.Clipboard; |
15 | import org.eclipse.swt.dnd.TextTransfer; |
16 | import org.eclipse.swt.dnd.Transfer; |
17 | import org.eclipse.swt.graphics.Image; |
18 | import org.eclipse.jface.action.*; |
19 | import org.eclipse.ui.*; |
20 | import org.eclipse.swt.SWT; |
21 | |
22 | import de.uka.ipd.sdq.tcfmoop.Activator; |
23 | import de.uka.ipd.sdq.tcfmoop.outputtree.Node; |
24 | import de.uka.ipd.sdq.tcfmoop.outputtree.Tree; |
25 | import de.uka.ipd.sdq.tcfmoop.tcmanager.IOptimizationTerminatedListener; |
26 | import de.uka.ipd.sdq.tcfmoop.tcmanager.IOutputChangedListener; |
27 | import de.uka.ipd.sdq.tcfmoop.tcmanager.IRequestManualTerminationListener; |
28 | import de.uka.ipd.sdq.tcfmoop.tcmanager.IRequestManualTerminationProvider; |
29 | import de.uka.ipd.sdq.tcfmoop.tcmanager.ITerminationCriteriaManager; |
30 | import de.uka.ipd.sdq.tcfmoop.tcmanager.ITerminationCriteriaManagerInitializedListener; |
31 | import de.uka.ipd.sdq.tcfmoop.tcmanager.TerminationCriteriaManager; |
32 | |
33 | public class TerminationCriteriaView extends ViewPart implements IOutputChangedListener, |
34 | ITerminationCriteriaManagerInitializedListener, |
35 | IOptimizationTerminatedListener, |
36 | IRequestManualTerminationProvider{ |
37 | |
38 | /** |
39 | * The ID of the view as specified by the extension. |
40 | */ |
41 | public static final String ID = "de.uka.ipd.sdq.tcfmoop.views.TerminationCriteriaView"; |
42 | public Tree terminationCriteriaManagerOutput; |
43 | private List<IRequestManualTerminationListener> requestManualTerminationListeners = new ArrayList<IRequestManualTerminationListener>(); |
44 | |
45 | private TreeViewer viewer; |
46 | //private DrillDownAdapter drillDownAdapter; |
47 | private Action requestTerminationAction; |
48 | private Action expandAllaction; |
49 | private Action colapseAllAction; |
50 | private Action copyAllAction; |
51 | private Action doubleClickAction; |
52 | private Map<ImageDescriptor, Image> imageCache = new HashMap<ImageDescriptor, Image>(11); |
53 | |
54 | private class ViewLabelProvider extends LabelProvider { |
55 | |
56 | public String getText(Object obj) { |
57 | return obj.toString(); |
58 | } |
59 | public Image getImage(Object obj) { |
60 | ImageDescriptor descriptor = null; |
61 | if (obj instanceof Node){ |
62 | switch(((Node)obj).type){ |
63 | case EXPRESSION: |
64 | descriptor = Activator.getImageDescriptor("icons/expression.gif"); |
65 | break; |
66 | case MANAGER: |
67 | descriptor = Activator.getImageDescriptor("icons/manager.gif"); |
68 | break; |
69 | case PARAMETER: |
70 | descriptor = Activator.getImageDescriptor("icons/parameter.gif"); |
71 | break; |
72 | case PARAMETER_GROUP: |
73 | descriptor = Activator.getImageDescriptor("icons/parameterGroup.gif"); |
74 | break; |
75 | case TERMINATION_CRITERIA: |
76 | descriptor = Activator.getImageDescriptor("icons/terminationCriteria.gif"); |
77 | break; |
78 | case WARNING: |
79 | descriptor = Activator.getImageDescriptor("icons/warning.gif"); |
80 | break; |
81 | default: |
82 | } |
83 | } |
84 | |
85 | Image image = (Image)imageCache.get(descriptor); |
86 | if (image == null) { |
87 | image = descriptor.createImage(); |
88 | imageCache.put(descriptor, image); |
89 | } |
90 | return image; |
91 | } |
92 | |
93 | } |
94 | |
95 | private class NameSorter extends ViewerSorter { |
96 | @Override |
97 | public void sort(Viewer viewer, Object[] elements){ |
98 | /* |
99 | * Leave the sorting job to the TerminationCriteriaManager |
100 | * and don't sort anything here |
101 | */ |
102 | }; |
103 | } |
104 | |
105 | /** |
106 | * The constructor. |
107 | */ |
108 | public TerminationCriteriaView() { |
109 | TerminationCriteriaManager.addTerminationCriteriaManagerInitializedListener(this); |
110 | } |
111 | |
112 | /** |
113 | * This is a callback that will allow us |
114 | * to create the viewer and initialize it. |
115 | */ |
116 | @Override |
117 | public void createPartControl(Composite parent) { |
118 | viewer = new TreeViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL); |
119 | viewer.setContentProvider(new TerminationCriteriaViewContentProvider(this)); |
120 | viewer.setLabelProvider(new ViewLabelProvider()); |
121 | viewer.setSorter(new NameSorter()); |
122 | viewer.setInput(getViewSite()); |
123 | |
124 | // Create the help context id for the viewer's control |
125 | PlatformUI.getWorkbench().getHelpSystem().setHelp(viewer.getControl(), "de.uka.ipd.sdq.tcfmoop.viewer"); |
126 | makeActions(); |
127 | hookContextMenu(); |
128 | hookDoubleClickAction(); |
129 | contributeToActionBars(); |
130 | |
131 | } |
132 | |
133 | private void hookContextMenu() { |
134 | MenuManager menuMgr = new MenuManager("#PopupMenu"); |
135 | menuMgr.setRemoveAllWhenShown(true); |
136 | menuMgr.addMenuListener(new IMenuListener() { |
137 | public void menuAboutToShow(IMenuManager manager) { |
138 | TerminationCriteriaView.this.fillContextMenu(manager); |
139 | } |
140 | }); |
141 | Menu menu = menuMgr.createContextMenu(viewer.getControl()); |
142 | viewer.getControl().setMenu(menu); |
143 | getSite().registerContextMenu(menuMgr, viewer); |
144 | } |
145 | |
146 | private void contributeToActionBars() { |
147 | IActionBars bars = getViewSite().getActionBars(); |
148 | fillLocalPullDown(bars.getMenuManager()); |
149 | fillLocalToolBar(bars.getToolBarManager()); |
150 | } |
151 | |
152 | private void fillLocalPullDown(IMenuManager manager) { |
153 | manager.add(requestTerminationAction); |
154 | manager.add(copyAllAction); |
155 | manager.add(colapseAllAction); |
156 | manager.add(expandAllaction); |
157 | } |
158 | |
159 | private void fillContextMenu(IMenuManager manager) { |
160 | manager.add(requestTerminationAction); |
161 | manager.add(new Separator()); |
162 | manager.add(copyAllAction); |
163 | manager.add(new Separator()); |
164 | manager.add(expandAllaction); |
165 | manager.add(colapseAllAction); |
166 | } |
167 | |
168 | private void fillLocalToolBar(IToolBarManager manager) { |
169 | manager.add(requestTerminationAction); |
170 | manager.add(new Separator()); |
171 | manager.add(copyAllAction); |
172 | manager.add(new Separator()); |
173 | manager.add(expandAllaction); |
174 | manager.add(colapseAllAction); |
175 | } |
176 | |
177 | private void makeActions() { |
178 | requestTerminationAction = new Action() { |
179 | public void run() { |
180 | fireRequestManualTerminationEvent(); |
181 | } |
182 | }; |
183 | requestTerminationAction.setText("Request Manual Stop"); |
184 | requestTerminationAction.setToolTipText("Request Manual Stop: Stops the optimization after the execution of the current iteration."); |
185 | requestTerminationAction.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages(). |
186 | getImageDescriptor(ISharedImages.IMG_ELCL_STOP)); |
187 | requestTerminationAction.setEnabled(false); |
188 | |
189 | expandAllaction = new Action() { |
190 | public void run() { |
191 | viewer.expandAll(); |
192 | } |
193 | }; |
194 | expandAllaction.setText("Expand All"); |
195 | expandAllaction.setToolTipText("Expand All Nodes"); |
196 | expandAllaction.setImageDescriptor(Activator.getImageDescriptor("icons/expandall.gif")); |
197 | |
198 | colapseAllAction = new Action() { |
199 | public void run() { |
200 | viewer.collapseAll(); |
201 | } |
202 | }; |
203 | colapseAllAction.setText("Colapse All"); |
204 | colapseAllAction.setToolTipText("Colapse All Nodes"); |
205 | colapseAllAction.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages(). |
206 | getImageDescriptor(ISharedImages.IMG_ELCL_COLLAPSEALL)); |
207 | |
208 | copyAllAction = new Action() { |
209 | public void run() { |
210 | Clipboard cb = new Clipboard(Display.getDefault()); |
211 | |
212 | String clipBoardText = terminationCriteriaManagerOutput.subTreeToString("\t", 0); |
213 | |
214 | TextTransfer textTransfer = TextTransfer.getInstance(); |
215 | cb.setContents(new Object[] { clipBoardText }, new Transfer[] { textTransfer }); |
216 | } |
217 | }; |
218 | copyAllAction.setText("Copy to Clipboard"); |
219 | copyAllAction.setToolTipText("Copy the content of this view to the clipboard in a suitable Tree representation."); |
220 | copyAllAction.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages(). |
221 | getImageDescriptor(ISharedImages.IMG_TOOL_COPY)); |
222 | |
223 | doubleClickAction = new Action() { |
224 | public void run() { |
225 | Object obj = ((IStructuredSelection)(viewer.getSelection())).iterator().next(); |
226 | if(viewer.getExpandedState(obj)){ |
227 | viewer.collapseToLevel(obj, 1); |
228 | }else{ |
229 | viewer.expandToLevel(obj, 1); |
230 | } |
231 | } |
232 | }; |
233 | } |
234 | |
235 | private void hookDoubleClickAction() { |
236 | viewer.addDoubleClickListener(new IDoubleClickListener() { |
237 | public void doubleClick(DoubleClickEvent event) { |
238 | doubleClickAction.run(); |
239 | } |
240 | }); |
241 | } |
242 | |
243 | /** |
244 | * Passing the focus request to the viewer's control. |
245 | */ |
246 | @Override |
247 | public void setFocus() { |
248 | viewer.getControl().setFocus(); |
249 | } |
250 | |
251 | @Override |
252 | public void handleTerminationCriteriaManagerInitializedEvent( |
253 | ITerminationCriteriaManager instance) { |
254 | instance.addOutputChangedListener(this); |
255 | instance.addOptimizationTerminatedListener(this); |
256 | |
257 | this.requestTerminationAction.setEnabled(true); |
258 | } |
259 | |
260 | @Override |
261 | public void handleOutputChangedEvent(final Tree outPut) { |
262 | Display.getDefault().syncExec(new Runnable() { |
263 | public void run() { |
264 | terminationCriteriaManagerOutput = outPut; |
265 | viewer.refresh(); |
266 | } |
267 | }); |
268 | } |
269 | |
270 | @Override |
271 | public void handleOptimizationTerminatedListener(TerminationCriteriaManager instance) { |
272 | instance.removeOutputChangedListener(this); |
273 | instance.removeOptimizationTerminatedListener(this); |
274 | |
275 | this.requestTerminationAction.setEnabled(false); |
276 | } |
277 | |
278 | private void fireRequestManualTerminationEvent(){ |
279 | for(IRequestManualTerminationListener listener : this.requestManualTerminationListeners){ |
280 | listener.handleManualTerminationRequest(); |
281 | } |
282 | this.requestTerminationAction.setEnabled(false); |
283 | } |
284 | |
285 | @Override |
286 | public void addRequestManualTerminationListener( |
287 | IRequestManualTerminationListener listener) { |
288 | this.requestManualTerminationListeners.add(listener); |
289 | |
290 | } |
291 | |
292 | @Override |
293 | public void removeRequestManualTerminationListener( |
294 | IRequestManualTerminationListener listener) { |
295 | this.requestManualTerminationListeners.remove(listener); |
296 | |
297 | } |
298 | } |