]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.debug.ui/src/org/simantics/debug/ui/GraphDebuggerView.java
7d9d67b2ad3161506e584e139d6cf75afe79e266
[simantics/platform.git] / bundles / org.simantics.debug.ui / src / org / simantics / debug / ui / GraphDebuggerView.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 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.IAction;
19 import org.eclipse.jface.action.IToolBarManager;
20 import org.eclipse.jface.action.Separator;
21 import org.eclipse.swt.SWT;
22 import org.eclipse.swt.widgets.Composite;
23 import org.eclipse.ui.IActionBars;
24 import org.eclipse.ui.ISharedImages;
25 import org.eclipse.ui.PlatformUI;
26 import org.eclipse.ui.part.ViewPart;
27 import org.simantics.Simantics;
28 import org.simantics.db.Resource;
29 import org.simantics.db.Session;
30 import org.simantics.db.exception.DatabaseException;
31 import org.simantics.debug.ui.internal.Activator;
32 import org.simantics.debug.ui.internal.DebugUtils;
33 import org.simantics.ui.SimanticsUI;
34 import org.simantics.ui.workbench.ResourceInput;
35 import org.simantics.utils.ui.BundleUtils;
36 import org.simantics.utils.ui.ErrorLogger;
37 import org.simantics.utils.ui.LayoutUtils;
38
39 public class GraphDebuggerView extends ViewPart {
40
41     public static final String VIEW_ID              = "org.simantics.debug.graphDebugger";
42
43 //    private final boolean      DEFAULT_RECYCLE_VIEW = true;
44
45     private ResourceInput      input;
46
47     private Session            session;
48
49     private GraphDebugger      debugger;
50
51 //    private IAction            recycleToggle;
52     private IAction            backAction;
53     private IAction            forwardAction;
54     private IAction            refreshAction;
55     private IAction            findAction;
56     private IAction            addStatementAction;
57     private IAction            addResourceAction;
58     private IAction            homeAction;
59
60     @Override
61     public void createPartControl(Composite parent) {
62 //      System.out.println("koss: " + getViewSite().getSecondaryId());
63         session = Simantics.getSession();
64
65         // Initialize input
66         String sid = getViewSite().getSecondaryId();
67         Resource r = null;
68         if (sid != null) {
69             input = ResourceInput.unmarshall(sid);
70             try {
71                 r = input.toResource(session);
72             } catch (DatabaseException e) {
73                 ErrorLogger.defaultLogError(e);
74             }
75         }
76
77         // Create UI
78         parent.setLayout(LayoutUtils.createNoBorderGridLayout(1));
79         debugger = new GraphDebugger(parent, SWT.NONE, session, r, getSite());
80         debugger.defaultInitializeUI();
81
82         // Contribute actions
83         makeActions();
84         contributeActions(getViewSite().getActionBars());
85         updateActionStates();
86     }
87
88     @Override
89     public void dispose() {
90         super.dispose();
91     }
92
93     private void makeActions() {
94 //        recycleToggle = new RecycleToggle();
95 //        recycleToggle.setChecked(DEFAULT_RECYCLE_VIEW);
96
97         backAction = new BackAction();
98         forwardAction = new ForwardAction();
99         refreshAction = new RefreshAction();
100         findAction = new FindAction();
101         addStatementAction = new AddStatementAction();
102         addResourceAction = new AddResourceAction();
103         homeAction = new HomeAction();
104     }
105
106     private void contributeActions(IActionBars actionBars) {
107         IToolBarManager toolBar = actionBars.getToolBarManager();
108         toolBar.add(backAction);
109         toolBar.add(forwardAction);
110         toolBar.add(new Separator());
111         toolBar.add(refreshAction);
112         toolBar.add(new Separator());
113         toolBar.add(findAction);
114         toolBar.add(addStatementAction);
115         toolBar.add(addResourceAction);
116         toolBar.add(new Separator());
117         toolBar.add(homeAction);
118
119 //        toolBar.add(recycleToggle);
120     }
121
122     @Override
123     public void setFocus() {
124         if (debugger != null)
125             debugger.setFocus();
126     }
127
128     private void refreshBrowser() {
129         debugger.refreshBrowser();
130     }
131
132 //    class RecycleToggle extends Action {
133 //        public RecycleToggle() {
134 //            super("Open New Views", Action.AS_CHECK_BOX);
135 //            setImageDescriptor(Activator.getImageDescriptor("icons/reload.gif"));
136 //            setToolTipText("Toggles whether or not to open new views when resource links are clicked.");
137 //        }
138 //    }
139 //
140     class RefreshAction extends Action {
141         public RefreshAction() {
142             super("Refresh", BundleUtils.getImageDescriptorFromPlugin(SimanticsUI.PLUGIN_ID, "icons/refresh.gif"));
143             setToolTipText("Refresh");
144         }
145         @Override
146         public void run() {
147             refreshBrowser();
148         }
149     }
150
151     class BackAction extends Action {
152         public BackAction() {
153             super("Back", Action.AS_PUSH_BUTTON);
154             setToolTipText("Back");
155             setImageDescriptor(PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(ISharedImages.IMG_TOOL_BACK));
156             setDisabledImageDescriptor(PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(ISharedImages.IMG_TOOL_BACK_DISABLED));
157         }
158         @Override
159         public void run() {
160             back();
161         }
162     }
163
164     class ForwardAction extends Action {
165         public ForwardAction() {
166             super("Forward", Action.AS_PUSH_BUTTON);
167             setToolTipText("Forward");
168             setImageDescriptor(PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(ISharedImages.IMG_TOOL_FORWARD));
169             setDisabledImageDescriptor(PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(ISharedImages.IMG_TOOL_FORWARD_DISABLED));
170         }
171         @Override
172         public void run() {
173             forward();
174         }
175     }
176
177     class HomeAction extends Action {
178         public HomeAction() {
179             super("Home", Action.AS_PUSH_BUTTON);
180             setToolTipText("Navigate to root library");
181             setImageDescriptor(PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(ISharedImages.IMG_ETOOL_HOME_NAV));
182             setDisabledImageDescriptor(PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(ISharedImages.IMG_ETOOL_HOME_NAV_DISABLED));
183         }
184         @Override
185         public void run() {
186             navigateHome();
187         }
188     }
189
190     class FindAction extends Action {
191         public FindAction() {
192             super("Find", BundleUtils.getImageDescriptorFromPlugin(Activator.PLUGIN_ID, "icons/cog_blue.png"));
193             setToolTipText("Find Resource");
194         }
195         @Override
196         public void run() {
197             DebugUtils.find(Simantics.getSession(), debugger);
198         }
199     }
200
201     class AddStatementAction extends Action {
202         public AddStatementAction() {
203             super("AddStatement", BundleUtils.getImageDescriptorFromPlugin(Activator.PLUGIN_ID, "icons/cog_add.png"));
204             setToolTipText("Add Statement Between Existing Resources");
205         }
206         @Override
207         public void run() {
208             try {
209                 DebugUtils.addStatement(Simantics.getSession(), debugger);
210             } catch (DatabaseException e) {
211                 Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, e.getMessage(), e));
212             }
213         }
214     }
215
216     class AddResourceAction extends Action {
217         public AddResourceAction() {
218             super("AddResource", BundleUtils.getImageDescriptorFromPlugin(Activator.PLUGIN_ID, "icons/cog_add.png"));
219             setToolTipText("Add New Related Resource");
220         }
221         @Override
222         public void run() {
223             try {
224                 DebugUtils.addResource(Simantics.getSession(), debugger);
225             } catch (DatabaseException e) {
226                 Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, e.getMessage(), e));
227             }
228         }
229     }
230
231     private void back() {
232         debugger.back();
233     }
234
235     private void forward() {
236         debugger.forward();
237     }
238
239     private void navigateHome() {
240         Resource rootLibrary = session.getRootLibrary();
241         debugger.changeLocation(rootLibrary);
242     }
243
244     private void updateActionStates() {
245         backAction.setEnabled(debugger.hasBackHistory());
246         forwardAction.setEnabled(debugger.hasForwardHistory());
247     }
248
249 }