~ubuntu-branches/ubuntu/trusty/eclipse-linuxtools/trusty

« back to all changes in this revision

Viewing changes to systemtap/org.eclipse.linuxtools.callgraph.core/src/org/eclipse/linuxtools/internal/callgraph/core/SystemTapView.java

  • Committer: Package Import Robot
  • Author(s): Jakub Adam
  • Date: 2012-06-29 12:07:30 UTC
  • Revision ID: package-import@ubuntu.com-20120629120730-bfri1xys1i71dpn6
Tags: upstream-1.0.0
ImportĀ upstreamĀ versionĀ 1.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.eclipse.linuxtools.internal.callgraph.core;
 
2
 
 
3
import java.io.BufferedReader;
 
4
import java.io.File;
 
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;
 
11
 
 
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;
 
35
 
 
36
public abstract class SystemTapView extends ViewPart {
 
37
   
 
38
    private final String NEW_LINE = Messages.getString("SystemTapView.1"); //$NON-NLS-1$
 
39
   
 
40
    public Composite masterComposite;
 
41
    private IMenuManager help;
 
42
    private Action kill;
 
43
 
 
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;
 
54
 
 
55
 
 
56
    /**
 
57
     * The constructor.
 
58
     *
 
59
     * @return
 
60
     */
 
61
    public SystemTapView() {
 
62
    }
 
63
 
 
64
    /**
 
65
     * This method will be called from GraphUIJob to load the view
 
66
     * @param targetDisplay
 
67
     * @param monitor
 
68
     * @return Status.OK_STATUS to continue, Status.CANCEL_STATUS to abort
 
69
     */
 
70
    public abstract IStatus initializeView(Display targetDisplay,
 
71
            IProgressMonitor monitor);
 
72
 
 
73
    /**
 
74
     * @param doMaximize
 
75
     *            : true && view minimized will maximize the view, otherwise it
 
76
     *            will just 'refresh'
 
77
     */
 
78
    public void maximizeOrRefresh(boolean doMaximize) {
 
79
        IWorkbenchPage page = this.getViewSite()
 
80
                .getWorkbenchWindow().getActivePage();
 
81
 
 
82
        if (doMaximize
 
83
                && page.getPartState(page.getActivePartReference()) != IWorkbenchPage.STATE_MAXIMIZED) {
 
84
            IWorkbenchAction action = ActionFactory.MAXIMIZE.create(this
 
85
                    .getViewSite().getWorkbenchWindow());
 
86
            action.run();
 
87
        } else {
 
88
            this.layout();
 
89
        }
 
90
    }
 
91
 
 
92
    public void layout() {
 
93
        masterComposite.layout();
 
94
    }
 
95
 
 
96
    /**
 
97
     * If view is not maximized it will be maximized
 
98
     */
 
99
    public void maximizeIfUnmaximized() {
 
100
        IWorkbenchPage page = this.getViewSite()
 
101
                .getWorkbenchWindow().getActivePage();
 
102
 
 
103
        if (page.getPartState(page.getActivePartReference()) != IWorkbenchPage.STATE_MAXIMIZED) {
 
104
            IWorkbenchAction action = ActionFactory.MAXIMIZE.create(this
 
105
                    .getViewSite().getWorkbenchWindow());
 
106
            action.run();
 
107
        }
 
108
    }
 
109
 
 
110
    /**
 
111
     * Schedules the updateMethod job in a UI Thread. Does not return until
 
112
     * updateMethod is complete.
 
113
     *
 
114
     * @throws InterruptedException
 
115
     */
 
116
    public void update() throws InterruptedException {
 
117
        ViewUIUpdater updater = new ViewUIUpdater("SystemTapView.update"); //$NON-NLS-1$
 
118
        updater.schedule();
 
119
        updater.join();
 
120
    }
 
121
 
 
122
    private class ViewUIUpdater extends UIJob {
 
123
 
 
124
        public ViewUIUpdater(String name) {
 
125
            super(name);
 
126
        }
 
127
 
 
128
        @Override
 
129
        public IStatus runInUIThread(IProgressMonitor monitor) {
 
130
            updateMethod();
 
131
            return Status.OK_STATUS;
 
132
        }
 
133
 
 
134
    };
 
135
 
 
136
    /**
 
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
 
140
     * SystemTapParser.
 
141
     *
 
142
     * @return
 
143
     */
 
144
    public SystemTapParser getParser() {
 
145
        return parser;
 
146
    }
 
147
 
 
148
    /**
 
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
 
151
     * unexpected.
 
152
     *
 
153
     * @param parser
 
154
     * @return
 
155
     */
 
156
    public boolean setParser(SystemTapParser parser) {
 
157
        this.parser = parser;
 
158
        if (this.parser == null)
 
159
                return false;
 
160
        return true;
 
161
    }
 
162
 
 
163
    /**
 
164
     * Perform whatever actions are necessary to 'update' this viewer. It is
 
165
     * recommended that the update function be called after the setParser method
 
166
     * is called.
 
167
     */
 
168
    public abstract void updateMethod();
 
169
 
 
170
    /**
 
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
 
173
     * point.
 
174
     */
 
175
    public abstract void setViewID();
 
176
   
 
177
    /**
 
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.
 
181
     *
 
182
     * @return True if an open action should be created, false otherwise.
 
183
     */
 
184
    protected abstract boolean createOpenAction();
 
185
   
 
186
    /**
 
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..
 
191
     *
 
192
     *  @return True if an open default action should be created, false otherwise.
 
193
     */
 
194
    protected abstract boolean createOpenDefaultAction();
 
195
 
 
196
   
 
197
    /**
 
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.
 
202
     */
 
203
    public void addFileMenu() {
 
204
        IMenuManager menu = getViewSite().getActionBars().getMenuManager();
 
205
        if (file == null) {
 
206
            file = new MenuManager(Messages.getString("SystemTapView.FileMenu")); //$NON-NLS-1$
 
207
            menu.add(file);
 
208
        }
 
209
       
 
210
       
 
211
        if (createOpenAction())
 
212
            file.add(open_file);
 
213
        if (createOpenDefaultAction())
 
214
            file.add(open_default);
 
215
       
 
216
        createSaveAction();
 
217
        file.add(save_file);
 
218
    }
 
219
   
 
220
   
 
221
    public void addHelpMenu() {
 
222
        IMenuManager menu = getViewSite().getActionBars().getMenuManager();
 
223
        help = new MenuManager(Messages.getString("SystemTapView.Help")); //$NON-NLS-1$
 
224
        menu.add(help);
 
225
        createHelpActions();
 
226
       
 
227
        help.add(help_version);
 
228
    }
 
229
 
 
230
   
 
231
    public void createHelpActions() {
 
232
        help_version = new Action(Messages.getString("SystemTapView.Version")) { //$NON-NLS-1$
 
233
            public void run() {
 
234
                Runtime rt = Runtime.getRuntime();
 
235
                try {
 
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$
 
241
 
 
242
                    while ((line = buf.readLine()) != null) {
 
243
                        message += line + NEW_LINE; //$NON-NLS-1$
 
244
                    }
 
245
 
 
246
                    try {
 
247
                        pr.waitFor();
 
248
                    } catch (InterruptedException e) {
 
249
                        e.printStackTrace();
 
250
                    }
 
251
 
 
252
                    Shell sh = new Shell();
 
253
 
 
254
                    MessageDialog.openInformation(sh, Messages
 
255
                            .getString("SystemTapView.StapVersion"), message); //$NON-NLS-1$
 
256
 
 
257
                } catch (IOException e) {
 
258
                    e.printStackTrace();
 
259
                }
 
260
            }
 
261
        };
 
262
       
 
263
        help_about = new Action(Messages.getString("SystemTapView.AboutMenu")) { //$NON-NLS-1$
 
264
            public void run() {
 
265
                Display disp = Display.getCurrent();
 
266
                if (disp == null){
 
267
                    disp = Display.getDefault();
 
268
                }
 
269
 
 
270
               
 
271
                Shell sh = new Shell(disp, SWT.MIN | SWT.MAX);
 
272
                sh.setSize(425, 540);
 
273
                GridLayout gl = new GridLayout(1, true);
 
274
                sh.setLayout(gl);
 
275
 
 
276
                sh.setText(""); //$NON-NLS-1$
 
277
               
 
278
                Image img = new Image(disp, PluginConstants.getPluginLocation()+"systemtap.png"); //$NON-NLS-1$
 
279
                Composite cmp = new Composite(sh, sh.getStyle());
 
280
                cmp.setLayout(gl);
 
281
                GridData data = new GridData(415,100);
 
282
                cmp.setLayoutData(data);
 
283
                cmp.setBackgroundImage(img);
 
284
 
 
285
                Composite c = new Composite(sh, sh.getStyle());
 
286
                c.setLayout(gl);
 
287
                GridData gd = new GridData(415,400);
 
288
                c.setLayoutData(gd);
 
289
                c.setLocation(0,300);
 
290
                StyledText viewer = new StyledText(c, SWT.READ_ONLY | SWT.MULTI
 
291
                        | SWT.V_SCROLL | SWT.WRAP | SWT.BORDER);       
 
292
               
 
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);
 
297
                viewer.setText(
 
298
                         "" + //$NON-NLS-1$
 
299
                         "" + //$NON-NLS-1$
 
300
                         "" + //$NON-NLS-1$
 
301
                         "" +  //$NON-NLS-1$
 
302
                         "" + //$NON-NLS-1$
 
303
                         "" + //$NON-NLS-1$
 
304
                         
 
305
                         "" + //$NON-NLS-1$
 
306
//                         
 
307
//                         Messages.getString("LaunchAbout.9") + //$NON-NLS-1$
 
308
//                         Messages.getString("LaunchAbout.10") + //$NON-NLS-1$
 
309
                         
 
310
                         "" + //$NON-NLS-1$
 
311
                         "" + //$NON-NLS-1$
 
312
                         "" + //$NON-NLS-1$
 
313
                         
 
314
//                         Messages.getString("LaunchAbout.14") + //$NON-NLS-1$
 
315
//                         Messages.getString("LaunchAbout.15") + //$NON-NLS-1$
 
316
//                         Messages.getString("LaunchAbout.16") + //$NON-NLS-1$
 
317
                         
 
318
                         "" + //$NON-NLS-1$
 
319
                         
 
320
//                         Messages.getString("LaunchAbout.18") + //$NON-NLS-1$
 
321
//                         Messages.getString("LaunchAbout.19") + //$NON-NLS-1$
 
322
                         
 
323
                         "" + //$NON-NLS-1$
 
324
                         "" //$NON-NLS-1$
 
325
                        );
 
326
 
 
327
 
 
328
               
 
329
                sh.open();       
 
330
            }
 
331
        };
 
332
    }
 
333
   
 
334
    protected void createSaveAction() {
 
335
        //Save callgraph.out
 
336
        save_file = new Action(Messages.getString("SystemTapView.SaveMenu")){ //$NON-NLS-1$
 
337
            public void run(){
 
338
                Shell sh = new Shell();
 
339
                FileDialog dialog = new FileDialog(sh, SWT.SAVE);
 
340
                String filePath = dialog.open();
 
341
               
 
342
                if (filePath != null) {
 
343
                    saveData(filePath);
 
344
                }
 
345
            }
 
346
        };
 
347
    }
 
348
   
 
349
   
 
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$
 
354
            public void run() {
 
355
                getParser().cancelJob();
 
356
            }
 
357
        };
 
358
        mgr.add(kill);
 
359
        setKillButtonEnabled(false);
 
360
    }
 
361
   
 
362
    public void setKillButtonEnabled(boolean val) {
 
363
        if (kill != null)
 
364
            kill.setEnabled(val);
 
365
    }
 
366
   
 
367
   
 
368
    public Action getKillButton() {
 
369
        return kill;
 
370
    }
 
371
   
 
372
    public  Action getHelp_version() {
 
373
        return help_version;
 
374
    }
 
375
 
 
376
    public  void setHelp_version(Action helpVersion) {
 
377
        help_version = helpVersion;
 
378
    }
 
379
   
 
380
    public Action getSave_file() {
 
381
        return save_file;
 
382
    }
 
383
 
 
384
 
 
385
    /**
 
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.
 
389
     *
 
390
     * @param sourcePath
 
391
     */
 
392
    public void saveData(String targetFile) {
 
393
        try {
 
394
            File file = new File(targetFile);
 
395
            file.delete();
 
396
            file.createNewFile();
 
397
           
 
398
            File sFile = new File(sourcePath);
 
399
            if (!sFile.exists()) {
 
400
                return;
 
401
            }
 
402
           
 
403
             FileChannel in = null;
 
404
             FileChannel out = null;
 
405
            
 
406
             try {         
 
407
                  in = new FileInputStream(sFile).getChannel();
 
408
                  out = new FileOutputStream(file).getChannel();
 
409
                 
 
410
                  if (in == null || out == null)
 
411
                      return;
 
412
         
 
413
                  long size = in.size();
 
414
                  MappedByteBuffer buf = in.map(FileChannel.MapMode.READ_ONLY, 0, size);
 
415
         
 
416
                  out.write(buf);
 
417
         
 
418
             } finally {
 
419
                  if (in != null)         
 
420
                      in.close();
 
421
                  if (out != null)    
 
422
                      out.close();
 
423
             }
 
424
           
 
425
           
 
426
        } catch (IOException e) {
 
427
            e.printStackTrace();
 
428
        }
 
429
    }
 
430
 
 
431
    public void setSourcePath(String file) {
 
432
        sourcePath = file;
 
433
    }
 
434
   
 
435
    public Action getOpen_file() {
 
436
        return open_file;
 
437
    }
 
438
}