]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.debug.ui/src/org/simantics/debug/ui/graph/GraphicalDebuggerEditor.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.debug.ui / src / org / simantics / debug / ui / graph / GraphicalDebuggerEditor.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2013 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *     Semantum Oy - index based searching and graph manipulation (#4255)\r
12  *******************************************************************************/\r
13 package org.simantics.debug.ui.graph;\r
14 \r
15 import org.eclipse.core.runtime.IStatus;\r
16 import org.eclipse.core.runtime.Status;\r
17 import org.eclipse.jface.action.Action;\r
18 import org.eclipse.jface.action.ActionContributionItem;\r
19 import org.eclipse.jface.action.IAction;\r
20 import org.eclipse.jface.action.Separator;\r
21 import org.eclipse.jface.layout.GridDataFactory;\r
22 import org.eclipse.swt.SWT;\r
23 import org.eclipse.swt.layout.GridData;\r
24 import org.eclipse.swt.layout.GridLayout;\r
25 import org.eclipse.swt.widgets.Composite;\r
26 import org.eclipse.swt.widgets.Control;\r
27 import org.eclipse.swt.widgets.ToolBar;\r
28 import org.eclipse.ui.ISharedImages;\r
29 import org.eclipse.ui.PlatformUI;\r
30 import org.simantics.Simantics;\r
31 import org.simantics.db.exception.DatabaseException;\r
32 import org.simantics.debug.ui.internal.Activator;\r
33 import org.simantics.debug.ui.internal.DebugUtils;\r
34 import org.simantics.ui.SimanticsUI;\r
35 import org.simantics.ui.workbench.ResourceEditorPart;\r
36 import org.simantics.utils.ui.BundleUtils;\r
37 import org.simantics.utils.ui.LayoutUtils;\r
38 \r
39 public class GraphicalDebuggerEditor extends ResourceEditorPart {\r
40 \r
41     public static final String EDITOR_ID = "org.simantics.debug.graphicalDebuggerEditor";\r
42 \r
43     private GraphicalDebugger      debugger;\r
44     private IAction            backAction;\r
45     private IAction            forwardAction;\r
46     private IAction            refreshAction;\r
47     private IAction            findAction;\r
48     private IAction            addStatementAction;\r
49     private IAction            addResourceAction;\r
50     private Action                         increaseDepthAction;\r
51     private Action                         decreaseDepthAction;\r
52 \r
53     @Override\r
54     public void createPartControl(Composite parent) {\r
55         // Create UI\r
56         parent.setLayout(LayoutUtils.createNoBorderGridLayout(1));\r
57 \r
58         debugger = new GraphicalDebugger(parent, SWT.NONE, getSession(), getInputResource());\r
59         debugger.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));\r
60         debugger.setLayout(LayoutUtils.createNoBorderGridLayout(1));\r
61 \r
62         Composite bar = new Composite(debugger, SWT.NONE);\r
63         bar.setLayout(new GridLayout(3, false));\r
64         bar.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));\r
65 \r
66         backAction = new BackAction();\r
67         forwardAction = new ForwardAction();\r
68         refreshAction = new RefreshAction();\r
69         findAction = new FindAction();\r
70         addStatementAction = new AddStatementAction();\r
71         addResourceAction = new AddResourceAction();\r
72         increaseDepthAction = new IncreaseDepthAction();\r
73         decreaseDepthAction = new DecreaseDepthAction();\r
74 \r
75         ToolBar toolBar = new ToolBar(bar, SWT.HORIZONTAL | SWT.FLAT);\r
76 \r
77         new ActionContributionItem(backAction).fill(toolBar, 0);\r
78         new ActionContributionItem(forwardAction).fill(toolBar, 1);\r
79         new Separator().fill(toolBar, 2);\r
80         new ActionContributionItem(refreshAction).fill(toolBar, 3);\r
81         new Separator().fill(toolBar, 4);\r
82         new ActionContributionItem(findAction).fill(toolBar, 5);\r
83         new ActionContributionItem(addStatementAction).fill(toolBar, 6);\r
84         new ActionContributionItem(addResourceAction).fill(toolBar, 7);\r
85         new Separator().fill(toolBar, 8);\r
86         new ActionContributionItem(decreaseDepthAction).fill(toolBar, 9);\r
87         new ActionContributionItem(increaseDepthAction).fill(toolBar, 10);\r
88 \r
89         debugger.createResourceText(bar);\r
90         Control dropLabel = debugger.createDropLabel(bar);\r
91         GridDataFactory.fillDefaults().grab(true, false).align(SWT.FILL, SWT.FILL).hint(SWT.DEFAULT, 20).span(3, 1).applyTo(dropLabel);\r
92         Control browser = debugger.createGraph(debugger);\r
93         GridDataFactory.fillDefaults().grab(true, true).span(3, 1).applyTo(browser);\r
94 \r
95         debugger.addHistoryListener(new GraphicalDebugger.HistoryListener() {\r
96             @Override\r
97             public void historyChanged() {\r
98                 updateActionStates();\r
99             }\r
100         });\r
101 \r
102         updateActionStates();\r
103     }\r
104 \r
105     @Override\r
106     public void setFocus() {\r
107         if (debugger != null)\r
108             debugger.setFocus();\r
109     }\r
110 \r
111     public void back() {\r
112         debugger.back();\r
113     }\r
114 \r
115     public void forward() {\r
116         debugger.forward();\r
117     }\r
118 \r
119     public void refreshBrowser() {\r
120         debugger.refreshBrowser();\r
121     }\r
122 \r
123     private void decreaseDepth() {\r
124         debugger.setDepth(debugger.getDepth() - 1);\r
125     }\r
126 \r
127     private void increaseDepth() {\r
128          debugger.setDepth(debugger.getDepth() + 1);\r
129     }\r
130 \r
131     private void updateActionStates() {\r
132         backAction.setEnabled(!debugger.hasBackHistory());\r
133         forwardAction.setEnabled(!debugger.hasForwardHistory());\r
134     }\r
135 \r
136     class RefreshAction extends Action {\r
137         public RefreshAction() {\r
138             super("Refresh", BundleUtils.getImageDescriptorFromPlugin(SimanticsUI.PLUGIN_ID, "icons/refresh.gif"));\r
139         }\r
140         @Override\r
141         public void run() {\r
142             refreshBrowser();\r
143         }\r
144     }\r
145 \r
146     class FindAction extends Action {\r
147         public FindAction() {\r
148             super("Find", BundleUtils.getImageDescriptorFromPlugin(Activator.PLUGIN_ID, "icons/cog_blue.png"));\r
149             setToolTipText("Find Resource");\r
150         }\r
151         @Override\r
152         public void run() {\r
153             DebugUtils.find(Simantics.getSession(), debugger);\r
154         }\r
155     }\r
156 \r
157     class AddStatementAction extends Action {\r
158         public AddStatementAction() {\r
159             super("AddStatement", BundleUtils.getImageDescriptorFromPlugin(Activator.PLUGIN_ID, "icons/cog_add.png"));\r
160             setToolTipText("Add Statement Between Existing Resources");\r
161         }\r
162         @Override\r
163         public void run() {\r
164             try {\r
165                 DebugUtils.addStatement(Simantics.getSession(), debugger);\r
166             } catch (DatabaseException e) {\r
167                 Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, e.getMessage(), e));\r
168             }\r
169         }\r
170     }\r
171     class AddResourceAction extends Action {\r
172         public AddResourceAction() {\r
173             super("AddResource", BundleUtils.getImageDescriptorFromPlugin(Activator.PLUGIN_ID, "icons/cog_add.png"));\r
174             setToolTipText("Add New Related Resource");\r
175         }\r
176         @Override\r
177         public void run() {\r
178             try {\r
179                 DebugUtils.addResource(Simantics.getSession(), debugger);\r
180             } catch (DatabaseException e) {\r
181                 Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, e.getMessage(), e));\r
182             }\r
183         }\r
184     }\r
185     class BackAction extends Action {\r
186         public BackAction() {\r
187             super("Back", Action.AS_PUSH_BUTTON);\r
188             setToolTipText("Back");\r
189             setImageDescriptor(PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(ISharedImages.IMG_TOOL_BACK));\r
190             setDisabledImageDescriptor(PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(ISharedImages.IMG_TOOL_BACK_DISABLED));\r
191         }\r
192         @Override\r
193         public void run() {\r
194             back();\r
195             updateActionStates();\r
196         }\r
197     }\r
198 \r
199     class ForwardAction extends Action {\r
200         public ForwardAction() {\r
201             super("Forward", Action.AS_PUSH_BUTTON);\r
202             setToolTipText("Forward");\r
203             setImageDescriptor(PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(ISharedImages.IMG_TOOL_FORWARD));\r
204             setDisabledImageDescriptor(PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(ISharedImages.IMG_TOOL_FORWARD_DISABLED));\r
205         }\r
206         @Override\r
207         public void run() {\r
208             forward();\r
209             updateActionStates();\r
210         }\r
211     }\r
212     \r
213     class DecreaseDepthAction extends Action {\r
214         public DecreaseDepthAction() {\r
215             super("Decrease", Action.AS_PUSH_BUTTON);\r
216             setImageDescriptor(PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(ISharedImages.IMG_TOOL_BACK));\r
217             setDisabledImageDescriptor(PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(ISharedImages.IMG_TOOL_BACK_DISABLED));\r
218         }\r
219         @Override\r
220         public void run() {\r
221             decreaseDepth();\r
222         }\r
223     }\r
224 \r
225     class IncreaseDepthAction extends Action {\r
226         public IncreaseDepthAction() {\r
227             super("Increase", Action.AS_PUSH_BUTTON);\r
228             setImageDescriptor(PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(ISharedImages.IMG_TOOL_FORWARD));\r
229             setDisabledImageDescriptor(PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(ISharedImages.IMG_TOOL_FORWARD_DISABLED));\r
230         }\r
231         @Override\r
232         public void run() {\r
233             increaseDepth();\r
234         }\r
235     }\r
236 }\r