Introduce "New view" action for views based on TmfView
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.ui / src / org / eclipse / tracecompass / tmf / ui / views / TmfViewFactory.java
CommitLineData
c389833c
JR
1/**********************************************************************
2 * Copyright (c) 2016 EfficiOS Inc.
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
10package org.eclipse.tracecompass.tmf.ui.views;
11
12import java.util.UUID;
13
14import org.eclipse.jdt.annotation.NonNullByDefault;
15import org.eclipse.jdt.annotation.Nullable;
16import org.eclipse.ui.IViewPart;
17import org.eclipse.ui.IWorkbench;
18import org.eclipse.ui.IWorkbenchPage;
19import org.eclipse.ui.IWorkbenchWindow;
20import org.eclipse.ui.PartInitException;
21import org.eclipse.ui.PlatformUI;
22
23import com.google.common.annotations.VisibleForTesting;
24
25/**
26 * Factory for TmfView.
27 *
28 * @author Jonathan Rajotte Julien
29 * @since 2.2
30 */
31public final class TmfViewFactory {
32
33 /**
34 * The separator used for secondary id internal use. This allow to have
35 * multiple level of information inside the secondary id.
36 */
37 @VisibleForTesting
38 public static final String INTERNAL_SECONDARY_ID_SEPARATOR = "&"; //$NON-NLS-1$
39
40 /**
41 * Empty constructor
42 */
43 private TmfViewFactory() {}
44
45 /**
46 * Create a new view. <br>
47 *
48 * If a view with the corresponding id already exist and no suffix were
49 * added the existing view will be given focus.
50 *
51 * @param viewId
52 * The id of the view to be created. <br>
53 * Format: <primary_id>[:secondary_id][&third_id]..[&n_id]
54 * @param generateSuffix
55 * Add a generated suffix id (UUID). This allow multiple view of
56 * the same id to be displayed.
57 * @return
58 * NULL if an error occurred.
59 * The view instance.
60 */
61 @NonNullByDefault
62 public static @Nullable IViewPart newView(String viewId, boolean generateSuffix) {
63 IViewPart viewPart = null;
64 String primaryId = null;
65 String secondaryId = null;
66
67 /* Parse the view id */
68 if (viewId.contains(TmfView.PRIMARY_SECONDARY_ID_SEPARATOR)) {
69 int index = viewId.indexOf(TmfView.PRIMARY_SECONDARY_ID_SEPARATOR);
70
71 primaryId = viewId.substring(0, index);
72 secondaryId = getBaseSecId(viewId.substring(index + 1));
73
74 } else {
75 primaryId = viewId;
76 }
77
78 if (generateSuffix) {
79 if (secondaryId == null) {
80 secondaryId = UUID.randomUUID().toString();
81 } else {
82 secondaryId += INTERNAL_SECONDARY_ID_SEPARATOR + UUID.randomUUID().toString();
83 }
84 }
85
86 IWorkbench workbench = PlatformUI.getWorkbench();
87 IWorkbenchWindow workbenchWindow = workbench.getActiveWorkbenchWindow();
88 IWorkbenchPage page = workbenchWindow.getActivePage();
89 try {
90 viewPart = page.showView(primaryId, secondaryId, IWorkbenchPage.VIEW_ACTIVATE);
91 page.activate(viewPart);
92 } catch (PartInitException e) {
93 /* Simply return null on error */
94 }
95
96 return viewPart;
97 }
98
99 /**
100 * Parse a secondary id and return the base secondary id minus any generated
101 * suffix (UUID).
102 *
103 * @param secId
104 * A view secondary id
105 * @return NULL when the passed string is a UUID. <br>
106 * The base secondary id
107 */
108 public static String getBaseSecId(String secId) {
109 if (secId == null) {
110 return null;
111 }
112
113 if (!secId.contains(INTERNAL_SECONDARY_ID_SEPARATOR)) {
114 if (isUUID(secId)) {
115 return null;
116 }
117 return secId;
118 }
119
120 int lastIndexSepartor = secId.lastIndexOf(INTERNAL_SECONDARY_ID_SEPARATOR);
121
122 /**
123 * Validate that the right side of the separator is a UUID since the
124 * separator could be a valid value from the base secondary id.
125 */
126 String potentialUUID = secId.substring(lastIndexSepartor + 1);
127
128 if (!isUUID(potentialUUID)) {
129 return secId;
130 }
131
132 return secId.substring(0, lastIndexSepartor);
133 }
134
135 /**
136 * Utility method for testing if a string is a valid full length UUID.
137 * <br>
138 * <pre>
139 * e.g:
140 * 9eaf1840-8a87-4314-a8b7-03e3eccf4766 -> true
141 * 1-1-1-1-1 -> false
142 * </pre>
143 *
144 * @param uuid
145 * The string to test
146 * @return If the passed string is a UUID
147 */
148 private static boolean isUUID(String uuid) {
149 if (uuid == null) {
150 return false;
151 }
152
153 try {
154 /*
155 * UUID.fromString does not check for length wise valid UUID only the
156 * UUID form so check if the reverse operation is valid.
157 */
158 UUID fromStringUUID = UUID.fromString(uuid);
159 String toStringUUID = fromStringUUID.toString();
160 return toStringUUID.equals(uuid);
161 } catch (IllegalArgumentException e) {
162 /**
163 * The substring is not a UUID. Assume that the separator come from
164 * the initial secondaryId.
165 */
166 return false;
167 }
168 }
169}
This page took 0.028825 seconds and 5 git commands to generate.