private static void loadPropertyNamesValues( Class clazz, Object instance, List names, List values) { PropertyDescriptor pd; BeanInfo info = null; try { info = Introspector.getBeanInfo(clazz); } catch (IntrospectionException ex) { Err.error(ex); } PropertyDescriptor[] propertyDescriptors = info.getPropertyDescriptors(); for (int i = 0; i < propertyDescriptors.length; ++i) { pd = propertyDescriptors[i]; Object value = SelfReferenceUtils.invokeReadProperty(info, pd, instance, pd.getName()); if (value == null) { // Err.pr( "No property set for " + pd.getName() + " in " + clazz); } else { names.add(pd.getName()); values.add(value); } } if (names.isEmpty()) { Err.error("Strange that there are no properties with values in " + clazz); } }
/** Should not be used directly by Strandz. See package org.strandz.core.info.convert */ public static String _getText(Object comp) { String result = null; Err.pr(SdzNote.DONT_USE_FROM_STRANDZ, "Don't use"); if (comp instanceof JComponent) { if (comp instanceof JTextComponent) { result = ((JTextComponent) comp).getText(); } else if (comp instanceof JLabel) { result = ((JLabel) comp).getText(); } else { Err.error( "ComponentUtils.getText(), not yet support JComponent of type " + comp.getClass()); } } else { Err.error("ComponentUtils.getText(), not yet support " + comp.getClass()); } return result; }
/** * Should not be used directly by Strandz. See package org.strandz.core.info.convert This must * come out as Strandz needs control over assignment * * @param comp * @param txt */ public static void _setText(Object comp, Object txt) { Err.pr(SdzNote.DONT_USE_FROM_STRANDZ, "Don't use"); if (comp instanceof JComponent) { String s = null; if (txt != null) { s = txt.toString(); } if (comp instanceof JTextComponent) { ((JTextComponent) comp).setText(s); } else if (comp instanceof JLabel) { ((JLabel) comp).setText(s); } else { Err.error( "ComponentUtils.setText(), not yet support JComponent of type " + comp.getClass()); } } else { Err.error("ComponentUtils.setText(), not yet support " + comp.getClass()); } }
public static void setEditable(Object comp, boolean b) { Class[] args1 = new Class[1]; args1[0] = Boolean.TYPE; Method setEditableControlMethod = null; String methodName = "setEditable"; Class controlClass = comp.getClass(); try { setEditableControlMethod = controlClass.getMethod(methodName, args1); } catch (Exception ex) { Err.error("Missing method " + methodName + " from " + controlClass); } SelfReferenceUtils.invoke(comp, setEditableControlMethod, Boolean.valueOf(b)); }
public static String getName(Object comp) { String result; Method getNameControlMethod = null; String methodName = "getName"; Class controlClass = comp.getClass(); try { getNameControlMethod = controlClass.getMethod(methodName, (Class[]) null); } catch (Exception ex) { Err.error("Missing method " + methodName + " from " + controlClass); } result = (String) SelfReferenceUtils.invoke(comp, getNameControlMethod); return result; }
public static boolean hasEditableMethod(Object comp) { boolean result = true; String methodName = "isEditable"; Class controlClass = comp.getClass(); try { controlClass.getMethod(methodName, (Class[]) null); } catch (NoSuchMethodException ex1) { result = false; } catch (Exception ex2) { Err.error("Missing method " + methodName + " from " + controlClass + ", ex: " + ex2); } return result; }
public static boolean controlNameIsUniqueIn(String name, List controls) { boolean result = false; if (name == null) { Err.error("controlNameIsUniqueIn() cannot be called with a null name"); } int timesFound = 0; for (Iterator iter = controls.iterator(); iter.hasNext(); ) { Component element = (Component) iter.next(); String elementName = element.getName(); if (elementName != null) { if (name.equals(elementName)) { timesFound++; } } } if (timesFound == 1) { result = true; } else if (timesFound == 0) { Err.error("This method assumes that <" + name + "> exists at least once"); } return result; }
/** * TODO - Think about reason are not going to ControlSignatures for this. Pros for not - * ControlSignatures is part of strandz core, whereas this is lgpl Pros for having in * ControlSignatures - Controls inside a table should be treated in exactly the same way as other * field controls. (This would require changes to info package). * * <p>Of course providing a service interface would be the answer, but changing info provides * inertia. Bugs because of differences will help the case for change. */ public static boolean isEditable(Object comp) { boolean result = false; Method isEditableControlMethod = null; String methodName = "isEditable"; Class controlClass = comp.getClass(); boolean noMethod = false; try { isEditableControlMethod = controlClass.getMethod(methodName, (Class[]) null); } catch (NoSuchMethodException ex1) { noMethod = true; } catch (Exception ex2) { Err.error("Missing method " + methodName + " from " + controlClass + ", ex: " + ex2); } if (!noMethod) { result = ((Boolean) SelfReferenceUtils.invoke(comp, isEditableControlMethod)).booleanValue(); } return result; }
public static void setText(Object comp, Object txt) { Class[] args1 = new Class[1]; args1[0] = String.class; Method setTextControlMethod = null; String methodName = "setText"; Class controlClass = comp.getClass(); try { setTextControlMethod = controlClass.getMethod(methodName, args1); } catch (Exception ex1) { args1[0] = Object.class; try { setTextControlMethod = controlClass.getMethod(methodName, args1); } catch (Exception ex2) { Err.error("Missing method " + methodName + " from " + controlClass); } } String s = null; if (txt != null) { s = txt.toString(); } SelfReferenceUtils.invoke(comp, setTextControlMethod, s); }
public static boolean equalsByProperties( JComponent component1, JComponent component2, WidgetClassifierI widgetClassifier) { ReasonNotEquals.addClassVisiting("pUtils.equalsByProperties()"); boolean result = true; List oneControls = getAllControls(component1); oneControls.add(0, component1); List twoControls = getAllControls(component2); twoControls.add(0, component2); if (oneControls.size() != twoControls.size()) { ReasonNotEquals.addReason( "got lists of controls of different sizes: " + oneControls.size() + " and " + twoControls.size()); Print.prList(oneControls, "oneControls"); Print.prList(twoControls, "twoControls"); result = false; } else { for (int i = 0; i < oneControls.size(); i++) { Container comp1 = (Container) oneControls.get(i); // Err.pr( "Observing a " + comp1.getClass()); Container comp2 = (Container) twoControls.get(i); if (comp1.getClass() != comp2.getClass()) { ReasonNotEquals.addReason( "first component is of type " + comp1.getClass() + " while second is of type " + comp2.getClass()); result = false; break; } else if (!Utils.equals(comp1.getName(), comp2.getName())) { ReasonNotEquals.addReason( "first component has name " + comp1.getName() + " while second has name " + comp2.getName()); result = false; break; } else if (widgetClassifier != null && !widgetClassifier.isAWidget( comp1.getClass())) { // If a comp1/comp2 is not a widget then end up here and we // won't examine // its properties // Err.pr( comp1.getClass() + " is not a widget: " + widgetClassifier.getReason()); } else { List propertyNamesOne = new ArrayList(); List propertyNamesTwo = new ArrayList(); List propertyValuesOne = new ArrayList(); List propertyValuesTwo = new ArrayList(); loadPropertyNamesValues(comp1.getClass(), comp1, propertyNamesOne, propertyValuesOne); loadPropertyNamesValues(comp2.getClass(), comp2, propertyNamesTwo, propertyValuesTwo); if (propertyNamesOne.size() != propertyValuesOne.size()) { Err.error("expect names and properties to be same size for " + comp1.getClass()); } else if (propertyNamesTwo.size() != propertyValuesTwo.size()) { Err.error("expect names and properties to be same size for " + comp2.getClass()); } else if (propertyNamesOne.size() != propertyNamesTwo.size()) { ReasonNotEquals.addReason( "there should be an equal number of properties in " + comp1.getClass() + " and " + comp2.getClass()); result = false; break; } else { // Err.pr( "Will be examining properties from a " + comp1.getClass() + " against " + // comp2.getClass()); for (int j = 0; j < propertyNamesOne.size(); j++) { String onePropName = (String) propertyNamesOne.get(j); String twoPropName = (String) propertyNamesTwo.get(j); if (!onePropName.equals(twoPropName)) { // If they are of thge same type, how could they possibly have // different property names, thus make this an error String txt = "expect property names to be equal, got " + onePropName + " and " + twoPropName; // ReasonNotEquals.addReason( txt); Err.error(txt); result = false; break; } else { Object onePropValue = propertyValuesOne.get(j); Object twoPropValue = propertyValuesTwo.get(j); boolean debug = false; /* if(onePropName.equals( "text")) { Err.debug(); debug = true; } */ if (onePropValue.getClass() != twoPropValue.getClass()) { Err.error( "Property values to be examining should be of the same type, got " + onePropValue.getClass().getName() + " and " + twoPropValue.getClass().getName()); result = false; break; } else if (!Utils.isSimple(onePropValue.getClass())) { // We only want to do value comparisons for simple types // Err.pr( "Not simple: <" + onePropValue + ">"); if (debug) { Err.error("Only supposed to be debugging on a simple property value"); } } else { if (debug) { Err.pr( "Doing cf between <" + onePropValue + "> and <" + twoPropValue + "> for " + onePropName + " in a " + comp1.getClass()); debug = false; } if (!onePropValue.equals(twoPropValue)) { ReasonNotEquals.addReason( "expect property values to be equal, got <" + onePropValue + "> and <" + twoPropValue + ">"); result = false; break; } else { // Err.pr( "\tconcordance, prop name " + onePropName + ", prop value " + // onePropValue); } } } } if (result == false) { break; // so break out of outer loop too } } } } } return result; }