]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.debug.ui/src/org/simantics/debug/ui/GraphDebuggerEditor.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.debug.ui / src / org / simantics / debug / ui / GraphDebuggerEditor.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;\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 GraphDebuggerEditor extends ResourceEditorPart {\r
40 \r
41     public static final String EDITOR_ID = "org.simantics.debug.graphDebuggerEditor";\r
42 \r
43     private GraphDebugger      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 \r
51     @Override\r
52     public void createPartControl(Composite parent) {\r
53         // Create UI\r
54         parent.setLayout(LayoutUtils.createNoBorderGridLayout(1));\r
55 \r
56         debugger = new GraphDebugger(parent, SWT.NONE, getSession(), getInputResource(), getSite());\r
57         debugger.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));\r
58         debugger.setLayout(LayoutUtils.createNoBorderGridLayout(1));\r
59 \r
60         Composite bar = new Composite(debugger, SWT.NONE);\r
61         bar.setLayout(new GridLayout(3, false));\r
62         bar.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));\r
63 \r
64         backAction = new BackAction();\r
65         forwardAction = new ForwardAction();\r
66         refreshAction = new RefreshAction();\r
67         findAction = new FindAction();\r
68         addStatementAction = new AddStatementAction();\r
69         addResourceAction = new AddResourceAction();\r
70 \r
71         ToolBar toolBar = new ToolBar(bar, SWT.HORIZONTAL | SWT.FLAT);\r
72 \r
73         new ActionContributionItem(backAction).fill(toolBar, 0);\r
74         new ActionContributionItem(forwardAction).fill(toolBar, 1);\r
75         new Separator().fill(toolBar, 2);\r
76         new ActionContributionItem(refreshAction).fill(toolBar, 3);\r
77         new Separator().fill(toolBar, 4);\r
78         new ActionContributionItem(findAction).fill(toolBar, 5);\r
79         new ActionContributionItem(addStatementAction).fill(toolBar, 6);\r
80         new ActionContributionItem(addResourceAction).fill(toolBar, 7);\r
81 \r
82         debugger.createResourceText(bar);\r
83         Control dropLabel = debugger.createDropLabel(bar);\r
84         GridDataFactory.fillDefaults().grab(true, false).align(SWT.FILL, SWT.FILL).hint(SWT.DEFAULT, 20).span(3, 1).applyTo(dropLabel);\r
85         Control browser = debugger.createBrowser(debugger);\r
86         GridDataFactory.fillDefaults().grab(true, true).span(3, 1).applyTo(browser);\r
87 \r
88         debugger.addHistoryListener(new GraphDebugger.HistoryListener() {\r
89             @Override\r
90             public void historyChanged() {\r
91                 updateActionStates();\r
92             }\r
93         });\r
94 \r
95         updateActionStates();\r
96     }\r
97 \r
98     @Override\r
99     public void setFocus() {\r
100         if (debugger != null)\r
101             debugger.setFocus();\r
102     }\r
103 \r
104     public void back() {\r
105         debugger.back();\r
106     }\r
107 \r
108     public void forward() {\r
109         debugger.forward();\r
110     }\r
111 \r
112     public void refreshBrowser() {\r
113         debugger.refreshBrowser();\r
114     }\r
115 \r
116     private void updateActionStates() {\r
117         backAction.setEnabled(!debugger.hasBackHistory());\r
118         forwardAction.setEnabled(!debugger.hasForwardHistory());\r
119     }\r
120 \r
121     class RefreshAction extends Action {\r
122         public RefreshAction() {\r
123             super("Refresh", BundleUtils.getImageDescriptorFromPlugin(SimanticsUI.PLUGIN_ID, "icons/refresh.gif"));\r
124             setToolTipText("Refresh");\r
125         }\r
126         @Override\r
127         public void run() {\r
128             refreshBrowser();\r
129         }\r
130     }\r
131 \r
132     class FindAction extends Action {\r
133         public FindAction() {\r
134             super("Find", BundleUtils.getImageDescriptorFromPlugin(Activator.PLUGIN_ID, "icons/cog_blue.png"));\r
135             setToolTipText("Find Resource");\r
136         }\r
137         @Override\r
138         public void run() {\r
139             DebugUtils.find(Simantics.getSession(), debugger);\r
140         }\r
141     }\r
142 \r
143     class AddStatementAction extends Action {\r
144         public AddStatementAction() {\r
145             super("AddStatement", BundleUtils.getImageDescriptorFromPlugin(Activator.PLUGIN_ID, "icons/cog_add.png"));\r
146             setToolTipText("Add Statement Between Existing Resources");\r
147         }\r
148         @Override\r
149         public void run() {\r
150             try {\r
151                 DebugUtils.addStatement(Simantics.getSession(), debugger);\r
152             } catch (DatabaseException e) {\r
153                 Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, e.getMessage(), e));\r
154             }\r
155         }\r
156     }\r
157 \r
158     class AddResourceAction extends Action {\r
159         public AddResourceAction() {\r
160             super("AddResource", BundleUtils.getImageDescriptorFromPlugin(Activator.PLUGIN_ID, "icons/cog_add.png"));\r
161             setToolTipText("Add New Related Resource");\r
162         }\r
163         @Override\r
164         public void run() {\r
165             try {\r
166                 DebugUtils.addResource(Simantics.getSession(), debugger);\r
167             } catch (DatabaseException e) {\r
168                 Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, e.getMessage(), e));\r
169             }\r
170         }\r
171     }\r
172 \r
173     class BackAction extends Action {\r
174         public BackAction() {\r
175             super("Back", Action.AS_PUSH_BUTTON);\r
176             setToolTipText("Back");\r
177             setImageDescriptor(PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(ISharedImages.IMG_TOOL_BACK));\r
178             setDisabledImageDescriptor(PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(ISharedImages.IMG_TOOL_BACK_DISABLED));\r
179         }\r
180         @Override\r
181         public void run() {\r
182             back();\r
183             updateActionStates();\r
184         }\r
185     }\r
186 \r
187     class ForwardAction extends Action {\r
188         public ForwardAction() {\r
189             super("Forward", Action.AS_PUSH_BUTTON);\r
190             setToolTipText("Forward");\r
191             setImageDescriptor(PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(ISharedImages.IMG_TOOL_FORWARD));\r
192             setDisabledImageDescriptor(PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(ISharedImages.IMG_TOOL_FORWARD_DISABLED));\r
193         }\r
194         @Override\r
195         public void run() {\r
196             forward();\r
197             updateActionStates();\r
198         }\r
199     }\r
200 \r
201 }\r