tmf: fix potential bug when selecting trace types in import wizard
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / project / wizards / importtrace / ImportTraceWizardPageOptions.java
1 /*******************************************************************************
2 * Copyright (c) 2013 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 * Matthew Khouzam - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.linuxtools.tmf.ui.project.wizards.importtrace;
14
15 import java.util.LinkedHashMap;
16 import java.util.Map;
17
18 import org.eclipse.core.resources.IFolder;
19 import org.eclipse.core.resources.IProject;
20 import org.eclipse.core.resources.ResourcesPlugin;
21 import org.eclipse.core.runtime.CoreException;
22 import org.eclipse.jface.viewers.IStructuredSelection;
23 import org.eclipse.linuxtools.tmf.core.TmfProjectNature;
24 import org.eclipse.swt.SWT;
25 import org.eclipse.swt.events.SelectionEvent;
26 import org.eclipse.swt.events.SelectionListener;
27 import org.eclipse.swt.layout.GridData;
28 import org.eclipse.swt.layout.GridLayout;
29 import org.eclipse.swt.widgets.Composite;
30 import org.eclipse.swt.widgets.List;
31 import org.eclipse.ui.IWorkbench;
32
33 /**
34 * This page selects the project to import to.
35 *
36 * @author Matthew Khouzam
37 * @since 2.0
38 */
39 public class ImportTraceWizardPageOptions extends AbstractImportTraceWizardPage {
40
41 private List fProjects;
42 private final Map<String, IProject> fProjectsMap = new LinkedHashMap<String, IProject>();
43
44 /**
45 * Import page that tells where the trace will go
46 *
47 * @param workbench
48 * The workbench reference.
49 * @param selection
50 * The current selection
51 */
52 public ImportTraceWizardPageOptions(IWorkbench workbench, IStructuredSelection selection) {
53 super(workbench, selection);
54 }
55
56 @Override
57 public void createControl(Composite parent) {
58 super.createControl(parent);
59 IFolder originalFolder = getBatchWizard().getTargetFolder();
60 IProject proj = null;
61 if (originalFolder != null) {
62 proj = originalFolder.getProject();
63 }
64
65 Composite optionPane = (Composite) this.getControl();
66 optionPane.setLayout(new GridLayout());
67 optionPane.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, true, true));
68
69 IProject[] projects = ResourcesPlugin.getWorkspace().getRoot().getProjects();
70
71 fProjects = new List(optionPane, SWT.NONE);
72 fProjects.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
73
74 for (IProject project : projects) {
75 try {
76 if (project.getNature(TmfProjectNature.ID) != null) {
77 final String name = project.getName();
78 fProjectsMap.put(name, project);
79 fProjects.add(name);
80 }
81 } catch (CoreException e) {
82 // TODO: add a logger to activator and then log it
83 }
84 }
85
86 fProjects.getSelection();
87 fProjects.addSelectionListener(new SelectionListener() {
88
89 private static final String TRACE = "Traces"; //$NON-NLS-1$
90
91 @Override
92 public void widgetSelected(SelectionEvent e) {
93 final String listItem = fProjects.getSelection()[0];
94 IFolder folder = fProjectsMap.get(listItem).getFolder(TRACE);
95 getBatchWizard().setTraceFolder(folder);
96 ImportTraceWizardPageOptions.this.setErrorMessage(null);
97 }
98
99 @Override
100 public void widgetDefaultSelected(SelectionEvent e) {
101 final String listItem = fProjects.getSelection()[0];
102 IFolder folder = fProjectsMap.get(listItem).getFolder(TRACE);
103 getBatchWizard().setTraceFolder(folder);
104 ImportTraceWizardPageOptions.this.setErrorMessage(null);
105 }
106 });
107 if (proj != null) {
108 fProjects.setSelection(fProjects.indexOf(proj.getName()));
109 this.setErrorMessage(null);
110 } else {
111 this.setErrorMessage(Messages.SharedSelectProject);
112 }
113 this.setTitle(Messages.ImportTraceWizardPageOptionsTitle);
114 }
115 }
This page took 0.035666 seconds and 6 git commands to generate.