Skip to main content
summaryrefslogtreecommitdiffstats
blob: cca5ca56614dca62ca00b0a5e35a04d53765c167 (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
/*******************************************************************************
 * Copyright (c) 2006 Sybase, 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:
 *     Sybase, Inc. - initial API and implementation
 *******************************************************************************/
package org.eclipse.jst.pagedesigner.properties;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IMenuCreator;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.w3c.dom.Document;
import org.w3c.dom.DocumentFragment;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.Text;

/**
 * @author mengbo
 * @version 1.5
 */
public class NavigationHiearchyAction extends Action {
	private Menu _hiearchyMenu;

	private Node _startNode;

	private Node _currentNode;

	private DesignerTabbedPropertySheetPage _propertyPage;

	private class MenuCreator implements IMenuCreator {
		public void dispose() {
			if (_hiearchyMenu != null) {
				for (int i = 0, n = _hiearchyMenu.getItemCount(); i < n; i++) {
					MenuItem menuItem = _hiearchyMenu.getItem(i);
					menuItem.setData(null);
				}
				_hiearchyMenu.dispose();
				_hiearchyMenu = null;
			}
		}

		public Menu getMenu(Menu parent) {
			return null;
		}

		public Menu getMenu(Control parent) {
			dispose();
			_hiearchyMenu = new Menu(parent);

			// next we need to add the list of parents node into the menu.
			Node node = _startNode;
			List list = new ArrayList();
			while (node != null && !(node instanceof Document)
					&& !(node instanceof DocumentFragment)) {
				list.add(node);
				node = node.getParentNode();
			}

			// adding ancesters reverse order.
			for (int i = list.size() - 1; i >= 0; i--) {
				Node thenode = (Node) list.get(i);
				MenuItem item = new MenuItem(_hiearchyMenu, SWT.CHECK);
				item.setSelection(thenode == _currentNode ? true : false);
				String text = thenode.getNodeName();
				item.setText(text);
				item.setData(thenode);
				item.addSelectionListener(new SelectionAdapter() {
					public void widgetSelected(SelectionEvent e) {
						Node selectedNode = (Node) e.widget.getData();
						_propertyPage.internalChangeSelection(selectedNode,
								_startNode);
					}
				});
			}

			return _hiearchyMenu;
		}
	}

	public NavigationHiearchyAction(DesignerTabbedPropertySheetPage propertyPage) {
		super("");
		setEnabled(true);
		setMenuCreator(new MenuCreator());
		this._propertyPage = propertyPage;
	}

	protected void changeSelection(Node selectedNode, Node startNode) {
		this._propertyPage.internalChangeSelection(selectedNode, startNode);
		this._currentNode = selectedNode;
		this._startNode = startNode;
		this.setText(this._currentNode.getNodeName());
	}

	protected void refresh(Node currentNode, Node startNode) {
		this._currentNode = currentNode;
		this._startNode = startNode;
		if (!(_currentNode instanceof Text)
				&& !(_currentNode instanceof Element)) {
			this.setText("---");
			this.setEnabled(false);
		} else {
			this.setText(_currentNode.getNodeName());
			this.setEnabled(true);
		}
	}

	/*
	 * (non-Javadoc) Method declared on IAction.
	 */
	public void run() {
		this._propertyPage.internalChangeSelection(_currentNode, _startNode);
	}
}

Back to the top