1
/*****************************************************************
2
* Copyright (c) 2010, 2011 Texas Instruments and others
3
* All rights reserved. This program and the accompanying materials
4
* are made available under the terms of the Eclipse Public License v1.0
5
* which accompanies this distribution, and is available at
6
* http://www.eclipse.org/legal/epl-v10.html
9
* Patrick Chuong (Texas Instruments) - Pin and Clone Supports (331781)
10
*****************************************************************/
11
package org.eclipse.cdt.debug.internal.ui.pinclone;
13
import java.util.Collections;
14
import java.util.HashMap;
15
import java.util.HashSet;
19
import org.eclipse.cdt.debug.ui.CDebugUIPlugin;
20
import org.eclipse.core.runtime.IProgressMonitor;
21
import org.eclipse.core.runtime.IStatus;
22
import org.eclipse.core.runtime.Status;
23
import org.eclipse.ui.IPartListener2;
24
import org.eclipse.ui.IViewPart;
25
import org.eclipse.ui.IViewReference;
26
import org.eclipse.ui.IWindowListener;
27
import org.eclipse.ui.IWorkbench;
28
import org.eclipse.ui.IWorkbenchListener;
29
import org.eclipse.ui.IWorkbenchPart;
30
import org.eclipse.ui.IWorkbenchPartReference;
31
import org.eclipse.ui.IWorkbenchWindow;
32
import org.eclipse.ui.PlatformUI;
33
import org.eclipse.ui.progress.WorkbenchJob;
36
* This class provides counter id for view that support multiple instances.
37
* It is assumed that view will use the counter id for it's secondary id.
39
public final class ViewIDCounterManager {
40
private static ViewIDCounterManager INSTANCE;
41
private static boolean fInitialized = false;
43
private boolean fShuttingDown = false;
44
private final Map<String, Set<Integer>> viewIdToNextCounterMap = Collections.synchronizedMap(new HashMap<String, Set<Integer>>());
46
private ViewIDCounterManager() {
51
* Returns an instance of the view id counter manager.
53
* @return the counter manager.
55
synchronized public static ViewIDCounterManager getInstance() {
56
if (INSTANCE == null) {
57
INSTANCE = new ViewIDCounterManager();
63
* Initialize this view ID counter manager. Catch up opened view and set the title
64
* accordingly from the view's secondary id.
66
synchronized public void init() {
67
if (fInitialized) return;
70
new WorkbenchJob("Initializing pinnable view") { //$NON-NLS-1$
73
public IStatus runInUIThread(IProgressMonitor monitor) {
74
IWorkbenchWindow[] windows = PlatformUI.getWorkbench().getWorkbenchWindows();
75
for (IWorkbenchWindow window : windows) {
76
IViewReference[] viewRefs = window.getActivePage().getViewReferences();
77
for (IViewReference viewRef : viewRefs) {
79
// initialize the view id counter map
80
if (PinCloneUtils.isClonedPart(viewRef)) {
81
String id = viewRef.getId();
82
String secondaryId = viewRef.getSecondaryId();
83
Set<Integer> secondaryIdSet = viewIdToNextCounterMap.get(id);
84
if (secondaryIdSet == null) {
85
secondaryIdSet = new HashSet<Integer>();
86
viewIdToNextCounterMap.put(id, secondaryIdSet);
88
secondaryId = PinCloneUtils.decodeClonedPartSecondaryId(secondaryId);
89
secondaryIdSet.add(Integer.valueOf(secondaryId));
93
IViewPart part = viewRef.getView(false);
94
if (part != null && PinCloneUtils.isClonedPart(part)) {
95
PinCloneUtils.setPartTitle(part);
97
} catch (Exception e) {
98
CDebugUIPlugin.log(e);
102
return Status.OK_STATUS;
107
private void initListeners() {
109
// add a workbench listener to listen to preShutdown and ignore view part close event
110
IWorkbench wb = PlatformUI.getWorkbench();
111
wb.addWorkbenchListener(new IWorkbenchListener() {
112
public void postShutdown(IWorkbench workbench) {}
114
public boolean preShutdown(IWorkbench workbench, boolean forced) {
115
fShuttingDown = true;
120
final IPartListener2 partListener = new IPartListener2() {
121
public void partVisible(IWorkbenchPartReference partRef) {}
122
public void partInputChanged(IWorkbenchPartReference partRef) {}
123
public void partHidden(IWorkbenchPartReference partRef) {}
124
public void partDeactivated(IWorkbenchPartReference partRef) {}
125
public void partBroughtToTop(IWorkbenchPartReference partRef) {}
126
public void partActivated(IWorkbenchPartReference partRef) {}
128
public void partOpened(IWorkbenchPartReference partRef) {
129
if (partRef instanceof IViewReference) {
130
IViewPart part = ((IViewReference) partRef).getView(false);
131
if (part != null && PinCloneUtils.isClonedPart(part)) {
132
PinCloneUtils.setPartTitle(part);
137
public void partClosed(IWorkbenchPartReference partRef) {
139
recycleCounterId(partRef);
143
// subscribe to existing workbench window listener
144
for (IWorkbenchWindow ww : wb.getWorkbenchWindows()) {
145
ww.getPartService().addPartListener(partListener);
148
// subscribe to new workbench window listener
149
wb.addWindowListener(new IWindowListener() {
150
public void windowDeactivated(IWorkbenchWindow window) {}
151
public void windowActivated(IWorkbenchWindow window) {}
152
public void windowClosed(IWorkbenchWindow window) {}
154
public void windowOpened(IWorkbenchWindow window) {
155
window.getPartService().addPartListener(partListener);
158
} catch (Exception e) {
159
CDebugUIPlugin.log(e);
163
private void recycleCounterId(IWorkbenchPartReference partRef) {
164
if (partRef instanceof IViewReference) {
165
IViewReference viewRef = ((IViewReference) partRef);
166
IWorkbenchPart part = viewRef.getPart(false);
167
if ( !(part instanceof IViewPart) || !PinCloneUtils.isClonedPart((IViewPart) part))
170
String viewId = viewRef.getId();
171
String secondaryId = viewRef.getSecondaryId();
173
if (secondaryId != null) {
174
Set<Integer> secondaryIdSet = viewIdToNextCounterMap.get(viewId);
175
if (secondaryIdSet != null) {
176
secondaryIdSet.remove(new Integer(PinCloneUtils.decodeClonedPartSecondaryId(secondaryId)));
182
public Integer getNextCounter(String viewId) {
183
Set<Integer> secondaryIdSet = viewIdToNextCounterMap.get(viewId);
184
if (secondaryIdSet == null) {
185
secondaryIdSet = new HashSet<Integer>();
186
viewIdToNextCounterMap.put(viewId, secondaryIdSet);
189
for (int i = 1; i < Integer.MAX_VALUE; ++i) {
190
Integer next = new Integer(i);
191
if (!secondaryIdSet.contains(next)) {
192
secondaryIdSet.add(next);
197
return Integer.valueOf(0);