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
CommitLineData
fc9ff6c4
BH
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 *******************************************************************************/
12package org.eclipse.tracecompass.internal.lttng2.control.ui.views.preferences;
13
14import java.io.File;
15import java.io.IOException;
16import java.nio.file.FileSystems;
17import java.nio.file.Files;
18import java.nio.file.Path;
19import java.nio.file.StandardCopyOption;
20
21import org.eclipse.jface.dialogs.MessageDialog;
22import org.eclipse.jface.preference.PreferencePage;
23import org.eclipse.jface.viewers.CheckStateChangedEvent;
24import org.eclipse.jface.viewers.CheckboxTreeViewer;
25import org.eclipse.jface.viewers.ICheckStateListener;
26import org.eclipse.jface.viewers.TreeViewer;
27import org.eclipse.osgi.util.NLS;
28import org.eclipse.swt.SWT;
29import org.eclipse.swt.events.SelectionAdapter;
30import org.eclipse.swt.events.SelectionEvent;
31import org.eclipse.swt.events.SelectionListener;
32import org.eclipse.swt.layout.GridData;
33import org.eclipse.swt.layout.GridLayout;
34import org.eclipse.swt.widgets.Button;
35import org.eclipse.swt.widgets.Composite;
36import org.eclipse.swt.widgets.Control;
37import org.eclipse.swt.widgets.DirectoryDialog;
38import org.eclipse.swt.widgets.Display;
39import org.eclipse.swt.widgets.FileDialog;
40import org.eclipse.tracecompass.internal.lttng2.control.core.LttngProfileManager;
41import org.eclipse.tracecompass.internal.lttng2.control.ui.views.messages.Messages;
674c702f 42import org.eclipse.tracecompass.tmf.ui.dialog.TmfFileDialogFactory;
fc9ff6c4
BH
43import org.eclipse.ui.IWorkbench;
44import org.eclipse.ui.IWorkbenchPreferencePage;
45import org.eclipse.ui.dialogs.FilteredTree;
46import org.eclipse.ui.dialogs.PatternFilter;
47
48/**
49 * LTTng control remote profile preferences page.
50 *
51 * @author Bernd Hufmann
52 */
53public class ControlRemoteProfilesPreferencePage extends PreferencePage implements IWorkbenchPreferencePage {
54
55 /** Preference page ID */
7cd2b0a0 56 public static final String ID = "org.eclipse.tracecompass.internal.lttng2.control.ui.views.preferences.ControlRemoteProfilesPreferencePage"; //$NON-NLS-1$
fc9ff6c4
BH
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) {
674c702f 150 FileDialog dialog = TmfFileDialogFactory.create(Display.getCurrent().getActiveShell(), SWT.OPEN);
fc9ff6c4
BH
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.044382 seconds and 5 git commands to generate.