Skip to main content
summaryrefslogtreecommitdiffstats
blob: 951878fd6476c3aa5a67f92f1698b3ee960d9931 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
/*******************************************************************************
 * Copyright (c) 2009-2013 Red Hat, Inc.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Red Hat - initial API and implementation
 *******************************************************************************/

package org.eclipse.linuxtools.internal.callgraph.core;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.linuxtools.tools.launch.core.factory.RuntimeProcessFactory;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.actions.ActionFactory;
import org.eclipse.ui.actions.ActionFactory.IWorkbenchAction;
import org.eclipse.ui.part.ViewPart;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.eclipse.ui.progress.UIJob;

public abstract class SystemTapView extends ViewPart {

    private final String NEW_LINE = Messages.getString("SystemTapView.1"); //$NON-NLS-1$

    public Composite masterComposite;
    private IMenuManager help;
    private Action kill;

    protected String viewID;
    private Action helpVersion;
    protected Action saveFile;
    protected Action openFile;
    protected Action openDefault;
    protected String sourcePath;
    protected IMenuManager file;
    private SystemTapParser parser;


    /**
     * This method will be called from GraphUIJob to load the view
     * @param targetDisplay
     * @param monitor
     * @return Status.OK_STATUS to continue, Status.CANCEL_STATUS to abort
     */
    public abstract IStatus initializeView(Display targetDisplay,
            IProgressMonitor monitor);

    /**
     * @param doMaximize
     *            : true && view minimized will maximize the view, otherwise it
     *            will just 'refresh'
     */
    public void maximizeOrRefresh(boolean doMaximize) {
        IWorkbenchPage page = this.getViewSite()
                .getWorkbenchWindow().getActivePage();

        if (doMaximize
                && page.getPartState(page.getActivePartReference()) != IWorkbenchPage.STATE_MAXIMIZED) {
            IWorkbenchAction action = ActionFactory.MAXIMIZE.create(this
                    .getViewSite().getWorkbenchWindow());
            action.run();
        } else {
            this.layout();
        }
    }

    public void layout() {
        masterComposite.layout();
    }

    /**
     * If view is not maximized it will be maximized
     */
    public void maximizeIfUnmaximized() {
        IWorkbenchPage page = this.getViewSite()
                .getWorkbenchWindow().getActivePage();

        if (page.getPartState(page.getActivePartReference()) != IWorkbenchPage.STATE_MAXIMIZED) {
            IWorkbenchAction action = ActionFactory.MAXIMIZE.create(this
                    .getViewSite().getWorkbenchWindow());
            action.run();
        }
    }

    /**
     * Schedules the updateMethod job in a UI Thread. Does not return until
     * updateMethod is complete.
     *
     * @throws InterruptedException
     */
    public void update() throws InterruptedException {
        ViewUIUpdater updater = new ViewUIUpdater("SystemTapView.update"); //$NON-NLS-1$
        updater.schedule();
        updater.join();
    }

    private class ViewUIUpdater extends UIJob {

        public ViewUIUpdater(String name) {
            super(name);
        }

        @Override
        public IStatus runInUIThread(IProgressMonitor monitor) {
            updateMethod();
            return Status.OK_STATUS;
        }

    }

    /**
     * Method for fetching a parser object. This method should return
     * the running parser, or else some features may not work. Create
     * your own parser parameter, but please ensure that it extends
     * SystemTapParser.
     *
     * @return
     */
    public SystemTapParser getParser() {
    	return parser;
    }

    /**
     * Method for setting the parser object of the view. Make this method return
     * true if the parser is of the expected class, false if it is null or
     * unexpected.
     *
     * @param parser
     * @return
     */
    public boolean setParser(SystemTapParser parser) {
    	this.parser = parser;
    	if (this.parser == null) {
    		return false;
        }
    	return true;
    }

    /**
     * Perform whatever actions are necessary to 'update' this viewer. It is
     * recommended that the update function be called after the setParser method
     * is called.
     */
    public abstract void updateMethod();

    /**
     * Implement this method to set the viewID variable to the id of the view
     * that extends SystemTapView and uses the core.systemtapview extension
     * point.
     */
    public abstract void setViewID();

    /**
     * Implement this method so that the Open button in the file menu created
     * by <code>addFileMenu()</code> is able to actually open files. User will
     * be prompted for a file to open.
     *
     * @return True if an open action should be created, false otherwise.
     */
    protected abstract boolean createOpenAction();

    /**
     * Implement this method so that the Open default button in the file menu created
     * by <code>addFileMenu()</code> is able to actually open default. The Open
     * default button should open from a fixed location, usually the default output
     * path if that is accessible..
     *
     *  @return True if an open default action should be created, false otherwise.
     */
    protected abstract boolean createOpenDefaultAction();


    /**
     * Create File menu -- calls the abstract protected methods
     * <code>createOpenAction()</code> and <code>createOpenDefaultAction()</code>. Have
     * these methods return false if you do not wish to create an Open or Open Default
     * option in the File menu of your view.
     */
    public void addFileMenu() {
        IMenuManager menu = getViewSite().getActionBars().getMenuManager();
        if (file == null) {
            file = new MenuManager(Messages.getString("SystemTapView.FileMenu")); //$NON-NLS-1$
            menu.add(file);
        }

        if (createOpenAction()) {
            file.add(openFile);
        }
        if (createOpenDefaultAction()) {
            file.add(openDefault);
        }

        createSaveAction();
        file.add(saveFile);
    }


    public void addHelpMenu() {
        IMenuManager menu = getViewSite().getActionBars().getMenuManager();
        help = new MenuManager(Messages.getString("SystemTapView.Help")); //$NON-NLS-1$
        menu.add(help);
        createHelpActions();

        help.add(helpVersion);
    }


    public void createHelpActions() {
        helpVersion = new Action(Messages.getString("SystemTapView.Version")) { //$NON-NLS-1$
            @Override
			public void run() {
                try {
                	Process pr = RuntimeProcessFactory.getFactory().exec("stap -V", null);
                    BufferedReader buf = new BufferedReader(
                            new InputStreamReader(pr.getErrorStream()));
                    String line = ""; //$NON-NLS-1$
                    String message = ""; //$NON-NLS-1$

                    while ((line = buf.readLine()) != null) {
                        message += line + NEW_LINE;
                    }

                    try {
                        pr.waitFor();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    Shell sh = new Shell();

                    MessageDialog.openInformation(sh, Messages
                            .getString("SystemTapView.StapVersion"), message); //$NON-NLS-1$

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        };

        Action helpAbout = new Action(Messages.getString("SystemTapView.AboutMenu")) { //$NON-NLS-1$
            @Override
			public void run() {
                Display disp = Display.getCurrent();
                if (disp == null){
                    disp = Display.getDefault();
                }


                Shell sh = new Shell(disp, SWT.MIN | SWT.MAX);
                sh.setSize(425, 540);
                GridLayout gl = new GridLayout(1, true);
                sh.setLayout(gl);

                sh.setText(""); //$NON-NLS-1$

                Image img = new Image(disp, PluginConstants.getPluginLocation()+"systemtap.png"); //$NON-NLS-1$
                Composite cmp = new Composite(sh, sh.getStyle());
                cmp.setLayout(gl);
                GridData data = new GridData(415,100);
                cmp.setLayoutData(data);
                cmp.setBackgroundImage(img);

                Composite c = new Composite(sh, sh.getStyle());
                c.setLayout(gl);
                GridData gd = new GridData(415,400);
                c.setLayoutData(gd);
                c.setLocation(0,300);
                StyledText viewer = new StyledText(c, SWT.READ_ONLY | SWT.MULTI
                        | SWT.V_SCROLL | SWT.WRAP | SWT.BORDER);

                GridData viewerGD = new GridData(SWT.FILL, SWT.FILL, true, true);
                viewer.setLayoutData(viewerGD);
                Font font = new Font(sh.getDisplay(), "Monospace", 11, SWT.NORMAL); //$NON-NLS-1$
                viewer.setFont(font);
                viewer.setText(
                         "" + //$NON-NLS-1$
                         "" + //$NON-NLS-1$
                         "" + //$NON-NLS-1$
                         "" +  //$NON-NLS-1$
                         "" + //$NON-NLS-1$
                         "" + //$NON-NLS-1$
                         "" + //$NON-NLS-1$
                         "" + //$NON-NLS-1$
                         "" + //$NON-NLS-1$
                         "" + //$NON-NLS-1$
                         "" + //$NON-NLS-1$
                         "" + //$NON-NLS-1$
                         "" //$NON-NLS-1$
                        );



                sh.open();
            }
        };
    }

    protected void createSaveAction() {
        //Save callgraph.out
        saveFile = new Action(Messages.getString("SystemTapView.SaveMenu")){ //$NON-NLS-1$
            @Override
			public void run(){
                Shell sh = new Shell();
                FileDialog dialog = new FileDialog(sh, SWT.SAVE);
                String filePath = dialog.open();

                if (filePath != null) {
                    saveData(filePath);
                }
            }
        };
    }


    protected void addKillButton() {
        IToolBarManager mgr = getViewSite().getActionBars().getToolBarManager();
        kill = new Action(Messages.getString("SystemTapView.StopScript"), //$NON-NLS-1$
                AbstractUIPlugin.imageDescriptorFromPlugin(CallgraphCorePlugin.PLUGIN_ID, "icons/progress_stop.gif")) { //$NON-NLS-1$
            @Override
			public void run() {
                getParser().cancelJob();
            }
        };
        mgr.add(kill);
        setKillButtonEnabled(false);
    }

    public void setKillButtonEnabled(boolean val) {
        if (kill != null) {
            kill.setEnabled(val);
        }
    }


    /**
     * Implement this method to save data in whichever format your program
     * needs. Keep in mind that the filePath variable should contain the
     * filePath of the most recently opened file.
     *
     * @param sourcePath
     */
    public void saveData(String targetFile) {
        try {
            File file = new File(targetFile);
            file.delete();
            file.createNewFile();

            File sFile = new File(sourcePath);
            if (!sFile.exists()) {
                return;
            }

             FileChannel in = null;
             FileChannel out = null;

             try {
                  in = new FileInputStream(sFile).getChannel();
                  out = new FileOutputStream(file).getChannel();

                  if (in == null || out == null) {
                      return;
                  }

                  long size = in.size();
                  MappedByteBuffer buf = in.map(FileChannel.MapMode.READ_ONLY, 0, size);

                  out.write(buf);

             } finally {
                  if (in != null) {
                      in.close();
                  }
                  if (out != null) {
                      out.close();
                  }
             }


        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void setSourcePath(String file) {
        sourcePath = file;
    }

}

Back to the top