Skip to main content
summaryrefslogtreecommitdiffstats
blob: 71a42143df516a8d0bdf9af8ce439c54961861de (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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*******************************************************************************
 * 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.parts;

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

import org.eclipse.draw2d.IFigure;
import org.eclipse.gef.EditPart;
import org.eclipse.jst.pagedesigner.css2.ICSSStyle;
import org.eclipse.jst.pagedesigner.css2.layout.FlowPage;
import org.eclipse.wst.css.core.internal.event.ICSSStyleListener;
import org.eclipse.wst.css.core.internal.provisional.document.ICSSModel;
import org.eclipse.wst.css.core.internal.provisional.document.ICSSSelector;
import org.eclipse.wst.html.core.internal.htmlcss.StyleListener;
import org.eclipse.wst.sse.core.internal.provisional.INodeNotifier;
import org.eclipse.wst.xml.core.internal.provisional.document.IDOMElement;
import org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * @author mengbo
 */
public class DocumentEditPart extends NodeEditPart implements StyleListener,
		ICSSStyleListener {
	boolean _refreshing = false;

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.gef.editparts.AbstractEditPart#getModelChildren()
	 */
	protected List getModelChildren() {
		List list = new ArrayList();
		Node model = (Node) getModel();
		if (model == null) {
			return list;
		}

		NodeList children1 = model.getChildNodes();
		for (int i = 0, n = children1.getLength(); i < n; i++) {
			Node node = children1.item(i);
			if (node.getNodeType() != Node.TEXT_NODE
					&& node.getNodeType() != Node.ELEMENT_NODE) {
				continue;
			}
			list.add(node);
		}
		return list;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.gef.editparts.AbstractGraphicalEditPart#createFigure()
	 */
	protected IFigure createFigure() {
		FlowPage f = new FlowPage();
		return f;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.wst.sse.core.internal.provisional.INodeAdapter#notifyChanged(org.eclipse.wst.sse.core.internal.provisional.INodeNotifier,
	 *      int, java.lang.Object, java.lang.Object, java.lang.Object, int)
	 */
	public void notifyChanged(INodeNotifier notifier, int eventType,
			Object changedFeature, Object oldValue, Object newValue, int pos) {
		refresh();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.gef.editparts.AbstractGraphicalEditPart#refresh()
	 */
	public void refresh() {
		if (_refreshing) {
			return;
		}
		_refreshing = true;
		try {
			super.refresh();
		} finally {
			_refreshing = false;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.gef.editparts.AbstractEditPart#refreshChildren()
	 */
	protected void refreshChildren() {
		super.refreshChildren();
		List children1 = getChildren();
		for (int i = 0, size = children1.size(); i < size; i++) {
			((EditPart) children1.get(i)).refresh();
		}
	}

	// protected void removeChildVisual(EditPart childEditPart)
	// {
	// if (childEditPart instanceof SubNodeEditPart)
	// {
	// Node node = ((SubNodeEditPart) childEditPart).getNodeForFigure();
	// if (node != null)
	// {
	// getDestDocumentForDesign().removeChild(node);
	// }
	// }
	// super.removeChildVisual(childEditPart);
	// }
	//    
	// protected void addChildVisual(EditPart childEditPart, int index)
	// {
	// if (childEditPart instanceof SubNodeEditPart)
	// {
	// Node node = ((SubNodeEditPart) childEditPart).getNodeForFigure();
	// if (node != null)
	// {
	// NodeList nodeList = getDestDocumentForDesign().getChildNodes();
	// if (nodeList.getLength() > index)
	// {
	// getDestDocumentForDesign().insertBefore(node, nodeList.item(index));
	// }
	// else
	// {
	// getDestDocumentForDesign().appendChild(node);
	// }
	// }
	// }
	// super.addChildVisual(childEditPart, index);
	// }

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.wst.html.core.internal.htmlcss.StyleListener#styleChanged()
	 */
	public void styleChanged() {
		// refresh the whole document when style change (<style> or <link>)
		this.refreshStyle();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.wst.css.core.internal.event.ICSSStyleListener#styleChanged(org.eclipse.wst.css.core.internal.provisional.document.ICSSModel,
	 *      org.eclipse.wst.css.core.document.ICSSSelector[],
	 *      org.eclipse.wst.css.core.document.ICSSSelector[], java.lang.String)
	 */
	public void styleChanged(ICSSModel srcModel, ICSSSelector[] removed,
			ICSSSelector[] added, String media) {
		if ((removed != null && removed.length > 0) || added != null
				&& added.length > 0) {
			this.refreshStyle();
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.wst.css.core.internal.event.ICSSStyleListener#styleUpdate(org.eclipse.wst.css.core.internal.provisional.document.ICSSModel)
	 */
	public void styleUpdate(ICSSModel srcModel) {
		this.refreshStyle();
	}

	/**
	 * 
	 */
	private void refreshStyle() {
		List childParts = this.getChildren();
		for (Iterator iter = childParts.iterator(); iter.hasNext();) {
			EditPart part = (EditPart) iter.next();
			if (part instanceof ElementEditPart) {
				IDOMNode node = (IDOMNode) ((ElementEditPart) part)
						.getNodeForFigure();
				if (node != null) {
					refreshChildStyles(node);
				}
			}
		}
		getFigure().revalidate();
		// getFigure().repaint();
	}

	/**
	 * @param node
	 */
	private void refreshChildStyles(IDOMNode node) {
		NodeList childNodes = node.getChildNodes();
		for (int i = 0, size = childNodes.getLength(); i < size; i++) {
			refreshChildStyles((IDOMNode) childNodes.item(i));
		}
		if (node instanceof IDOMElement) {
			// only refresh style on element.
			ICSSStyle a = (ICSSStyle) node.getAdapterFor(ICSSStyle.class);
			if (a != null) {
				a.reset();
			}
		}
	}
}

Back to the top