1 /*******************************************************************************
2 * Copyright (c) 2013 Ericsson
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * Marc-Andre Laperle - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.internal
.tmf
.ui
.project
.wizards
.tracepkg
.importexport
;
16 import java
.lang
.reflect
.InvocationTargetException
;
17 import java
.text
.MessageFormat
;
18 import java
.util
.ArrayList
;
19 import java
.util
.List
;
21 import org
.eclipse
.core
.resources
.IFile
;
22 import org
.eclipse
.core
.resources
.IMarker
;
23 import org
.eclipse
.core
.resources
.IResource
;
24 import org
.eclipse
.core
.runtime
.CoreException
;
25 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
26 import org
.eclipse
.core
.runtime
.IStatus
;
27 import org
.eclipse
.jface
.dialogs
.IDialogSettings
;
28 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
29 import org
.eclipse
.jface
.operation
.IRunnableWithProgress
;
30 import org
.eclipse
.jface
.viewers
.CheckboxTreeViewer
;
31 import org
.eclipse
.jface
.viewers
.ColumnLabelProvider
;
32 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
33 import org
.eclipse
.jface
.viewers
.TreeViewerColumn
;
34 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.Activator
;
35 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.project
.wizards
.tracepkg
.AbstractTracePackageWizardPage
;
36 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.project
.wizards
.tracepkg
.TracePackageBookmarkElement
;
37 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.project
.wizards
.tracepkg
.TracePackageElement
;
38 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.project
.wizards
.tracepkg
.TracePackageFilesElement
;
39 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.project
.wizards
.tracepkg
.TracePackageLabelProvider
;
40 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.project
.wizards
.tracepkg
.TracePackageSupplFileElement
;
41 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.project
.wizards
.tracepkg
.TracePackageSupplFilesElement
;
42 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.project
.wizards
.tracepkg
.TracePackageTraceElement
;
43 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfTraceElement
;
44 import org
.eclipse
.swt
.SWT
;
45 import org
.eclipse
.swt
.events
.ModifyEvent
;
46 import org
.eclipse
.swt
.events
.ModifyListener
;
47 import org
.eclipse
.swt
.events
.SelectionAdapter
;
48 import org
.eclipse
.swt
.events
.SelectionEvent
;
49 import org
.eclipse
.swt
.layout
.GridData
;
50 import org
.eclipse
.swt
.layout
.GridLayout
;
51 import org
.eclipse
.swt
.layout
.RowLayout
;
52 import org
.eclipse
.swt
.widgets
.Button
;
53 import org
.eclipse
.swt
.widgets
.Composite
;
54 import org
.eclipse
.swt
.widgets
.Group
;
55 import org
.eclipse
.swt
.widgets
.Label
;
58 * Wizard page for the export trace package wizard
60 * @author Marc-Andre Laperle
62 public class ExportTracePackageWizardPage
extends AbstractTracePackageWizardPage
{
64 private static final int CONTENT_COL_WIDTH
= 300;
65 private static final int SIZE_COL_WIDTH
= 100;
67 private static final String ZIP_EXTENSION
= ".zip"; //$NON-NLS-1$
68 private static final String TAR_EXTENSION
= ".tar"; //$NON-NLS-1$
69 private static final String TAR_GZ_EXTENSION
= ".tar.gz"; //$NON-NLS-1$
70 private static final String TGZ_EXTENSION
= ".tgz"; //$NON-NLS-1$
72 private static final String ICON_PATH
= "icons/wizban/export_wiz.png"; //$NON-NLS-1$
75 * The page name, can be referenced from other pages
77 public static final String PAGE_NAME
= "ExportTracePackageWizardPage"; //$NON-NLS-1$
78 // dialog store id constants
79 private static final String STORE_COMPRESS_CONTENTS_ID
= PAGE_NAME
+ ".STORE_COMPRESS_CONTENTS_ID"; //$NON-NLS-1$
80 private static final String STORE_FORMAT_ID
= PAGE_NAME
+ ".STORE_FORMAT_ID"; //$NON-NLS-1$
82 private Button fCompressContentsCheckbox
;
83 private Button fZipFormatButton
;
84 private Button fTargzFormatButton
;
85 private Label fApproximateSizeLabel
;
86 private List
<TmfTraceElement
> fSelectedTraces
;
89 * Constructor for the export trace package wizard page
92 * the current object selection
94 public ExportTracePackageWizardPage(IStructuredSelection selection
) {
95 super(PAGE_NAME
, Messages
.ExportTracePackageWizardPage_Title
, Activator
.getDefault().getImageDescripterFromPath(ICON_PATH
), selection
);
97 Object
[] selectedElements
= getSelection().toArray();
98 fSelectedTraces
= new ArrayList
<TmfTraceElement
>();
99 for (Object selectedElement
: selectedElements
) {
100 if (selectedElement
instanceof TmfTraceElement
) {
101 fSelectedTraces
.add(((TmfTraceElement
) selectedElement
).getElementUnderTraceFolder());
107 * Set the selected trace from the previous page to be displayed in the
110 * @param selectedTraces
113 public void setSelectedTraces(List
<TmfTraceElement
> selectedTraces
) {
114 if (!fSelectedTraces
.containsAll(selectedTraces
) || !selectedTraces
.containsAll(fSelectedTraces
)) {
115 fSelectedTraces
= selectedTraces
;
116 CheckboxTreeViewer elementViewer
= getElementViewer();
117 elementViewer
.setInput(createElementViewerInput());
118 elementViewer
.expandToLevel(2);
119 setAllChecked(elementViewer
, false, true);
120 updateApproximateSelectedSize();
125 public void createControl(Composite parent
) {
127 initializeDialogUnits(parent
);
129 Composite composite
= new Composite(parent
, SWT
.NULL
);
130 composite
.setLayout(new GridLayout());
131 composite
.setLayoutData(new GridData(GridData
.VERTICAL_ALIGN_FILL
| GridData
.HORIZONTAL_ALIGN_FILL
));
133 createElementViewer(composite
);
134 createButtonsGroup(composite
);
135 createFilePathGroup(composite
, Messages
.ExportTracePackageWizardPage_ToArchive
, SWT
.SAVE
);
136 createOptionsGroup(composite
);
138 restoreWidgetValues();
139 setMessage(Messages
.ExportTracePackageWizardPage_ChooseContent
);
141 updateApproximateSelectedSize();
142 updatePageCompletion();
144 setControl(composite
);
148 * Restore widget values to the values that they held last time this wizard
149 * was used to completion.
152 protected void restoreWidgetValues() {
153 super.restoreWidgetValues();
155 IDialogSettings settings
= getDialogSettings();
156 if (settings
!= null) {
157 if (getFilePathCombo().getItemCount() > 0) {
158 String item
= getFilePathCombo().getItem(0);
159 setFilePathValue(item
);
161 fCompressContentsCheckbox
.setSelection(settings
.getBoolean(STORE_COMPRESS_CONTENTS_ID
));
162 fZipFormatButton
.setSelection(settings
.getBoolean(STORE_FORMAT_ID
));
163 fTargzFormatButton
.setSelection(!settings
.getBoolean(STORE_FORMAT_ID
));
164 updateWithFilePathSelection();
169 protected void createFilePathGroup(Composite parent
, String label
, int fileDialogStyle
) {
170 super.createFilePathGroup(parent
, label
, fileDialogStyle
);
172 getFilePathCombo().addModifyListener(new ModifyListener() {
174 public void modifyText(ModifyEvent e
) {
175 updatePageCompletion();
180 private void createOptionsGroup(Composite parent
) {
181 Group optionsGroup
= new Group(parent
, SWT
.NONE
);
182 optionsGroup
.setLayout(new RowLayout(SWT
.VERTICAL
));
183 optionsGroup
.setLayoutData(new GridData(GridData
.HORIZONTAL_ALIGN_FILL
184 | GridData
.GRAB_HORIZONTAL
));
185 optionsGroup
.setText(Messages
.ExportTracePackageWizardPage_Options
);
187 SelectionAdapter listener
= new SelectionAdapter() {
189 public void widgetSelected(SelectionEvent e
) {
190 updateWithFilePathSelection();
194 fZipFormatButton
= new Button(optionsGroup
, SWT
.RADIO
| SWT
.LEFT
);
195 fZipFormatButton
.setText(Messages
.ExportTracePackageWizardPage_SaveInZipFormat
);
196 fZipFormatButton
.setSelection(true);
197 fZipFormatButton
.addSelectionListener(listener
);
199 fTargzFormatButton
= new Button(optionsGroup
, SWT
.RADIO
| SWT
.LEFT
);
200 fTargzFormatButton
.setText(Messages
.ExportTracePackageWizardPage_SaveInTarFormat
);
201 fTargzFormatButton
.setSelection(false);
202 fTargzFormatButton
.addSelectionListener(listener
);
204 fCompressContentsCheckbox
= new Button(optionsGroup
, SWT
.CHECK
| SWT
.LEFT
);
205 fCompressContentsCheckbox
.setText(Messages
.ExportTracePackageWizardPage_CompressContents
);
206 fCompressContentsCheckbox
.setSelection(true);
207 fCompressContentsCheckbox
.addSelectionListener(listener
);
211 protected void createElementViewer(Composite parent
) {
212 super.createElementViewer(parent
);
214 CheckboxTreeViewer elementViewer
= getElementViewer();
215 elementViewer
.getTree().setHeaderVisible(true);
217 TreeViewerColumn column
= new TreeViewerColumn(elementViewer
, SWT
.NONE
);
218 column
.getColumn().setWidth(CONTENT_COL_WIDTH
);
219 column
.getColumn().setText(Messages
.ExportTracePackageWizardPage_ContentColumnName
);
220 column
.setLabelProvider(new TracePackageLabelProvider());
223 column
= new TreeViewerColumn(elementViewer
, SWT
.NONE
);
224 column
.getColumn().setWidth(SIZE_COL_WIDTH
);
225 column
.getColumn().setText(Messages
.ExportTracePackageWizardPage_SizeColumnName
);
226 column
.setLabelProvider(new ColumnLabelProvider() {
228 public String
getText(Object element
) {
229 TracePackageElement tracePackageElement
= (TracePackageElement
) element
;
230 long size
= tracePackageElement
.getSize(false);
235 TracePackageElement curElement
= tracePackageElement
.getParent();
236 while (curElement
!= null) {
237 curElement
= curElement
.getParent();
241 return indent(getHumanReadable(size
), level
);
244 private String
indent(String humanReadable
, int level
) {
245 StringBuilder s
= new StringBuilder(humanReadable
);
246 for (int i
= 0; i
< level
; ++i
) {
247 final String indentStr
= " "; //$NON-NLS-1$
248 s
.insert(0, indentStr
);
254 elementViewer
.setInput(createElementViewerInput());
255 elementViewer
.expandToLevel(2);
256 setAllChecked(elementViewer
, false, true);
260 protected void updateApproximateSelectedSize() {
261 long checkedSize
= 0;
262 TracePackageElement
[] tracePackageElements
= (TracePackageElement
[]) getElementViewer().getInput();
263 for (TracePackageElement element
: tracePackageElements
) {
264 checkedSize
+= element
.getSize(true);
266 checkedSize
= Math
.max(0, checkedSize
);
267 fApproximateSizeLabel
.setText(MessageFormat
.format(Messages
.ExportTracePackageWizardPage_ApproximateSizeLbl
, getHumanReadable(checkedSize
)));
271 * Get the human readable string for a size in bytes. (KB, MB, etc).
274 * the size to print in human readable,
275 * @return the human readable string
277 private static String
getHumanReadable(long size
) {
278 String humanSuffix
[] = { Messages
.ExportTracePackageWizardPage_SizeByte
, Messages
.ExportTracePackageWizardPage_SizeKilobyte
,
279 Messages
.ExportTracePackageWizardPage_SizeMegabyte
, Messages
.ExportTracePackageWizardPage_SizeGigabyte
,
280 Messages
.ExportTracePackageWizardPage_SizeTerabyte
};
284 while (curSize
>= 1024) {
289 return Long
.toString(curSize
) + " " + humanSuffix
[suffixIndex
]; //$NON-NLS-1$
293 protected Object
createElementViewerInput() {
294 List
<TracePackageTraceElement
> traceElements
= new ArrayList
<TracePackageTraceElement
>();
295 for (TmfTraceElement tmfTraceElement
: fSelectedTraces
) {
296 TracePackageTraceElement traceElement
= new TracePackageTraceElement(null, tmfTraceElement
);
299 List
<TracePackageElement
> children
= new ArrayList
<TracePackageElement
>();
300 TracePackageFilesElement filesElement
= new TracePackageFilesElement(traceElement
, tmfTraceElement
.getResource());
301 filesElement
.setChecked(true);
302 // Always export the files
303 filesElement
.setEnabled(false);
304 children
.add(filesElement
);
306 // Supplementary files
307 IResource
[] supplementaryResources
= tmfTraceElement
.getSupplementaryResources();
308 List
<TracePackageElement
> suppFilesChildren
= new ArrayList
<TracePackageElement
>();
309 TracePackageSupplFilesElement suppFilesElement
= new TracePackageSupplFilesElement(traceElement
);
310 children
.add(suppFilesElement
);
311 for (IResource res
: supplementaryResources
) {
312 suppFilesChildren
.add(new TracePackageSupplFileElement(res
, suppFilesElement
));
314 suppFilesElement
.setChildren(suppFilesChildren
.toArray(new TracePackageElement
[] {}));
317 IFile bookmarksFile
= tmfTraceElement
.getBookmarksFile();
318 if (bookmarksFile
!= null && bookmarksFile
.exists()) {
319 IMarker
[] findMarkers
;
321 findMarkers
= bookmarksFile
.findMarkers(IMarker
.BOOKMARK
, false, IResource
.DEPTH_ZERO
);
322 if (findMarkers
.length
> 0) {
323 children
.add(new TracePackageBookmarkElement(traceElement
, null));
325 } catch (CoreException e
) {
326 // Should not happen since we just checked bookmarksFile.exists() but log it just in case
327 Activator
.getDefault().logError("Error finding bookmarks", e
); //$NON-NLS-1$
331 traceElement
.setChildren(children
.toArray(new TracePackageElement
[] {}));
333 traceElements
.add(traceElement
);
337 return traceElements
.toArray(new TracePackageTraceElement
[] {});
341 protected final Composite
createButtonsGroup(Composite parent
) {
342 Composite buttonGroup
= super.createButtonsGroup(parent
);
344 // Add the label on the same row of the select/deselect all buttons
345 fApproximateSizeLabel
= new Label(buttonGroup
, SWT
.RIGHT
);
346 GridData layoutData
= new GridData(GridData
.FILL_HORIZONTAL
);
347 layoutData
.grabExcessHorizontalSpace
= true;
348 fApproximateSizeLabel
.setLayoutData(layoutData
);
354 * Save widget values to Dialog settings
357 protected void saveWidgetValues() {
358 super.saveWidgetValues();
360 IDialogSettings settings
= getDialogSettings();
361 if (settings
!= null) {
362 settings
.put(STORE_COMPRESS_CONTENTS_ID
, fCompressContentsCheckbox
.getSelection());
363 settings
.put(STORE_FORMAT_ID
, fZipFormatButton
.getSelection());
367 private String
getOutputExtension() {
368 if (fZipFormatButton
.getSelection()) {
369 return ZIP_EXTENSION
;
370 } else if (fCompressContentsCheckbox
.getSelection()) {
371 return TAR_GZ_EXTENSION
;
373 return TAR_EXTENSION
;
378 protected void updateWithFilePathSelection() {
379 String filePathValue
= getFilePathValue();
380 if (filePathValue
.isEmpty()) {
384 filePathValue
= stripKnownExtension(filePathValue
);
385 filePathValue
= filePathValue
.concat(getOutputExtension());
387 setFilePathValue(filePathValue
);
390 private static String
stripKnownExtension(String str
) {
392 if (str
.endsWith(TAR_GZ_EXTENSION
)) {
393 ret
= ret
.substring(0, ret
.lastIndexOf(".")); //$NON-NLS-1$
396 if (ret
.endsWith(ZIP_EXTENSION
) | ret
.endsWith(TAR_EXTENSION
) | ret
.endsWith(TGZ_EXTENSION
)) {
397 ret
= ret
.substring(0, ret
.lastIndexOf(".")); //$NON-NLS-1$
404 * Finish the wizard page
406 * @return true on success
408 public boolean finish() {
409 if (!checkForOverwrite()) {
415 TracePackageTraceElement
[] traceExportElements
= (TracePackageTraceElement
[]) getElementViewer().getInput();
416 boolean useCompression
= fCompressContentsCheckbox
.getSelection();
417 boolean useTar
= fTargzFormatButton
.getSelection();
418 String fileName
= getFilePathValue();
419 final TracePackageExportOperation exporter
= new TracePackageExportOperation(traceExportElements
, useCompression
, useTar
, fileName
);
422 getContainer().run(true, true, new IRunnableWithProgress() {
425 public void run(IProgressMonitor monitor
) throws InvocationTargetException
, InterruptedException
{
426 exporter
.run(monitor
);
430 IStatus status
= exporter
.getStatus();
431 if (status
.getSeverity() == IStatus
.ERROR
) {
432 handleErrorStatus(status
);
435 } catch (InvocationTargetException e
) {
436 handleError(org
.eclipse
.linuxtools
.internal
.tmf
.ui
.project
.wizards
.tracepkg
.Messages
.TracePackage_ErrorOperation
, e
);
437 } catch (InterruptedException e
) {
440 return exporter
.getStatus().getSeverity() == IStatus
.OK
;
443 private boolean checkForOverwrite() {
444 File file
= new File(getFilePathValue());
446 return MessageDialog
.openQuestion(getContainer().getShell(), null, Messages
.ExportTracePackageWizardPage_AlreadyExitst
);