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