Commit | Line | Data |
---|---|---|
05627bda | 1 | /******************************************************************************* |
ed902a2b | 2 | * Copyright (c) 2012, 2014 Ericsson |
05627bda MD |
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 | * Mathieu Denis <mathieu.denis@polymtl.ca> - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
2bdf0193 | 13 | package org.eclipse.tracecompass.tmf.ui.widgets.tabsview; |
05627bda MD |
14 | |
15 | import java.util.Collection; | |
16 | import java.util.HashMap; | |
17 | ||
05627bda MD |
18 | import org.eclipse.swt.SWT; |
19 | import org.eclipse.swt.SWTException; | |
20 | import org.eclipse.swt.custom.CTabFolder; | |
21 | import org.eclipse.swt.custom.CTabItem; | |
22 | import org.eclipse.swt.layout.FillLayout; | |
23 | import org.eclipse.swt.widgets.Composite; | |
2bdf0193 | 24 | import org.eclipse.tracecompass.tmf.ui.viewers.ITmfViewer; |
05627bda MD |
25 | |
26 | /** | |
27 | * Allows the user to create multiple tabs which makes it look like folders. It | |
28 | * simplifies the management of the viewer contained in each tab. | |
29 | * | |
30 | * The indexing of the viewers is based on their name. | |
31 | * | |
32 | * @author Mathieu Denis | |
05627bda MD |
33 | */ |
34 | public class TmfViewerFolder extends Composite { | |
35 | ||
36 | /** | |
37 | * The list of viewers in the folder | |
38 | */ | |
39 | private final HashMap<String, ITmfViewer> fViewers; | |
40 | ||
41 | /** | |
42 | * The parent folder that contains all viewers | |
43 | */ | |
44 | private CTabFolder fFolder; | |
45 | ||
46 | /** | |
47 | * Constructor with empty style | |
48 | * | |
49 | * @param parent | |
50 | * The parent composite | |
51 | */ | |
52 | public TmfViewerFolder(Composite parent) { | |
53 | this(parent, SWT.NONE); | |
54 | } | |
55 | ||
56 | /** | |
57 | * Constructor | |
58 | * | |
59 | * @param parent | |
60 | * The parent composite | |
61 | * @param style | |
62 | * The style of the view that will be created | |
63 | */ | |
64 | public TmfViewerFolder(Composite parent, int style) { | |
65 | super(parent, style); | |
66 | setLayout(new FillLayout()); | |
67 | ||
507b1336 | 68 | fViewers = new HashMap<>(); |
05627bda MD |
69 | initFolder(); |
70 | } | |
71 | ||
05627bda MD |
72 | @Override |
73 | public void dispose() { | |
74 | super.dispose(); | |
75 | for (ITmfViewer viewer : fViewers.values()) { | |
76 | viewer.dispose(); | |
77 | } | |
78 | if (fFolder != null) { | |
79 | fFolder.dispose(); | |
80 | } | |
81 | } | |
82 | ||
83 | /** | |
84 | * Disposes of all the viewers contained in the folder and restart to a | |
85 | * clean state. | |
86 | */ | |
87 | public void clear() { | |
88 | for (ITmfViewer viewer : fViewers.values()) { | |
89 | viewer.dispose(); | |
90 | } | |
91 | fViewers.clear(); | |
92 | fFolder.dispose(); | |
93 | initFolder(); | |
94 | } | |
95 | ||
96 | /** | |
97 | * Create a new tab that will hold the viewer content. The viewer name will | |
98 | * be used as the name for the tab. The viewer ID must be unique and can be | |
99 | * used to retrieve the viewer from the folder. | |
100 | * | |
101 | * The parent of the viewer control must be the folder returned by | |
102 | * {@link #getParentFolder()} | |
103 | * | |
104 | * @param viewer | |
105 | * The viewer to put in the new tab | |
106 | * @param viewerID | |
107 | * The ID that will be assigned to this viewer for easy | |
108 | * retrieving | |
109 | * @param style | |
110 | * The style of the widget to build | |
111 | * @return true on success, false otherwise | |
112 | */ | |
113 | public boolean addTab(ITmfViewer viewer, String viewerID, int style) { | |
114 | if (fFolder == null | |
115 | || viewer.getControl().getParent() != fFolder | |
116 | || fViewers.containsKey(viewerID)) { | |
117 | return false; | |
118 | } | |
119 | CTabItem item = new CTabItem(fFolder, style); | |
120 | item.setText(viewer.getName()); | |
121 | item.setControl(viewer.getControl()); | |
122 | // Register the viewer in the map to dispose it at closing time | |
123 | fViewers.put(viewerID, viewer); | |
124 | return true; | |
125 | } | |
126 | ||
127 | /** | |
128 | * Gets the folder that will be use as the parent of tabs that will hold the | |
129 | * viewer. | |
130 | * | |
131 | * In order to be able to add new tabs in this view, the parent of the | |
132 | * viewer control has to be this composite. | |
133 | * | |
134 | * @return the folder composite to use as the parent for the viewer control | |
135 | * to create. | |
136 | */ | |
137 | public Composite getParentFolder() { | |
138 | return fFolder; | |
139 | } | |
140 | ||
141 | /** | |
142 | * Gets a viewer based on his name. | |
143 | * | |
144 | * @param viewerName | |
145 | * The name of the viewer to find in the folder | |
146 | * @return The viewer which name is viewerName, or null if there is no such | |
147 | * viewer | |
148 | */ | |
149 | public ITmfViewer getViewer(String viewerName) { | |
150 | return fViewers.get(viewerName); | |
151 | } | |
152 | ||
153 | /** | |
154 | * Gets the viewers list contained in the folder view. The list can return | |
155 | * the viewers in any order. It is not to be assumed that the viewers are | |
156 | * returned in the same order as they were inserted. | |
157 | * | |
158 | * @return a collection of viewers contained in this view. | |
159 | */ | |
160 | public Collection<ITmfViewer> getViewers() { | |
161 | return fViewers.values(); | |
162 | } | |
163 | ||
164 | /** | |
165 | * Selects the tab at the specified index from the insertion order | |
166 | * | |
167 | * @param index | |
168 | * The index of the tab to be selected | |
169 | * @throws SWTException | |
170 | * <ul> | |
171 | * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed | |
172 | * </li> | |
173 | * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the | |
174 | * thread that created the receiver</li> | |
175 | * </ul> | |
176 | */ | |
177 | public void setSelection(int index) throws SWTException { | |
178 | fFolder.setSelection(index); | |
179 | } | |
180 | ||
181 | /** | |
182 | * Initializes the folder or put it a back to a clean state. | |
183 | */ | |
184 | private void initFolder() { | |
185 | if (fFolder != null) { | |
186 | fFolder.dispose(); | |
187 | } | |
188 | fFolder = new CTabFolder(this, SWT.LEFT | SWT.BORDER); | |
189 | fFolder.setSimple(false); | |
190 | } | |
191 | } |