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