Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 7e2f9b2fdcc7532d69e957f66dca035f9550b2f2 (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
/*******************************************************************************
 * Copyright (c) 2011, 2012 Wind River Systems, Inc. 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.tcf.te.ui.trees;

import org.eclipse.core.runtime.Assert;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Tree;

/**
 * The header context menu for the execution context viewer. This context menu
 * provides the list of columns that users can configure them by simply checking
 * or unchecking them.
 */
public class TreeViewerHeaderMenu extends Menu implements SelectionListener, Listener, DisposeListener {
	//The menu control used to configure the columns.
	private Menu treeMenu;
	//The tree to be configured.
	private AbstractTreeControl treeControl;


	/**
	 * Create a header menu for the execution context viewer.
	 *
	 * @param tree The execution context tree.
	 */
	public TreeViewerHeaderMenu(AbstractTreeControl treeControl) {
		super(treeControl.getViewer().getControl());
		this.treeControl = treeControl;
		Tree tree = (Tree) treeControl.getViewer().getControl();
		tree.addListener(SWT.MenuDetect, this);
		tree.addDisposeListener(this);
		treeMenu = tree.getMenu();
	}

	/**
	 * Create the menu.
	 */
	public void create() {
		ColumnDescriptor[] columns = treeControl.getViewerColumns();
		Assert.isNotNull(columns);
		for (int i = 0; i < columns.length; i++) {
			ColumnDescriptor column = columns[i];
			MenuItem menuItem = new MenuItem(this, SWT.CHECK);
			menuItem.setText(column.getName());
			menuItem.setSelection(column.isVisible());
			menuItem.addSelectionListener(this);
			menuItem.setData(column);
			menuItem.setEnabled(i != 0);
		}
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.swt.events.SelectionListener#widgetSelected(org.eclipse.swt.events.SelectionEvent)
	 */
	@Override
    public void widgetSelected(SelectionEvent e) {
		MenuItem item = (MenuItem) e.getSource();
		ColumnDescriptor column = (ColumnDescriptor) item.getData();
		boolean visible = item.getSelection();
		if (treeControl.setColumnVisible(column, visible)){
			treeControl.columnMoved();
			treeControl.getViewer().refresh();
		}
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.swt.events.SelectionListener#widgetDefaultSelected(org.eclipse.swt.events.SelectionEvent)
	 */
	@Override
    public void widgetDefaultSelected(SelectionEvent e) {
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.swt.widgets.Listener#handleEvent(org.eclipse.swt.widgets.Event)
	 */
	@Override
    public void handleEvent(Event event) {
		Tree t = (Tree) event.widget;
		Point pt = t.getDisplay().map(null, t, event.x, event.y);
		Rectangle clientArea = t.getClientArea();
		boolean isHeader = ((pt.y - clientArea.y) <= t.getHeaderHeight());
		t.setMenu(isHeader ? this : treeMenu);
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.swt.events.DisposeListener#widgetDisposed(org.eclipse.swt.events.DisposeEvent)
	 */
	@Override
    public void widgetDisposed(DisposeEvent e) {
		if (treeMenu != null && !treeMenu.isDisposed())
			treeMenu.dispose();
		if (!isDisposed())
			dispose();
	}

	/**
	 * Override the super method to allow the subclassing.
	 */
	@Override
	protected void checkSubclass() {
	}

	/**
	 * Update the menu item's check state according to the new column's visibility.
	 */
	public void updateSelection() {
		ColumnDescriptor[] columns = treeControl.getViewerColumns();
		Assert.isNotNull(columns);
		for (int i = 0; i < columns.length; i++) {
			MenuItem item = this.getItem(i);
			ColumnDescriptor column = columns[i];
			item.setSelection(column.isVisible());
		}
    }
}

Back to the top