Skip to main content
summaryrefslogtreecommitdiffstats
blob: 868c54ac99a49bb30af7ffbddae3dd2f8bf7f2c8 (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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*******************************************************************************
 * 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.actions.range;

import org.eclipse.gef.commands.Command;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jst.pagedesigner.commands.range.ApplyStyleCommand;
import org.eclipse.jst.pagedesigner.dom.DOMPositionHelper;
import org.eclipse.jst.pagedesigner.dom.DOMRange;
import org.eclipse.jst.pagedesigner.dom.DOMRangeHelper;
import org.eclipse.jst.pagedesigner.dom.EditModelQuery;
import org.eclipse.jst.pagedesigner.dom.IDOMPosition;
import org.eclipse.jst.pagedesigner.viewer.DesignPosition;
import org.eclipse.jst.pagedesigner.viewer.DesignRange;
import org.eclipse.jst.pagedesigner.viewer.IHTMLGraphicalViewer;
import org.w3c.dom.Node;

/**
 * @author mengbo
 */
public class ChangeStyleAction extends DesignerToolBarAction {
	private String _expectedTag;

	/**
	 * @param text
	 * @param image
	 */
	public ChangeStyleAction(String text, String name, ImageDescriptor image,
			int style) {
		super(text, style);
		_expectedTag = name;
		this.setImageDescriptor(image);
	}

	public ChangeStyleAction(String text, String name, ImageDescriptor enabled,
			ImageDescriptor disabled, int style) {
		super(text, style);
		_expectedTag = name;
		setImageDescriptor(enabled);
		setDisabledImageDescriptor(disabled);
	}

	protected String getExpectedCSSProperty() {
		return null;
	}

	protected String getExpectedCSSPropertyValue() {
		return null;
	}

	protected boolean isApplied(DOMRange range) {
		if (range == null) {
			return false;
		}

		boolean ordered = range.isOrdered();
		IDOMPosition start = ordered ? range.getStartPosition() : range
				.getEndPosition();
		IDOMPosition end = ordered ? range.getEndPosition() : range
				.getStartPosition();
		Node startnode = start.getContainerNode();
		Node endnode = end.getContainerNode();
		if (!EditModelQuery.hasAncestor(startnode, _expectedTag, true)) {
			return false;
		}
		for (Node node = startnode; node != endnode; node = EditModelQuery
				.getInstance().getNextLeafNeighbor(node)) {
			if (!EditModelQuery.hasAncestor(node, _expectedTag, true)) {
				return false;
			}
		}
		if (!EditModelQuery.hasAncestor(endnode, _expectedTag, true)) {
			return false;
		}
		return true;
	}

	/**
	 * @return
	 */
	protected String getExpectedTag() {
		return _expectedTag;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jst.pagedesigner.commands.range.DesignerToolBarAction#getCommand()
	 */
	protected Command getCommand() {
		DesignRange range = getViewer().getRangeSelection();
		DOMRange dRange = DOMRangeHelper.toDOMRange(range);
		Command command;
		if (isApplied(dRange)) {
			// command = new UnapplyStyleCommand(getViewer(), _expectedTag,
			// null, null);
			// since the un-applystyle is not implemented yet,we do nothing
			// here.
			command = null;
			this.setChecked(true);
		} else {
			command = new ApplyStyleCommand(getViewer(), _expectedTag, null,
					null);
		}
		return command;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ui.texteditor.IUpdate#update()
	 */
	public void update() {
		if (canRun(getViewer())) {
			setEnabled(true);
		} else {
			setEnabled(false);
		}
		updateStatus();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jst.pagedesigner.actions.range.DesignerToolBarAction#updateStatus()
	 */
	public void updateStatus() {
		IHTMLGraphicalViewer viewer = getViewer();
		if (viewer != null && viewer.isInRangeMode()
				&& viewer.getModel().getDocument().hasChildNodes()) {
			DesignRange range = getViewer().getRangeSelection();
			if (range != null && range.isValid()) {
				DOMRange domRange = null;
				domRange = new DOMRange(DOMPositionHelper.toDOMPosition(range
						.getStartPosition()), DOMPositionHelper
						.toDOMPosition(range.getEndPosition()));
				if (isApplied(domRange)) {
					this.setChecked(true);
				} else {
					this.setChecked(false);
				}
				return;
			}
		}
		this.setChecked(false);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jst.pagedesigner.actions.range.DesignerToolBarAction#canRun(org.eclipse.jst.pagedesigner.viewer.IHTMLGraphicalViewer)
	 */
	protected boolean canRun(IHTMLGraphicalViewer viewer) {
		if (viewer != null && viewer.isInRangeMode()
				&& viewer.getModel().getDocument().hasChildNodes()) {
			DesignRange range = viewer.getRangeSelection();
			if (range != null && range.isValid()) {
				DesignPosition startPos = range.getStartPosition();
				DesignPosition endPos = range.getEndPosition();
				if (startPos != endPos) {
					return true;
				}
			}
		}
		return false;
	}
}

Back to the top