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