Skip to main content
summaryrefslogtreecommitdiffstats
blob: d00ffaf37cc9e5b90ccf1771c062fe7ce81e74b1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*******************************************************************************
 * Copyright (c) 2013 Wind River Systems and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     Wind River Systems - initial API and implementation
 *******************************************************************************/
package org.eclipse.debug.ui.contexts;

import org.eclipse.debug.internal.ui.DebugUIPlugin;
import org.eclipse.debug.internal.ui.SWTFactory;
import org.eclipse.debug.ui.AbstractDebugView;
import org.eclipse.debug.ui.DebugUITools;
import org.eclipse.debug.ui.IDebugUIConstants;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.ui.IMemento;

/**
 * Common debug view extension to support pinning to debug context. It 
 * implements creating and clearing the pin control.  Also, adds the pin and 
 * clone actions to the view's tool-bar. 
 * 
 * @since 3.9
 */
abstract public class AbstractPinnableDebugView extends AbstractDebugView implements IPinnablePart {

	private static final String PINNED_CONTEXT_VIEWER_ID = DebugUIPlugin.getUniqueIdentifier() + ".PINNED_CONTEXT_VIEWER_ID"; //$NON-NLS-1$
	
    private Composite fComposite;
    private IPinnedContextViewer fPinnedContextViewer;

    public void pin(IPinnedContextFactory factory) {
    	if (isPinned()) clearPin();
        fPinnedContextViewer = factory.createPinnedContextViewer(this);
        Control control = fPinnedContextViewer.createControl(fComposite);
        control.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
        control.moveAbove(null);
        fComposite.layout(true,  true);
    }

    public void clearPin() {
        if (fPinnedContextViewer != null) {
            fPinnedContextViewer.dispose();
            fPinnedContextViewer = null;
        }
        if (fComposite != null && !fComposite.isDisposed()) {
        	fComposite.layout(true,  true);
        }
    }

    public boolean isPinned() {
        return fPinnedContextViewer != null;
    }

    public String getPinnedFactoryId() {
    	if (fPinnedContextViewer != null) {
    		return fPinnedContextViewer.getFactoryId();
    	}
    	return null;
    }
    
    public final void createPartControl(Composite parent) {
        setPartTitle();
        fComposite = new Composite(parent, SWT.NONE);
        GridLayout layout = new GridLayout();
        layout.marginTop = layout.marginHeight = layout.marginLeft= layout.marginRight = layout.marginBottom = layout.marginWidth = layout.verticalSpacing = 0;   
        fComposite.setLayout(layout);
        
        Composite viewerComposite = SWTFactory.createComposite(fComposite, parent.getFont(), 1, 1, GridData.FILL_BOTH, 0, 0);
        viewerComposite.setLayout(new FillLayout());
        super.createPartControl(viewerComposite);
    }
	
    public void dispose() {
        clearPin();
        super.dispose();
    }

    /**
     * Initialize the pinned context viewer state from the given memento. 
     * @param memento
     */
    public void initPinnedContextViewerState(IMemento memento) {
    	if (memento != null) {
	    	String factoryId = memento.getString(PINNED_CONTEXT_VIEWER_ID); 
	    	if (factoryId != null) {
	    		IPinnedContextFactory factory = 
	    				DebugUITools.getDebugContextManager().getPinnedContextViewerFactory(factoryId);
	    		if (factory != null ) {
	    			pin(factory);
	    			if (fPinnedContextViewer != null) fPinnedContextViewer.restorePinnedContext(memento);
	        	}
	    	}
    	}
    }
    
	/**
	 * Saves the current state of the viewer
	 * @param memento the memento to write the viewer state into
	 */
	public void savePinnedContextViewerState(IMemento memento) {
		if (fPinnedContextViewer != null) {
			memento.putString(PINNED_CONTEXT_VIEWER_ID, fPinnedContextViewer.getFactoryId());
			fPinnedContextViewer.savePinnedContext(memento);
		}
	}
	
    protected void createActions() {
        setAction("NewPinnedView", new NewPinnedViewAction(this)); //$NON-NLS-1$
    }
    
    protected void configureToolBar(IToolBarManager tbm) {
        tbm.add(new Separator(this.getClass().getName()));
        tbm.add(new Separator(IDebugUIConstants.NAVIGATION_GROUP));
        tbm.add(getAction("NewPinnedView")); //$NON-NLS-1$
    }
    
    /**
     * Set the part title to include the secondary id as part of the title.
     * 
     * @param part the view part
     */
    private void setPartTitle() {
        if (!NewViewInstanceAction.isClonedPart(this))
            return;
        
        String secondaryId = getViewSite().getSecondaryId();
        secondaryId = NewViewInstanceAction.decodeClonedPartSecondaryId(secondaryId);           
        
        String name = getPartName();
        String tag = " <" + secondaryId + ">";   //$NON-NLS-1$//$NON-NLS-2$
        if (name.indexOf(tag) < 0) {
            name = name + tag;
            setPartName(name);
        }
    }

}

Back to the top