Merge branch 'master' into lttng-kepler
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / widgets / tabsview / TmfViewerFolder.java
CommitLineData
05627bda
MD
1/*******************************************************************************
2 * Copyright (c) 2012 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
13package org.eclipse.linuxtools.tmf.ui.widgets.tabsview;
14
15import java.util.Collection;
16import java.util.HashMap;
17
18import org.eclipse.linuxtools.tmf.ui.viewers.ITmfViewer;
19import org.eclipse.swt.SWT;
20import org.eclipse.swt.SWTException;
21import org.eclipse.swt.custom.CTabFolder;
22import org.eclipse.swt.custom.CTabItem;
23import org.eclipse.swt.layout.FillLayout;
24import 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 */
36public 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<String, ITmfViewer>();
71 initFolder();
72 }
73
74 /*
75 * (non-Javadoc)
76 *
77 * @see org.eclipse.swt.widgets.Widget#dispose()
78 */
79 @Override
80 public void dispose() {
81 super.dispose();
82 for (ITmfViewer viewer : fViewers.values()) {
83 viewer.dispose();
84 }
85 if (fFolder != null) {
86 fFolder.dispose();
87 }
88 }
89
90 /**
91 * Disposes of all the viewers contained in the folder and restart to a
92 * clean state.
93 */
94 public void clear() {
95 for (ITmfViewer viewer : fViewers.values()) {
96 viewer.dispose();
97 }
98 fViewers.clear();
99 fFolder.dispose();
100 initFolder();
101 }
102
103 /**
104 * Create a new tab that will hold the viewer content. The viewer name will
105 * be used as the name for the tab. The viewer ID must be unique and can be
106 * used to retrieve the viewer from the folder.
107 *
108 * The parent of the viewer control must be the folder returned by
109 * {@link #getParentFolder()}
110 *
111 * @param viewer
112 * The viewer to put in the new tab
113 * @param viewerID
114 * The ID that will be assigned to this viewer for easy
115 * retrieving
116 * @param style
117 * The style of the widget to build
118 * @return true on success, false otherwise
119 */
120 public boolean addTab(ITmfViewer viewer, String viewerID, int style) {
121 if (fFolder == null
122 || viewer.getControl().getParent() != fFolder
123 || fViewers.containsKey(viewerID)) {
124 return false;
125 }
126 CTabItem item = new CTabItem(fFolder, style);
127 item.setText(viewer.getName());
128 item.setControl(viewer.getControl());
129 // Register the viewer in the map to dispose it at closing time
130 fViewers.put(viewerID, viewer);
131 return true;
132 }
133
134 /**
135 * Gets the folder that will be use as the parent of tabs that will hold the
136 * viewer.
137 *
138 * In order to be able to add new tabs in this view, the parent of the
139 * viewer control has to be this composite.
140 *
141 * @return the folder composite to use as the parent for the viewer control
142 * to create.
143 */
144 public Composite getParentFolder() {
145 return fFolder;
146 }
147
148 /**
149 * Gets a viewer based on his name.
150 *
151 * @param viewerName
152 * The name of the viewer to find in the folder
153 * @return The viewer which name is viewerName, or null if there is no such
154 * viewer
155 */
156 public ITmfViewer getViewer(String viewerName) {
157 return fViewers.get(viewerName);
158 }
159
160 /**
161 * Gets the viewers list contained in the folder view. The list can return
162 * the viewers in any order. It is not to be assumed that the viewers are
163 * returned in the same order as they were inserted.
164 *
165 * @return a collection of viewers contained in this view.
166 */
167 public Collection<ITmfViewer> getViewers() {
168 return fViewers.values();
169 }
170
171 /**
172 * Selects the tab at the specified index from the insertion order
173 *
174 * @param index
175 * The index of the tab to be selected
176 * @throws SWTException
177 * <ul>
178 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed
179 * </li>
180 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the
181 * thread that created the receiver</li>
182 * </ul>
183 */
184 public void setSelection(int index) throws SWTException {
185 fFolder.setSelection(index);
186 }
187
188 /**
189 * Initializes the folder or put it a back to a clean state.
190 */
191 private void initFolder() {
192 if (fFolder != null) {
193 fFolder.dispose();
194 }
195 fFolder = new CTabFolder(this, SWT.LEFT | SWT.BORDER);
196 fFolder.setSimple(false);
197 }
198}
This page took 0.030922 seconds and 5 git commands to generate.