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