📄 algorithmlistpanel.java
字号:
repaint(); } }); ((GenericObjectEditor.GOEPanel) m_ClassifierEditor.getCustomEditor()).addOkListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Classifier newCopy = (Classifier) copyObject(m_ClassifierEditor.getValue()); addNewAlgorithm(newCopy); } }); m_DeleteBut.setEnabled(false); m_DeleteBut.addActionListener(this); m_AddBut.setEnabled(false); m_AddBut.addActionListener(this); m_EditBut.setEnabled(false); m_EditBut.addActionListener(this); m_LoadOptionsBut.setEnabled(false); m_LoadOptionsBut.addActionListener(this); m_SaveOptionsBut.setEnabled(false); m_SaveOptionsBut.addActionListener(this); m_UpBut.setEnabled(false); m_UpBut.addActionListener(this); m_DownBut.setEnabled(false); m_DownBut.addActionListener(this); m_List.addListSelectionListener(new ListSelectionListener() { public void valueChanged(ListSelectionEvent e) { setButtons(e); } }); m_FileChooser.addChoosableFileFilter(m_XMLFilter); m_FileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); setLayout(new BorderLayout()); setBorder(BorderFactory.createTitledBorder("Algorithms")); JPanel topLab = new JPanel(); GridBagLayout gb = new GridBagLayout(); GridBagConstraints constraints = new GridBagConstraints(); topLab.setBorder(BorderFactory.createEmptyBorder(10, 5, 10, 5)); topLab.setLayout(gb); constraints.gridx=0;constraints.gridy=0;constraints.weightx=5; constraints.fill = GridBagConstraints.HORIZONTAL; constraints.gridwidth=1;constraints.gridheight=1; constraints.insets = new Insets(0,2,0,2); topLab.add(m_AddBut,constraints); constraints.gridx=1;constraints.gridy=0;constraints.weightx=5; constraints.gridwidth=1;constraints.gridheight=1; topLab.add(m_EditBut,constraints); constraints.gridx=2;constraints.gridy=0;constraints.weightx=5; constraints.gridwidth=1;constraints.gridheight=1; topLab.add(m_DeleteBut,constraints); JPanel bottomLab = new JPanel(); gb = new GridBagLayout(); constraints = new GridBagConstraints(); bottomLab.setBorder(BorderFactory.createEmptyBorder(10, 5, 10, 5)); bottomLab.setLayout(gb); constraints.gridx=0;constraints.gridy=0;constraints.weightx=5; constraints.fill = GridBagConstraints.HORIZONTAL; constraints.gridwidth=1;constraints.gridheight=1; constraints.insets = new Insets(0,2,0,2); bottomLab.add(m_LoadOptionsBut,constraints); constraints.gridx=1;constraints.gridy=0;constraints.weightx=5; constraints.gridwidth=1;constraints.gridheight=1; bottomLab.add(m_SaveOptionsBut,constraints); constraints.gridx=2;constraints.gridy=0;constraints.weightx=5; constraints.gridwidth=1;constraints.gridheight=1; bottomLab.add(m_UpBut,constraints); constraints.gridx=3;constraints.gridy=0;constraints.weightx=5; constraints.gridwidth=1;constraints.gridheight=1; bottomLab.add(m_DownBut,constraints); add(topLab, BorderLayout.NORTH); add(new JScrollPane(m_List), BorderLayout.CENTER); add(bottomLab, BorderLayout.SOUTH); } /** * Tells the panel to act on a new experiment. * * @param exp a value of type 'Experiment' */ public void setExperiment(Experiment exp) { m_Exp = exp; m_AddBut.setEnabled(true); m_List.setModel(m_AlgorithmListModel); m_List.setCellRenderer(new ObjectCellRenderer()); m_AlgorithmListModel.removeAllElements(); if (m_Exp.getPropertyArray() instanceof Classifier[]) { Classifier[] algorithms = (Classifier[]) m_Exp.getPropertyArray(); for (int i=0; i<algorithms.length; i++) { m_AlgorithmListModel.addElement(algorithms[i]); } } m_EditBut.setEnabled((m_AlgorithmListModel.size() > 0)); m_DeleteBut.setEnabled((m_AlgorithmListModel.size() > 0)); m_LoadOptionsBut.setEnabled((m_AlgorithmListModel.size() > 0)); m_SaveOptionsBut.setEnabled((m_AlgorithmListModel.size() > 0)); m_UpBut.setEnabled(JListHelper.canMoveUp(m_List)); m_DownBut.setEnabled(JListHelper.canMoveDown(m_List)); } /** * Add a new algorithm to the list. * * @param newScheme the new scheme to add */ private void addNewAlgorithm(Classifier newScheme) { if (!m_Editing) m_AlgorithmListModel.addElement(newScheme); else m_AlgorithmListModel.setElementAt(newScheme, m_List.getSelectedIndex()); Classifier[] cArray = new Classifier[m_AlgorithmListModel.size()]; for (int i=0; i<cArray.length; i++) { cArray[i] = (Classifier) m_AlgorithmListModel.elementAt(i); } m_Exp.setPropertyArray(cArray); m_Editing = false; } /** * sets the state of the buttons according to the selection state of the * JList * * @param e the event */ private void setButtons(ListSelectionEvent e) { if (e.getSource() == m_List) { m_DeleteBut.setEnabled(m_List.getSelectedIndex() > -1); m_AddBut.setEnabled(true); m_EditBut.setEnabled(m_List.getSelectedIndices().length == 1); m_LoadOptionsBut.setEnabled(m_List.getSelectedIndices().length == 1); m_SaveOptionsBut.setEnabled(m_List.getSelectedIndices().length == 1); m_UpBut.setEnabled(JListHelper.canMoveUp(m_List)); m_DownBut.setEnabled(JListHelper.canMoveDown(m_List)); } } /** * Handle actions when buttons get pressed. * * @param e a value of type 'ActionEvent' */ public void actionPerformed(ActionEvent e) { if (e.getSource() == m_AddBut) { m_Editing = false; if (m_PD == null) { int x = getLocationOnScreen().x; int y = getLocationOnScreen().y; m_PD = new PropertyDialog(m_ClassifierEditor, x, y); } else { m_PD.setVisible(true); } } else if (e.getSource() == m_EditBut) { if (m_List.getSelectedValue() != null) { m_Editing = true; if (m_PD == null) { int x = getLocationOnScreen().x; int y = getLocationOnScreen().y; m_PD = new PropertyDialog(m_ClassifierEditor, x, y); } else { m_PD.setVisible(true); } m_PD.getEditor().setValue(m_List.getSelectedValue()); } } else if (e.getSource() == m_DeleteBut) { int [] selected = m_List.getSelectedIndices(); if (selected != null) { for (int i = selected.length - 1; i >= 0; i--) { int current = selected[i]; m_AlgorithmListModel.removeElementAt(current); if (m_Exp.getDatasets().size() > current) { m_List.setSelectedIndex(current); } else { m_List.setSelectedIndex(current - 1); } } } if (m_List.getSelectedIndex() == -1) { m_EditBut.setEnabled(false); m_DeleteBut.setEnabled(false); m_LoadOptionsBut.setEnabled(false); m_SaveOptionsBut.setEnabled(false); m_UpBut.setEnabled(false); m_DownBut.setEnabled(false); } Classifier[] cArray = new Classifier[m_AlgorithmListModel.size()]; for (int i=0; i<cArray.length; i++) { cArray[i] = (Classifier) m_AlgorithmListModel.elementAt(i); } m_Exp.setPropertyArray(cArray); } else if (e.getSource() == m_LoadOptionsBut) { if (m_List.getSelectedValue() != null) { int returnVal = m_FileChooser.showOpenDialog(this); if (returnVal == JFileChooser.APPROVE_OPTION) { try { File file = m_FileChooser.getSelectedFile(); if (!file.getAbsolutePath().toLowerCase().endsWith(".xml")) file = new File(file.getAbsolutePath() + ".xml"); XMLClassifier xmlcls = new XMLClassifier(); Classifier c = (Classifier) xmlcls.read(file); m_AlgorithmListModel.setElementAt(c, m_List.getSelectedIndex()); } catch (Exception ex) { ex.printStackTrace(); } } } } else if (e.getSource() == m_SaveOptionsBut) { if (m_List.getSelectedValue() != null) { int returnVal = m_FileChooser.showSaveDialog(this); if (returnVal == JFileChooser.APPROVE_OPTION) { try { File file = m_FileChooser.getSelectedFile(); if (!file.getAbsolutePath().toLowerCase().endsWith(".xml")) file = new File(file.getAbsolutePath() + ".xml"); XMLClassifier xmlcls = new XMLClassifier(); xmlcls.write(file, m_List.getSelectedValue()); } catch (Exception ex) { ex.printStackTrace(); } } } } else if (e.getSource() == m_UpBut) { JListHelper.moveUp(m_List); Classifier[] cArray = new Classifier[m_AlgorithmListModel.size()]; for (int i=0; i<cArray.length; i++) { cArray[i] = (Classifier) m_AlgorithmListModel.elementAt(i); } m_Exp.setPropertyArray(cArray); } else if (e.getSource() == m_DownBut) { JListHelper.moveDown(m_List); Classifier[] cArray = new Classifier[m_AlgorithmListModel.size()]; for (int i=0; i<cArray.length; i++) { cArray[i] = (Classifier) m_AlgorithmListModel.elementAt(i); } m_Exp.setPropertyArray(cArray); } } /** * 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(); } catch (Exception ex) { System.err.println("AlgorithmListPanel: Problem copying object"); System.err.println(ex); } return result; } /** * Tests out the algorithm list panel from the command line. * * @param args ignored */ public static void main(String [] args) { try { final JFrame jf = new JFrame("Algorithm List Editor"); jf.getContentPane().setLayout(new BorderLayout()); AlgorithmListPanel dp = new AlgorithmListPanel(); jf.getContentPane().add(dp, BorderLayout.CENTER); jf.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { jf.dispose(); System.exit(0); } }); jf.pack(); jf.setVisible(true); System.err.println("Short nap"); Thread.currentThread().sleep(3000); System.err.println("Done"); dp.setExperiment(new Experiment()); } catch (Exception ex) { ex.printStackTrace(); System.err.println(ex.getMessage()); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -