genericobjecteditor.java
来自「Java 编写的多种数据挖掘算法 包括聚类、分类、预处理等」· Java 代码 · 共 1,764 行 · 第 1/4 页
JAVA
1,764 行
if (value != null) m_Capabilities.assign(value); else m_Capabilities = new Capabilities(null); updateList(); } /** * returns the currently selected capabilities * * @return the currently selected capabilities */ public Capabilities getCapabilities() { return m_Capabilities; } /** * sets the JPopupMenu to display again after closing the dialog * * @param value the JPopupMenu to display again */ public void setPopup(JPopupMenu value) { m_Popup = value; } /** * returns the currently set JPopupMenu * * @return the current JPopupMenu */ public JPopupMenu getPopup() { return m_Popup; } /** * if a JPopupMenu is set, it is displayed again. Displaying this dialog * closes any JPopupMenu automatically. */ public void showPopup() { if (getPopup() != null) getPopup().setVisible(true); } } /** * Creates a popup menu containing a tree that is aware * of the screen dimensions. */ public class JTreePopupMenu extends JPopupMenu { /** for serialization */ static final long serialVersionUID = -3404546329655057387L; /** the popup itself */ private JPopupMenu m_Self; /** The tree */ private JTree m_tree; /** The scroller */ private JScrollPane m_scroller; /** The filter button in case of CapabilitiesHandlers */ private JButton m_FilterButton = new JButton("Filter..."); /** The remove filter button in case of CapabilitiesHandlers */ private JButton m_RemoveFilterButton = new JButton("Remove filter"); /** The button for closing the popup again */ private JButton m_CloseButton = new JButton("Close"); /** * Constructs a new popup menu. * * @param tree the tree to put in the menu */ public JTreePopupMenu(JTree tree) { m_Self = this; setLayout(new BorderLayout()); JPanel panel = new JPanel(new FlowLayout(FlowLayout.RIGHT)); add(panel, BorderLayout.SOUTH); if (ClassDiscovery.hasInterface(CapabilitiesHandler.class, m_ClassType)) { // filter m_FilterButton.setMnemonic('F'); m_FilterButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (e.getSource() == m_FilterButton) { CapabilitiesFilterDialog dialog = new CapabilitiesFilterDialog(); dialog.setCapabilities(m_CapabilitiesFilter); dialog.setPopup(m_Self); dialog.setVisible(true); repaint(); } } }); panel.add(m_FilterButton); // remove m_RemoveFilterButton.setMnemonic('R'); m_RemoveFilterButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (e.getSource() == m_RemoveFilterButton) { m_CapabilitiesFilter = null; repaint(); } } }); panel.add(m_RemoveFilterButton); } // close m_CloseButton.setMnemonic('C'); m_CloseButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (e.getSource() == m_CloseButton) { m_Self.setVisible(false); } } }); panel.add(m_CloseButton); m_tree = tree; JPanel treeView = new JPanel(); treeView.setLayout(new BorderLayout()); treeView.add(m_tree, BorderLayout.NORTH); // make backgrounds look the same treeView.setBackground(m_tree.getBackground()); m_scroller = new JScrollPane(treeView); m_scroller.setPreferredSize(new Dimension(300, 400)); m_scroller.getVerticalScrollBar().setUnitIncrement(20); add(m_scroller); } /** * Displays the menu, making sure it will fit on the screen. * * @param invoker the component thast invoked the menu * @param x the x location of the popup * @param y the y location of the popup */ public void show(Component invoker, int x, int y) { super.show(invoker, x, y); // calculate available screen area for popup java.awt.Point location = getLocationOnScreen(); java.awt.Dimension screenSize = getToolkit().getScreenSize(); int maxWidth = (int) (screenSize.getWidth() - location.getX()); int maxHeight = (int) (screenSize.getHeight() - location.getY()); // if the part of the popup goes off the screen then resize it Dimension scrollerSize = m_scroller.getPreferredSize(); int height = (int) scrollerSize.getHeight(); int width = (int) scrollerSize.getWidth(); if (width > maxWidth) width = maxWidth; if (height > maxHeight) height = maxHeight; // commit any size changes m_scroller.setPreferredSize(new Dimension(width, height)); revalidate(); pack(); } } /** * Handles the GUI side of editing values. */ public class GOEPanel extends JPanel { /** for serialization */ static final long serialVersionUID = 3656028520876011335L; /** The component that performs classifier customization */ protected PropertySheetPanel m_ChildPropertySheet; /** The name of the current class */ protected JLabel m_ClassNameLabel; /** Open object from disk */ protected JButton m_OpenBut; /** Save object to disk */ protected JButton m_SaveBut; /** ok button */ protected JButton m_okBut; /** cancel button */ protected JButton m_cancelBut; /** The filechooser for opening and saving object files */ protected JFileChooser m_FileChooser; /** Creates the GUI editor component */ public GOEPanel() { m_Backup = copyObject(m_Object); m_ClassNameLabel = new JLabel("None"); m_ClassNameLabel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5)); m_ChildPropertySheet = new PropertySheetPanel(); m_ChildPropertySheet.addPropertyChangeListener (new PropertyChangeListener() { public void propertyChange(PropertyChangeEvent evt) { m_Support.firePropertyChange("", null, null); } }); m_OpenBut = new JButton("Open..."); m_OpenBut.setToolTipText("Load a configured object"); m_OpenBut.setEnabled(true); m_OpenBut.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Object object = openObject(); if (object != null) { // setValue takes care of: Making sure obj is of right type, // and firing property change. setValue(object); // Need a second setValue to get property values filled in OK. // Not sure why. setValue(object); } } }); m_SaveBut = new JButton("Save..."); m_SaveBut.setToolTipText("Save the current configured object"); m_SaveBut.setEnabled(true); m_SaveBut.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { saveObject(m_Object); } }); m_okBut = new JButton("OK"); m_okBut.setEnabled(true); m_okBut.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { m_Backup = copyObject(m_Object); if ((getTopLevelAncestor() != null) && (getTopLevelAncestor() instanceof Window)) { Window w = (Window) getTopLevelAncestor(); w.dispose(); } } }); m_cancelBut = new JButton("Cancel"); m_cancelBut.setEnabled(true); m_cancelBut.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (m_Backup != null) { m_Object = copyObject(m_Backup); // To fire property change m_Support.firePropertyChange("", null, null); m_ObjectNames = getClassesFromProperties(); updateObjectNames(); updateChildPropertySheet(); } if ((getTopLevelAncestor() != null) && (getTopLevelAncestor() instanceof Window)) { Window w = (Window) getTopLevelAncestor(); w.dispose(); } } }); setLayout(new BorderLayout()); if (m_canChangeClassInDialog) { JButton chooseButton = createChooseClassButton(); JPanel top = new JPanel(); top.setLayout(new BorderLayout()); top.setBorder(BorderFactory.createEmptyBorder(5,5,5,5)); top.add(chooseButton, BorderLayout.WEST); top.add(m_ClassNameLabel, BorderLayout.CENTER); add(top, BorderLayout.NORTH); } else { add(m_ClassNameLabel, BorderLayout.NORTH); } add(m_ChildPropertySheet, BorderLayout.CENTER); // Since we resize to the size of the property sheet, a scrollpane isn't // typically needed // add(new JScrollPane(m_ChildPropertySheet), BorderLayout.CENTER); JPanel okcButs = new JPanel(); okcButs.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); okcButs.setLayout(new GridLayout(1, 4, 5, 5)); okcButs.add(m_OpenBut); okcButs.add(m_SaveBut); okcButs.add(m_okBut); okcButs.add(m_cancelBut); add(okcButs, BorderLayout.SOUTH); if (m_ClassType != null) { m_ObjectNames = getClassesFromProperties(); if (m_Object != null) { updateObjectNames(); updateChildPropertySheet(); } } } /** * Enables/disables the cancel button. * * @param flag true to enable cancel button, false * to disable it */ protected void setCancelButton(boolean flag) { if(m_cancelBut != null) m_cancelBut.setEnabled(flag); } /** * Opens an object from a file selected by the user. * * @return the loaded object, or null if the operation was cancelled */ protected Object openObject() { if (m_FileChooser == null) { createFileChooser(); } int returnVal = m_FileChooser.showOpenDialog(this); if (returnVal == JFileChooser.APPROVE_OPTION) { File selected = m_FileChooser.getSelectedFile(); try { ObjectInputStream oi = new ObjectInputStream(new BufferedInputStream(new FileInputStream(selected))); Object obj = oi.readObject(); oi.close(); if (!m_ClassType.isAssignableFrom(obj.getClass())) { throw new Exception("Object not of type: " + m_ClassType.getName()); } return obj; } catch (Exception ex) { JOptionPane.showMessageDialog(this, "Couldn't read object: " + selected.getName() + "\n" + ex.getMessage(), "Open object file", JOptionPane.ERROR_MESSAGE); } } return null; } /** * Saves an object to a file selected by the user. * * @param object the object to save */ protected void saveObject(Object object) { if (m_FileChooser == null) { createFileChooser(); } int returnVal = m_FileChooser.showSaveDialog(this); if (returnVal == JFileChooser.APPROVE_OPTION) { File sFile = m_FileChooser.getSelectedFile(); try { ObjectOutputStream oo = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(sFile))); oo.writeObject(object); oo.close(); } catch (Exception ex) { JOptionPane.showMessageDialog(this, "Couldn't write to file: " + sFile.getName() + "\n" + ex.getMessage(), "Save object", JOptionPane.ERROR_MESSAGE); } } } /** * Creates the file chooser the user will use to save/load files with. */ protected void createFileChooser() { m_FileChooser = new JFileChooser(new File(System.getProperty("user.dir"))); m_FileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); } /** * Makes a copy of an object using serialization * @param source the object to copy * @return a copy of the source object */ protected Object copyObject(Object source) { Object result = null; try { SerializedObject so = new SerializedObject(source); result = so.getObject(); setCancelButton(true); } catch (Exception ex) { setCancelButton(false); System.err.println("GenericObjectEditor: Problem making backup object"); System.err.println(ex); } return result; } /** * Allows customization of the action label on the dialog. * @param newLabel the new string for the ok button */ public void setOkButtonText(String newLabel) { m_okBut.setText(newLabel); } /** * This is used to hook an action listener to the ok button * @param a The action listener. */ public void addOkListener(ActionListener a) { m_okBut.addActionListener(a); } /**
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?