90244ee3c9871d94403543ba6fec50ceef14f2d6
[deliverable/tracecompass.git] / lttng / org.eclipse.tracecompass.lttng2.control.ui / src / org / eclipse / tracecompass / internal / lttng2 / control / ui / views / preferences / ControlRemoteProfilesPreferencePage.java
1 /*******************************************************************************
2 * Copyright (c) 2015 Ericsson
3 *
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
8 *
9 * Contributors:
10 * Bernd Hufmann - Initial API and implementation
11 *******************************************************************************/
12 package org.eclipse.tracecompass.internal.lttng2.control.ui.views.preferences;
13
14 import java.io.File;
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;
20
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;
46
47 /**
48 * LTTng control remote profile preferences page.
49 *
50 * @author Bernd Hufmann
51 */
52 public class ControlRemoteProfilesPreferencePage extends PreferencePage implements IWorkbenchPreferencePage {
53
54 /** Preference page ID */
55 public static final String ID = "org.eclipse.tracecompass.internal.lttng2.control.ui.views.preferences.ControlRemoteProfilesPreferencePage"; //$NON-NLS-1$
56
57 private CheckboxTreeViewer fFolderViewer;
58
59 private Button fDeleteButton = null;
60 private Button fImportButton = null;
61 private Button fExportButton = null;
62
63 @Override
64 public void init(IWorkbench workbench) {
65 }
66
67 @Override
68 protected Control createContents(Composite parent) {
69 Composite composite;
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));
73
74 final FilteredTree filteredTree = new FilteredTree(composite,
75 SWT.MULTI | SWT.FULL_SELECTION | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER, new PatternFilter(), true) {
76 @Override
77 protected TreeViewer doCreateTreeViewer(Composite aParent, int style) {
78 fFolderViewer = LTTngProfileViewer.createLTTngProfileViewer(aParent, style);
79 return fFolderViewer;
80 }
81 };
82
83 filteredTree.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
84
85 Composite buttonComposite = createVerticalButtonBar(composite);
86 buttonComposite.setLayout(new GridLayout());
87 buttonComposite.setLayoutData(new GridData(GridData.CENTER, GridData.BEGINNING, false, false));
88
89 fFolderViewer.addCheckStateListener(new ICheckStateListener() {
90 @Override
91 public void checkStateChanged(CheckStateChangedEvent event) {
92 enableButtons();
93 }
94 });
95
96 return composite;
97 }
98
99 private Composite createVerticalButtonBar(Composite composite) {
100 Composite buttonComposite = new Composite(composite, SWT.NONE);
101
102 fDeleteButton = createVerticalButton(buttonComposite, Messages.TraceControl_DeleteButtonText);
103 fDeleteButton.addSelectionListener(new SelectionAdapter() {
104 @Override
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$
111 }
112 }
113
114 boolean delete = MessageDialog.openConfirm(getShell(),
115 Messages.TraceControl_DeleteProfileTitle,
116 Messages.TraceControl_DeleteQuery+ "\n" + files.toString()); //$NON-NLS-1$
117
118 if (!delete) {
119 return;
120 }
121
122 for (Object element : checkedElements) {
123 if (element instanceof File) {
124 File sourceFile = (File) element;
125 Path source = FileSystems.getDefault().getPath(sourceFile.getAbsolutePath());
126 try {
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$
132 }
133 }
134 }
135 fFolderViewer.setInput(LTTngProfileViewer.getViewerInput());
136 enableButtons();
137 }
138 });
139
140 fImportButton = createVerticalButton(buttonComposite, Messages.TraceControl_ImportButtonText);
141 fExportButton = createVerticalButton(buttonComposite, Messages.TraceControl_ExportButtonText);
142
143 fImportButton.addSelectionListener(new SelectionListener() {
144 @Override
145 public void widgetDefaultSelected(SelectionEvent e) {}
146
147 @Override
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());
158 }
159 }
160 });
161
162 fExportButton.addSelectionListener(new SelectionAdapter() {
163 @Override
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();
168 if (path != null) {
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);
176 }
177 }
178 }
179 }
180 });
181
182 enableButtons();
183 return buttonComposite;
184 }
185
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);
190 return button;
191 }
192
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);
199 }
200
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()));
207
208 if (!overwrite) {
209 return;
210 }
211 }
212 try {
213 Files.copy(source, destFile, StandardCopyOption.REPLACE_EXISTING);
214 } catch (IOException e1) {
215 MessageDialog.openError(getShell(),
216 errorTitle,
217 "Error copying profile:\n" + e1.toString()); //$NON-NLS-1$
218 }
219 }
220
221 }
This page took 0.036294 seconds and 4 git commands to generate.