List of usage examples for java.beans PropertyEditor isPaintable
boolean isPaintable();
From source file:org.lnicholls.galleon.gui.HMEConfigurationPanel.java
public HMEConfigurationPanel(Object bean) { setLayout(new GridLayout(0, 1)); target = bean;//from w ww .j a va2 s .c o m try { BeanInfo bi = Introspector.getBeanInfo(target.getClass()); properties = bi.getPropertyDescriptors(); } catch (IntrospectionException ex) { Tools.logException(HMEConfigurationPanel.class, ex, "PropertySheet: Couldn't introspect"); return; } editors = new PropertyEditor[properties.length]; values = new Object[properties.length]; views = new Component[properties.length]; labels = new JLabel[properties.length]; for (int i = 0; i < properties.length; i++) { // Don't display hidden or expert properties. if (properties[i].isHidden() || properties[i].isExpert()) { continue; } String name = properties[i].getDisplayName(); Class type = properties[i].getPropertyType(); Method getter = properties[i].getReadMethod(); Method setter = properties[i].getWriteMethod(); // Only display read/write properties. if (getter == null || setter == null) { continue; } Component view = null; try { Object args[] = {}; Object value = getter.invoke(target, args); values[i] = value; PropertyEditor editor = null; Class pec = properties[i].getPropertyEditorClass(); if (pec != null) { try { editor = (PropertyEditor) pec.newInstance(); } catch (Exception ex) { // Drop through. } } if (editor == null) { editor = PropertyEditorManager.findEditor(type); } editors[i] = editor; // If we can't edit this component, skip it. if (editor == null) { // If it's a user-defined property we give a warning. String getterClass = properties[i].getReadMethod().getDeclaringClass().getName(); if (getterClass.indexOf("java.") != 0) { log.error("Warning: Can't find public property editor for property \"" + name + "\". Skipping."); } continue; } // Don't try to set null values: if (value == null) { // If it's a user-defined property we give a warning. String getterClass = properties[i].getReadMethod().getDeclaringClass().getName(); if (getterClass.indexOf("java.") != 0) { log.error("Warning: Property \"" + name + "\" has null initial value. Skipping."); } continue; } editor.setValue(value); // editor.addPropertyChangeListener(adaptor); // Now figure out how to display it... if (editor.isPaintable() && editor.supportsCustomEditor()) { view = new PropertyCanvas(frame, editor); } else if (editor.getTags() != null) { view = new PropertySelector(editor); } else if (editor.getAsText() != null) { String init = editor.getAsText(); view = new PropertyText(editor); } else { log.error("Warning: Property \"" + name + "\" has non-displayabale editor. Skipping."); continue; } } catch (InvocationTargetException ex) { Tools.logException(HMEConfigurationPanel.class, ex.getTargetException(), "Skipping property " + name); continue; } catch (Exception ex) { Tools.logException(HMEConfigurationPanel.class, ex, "Skipping property " + name); continue; } labels[i] = new JLabel(WordUtils.capitalize(name), JLabel.RIGHT); // add(labels[i]); views[i] = view; // add(views[i]); } int validCounter = 0; for (int i = 0; i < labels.length; i++) { if (labels[i] != null) validCounter++; } String rowStrings = ""; // general if (validCounter > 0) rowStrings = "pref, "; else rowStrings = "pref"; int counter = 0; for (int i = 0; i < labels.length; i++) { if (labels[i] != null) { if (++counter == (validCounter)) rowStrings = rowStrings + "9dlu, " + "pref"; else rowStrings = rowStrings + "9dlu, " + "pref, "; } } FormLayout layout = new FormLayout("right:pref, 3dlu, 50dlu:g, right:pref:grow", rowStrings); PanelBuilder builder = new PanelBuilder(layout); //DefaultFormBuilder builder = new DefaultFormBuilder(new FormDebugPanel(), layout); builder.setDefaultDialogBorder(); CellConstraints cc = new CellConstraints(); builder.addSeparator("General", cc.xyw(1, 1, 4)); counter = 0; for (int i = 0; i < labels.length; i++) { if (labels[i] != null) { counter++; builder.add(labels[i], cc.xy(1, counter * 2 + 1)); builder.add(views[i], cc.xy(3, counter * 2 + 1)); } } JPanel panel = builder.getPanel(); //FormDebugUtils.dumpAll(panel); add(panel); }