~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/ExportScriptAction.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.File;
 
15
import java.io.FileNotFoundException;
 
16
import java.io.FileWriter;
 
17
import java.text.MessageFormat;
 
18
 
 
19
import org.eclipse.jface.action.IAction;
 
20
import org.eclipse.jface.dialogs.MessageDialog;
 
21
import org.eclipse.jface.wizard.WizardDialog;
 
22
import org.eclipse.ui.IMemento;
 
23
import org.eclipse.ui.IViewPart;
 
24
import org.eclipse.ui.IWorkbench;
 
25
import org.eclipse.ui.IWorkbenchPage;
 
26
import org.eclipse.ui.IWorkbenchWindowActionDelegate;
 
27
import org.eclipse.ui.PlatformUI;
 
28
import org.eclipse.ui.WorkbenchException;
 
29
import org.eclipse.ui.XMLMemento;
 
30
 
 
31
import org.eclipse.linuxtools.internal.systemtap.ui.dashboardextension.Localization;
 
32
import org.eclipse.linuxtools.internal.systemtap.ui.dashboardextension.dialogs.ExportScriptDialog;
 
33
import org.eclipse.linuxtools.systemtap.ui.graphingapi.nonui.datasets.IDataSet;
 
34
import org.eclipse.linuxtools.systemtap.ui.graphingapi.nonui.datasets.IDataSetParser;
 
35
import org.eclipse.linuxtools.systemtap.ui.graphingapi.nonui.filters.IDataSetFilter;
 
36
import org.eclipse.linuxtools.systemtap.ui.graphingapi.nonui.structures.GraphData;
 
37
import org.eclipse.linuxtools.systemtap.ui.graphingapi.ui.wizards.dataset.DataSetWizard;
 
38
import org.eclipse.linuxtools.systemtap.ui.structures.TreeNode;
 
39
import org.eclipse.linuxtools.systemtap.ui.structures.ZipArchive;
 
40
import org.eclipse.linuxtools.systemtap.ui.dashboard.DashboardPerspective;
 
41
import org.eclipse.linuxtools.systemtap.ui.dashboard.structures.DashboardMetaData;
 
42
import org.eclipse.linuxtools.systemtap.ui.dashboard.structures.DashboardModule;
 
43
import org.eclipse.linuxtools.systemtap.ui.dashboard.structures.DashboardModuleFileFilter;
 
44
import org.eclipse.linuxtools.systemtap.ui.dashboard.views.DashboardModuleBrowserView;
 
45
import org.eclipse.linuxtools.systemtap.ui.graphing.GraphingConstants;
 
46
import org.eclipse.linuxtools.systemtap.ui.ide.IDEPerspective;
 
47
import org.eclipse.linuxtools.systemtap.ui.ide.actions.RunScriptAction;
 
48
import org.eclipse.linuxtools.systemtap.ui.systemtapgui.SystemTapGUISettings;
 
49
 
 
50
/**
 
51
 * This class brings up a dialog box for the user to select what they want the
 
52
 * new module to contain.  If a new module is build, it will be exported to the
 
53
 * dashboard for use at any time.
 
54
 * @author Ryan Morse
 
55
 */
 
56
public class ExportScriptAction extends RunScriptAction implements IWorkbenchWindowActionDelegate {
 
57
        /**
 
58
         * This method will bring up the export script dialog window for the user
 
59
         * to select what they want to new module to contain.  If the user enters
 
60
         * module information and clicks ok the module will be built and added to
 
61
         * the dashboard.
 
62
         */
 
63
        
 
64
        private static String scriptFileName = "/script.stp";
 
65
        
 
66
        public void run(IAction action) {
 
67
                String script = getFilePath();
 
68
                if(null == script || script.length() <= 0) {
 
69
                        String msg = MessageFormat.format(Localization.getString("ExportScriptAction.NoFileToExport"), (Object[])null);
 
70
                        MessageDialog.openWarning(fWindow.getShell(), Localization.getString("ExportScriptAction.Error"), msg);
 
71
                } else {
 
72
                        DataSetWizard wizard = new DataSetWizard(GraphingConstants.DataSetMetaData, script);
 
73
                        IWorkbench workbench = PlatformUI.getWorkbench();
 
74
                        wizard.init(workbench, null);
 
75
                        WizardDialog dialog = new WizardDialog(workbench.getActiveWorkbenchWindow().getShell(), wizard);
 
76
                        dialog.create();
 
77
                        dialog.open();
 
78
 
 
79
                        IDataSetParser parser = wizard.getParser();
 
80
                        IDataSet dataSet = wizard.getDataSet();
 
81
 
 
82
                        wizard.dispose();
 
83
                        
 
84
                        if(null == parser || null == dataSet)
 
85
                                return;
 
86
                        
 
87
                        ExportScriptDialog exportDialog = new ExportScriptDialog(fWindow.getShell(), dataSet);
 
88
                        exportDialog.open();
 
89
                        
 
90
                        exportDialog.dispose();
 
91
                        if(!exportDialog.isCanceled()) {
 
92
                                String category = exportDialog.getCategory();
 
93
                                String display = exportDialog.getDisplay();
 
94
                                String description = exportDialog.getDescription();
 
95
                                GraphData[] gd = exportDialog.getGraphs();
 
96
                                TreeNode filters = exportDialog.getGraphFilters();
 
97
 
 
98
                                validateDirectory();
 
99
                                File meta = saveMetaData(display, category, description, dataSet, parser, gd, filters,"local");
 
100
                                String archiveName = getSaveDirectory() + "/" + category.replace(' ', '_') + "." + display.replace(' ', '_');
 
101
                                buildArchive(archiveName, new File(script), meta);
 
102
                                cleanupFiles(new String[] {archiveName, meta.getAbsolutePath()});
 
103
                                updateDashboard();
 
104
                        }
 
105
                }
 
106
        }
 
107
        
 
108
        /**
 
109
         * This method will check to make sure the exported module directory is valid.
 
110
         * If it isn't then the foleders will be created in order to make the directory
 
111
         * valid.
 
112
         */
 
113
        private void validateDirectory() {
 
114
                File folder = new File(getSaveDirectory());
 
115
 
 
116
                if(!folder.exists())
 
117
                        folder.mkdir();
 
118
        }
 
119
        
 
120
        /**
 
121
         * This method will create a new XML Memento used to store all of the meta data
 
122
         * for the module.  This data is all based on what the user selected from the
 
123
         * dialog box.
 
124
         * @param disp The string to display that represents this module.
 
125
         * @param cat The category string representing where this module will be placed
 
126
         * @param desc The description string for this module
 
127
         * @param dataSet The IDataSet that holds the data for this module
 
128
         * @param parser The parer that can obtain the information from the raw output
 
129
         * @param gd GraphData Array specifying all of the information needed to create the selected graphs
 
130
         * @param filters TreeNode containing all of the selected filters for each graph.
 
131
         */
 
132
        private File saveMetaData(String disp, String cat, String desc, IDataSet dataSet, IDataSetParser parser, GraphData[] gd, TreeNode filters, String location) {
 
133
                File meta = null;
 
134
                XMLMemento data = XMLMemento.createWriteRoot(DashboardMetaData.XMLDashboardItem);
 
135
 
 
136
                try {
 
137
                        IMemento child, child2, child3;
 
138
 
 
139
                        data.putString(DashboardMetaData.XMLdDisplay, disp);
 
140
                        data.putString(DashboardMetaData.XMLdCategory, cat);
 
141
                        data.putString(DashboardMetaData.XMLdDescription, desc);
 
142
                        data.putString(DashboardMetaData.XMLdDataset, dataSet.getID());
 
143
                        data.putString(DashboardMetaData.XMLdScript, scriptFileName);
 
144
                        data.putString(DashboardMetaData.XMLdLocation, location);
 
145
                        data.putString(DashboardMetaData.XMLdScriptFileName, scriptFileName);
 
146
                                
 
147
                        
 
148
                        child = data.createChild(DashboardMetaData.XMLParsingExpressions);
 
149
                        String[] cols = dataSet.getTitles();
 
150
                        for(int i=0; i<cols.length; i++) {
 
151
                                child2 = child.createChild(DashboardMetaData.XMLpColumn);
 
152
                                child2.putString(DashboardMetaData.XMLpName, cols[i]);
 
153
                        }
 
154
                        parser.saveXML(child.createChild(DashboardMetaData.XMLpParser));
 
155
 
 
156
                        child = data.createChild(DashboardMetaData.XMLGraphDisplays);
 
157
                        for(int j,i=0; i<gd.length; i++) {
 
158
                                child2 = child.createChild(DashboardMetaData.XMLgGraph);
 
159
                                child2.putString(DashboardMetaData.XMLgId, gd[i].graphID);
 
160
                                child2.putString(DashboardMetaData.XMLgTitle, gd[i].title);
 
161
 
 
162
                                TreeNode treeChild = filters.getChildAt(i);
 
163
                                for(j=0; j<treeChild.getChildCount(); j++) {
 
164
                                        ((IDataSetFilter)(treeChild.getChildAt(j).getData())).writeXML(child2);
 
165
                                }
 
166
                                
 
167
                                child3 = child2.createChild(DashboardMetaData.XMLgSeries);
 
168
                                child3.putString(DashboardMetaData.XMLgAxis, DashboardMetaData.XMLgAxisX);
 
169
                                child3.putInteger(DashboardMetaData.XMLgColumn, gd[i].xSeries);
 
170
                                for(j=0; j<gd[i].ySeries.length; j++) {
 
171
                                        child3 = child2.createChild(DashboardMetaData.XMLgSeries);
 
172
                                        child3.putString(DashboardMetaData.XMLgAxis, DashboardMetaData.XMLgAxisY);
 
173
                                        child3.putInteger(DashboardMetaData.XMLgColumn, gd[i].ySeries[j]);
 
174
                                }
 
175
                        }
 
176
                        
 
177
                        meta = new File(getSaveDirectory() + DashboardModule.metaFileName);
 
178
                        FileWriter writer = new FileWriter(meta);
 
179
                        data.save(writer);
 
180
                        writer.close();
 
181
                } catch(FileNotFoundException fnfe) {
 
182
                        return meta;
 
183
                } catch(Exception e) {
 
184
                        return meta;
 
185
                }
 
186
                return meta;
 
187
        }
 
188
        
 
189
        /**
 
190
         * This method will create the module archive by first zipping the .stp file and the meta data
 
191
         * together.  Then it will compress the .zip file into a .gz file.  The .gz file's extension is 
 
192
         * set to .dash to discurage users from trying to modify it and to make it sepecific to the 
 
193
         * SystemTapGUI dashboard.
 
194
         * @param archiveName The name to use for the file containing the new module data.
 
195
         * @param script The file representing the .stp script file to use for the module
 
196
         * @param meta The XML Memento file representing the module details.
 
197
         */
 
198
        private void buildArchive(String archiveName, File script, File meta) {
 
199
                String[] files = new String[] {script.getAbsolutePath(), meta.getAbsolutePath()};
 
200
                String[] names = new String[] {scriptFileName, DashboardModule.metaFileName};
 
201
                
 
202
                ZipArchive.zipFiles(archiveName, files, names);
 
203
                ZipArchive.compressFile(archiveName + DashboardModuleFileFilter.DashboardModuleExtension, archiveName);
 
204
        }
 
205
        
 
206
        /**
 
207
         * This method will delete any extra files that were generated as a result of building
 
208
         * the Dashboard module.
 
209
         * @param files A list of all of the file paths that should be removed
 
210
         */
 
211
        private void cleanupFiles(String[] files) {
 
212
                if(null == files)
 
213
                        return;
 
214
                
 
215
                File f;
 
216
                for(int i=0; i<files.length; i++) {
 
217
                        f = new File(files[i]);
 
218
                        if(f.exists())
 
219
                                f.delete();
 
220
                }
 
221
        }
 
222
        
 
223
        /**
 
224
         * This method will get the directory name that should be used for saving the dashboard modules.
 
225
         */
 
226
        private String getSaveDirectory() {
 
227
                return SystemTapGUISettings.settingsFolder + "/dashboard";
 
228
        }
 
229
        
 
230
        /**
 
231
         * This method forces the Dashboard's DashboardModuleBrowserView to refresh itself to ensure
 
232
         * that is contains the most up-to-date module list.
 
233
         */
 
234
        private void updateDashboard() {
 
235
                try {
 
236
                        IWorkbenchPage p = PlatformUI.getWorkbench().showPerspective(DashboardPerspective.ID, PlatformUI.getWorkbench().getActiveWorkbenchWindow());
 
237
                        IViewPart ivp = p.findView(DashboardModuleBrowserView.ID);
 
238
                        ((DashboardModuleBrowserView)ivp).refresh();
 
239
 
 
240
                        p = PlatformUI.getWorkbench().showPerspective(IDEPerspective.ID, PlatformUI.getWorkbench().getActiveWorkbenchWindow());
 
241
                } catch(WorkbenchException we) {}
 
242
        }
 
243
        
 
244
        /**
 
245
         * Removes all internal references to objects.  No other method should be called after this.
 
246
         */
 
247
        public void dispose() {
 
248
                super.dispose();
 
249
        }
 
250
}