/** Try to create a Java object using a one-string-param constructor. */ public static Object newStringConstructor(String type, String param) throws Exception { Constructor c = Utils.getClass(type).getConstructor(String.class); try { return c.newInstance(param); } catch (InvocationTargetException e) { Throwable t = e.getTargetException(); if (t instanceof Exception) { throw (Exception) t; } else { throw e; } } }
public XTextField( Object value, Class<?> expectedClass, int colWidth, boolean isCallable, JButton button, XOperations operation) { super(new BorderLayout()); this.button = button; this.operation = operation; add(textField = new JTextField(value.toString(), colWidth), BorderLayout.CENTER); if (isCallable) textField.addActionListener(this); boolean fieldEditable = Utils.isEditableType(expectedClass.getName()); if (fieldEditable && isCallable) { textField.setEditable(true); } else { textField.setEditable(false); } }
protected void init(Object value, Class<?> expectedClass) { boolean fieldEditable = Utils.isEditableType(expectedClass.getName()); clearObject(); if (value != null) { textField.setText(value.toString()); } else { // null String value for the moment textField.setText(""); } textField.setToolTipText(null); if (fieldEditable) { if (!textField.isEditable()) { textField.setEditable(true); } } else { if (textField.isEditable()) { textField.setEditable(false); } } }
/** * This method attempts to create an object of the given "type" using the "value" parameter. e.g. * calling createObjectFromString("java.lang.Integer", "10") will return an Integer object * initialized to 10. */ public static Object createObjectFromString(String type, String value) throws Exception { Object result; if (primitiveToWrapper.containsKey(type)) { if (type.equals(Character.TYPE.getName())) { result = new Character(value.charAt(0)); } else { result = newStringConstructor(((Class<?>) primitiveToWrapper.get(type)).getName(), value); } } else if (type.equals(Character.class.getName())) { result = new Character(value.charAt(0)); } else if (Number.class.isAssignableFrom(Utils.getClass(type))) { result = createNumberFromStringValue(value); } else if (value == null || value.toString().equals("null")) { // hack for null value result = null; } else { // try to create a Java object using // the one-string-param constructor result = newStringConstructor(type, value); } return result; }