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