📄 genericarrayeditor.java
字号:
} public Dimension getPreferredSize() { Font f = this.getFont(); FontMetrics fm = this.getFontMetrics(f); return new Dimension(0, fm.getHeight()); } }; } catch (Exception ex) { return null; } } } /** * Updates the type of object being edited, so attempts to find an * appropriate propertyeditor. * * @param o a value of type 'Object' */ private void updateEditorType(Object o) { // Determine if the current object is an array m_ElementEditor = null; m_ListModel = null; removeAll(); if ((o != null) && (o.getClass().isArray())) { Class elementClass = o.getClass().getComponentType(); PropertyEditor editor = PropertyEditorManager.findEditor(elementClass); Component view = null; ListCellRenderer lcr = new DefaultListCellRenderer(); if (editor != null) { if (editor instanceof GenericObjectEditor) { ((GenericObjectEditor) editor).setClassType(elementClass); } //setting the value in the editor so that //we don't get a NullPointerException //when we do getAsText() in the constructor of //PropertyValueSelector() if(Array.getLength(o) > 0) { editor.setValue(Array.get(o,0)); } else { if (editor instanceof GenericObjectEditor) { ((GenericObjectEditor)editor).setDefaultValue(); } else { try { editor.setValue(elementClass.newInstance()); } catch(Exception ex) { m_ElementEditor=null; System.err.println(ex.getMessage()); add(m_Label, BorderLayout.CENTER); m_Support.firePropertyChange("", null, null); validate(); return; } } } if (editor.isPaintable() && editor.supportsCustomEditor()) { view = new PropertyPanel(editor); lcr = new EditorListCellRenderer(editor.getClass(), elementClass); } else if (editor.getTags() != null) { view = new PropertyValueSelector(editor); } else if (editor.getAsText() != null) { view = new PropertyText(editor); } } if (view == null) { System.err.println("No property editor for class: " + elementClass.getName()); } else { m_ElementEditor = editor; // Create the ListModel and populate it m_ListModel = new DefaultListModel(); m_ElementClass = elementClass; for (int i = 0; i < Array.getLength(o); i++) { m_ListModel.addElement(Array.get(o,i)); } m_ElementList.setCellRenderer(lcr); m_ElementList.setModel(m_ListModel); if (m_ListModel.getSize() > 0) { m_ElementList.setSelectedIndex(0); } else { m_DeleteBut.setEnabled(false); m_EditBut.setEnabled(false); } m_UpBut.setEnabled(JListHelper.canMoveDown(m_ElementList)); m_DownBut.setEnabled(JListHelper.canMoveDown(m_ElementList)); //have already set the value above in the editor //try { //if (m_ListModel.getSize() > 0) { // m_ElementEditor.setValue(m_ListModel.getElementAt(0)); //} else { // if (m_ElementEditor instanceof GenericObjectEditor) { // ((GenericObjectEditor)m_ElementEditor).setDefaultValue(); // } else { // m_ElementEditor.setValue(m_ElementClass.newInstance()); // } //} JPanel panel = new JPanel(); panel.setLayout(new BorderLayout()); panel.add(view, BorderLayout.CENTER); panel.add(m_AddBut, BorderLayout.EAST); add(panel, BorderLayout.NORTH); add(new JScrollPane(m_ElementList), BorderLayout.CENTER); JPanel panel2 = new JPanel(); panel2.setLayout(new GridLayout(1, 4)); panel2.add(m_DeleteBut); panel2.add(m_EditBut); panel2.add(m_UpBut); panel2.add(m_DownBut); add(panel2, BorderLayout.SOUTH); m_ElementEditor .addPropertyChangeListener(new PropertyChangeListener() { public void propertyChange(PropertyChangeEvent e) { repaint(); } }); //} catch (Exception ex) { // System.err.println(ex.getMessage()); // m_ElementEditor = null; //} } } if (m_ElementEditor == null) { add(m_Label, BorderLayout.CENTER); } m_Support.firePropertyChange("", null, null); validate(); } /** * Sets the current object array. * * @param o an object that must be an array. */ public void setValue(Object o) { // Create a new list model, put it in the list and resize? updateEditorType(o); } /** * Gets the current object array. * * @return the current object array */ public Object getValue() { if (m_ListModel == null) { return null; } // Convert the listmodel to an array of strings and return it. int length = m_ListModel.getSize(); Object result = Array.newInstance(m_ElementClass, length); for (int i = 0; i < length; i++) { Array.set(result, i, m_ListModel.elementAt(i)); } return result; } /** * Supposedly returns an initialization string to create a classifier * identical to the current one, including it's state, but this doesn't * appear possible given that the initialization string isn't supposed to * contain multiple statements. * * @return the java source code initialisation string */ public String getJavaInitializationString() { return "null"; } /** * Returns true to indicate that we can paint a representation of the * string array * * @return true */ public boolean isPaintable() { return true; } /** * Paints a representation of the current classifier. * * @param gfx the graphics context to use * @param box the area we are allowed to paint into */ public void paintValue(java.awt.Graphics gfx, java.awt.Rectangle box) { FontMetrics fm = gfx.getFontMetrics(); int vpad = (box.height - fm.getAscent()) / 2; String rep = m_ListModel.getSize() + " " + m_ElementClass.getName(); gfx.drawString(rep, 2, fm.getHeight() + vpad); } /** * Returns null as we don't support getting/setting values as text. * * @return null */ public String getAsText() { return null; } /** * Returns null as we don't support getting/setting values as text. * * @param text the text value * @exception IllegalArgumentException as we don't support * getting/setting values as text. */ public void setAsText(String text) { throw new IllegalArgumentException(text); } /** * Returns null as we don't support getting values as tags. * * @return null */ public String[] getTags() { return null; } /** * Returns true because we do support a custom editor. * * @return true */ public boolean supportsCustomEditor() { return true; } /** * Returns the array editing component. * * @return a value of type 'java.awt.Component' */ public java.awt.Component getCustomEditor() { return this; } /** * Adds a PropertyChangeListener who will be notified of value changes. * * @param l a value of type 'PropertyChangeListener' */ public void addPropertyChangeListener(PropertyChangeListener l) { m_Support.addPropertyChangeListener(l); } /** * Removes a PropertyChangeListener. * * @param l a value of type 'PropertyChangeListener' */ public void removePropertyChangeListener(PropertyChangeListener l) { m_Support.removePropertyChangeListener(l); } /** * Tests out the array editor from the command line. * * @param args ignored */ public static void main(String [] args) { try { GenericObjectEditor.registerEditors(); final GenericArrayEditor ce = new GenericArrayEditor(); final weka.filters.Filter [] initial = new weka.filters.Filter [0]; /* { new weka.filters.AddFilter() };*/ /* final String [] initial = { "Hello", "There", "Bob" };*/ PropertyDialog pd = new PropertyDialog(ce, 100, 100); pd.setSize(200,200); pd.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); ce.setValue(initial); //ce.validate(); } catch (Exception ex) { ex.printStackTrace(); System.err.println(ex.getMessage()); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -