📄 genericpropertysheet.java
字号:
private PropertyDescriptor[] properties; private PropertyEditor[] editors; private Object[] values; private Component[] views; private JLabel[] labels; private boolean processEvents; PropertySheetPanel(GenericPropertySheet frame) { _frame = frame; this.setLayout(null); this.setBorder(BorderFactory.createEmptyBorder(0, 5, 5, 5)); } synchronized void setTarget(Object targ) { //System.out.println("Enter> // PropertySheetPanel.setTarget()"); // We make the panel invisible during the reconfiguration // to try to reduce screen flicker. setVisible(false); removeAll(); targetBean = targ; try { BeanInfo bi = Introspector.getBeanInfo(targetBean.getClass()); properties = bi.getPropertyDescriptors(); } catch (IntrospectionException ex) { error("GenericPropertySheet: Couldn't introspect", ex); return; } editors = new PropertyEditor[properties.length]; values = new Object[properties.length]; views = new Component[properties.length]; labels = new JLabel[properties.length]; int numEditorsToDisplay = 0; for (int i = 0; i < properties.length; i++) { String name = properties[i].getDisplayName(); // Don't display hidden or expert properties. if (properties[i].isHidden() || properties[i].isExpert()) { System.out.println("Ignoring hidden or expert property " + name); continue; } 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)) { System.out.println("Ignoring read-only/write-only property " + name); continue; } Component view = null; try { Object args[] = {}; Object value = getter.invoke(targetBean, 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. System.out.println("Cannot instanciate editor class: " + pec); System.out.println("Will try to find editor using " + "PropertyEditorManager"); } } 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) System.err.println("Warning: Can't find public property editor for property \"" + name + "\". Skipping."); continue; } //System.out.println("About to set value " + value); editor.setValue(value); editor.addPropertyChangeListener(_frame); // 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) { view = new PropertyText(editor); } else { System.err.println("Warning: Property \"" + name + "\" has non-displayabale editor. Skipping."); continue; } if (editor instanceof GenericPropertyEditorInterface) ((GenericPropertyEditorInterface) editor).setTargetBean(targetBean); } catch (InvocationTargetException ex) { System.err.println("Skipping property " + name + " ; exception on target: " + ex.getTargetException()); ex.getTargetException().printStackTrace(); continue; } catch (Exception ex) { System.err.println("Skipping property " + name + "; exception: " + ex); ex.printStackTrace(); continue; } labels[i] = new JLabel(name, JLabel.LEFT); views[i] = view; numEditorsToDisplay++; } // end for this.setLayout(new GridLayout(numEditorsToDisplay, 2)); for (int i = 0; i < properties.length; i++) if (views[i] != null) { add(labels[i]); add(views[i]); } _frame.setNumEditorsToDisplay(numEditorsToDisplay); _frame.setFrameSize(); processEvents = true; setVisible(true); //System.out.println("Exit> PropertySheetPanel.setTarget()"); } // end setTarget synchronized void setCustomizer(Customizer c) { if (c != null) c.addPropertyChangeListener(_frame); } synchronized void wasModified(PropertyChangeEvent evt) { //System.out.println("Enter> // PropertySheetPanel.wasModified"); //System.out.println("evt = " + evt); if (!processEvents) { //System.out.println("Exit>GPS::wasModified"); return; } if (evt.getSource() instanceof PropertyEditor) { PropertyEditor editor = (PropertyEditor) evt.getSource(); //System.out.println("editor="+editor); for (int i = 0; i < editors.length; i++) if (editors[i] == editor) { PropertyDescriptor property = properties[i]; Object value = editor.getValue(); // if value is the string "null", reset it to null if ((value != null) && (value instanceof String) && ((String) value).trim().equalsIgnoreCase("null")) value = null; values[i] = value; Method setter = property.getWriteMethod(); try { Object args[] = { value }; args[0] = value; setter.invoke(targetBean, args); } catch (InvocationTargetException ex) { if (ex.getTargetException() instanceof PropertyVetoException) { //warning("Vetoed; reason is: " // + // ex.getTargetException().getMessage()); // temp dealock fix...I need to remove the // deadlock. System.err.println("WARNING: Vetoed; reason is: " + ex.getTargetException().getMessage()); } else error("InvocationTargetException while updating " + property.getName(), ex.getTargetException()); } catch (Exception ex) { error("Unexpected exception while updating " + property.getName(), ex); } if ((views[i] != null) && (views[i] instanceof PropertyCanvas)) { //System.out.println("repainting view"); views[i].repaint(); } break; } } //System.out.println("updating other values..."); // we want to update in the target // Now re-read all the properties and update the editors // for any other properties that have changed. for (int i = 0; i < properties.length; i++) { Object o; try { Method getter = properties[i].getReadMethod(); Object args[] = {}; o = getter.invoke(targetBean, args); } catch (Exception ex) { //System.out.println(" setting o to null"); o = null; } //System.out.println(" values[" + i + "]=" + values[i]); // check if 'o' is of type Object[] if ((o instanceof Object[]) && (values[i] instanceof Object[])) { Object[] oldVal = (Object[]) values[i]; Object[] newVal = (Object[]) o; if (newVal.length == oldVal.length) { for (int j = 0; j < newVal.length; j++) if (!newVal[j].equals(oldVal[j])) break; continue; } } else if ((o == values[i]) || ((o != null) && o.equals(values[i]))) // The property is equal to its old value. continue; values[i] = o; //System.out.println(" editors[" + i + "]=" + // editors[i]); // Make sure we have an editor for this property... if (editors[i] == null) continue; //System.out.println(" calling setValue on // editors["+i+"]="+editors[i]); // The property has changed! Update the editor. editors[i].setValue(o); if (views[i] != null) views[i].repaint(); } // Make sure the target bean gets repainted. if (Beans.isInstanceOf(targetBean, Component.class)) ((Component) (Beans.getInstanceOf(targetBean, Component.class))).repaint(); //System.out.println("Exit-> // PropertySheetPanel.wasModified"); } //----------------------------------------------------------------------// private void warning(String s) {// System.out.println("Warning: " + s);// } //---------------------------------------------------------------------- // Log an error. private void error(String message, Throwable th) { String mess = message + ":\n" + th; System.err.println(message); th.printStackTrace(); System.out.println(mess); } //----------------------------------------------------------------------}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -