📄 genericarrayeditor.java
字号:
/* * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *//* * GenericArrayEditor.java * Copyright (C) 1999 Len Trigg * */package weka.gui;import weka.core.SelectedTag;import weka.core.SerializedObject;import weka.classifiers.Classifier;import java.lang.reflect.Array;import java.beans.PropertyChangeEvent;import java.beans.PropertyChangeSupport;import java.beans.PropertyChangeListener;import java.beans.PropertyEditor;import java.beans.PropertyEditorManager;import java.awt.Graphics;import java.awt.Insets;import java.awt.Rectangle;import java.awt.Font;import java.awt.Component;import java.awt.Dimension;import java.awt.BorderLayout;import java.awt.GridLayout;import java.awt.FontMetrics;import java.awt.FlowLayout;import java.awt.event.ItemListener;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.ItemEvent;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import java.awt.event.MouseAdapter;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import javax.swing.JLabel;import javax.swing.JButton;import javax.swing.ListCellRenderer;import javax.swing.DefaultListCellRenderer;import javax.swing.JPanel;import javax.swing.JList;import javax.swing.ListModel;import javax.swing.DefaultListModel;import javax.swing.JScrollPane;import javax.swing.SwingConstants;import javax.swing.event.ListSelectionListener;import javax.swing.event.ListSelectionEvent;import javax.swing.JOptionPane;/** * A PropertyEditor for arrays of objects that themselves have * property editors. * * @author Len Trigg (trigg@cs.waikato.ac.nz) * @version $Revision: 1.16 $ */public class GenericArrayEditor extends JPanel implements PropertyEditor { /** Handles property change notification */ private PropertyChangeSupport m_Support = new PropertyChangeSupport(this); /** The label for when we can't edit that type */ private JLabel m_Label = new JLabel("Can't edit", SwingConstants.CENTER); /** The list component displaying current values */ private JList m_ElementList = new JList(); /** The class of objects allowed in the array */ private Class m_ElementClass = String.class; /** The defaultlistmodel holding our data */ private DefaultListModel m_ListModel; /** The property editor for the class we are editing */ private PropertyEditor m_ElementEditor; /** Click this to delete the selected array values */ private JButton m_DeleteBut = new JButton("Delete"); /** Click this to edit the selected array value */ private JButton m_EditBut = new JButton("Edit"); /** Click this to move the selected array value(s) one up */ private JButton m_UpBut = new JButton("Up"); /** Click this to move the selected array value(s) one down */ private JButton m_DownBut = new JButton("Down"); /** Click to add the current object configuration to the array */ private JButton m_AddBut = new JButton("Add"); /** The property editor for editing existing elements */ private PropertyEditor m_Editor = new GenericObjectEditor(); /** The currently displayed property dialog, if any */ private PropertyDialog m_PD; /** Listens to buttons being pressed and taking the appropriate action */ private ActionListener m_InnerActionListener = new ActionListener() { public void actionPerformed(ActionEvent e) { if (e.getSource() == m_DeleteBut) { int [] selected = m_ElementList.getSelectedIndices(); if (selected != null) { for (int i = 0; i < selected.length; i++) { int current = selected[i]; m_ListModel.removeElementAt(current); if (m_ListModel.size() > current) { m_ElementList.setSelectedIndex(current); } } m_Support.firePropertyChange("", null, null); } } else if (e.getSource() == m_EditBut) { ((GenericObjectEditor) m_Editor).setClassType(m_ElementClass); m_Editor.setValue(m_ElementList.getSelectedValue()); if (m_Editor.getValue() != null) { if (m_PD == null) { int x = getLocationOnScreen().x; int y = getLocationOnScreen().y; m_PD = new PropertyDialog(m_Editor, x, y); } else { m_PD.setVisible(true); } m_ElementList.setSelectedValue(m_Editor.getValue(), false); m_Support.firePropertyChange("", null, null); } } else if (e.getSource() == m_UpBut) { JListHelper.moveUp(m_ElementList); m_Support.firePropertyChange("", null, null); } else if (e.getSource() == m_DownBut) { JListHelper.moveDown(m_ElementList); m_Support.firePropertyChange("", null, null); } else if (e.getSource() == m_AddBut) { int selected = m_ElementList.getSelectedIndex(); Object addObj = m_ElementEditor.getValue(); // Make a full copy of the object using serialization try { SerializedObject so = new SerializedObject(addObj); addObj = so.getObject(); if (selected != -1) { m_ListModel.insertElementAt(addObj, selected); } else { m_ListModel.addElement(addObj); } m_Support.firePropertyChange("", null, null); } catch (Exception ex) { JOptionPane.showMessageDialog(GenericArrayEditor.this, "Could not create an object copy", null, JOptionPane.ERROR_MESSAGE); } } } }; /** Listens to list items being selected and takes appropriate action */ private ListSelectionListener m_InnerSelectionListener = new ListSelectionListener() { public void valueChanged(ListSelectionEvent e) { if (e.getSource() == m_ElementList) { // Enable the delete/edit button if (m_ElementList.getSelectedIndex() != -1) { m_DeleteBut.setEnabled(true); m_EditBut.setEnabled(m_ElementList.getSelectedIndices().length == 1); m_UpBut.setEnabled(JListHelper.canMoveUp(m_ElementList)); m_DownBut.setEnabled(JListHelper.canMoveDown(m_ElementList)); } // disable delete/edit button else { m_DeleteBut.setEnabled(false); m_EditBut.setEnabled(false); m_UpBut.setEnabled(false); m_DownBut.setEnabled(false); } } } }; /** Listens to mouse events and takes appropriate action */ private MouseListener m_InnerMouseListener = new MouseAdapter() { public void mouseClicked(MouseEvent e) { if (e.getSource() == m_ElementList) { if (e.getClickCount() == 2) { // unfortunately, locationToIndex only returns the nearest entry // and not the exact one, i.e. if there's one item in the list and // one doublelclicks somewhere in the list, this index will be // returned int index = m_ElementList.locationToIndex(e.getPoint()); if (index > -1) m_InnerActionListener.actionPerformed( new ActionEvent(m_EditBut, 0, "")); } } } }; /** * Sets up the array editor. */ public GenericArrayEditor() { setLayout(new BorderLayout()); add(m_Label, BorderLayout.CENTER); m_DeleteBut.addActionListener(m_InnerActionListener); m_EditBut.addActionListener(m_InnerActionListener); m_UpBut.addActionListener(m_InnerActionListener); m_DownBut.addActionListener(m_InnerActionListener); m_AddBut.addActionListener(m_InnerActionListener); m_ElementList.addListSelectionListener(m_InnerSelectionListener); m_ElementList.addMouseListener(m_InnerMouseListener); m_AddBut.setToolTipText("Add the current item to the list"); m_DeleteBut.setToolTipText("Delete the selected list item"); m_EditBut.setToolTipText("Edit the selected list item"); m_UpBut.setToolTipText("Move the selected item(s) one up"); m_DownBut.setToolTipText("Move the selected item(s) one down"); } /** This class handles the creation of list cell renderers from the * property editors. */ private class EditorListCellRenderer implements ListCellRenderer { /** The class of the property editor for array objects */ private Class m_EditorClass; /** The class of the array values */ private Class m_ValueClass; /** * Creates the list cell renderer. * * @param editorClass The class of the property editor for array objects * @param valueClass The class of the array values */ public EditorListCellRenderer(Class editorClass, Class valueClass) { m_EditorClass = editorClass; m_ValueClass = valueClass; } /** * Creates a cell rendering component. * * @param JList the list that will be rendered in * @param Object the cell value * @param int which element of the list to render * @param boolean true if the cell is selected * @param boolean true if the cell has the focus * @return the rendering component */ public Component getListCellRendererComponent(final JList list, final Object value, final int index, final boolean isSelected, final boolean cellHasFocus) { try { final PropertyEditor e = (PropertyEditor)m_EditorClass.newInstance(); if (e instanceof GenericObjectEditor) { // ((GenericObjectEditor) e).setDisplayOnly(true); ((GenericObjectEditor) e).setClassType(m_ValueClass); } e.setValue(value); return new JPanel() { public void paintComponent(Graphics g) { Insets i = this.getInsets(); Rectangle box = new Rectangle(i.left, i.top, this.getWidth() - i.right, this.getHeight() - i.bottom ); g.setColor(isSelected ? list.getSelectionBackground() : list.getBackground()); g.fillRect(0, 0, this.getWidth(), this.getHeight()); g.setColor(isSelected ? list.getSelectionForeground() : list.getForeground()); e.paintValue(g, box);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -