/
XMLDesignPage.java
166 lines (145 loc) · 4.76 KB
/
XMLDesignPage.java
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
/*******************************************************************************
* Copyright (c) 2017 Red Hat, Inc and others.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Red Hat, Inc - initial API and implementation
*******************************************************************************/
package org.eclipse.reddeer.eclipse.wst.xml.ui.tabletree;
import org.eclipse.swt.widgets.TreeItem;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;
import org.eclipse.reddeer.common.matcher.AndMatcher;
import org.eclipse.reddeer.common.util.Display;
import org.eclipse.reddeer.common.util.ResultRunnable;
import org.eclipse.reddeer.core.handler.TreeItemHandler;
import org.eclipse.reddeer.core.lookup.TreeItemLookup;
import org.eclipse.reddeer.core.matcher.TreeItemTextMatcher;
import org.eclipse.reddeer.swt.impl.tree.DefaultTree;
/**
* Represents the Design page of {@link XMLMultiPageEditorPart}. The page
* allows to work with XML in tree format.
* @author Lucia Jelinkova
*
*/
public class XMLDesignPage {
/**
* Return node specified by the path (from the top of the tree).
*
* @param path the path
* @return the node
*/
public XMLDesignPageNode getNode(String... path){
TreeItem item = TreeItemLookup.getInstance().getTreeItem(new DefaultTree().getSWTWidget(), 0, createNodeMatchers(path));
return new XMLDesignPageNode(item);
}
/**
* Return node specified by the path (from the specified node).
*
* @param node the node
* @param path the path
* @return the node
*/
public XMLDesignPageNode getNode(XMLDesignPageNode node, String... path){
TreeItem item = TreeItemLookup.getInstance().getTreeItem(node.item, 0, createNodeMatchers(path));
return new XMLDesignPageNode(item);
}
/**
* Return the value of the specified attribute of the specified node.
*
* @param node the node
* @param name the name
* @return the attribute value
*/
@SuppressWarnings("unchecked")
public String getAttributeValue(XMLDesignPageNode node, String name){
TreeItem item = TreeItemLookup.getInstance().getTreeItem(node.item, 0, createAttributeMatcher(name));
return TreeItemHandler.getInstance().getText(item, 1);
}
/**
* Return text content of the specified node.
*
* @param node the node
* @return the content
*/
public String getContent(XMLDesignPageNode node){
return node.getTreeItemText(1);
}
/**
* Represents one XML node on design page
*
* @author Lucia Jelinkova
*
*/
public static class XMLDesignPageNode {
private TreeItem item;
/**
* Instantiates a new XML design page node.
*
* @param item the item
*/
public XMLDesignPageNode(TreeItem item) {
this.item = item;
}
/**
* Gets the name.
*
* @return the name
*/
public String getName(){
return getTreeItemText(0);
}
private String getTreeItemText(int cellIndex){
return TreeItemHandler.getInstance().getText(item, cellIndex);
}
}
@SuppressWarnings("unchecked")
private Matcher<org.eclipse.swt.widgets.TreeItem>[] createNodeMatchers(String[] treeItemPath) {
Matcher<org.eclipse.swt.widgets.TreeItem>[] matchers = new Matcher[treeItemPath.length];
for (int i = 0; i < treeItemPath.length; i++){
matchers[i] = new AndMatcher(new NodeMatcher(), new TreeItemTextMatcher(treeItemPath[i]));
}
return matchers;
}
@SuppressWarnings("unchecked")
private Matcher<TreeItem> createAttributeMatcher(String name) {
return new AndMatcher(new AttributeMatcher(), new TreeItemTextMatcher(name));
}
private static class NodeMatcher extends TypeSafeMatcher<org.eclipse.swt.widgets.TreeItem> {
@Override
public void describeTo(Description description) {
description.appendText("represents XML node");
}
@Override
protected boolean matchesSafely(final org.eclipse.swt.widgets.TreeItem item) {
String dataClassName = Display.syncExec(new ResultRunnable<String>() {
@Override
public String run() {
return item.getData().getClass().getSimpleName();
}
});
return dataClassName.equals("ElementImpl");
}
}
private static class AttributeMatcher extends TypeSafeMatcher<org.eclipse.swt.widgets.TreeItem> {
@Override
public void describeTo(Description description) {
description.appendText("represents XML attribute");
}
@Override
protected boolean matchesSafely(final org.eclipse.swt.widgets.TreeItem item) {
String dataClassName = Display.syncExec(new ResultRunnable<String>() {
@Override
public String run() {
return item.getData().getClass().getSimpleName();
}
});
return dataClassName.equals("AttrImpl");
}
}
}