package jp.ac.osaka_u.sanken.util; import java.awt.BorderLayout; import java.awt.Component; import java.awt.Dimension; import java.awt.Point; import java.awt.Rectangle; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; import javax.swing.DefaultListModel; import javax.swing.ImageIcon; import javax.swing.JList; import javax.swing.JPanel; import javax.swing.JRadioButton; import javax.swing.JTextField; import javax.swing.ListCellRenderer; import javax.swing.ListModel; import javax.swing.event.ListSelectionListener; /** * 項目の追加・削除可能なリスト * @author mouse * */ public class EditableList extends JPanel { /** * */ private static final long serialVersionUID = 8390493649442688888L; private EditableListList list; private JTextField textField; public EditableList(){ this(new DefaultListModel()); } /** * モデルを指定してリスト生成 * @param model */ public EditableList(ListModel model){ this.list = new EditableListList(model); this.list.setFixedCellHeight(26); initialize(); } /** * リストイベント追加委譲 * @param l */ public void addListSelectionListener(ListSelectionListener l){ this.list.addListSelectionListener(l); } /** * リストイベント削除委譲 * @param l */ public void removeListSelectionListener(ListSelectionListener l){ this.list.removeListSelectionListener(l); } /** * モデル取得委譲 * @return */ public ListModel getModel(){ return list.getModel(); } /** * 項目追加用テキストフィールド取得 * @return */ public JTextField getTextField(){ if (textField == null){ textField = new JTextField(); textField.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { addCurrentItem(); } }); } return textField; } private void initialize(){ this.setLayout(new BorderLayout()); this.add(list, BorderLayout.CENTER); this.add(getTextField(), BorderLayout.SOUTH); } /** * 適切なサイズを返す(TODO 現実装では横幅400固定) */ public Dimension getPreferredSize(){ return new Dimension(400, (list.getModel().getSize()) * list.getFixedCellHeight() + getTextField().getPreferredSize().height); } private void addCurrentItem(){ String word = getTextField().getText(); if (word.trim().isEmpty()){ return; } boolean exists = false; for (int i=0; i=0) { Object value = m.get(index); if(value instanceof EditableListItem) { if (!((EditableListItem)value).deleteable){ return; } } ((DefaultListModel)l.getModel()).remove(index); Rectangle rect = l.getCellBounds(index, index); if (rect != null){ l.repaint(rect); } } } } @Override public void mouseMoved(MouseEvent e) { } @Override public void mouseEntered(MouseEvent e) {} @Override public void mousePressed(MouseEvent e) {} @Override public void mouseReleased(MouseEvent e) {} @Override public void mouseDragged(MouseEvent e) {} } }