1 /*******************************************************************************
2 * Copyright (c) 2009, 2016 Ericsson
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
10 * Francois Chouinard - Initial API and implementation
11 * Bernd Hufmann - Added possibility to pin view
12 * Marc-Andre Laperle - Support for view alignment
13 *******************************************************************************/
15 package org
.eclipse
.tracecompass
.tmf
.ui
.views
;
17 import static org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
.checkNotNull
;
19 import org
.eclipse
.jface
.action
.IMenuManager
;
20 import org
.eclipse
.jface
.action
.IToolBarManager
;
21 import org
.eclipse
.jface
.action
.Separator
;
22 import org
.eclipse
.swt
.events
.ControlAdapter
;
23 import org
.eclipse
.swt
.events
.ControlEvent
;
24 import org
.eclipse
.swt
.widgets
.Composite
;
25 import org
.eclipse
.tracecompass
.internal
.tmf
.ui
.views
.TimeAlignViewsAction
;
26 import org
.eclipse
.tracecompass
.internal
.tmf
.ui
.views
.TmfAlignmentSynchronizer
;
27 import org
.eclipse
.tracecompass
.tmf
.core
.component
.ITmfComponent
;
28 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfSignal
;
29 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfSignalManager
;
30 import org
.eclipse
.ui
.IActionBars
;
31 import org
.eclipse
.ui
.IPartListener
;
32 import org
.eclipse
.ui
.IViewSite
;
33 import org
.eclipse
.ui
.IWorkbenchActionConstants
;
34 import org
.eclipse
.ui
.IWorkbenchPart
;
35 import org
.eclipse
.ui
.part
.ViewPart
;
38 * Basic abstract TMF view class implementation.
40 * It registers any sub class to the signal manager for receiving and sending
43 * @author Francois Chouinard
45 public abstract class TmfView
extends ViewPart
implements ITmfComponent
{
47 private final String fName
;
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();
54 * Action class for pinning of TmfView.
56 protected PinTmfViewAction fPinAction
;
59 * Action class for spawning a new view based on this view type.
63 private NewTmfViewAction fNewAction
;
65 private static TimeAlignViewsAction fAlignViewsAction
;
68 * The separator used for distinguishing between primary and secondary id of a view id.
71 public static final String PRIMARY_SECONDARY_ID_SEPARATOR
= ":"; //$NON-NLS-1$
73 // ------------------------------------------------------------------------
75 // ------------------------------------------------------------------------
78 * Constructor. Creates a TMF view and registers to the signal manager.
83 public TmfView(String viewName
) {
86 TmfSignalManager
.register(this);
90 * Disposes this view and de-registers itself from the signal manager
93 public void dispose() {
94 TmfSignalManager
.deregister(this);
96 /* Workaround for Bug 490400: Clear the action bars */
97 IActionBars bars
= getViewSite().getActionBars();
98 bars
.getToolBarManager().removeAll();
99 bars
.getMenuManager().removeAll();
104 // ------------------------------------------------------------------------
106 // ------------------------------------------------------------------------
109 public String
getName() {
114 public void broadcast(TmfSignal signal
) {
115 TmfSignalManager
.dispatchSignal(signal
);
119 public void broadcastAsync(TmfSignal signal
) {
120 TmfSignalManager
.dispatchSignalAsync(signal
);
123 // ------------------------------------------------------------------------
124 // View pinning support
125 // ------------------------------------------------------------------------
128 * Returns whether the pin flag is set.
129 * For example, this flag can be used to ignore time synchronization signals from other TmfViews.
133 public boolean isPinned() {
134 return ((fPinAction
!= null) && (fPinAction
.isChecked()));
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.
141 protected void contributePinActionToToolBar() {
142 if (fPinAction
== null) {
143 fPinAction
= new PinTmfViewAction();
145 IToolBarManager toolBarManager
= getViewSite().getActionBars()
146 .getToolBarManager();
147 toolBarManager
.add(new Separator(IWorkbenchActionConstants
.MB_ADDITIONS
));
148 toolBarManager
.add(fPinAction
);
153 * Add the "New view" action to the expandable menu. This action spawn a new
154 * view of the same type as the caller.
158 private void contributeNewActionToMenu(IMenuManager menuManager
) {
159 if (fNewAction
== null) {
160 fNewAction
= new NewTmfViewAction(TmfView
.this) {
163 TmfViewFactory
.newView(checkNotNull(TmfView
.this.getViewId()), true);
166 menuManager
.add(fNewAction
);
171 public void createPartControl(final Composite parent
) {
172 fParentComposite
= parent
;
173 IMenuManager menuManager
= getViewSite().getActionBars()
177 contributeNewActionToMenu(menuManager
);
178 menuManager
.add(new Separator());
181 if (this instanceof ITmfTimeAligned
) {
182 contributeAlignViewsActionToToolbar();
184 fControlListener
= new ControlAdapter() {
186 public void controlResized(ControlEvent e
) {
187 TIME_ALIGNMENT_SYNCHRONIZER
.handleViewResized(TmfView
.this);
190 parent
.addControlListener(fControlListener
);
192 getSite().getPage().addPartListener(new IPartListener() {
194 public void partOpened(IWorkbenchPart part
) {
199 public void partDeactivated(IWorkbenchPart part
) {
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);
214 public void partBroughtToTop(IWorkbenchPart part
) {
219 public void partActivated(IWorkbenchPart part
) {
226 private void contributeAlignViewsActionToToolbar() {
227 if (fAlignViewsAction
== null) {
228 fAlignViewsAction
= new TimeAlignViewsAction();
231 IToolBarManager toolBarManager
= getViewSite().getActionBars()
232 .getToolBarManager();
233 toolBarManager
.add(new Separator(IWorkbenchActionConstants
.MB_ADDITIONS
));
234 toolBarManager
.add(fAlignViewsAction
);
238 * Returns the parent control of the view
240 * @return the parent control
244 public Composite
getParentComposite() {
245 return fParentComposite
;
249 * Return the Eclipse view ID in the format <Primary ID>:<Secondary ID> or
250 * simply <Primary ID> if secondary ID is null
252 * @return This view's view ID
255 protected String
getViewId() {
256 IViewSite viewSite
= getViewSite();
257 String secondaryId
= viewSite
.getSecondaryId();
258 if (secondaryId
== null) {
259 return viewSite
.getId();
261 return viewSite
.getId() + PRIMARY_SECONDARY_ID_SEPARATOR
+ secondaryId
;