Skip to main content
summaryrefslogtreecommitdiffstats
blob: ea8a66339d6008a99e96127de7318bd0052917e4 (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
/*******************************************************************************
 * 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.jface.text.Assert;
import org.eclipse.jst.pagedesigner.commands.range.Paragraph;
import org.eclipse.jst.pagedesigner.commands.range.ParagraphApplyStyleCommand;
import org.eclipse.jst.pagedesigner.commands.range.ParagraphFinder;
import org.eclipse.jst.pagedesigner.dom.DOMRange;
import org.eclipse.jst.pagedesigner.dom.EditModelQuery;
import org.eclipse.jst.pagedesigner.dom.IDOMPosition;
import org.eclipse.jst.pagedesigner.viewer.IHTMLGraphicalViewer;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

/**
 * @author mengbo
 */
public class ParagraphStyleAction extends DesignerToolBarAction {
	private String _tagName;

	private Node _applyingNode;

	/**
	 * @param text
	 * @param name
	 * @param image
	 * @param style
	 */
	public ParagraphStyleAction(String text, String name,
			ImageDescriptor image, int style) {
		super(text, style);
		_tagName = name;
		setImageDescriptor(image);
	}

	public ParagraphStyleAction(String text, Node node, ImageDescriptor image,
			int style) {
		super(text, style);
		_applyingNode = node;
		setImageDescriptor(image);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jst.pagedesigner.editors.actions.DesignerToolBarAction#isApplied(org.eclipse.jst.pagedesigner.dom.DOMRange)
	 */
	protected boolean isApplied(DOMRange range) {
		Assert.isTrue(getExpectedTag() != null);
		if (range != null) {
			boolean ordered = range.isOrdered();
			IDOMPosition start = ordered ? range.getStartPosition() : range
					.getEndPosition();
			IDOMPosition end = ordered ? range.getEndPosition() : range
					.getStartPosition();
			Node common = null;
			if (EditModelQuery.isSame(range)) {
				ParagraphFinder finder = new ParagraphFinder(start);
				Paragraph p = finder.getParagraph(start);
				common = p.getLowestContainer();
			} else {
				common = EditModelQuery.getInstance().getCommonAncestor(start,
						end);
			}
			// the lowest common block parent is the container to apply style.
			if (containsTag(common)) {
				return true;
			} else {
				return false;
			}
		} else {
			return false;
		}
	}

	protected boolean containsTag(Node common) {
		// the lowest common block parent is the container to apply style.
		if (_applyingNode == null) {
			return common.getNodeName() != null
					&& getExpectedTag().equalsIgnoreCase(
							common.getNodeName().toLowerCase());
		}
		// return false;
		else {
			String align = ((Element) _applyingNode).getAttribute("align");
			if (!(common instanceof Element)) {
				return false;
			}
			String cAlign = ((Element) common).getAttribute("align");
			if (align == null || cAlign == null) {
				return false;
			}
			if (align.equals(cAlign)) {
				return true;
			} else {
				return false;
			}
		}

	}

	/**
	 * @return Returns the _expectedTag.
	 */
	public String getExpectedTag() {
		if (_tagName == null) {
			return _applyingNode.getNodeName().toLowerCase();
		} else {
			return _tagName.toLowerCase();
		}
	}

	/**
	 * @return Returns the _applyingNode.
	 */
	public Element getApplyingNode() {
		if (_applyingNode != null) {
			return (Element) _applyingNode;
		} else {
			return null;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jst.pagedesigner.commands.range.DesignerToolBarAction#getCommand()
	 */
	protected Command getCommand() {
		ParagraphApplyStyleCommand command = null;
		if (getApplyingNode() != null) {
			command = new ParagraphApplyStyleCommand(getViewer(),
					getApplyingNode(), null, null);
		} else {
			command = new ParagraphApplyStyleCommand(getViewer(),
					getExpectedTag(), null, null);
		}
		return command;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jst.pagedesigner.commands.range.DesignerToolBarAction#setViewer(org.eclipse.jst.pagedesigner.viewer.IHTMLGraphicalViewer)
	 */
	public void setViewer(IHTMLGraphicalViewer viewer) {
		// TODO Auto-generated method stub
		super.setViewer(viewer);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ui.texteditor.IUpdate#update()
	 */
	public void update() {
		// TODO Auto-generated method stub
		super.update();
	}
}

Back to the top