package com.platformda.optimize;
import java.awt.BorderLayout;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JToolBar;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;
import org.openide.DialogDescriptor;
import org.openide.DialogDisplayer;
import org.openide.NotifyDescriptor;
/**
*
* @author Administrator
*/
public class OptDurationManager {
public OptDurationManager() {
}
OptDurationManager mOptDurationManager;
JButton addButton = new JButton();
JButton delButton = new JButton();
JButton editButton = new JButton();
ImageIcon addIcon = new ImageIcon();
ImageIcon delIcon = new ImageIcon();
ImageIcon editIcon = new ImageIcon();
OptAlgorithmManager manager = new OptAlgorithmManager();
List algorithms = OptAlgorithmManager.getAllAlgorithms();
List addAlgorithms = new ArrayList();
String[] columnName = {"AlgorithmName", "Duration"};
String[][] rowData;
String[] addRowData;
DefaultTableModel model = new DefaultTableModel();
JTable table = new JTable();
JScrollPane scrollPane = new JScrollPane();
DefaultListModel listModel;
JList optAlgorithmList;
JScrollPane AddAlgorithmScroll;
JPanel tableJPanel = new JPanel();
JPanel headerJPanel = new JPanel();
JPanel addJPanel = new JPanel();
JPanel addCenterJPanel = new JPanel();
JPanel editJPanel = new JPanel();
Point mousePoint;
String AlgorithmName;
int delRow;
static int addRow;
static OptSettingEditor settingEditor;
static OptSetting setting;
DialogDescriptor AddDescriptor;
DialogDescriptor EditDescriptor;
public void DurationManager() {
JFrame mJFrame = new JFrame();
mJFrame.setLayout(new BorderLayout());
mJFrame.add(initTableJScrollPane(), BorderLayout.CENTER);
mJFrame.add(tableHeader(), BorderLayout.NORTH);
mJFrame.setBounds(300, 300, 300, 100);
mJFrame.setTitle("OptDurationManager");
mJFrame.pack();
mJFrame.setVisible(true);
mJFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public JPanel initTableJScrollPane() {
int i = 0;
rowData = new String[algorithms.size()][2];
for (OptAlgorithm mAlgorithm : algorithms) {
rowData[i][0] = mAlgorithm.getName();
rowData[i][1] = String.valueOf(i++);
}
model = new DefaultTableModel(rowData, columnName);
table = new JTable(model);
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
table.addMouseListener(new MouseAdapter() {
@Override
public void mouseReleased(MouseEvent e) {
mousePoint = e.getPoint();
AlgorithmName = table.getValueAt(table.rowAtPoint(mousePoint), 0).toString();
System.out.println("(TableRow) " + table.rowAtPoint(mousePoint) + " : " + AlgorithmName);
}
});
scrollPane = new JScrollPane(table);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
tableJPanel.setLayout(new BorderLayout());
tableJPanel.add(scrollPane, BorderLayout.NORTH);
return tableJPanel;
}
public JPanel tableHeader() {
headerJPanel.setLayout(new BorderLayout());
addIcon = new ImageIcon("Picture/add.png");
addButton.setIcon(addIcon);
addButton.setToolTipText("add");
delIcon = new ImageIcon("Picture/remove.png");
delButton.setIcon(delIcon);
delButton.setToolTipText("remove");
editIcon = new ImageIcon("Picture/edit.png");
editButton.setIcon(editIcon);
editButton.setToolTipText("edit");
delButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
delRow = table.rowAtPoint(mousePoint);
if (delRow != -1) {
int delresponse = JOptionPane.showConfirmDialog(null, "Are you sure to remove ?", "Warning", JOptionPane.OK_CANCEL_OPTION);
if (delresponse == JOptionPane.OK_OPTION) {
model.removeRow(table.rowAtPoint(mousePoint));
for (OptAlgorithm mAlgorithm : algorithms) {
if (AlgorithmName.equals(mAlgorithm.getName())) {
addAlgorithms.add(mAlgorithm);
}
}
System.out.println("(Table)RowCount: " + table.getRowCount());
}
}
} catch (Exception e2) {
JOptionPane.showMessageDialog(null, "please choose remove item first ! ");
}
}
});
//问题1:删除后点击编辑,还可以编辑:连续点多次删除,会逐行删除,接着点击编辑,会编辑连续删除中的第一个
//问题2:个数多于algorithms.size()的无法编辑(增添的行数已经增加到table中,并且可以获取)
//---编辑是按行数来,删除后,也会出现编辑不对应的情况...
//解决问题2:获取该行的mAlgorithm.getName(),然后遍历寻找该algorithm,即可实现对应,且不论行数为多少都可编辑
editButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
try {
for (OptAlgorithm mAlgorithm : algorithms) {
if (AlgorithmName.equals(mAlgorithm.getName())) {
setting = mAlgorithm.getSetting();
settingEditor = mAlgorithm.getOptSettingEditor();
}
}
editJPanel.setLayout(new BorderLayout());
editJPanel.add(settingEditor.getEditorComponent(), BorderLayout.NORTH);
EditDescriptor = new DialogDescriptor(editJPanel, AlgorithmName, true,
DialogDescriptor.OK_CANCEL_OPTION, DialogDescriptor.OK_OPTION, null);
Object result = DialogDisplayer.getDefault().notify(EditDescriptor); // displays the dialog
if (result == NotifyDescriptor.OK_OPTION) {
if (settingEditor.stopEditing()) {
}
editJPanel.removeAll();
} else if (result == NotifyDescriptor.CANCEL_OPTION) {
editJPanel.removeAll();
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "please choose edit item first !");
}
}
});
//问题3:点击jlist后所显示的面板无法全显,解决:增加JList每个单元格的高度
//已实现:如果某一个algorithm已经修改数据,那么在add时,点击该algorithm会显示已经修改过的值;
//如果删除该algorithm,则再添加时点击该algorithm会显示最初的值
//可改进:只有没有的algorithm,才需要添加;遇到的问题是:弹出的对话框太小。。。
addButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
addJPanel.removeAll();
addJPanel.setLayout(new BorderLayout());
listModel = new DefaultListModel();
for (OptAlgorithm mAlgorithm : algorithms) {
listModel.addElement(mAlgorithm.getName());
}
optAlgorithmList = new JList(listModel);
optAlgorithmList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
optAlgorithmList.setFixedCellWidth(80);
optAlgorithmList.setFixedCellHeight(32);
AddAlgorithmScroll = new JScrollPane(optAlgorithmList);
AddAlgorithmScroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
addCenterJPanel.setLayout(new BorderLayout());
//没有设置大小的效果。。
addCenterJPanel.setSize(1000, 1000);
// addCenterJPanel.setBounds(new Rectangle(500, 1000));
setting = algorithms.get(0).getSetting();
settingEditor = algorithms.get(0).getOptSettingEditor();
addCenterJPanel.add(settingEditor.getEditorComponent(), BorderLayout.NORTH);
addJPanel.add(addCenterJPanel, BorderLayout.CENTER);
optAlgorithmList.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent me) {
try {
addRow = optAlgorithmList.getSelectedIndex();
System.out.println("(JListRow) " + addRow + " : " + optAlgorithmList.getSelectedValue().toString());
addCenterJPanel.removeAll();
setting = algorithms.get(addRow).getSetting();
settingEditor = algorithms.get(addRow).getOptSettingEditor();
addCenterJPanel.add(settingEditor.getEditorComponent(), BorderLayout.NORTH);
addJPanel.add(addCenterJPanel, BorderLayout.CENTER);
addJPanel.validate();
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Error !");
}
}
});
addJPanel.add(AddAlgorithmScroll, BorderLayout.WEST);
AddDescriptor = new DialogDescriptor(addJPanel, "addAlgorithm", true,
DialogDescriptor.OK_CANCEL_OPTION, DialogDescriptor.OK_OPTION, null);
Object result = DialogDisplayer.getDefault().notify(AddDescriptor); // displays the dialog
addRowData = new String[2];
if (result == NotifyDescriptor.OK_OPTION) {
addRowData[0] = algorithms.get(addRow).getName();
addRowData[1] = String.valueOf(0);
model.addRow(addRowData);
} else if (result == NotifyDescriptor.CANCEL_OPTION) {
}
}
});
JToolBar tableToolBar = new JToolBar();
tableToolBar.add(addButton);
tableToolBar.add(delButton);
tableToolBar.add(editButton);
headerJPanel.add(tableToolBar, BorderLayout.NORTH);
return headerJPanel;
}
}
今天把前几天写的代码重写了一下,昨天写的被主管批了一顿,想想,的确挺垃圾的,就是玩码字的,java实现的是一种接口思想。
今天写的是200多行,实现的是昨天500多行的作用,也算是一点进步吧。