Commit | Line | Data |
---|---|---|
9ba2101a PT |
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 | * Patrick Tasse - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
13 | package org.eclipse.tracecompass.internal.tmf.remote.ui.wizards.fetch.preferences; | |
14 | ||
15 | import java.io.BufferedWriter; | |
16 | import java.io.File; | |
17 | import java.io.FileWriter; | |
18 | import java.io.IOException; | |
19 | import java.net.URI; | |
20 | import java.net.URISyntaxException; | |
21 | import java.util.ArrayList; | |
22 | import java.util.HashSet; | |
23 | import java.util.List; | |
24 | import java.util.Set; | |
25 | import java.util.regex.Pattern; | |
26 | import java.util.regex.PatternSyntaxException; | |
27 | ||
28 | import javax.xml.parsers.ParserConfigurationException; | |
29 | import javax.xml.transform.TransformerException; | |
30 | ||
31 | import org.eclipse.core.resources.IResource; | |
32 | import org.eclipse.core.resources.ResourcesPlugin; | |
f8bfc0e5 | 33 | import org.eclipse.core.runtime.IPath; |
9ba2101a PT |
34 | import org.eclipse.core.runtime.IProgressMonitor; |
35 | import org.eclipse.core.runtime.NullProgressMonitor; | |
36 | import org.eclipse.core.runtime.URIUtil; | |
f8bfc0e5 BH |
37 | import org.eclipse.core.runtime.preferences.DefaultScope; |
38 | import org.eclipse.core.runtime.preferences.IEclipsePreferences; | |
9ba2101a PT |
39 | import org.eclipse.jface.action.Action; |
40 | import org.eclipse.jface.action.IMenuListener; | |
41 | import org.eclipse.jface.action.IMenuManager; | |
42 | import org.eclipse.jface.action.MenuManager; | |
43 | import org.eclipse.jface.action.Separator; | |
44 | import org.eclipse.jface.dialogs.Dialog; | |
45 | import org.eclipse.jface.dialogs.MessageDialog; | |
46 | import org.eclipse.jface.preference.PreferencePage; | |
47 | import org.eclipse.jface.util.LocalSelectionTransfer; | |
48 | import org.eclipse.jface.viewers.AbstractTreeViewer; | |
49 | import org.eclipse.jface.viewers.ColumnLabelProvider; | |
50 | import org.eclipse.jface.viewers.ISelectionChangedListener; | |
51 | import org.eclipse.jface.viewers.IStructuredSelection; | |
52 | import org.eclipse.jface.viewers.SelectionChangedEvent; | |
53 | import org.eclipse.jface.viewers.StructuredSelection; | |
54 | import org.eclipse.jface.viewers.TreeViewer; | |
55 | import org.eclipse.jface.viewers.Viewer; | |
56 | import org.eclipse.remote.core.IRemoteServicesManager; | |
57 | import org.eclipse.swt.SWT; | |
58 | import org.eclipse.swt.dnd.Clipboard; | |
59 | import org.eclipse.swt.dnd.Transfer; | |
60 | import org.eclipse.swt.events.KeyAdapter; | |
61 | import org.eclipse.swt.events.KeyEvent; | |
62 | import org.eclipse.swt.events.ModifyEvent; | |
63 | import org.eclipse.swt.events.ModifyListener; | |
64 | import org.eclipse.swt.events.SelectionAdapter; | |
65 | import org.eclipse.swt.events.SelectionEvent; | |
66 | import org.eclipse.swt.graphics.Image; | |
67 | import org.eclipse.swt.layout.GridData; | |
68 | import org.eclipse.swt.layout.GridLayout; | |
69 | import org.eclipse.swt.widgets.Button; | |
70 | import org.eclipse.swt.widgets.Combo; | |
71 | import org.eclipse.swt.widgets.Composite; | |
72 | import org.eclipse.swt.widgets.Control; | |
73 | import org.eclipse.swt.widgets.Display; | |
74 | import org.eclipse.swt.widgets.FileDialog; | |
75 | import org.eclipse.swt.widgets.Label; | |
76 | import org.eclipse.swt.widgets.Menu; | |
77 | import org.eclipse.swt.widgets.Text; | |
78 | import org.eclipse.tracecompass.internal.tmf.remote.ui.Activator; | |
79 | import org.eclipse.tracecompass.internal.tmf.remote.ui.messages.RemoteMessages; | |
80 | import org.eclipse.tracecompass.internal.tmf.remote.ui.wizards.fetch.model.ExtractRemoteProfilesOperation; | |
81 | import org.eclipse.tracecompass.internal.tmf.remote.ui.wizards.fetch.model.RemoteImportConnectionNodeElement; | |
82 | import org.eclipse.tracecompass.internal.tmf.remote.ui.wizards.fetch.model.RemoteImportProfileElement; | |
83 | import org.eclipse.tracecompass.internal.tmf.remote.ui.wizards.fetch.model.RemoteImportProfilesWriter; | |
84 | import org.eclipse.tracecompass.internal.tmf.remote.ui.wizards.fetch.model.RemoteImportTraceGroupElement; | |
85 | import org.eclipse.tracecompass.internal.tmf.ui.project.wizards.importtrace.Messages; | |
86 | import org.eclipse.tracecompass.internal.tmf.ui.project.wizards.tracepkg.TracePackageContentProvider; | |
87 | import org.eclipse.tracecompass.internal.tmf.ui.project.wizards.tracepkg.TracePackageElement; | |
88 | import org.eclipse.tracecompass.internal.tmf.ui.project.wizards.tracepkg.TracePackageFilesElement; | |
89 | import org.eclipse.tracecompass.internal.tmf.ui.project.wizards.tracepkg.TracePackageTraceElement; | |
90 | import org.eclipse.tracecompass.tmf.core.project.model.TmfTraceType; | |
91 | import org.eclipse.tracecompass.tmf.remote.core.proxy.RemoteSystemProxy; | |
92 | import org.eclipse.ui.ISharedImages; | |
93 | import org.eclipse.ui.IWorkbench; | |
94 | import org.eclipse.ui.IWorkbenchCommandConstants; | |
95 | import org.eclipse.ui.IWorkbenchPreferencePage; | |
96 | import org.eclipse.ui.PlatformUI; | |
97 | import org.eclipse.ui.dialogs.FilteredTree; | |
98 | import org.eclipse.ui.dialogs.PatternFilter; | |
99 | ||
100 | /** | |
101 | * Preference page for profiles | |
102 | * | |
103 | * @author Patrick Tasse | |
104 | */ | |
105 | public class RemoteProfilesPreferencePage extends PreferencePage implements IWorkbenchPreferencePage { | |
106 | ||
107 | /** The ID of this preference page */ | |
108 | public static final String ID = "org.eclipse.linuxtools.tmf.remote.ui.preferences.remoteprofiles"; //$NON-NLS-1$ | |
109 | ||
110 | private static final String REMOTE_PROFILES_XML_FILE_NAME = "remote_profiles.xml"; //$NON-NLS-1$ | |
111 | private static final String REMOTE_PROFILES_XML_FILE_PATH = | |
112 | Activator.getDefault().getStateLocation().addTrailingSeparator().append(REMOTE_PROFILES_XML_FILE_NAME).toOSString(); | |
113 | ||
f8bfc0e5 BH |
114 | private static final String REMOTE_PROFILES_LOCATION_PREF = "REMOTE_PROFILES_LOCATION"; //$NON-NLS-1$ |
115 | private static final String REMOTE_PROFILES_LOCATION_DIR_DEF = ""; //$NON-NLS-1$ | |
116 | ||
9ba2101a PT |
117 | private static final String DEFAULT_ROOT_IMPORT_PATH = "/rootpath"; //$NON-NLS-1$ |
118 | private static final String DEFAULT_IMPORT_NAME = ""; //$NON-NLS-1$ | |
119 | private static final String DEFAULT_FILE_PATTERN = ".*"; //$NON-NLS-1$ | |
120 | private static final String TRACE_TYPE_AUTO_DETECT = Messages.ImportTraceWizard_AutoDetection; | |
121 | private static final String SSH_SCHEME = "ssh"; //$NON-NLS-1$ | |
122 | ||
123 | private TreeViewer fTreeViewer; | |
124 | private List<RemoteImportProfileElement> fProfiles; | |
125 | private String fSelectedProfileName; | |
126 | private DetailsPanel fDetailsPanel; | |
127 | private Button fAddButton; | |
128 | private Button fRemoveButton; | |
129 | private Button fImportButton; | |
130 | private Button fExportButton; | |
131 | private Button fMoveUpButton; | |
132 | private Button fMoveDownButton; | |
133 | private Action fDeleteAction; | |
134 | private Action fCutAction; | |
135 | private Action fCopyAction; | |
136 | private Action fPasteAction; | |
137 | ||
f8bfc0e5 BH |
138 | private static final String fProfileFilePath; |
139 | ||
140 | static { | |
141 | String profileFilePath = REMOTE_PROFILES_XML_FILE_PATH; | |
142 | ||
143 | // Get alternative location under the parent of Activator.getDefault().getStateLocation() | |
144 | IEclipsePreferences prefs = DefaultScope.INSTANCE.getNode(Activator.PLUGIN_ID); | |
145 | String plugin = prefs.get(REMOTE_PROFILES_LOCATION_PREF, REMOTE_PROFILES_LOCATION_DIR_DEF); | |
146 | ||
147 | if (!plugin.isEmpty()) { | |
148 | // Alternative location | |
149 | IPath profileFolderPath = Activator.getDefault().getStateLocation().removeLastSegments(1).append(plugin); | |
150 | File profileFolder = profileFolderPath.toFile(); | |
151 | // Create folder if it doesn't exist | |
152 | if (profileFolder.exists() || profileFolder.mkdir()) { | |
153 | profileFilePath = profileFolderPath.append(REMOTE_PROFILES_XML_FILE_NAME).toString(); | |
154 | } | |
155 | } | |
156 | fProfileFilePath = profileFilePath; | |
157 | } | |
158 | ||
9ba2101a PT |
159 | /** |
160 | * Constructor | |
161 | */ | |
162 | public RemoteProfilesPreferencePage() { | |
163 | } | |
164 | ||
165 | @Override | |
166 | public void init(IWorkbench workbench) { | |
167 | noDefaultAndApplyButton(); | |
168 | } | |
169 | ||
170 | @Override | |
171 | protected Control createContents(Composite parent) { | |
172 | Composite composite = new Composite(parent, SWT.NONE); | |
173 | GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true); | |
174 | composite.setLayoutData(gd); | |
175 | ||
176 | GridLayout gl = new GridLayout(2, false); | |
177 | composite.setLayout(gl); | |
178 | ||
179 | PatternFilter patternFilter = new PatternFilter() { | |
180 | // show all children of matching profiles or profiles with matching connection node | |
181 | @Override | |
182 | protected boolean isLeafMatch(Viewer viewer, Object element) { | |
183 | TreeViewer treeViewer = (TreeViewer) viewer; | |
184 | TracePackageContentProvider contentProvider = (TracePackageContentProvider) treeViewer.getContentProvider(); | |
185 | Object parentElement = element; | |
186 | while (!(parentElement instanceof RemoteImportProfileElement)) { | |
187 | parentElement = contentProvider.getParent(parentElement); | |
188 | if (parentElement instanceof TracePackageTraceElement) { | |
189 | // don't show children of trace element | |
190 | return false; | |
191 | } | |
192 | } | |
193 | RemoteImportProfileElement profile = (RemoteImportProfileElement) parentElement; | |
194 | if (super.isLeafMatch(viewer, profile)) { | |
195 | return true; | |
196 | } | |
197 | for (Object child : contentProvider.getChildren(profile)) { | |
198 | if (child instanceof RemoteImportConnectionNodeElement) { | |
199 | if (super.isLeafMatch(viewer, child)) { | |
200 | return true; | |
201 | } | |
202 | } | |
203 | } | |
204 | return false; | |
205 | } | |
206 | }; | |
207 | ||
208 | final FilteredTree filteredTree = new FilteredTree(composite, | |
209 | SWT.MULTI | SWT.FULL_SELECTION | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER, patternFilter, true); | |
210 | gd = new GridData(SWT.FILL, SWT.FILL, true, true); | |
211 | gd.heightHint = 0; | |
212 | filteredTree.setLayoutData(gd); | |
213 | final TreeViewer treeViewer = filteredTree.getViewer(); | |
214 | fTreeViewer = treeViewer; | |
215 | ||
216 | treeViewer.setContentProvider(new TracePackageContentProvider() { | |
217 | @Override | |
218 | public Object[] getElements(Object inputElement) { | |
219 | if (inputElement instanceof ArrayList) { | |
220 | return ((ArrayList<?>) inputElement).toArray(); | |
221 | } | |
222 | return super.getElements(inputElement); | |
223 | } | |
224 | ||
225 | @Override | |
226 | public boolean hasChildren(Object element) { | |
227 | if (element instanceof TracePackageTraceElement) { | |
228 | return false; | |
229 | } | |
230 | return super.hasChildren(element); | |
231 | } | |
232 | }); | |
233 | ||
234 | treeViewer.setLabelProvider(new ColumnLabelProvider() { | |
235 | @Override | |
236 | public String getText(Object element) { | |
237 | if (element instanceof TracePackageTraceElement) { | |
238 | for (TracePackageElement files : ((TracePackageTraceElement) element).getChildren()) { | |
239 | if (files instanceof TracePackageFilesElement) { | |
240 | return ((TracePackageFilesElement) files).getFileName(); | |
241 | } | |
242 | } | |
243 | } else if (element instanceof TracePackageElement) { | |
244 | return ((TracePackageElement) element).getText(); | |
245 | } | |
246 | return super.getText(element); | |
247 | } | |
248 | @Override | |
249 | public Image getImage(Object element) { | |
250 | if (element instanceof TracePackageTraceElement) { | |
251 | for (TracePackageElement files : ((TracePackageTraceElement) element).getChildren()) { | |
252 | return files.getImage(); | |
253 | } | |
254 | } else if (element instanceof TracePackageElement) { | |
255 | return ((TracePackageElement) element).getImage(); | |
256 | } | |
257 | return super.getImage(element); | |
258 | } | |
259 | }); | |
260 | ||
261 | treeViewer.addSelectionChangedListener(new ISelectionChangedListener() { | |
262 | @Override | |
263 | public void selectionChanged(SelectionChangedEvent event) { | |
264 | IStructuredSelection selection = (IStructuredSelection) event.getSelection(); | |
265 | TracePackageElement element = (TracePackageElement) (selection.size() == 1 ? selection.getFirstElement() : null); | |
266 | fDetailsPanel.refreshDetailsPanel(element); | |
267 | enableButtons(selection); | |
268 | fSelectedProfileName = null; | |
269 | while (element != null) { | |
270 | if (element instanceof RemoteImportProfileElement) { | |
271 | fSelectedProfileName = ((RemoteImportProfileElement) element).getProfileName(); | |
272 | } | |
273 | element = element.getParent(); | |
274 | } | |
275 | } | |
276 | }); | |
277 | ||
278 | createGlobalActions(); | |
279 | createContextMenu(); | |
280 | ||
f8bfc0e5 | 281 | fProfiles = readProfiles(fProfileFilePath, new NullProgressMonitor()); |
9ba2101a PT |
282 | |
283 | treeViewer.setAutoExpandLevel(AbstractTreeViewer.ALL_LEVELS); | |
284 | treeViewer.setInput(fProfiles); | |
285 | treeViewer.expandAll(); | |
286 | ||
287 | Composite buttonBar = createVerticalButtonBar(composite); | |
288 | gd = new GridData(SWT.CENTER, SWT.BEGINNING, false, false); | |
289 | gd.verticalIndent = filteredTree.getFilterControl().computeSize(SWT.DEFAULT, SWT.DEFAULT).y + gl.verticalSpacing; | |
290 | buttonBar.setLayoutData(gd); | |
291 | enableButtons((IStructuredSelection) treeViewer.getSelection()); | |
292 | ||
293 | Composite details = new Composite(composite, SWT.NONE); | |
294 | gd = new GridData(SWT.FILL, SWT.CENTER, false, false, 2, 1); | |
295 | details.setLayoutData(gd); | |
296 | gl = new GridLayout(2, false); | |
297 | gl.marginWidth = 0; | |
298 | details.setLayout(gl); | |
299 | ||
300 | Label label = new Label(details, SWT.NONE); | |
301 | label.setText(RemoteMessages.RemoteProfilesPreferencePage_DetailsPanelLabel); | |
302 | gd = new GridData(SWT.BEGINNING, SWT.CENTER, false, false, 2, 1); | |
303 | label.setLayoutData(gd); | |
304 | ||
305 | fDetailsPanel = new DetailsPanel(details); | |
306 | ||
307 | validate(); | |
308 | ||
309 | for (RemoteImportProfileElement profile : fProfiles) { | |
310 | if (profile.getProfileName().equals(fSelectedProfileName)) { | |
311 | fTreeViewer.setSelection(new StructuredSelection(profile)); | |
312 | } | |
313 | } | |
314 | ||
315 | Dialog.applyDialogFont(composite); | |
316 | return composite; | |
317 | } | |
318 | ||
319 | /** | |
320 | * Get the remote profiles stored in the preferences | |
321 | * | |
322 | * @param monitor | |
323 | * a progress monitor | |
324 | * | |
325 | * @return the list of remote profiles | |
326 | */ | |
327 | public static List<RemoteImportProfileElement> getRemoteProfiles(IProgressMonitor monitor) { | |
f8bfc0e5 | 328 | return readProfiles(fProfileFilePath, monitor); |
9ba2101a PT |
329 | } |
330 | ||
331 | private static List<RemoteImportProfileElement> readProfiles(String path, IProgressMonitor monitor) { | |
332 | final ExtractRemoteProfilesOperation op = new ExtractRemoteProfilesOperation(path); | |
333 | op.run(monitor); | |
334 | List<RemoteImportProfileElement> profiles = new ArrayList<>(); | |
335 | if (!op.getStatus().isOK()) { | |
336 | return profiles; | |
337 | } | |
338 | TracePackageElement[] resultElements = op.getResultElements(); | |
339 | if (resultElements != null) { | |
340 | for (TracePackageElement element : resultElements) { | |
341 | if (element instanceof RemoteImportProfileElement) { | |
342 | profiles.add((RemoteImportProfileElement) element); | |
343 | } | |
344 | } | |
345 | } | |
346 | return profiles; | |
347 | } | |
348 | ||
349 | private boolean writeProfiles(List<RemoteImportProfileElement> profiles, String path) { | |
350 | try { | |
351 | String contents = RemoteImportProfilesWriter.writeProfilesToXML(profiles.toArray(new TracePackageElement[0])); | |
352 | File file = new File(path); | |
353 | try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) { | |
354 | writer.write(contents); | |
355 | return true; | |
356 | } | |
357 | } catch (IOException | ParserConfigurationException | TransformerException e) { | |
358 | MessageDialog.openError(getShell(), RemoteMessages.RemoteProfilesPreferencePage_ErrorWritingProfile, e.getMessage()); | |
359 | } | |
360 | return false; | |
361 | } | |
362 | ||
363 | private void createGlobalActions() { | |
364 | fDeleteAction = new Action(RemoteMessages.RemoteProfilesPreferencePage_DeleteAction) { | |
365 | @Override | |
366 | public void run() { | |
367 | final IStructuredSelection selection = (IStructuredSelection) fTreeViewer.getSelection(); | |
368 | if (selection.size() == 0) { | |
369 | return; | |
370 | } | |
371 | for (Object item : selection.toList()) { | |
372 | if (item instanceof RemoteImportProfileElement) { | |
373 | fProfiles.remove(item); | |
374 | fTreeViewer.refresh(); | |
375 | validate(); | |
376 | } else if (item instanceof TracePackageElement) { | |
377 | TracePackageElement element = (TracePackageElement) item; | |
378 | TracePackageElement parent = element.getParent(); | |
379 | parent.removeChild(element); | |
380 | fTreeViewer.refresh(parent); | |
381 | validate(); | |
382 | } | |
383 | } | |
384 | } | |
385 | }; | |
386 | fDeleteAction.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(ISharedImages.IMG_TOOL_DELETE)); | |
387 | fDeleteAction.setActionDefinitionId(IWorkbenchCommandConstants.EDIT_DELETE); | |
388 | fDeleteAction.setAccelerator(SWT.DEL); | |
389 | ||
390 | fCutAction = new Action(RemoteMessages.RemoteProfilesPreferencePage_CutAction) { | |
391 | @Override | |
392 | public void run() { | |
393 | final IStructuredSelection selection = (IStructuredSelection) fTreeViewer.getSelection(); | |
394 | if (selection.size() != 1) { | |
395 | return; | |
396 | } | |
397 | setClipboardContents(selection); | |
398 | Object item = selection.getFirstElement(); | |
399 | if (item instanceof RemoteImportProfileElement) { | |
400 | fProfiles.remove(item); | |
401 | fTreeViewer.refresh(); | |
402 | validate(); | |
403 | } else if (item instanceof TracePackageElement) { | |
404 | TracePackageElement element = (TracePackageElement) item; | |
405 | TracePackageElement parent = element.getParent(); | |
406 | parent.removeChild(element); | |
407 | fTreeViewer.refresh(parent); | |
408 | validate(); | |
409 | } | |
410 | } | |
411 | }; | |
412 | fCutAction.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(ISharedImages.IMG_TOOL_CUT)); | |
413 | fCutAction.setActionDefinitionId(IWorkbenchCommandConstants.EDIT_CUT); | |
414 | fCutAction.setAccelerator(SWT.CTRL | 'X'); | |
415 | ||
416 | fCopyAction = new Action(RemoteMessages.RemoteProfilesPreferencePage_CopyAction) { | |
417 | @Override | |
418 | public void run() { | |
419 | final IStructuredSelection selection = (IStructuredSelection) fTreeViewer.getSelection(); | |
420 | if (selection.size() != 1) { | |
421 | return; | |
422 | } | |
423 | setClipboardContents(new StructuredSelection( | |
424 | copyElement(null, (TracePackageElement) selection.getFirstElement()))); | |
425 | } | |
426 | }; | |
427 | fCopyAction.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(ISharedImages.IMG_TOOL_COPY)); | |
428 | fCopyAction.setActionDefinitionId(IWorkbenchCommandConstants.EDIT_COPY); | |
429 | fCopyAction.setAccelerator(SWT.CTRL | 'C'); | |
430 | ||
431 | fPasteAction = new Action(RemoteMessages.RemoteProfilesPreferencePage_PasteAction) { | |
432 | @Override | |
433 | public void run() { | |
434 | final IStructuredSelection selection = (IStructuredSelection) fTreeViewer.getSelection(); | |
435 | if (selection.size() > 1) { | |
436 | return; | |
437 | } | |
438 | if (!validatePaste(selection.getFirstElement())) { | |
439 | return; | |
440 | } | |
441 | IStructuredSelection data = getClipboardContents(); | |
442 | data = getClipboardContents(); | |
443 | if (data == null) { | |
444 | return; | |
445 | } | |
446 | for (Object object : data.toArray()) { | |
447 | if (object instanceof RemoteImportProfileElement) { | |
448 | TracePackageElement element = copyElement(null, (TracePackageElement) object); | |
449 | TracePackageElement target = (TracePackageElement) selection.getFirstElement(); | |
450 | if (target == null) { | |
451 | fProfiles.add((RemoteImportProfileElement) element); | |
452 | } else { | |
453 | int index = fProfiles.indexOf(target); | |
454 | fProfiles.add(index + 1, (RemoteImportProfileElement) element); | |
455 | } | |
456 | Object[] expanded = fTreeViewer.getExpandedElements(); | |
457 | fTreeViewer.refresh(); | |
458 | fTreeViewer.setExpandedElements(expanded); | |
459 | fTreeViewer.expandToLevel(element, AbstractTreeViewer.ALL_LEVELS); | |
460 | fTreeViewer.setSelection(new StructuredSelection(element)); | |
461 | validate(); | |
462 | } else if (object instanceof TracePackageElement && selection.getFirstElement() instanceof TracePackageElement) { | |
463 | TracePackageElement element = copyElement(null, (TracePackageElement) object); | |
464 | TracePackageElement target = (TracePackageElement) selection.getFirstElement(); | |
465 | if (target.getClass().equals(element.getClass())) { | |
466 | int index = target.getParent().indexOf(target); | |
467 | target.getParent().addChild(index + 1, element); | |
468 | fTreeViewer.refresh(target.getParent()); | |
469 | } else { | |
470 | target.addChild(0, element); | |
471 | fTreeViewer.refresh(target); | |
472 | } | |
473 | fTreeViewer.expandToLevel(element, AbstractTreeViewer.ALL_LEVELS); | |
474 | fTreeViewer.setSelection(new StructuredSelection(element)); | |
475 | validate(); | |
476 | } | |
477 | } | |
478 | } | |
479 | }; | |
480 | fPasteAction.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(ISharedImages.IMG_TOOL_PASTE)); | |
481 | fPasteAction.setActionDefinitionId(IWorkbenchCommandConstants.EDIT_PASTE); | |
482 | fPasteAction.setAccelerator(SWT.CTRL | 'V'); | |
483 | ||
484 | fTreeViewer.getTree().addKeyListener(new KeyAdapter() { | |
485 | @Override | |
486 | public void keyPressed(KeyEvent e) { | |
487 | if (e.stateMask == 0 && e.keyCode == SWT.DEL) { | |
488 | fDeleteAction.run(); | |
489 | } | |
490 | if ((e.stateMask & SWT.CTRL) == SWT.CTRL) { | |
491 | if (e.keyCode == 'x') { | |
492 | fCutAction.run(); | |
493 | } else if (e.keyCode == 'c') { | |
494 | fCopyAction.run(); | |
495 | } else if (e.keyCode == 'v') { | |
496 | fPasteAction.run(); | |
497 | } | |
498 | } | |
499 | } | |
500 | }); | |
501 | } | |
502 | ||
503 | private void createContextMenu() { | |
504 | MenuManager menuManager = new MenuManager(); | |
505 | menuManager.setRemoveAllWhenShown(true); | |
506 | menuManager.addMenuListener(new IMenuListener() { | |
507 | @Override | |
508 | public void menuAboutToShow(IMenuManager manager) { | |
509 | fillContextMenu(manager); | |
510 | } | |
511 | }); | |
512 | ||
513 | Menu contextMenu = menuManager.createContextMenu(fTreeViewer.getTree()); | |
514 | fTreeViewer.getTree().setMenu(contextMenu); | |
515 | } | |
516 | ||
517 | private void fillContextMenu(IMenuManager manager) { | |
518 | final IStructuredSelection selection = (IStructuredSelection) fTreeViewer.getSelection(); | |
519 | final List<Object> items = selection.toList(); | |
520 | if (items.size() == 1) { | |
521 | Object item = items.get(0); | |
522 | if (item instanceof RemoteImportProfileElement) { | |
523 | final RemoteImportProfileElement profile = (RemoteImportProfileElement) item; | |
524 | manager.add(new Action(RemoteMessages.RemoteProfilesPreferencePage_NewConnectionNode) { | |
525 | @Override | |
526 | public void run() { | |
527 | newConnectionNode(profile, null); | |
528 | } | |
529 | }); | |
530 | } else if (item instanceof RemoteImportConnectionNodeElement) { | |
531 | final RemoteImportConnectionNodeElement node = (RemoteImportConnectionNodeElement) item; | |
532 | manager.add(new Action(RemoteMessages.RemoteProfilesPreferencePage_NewTraceGroupAction) { | |
533 | @Override | |
534 | public void run() { | |
535 | newTraceGroup(node, null); | |
536 | } | |
537 | }); | |
538 | } else if (item instanceof RemoteImportTraceGroupElement) { | |
539 | final RemoteImportTraceGroupElement traceGroup = (RemoteImportTraceGroupElement) item; | |
540 | manager.add(new Action(RemoteMessages.RemoteProfilesPreferencePage_NewTraceAction) { | |
541 | @Override | |
542 | public void run() { | |
543 | newTrace(traceGroup, null); | |
544 | } | |
545 | }); | |
546 | } | |
547 | } | |
548 | manager.add(new Separator()); | |
549 | manager.add(fDeleteAction); | |
550 | fDeleteAction.setEnabled(items.size() > 0); | |
551 | manager.add(new Separator()); | |
552 | manager.add(fCutAction); | |
553 | fCutAction.setEnabled(items.size() == 1); | |
554 | manager.add(fCopyAction); | |
555 | fCopyAction.setEnabled(items.size() == 1); | |
556 | manager.add(fPasteAction); | |
557 | fPasteAction.setEnabled(items.size() <= 1 && validatePaste(selection.getFirstElement())); | |
558 | } | |
559 | ||
560 | private Composite createVerticalButtonBar(Composite parent) { | |
561 | Composite composite = new Composite(parent, SWT.NONE); | |
562 | composite.setLayout(new GridLayout()); | |
563 | ||
564 | fAddButton = createVerticalButton(composite, RemoteMessages.RemoteProfilesPreferencePage_AddButton); | |
565 | fAddButton.addSelectionListener(new SelectionAdapter() { | |
566 | @Override | |
567 | public void widgetSelected(SelectionEvent e) { | |
568 | IStructuredSelection selection = (IStructuredSelection) fTreeViewer.getSelection(); | |
569 | if (selection.isEmpty()) { | |
570 | newProfile(null); | |
571 | } else if (selection.getFirstElement() instanceof TracePackageElement) { | |
572 | TracePackageElement previous = (TracePackageElement) selection.getFirstElement(); | |
573 | if (previous instanceof RemoteImportProfileElement) { | |
574 | newProfile(previous); | |
575 | } else if (previous instanceof RemoteImportConnectionNodeElement) { | |
576 | newConnectionNode(previous.getParent(), previous); | |
577 | } else if (previous instanceof RemoteImportTraceGroupElement) { | |
578 | newTraceGroup(previous.getParent(), previous); | |
579 | } else if (previous instanceof TracePackageTraceElement) { | |
580 | newTrace(previous.getParent(), previous); | |
581 | } | |
582 | } | |
583 | } | |
584 | }); | |
585 | ||
586 | fRemoveButton = createVerticalButton(composite, RemoteMessages.RemoteProfilesPreferencePage_RemoveButton); | |
587 | fRemoveButton.addSelectionListener(new SelectionAdapter() { | |
588 | @Override | |
589 | public void widgetSelected(SelectionEvent e) { | |
590 | IStructuredSelection selection = (IStructuredSelection) fTreeViewer.getSelection(); | |
591 | for (Object item : selection.toList()) { | |
592 | if (item instanceof RemoteImportProfileElement) { | |
593 | fProfiles.remove(item); | |
594 | } else if (item instanceof TracePackageElement) { | |
595 | TracePackageElement element = (TracePackageElement) item; | |
596 | element.getParent().removeChild(element); | |
597 | } | |
598 | } | |
599 | fTreeViewer.refresh(); | |
600 | validate(); | |
601 | } | |
602 | }); | |
603 | ||
604 | new Label(composite, SWT.NONE); | |
605 | ||
606 | fImportButton = createVerticalButton(composite, RemoteMessages.RemoteProfilesPreferencePage_ImportButton); | |
607 | fImportButton.addSelectionListener(new SelectionAdapter() { | |
608 | @Override | |
609 | public void widgetSelected(SelectionEvent e) { | |
610 | FileDialog dialog = new FileDialog(Display.getCurrent().getActiveShell(), SWT.OPEN); | |
611 | dialog.setText(RemoteMessages.RemoteProfilesPreferencePage_ImportFileDialogTitle); | |
612 | dialog.setFilterExtensions(new String[] { "*.xml", "*" }); //$NON-NLS-1$ //$NON-NLS-2$ | |
613 | String path = dialog.open(); | |
614 | if (path != null) { | |
615 | List<RemoteImportProfileElement> profiles = readProfiles(path, new NullProgressMonitor()); | |
616 | fProfiles.addAll(profiles); | |
617 | fTreeViewer.refresh(); | |
618 | for (RemoteImportProfileElement profile : profiles) { | |
619 | fTreeViewer.expandToLevel(profile, AbstractTreeViewer.ALL_LEVELS); | |
620 | } | |
621 | fTreeViewer.setSelection(new StructuredSelection(profiles)); | |
622 | validate(); | |
623 | } | |
624 | } | |
625 | }); | |
626 | ||
627 | fExportButton = createVerticalButton(composite, RemoteMessages.RemoteProfilesPreferencePage_ExportButton); | |
628 | fExportButton.addSelectionListener(new SelectionAdapter() { | |
629 | @Override | |
630 | public void widgetSelected(SelectionEvent e) { | |
631 | FileDialog dialog = new FileDialog(Display.getCurrent().getActiveShell(), SWT.SAVE); | |
632 | dialog.setText(RemoteMessages.RemoteProfilesPreferencePage_ExportFileDialogTitle); | |
633 | dialog.setFilterExtensions(new String[] { "*.xml", "*" }); //$NON-NLS-1$ //$NON-NLS-2$ | |
634 | String path = dialog.open(); | |
635 | if (path != null) { | |
636 | IStructuredSelection selection = (IStructuredSelection) fTreeViewer.getSelection(); | |
637 | List<RemoteImportProfileElement> profiles = new ArrayList<>(); | |
638 | for (Object element : selection.toList()) { | |
639 | if (element instanceof RemoteImportProfileElement) { | |
640 | profiles.add((RemoteImportProfileElement) element); | |
641 | } | |
642 | } | |
643 | writeProfiles(profiles, path); | |
644 | } | |
645 | } | |
646 | }); | |
647 | ||
648 | new Label(composite, SWT.NONE); | |
649 | ||
650 | fMoveUpButton = createVerticalButton(composite, RemoteMessages.RemoteProfilesPreferencePage_MoveUpButton); | |
651 | fMoveUpButton.addSelectionListener(new SelectionAdapter() { | |
652 | @Override | |
653 | public void widgetSelected(SelectionEvent e) { | |
654 | IStructuredSelection selection = (IStructuredSelection) fTreeViewer.getSelection(); | |
655 | Object element = selection.getFirstElement(); | |
656 | if (element instanceof RemoteImportProfileElement) { | |
657 | int index = fProfiles.indexOf(element); | |
658 | if (index > 0) { | |
659 | RemoteImportProfileElement profile = fProfiles.remove(index); | |
660 | fProfiles.add(index - 1, profile); | |
661 | Object[] expanded = fTreeViewer.getExpandedElements(); | |
662 | fTreeViewer.refresh(); | |
663 | fTreeViewer.setExpandedElements(expanded); | |
664 | enableButtons(selection); | |
665 | } | |
666 | } else if (element instanceof TracePackageElement) { | |
667 | TracePackageElement child = (TracePackageElement) element; | |
668 | TracePackageElement parentElement = child.getParent(); | |
669 | int index = parentElement.indexOf(child); | |
670 | if (index > 0) { | |
671 | parentElement.removeChild(child); | |
672 | parentElement.addChild(index - 1, child); | |
673 | Object[] expanded = fTreeViewer.getExpandedElements(); | |
674 | fTreeViewer.refresh(parentElement); | |
675 | fTreeViewer.setExpandedElements(expanded); | |
676 | enableButtons(selection); | |
677 | } | |
678 | } | |
679 | } | |
680 | }); | |
681 | ||
682 | fMoveDownButton = createVerticalButton(composite, RemoteMessages.RemoteProfilesPreferencePage_MoveDownButton); | |
683 | fMoveDownButton.addSelectionListener(new SelectionAdapter() { | |
684 | @Override | |
685 | public void widgetSelected(SelectionEvent e) { | |
686 | IStructuredSelection selection = (IStructuredSelection) fTreeViewer.getSelection(); | |
687 | Object element = selection.getFirstElement(); | |
688 | if (element instanceof RemoteImportProfileElement) { | |
689 | int index = fProfiles.indexOf(selection.getFirstElement()); | |
690 | if (index >= 0 && index < fProfiles.size() - 1) { | |
691 | RemoteImportProfileElement profile = fProfiles.remove(index); | |
692 | fProfiles.add(index + 1, profile); | |
693 | Object[] expanded = fTreeViewer.getExpandedElements(); | |
694 | fTreeViewer.refresh(); | |
695 | fTreeViewer.setExpandedElements(expanded); | |
696 | enableButtons(selection); | |
697 | } | |
698 | } else if (element instanceof TracePackageElement) { | |
699 | TracePackageElement child = (TracePackageElement) element; | |
700 | TracePackageElement parentElement = child.getParent(); | |
701 | int index = parentElement.indexOf(child); | |
702 | if (index >= 0 && index < parentElement.getChildren().length - 1) { | |
703 | parentElement.removeChild(child); | |
704 | parentElement.addChild(index + 1, child); | |
705 | Object[] expanded = fTreeViewer.getExpandedElements(); | |
706 | fTreeViewer.refresh(parentElement); | |
707 | fTreeViewer.setExpandedElements(expanded); | |
708 | enableButtons(selection); | |
709 | } | |
710 | } | |
711 | } | |
712 | }); | |
713 | ||
714 | return composite; | |
715 | } | |
716 | ||
717 | private static Button createVerticalButton(Composite parent, String text) { | |
718 | Button button = new Button(parent, SWT.PUSH); | |
719 | button.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL)); | |
720 | button.setText(text); | |
721 | return button; | |
722 | } | |
723 | ||
724 | private void enableButtons(IStructuredSelection selection) { | |
725 | boolean allProfiles = false; | |
726 | for (Object element : selection.toList()) { | |
727 | if (element instanceof RemoteImportProfileElement) { | |
728 | allProfiles = true; | |
729 | } else { | |
730 | allProfiles = false; | |
731 | break; | |
732 | } | |
733 | } | |
734 | fAddButton.setEnabled(selection.size() <= 1); | |
735 | fRemoveButton.setEnabled(!selection.isEmpty()); | |
736 | fExportButton.setEnabled(allProfiles); | |
737 | int index = 0; | |
738 | int length = 0; | |
739 | if (selection.size() == 1) { | |
740 | Object item = selection.getFirstElement(); | |
741 | if (item instanceof RemoteImportProfileElement) { | |
742 | index = fProfiles.indexOf(item); | |
743 | length = fProfiles.size(); | |
744 | } else if (item instanceof TracePackageElement) { | |
745 | TracePackageElement element = (TracePackageElement) item; | |
746 | TracePackageElement parent = element.getParent(); | |
747 | index = parent.indexOf(element); | |
748 | length = parent.getChildren().length; | |
749 | } | |
750 | } | |
751 | fMoveUpButton.setEnabled(index > 0); | |
752 | fMoveDownButton.setEnabled(index < length - 1); | |
753 | } | |
754 | ||
755 | private class DetailsPanel { | |
756 | ||
757 | private Composite fComposite; | |
758 | ||
759 | public DetailsPanel(Composite parent) { | |
760 | fComposite = new Composite(parent, SWT.BORDER); | |
761 | GridLayout gl = new GridLayout(2, false); | |
762 | fComposite.setLayout(gl); | |
763 | GridData gd = new GridData(SWT.FILL, SWT.BEGINNING, true, false); | |
764 | Combo combo = new Combo(fComposite, SWT.BORDER); | |
765 | combo.setText("*"); //$NON-NLS-1$ | |
766 | gd.heightHint = 2 * gl.marginHeight + gl.verticalSpacing + 2 * (combo.computeSize(SWT.DEFAULT, SWT.DEFAULT).y); | |
767 | fComposite.setLayoutData(gd); | |
768 | combo.dispose(); | |
769 | } | |
770 | ||
771 | public void refreshDetailsPanel(final TracePackageElement selection) { | |
772 | for (Control control : fComposite.getChildren()) { | |
773 | control.dispose(); | |
774 | } | |
775 | ||
776 | if (selection instanceof RemoteImportProfileElement) { | |
777 | final RemoteImportProfileElement element = (RemoteImportProfileElement) selection; | |
778 | ||
779 | Label label = new Label(fComposite, SWT.NONE); | |
780 | label.setText(RemoteMessages.RemoteProfilesPreferencePage_ProfileNameLabel); | |
781 | final Text profileNameText = new Text(fComposite, SWT.BORDER); | |
782 | GridData gd = new GridData(SWT.FILL, SWT.CENTER, true, false); | |
783 | profileNameText.setLayoutData(gd); | |
784 | profileNameText.setText(element.getProfileName()); | |
785 | profileNameText.addModifyListener(new ModifyListener() { | |
786 | @Override | |
787 | public void modifyText(ModifyEvent e) { | |
788 | element.setProfileName(profileNameText.getText().trim()); | |
789 | fTreeViewer.refresh(element, true); | |
790 | validate(); | |
791 | fSelectedProfileName = element.getProfileName(); | |
792 | } | |
793 | }); | |
794 | ||
795 | } else if (selection instanceof RemoteImportConnectionNodeElement) { | |
796 | final RemoteImportConnectionNodeElement element = (RemoteImportConnectionNodeElement) selection; | |
797 | ||
798 | Label label = new Label(fComposite, SWT.NONE); | |
799 | label.setText(RemoteMessages.RemoteProfilesPreferencePage_ConnectionNodeNameLabel); | |
800 | final Text nameText = new Text(fComposite, SWT.BORDER); | |
801 | nameText.setText(element.getName()); | |
802 | GridData gd = new GridData(SWT.FILL, SWT.CENTER, true, false); | |
803 | nameText.setLayoutData(gd); | |
804 | nameText.addModifyListener(new ModifyListener() { | |
805 | @Override | |
806 | public void modifyText(ModifyEvent e) { | |
807 | element.setName(nameText.getText().trim()); | |
808 | fTreeViewer.refresh(element, true); | |
809 | validate(); | |
810 | } | |
811 | }); | |
812 | ||
813 | label = new Label(fComposite, SWT.NONE); | |
814 | label.setText(RemoteMessages.RemoteProfilesPreferencePage_ConnectionNodeURILabel); | |
815 | final Text uriText = new Text(fComposite, SWT.BORDER); | |
816 | uriText.setText(element.getURI()); | |
817 | gd = new GridData(SWT.FILL, SWT.CENTER, true, false); | |
818 | uriText.setLayoutData(gd); | |
819 | uriText.addModifyListener(new ModifyListener() { | |
820 | @Override | |
821 | public void modifyText(ModifyEvent e) { | |
822 | element.setURI(uriText.getText().trim()); | |
823 | fTreeViewer.refresh(element, true); | |
824 | validate(); | |
825 | } | |
826 | }); | |
827 | ||
828 | } else if (selection instanceof RemoteImportTraceGroupElement) { | |
829 | final RemoteImportTraceGroupElement element = (RemoteImportTraceGroupElement) selection; | |
830 | ||
831 | Label label = new Label(fComposite, SWT.NONE); | |
832 | label.setText(RemoteMessages.RemoteProfilesPreferencePage_RootPathLabel); | |
833 | final Text rootText = new Text(fComposite, SWT.BORDER); | |
834 | GridData gd = new GridData(SWT.FILL, SWT.CENTER, true, false); | |
835 | rootText.setLayoutData(gd); | |
836 | rootText.setText(element.getRootImportPath()); | |
837 | rootText.addModifyListener(new ModifyListener() { | |
838 | @Override | |
839 | public void modifyText(ModifyEvent e) { | |
840 | element.setRootImportPath(rootText.getText().trim()); | |
841 | fTreeViewer.refresh(element, true); | |
842 | validate(); | |
843 | } | |
844 | }); | |
845 | ||
1f2128b3 BH |
846 | // create label for alignment |
847 | new Label(fComposite, SWT.NONE); | |
9ba2101a PT |
848 | final Button recursiveButton = new Button(fComposite, SWT.CHECK); |
849 | gd = new GridData(SWT.FILL, SWT.CENTER, true, false); | |
850 | recursiveButton.setLayoutData(gd); | |
851 | recursiveButton.setText(RemoteMessages.RemoteProfilesPreferencePage_RecursiveButton); | |
852 | recursiveButton.setSelection(element.isRecursive()); | |
853 | recursiveButton.addSelectionListener(new SelectionAdapter() { | |
854 | @Override | |
855 | public void widgetSelected(SelectionEvent e) { | |
856 | element.setRecursive(recursiveButton.getSelection()); | |
857 | fTreeViewer.refresh(element, true); | |
858 | } | |
859 | }); | |
860 | ||
861 | } else if (selection instanceof TracePackageTraceElement) { | |
862 | final TracePackageTraceElement element = (TracePackageTraceElement) selection; | |
863 | ||
864 | Label label = new Label(fComposite, SWT.NONE); | |
865 | label.setText(RemoteMessages.RemoteProfilesPreferencePage_FilePatternLabel); | |
866 | final Text fileNameText = new Text(fComposite, SWT.BORDER); | |
867 | for (TracePackageElement child : element.getChildren()) { | |
868 | if (child instanceof TracePackageFilesElement) { | |
869 | TracePackageFilesElement files = (TracePackageFilesElement) child; | |
870 | fileNameText.setText(files.getFileName()); | |
871 | } | |
872 | } | |
873 | GridData gd = new GridData(SWT.FILL, SWT.CENTER, true, false); | |
874 | fileNameText.setLayoutData(gd); | |
875 | fileNameText.addModifyListener(new ModifyListener() { | |
876 | @Override | |
877 | public void modifyText(ModifyEvent e) { | |
878 | for (TracePackageElement child : element.getChildren()) { | |
879 | if (child instanceof TracePackageFilesElement) { | |
880 | TracePackageFilesElement files = (TracePackageFilesElement) child; | |
881 | files.setFileName(fileNameText.getText().trim()); | |
882 | } | |
883 | } | |
884 | fTreeViewer.refresh(element, true); | |
885 | validate(); | |
886 | } | |
887 | }); | |
888 | ||
889 | label = new Label(fComposite, SWT.NONE); | |
890 | label.setText(RemoteMessages.RemoteProfilesPreferencePage_TraceTypeLabel); | |
891 | final Combo combo = new Combo(fComposite, SWT.BORDER | SWT.READ_ONLY); | |
892 | String[] availableTraceTypes = TmfTraceType.getAvailableTraceTypes(); | |
893 | String[] traceTypeList = new String[availableTraceTypes.length + 1]; | |
894 | traceTypeList[0] = TRACE_TYPE_AUTO_DETECT; | |
895 | for (int i = 0; i < availableTraceTypes.length; i++) { | |
896 | traceTypeList[i + 1] = availableTraceTypes[i]; | |
897 | } | |
898 | combo.setItems(traceTypeList); | |
899 | combo.select(0); | |
900 | for (int i = 1; i < traceTypeList.length; i++) { | |
901 | String traceType = traceTypeList[i]; | |
902 | String traceTypeId = TmfTraceType.getTraceTypeId(traceType); | |
903 | if (traceTypeId.equals(element.getTraceType())) { | |
904 | combo.select(i); | |
905 | break; | |
906 | } | |
907 | } | |
908 | gd = new GridData(SWT.FILL, SWT.CENTER, true, false); | |
909 | combo.setLayoutData(gd); | |
910 | combo.addSelectionListener(new SelectionAdapter() { | |
911 | @Override | |
912 | public void widgetSelected(SelectionEvent e) { | |
913 | if (combo.getSelectionIndex() == 0) { | |
914 | element.setTraceType(null); | |
915 | } else { | |
916 | String traceType = combo.getText(); | |
917 | String traceTypeId = TmfTraceType.getTraceTypeId(traceType); | |
918 | element.setTraceType(traceTypeId); | |
919 | } | |
920 | } | |
921 | }); | |
922 | ||
923 | } | |
924 | ||
925 | fComposite.layout(); | |
926 | } | |
927 | } | |
928 | ||
929 | private RemoteImportProfileElement newProfile(TracePackageElement previous) { | |
930 | RemoteImportProfileElement profile = new RemoteImportProfileElement(null, RemoteMessages.RemoteProfilesPreferencePage_DefaultProfileName); | |
931 | RemoteImportConnectionNodeElement node = new RemoteImportConnectionNodeElement(profile, RemoteMessages.RemoteProfilesPreferencePage_DefaultConnectionNodeName, RemoteMessages.RemoteProfilesPreferencePage_DefaultConnectionNodeURI); | |
932 | RemoteImportTraceGroupElement traceGroup = new RemoteImportTraceGroupElement(node, DEFAULT_ROOT_IMPORT_PATH); | |
933 | TracePackageTraceElement trace = new TracePackageTraceElement(traceGroup, DEFAULT_IMPORT_NAME, null); | |
934 | new TracePackageFilesElement(trace, DEFAULT_FILE_PATTERN); | |
935 | int index = previous == null ? fProfiles.size() : fProfiles.indexOf(previous) + 1; | |
936 | fProfiles.add(index, profile); | |
937 | newElementAdded(profile); | |
938 | return profile; | |
939 | } | |
940 | ||
941 | private RemoteImportConnectionNodeElement newConnectionNode(TracePackageElement parent, TracePackageElement previous) { | |
942 | RemoteImportConnectionNodeElement node = new RemoteImportConnectionNodeElement(null, RemoteMessages.RemoteProfilesPreferencePage_DefaultConnectionNodeName, RemoteMessages.RemoteProfilesPreferencePage_DefaultConnectionNodeURI); | |
943 | RemoteImportTraceGroupElement traceGroup = new RemoteImportTraceGroupElement(node, DEFAULT_ROOT_IMPORT_PATH); | |
944 | TracePackageTraceElement trace = new TracePackageTraceElement(traceGroup, DEFAULT_IMPORT_NAME, null); | |
945 | new TracePackageFilesElement(trace, DEFAULT_FILE_PATTERN); | |
946 | int index = previous == null ? parent.getChildren().length : parent.indexOf(previous) + 1; | |
947 | parent.addChild(index, node); | |
948 | newElementAdded(node); | |
949 | return node; | |
950 | } | |
951 | ||
952 | private RemoteImportTraceGroupElement newTraceGroup(TracePackageElement parent, TracePackageElement previous) { | |
953 | RemoteImportTraceGroupElement traceGroup = new RemoteImportTraceGroupElement(null, DEFAULT_ROOT_IMPORT_PATH); | |
954 | TracePackageTraceElement trace = new TracePackageTraceElement(traceGroup, DEFAULT_IMPORT_NAME, null); | |
955 | new TracePackageFilesElement(trace, DEFAULT_FILE_PATTERN); | |
956 | int index = previous == null ? parent.getChildren().length : parent.indexOf(previous) + 1; | |
957 | parent.addChild(index, traceGroup); | |
958 | newElementAdded(traceGroup); | |
959 | return traceGroup; | |
960 | } | |
961 | ||
962 | private TracePackageTraceElement newTrace(TracePackageElement parent, TracePackageElement previous) { | |
963 | TracePackageTraceElement trace = new TracePackageTraceElement(null, DEFAULT_IMPORT_NAME, null); | |
964 | new TracePackageFilesElement(trace, DEFAULT_FILE_PATTERN); | |
965 | int index = previous == null ? parent.getChildren().length : parent.indexOf(previous) + 1; | |
966 | parent.addChild(index, trace); | |
967 | newElementAdded(trace); | |
968 | return trace; | |
969 | } | |
970 | ||
971 | private void newElementAdded(TracePackageElement element) { | |
972 | if (element.getParent() != null) { | |
973 | fTreeViewer.refresh(element.getParent()); | |
974 | } else { | |
975 | fTreeViewer.refresh(); | |
976 | } | |
977 | fTreeViewer.expandToLevel(element, AbstractTreeViewer.ALL_LEVELS); | |
978 | fTreeViewer.setSelection(new StructuredSelection(element)); | |
979 | validate(); | |
980 | } | |
981 | ||
982 | private TracePackageElement copyElement(TracePackageElement parent, TracePackageElement element) { | |
983 | TracePackageElement copy = null; | |
984 | if (element instanceof RemoteImportProfileElement) { | |
985 | RemoteImportProfileElement source = (RemoteImportProfileElement) element; | |
986 | copy = new RemoteImportProfileElement(parent, source.getProfileName()); | |
987 | } else if (element instanceof RemoteImportConnectionNodeElement) { | |
988 | RemoteImportConnectionNodeElement source = (RemoteImportConnectionNodeElement) element; | |
989 | copy = new RemoteImportConnectionNodeElement(parent, source.getName(), source.getURI()); | |
990 | } else if (element instanceof RemoteImportTraceGroupElement) { | |
991 | RemoteImportTraceGroupElement source = (RemoteImportTraceGroupElement) element; | |
992 | copy = new RemoteImportTraceGroupElement(parent, source.getRootImportPath()); | |
993 | ((RemoteImportTraceGroupElement) copy).setRecursive(source.isRecursive()); | |
994 | } else if (element instanceof TracePackageTraceElement) { | |
995 | TracePackageTraceElement source = (TracePackageTraceElement) element; | |
996 | copy = new TracePackageTraceElement(parent, source.getImportName(), source.getTraceType()); | |
997 | } else if (element instanceof TracePackageFilesElement) { | |
998 | TracePackageFilesElement source = (TracePackageFilesElement) element; | |
999 | copy = new TracePackageFilesElement(parent, source.getFileName()); | |
1000 | } | |
1001 | for (TracePackageElement child : element.getChildren()) { | |
1002 | copyElement(copy, child); | |
1003 | } | |
1004 | return copy; | |
1005 | } | |
1006 | ||
1007 | private static boolean validatePaste(Object target) { | |
1008 | IStructuredSelection data = getClipboardContents(); | |
1009 | if (data == null || data.isEmpty()) { | |
1010 | return false; | |
1011 | } | |
1012 | for (Object item : data.toArray()) { | |
1013 | if (item instanceof RemoteImportConnectionNodeElement) { | |
1014 | if (!(target instanceof RemoteImportConnectionNodeElement || | |
1015 | target instanceof RemoteImportProfileElement)) { | |
1016 | return false; | |
1017 | } | |
1018 | } else if (item instanceof RemoteImportTraceGroupElement) { | |
1019 | if (!(target instanceof RemoteImportTraceGroupElement || | |
1020 | target instanceof RemoteImportConnectionNodeElement)) { | |
1021 | return false; | |
1022 | } | |
1023 | } else if (item instanceof TracePackageTraceElement) { | |
1024 | if (!(target instanceof TracePackageTraceElement || | |
1025 | target instanceof RemoteImportTraceGroupElement)) { | |
1026 | return false; | |
1027 | } | |
1028 | } else if (item instanceof RemoteImportProfileElement) { | |
1029 | if (!(target instanceof RemoteImportProfileElement || | |
1030 | target == null)) { | |
1031 | return false; | |
1032 | } | |
1033 | } else { | |
1034 | return false; | |
1035 | } | |
1036 | } | |
1037 | return true; | |
1038 | } | |
1039 | ||
1040 | private static void setClipboardContents(IStructuredSelection data) { | |
1041 | LocalSelectionTransfer transfer = LocalSelectionTransfer.getTransfer(); | |
1042 | transfer.setSelection(data); | |
1043 | Clipboard clipboard = new Clipboard(PlatformUI.getWorkbench().getDisplay()); | |
1044 | clipboard.setContents(new Object[] { new Object() }, new Transfer[] { transfer }); | |
1045 | clipboard.dispose(); | |
1046 | } | |
1047 | ||
1048 | private static IStructuredSelection getClipboardContents() { | |
1049 | Clipboard clipboard = new Clipboard(PlatformUI.getWorkbench().getDisplay()); | |
1050 | IStructuredSelection data = (IStructuredSelection) clipboard.getContents(LocalSelectionTransfer.getTransfer()); | |
1051 | clipboard.dispose(); | |
1052 | return data; | |
1053 | } | |
1054 | ||
1055 | private void validate() { | |
1056 | setValid(false); | |
1057 | Set<String> profileNames = new HashSet<>(); | |
1058 | for (RemoteImportProfileElement profile : fProfiles) { | |
1059 | if (profile.getProfileName().length() == 0) { | |
1060 | setErrorMessage(RemoteMessages.RemoteProfilesPreferencePage_EmptyProfileNameError); | |
1061 | return; | |
1062 | } | |
1063 | String prefix = profile.getProfileName() + ": "; //$NON-NLS-1$ | |
1064 | if (!profileNames.add(profile.getProfileName())) { | |
1065 | setErrorMessage(prefix + RemoteMessages.RemoteProfilesPreferencePage_DuplicateProfileNameError); | |
1066 | return; | |
1067 | } | |
1068 | Set<String> nodeNames = new HashSet<>(); | |
1069 | int nodeCount = 0; | |
1070 | for (TracePackageElement profileChild : profile.getChildren()) { | |
1071 | if (profileChild instanceof RemoteImportConnectionNodeElement) { | |
1072 | nodeCount++; | |
1073 | RemoteImportConnectionNodeElement node = (RemoteImportConnectionNodeElement) profileChild; | |
1074 | if (node.getName().length() == 0) { | |
1075 | setErrorMessage(prefix + RemoteMessages.RemoteProfilesPreferencePage_EmptyNodeNameError); | |
1076 | return; | |
1077 | } | |
1078 | // validate node name against the OS since the name will be used as folder name at the destination | |
1079 | if (!ResourcesPlugin.getWorkspace().validateName(node.getName(), IResource.FOLDER).isOK()) { | |
1080 | setErrorMessage(prefix + RemoteMessages.RemoteProfilesPreferencePage_InvalidNodeName); | |
1081 | return; | |
1082 | } | |
1083 | if (!nodeNames.add(node.getName())) { | |
1084 | setErrorMessage(prefix + RemoteMessages.RemoteProfilesPreferencePage_DuplicateConnectionNodeNameError); | |
1085 | return; | |
1086 | } | |
1087 | ||
1088 | if (node.getURI().length() == 0) { | |
1089 | setErrorMessage(prefix + RemoteMessages.RemoteProfilesPreferencePage_EmptyNodeURIError); | |
1090 | return; | |
1091 | } | |
1092 | try { | |
1093 | URI uri = URIUtil.fromString(node.getURI()); | |
1094 | IRemoteServicesManager manager = RemoteSystemProxy.getService(IRemoteServicesManager.class); | |
1095 | if (manager == null || manager.getConnectionType(uri) == null) { | |
1096 | setErrorMessage(prefix + RemoteMessages.RemoteProfilesPreferencePage_UnsupportedURISchemeError); | |
1097 | return; | |
1098 | } | |
1099 | if (uri.getScheme().equals(SSH_SCHEME)) { | |
1100 | if (uri.getHost() == null) { | |
1101 | setErrorMessage(prefix + RemoteMessages.RemoteProfilesPreferencePage_InvalidHostOrPortError); | |
1102 | return; | |
1103 | } | |
1104 | if (uri.getUserInfo() == null) { | |
1105 | setErrorMessage(prefix + RemoteMessages.RemoteProfilesPreferencePage_MissingUserInfoError); | |
1106 | return; | |
1107 | } | |
1108 | } | |
1109 | } catch (URISyntaxException e) { | |
1110 | setErrorMessage(prefix + RemoteMessages.RemoteProfilesPreferencePage_InvalidNodeURIError); | |
1111 | return; | |
1112 | } | |
1113 | int traceGroupCount = 0; | |
1114 | for (TracePackageElement nodeChild : node.getChildren()) { | |
1115 | if (nodeChild instanceof RemoteImportTraceGroupElement) { | |
1116 | traceGroupCount++; | |
1117 | RemoteImportTraceGroupElement traceGroup = (RemoteImportTraceGroupElement) nodeChild; | |
1118 | if (traceGroup.getRootImportPath().length() == 0) { | |
1119 | setErrorMessage(prefix + RemoteMessages.RemoteProfilesPreferencePage_EmptyRootPathError); | |
1120 | return; | |
1121 | } | |
1122 | int traceCount = 0; | |
1123 | for (TracePackageElement traceGroupChild : traceGroup.getChildren()) { | |
1124 | if (traceGroupChild instanceof TracePackageTraceElement) { | |
1125 | traceCount++; | |
1126 | TracePackageTraceElement trace = (TracePackageTraceElement) traceGroupChild; | |
1127 | ||
1128 | for (TracePackageElement traceChild : trace.getChildren()) { | |
1129 | if (traceChild instanceof TracePackageFilesElement) { | |
1130 | TracePackageFilesElement files = (TracePackageFilesElement) traceChild; | |
1131 | if (files.getFileName().length() == 0) { | |
1132 | setErrorMessage(prefix + RemoteMessages.RemoteProfilesPreferencePage_EmptyFilePatternError); | |
1133 | return; | |
1134 | } | |
1135 | try { | |
1136 | Pattern.compile(files.getFileName()); | |
1137 | } catch (PatternSyntaxException e) { | |
1138 | setErrorMessage(prefix + RemoteMessages.RemoteProfilesPreferencePage_InvalidFilePatternError); | |
1139 | return; | |
1140 | } | |
1141 | } | |
1142 | } | |
1143 | } | |
1144 | } | |
1145 | if (traceCount == 0) { | |
1146 | setErrorMessage(prefix + RemoteMessages.RemoteProfilesPreferencePage_MissingTraceError); | |
1147 | return; | |
1148 | } | |
1149 | } | |
1150 | } | |
1151 | if (traceGroupCount == 0) { | |
1152 | setErrorMessage(prefix + RemoteMessages.RemoteProfilesPreferencePage_MissingTraceGroupError); | |
1153 | return; | |
1154 | } | |
1155 | } | |
1156 | } | |
1157 | if (nodeCount == 0) { | |
1158 | setErrorMessage(prefix + RemoteMessages.RemoteProfilesPreferencePage_MissingConnectionNodeError); | |
1159 | return; | |
1160 | } | |
1161 | } | |
1162 | setValid(true); | |
1163 | setErrorMessage(null); | |
1164 | } | |
1165 | ||
1166 | @Override | |
1167 | public boolean performOk() { | |
f8bfc0e5 | 1168 | return writeProfiles(fProfiles, fProfileFilePath); |
9ba2101a PT |
1169 | } |
1170 | ||
1171 | /** | |
1172 | * Set the selected profile name | |
1173 | * | |
1174 | * @param profileName the selected profile name | |
1175 | */ | |
1176 | public void setSelectedProfile(String profileName) { | |
1177 | fSelectedProfileName = profileName; | |
1178 | if (fTreeViewer != null && !fTreeViewer.getTree().isDisposed()) { | |
1179 | for (RemoteImportProfileElement profile : fProfiles) { | |
1180 | if (profile.getProfileName().equals(profileName)) { | |
1181 | fTreeViewer.setSelection(new StructuredSelection(profile)); | |
1182 | } | |
1183 | } | |
1184 | } | |
1185 | } | |
1186 | ||
1187 | /** | |
1188 | * Return the selected profile name | |
1189 | * | |
1190 | * @return the selected profile name or null | |
1191 | */ | |
1192 | public String getSelectedProfile() { | |
1193 | return fSelectedProfileName; | |
1194 | } | |
1195 | } |