Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 44a0373dfc06d31b55ed38da602ded68ab7e2c9b (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
/*****************************************************************************
 * Copyright (c) 2004 IBM Corporation 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: IBM Corporation - initial API and implementation
 ****************************************************************************/
package org.eclipse.wst.xml.ui.internal.tabletree;

import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.ICellModifier;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Item;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
import org.eclipse.ui.views.properties.IPropertyDescriptor;
import org.eclipse.wst.common.contentmodel.CMElementDeclaration;
import org.eclipse.wst.common.contentmodel.modelquery.ModelQuery;
import org.eclipse.wst.common.contentmodel.util.CMDescriptionBuilder;
import org.eclipse.wst.xml.internal.ui.XMLEditorResourceHandler;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

public class XMLTreeExtension extends TreeExtension {

	public final static String STRUCTURE_PROPERTY = XMLEditorResourceHandler.getResourceString("%XMLTreeExtension.0"); //$NON-NLS-1$
	public final static String VALUE_PROPERTY = XMLEditorResourceHandler.getResourceString("%XMLTreeExtension.1"); //$NON-NLS-1$

	protected Composite control;
	protected MyCellModifier modifier;
	protected XMLTableTreePropertyDescriptorFactory propertyDescriptorFactory;
	protected CMDescriptionBuilder decriptionBuilder = new CMDescriptionBuilder();
	protected TreeContentHelper treeContentHelper = new TreeContentHelper();

	protected Color f1, f2, b1, b2;
	protected boolean cachedDataIsValid = true;
	private ModelQuery fModelQuery;

	public XMLTreeExtension(Tree tree) {
		super(tree);
		control = tree;
		modifier = new MyCellModifier();
		setCellModifier(modifier);
		String[] properties = {STRUCTURE_PROPERTY, VALUE_PROPERTY};
		setColumnProperties(properties);

		f1 = tree.getDisplay().getSystemColor(SWT.COLOR_BLACK);
		Color background = tree.getDisplay().getSystemColor(SWT.COLOR_LIST_BACKGROUND);

		int r = Math.abs(background.getRed() - 125);
		int g = Math.abs(background.getGreen() - 85);
		int b = Math.abs(background.getBlue() - 105);

		f2 = new Color(tree.getDisplay(), r, g, b);
		b1 = tree.getDisplay().getSystemColor(SWT.COLOR_LIST_SELECTION);
		b2 = background;
	}

	public void dispose() {
		super.dispose();
		f2.dispose();
	}

	public void resetCachedData() {
		cachedDataIsValid = false;
	}

	public void paintItems(GC gc, TreeItem[] items, Rectangle treeBounds) {
		super.paintItems(gc, items, treeBounds);
		cachedDataIsValid = true;
	}

	protected Object[] computeTreeExtensionData(Object object) {
		Color color = f1;
		String string = ""; //$NON-NLS-1$
		if (string.length() == 0) {
			string = (String) modifier.getValue(object, VALUE_PROPERTY);
			color = f1;
		}
		if (string.length() == 0 && object instanceof Element) {
			string = getElementValueHelper((Element) object);
			color = f2;
		}
		Object[] data = new Object[2];
		data[0] = string;
		data[1] = color;
		return data;
	}

	protected void paintItem(GC gc, TreeItem item, Rectangle bounds) {
		super.paintItem(gc, item, bounds);
		Object[] data = computeTreeExtensionData(item.getData());
		if (data != null && data.length == 2) {
			gc.setClipping(columnPosition, bounds.y + 1, controlWidth, bounds.height);
			gc.setForeground((Color) data[1]);
			gc.drawString((String) data[0], columnPosition + 5, bounds.y + 1);
			gc.setClipping((Rectangle) null);
		}
	}

	protected void addEmptyTreeMessage(GC gc) {
		// here we print a message when the document is empty just to give the
		// user a visual cue
		// so that they know how to proceed to edit the blank view
		gc.setForeground(tree.getDisplay().getSystemColor(SWT.COLOR_BLACK));
		gc.setBackground(tree.getDisplay().getSystemColor(SWT.COLOR_LIST_BACKGROUND));
		gc.drawString(XMLEditorResourceHandler.getResourceString("%XMLTreeExtension.3"), 10, 10); //$NON-NLS-1$
		gc.drawString(XMLEditorResourceHandler.getResourceString("%XMLTreeExtension.4"), 10, 10 + gc.getFontMetrics().getHeight()); //$NON-NLS-1$
	}

	public String getElementValueHelper(Element element) {
		String result = null;

		if (result == null && getModelQuery() != null) {
			CMElementDeclaration ed = getModelQuery().getCMElementDeclaration(element);
			if (ed != null && !Boolean.TRUE.equals(ed.getProperty("isInferred"))) { //$NON-NLS-1$
				result = decriptionBuilder.buildDescription(ed);
			}
		}
		return result != null ? result : ""; //$NON-NLS-1$
	}

	/**
	 *  
	 */
	public class MyCellModifier implements ICellModifier, TreeExtension.ICellEditorProvider {
		public boolean canModify(Object element, String property) {
			boolean result = false;
			if (element instanceof Node) {
				Node node = (Node) element;
				result = property == VALUE_PROPERTY && treeContentHelper.isEditable(node);
			}
			return result;
		}

		public Object getValue(Object object, String property) {
			String result = null;
			if (object instanceof Node) {
				result = treeContentHelper.getNodeValue((Node) object);
			}
			return (result != null) ? result : ""; //$NON-NLS-1$
		}

		public void modify(Object element, String property, Object value) {
			//enableNodeSelectionListener(false);
			Item item = (Item) element;
			String oldValue = treeContentHelper.getNodeValue((Node) item.getData());
			String newValue = value.toString();
			if (newValue != null && !newValue.equals(oldValue)) {
				treeContentHelper.setNodeValue((Node) item.getData(), value.toString());
			}
			//enableNodeSelectionListener(true);
		}

		public CellEditor getCellEditor(Object o, int col) {
			IPropertyDescriptor pd = propertyDescriptorFactory.createPropertyDescriptor(o);
			return pd != null ? pd.createPropertyEditor(control) : null;
		}
	}


	public ModelQuery getModelQuery() {
		return fModelQuery;
	}

	/**
	 * @param query
	 */
	public void setModelQuery(ModelQuery query) {
		fModelQuery = query;
		propertyDescriptorFactory = new XMLTableTreePropertyDescriptorFactory(query);
	}

}

Back to the top