tmf: Log the exception when error occurs while opening a trace
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.ui / src / org / eclipse / tracecompass / tmf / ui / project / model / TraceUtils.java
1 /*******************************************************************************
2 * Copyright (c) 2013, 2014 École Polytechnique de Montréal and others
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 * Geneviève Bastien - Initial API and implementation
11 * Marc-Andre Laperle - Add method to get opened tmf projects
12 * Patrick Tasse - Add support for folder elements
13 *******************************************************************************/
14
15 package org.eclipse.tracecompass.tmf.ui.project.model;
16
17 import java.util.ArrayList;
18 import java.util.List;
19
20 import org.eclipse.core.resources.IFolder;
21 import org.eclipse.core.resources.IProject;
22 import org.eclipse.core.resources.ResourcesPlugin;
23 import org.eclipse.core.runtime.CoreException;
24 import org.eclipse.core.runtime.IProgressMonitor;
25 import org.eclipse.jface.dialogs.MessageDialog;
26 import org.eclipse.swt.widgets.Display;
27 import org.eclipse.swt.widgets.Shell;
28 import org.eclipse.tracecompass.internal.tmf.ui.Activator;
29 import org.eclipse.tracecompass.tmf.core.TmfProjectNature;
30 import org.eclipse.ui.PlatformUI;
31
32 /**
33 * Utility class for common tmf.ui functionalities
34 */
35 public class TraceUtils {
36
37 private TraceUtils() {
38 }
39
40 /**
41 * Displays an error message in a box
42 *
43 * @param boxTitle
44 * The message box title
45 * @param errorMsg
46 * The error message to display
47 */
48 public static void displayErrorMsg(final String boxTitle, final String errorMsg) {
49 displayErrorMsg(boxTitle, errorMsg, null);
50 }
51
52 /**
53 * Displays an error message in a box
54 *
55 * @param boxTitle
56 * The message box title
57 * @param errorMsg
58 * The error message to display
59 * @param exception
60 * the exception or null if the error does not originate from an
61 * exception
62 * @since 2.2
63 */
64 public static void displayErrorMsg(final String boxTitle, final String errorMsg, Throwable exception) {
65 Display.getDefault().asyncExec(new Runnable() {
66 @Override
67 public void run() {
68 final Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
69 Activator.getDefault().logError(errorMsg, exception);
70 MessageDialog.openError(shell, boxTitle, errorMsg);
71 }
72 });
73 }
74
75 /**
76 * Get the opened (accessible) projects with Tmf nature
77 *
78 * @return the Tmf projects
79 */
80 public static List<IProject> getOpenedTmfProjects() {
81 IProject[] projects = ResourcesPlugin.getWorkspace().getRoot().getProjects();
82 List<IProject> tmfProjects = new ArrayList<>();
83 for (IProject project : projects) {
84 try {
85 if (project.isAccessible() && project.getNature(TmfProjectNature.ID) != null) {
86 tmfProjects.add(project);
87 }
88 } catch (CoreException e) {
89 Activator.getDefault().logError("Error getting opened tmf projects", e); //$NON-NLS-1$
90 }
91 }
92 return tmfProjects;
93 }
94
95 /**
96 * Create a folder, ensuring all parent folders are also created.
97 *
98 * @param folder
99 * the folder to create
100 * @param monitor
101 * the progress monitor
102 * @throws CoreException
103 * if the folder cannot be created
104 */
105 public static void createFolder(IFolder folder, IProgressMonitor monitor) throws CoreException {
106 if (!folder.exists()) {
107 if (folder.getParent() instanceof IFolder) {
108 createFolder((IFolder) folder.getParent(), monitor);
109 }
110 folder.create(true, true, monitor);
111 }
112 }
113
114 }
This page took 0.03649 seconds and 5 git commands to generate.