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 / TmfView.java
CommitLineData
b0d3496e 1/*******************************************************************************
1cefda97 2 * Copyright (c) 2009, 2016 Ericsson
db59ddc2 3 *
b0d3496e
ASL
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
013a5f1c 8 *
b0d3496e
ASL
9 * Contributors:
10 * Francois Chouinard - Initial API and implementation
db59ddc2 11 * Bernd Hufmann - Added possibility to pin view
d2e4afa7 12 * Marc-Andre Laperle - Support for view alignment
b0d3496e
ASL
13 *******************************************************************************/
14
2bdf0193 15package org.eclipse.tracecompass.tmf.ui.views;
b0d3496e 16
c389833c
JR
17import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
18
19import org.eclipse.jface.action.IMenuManager;
db59ddc2
BH
20import org.eclipse.jface.action.IToolBarManager;
21import org.eclipse.jface.action.Separator;
d2e4afa7
MAL
22import org.eclipse.swt.events.ControlAdapter;
23import org.eclipse.swt.events.ControlEvent;
24import org.eclipse.swt.widgets.Composite;
25import org.eclipse.tracecompass.internal.tmf.ui.views.TimeAlignViewsAction;
26import org.eclipse.tracecompass.internal.tmf.ui.views.TmfAlignmentSynchronizer;
2bdf0193
AM
27import org.eclipse.tracecompass.tmf.core.component.ITmfComponent;
28import org.eclipse.tracecompass.tmf.core.signal.TmfSignal;
29import org.eclipse.tracecompass.tmf.core.signal.TmfSignalManager;
1cefda97 30import org.eclipse.ui.IActionBars;
d2e4afa7 31import org.eclipse.ui.IPartListener;
dd8c2fe6 32import org.eclipse.ui.IViewSite;
db59ddc2 33import org.eclipse.ui.IWorkbenchActionConstants;
d2e4afa7 34import org.eclipse.ui.IWorkbenchPart;
b0d3496e
ASL
35import org.eclipse.ui.part.ViewPart;
36
37/**
2f2265e2 38 * Basic abstract TMF view class implementation.
db59ddc2 39 *
2f2265e2 40 * It registers any sub class to the signal manager for receiving and sending
db59ddc2
BH
41 * TMF signals.
42 *
2f2265e2 43 * @author Francois Chouinard
b0d3496e
ASL
44 */
45public abstract class TmfView extends ViewPart implements ITmfComponent {
46
3ac5721a 47 private final String fName;
d2e4afa7
MAL
48 /** This allows us to keep track of the view sizes */
49 private Composite fParentComposite;
50 private ControlAdapter fControlListener;
51 private static final TmfAlignmentSynchronizer TIME_ALIGNMENT_SYNCHRONIZER = new TmfAlignmentSynchronizer();
248af329 52
3ac5721a
AM
53 /**
54 * Action class for pinning of TmfView.
3ac5721a
AM
55 */
56 protected PinTmfViewAction fPinAction;
c389833c
JR
57
58 /**
59 * Action class for spawning a new view based on this view type.
60 *
61 * @since 2.2
62 */
63 private NewTmfViewAction fNewAction;
64
d2e4afa7 65 private static TimeAlignViewsAction fAlignViewsAction;
3ac5721a 66
c389833c
JR
67 /**
68 * The separator used for distinguishing between primary and secondary id of a view id.
69 * @since 2.2
70 */
71 public static final String PRIMARY_SECONDARY_ID_SEPARATOR = ":"; //$NON-NLS-1$
72
3ac5721a
AM
73 // ------------------------------------------------------------------------
74 // Constructor
75 // ------------------------------------------------------------------------
76
77 /**
78 * Constructor. Creates a TMF view and registers to the signal manager.
79 *
80 * @param viewName
81 * A view name
82 */
83 public TmfView(String viewName) {
84 super();
85 fName = viewName;
86 TmfSignalManager.register(this);
87 }
88
89 /**
90 * Disposes this view and de-registers itself from the signal manager
91 */
92 @Override
93 public void dispose() {
94 TmfSignalManager.deregister(this);
1cefda97
PT
95
96 /* Workaround for Bug 490400: Clear the action bars */
97 IActionBars bars = getViewSite().getActionBars();
98 bars.getToolBarManager().removeAll();
99 bars.getMenuManager().removeAll();
100
3ac5721a
AM
101 super.dispose();
102 }
103
104 // ------------------------------------------------------------------------
105 // ITmfComponent
106 // ------------------------------------------------------------------------
107
108 @Override
109 public String getName() {
110 return fName;
111 }
112
113 @Override
114 public void broadcast(TmfSignal signal) {
115 TmfSignalManager.dispatchSignal(signal);
116 }
8d2e2848 117
d91063d0
BH
118 @Override
119 public void broadcastAsync(TmfSignal signal) {
120 TmfSignalManager.dispatchSignalAsync(signal);
121 }
122
ea279a69
FC
123 // ------------------------------------------------------------------------
124 // View pinning support
125 // ------------------------------------------------------------------------
126
db59ddc2
BH
127 /**
128 * Returns whether the pin flag is set.
129 * For example, this flag can be used to ignore time synchronization signals from other TmfViews.
130 *
131 * @return pin flag
db59ddc2
BH
132 */
133 public boolean isPinned() {
134 return ((fPinAction != null) && (fPinAction.isChecked()));
135 }
136
137 /**
138 * Method adds a pin action to the TmfView. The pin action allows to toggle the <code>fIsPinned</code> flag.
139 * For example, this flag can be used to ignore time synchronization signals from other TmfViews.
db59ddc2
BH
140 */
141 protected void contributePinActionToToolBar() {
142 if (fPinAction == null) {
143 fPinAction = new PinTmfViewAction();
144
145 IToolBarManager toolBarManager = getViewSite().getActionBars()
146 .getToolBarManager();
147 toolBarManager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
148 toolBarManager.add(fPinAction);
149 }
150 }
d2e4afa7 151
c389833c
JR
152 /**
153 * Add the "New view" action to the expandable menu. This action spawn a new
154 * view of the same type as the caller.
155 *
156 * @since 2.2
157 */
158 private void contributeNewActionToMenu(IMenuManager menuManager) {
159 if (fNewAction == null) {
160 fNewAction = new NewTmfViewAction(TmfView.this) {
161 @Override
162 public void run() {
163 TmfViewFactory.newView(checkNotNull(TmfView.this.getViewId()), true);
164 }
165 };
166 menuManager.add(fNewAction);
167 }
168 }
169
d2e4afa7
MAL
170 @Override
171 public void createPartControl(final Composite parent) {
172 fParentComposite = parent;
c389833c
JR
173 IMenuManager menuManager = getViewSite().getActionBars()
174 .getMenuManager();
175
176 /* Add to menu */
177 contributeNewActionToMenu(menuManager);
178 menuManager.add(new Separator());
179
180
d2e4afa7
MAL
181 if (this instanceof ITmfTimeAligned) {
182 contributeAlignViewsActionToToolbar();
183
184 fControlListener = new ControlAdapter() {
185 @Override
186 public void controlResized(ControlEvent e) {
187 TIME_ALIGNMENT_SYNCHRONIZER.handleViewResized(TmfView.this);
188 }
189 };
190 parent.addControlListener(fControlListener);
191
192 getSite().getPage().addPartListener(new IPartListener() {
193 @Override
194 public void partOpened(IWorkbenchPart part) {
195 // do nothing
196 }
197
198 @Override
199 public void partDeactivated(IWorkbenchPart part) {
200 // do nothing
201 }
202
203 @Override
204 public void partClosed(IWorkbenchPart part) {
205 if (part == TmfView.this && fControlListener != null && !fParentComposite.isDisposed()) {
206 fParentComposite.removeControlListener(fControlListener);
207 fControlListener = null;
208 getSite().getPage().removePartListener(this);
209 TIME_ALIGNMENT_SYNCHRONIZER.handleViewClosed(TmfView.this);
210 }
211 }
212
213 @Override
214 public void partBroughtToTop(IWorkbenchPart part) {
215 // do nothing
216 }
217
218 @Override
219 public void partActivated(IWorkbenchPart part) {
220 // do nothing
221 }
222 });
223 }
224 }
225
226 private void contributeAlignViewsActionToToolbar() {
227 if (fAlignViewsAction == null) {
228 fAlignViewsAction = new TimeAlignViewsAction();
229 }
230
231 IToolBarManager toolBarManager = getViewSite().getActionBars()
232 .getToolBarManager();
233 toolBarManager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
234 toolBarManager.add(fAlignViewsAction);
235 }
236
237 /**
238 * Returns the parent control of the view
239 *
240 * @return the parent control
241 *
242 * @since 1.0
243 */
244 public Composite getParentComposite() {
245 return fParentComposite;
246 }
dd8c2fe6
GB
247
248 /**
249 * Return the Eclipse view ID in the format <Primary ID>:<Secondary ID> or
250 * simply <Primary ID> if secondary ID is null
251 *
252 * @return This view's view ID
f625ccd4 253 * @since 2.2
dd8c2fe6
GB
254 */
255 protected String getViewId() {
256 IViewSite viewSite = getViewSite();
257 String secondaryId = viewSite.getSecondaryId();
258 if (secondaryId == null) {
259 return viewSite.getId();
260 }
c389833c 261 return viewSite.getId() + PRIMARY_SECONDARY_ID_SEPARATOR + secondaryId;
dd8c2fe6 262 }
013a5f1c 263}
This page took 0.136057 seconds and 5 git commands to generate.