Skip to content

Commit 08901a5

Browse files
committed
(fix) GenericPropertyDescriptor cannot be cast to NumericPropertyDescriptor
* Also remove some deprecated usages. * Add support for regex property type. * Remove unsupported property types. Note: contraints are currently ignored when creating the editors. Fixes #108
1 parent d28a034 commit 08901a5

23 files changed

+226
-145
lines changed

ReleaseNotes.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ This is a minor release.
1717

1818
### Fixed Issues
1919

20+
* [#108](https://github.com/pmd/pmd-eclipse-plugin/issues/108): GenericPropertyDescriptor cannot be cast to NumericPropertyDescriptor
21+
2022
### API Changes
2123

2224
* The minimum eclipse version is now definitely Kepler (4.3) as
@@ -27,7 +29,14 @@ This is a minor release.
2729
* `getLogLevel()`
2830
* `setLogLevel(Level)`
2931
* `LOG_LEVEL`
30-
* `net.sourceforge.pmd.eclipse.plugin.PMDPlugin#ROOT_LOG_ID`
32+
* `net.sourceforge.pmd.eclipse.plugin.PMDPlugin#ROOT_LOG_ID`
33+
* The following property editors are deprecated and not used anymore:
34+
* `net.sourceforge.pmd.eclipse.ui.preferences.editors.FileEditorFactory`
35+
* `net.sourceforge.pmd.eclipse.ui.preferences.editors.FloatEditorFactory`
36+
* `net.sourceforge.pmd.eclipse.ui.preferences.editors.MethodEditorFactory`
37+
* `net.sourceforge.pmd.eclipse.ui.preferences.editors.MultiMethodEditorFactory`
38+
* `net.sourceforge.pmd.eclipse.ui.preferences.editors.MultiTypeEditorFactory`
39+
* `net.sourceforge.pmd.eclipse.ui.preferences.editors.TypeEditorFactory`
3140

3241
### External Contributions
3342

net.sourceforge.pmd.eclipse.plugin/src/main/java/net/sourceforge/pmd/eclipse/ui/preferences/editors/AbstractMultiValueEditorFactory.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import java.util.Collections;
99
import java.util.List;
1010

11+
import org.apache.commons.lang3.StringUtils;
1112
import org.eclipse.swt.SWT;
1213
import org.eclipse.swt.layout.GridData;
1314
import org.eclipse.swt.layout.GridLayout;
@@ -24,7 +25,6 @@
2425
import net.sourceforge.pmd.eclipse.util.Util;
2526
import net.sourceforge.pmd.properties.PropertyDescriptor;
2627
import net.sourceforge.pmd.properties.PropertySource;
27-
import net.sourceforge.pmd.util.StringUtil;
2828

2929
/**
3030
* As a stateless factory it is responsible for building editors that manipulating value collections
@@ -267,9 +267,8 @@ public void handleEvent(Event event) {
267267

268268

269269
protected void fillWidget(Text textWidget, PropertyDescriptor<List<T>> desc, PropertySource source) {
270-
271270
List<T> values = valueFor(source, desc);
272-
textWidget.setText(values == null ? "" : StringUtil.asString(values.toArray(), DELIMITER + ' '));
271+
textWidget.setText(values == null ? "" : StringUtils.join(values, DELIMITER + ' '));
273272
adjustRendering(source, desc, textWidget);
274273
}
275274

@@ -278,7 +277,7 @@ protected List<String> textWidgetValues(Text textWidget) {
278277

279278
String values = textWidget.getText().trim();
280279

281-
if (StringUtil.isEmpty(values)) {
280+
if (StringUtils.isBlank(values)) {
282281
return Collections.emptyList();
283282
}
284283

net.sourceforge.pmd.eclipse.plugin/src/main/java/net/sourceforge/pmd/eclipse/ui/preferences/editors/AbstractRealNumberEditor.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import org.eclipse.swt.widgets.Composite;
88
import org.eclipse.swt.widgets.Spinner;
99

10-
import net.sourceforge.pmd.properties.NumericPropertyDescriptor;
10+
import net.sourceforge.pmd.properties.PropertyDescriptor;
1111
import net.sourceforge.pmd.properties.PropertySource;
1212

1313
/**
@@ -25,11 +25,14 @@ protected AbstractRealNumberEditor() {
2525

2626

2727
protected final Spinner newSpinnerFor(Composite parent, PropertySource source,
28-
NumericPropertyDescriptor<T> numDesc) {
28+
PropertyDescriptor<T> numDesc) {
2929

3030
Spinner spinner = newSpinnerFor(parent, DIGITS);
31-
int min = (int) (numDesc.lowerLimit().doubleValue() * SCALE);
32-
int max = (int) (numDesc.upperLimit().doubleValue() * SCALE);
31+
//TODO: currently it is not possible to determine the numeric constraints values
32+
//int min = (int) (numDesc.lowerLimit().doubleValue() * SCALE);
33+
//int max = (int) (numDesc.upperLimit().doubleValue() * SCALE);
34+
int min = (int) (DEFAULT_MINIMUM * SCALE);
35+
int max = (int) (DEFAULT_MAXIMUM * SCALE);
3336
spinner.setMinimum(min);
3437
spinner.setMaximum(max);
3538

net.sourceforge.pmd.eclipse.plugin/src/main/java/net/sourceforge/pmd/eclipse/ui/preferences/editors/BooleanEditorFactory.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313

1414
import net.sourceforge.pmd.eclipse.ui.preferences.br.SizeChangeListener;
1515
import net.sourceforge.pmd.eclipse.ui.preferences.br.ValueChangeListener;
16-
import net.sourceforge.pmd.properties.BooleanProperty;
1716
import net.sourceforge.pmd.properties.PropertyDescriptor;
17+
import net.sourceforge.pmd.properties.PropertyFactory;
1818
import net.sourceforge.pmd.properties.PropertySource;
1919

2020
/**
@@ -29,13 +29,9 @@ private BooleanEditorFactory() { }
2929

3030

3131
public PropertyDescriptor<Boolean> createDescriptor(String name, String description, Control[] otherData) {
32-
33-
return new BooleanProperty(
34-
name,
35-
description,
36-
otherData == null ? false : valueFrom(otherData[1]),
37-
0
38-
);
32+
return PropertyFactory.booleanProperty(name).desc(description)
33+
.defaultValue(otherData == null ? false : valueFrom(otherData[1]))
34+
.build();
3935
}
4036

4137

net.sourceforge.pmd.eclipse.plugin/src/main/java/net/sourceforge/pmd/eclipse/ui/preferences/editors/CharacterEditorFactory.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
package net.sourceforge.pmd.eclipse.ui.preferences.editors;
66

7+
import org.apache.commons.lang3.StringUtils;
78
import org.eclipse.swt.SWT;
89
import org.eclipse.swt.widgets.Composite;
910
import org.eclipse.swt.widgets.Control;
@@ -13,10 +14,9 @@
1314

1415
import net.sourceforge.pmd.eclipse.ui.preferences.br.SizeChangeListener;
1516
import net.sourceforge.pmd.eclipse.ui.preferences.br.ValueChangeListener;
16-
import net.sourceforge.pmd.properties.CharacterProperty;
1717
import net.sourceforge.pmd.properties.PropertyDescriptor;
18+
import net.sourceforge.pmd.properties.PropertyFactory;
1819
import net.sourceforge.pmd.properties.PropertySource;
19-
import net.sourceforge.pmd.util.StringUtil;
2020

2121
/**
2222
* @author Brian Remedios
@@ -30,20 +30,16 @@ private CharacterEditorFactory() { }
3030

3131

3232
public PropertyDescriptor<Character> createDescriptor(String name, String description, Control[] otherData) {
33-
34-
return new CharacterProperty(
35-
name,
36-
description,
37-
'a',
38-
0);
33+
return PropertyFactory.charProperty(name).desc(description)
34+
.defaultValue('a').build();
3935
}
4036

4137

4238
protected Character valueFrom(Control valueControl) {
4339

4440
String value = ((Text) valueControl).getText().trim();
4541

46-
return (StringUtil.isEmpty(value) || value.length() > 1) ? null
42+
return (StringUtils.isBlank(value) || value.length() > 1) ? null
4743
: value.charAt(0);
4844
}
4945

net.sourceforge.pmd.eclipse.plugin/src/main/java/net/sourceforge/pmd/eclipse/ui/preferences/editors/DoubleEditorFactory.java

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212

1313
import net.sourceforge.pmd.eclipse.ui.preferences.br.SizeChangeListener;
1414
import net.sourceforge.pmd.eclipse.ui.preferences.br.ValueChangeListener;
15-
import net.sourceforge.pmd.properties.DoubleProperty;
16-
import net.sourceforge.pmd.properties.NumericPropertyDescriptor;
1715
import net.sourceforge.pmd.properties.PropertyDescriptor;
16+
import net.sourceforge.pmd.properties.PropertyFactory;
1817
import net.sourceforge.pmd.properties.PropertySource;
18+
import net.sourceforge.pmd.properties.constraints.NumericConstraints;
1919

2020
/**
2121
* @author Brian Remedios
@@ -29,15 +29,10 @@ private DoubleEditorFactory() { }
2929

3030

3131
public PropertyDescriptor<Double> createDescriptor(String name, String description, Control[] otherData) {
32-
33-
return new DoubleProperty(
34-
name,
35-
description,
36-
defaultIn(otherData).doubleValue(),
37-
minimumIn(otherData).doubleValue(),
38-
maximumIn(otherData).doubleValue(),
39-
0.0f
40-
);
32+
return PropertyFactory.doubleProperty(name).desc(description)
33+
.defaultValue(defaultIn(otherData).doubleValue())
34+
.require(NumericConstraints.inRange(minimumIn(otherData).doubleValue(), maximumIn(otherData).doubleValue()))
35+
.build();
4136
}
4237

4338

@@ -50,7 +45,7 @@ protected Double valueFrom(Control valueControl) {
5045
public Control newEditorOn(Composite parent, final PropertyDescriptor<Double> desc, final PropertySource source,
5146
final ValueChangeListener listener, SizeChangeListener sizeListener) {
5247

53-
final Spinner spinner = newSpinnerFor(parent, source, (NumericPropertyDescriptor<Double>) desc);
48+
final Spinner spinner = newSpinnerFor(parent, source, desc);
5449

5550
spinner.addModifyListener(new ModifyListener() {
5651
public void modifyText(ModifyEvent event) {

net.sourceforge.pmd.eclipse.plugin/src/main/java/net/sourceforge/pmd/eclipse/ui/preferences/editors/EnumerationEditorFactory.java

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
package net.sourceforge.pmd.eclipse.ui.preferences.editors;
66

7-
import java.util.Map.Entry;
7+
import java.util.HashMap;
88

99
import org.eclipse.swt.SWT;
1010
import org.eclipse.swt.events.SelectionAdapter;
@@ -15,9 +15,8 @@
1515

1616
import net.sourceforge.pmd.eclipse.ui.preferences.br.SizeChangeListener;
1717
import net.sourceforge.pmd.eclipse.ui.preferences.br.ValueChangeListener;
18-
import net.sourceforge.pmd.properties.EnumeratedProperty;
19-
import net.sourceforge.pmd.properties.EnumeratedPropertyDescriptor;
2018
import net.sourceforge.pmd.properties.PropertyDescriptor;
19+
import net.sourceforge.pmd.properties.PropertyFactory;
2120
import net.sourceforge.pmd.properties.PropertySource;
2221

2322
/**
@@ -39,7 +38,7 @@ protected Object valueFrom(Control valueControl) {
3938

4039

4140
public PropertyDescriptor<Object> createDescriptor(String name, String optionalDescription, Control[] otherData) {
42-
return new EnumeratedProperty<Object>(name, "Value set " + name, null, null, 0, Object.class, 0.0f);
41+
return PropertyFactory.enumProperty(name, new HashMap<String, Object>()).desc("Value set " + name).build();
4342
}
4443

4544

@@ -48,23 +47,35 @@ public Control newEditorOn(Composite parent, final PropertyDescriptor<Object> de
4847

4948
final Combo combo = new Combo(parent, SWT.READ_ONLY);
5049

51-
final EnumeratedPropertyDescriptor<Object, Object> ep = (EnumeratedPropertyDescriptor<Object, Object>) desc;
5250
Object value = valueFor(source, desc);
53-
combo.setItems(SWTUtil.labelsIn(choices(ep), 0));
54-
int selectionIdx = indexOf(value, choices(ep));
51+
Object[][] choices = choices(desc);
52+
int selectionIdx = indexOf(value, choices);
53+
if (selectionIdx == -1) {
54+
Object[][] newChoices = new Object[choices.length + 1][2];
55+
newChoices[0] = new Object[]{ value.toString(), value };
56+
for (int i = 0; i < choices.length; i++) {
57+
newChoices[i + 1] = choices[i];
58+
}
59+
choices = newChoices;
60+
selectionIdx = 0;
61+
}
62+
63+
combo.setData(choices);
64+
combo.setItems(SWTUtil.labelsIn(choices, 0));
5565
if (selectionIdx >= 0) {
5666
combo.select(selectionIdx);
5767
}
5868

5969
combo.addSelectionListener(new SelectionAdapter() {
6070
public void widgetSelected(SelectionEvent e) {
6171
int selectionIdx = combo.getSelectionIndex();
62-
Object newValue = choices(ep)[selectionIdx][1];
72+
Object[][] choices = (Object[][]) combo.getData();
73+
Object newValue = choices[selectionIdx][1];
6374
if (newValue == valueFor(source, desc)) {
6475
return;
6576
}
6677

67-
source.setProperty(ep, newValue);
78+
source.setProperty(desc, newValue);
6879
listener.changed(source, desc, newValue);
6980
adjustRendering(source, desc, combo);
7081
}
@@ -92,13 +103,9 @@ public static int indexOf(Object item, Object[][] items, int testColumnIndex) {
92103
return -1;
93104
}
94105

95-
public static <E, T> Object[][] choices(EnumeratedPropertyDescriptor<E, T> prop) {
96-
Object[][] res = new Object[prop.mappings().size()][2];
97-
int i = 0;
98-
for (Entry<String, E> e : prop.mappings().entrySet()) {
99-
res[i][0] = e.getKey();
100-
res[i++][1] = e.getValue();
101-
}
106+
public static <T> Object[][] choices(PropertyDescriptor<T> prop) {
107+
Object[][] res = new Object[0][2];
108+
// TODO: prop.mapping() would be needed to get the valid choices....
102109
return res;
103110
}
104111
}

net.sourceforge.pmd.eclipse.plugin/src/main/java/net/sourceforge/pmd/eclipse/ui/preferences/editors/FileEditorFactory.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@
2121

2222
/**
2323
* @author Brian Remedios
24+
*
25+
* @deprecated will be removed
2426
*/
27+
@Deprecated
2528
public class FileEditorFactory extends AbstractEditorFactory<File> {
2629

2730

@@ -32,7 +35,6 @@ protected FileEditorFactory() { }
3235

3336

3437
public PropertyDescriptor<File> createDescriptor(String name, String description, Control[] otherData) {
35-
3638
return new FileProperty(
3739
name,
3840
description,

net.sourceforge.pmd.eclipse.plugin/src/main/java/net/sourceforge/pmd/eclipse/ui/preferences/editors/FilePicker.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import java.io.File;
88

9+
import org.apache.commons.lang3.StringUtils;
910
import org.eclipse.swt.SWT;
1011
import org.eclipse.swt.events.SelectionAdapter;
1112
import org.eclipse.swt.events.SelectionEvent;
@@ -20,8 +21,6 @@
2021
import org.eclipse.swt.widgets.Shell;
2122
import org.eclipse.swt.widgets.Text;
2223

23-
import net.sourceforge.pmd.util.StringUtil;
24-
2524
/**
2625
* A general purpose selection widget that deals with files.
2726
*
@@ -116,7 +115,7 @@ public void setEditable(boolean flag) {
116115
public File getFile() {
117116

118117
String name = fileField.getText();
119-
return StringUtil.isEmpty(name) ? null : new File(name);
118+
return StringUtils.isBlank(name) ? null : new File(name);
120119
}
121120

122121
}

net.sourceforge.pmd.eclipse.plugin/src/main/java/net/sourceforge/pmd/eclipse/ui/preferences/editors/FloatEditorFactory.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@
1919

2020
/**
2121
* @author Brian Remedios
22+
*
23+
* @deprecated This type is unused and won't be supported in the future by PMD
2224
*/
25+
@Deprecated
2326
public class FloatEditorFactory extends AbstractRealNumberEditor<Float> {
2427

2528
public static final FloatEditorFactory INSTANCE = new FloatEditorFactory();
@@ -29,7 +32,6 @@ private FloatEditorFactory() { }
2932

3033

3134
public PropertyDescriptor<Float> createDescriptor(String name, String description, Control[] otherData) {
32-
3335
return new FloatProperty(
3436
name,
3537
description,

0 commit comments

Comments
 (0)