Skip to content

Commit 8d2e81d

Browse files
committed
Ensure that only valid attributes are suggested.
1 parent 9b84c0d commit 8d2e81d

File tree

1 file changed

+25
-11
lines changed

1 file changed

+25
-11
lines changed

sierra-previewer/src/main/java/org/httprpc/sierra/previewer/SierraXMLCompletionProvider.java

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
*/
1414
package org.httprpc.sierra.previewer;
1515

16+
import java.awt.Color;
17+
import java.awt.Font;
18+
import java.awt.Image;
1619
import java.util.ArrayList;
1720
import java.util.Collections;
1821
import java.util.Comparator;
@@ -24,6 +27,7 @@
2427
import java.util.regex.Matcher;
2528
import java.util.regex.Pattern;
2629

30+
import javax.swing.Icon;
2731
import javax.swing.JComponent;
2832
import javax.swing.text.BadLocationException;
2933
import javax.swing.text.JTextComponent;
@@ -35,7 +39,7 @@
3539
import org.httprpc.sierra.UILoader;
3640

3741
/**
38-
* Provides context-aware autocompletion for Sierra DSL XML by
42+
* Provides context-aware autocompletion for Sierra DSL XML by
3943
* asking UILoader to obtain valid elements and attributes for the
4044
* current tag. Includes attribute value definitions in the description.
4145
*/
@@ -47,7 +51,7 @@ public class SierraXMLCompletionProvider extends DefaultCompletionProvider {
4751
// Map to store the final resolved attributes for each tag
4852
// TagName -> {AttributeName -> Description/ValueDefinitionString}
4953
private final Map<String, Map<String, String>> elementAttributeDefinitions = new HashMap<>();
50-
54+
5155
private final Map<String, Map<String, String>> baseClassAttributeDefinitions = new HashMap<>();
5256

5357
private final List<Completion> tagCompletions = new ArrayList<>();
@@ -65,15 +69,15 @@ private void loadTags() {
6569
Map<String, String> attributes = getAttributesForClass(type);
6670
elementAttributeDefinitions.put(tagName, attributes);
6771
}
68-
72+
6973
tagCompletions.sort(Comparator.comparing(Completion::getInputText));
70-
71-
74+
75+
7276
baseClassAttributeDefinitions.put(JComponent.class.getName(), getAttributesForClass(JComponent.class));
7377
addCommonAttributes();
7478

7579
}
76-
80+
7781
private void addCommonAttributes() {
7882
Map<String, String> attributes = new HashMap<>();
7983
// Add common attributes applicable to all components
@@ -88,19 +92,29 @@ private void addCommonAttributes() {
8892
attributes.put("styleClass", "String");
8993
baseClassAttributeDefinitions.put("CommonAttributes", attributes);
9094
}
91-
95+
9296
private Map<String, String> getAttributesForClass(Class<?> type){
9397
Map<String, String> attributes = new HashMap<>();
9498
for (var entry : BeanAdapter.getProperties(type).entrySet()) {
9599
var property = entry.getValue();
96100
var mutator = property.getMutator();
97101

98-
if (mutator == null || mutator.getDeclaringClass() != type) {
102+
if (mutator == null) {
99103
continue;
100104
}
101-
var attributeName = entry.getKey();
105+
102106
var propertyType = mutator.getParameterTypes()[0];
103-
attributes.put(attributeName, propertyType.getSimpleName());
107+
108+
if (propertyType.isPrimitive()
109+
|| Number.class.isAssignableFrom(propertyType)
110+
|| Enum.class.isAssignableFrom(propertyType)
111+
|| propertyType == String.class
112+
|| propertyType == Color.class
113+
|| propertyType == Font.class
114+
|| propertyType == Icon.class
115+
|| propertyType == Image.class) {
116+
attributes.put(entry.getKey(), propertyType.getSimpleName());
117+
}
104118
}
105119
return attributes;
106120
}
@@ -120,7 +134,7 @@ public List<Completion> getCompletions(JTextComponent comp) {
120134
if (allAttributes == null) {
121135
return new ArrayList<>();
122136
}
123-
137+
124138
// parent class
125139
// @todo we need to walk all the class hierarchy here?
126140
allAttributes.putAll(baseClassAttributeDefinitions.get(JComponent.class.getName()));

0 commit comments

Comments
 (0)