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;
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;
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;
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.
58
public class CreateModuleAction extends Action implements IWorkbenchWindowActionDelegate {
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
66
//private static String scriptFileName = "/script.stp";
67
public String script = null;
69
public void init(IWorkbenchWindow window) {
73
protected IWorkbenchWindow fWindow = null;
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);
80
ScriptDetails sd = new ScriptDetails(fWindow.getShell());
82
if (sd.isCanceled()) return;
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);
92
IDataSetParser parser = wizard.getParser();
93
IDataSet dataSet = wizard.getDataSet();
97
if(null == parser || null == dataSet)
100
ExportScriptDialog exportDialog = new ExportScriptDialog(fWindow.getShell(), dataSet);
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();
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()});
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
126
private void validateDirectory() {
127
File folder = new File(getSaveDirectory());
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
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.
145
private File saveMetaData(String disp, String cat, String desc, IDataSet dataSet, IDataSetParser parser, GraphData[] gd, TreeNode filters, String location) {
147
XMLMemento data = XMLMemento.createWriteRoot(DashboardMetaData.XMLDashboardItem);
150
IMemento child, child2, child3;
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);
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]);
167
parser.saveXML(child.createChild(DashboardMetaData.XMLpParser));
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);
175
TreeNode treeChild = filters.getChildAt(i);
176
for(j=0; j<treeChild.getChildCount(); j++) {
177
((IDataSetFilter)(treeChild.getChildAt(j).getData())).writeXML(child2);
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]);
190
meta = new File(getSaveDirectory() + DashboardModule.metaFileName);
191
FileWriter writer = new FileWriter(meta);
194
} catch(FileNotFoundException fnfe) {
196
} catch(Exception e) {
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.
211
private void buildArchive(String archiveName, File script, File meta) {
212
String[] files = new String[] {meta.getAbsolutePath()};
213
String[] names = new String[] {DashboardModule.metaFileName};
215
ZipArchive.zipFiles(archiveName, files, names);
216
ZipArchive.compressFile(archiveName + DashboardModuleFileFilter.DashboardModuleExtension, archiveName);
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
224
private void cleanupFiles(String[] files) {
229
for(int i=0; i<files.length; i++) {
230
f = new File(files[i]);
237
* This method will get the directory name that should be used for saving the dashboard modules.
239
private String getSaveDirectory() {
240
return SystemTapGUISettings.settingsFolder + "/dashboard";
244
* This method forces the Dashboard's DashboardModuleBrowserView to refresh itself to ensure
245
* that is contains the most up-to-date module list.
247
private void updateDashboard() {
249
IWorkbenchPage p = PlatformUI.getWorkbench().showPerspective(DashboardPerspective.ID, PlatformUI.getWorkbench().getActiveWorkbenchWindow());
250
IViewPart ivp = p.findView(DashboardModuleBrowserView.ID);
251
((DashboardModuleBrowserView)ivp).refresh();
253
//p = PlatformUI.getWorkbench().showPerspective(IDEPerspective.ID, PlatformUI.getWorkbench().getActiveWorkbenchWindow());
254
} catch(WorkbenchException we) {}
258
* Removes all internal references to objects. No other method should be called after this.
260
/*public void dispose() {
264
public void selectionChanged(IAction action, ISelection selection) {
265
// TODO Auto-generated method stub
269
public void dispose() {
270
// TODO Auto-generated method stub