1 /*******************************************************************************
2 * Copyright (c) 2015 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 * Bernd Hufmann - Initial API and implementation
11 *******************************************************************************/
12 package org
.eclipse
.tracecompass
.internal
.lttng2
.control
.ui
.views
.preferences
;
15 import java
.io
.IOException
;
16 import java
.nio
.file
.FileSystems
;
17 import java
.nio
.file
.Files
;
18 import java
.nio
.file
.Path
;
19 import java
.nio
.file
.StandardCopyOption
;
21 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
22 import org
.eclipse
.jface
.preference
.PreferencePage
;
23 import org
.eclipse
.jface
.viewers
.CheckStateChangedEvent
;
24 import org
.eclipse
.jface
.viewers
.CheckboxTreeViewer
;
25 import org
.eclipse
.jface
.viewers
.ICheckStateListener
;
26 import org
.eclipse
.jface
.viewers
.TreeViewer
;
27 import org
.eclipse
.osgi
.util
.NLS
;
28 import org
.eclipse
.swt
.SWT
;
29 import org
.eclipse
.swt
.events
.SelectionAdapter
;
30 import org
.eclipse
.swt
.events
.SelectionEvent
;
31 import org
.eclipse
.swt
.events
.SelectionListener
;
32 import org
.eclipse
.swt
.layout
.GridData
;
33 import org
.eclipse
.swt
.layout
.GridLayout
;
34 import org
.eclipse
.swt
.widgets
.Button
;
35 import org
.eclipse
.swt
.widgets
.Composite
;
36 import org
.eclipse
.swt
.widgets
.Control
;
37 import org
.eclipse
.swt
.widgets
.DirectoryDialog
;
38 import org
.eclipse
.swt
.widgets
.Display
;
39 import org
.eclipse
.swt
.widgets
.FileDialog
;
40 import org
.eclipse
.tracecompass
.internal
.lttng2
.control
.core
.LttngProfileManager
;
41 import org
.eclipse
.tracecompass
.internal
.lttng2
.control
.ui
.views
.messages
.Messages
;
42 import org
.eclipse
.ui
.IWorkbench
;
43 import org
.eclipse
.ui
.IWorkbenchPreferencePage
;
44 import org
.eclipse
.ui
.dialogs
.FilteredTree
;
45 import org
.eclipse
.ui
.dialogs
.PatternFilter
;
48 * LTTng control remote profile preferences page.
50 * @author Bernd Hufmann
52 public class ControlRemoteProfilesPreferencePage
extends PreferencePage
implements IWorkbenchPreferencePage
{
54 /** Preference page ID */
55 public static String ID
= "org.eclipse.tracecompass.internal.lttng2.control.ui.views.preferences.ControlRemoteProfilesPreferencePage"; //$NON-NLS-1$
57 private CheckboxTreeViewer fFolderViewer
;
59 private Button fDeleteButton
= null;
60 private Button fImportButton
= null;
61 private Button fExportButton
= null;
64 public void init(IWorkbench workbench
) {
68 protected Control
createContents(Composite parent
) {
70 composite
= new Composite(parent
, SWT
.NONE
);
71 composite
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
72 composite
.setLayout(new GridLayout(2, false));
74 final FilteredTree filteredTree
= new FilteredTree(composite
,
75 SWT
.MULTI
| SWT
.FULL_SELECTION
| SWT
.H_SCROLL
| SWT
.V_SCROLL
| SWT
.BORDER
, new PatternFilter(), true) {
77 protected TreeViewer
doCreateTreeViewer(Composite aParent
, int style
) {
78 fFolderViewer
= LTTngProfileViewer
.createLTTngProfileViewer(aParent
, style
);
83 filteredTree
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, true));
85 Composite buttonComposite
= createVerticalButtonBar(composite
);
86 buttonComposite
.setLayout(new GridLayout());
87 buttonComposite
.setLayoutData(new GridData(GridData
.CENTER
, GridData
.BEGINNING
, false, false));
89 fFolderViewer
.addCheckStateListener(new ICheckStateListener() {
91 public void checkStateChanged(CheckStateChangedEvent event
) {
99 private Composite
createVerticalButtonBar(Composite composite
) {
100 Composite buttonComposite
= new Composite(composite
, SWT
.NONE
);
102 fDeleteButton
= createVerticalButton(buttonComposite
, Messages
.TraceControl_DeleteButtonText
);
103 fDeleteButton
.addSelectionListener(new SelectionAdapter() {
105 public void widgetSelected(SelectionEvent e
) {
106 Object
[] checkedElements
= fFolderViewer
.getCheckedElements();
107 StringBuffer files
= new StringBuffer();
108 for (Object element
: checkedElements
) {
109 if (element
instanceof File
) {
110 files
.append(((File
) element
).toString()).append("\n"); //$NON-NLS-1$
114 boolean delete
= MessageDialog
.openConfirm(getShell(),
115 Messages
.TraceControl_DeleteProfileTitle
,
116 Messages
.TraceControl_DeleteQuery
+ "\n" + files
.toString()); //$NON-NLS-1$
122 for (Object element
: checkedElements
) {
123 if (element
instanceof File
) {
124 File sourceFile
= (File
) element
;
125 Path source
= FileSystems
.getDefault().getPath(sourceFile
.getAbsolutePath());
127 Files
.delete(source
);
128 } catch (IOException e1
) {
129 MessageDialog
.openError(getShell(),
130 Messages
.TraceControl_DeleteProfileTitle
,
131 "Error deleting profile:\n" + e1
.toString()); //$NON-NLS-1$
135 fFolderViewer
.setInput(LTTngProfileViewer
.getViewerInput());
140 fImportButton
= createVerticalButton(buttonComposite
, Messages
.TraceControl_ImportButtonText
);
141 fExportButton
= createVerticalButton(buttonComposite
, Messages
.TraceControl_ExportButtonText
);
143 fImportButton
.addSelectionListener(new SelectionListener() {
145 public void widgetDefaultSelected(SelectionEvent e
) {}
148 public void widgetSelected(SelectionEvent e
) {
149 FileDialog dialog
= new FileDialog(Display
.getCurrent().getActiveShell(), SWT
.OPEN
);
150 dialog
.setText(Messages
.TraceControl_ImportProfileTitle
);
151 dialog
.setFilterExtensions(new String
[] { "*.lttng", "*" }); //$NON-NLS-1$ //$NON-NLS-2$
152 String sourceFile
= dialog
.open();
153 if (sourceFile
!= null) {
154 Path source
= FileSystems
.getDefault().getPath(sourceFile
);
155 Path destPath
= FileSystems
.getDefault().getPath(LttngProfileManager
.getProfilePath().toFile().toString());
156 copyProfileFile(source
, destPath
, Messages
.TraceControl_ImportProfileTitle
);
157 fFolderViewer
.setInput(LTTngProfileViewer
.getViewerInput());
162 fExportButton
.addSelectionListener(new SelectionAdapter() {
164 public void widgetSelected(SelectionEvent e
) {
165 DirectoryDialog dialog
= new DirectoryDialog(Display
.getCurrent().getActiveShell());
166 dialog
.setText(Messages
.TraceControl_ExportProfileTitle
);
167 String path
= dialog
.open();
169 Object
[] checkedElements
= fFolderViewer
.getCheckedElements();
170 for (Object element
: checkedElements
) {
171 if (element
instanceof File
) {
172 File sourceFile
= (File
) element
;
173 Path source
= FileSystems
.getDefault().getPath(sourceFile
.getAbsolutePath());
174 Path destPath
= FileSystems
.getDefault().getPath(path
);
175 copyProfileFile(source
, destPath
, Messages
.TraceControl_ExportProfileTitle
);
183 return buttonComposite
;
186 private static Button
createVerticalButton(Composite parent
, String text
) {
187 Button button
= new Button(parent
, SWT
.PUSH
);
188 button
.setLayoutData(new GridData(GridData
.HORIZONTAL_ALIGN_FILL
));
189 button
.setText(text
);
193 private void enableButtons() {
194 Object
[] checked
= fFolderViewer
.getCheckedElements();
195 boolean enabled
= (checked
!= null) && (checked
.length
> 0);
196 fDeleteButton
.setEnabled(enabled
);
197 fExportButton
.setEnabled(enabled
);
198 fImportButton
.setEnabled(true);
201 private void copyProfileFile(Path source
, Path destPath
, String errorTitle
) {
202 Path destFile
= destPath
.resolve(source
.getFileName());
203 if (destFile
.toFile().exists()) {
204 boolean overwrite
= MessageDialog
.openConfirm(getShell(),
205 Messages
.TraceControl_ProfileAlreadyExists
,
206 NLS
.bind(Messages
.TraceControl_OverwriteQuery
, destFile
.getFileName()));
213 Files
.copy(source
, destFile
, StandardCopyOption
.REPLACE_EXISTING
);
214 } catch (IOException e1
) {
215 MessageDialog
.openError(getShell(),
217 "Error copying profile:\n" + e1
.toString()); //$NON-NLS-1$