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