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