1
package org.eclipse.linuxtools.internal.callgraph.core;
3
import java.io.BufferedReader;
5
import java.io.FileInputStream;
6
import java.io.FileOutputStream;
7
import java.io.IOException;
8
import java.io.InputStreamReader;
9
import java.nio.MappedByteBuffer;
10
import java.nio.channels.FileChannel;
12
import org.eclipse.core.runtime.IProgressMonitor;
13
import org.eclipse.core.runtime.IStatus;
14
import org.eclipse.core.runtime.Status;
15
import org.eclipse.jface.action.Action;
16
import org.eclipse.jface.action.IMenuManager;
17
import org.eclipse.jface.action.IToolBarManager;
18
import org.eclipse.jface.action.MenuManager;
19
import org.eclipse.jface.dialogs.MessageDialog;
20
import org.eclipse.swt.SWT;
21
import org.eclipse.swt.custom.StyledText;
22
import org.eclipse.swt.graphics.Font;
23
import org.eclipse.swt.graphics.Image;
24
import org.eclipse.swt.layout.GridData;
25
import org.eclipse.swt.layout.GridLayout;
26
import org.eclipse.swt.widgets.Composite;
27
import org.eclipse.swt.widgets.Display;
28
import org.eclipse.swt.widgets.FileDialog;
29
import org.eclipse.swt.widgets.Shell;
30
import org.eclipse.ui.IWorkbenchPage;
31
import org.eclipse.ui.actions.ActionFactory;
32
import org.eclipse.ui.actions.ActionFactory.IWorkbenchAction;
33
import org.eclipse.ui.part.ViewPart;
34
import org.eclipse.ui.progress.UIJob;
36
public abstract class SystemTapView extends ViewPart {
38
private final String NEW_LINE = Messages.getString("SystemTapView.1"); //$NON-NLS-1$
40
public Composite masterComposite;
41
private IMenuManager help;
44
protected String viewID;
45
@SuppressWarnings("unused")
46
private Action help_about;
47
private Action help_version;
48
protected Action save_file;
49
protected Action open_file;
50
protected Action open_default;
51
protected String sourcePath;
52
protected IMenuManager file;
53
private SystemTapParser parser;
61
public SystemTapView() {
65
* This method will be called from GraphUIJob to load the view
66
* @param targetDisplay
68
* @return Status.OK_STATUS to continue, Status.CANCEL_STATUS to abort
70
public abstract IStatus initializeView(Display targetDisplay,
71
IProgressMonitor monitor);
75
* : true && view minimized will maximize the view, otherwise it
78
public void maximizeOrRefresh(boolean doMaximize) {
79
IWorkbenchPage page = this.getViewSite()
80
.getWorkbenchWindow().getActivePage();
83
&& page.getPartState(page.getActivePartReference()) != IWorkbenchPage.STATE_MAXIMIZED) {
84
IWorkbenchAction action = ActionFactory.MAXIMIZE.create(this
85
.getViewSite().getWorkbenchWindow());
92
public void layout() {
93
masterComposite.layout();
97
* If view is not maximized it will be maximized
99
public void maximizeIfUnmaximized() {
100
IWorkbenchPage page = this.getViewSite()
101
.getWorkbenchWindow().getActivePage();
103
if (page.getPartState(page.getActivePartReference()) != IWorkbenchPage.STATE_MAXIMIZED) {
104
IWorkbenchAction action = ActionFactory.MAXIMIZE.create(this
105
.getViewSite().getWorkbenchWindow());
111
* Schedules the updateMethod job in a UI Thread. Does not return until
112
* updateMethod is complete.
114
* @throws InterruptedException
116
public void update() throws InterruptedException {
117
ViewUIUpdater updater = new ViewUIUpdater("SystemTapView.update"); //$NON-NLS-1$
122
private class ViewUIUpdater extends UIJob {
124
public ViewUIUpdater(String name) {
129
public IStatus runInUIThread(IProgressMonitor monitor) {
131
return Status.OK_STATUS;
137
* Method for fetching a parser object. This method should return
138
* the running parser, or else some features may not work. Create
139
* your own parser parameter, but please ensure that it extends
144
public SystemTapParser getParser() {
149
* Method for setting the parser object of the view. Make this method return
150
* true if the parser is of the expected class, false if it is null or
156
public boolean setParser(SystemTapParser parser) {
157
this.parser = parser;
158
if (this.parser == null)
164
* Perform whatever actions are necessary to 'update' this viewer. It is
165
* recommended that the update function be called after the setParser method
168
public abstract void updateMethod();
171
* Implement this method to set the viewID variable to the id of the view
172
* that extends SystemTapView and uses the core.systemtapview extension
175
public abstract void setViewID();
178
* Implement this method so that the Open button in the file menu created
179
* by <code>addFileMenu()</code> is able to actually open files. User will
180
* be prompted for a file to open.
182
* @return True if an open action should be created, false otherwise.
184
protected abstract boolean createOpenAction();
187
* Implement this method so that the Open default button in the file menu created
188
* by <code>addFileMenu()</code> is able to actually open default. The Open
189
* default button should open from a fixed location, usually the default output
190
* path if that is accessible..
192
* @return True if an open default action should be created, false otherwise.
194
protected abstract boolean createOpenDefaultAction();
198
* Create File menu -- calls the abstract protected methods
199
* <code>createOpenAction()</code> and <code>createOpenDefaultAction()</code>. Have
200
* these methods return false if you do not wish to create an Open or Open Default
201
* option in the File menu of your view.
203
public void addFileMenu() {
204
IMenuManager menu = getViewSite().getActionBars().getMenuManager();
206
file = new MenuManager(Messages.getString("SystemTapView.FileMenu")); //$NON-NLS-1$
211
if (createOpenAction())
213
if (createOpenDefaultAction())
214
file.add(open_default);
221
public void addHelpMenu() {
222
IMenuManager menu = getViewSite().getActionBars().getMenuManager();
223
help = new MenuManager(Messages.getString("SystemTapView.Help")); //$NON-NLS-1$
227
help.add(help_version);
231
public void createHelpActions() {
232
help_version = new Action(Messages.getString("SystemTapView.Version")) { //$NON-NLS-1$
234
Runtime rt = Runtime.getRuntime();
236
Process pr = rt.exec("stap -V"); //$NON-NLS-1$
237
BufferedReader buf = new BufferedReader(
238
new InputStreamReader(pr.getErrorStream()));
239
String line = ""; //$NON-NLS-1$
240
String message = ""; //$NON-NLS-1$
242
while ((line = buf.readLine()) != null) {
243
message += line + NEW_LINE; //$NON-NLS-1$
248
} catch (InterruptedException e) {
252
Shell sh = new Shell();
254
MessageDialog.openInformation(sh, Messages
255
.getString("SystemTapView.StapVersion"), message); //$NON-NLS-1$
257
} catch (IOException e) {
263
help_about = new Action(Messages.getString("SystemTapView.AboutMenu")) { //$NON-NLS-1$
265
Display disp = Display.getCurrent();
267
disp = Display.getDefault();
271
Shell sh = new Shell(disp, SWT.MIN | SWT.MAX);
272
sh.setSize(425, 540);
273
GridLayout gl = new GridLayout(1, true);
276
sh.setText(""); //$NON-NLS-1$
278
Image img = new Image(disp, PluginConstants.getPluginLocation()+"systemtap.png"); //$NON-NLS-1$
279
Composite cmp = new Composite(sh, sh.getStyle());
281
GridData data = new GridData(415,100);
282
cmp.setLayoutData(data);
283
cmp.setBackgroundImage(img);
285
Composite c = new Composite(sh, sh.getStyle());
287
GridData gd = new GridData(415,400);
289
c.setLocation(0,300);
290
StyledText viewer = new StyledText(c, SWT.READ_ONLY | SWT.MULTI
291
| SWT.V_SCROLL | SWT.WRAP | SWT.BORDER);
293
GridData viewerGD = new GridData(SWT.FILL, SWT.FILL, true, true);
294
viewer.setLayoutData(viewerGD);
295
Font font = new Font(sh.getDisplay(), "Monospace", 11, SWT.NORMAL); //$NON-NLS-1$
296
viewer.setFont(font);
307
// Messages.getString("LaunchAbout.9") + //$NON-NLS-1$
308
// Messages.getString("LaunchAbout.10") + //$NON-NLS-1$
314
// Messages.getString("LaunchAbout.14") + //$NON-NLS-1$
315
// Messages.getString("LaunchAbout.15") + //$NON-NLS-1$
316
// Messages.getString("LaunchAbout.16") + //$NON-NLS-1$
320
// Messages.getString("LaunchAbout.18") + //$NON-NLS-1$
321
// Messages.getString("LaunchAbout.19") + //$NON-NLS-1$
334
protected void createSaveAction() {
336
save_file = new Action(Messages.getString("SystemTapView.SaveMenu")){ //$NON-NLS-1$
338
Shell sh = new Shell();
339
FileDialog dialog = new FileDialog(sh, SWT.SAVE);
340
String filePath = dialog.open();
342
if (filePath != null) {
350
protected void addKillButton() {
351
IToolBarManager mgr = getViewSite().getActionBars().getToolBarManager();
352
kill = new Action(Messages.getString("SystemTapView.StopScript"), //$NON-NLS-1$
353
CallgraphCorePlugin.imageDescriptorFromPlugin(CallgraphCorePlugin.PLUGIN_ID, "icons/progress_stop.gif")) { //$NON-NLS-1$
355
getParser().cancelJob();
359
setKillButtonEnabled(false);
362
public void setKillButtonEnabled(boolean val) {
364
kill.setEnabled(val);
368
public Action getKillButton() {
372
public Action getHelp_version() {
376
public void setHelp_version(Action helpVersion) {
377
help_version = helpVersion;
380
public Action getSave_file() {
386
* Implement this method to save data in whichever format your program
387
* needs. Keep in mind that the filePath variable should contain the
388
* filePath of the most recently opened file.
392
public void saveData(String targetFile) {
394
File file = new File(targetFile);
396
file.createNewFile();
398
File sFile = new File(sourcePath);
399
if (!sFile.exists()) {
403
FileChannel in = null;
404
FileChannel out = null;
407
in = new FileInputStream(sFile).getChannel();
408
out = new FileOutputStream(file).getChannel();
410
if (in == null || out == null)
413
long size = in.size();
414
MappedByteBuffer buf = in.map(FileChannel.MapMode.READ_ONLY, 0, size);
426
} catch (IOException e) {
431
public void setSourcePath(String file) {
435
public Action getOpen_file() {