tmf.ui: Introduce TmfFileDialogFactory
[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.tracecompass.tmf.ui.dialog.TmfFileDialogFactory;
43 import org.eclipse.ui.IWorkbench;
44 import org.eclipse.ui.IWorkbenchPreferencePage;
45 import org.eclipse.ui.dialogs.FilteredTree;
46 import org.eclipse.ui.dialogs.PatternFilter;
47
48 /**
49 * LTTng control remote profile preferences page.
50 *
51 * @author Bernd Hufmann
52 */
53 public class ControlRemoteProfilesPreferencePage extends PreferencePage implements IWorkbenchPreferencePage {
54
55 /** Preference page ID */
56 public static final String ID = "org.eclipse.tracecompass.internal.lttng2.control.ui.views.preferences.ControlRemoteProfilesPreferencePage"; //$NON-NLS-1$
57
58 private CheckboxTreeViewer fFolderViewer;
59
60 private Button fDeleteButton = null;
61 private Button fImportButton = null;
62 private Button fExportButton = null;
63
64 @Override
65 public void init(IWorkbench workbench) {
66 }
67
68 @Override
69 protected Control createContents(Composite parent) {
70 Composite composite;
71 composite = new Composite(parent, SWT.NONE);
72 composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
73 composite.setLayout(new GridLayout(2, false));
74
75 final FilteredTree filteredTree = new FilteredTree(composite,
76 SWT.MULTI | SWT.FULL_SELECTION | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER, new PatternFilter(), true) {
77 @Override
78 protected TreeViewer doCreateTreeViewer(Composite aParent, int style) {
79 fFolderViewer = LTTngProfileViewer.createLTTngProfileViewer(aParent, style);
80 return fFolderViewer;
81 }
82 };
83
84 filteredTree.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
85
86 Composite buttonComposite = createVerticalButtonBar(composite);
87 buttonComposite.setLayout(new GridLayout());
88 buttonComposite.setLayoutData(new GridData(GridData.CENTER, GridData.BEGINNING, false, false));
89
90 fFolderViewer.addCheckStateListener(new ICheckStateListener() {
91 @Override
92 public void checkStateChanged(CheckStateChangedEvent event) {
93 enableButtons();
94 }
95 });
96
97 return composite;
98 }
99
100 private Composite createVerticalButtonBar(Composite composite) {
101 Composite buttonComposite = new Composite(composite, SWT.NONE);
102
103 fDeleteButton = createVerticalButton(buttonComposite, Messages.TraceControl_DeleteButtonText);
104 fDeleteButton.addSelectionListener(new SelectionAdapter() {
105 @Override
106 public void widgetSelected(SelectionEvent e) {
107 Object[] checkedElements = fFolderViewer.getCheckedElements();
108 StringBuffer files = new StringBuffer();
109 for (Object element : checkedElements) {
110 if (element instanceof File) {
111 files.append(((File) element).toString()).append("\n"); //$NON-NLS-1$
112 }
113 }
114
115 boolean delete = MessageDialog.openConfirm(getShell(),
116 Messages.TraceControl_DeleteProfileTitle,
117 Messages.TraceControl_DeleteQuery+ "\n" + files.toString()); //$NON-NLS-1$
118
119 if (!delete) {
120 return;
121 }
122
123 for (Object element : checkedElements) {
124 if (element instanceof File) {
125 File sourceFile = (File) element;
126 Path source = FileSystems.getDefault().getPath(sourceFile.getAbsolutePath());
127 try {
128 Files.delete(source);
129 } catch (IOException e1) {
130 MessageDialog.openError(getShell(),
131 Messages.TraceControl_DeleteProfileTitle,
132 "Error deleting profile:\n" + e1.toString()); //$NON-NLS-1$
133 }
134 }
135 }
136 fFolderViewer.setInput(LTTngProfileViewer.getViewerInput());
137 enableButtons();
138 }
139 });
140
141 fImportButton = createVerticalButton(buttonComposite, Messages.TraceControl_ImportButtonText);
142 fExportButton = createVerticalButton(buttonComposite, Messages.TraceControl_ExportButtonText);
143
144 fImportButton.addSelectionListener(new SelectionListener() {
145 @Override
146 public void widgetDefaultSelected(SelectionEvent e) {}
147
148 @Override
149 public void widgetSelected(SelectionEvent e) {
150 FileDialog dialog = TmfFileDialogFactory.create(Display.getCurrent().getActiveShell(), SWT.OPEN);
151 dialog.setText(Messages.TraceControl_ImportProfileTitle);
152 dialog.setFilterExtensions(new String[] { "*.lttng", "*" }); //$NON-NLS-1$ //$NON-NLS-2$
153 String sourceFile = dialog.open();
154 if (sourceFile != null) {
155 Path source = FileSystems.getDefault().getPath(sourceFile);
156 Path destPath = FileSystems.getDefault().getPath(LttngProfileManager.getProfilePath().toFile().toString());
157 copyProfileFile(source, destPath, Messages.TraceControl_ImportProfileTitle);
158 fFolderViewer.setInput(LTTngProfileViewer.getViewerInput());
159 }
160 }
161 });
162
163 fExportButton.addSelectionListener(new SelectionAdapter() {
164 @Override
165 public void widgetSelected(SelectionEvent e) {
166 DirectoryDialog dialog = new DirectoryDialog(Display.getCurrent().getActiveShell());
167 dialog.setText(Messages.TraceControl_ExportProfileTitle);
168 String path = dialog.open();
169 if (path != null) {
170 Object[] checkedElements = fFolderViewer.getCheckedElements();
171 for (Object element : checkedElements) {
172 if (element instanceof File) {
173 File sourceFile = (File) element;
174 Path source = FileSystems.getDefault().getPath(sourceFile.getAbsolutePath());
175 Path destPath = FileSystems.getDefault().getPath(path);
176 copyProfileFile(source, destPath, Messages.TraceControl_ExportProfileTitle);
177 }
178 }
179 }
180 }
181 });
182
183 enableButtons();
184 return buttonComposite;
185 }
186
187 private static Button createVerticalButton(Composite parent, String text) {
188 Button button = new Button(parent, SWT.PUSH);
189 button.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL));
190 button.setText(text);
191 return button;
192 }
193
194 private void enableButtons() {
195 Object[] checked = fFolderViewer.getCheckedElements();
196 boolean enabled = (checked != null) && (checked.length > 0);
197 fDeleteButton.setEnabled(enabled);
198 fExportButton.setEnabled(enabled);
199 fImportButton.setEnabled(true);
200 }
201
202 private void copyProfileFile(Path source, Path destPath, String errorTitle) {
203 Path destFile = destPath.resolve(source.getFileName());
204 if (destFile.toFile().exists()) {
205 boolean overwrite = MessageDialog.openConfirm(getShell(),
206 Messages.TraceControl_ProfileAlreadyExists,
207 NLS.bind(Messages.TraceControl_OverwriteQuery, destFile.getFileName()));
208
209 if (!overwrite) {
210 return;
211 }
212 }
213 try {
214 Files.copy(source, destFile, StandardCopyOption.REPLACE_EXISTING);
215 } catch (IOException e1) {
216 MessageDialog.openError(getShell(),
217 errorTitle,
218 "Error copying profile:\n" + e1.toString()); //$NON-NLS-1$
219 }
220 }
221
222 }
This page took 0.035212 seconds and 5 git commands to generate.