📄 propertysheetpanel.java
字号:
Class pec = m_Properties[i].getPropertyEditorClass(); if (pec != null) { try { editor = (PropertyEditor)pec.newInstance(); } catch (Exception ex) { // Drop through. } } if (editor == null) { editor = PropertyEditorManager.findEditor(type); } m_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 = m_Properties[i].getReadMethod() .getDeclaringClass().getName(); /* if (getterClass.indexOf("java.") != 0) { System.err.println("Warning: Can't find public property editor" + " for property \"" + name + "\" (class \"" + type.getName() + "\"). Skipping."); } */ continue; } if (editor instanceof GenericObjectEditor) { ((GenericObjectEditor) editor).setClassType(type); } // Don't try to set null values: if (value == null) { // If it's a user-defined property we give a warning. String getterClass = m_Properties[i].getReadMethod() .getDeclaringClass().getName(); /* if (getterClass.indexOf("java.") != 0) { System.err.println("Warning: Property \"" + name + "\" has null initial value. Skipping."); } */ continue; } editor.setValue(value); // now look for a TipText method for this property String tipName = name + "TipText"; for (int j = 0; j < m_Methods.length; j++) { String mname = m_Methods[j].getDisplayName(); Method meth = m_Methods[j].getMethod(); if (mname.equals(tipName)) { if (meth.getReturnType().equals(String.class)) { try { String tempTip = (String)(meth.invoke(m_Target, args)); int ci = tempTip.indexOf('.'); if (ci < 0) { m_TipTexts[i] = tempTip; } else { m_TipTexts[i] = tempTip.substring(0, ci); } if (m_HelpText != null) { if (firstTip) { m_HelpText.append("OPTIONS\n"); firstTip = false; } m_HelpText.append(name).append(" -- "); m_HelpText.append(tempTip).append("\n\n"); //jt.setText(m_HelpText.toString()); } } catch (Exception ex) { } break; } } } // Now figure out how to display it... if (editor.isPaintable() && editor.supportsCustomEditor()) { view = new PropertyPanel(editor); } else if (editor.getTags() != null) { view = new PropertyValueSelector(editor); } else if (editor.getAsText() != null) { //String init = editor.getAsText(); view = new PropertyText(editor); } else { System.err.println("Warning: Property \"" + name + "\" has non-displayabale editor. Skipping."); continue; } editor.addPropertyChangeListener(this); } 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; } m_Labels[i] = new JLabel(name, SwingConstants.RIGHT); m_Labels[i].setBorder(BorderFactory.createEmptyBorder(10, 10, 0, 5)); m_Views[i] = view; GridBagConstraints gbConstraints = new GridBagConstraints(); gbConstraints.anchor = GridBagConstraints.EAST; gbConstraints.fill = GridBagConstraints.HORIZONTAL; gbConstraints.gridy = i+componentOffset; gbConstraints.gridx = 0; gbLayout.setConstraints(m_Labels[i], gbConstraints); add(m_Labels[i]); JPanel newPanel = new JPanel(); if (m_TipTexts[i] != null) { m_Views[i].setToolTipText(m_TipTexts[i]); } newPanel.setBorder(BorderFactory.createEmptyBorder(10, 5, 0, 10)); newPanel.setLayout(new BorderLayout()); newPanel.add(m_Views[i], BorderLayout.CENTER); gbConstraints = new GridBagConstraints(); gbConstraints.anchor = GridBagConstraints.WEST; gbConstraints.fill = GridBagConstraints.BOTH; gbConstraints.gridy = i+componentOffset; gbConstraints.gridx = 1; gbConstraints.weightx = 100; gbLayout.setConstraints(newPanel, gbConstraints); add(newPanel); m_NumEditable ++; } if (m_NumEditable == 0) { JLabel empty = new JLabel("No editable properties", SwingConstants.CENTER); Dimension d = empty.getPreferredSize(); empty.setPreferredSize(new Dimension(d.width * 2, d.height * 2)); empty.setBorder(BorderFactory.createEmptyBorder(10, 5, 0, 10)); GridBagConstraints gbConstraints = new GridBagConstraints(); gbConstraints.anchor = GridBagConstraints.CENTER; gbConstraints.fill = GridBagConstraints.HORIZONTAL; gbConstraints.gridy = componentOffset; gbConstraints.gridx = 0; gbLayout.setConstraints(empty, gbConstraints); add(empty); } validate(); setVisible(true); } protected void openHelpFrame() { JTextArea ta = new JTextArea(); ta.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); ta.setLineWrap(true); ta.setWrapStyleWord(true); //ta.setBackground(getBackground()); ta.setEditable(false); ta.setText(m_HelpText.toString()); ta.setCaretPosition(0); final JFrame jf = new JFrame("Information"); jf.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { jf.dispose(); if (m_HelpFrame == jf) { m_HelpBut.setEnabled(true); } } }); jf.getContentPane().setLayout(new BorderLayout()); jf.getContentPane().add(new JScrollPane(ta), BorderLayout.CENTER); jf.pack(); jf.setSize(400, 350); jf.setLocation(getTopLevelAncestor().getLocationOnScreen().x + getTopLevelAncestor().getSize().width, getTopLevelAncestor().getLocationOnScreen().y); jf.setVisible(true); m_HelpFrame = jf; } /** * Gets the number of editable properties for the current target. * * @return the number of editable properties. */ public int editableProperties() { return m_NumEditable; } /** * Updates the propertysheet when a value has been changed (from outside * the propertysheet?). * * @param evt a value of type 'PropertyChangeEvent' */ synchronized void wasModified(PropertyChangeEvent evt) { // System.err.println("wasModified"); if (evt.getSource() instanceof PropertyEditor) { PropertyEditor editor = (PropertyEditor) evt.getSource(); for (int i = 0 ; i < m_Editors.length; i++) { if (m_Editors[i] == editor) { PropertyDescriptor property = m_Properties[i]; Object value = editor.getValue(); m_Values[i] = value; Method setter = property.getWriteMethod(); try { Object args[] = { value }; args[0] = value; setter.invoke(m_Target, args); } catch (InvocationTargetException ex) { if (ex.getTargetException() instanceof PropertyVetoException) { System.err.println("WARNING: Vetoed; reason is: " + ex.getTargetException().getMessage()); } else { System.err.println("InvocationTargetException while updating " + property.getName()); } } catch (Exception ex) { System.err.println("Unexpected exception while updating " + property.getName()); } if (m_Views[i] != null && m_Views[i] instanceof PropertyPanel) { //System.err.println("Trying to repaint the property canvas"); m_Views[i].repaint(); revalidate(); } break; } } } // Now re-read all the properties and update the editors // for any other properties that have changed. for (int i = 0; i < m_Properties.length; i++) { Object o; try { Method getter = m_Properties[i].getReadMethod(); Method setter = m_Properties[i].getWriteMethod(); if (getter == null || setter == null) { // ignore set/get only properties continue; } Object args[] = { }; o = getter.invoke(m_Target, args); } catch (Exception ex) { o = null; } if (o == m_Values[i] || (o != null && o.equals(m_Values[i]))) { // The property is equal to its old value. continue; } m_Values[i] = o; // Make sure we have an editor for this property... if (m_Editors[i] == null) { continue; } // The property has changed! Update the editor. m_Editors[i].removePropertyChangeListener(this); m_Editors[i].setValue(o); m_Editors[i].addPropertyChangeListener(this); if (m_Views[i] != null) { //System.err.println("Trying to repaint " + (i + 1)); m_Views[i].repaint(); } } // Make sure the target bean gets repainted. if (Beans.isInstanceOf(m_Target, Component.class)) { ((Component)(Beans.getInstanceOf(m_Target, Component.class))).repaint(); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -