Optimize code in PropertySheetPanel

refs #62
This commit is contained in:
2015-12-23 01:56:19 +01:00
parent 3172d1b4b5
commit 96f5fd8a14

View File

@@ -234,26 +234,20 @@ public final class PropertySheetPanel extends JPanel implements PropertyChangeLi
} }
} }
int methsFound = 0; // don't loop too long, so count until all found // Allow object to mark properties hidden
for (MethodDescriptor methodDescriptor : methodDescriptors) { BeanInspector.callIfAvailable(targ, "hideHideable", null);
String name = methodDescriptor.getDisplayName();
Method meth = methodDescriptor.getMethod(); // Allow object to reorder properties
if (name.equals("hideHideable")) { Object retV = BeanInspector.callIfAvailable(targ, "customPropertyOrder", null);
Object args[] = {}; if (retV != null) {
try { if (retV.getClass().isArray()) {
meth.invoke(targetObject, args); reorderProperties((String[]) retV);
} catch (Exception ex) { } else {
} LOGGER.severe("Class defines custom property order but returns invalid type.");
methsFound++;
} else if (name.equals("customPropertyOrder")) {
methsFound++;
reorderProperties(meth);
}
if (methsFound == 2) {
break; // small speed-up
} }
} }
// Now lets search for the individual properties, their // Now lets search for the individual properties, their
// values, views and editors... // values, views and editors...
propertyEditors = new PropertyEditor[propertyDescriptors.length]; propertyEditors = new PropertyEditor[propertyDescriptors.length];
@@ -324,7 +318,7 @@ public final class PropertySheetPanel extends JPanel implements PropertyChangeLi
setVisible(true); setVisible(true);
} }
private String prepareLabel(String label) { private static String prepareLabel(String label) {
// Add some specific display for some greeks here // Add some specific display for some greeks here
label = StringTools.translateGreek(label); label = StringTools.translateGreek(label);
label = StringTools.humaniseCamelCase(label); label = StringTools.humaniseCamelCase(label);
@@ -438,21 +432,14 @@ public final class PropertySheetPanel extends JPanel implements PropertyChangeLi
/** /**
* Be sure to give a clone * Be sure to give a clone
* *
* @param meth * @param swProps
* @return * @return
*/ */
private PropertyDescriptor[] reorderProperties(Method meth) { private PropertyDescriptor[] reorderProperties(String[] swProps) {
Object[] args = {}; Object[] args = {};
Object retV = null;
PropertyDescriptor[] newProps = null; PropertyDescriptor[] newProps = null;
if (swProps != null) {
try { try {
retV = meth.invoke(targetObject, args); // should return String[] to be interpreted as a list of ordered properties
} catch (Exception ex) {
}
if (retV != null) {
try {
if (retV.getClass().isArray()) { // reorder the properties
String[] swProps = (String[]) retV;
PropertyDescriptor[] oldProps = propertyDescriptors.clone(); PropertyDescriptor[] oldProps = propertyDescriptors.clone();
newProps = new PropertyDescriptor[oldProps.length]; newProps = new PropertyDescriptor[oldProps.length];
//int findFirst=findFirstProp(props[0], oldProps); //int findFirst=findFirstProp(props[0], oldProps);
@@ -469,7 +456,6 @@ public final class PropertySheetPanel extends JPanel implements PropertyChangeLi
} }
} }
propertyDescriptors = newProps; propertyDescriptors = newProps;
}
} catch (Exception e) { } catch (Exception e) {
System.err.println("Error during reordering properties: " + e.getMessage()); System.err.println("Error during reordering properties: " + e.getMessage());
return propertyDescriptors; return propertyDescriptors;
@@ -584,12 +570,10 @@ public final class PropertySheetPanel extends JPanel implements PropertyChangeLi
Method getter = propertyDescriptors[i].getReadMethod(); Method getter = propertyDescriptors[i].getReadMethod();
objectValues[i] = newValue; objectValues[i] = newValue;
Method setter = property.getWriteMethod(); Method setter = property.getWriteMethod();
// @todo: Streiche so something was changed, i could check if i have to change the editor
PropertyEditor tmpEdit = null;
// the findEditor method using properties may retrieve a primitive editor, the other one, for obscure reasons, cant. // the findEditor method using properties may retrieve a primitive editor, the other one, for obscure reasons, cant.
// so Ill use the mightier first. // so Ill use the mightier first.
tmpEdit = PropertyEditorProvider.findEditor(propertyDescriptors[i], newValue); PropertyEditor tmpEdit = PropertyEditorProvider.findEditor(propertyDescriptors[i], newValue);
if (tmpEdit == null) { if (tmpEdit == null) {
tmpEdit = PropertyEditorProvider.findEditor(propertyDescriptors[i].getPropertyType()); tmpEdit = PropertyEditorProvider.findEditor(propertyDescriptors[i].getPropertyType());
} }
@@ -600,8 +584,8 @@ public final class PropertySheetPanel extends JPanel implements PropertyChangeLi
((GenericObjectEditor) tmpEdit).setClassType(propertyDescriptors[i].getPropertyType()); ((GenericObjectEditor) tmpEdit).setClassType(propertyDescriptors[i].getPropertyType());
} }
propertyEditors[i].setValue(newValue); propertyEditors[i].setValue(newValue);
JComponent newView = null;
newView = getView(tmpEdit); JComponent newView = getView(tmpEdit);
if (newView == null) { if (newView == null) {
LOGGER.warning("Property \"" + propertyDescriptors[i].getDisplayName() + "\" has non-displayable editor. Skipping."); LOGGER.warning("Property \"" + propertyDescriptors[i].getDisplayName() + "\" has non-displayable editor. Skipping.");
return false; return false;
@@ -670,7 +654,7 @@ public final class PropertySheetPanel extends JPanel implements PropertyChangeLi
for (int i = 0; i < propertyEditors.length; i++) { for (int i = 0; i < propertyEditors.length; i++) {
if (propertyEditors[i] == editor) { if (propertyEditors[i] == editor) {
propIndex = i; propIndex = i;
if (wasModified(i, editor.getValue(), true)) { if (wasModified(i, editor.getValue())) {
break; break;
} }
} }
@@ -688,13 +672,14 @@ public final class PropertySheetPanel extends JPanel implements PropertyChangeLi
* propertysheet?). * propertysheet?).
* *
*/ */
synchronized boolean wasModified(int propIndex, Object value, boolean followDependencies) { synchronized boolean wasModified(int propIndex, Object value) {
if (!updateValue(propIndex, value)) { if (!updateValue(propIndex, value)) {
return false; return false;
} }
boolean doRepaint = false; boolean doRepaint = false;
// ToDo Should be foreach (to skip non existing editors)
for (int i = 0; i < propertyEditors.length; i++) { // check the views for out-of-date information. this is different than checking the editors for (int i = 0; i < propertyEditors.length; i++) { // check the views for out-of-date information. this is different than checking the editors
if (i != propIndex) { if (i != propIndex) {
if (updateFieldView(i)) { if (updateFieldView(i)) {
@@ -715,10 +700,12 @@ public final class PropertySheetPanel extends JPanel implements PropertyChangeLi
// for any other properties that have changed. // for any other properties that have changed.
for (int i = 0; i < propertyDescriptors.length; i++) { for (int i = 0; i < propertyDescriptors.length; i++) {
Object o; Object o;
Method getter = null; Method getter;
// Make sure we have an editor for this property...
if (propertyEditors[i] == null) { if (propertyEditors[i] == null) {
continue; /// TODO: MK: Im not quite sure this is all good, but it avoids a latency problem continue;
} }
try { try {
getter = propertyDescriptors[i].getReadMethod(); getter = propertyDescriptors[i].getReadMethod();
Object args[] = {}; Object args[] = {};
@@ -728,6 +715,7 @@ public final class PropertySheetPanel extends JPanel implements PropertyChangeLi
System.err.println(ex.getMessage()); System.err.println(ex.getMessage());
ex.printStackTrace(); ex.printStackTrace();
} }
if ((o != null) && o == objectValues[i] && (BeanInspector.isJavaPrimitive(o.getClass()))) { if ((o != null) && o == objectValues[i] && (BeanInspector.isJavaPrimitive(o.getClass()))) {
// The property is equal to its old value. // The property is equal to its old value.
continue; continue;
@@ -737,10 +725,7 @@ public final class PropertySheetPanel extends JPanel implements PropertyChangeLi
continue; continue;
} }
objectValues[i] = o; objectValues[i] = o;
// Make sure we have an editor for this property...
if (propertyEditors[i] == null) {
continue;
}
// The property has changed! Update the editor. // The property has changed! Update the editor.
propertyEditors[i].removePropertyChangeListener(this); propertyEditors[i].removePropertyChangeListener(this);
propertyEditors[i].setValue(o); propertyEditors[i].setValue(o);