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
9
* IBM Corporation - Jeff Briggs, Henry Hughes, Ryan Morse, Anithra P J
10
*******************************************************************************/
12
package org.eclipse.linuxtools.internal.systemtap.ui.dashboardextension.actions;
15
import java.io.FileNotFoundException;
16
import java.io.FileWriter;
17
import java.text.MessageFormat;
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;
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;
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.
56
public class ExportScriptAction extends RunScriptAction implements IWorkbenchWindowActionDelegate {
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
64
private static String scriptFileName = "/script.stp";
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);
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);
79
IDataSetParser parser = wizard.getParser();
80
IDataSet dataSet = wizard.getDataSet();
84
if(null == parser || null == dataSet)
87
ExportScriptDialog exportDialog = new ExportScriptDialog(fWindow.getShell(), dataSet);
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();
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()});
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
113
private void validateDirectory() {
114
File folder = new File(getSaveDirectory());
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
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.
132
private File saveMetaData(String disp, String cat, String desc, IDataSet dataSet, IDataSetParser parser, GraphData[] gd, TreeNode filters, String location) {
134
XMLMemento data = XMLMemento.createWriteRoot(DashboardMetaData.XMLDashboardItem);
137
IMemento child, child2, child3;
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);
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]);
154
parser.saveXML(child.createChild(DashboardMetaData.XMLpParser));
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);
162
TreeNode treeChild = filters.getChildAt(i);
163
for(j=0; j<treeChild.getChildCount(); j++) {
164
((IDataSetFilter)(treeChild.getChildAt(j).getData())).writeXML(child2);
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]);
177
meta = new File(getSaveDirectory() + DashboardModule.metaFileName);
178
FileWriter writer = new FileWriter(meta);
181
} catch(FileNotFoundException fnfe) {
183
} catch(Exception e) {
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.
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};
202
ZipArchive.zipFiles(archiveName, files, names);
203
ZipArchive.compressFile(archiveName + DashboardModuleFileFilter.DashboardModuleExtension, archiveName);
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
211
private void cleanupFiles(String[] files) {
216
for(int i=0; i<files.length; i++) {
217
f = new File(files[i]);
224
* This method will get the directory name that should be used for saving the dashboard modules.
226
private String getSaveDirectory() {
227
return SystemTapGUISettings.settingsFolder + "/dashboard";
231
* This method forces the Dashboard's DashboardModuleBrowserView to refresh itself to ensure
232
* that is contains the most up-to-date module list.
234
private void updateDashboard() {
236
IWorkbenchPage p = PlatformUI.getWorkbench().showPerspective(DashboardPerspective.ID, PlatformUI.getWorkbench().getActiveWorkbenchWindow());
237
IViewPart ivp = p.findView(DashboardModuleBrowserView.ID);
238
((DashboardModuleBrowserView)ivp).refresh();
240
p = PlatformUI.getWorkbench().showPerspective(IDEPerspective.ID, PlatformUI.getWorkbench().getActiveWorkbenchWindow());
241
} catch(WorkbenchException we) {}
245
* Removes all internal references to objects. No other method should be called after this.
247
public void dispose() {