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

« back to all changes in this revision

Viewing changes to systemtap/org.eclipse.linuxtools.systemtap.ui.dashboardextension/src/org/eclipse/linuxtools/internal/systemtap/ui/dashboardextension/actions/ViewScriptAction.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
/*******************************************************************************
 
2
 * Copyright (c) 2006 IBM Corporation.
 
3
 * All rights reserved. This program and the accompanying materials
 
4
 * are made available under the terms of the Eclipse Public License v1.0
 
5
 * which accompanies this distribution, and is available at
 
6
 * http://www.eclipse.org/legal/epl-v10.html
 
7
 *
 
8
 * Contributors:
 
9
 *     IBM Corporation - Jeff Briggs, Henry Hughes, Ryan Morse, Anithra P J
 
10
 *******************************************************************************/
 
11
 
 
12
package org.eclipse.linuxtools.internal.systemtap.ui.dashboardextension.actions;
 
13
 
 
14
import java.io.FileInputStream;
 
15
import java.io.IOException;
 
16
 
 
17
import org.eclipse.jface.action.Action;
 
18
import org.eclipse.jface.action.IAction;
 
19
import org.eclipse.jface.viewers.ISelection;
 
20
import org.eclipse.jface.viewers.IStructuredSelection;
 
21
import org.eclipse.ui.IEditorPart;
 
22
import org.eclipse.ui.IViewActionDelegate;
 
23
import org.eclipse.ui.IViewPart;
 
24
import org.eclipse.ui.IWorkbenchPage;
 
25
import org.eclipse.ui.PlatformUI;
 
26
import org.eclipse.ui.WorkbenchException;
 
27
 
 
28
import org.eclipse.linuxtools.systemtap.ui.editor.SimpleEditor;
 
29
import org.eclipse.linuxtools.systemtap.ui.dashboard.structures.DashboardModule;
 
30
import org.eclipse.linuxtools.systemtap.ui.dashboard.structures.ModuleTreeNode;
 
31
import org.eclipse.linuxtools.systemtap.ui.ide.IDEPerspective;
 
32
import org.eclipse.linuxtools.systemtap.ui.ide.actions.TempFileAction;
 
33
 
 
34
/**
 
35
 * A class that handles extracting the original script from the dashboard module.
 
36
 * It will then create a new active editor in the IDE perspective and display the
 
37
 * module in a temp file.  Users can then modify this script and use the modified
 
38
 * version as well as the original.
 
39
 * @author Ryan Morse
 
40
 */
 
41
public class ViewScriptAction extends Action implements IViewActionDelegate {
 
42
        /**
 
43
         * This method sets what item the user has selected to view.
 
44
         */
 
45
        public void init(IViewPart view) {
 
46
                selectedItem = null;
 
47
        }
 
48
        
 
49
        /**
 
50
         * This method will retreive the script from the selected module for the user
 
51
         * to see.  It will then create a new active editor in the IDE perspective and 
 
52
         * display the module in a temp file.  Users can then modify this script and 
 
53
         * use the modified version as well as the original.
 
54
         * @param act An action representing the click event used to start this method.
 
55
         */
 
56
        public void run(IAction act) {
 
57
                DashboardModule data = (DashboardModule)selectedItem.getData();
 
58
                
 
59
                try {
 
60
                        IWorkbenchPage p = PlatformUI.getWorkbench().showPerspective(IDEPerspective.ID, PlatformUI.getWorkbench().getActiveWorkbenchWindow());
 
61
 
 
62
                        TempFileAction tfa = new TempFileAction();
 
63
                        tfa.run();
 
64
                        
 
65
                        IEditorPart edit = p.getActiveEditor();
 
66
                        
 
67
                        if(null != edit) {
 
68
                                if(edit instanceof SimpleEditor) {
 
69
                                        SimpleEditor editor = (SimpleEditor)edit;
 
70
                                        
 
71
                                        //Copy the file just to ensure the user has to save their own copy if they want it
 
72
                                        FileInputStream fin;            
 
73
                                        try {
 
74
                                            fin = new FileInputStream(data.script);
 
75
                                            StringBuilder sb = new StringBuilder();
 
76
                                            
 
77
                                            int c;
 
78
                                            while((c = fin.read()) != -1)
 
79
                                                sb.append((char)c);
 
80
                                            
 
81
                                            fin.close();                
 
82
                                                editor.insertText(sb.toString());
 
83
                                        } catch (IOException e) {}
 
84
                                }
 
85
                        }
 
86
                } catch(WorkbenchException we) {}
 
87
        }
 
88
        
 
89
        /**
 
90
         * This method will update the selected item when a new item is selected.
 
91
         * @param action The action that fired this method.
 
92
         * @param selection The newly selected item.
 
93
         */
 
94
        public void selectionChanged(IAction action, ISelection selection) {
 
95
                if(selection instanceof IStructuredSelection) {
 
96
                        IStructuredSelection selected = (IStructuredSelection)selection;
 
97
                        Object o = selected.getFirstElement();
 
98
                        if(o instanceof ModuleTreeNode) {
 
99
                                selectedItem = (ModuleTreeNode)o;
 
100
                                o = selectedItem.getData();
 
101
                                if(null != o) {
 
102
                                        setEnabled(true);
 
103
                                        return;
 
104
                                }
 
105
                        }
 
106
                }
 
107
                setEnabled(false);
 
108
        }
 
109
        
 
110
        private ModuleTreeNode selectedItem;
 
111
}