1 | package jp.ac.osaka_u.sanken.util;
|
---|
2 |
|
---|
3 | import java.awt.BorderLayout;
|
---|
4 | import java.awt.Component;
|
---|
5 | import java.awt.Dimension;
|
---|
6 | import java.awt.Point;
|
---|
7 | import java.awt.Rectangle;
|
---|
8 | import java.awt.event.ActionEvent;
|
---|
9 | import java.awt.event.ActionListener;
|
---|
10 | import java.awt.event.MouseEvent;
|
---|
11 | import java.awt.event.MouseListener;
|
---|
12 | import java.awt.event.MouseMotionListener;
|
---|
13 |
|
---|
14 | import javax.swing.DefaultListModel;
|
---|
15 | import javax.swing.ImageIcon;
|
---|
16 | import javax.swing.JList;
|
---|
17 | import javax.swing.JPanel;
|
---|
18 | import javax.swing.JRadioButton;
|
---|
19 | import javax.swing.JTextField;
|
---|
20 | import javax.swing.ListCellRenderer;
|
---|
21 | import javax.swing.ListModel;
|
---|
22 | import javax.swing.event.ListSelectionListener;
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * 鬆�岼縺ョ霑ス蜉繝サ蜑企勁蜿ッ閭ス縺ェ繝ェ繧ケ繝� |
---|
26 | * @author mouse
|
---|
27 | *
|
---|
28 | */
|
---|
29 | public class EditableList extends JPanel {
|
---|
30 | /**
|
---|
31 | *
|
---|
32 | */
|
---|
33 | private static final long serialVersionUID = 8390493649442688888L;
|
---|
34 |
|
---|
35 | private EditableListList list;
|
---|
36 |
|
---|
37 | private JTextField textField;
|
---|
38 |
|
---|
39 | public EditableList(){
|
---|
40 | this(new DefaultListModel());
|
---|
41 | }
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * 繝「繝�Ν繧呈欠螳壹@縺ヲ繝ェ繧ケ繝育函謌� |
---|
45 | * @param model
|
---|
46 | */
|
---|
47 | public EditableList(ListModel model){
|
---|
48 | this.list = new EditableListList(model);
|
---|
49 | this.list.setFixedCellHeight(26);
|
---|
50 | initialize();
|
---|
51 | }
|
---|
52 |
|
---|
53 | /**
|
---|
54 | * 繝ェ繧ケ繝医う繝吶Φ繝郁ソス蜉蟋碑ュイ
|
---|
55 | * @param l
|
---|
56 | */
|
---|
57 | public void addListSelectionListener(ListSelectionListener l){
|
---|
58 | this.list.addListSelectionListener(l);
|
---|
59 | }
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * 繝ェ繧ケ繝医う繝吶Φ繝亥炎髯、蟋碑ュイ
|
---|
63 | * @param l
|
---|
64 | */
|
---|
65 | public void removeListSelectionListener(ListSelectionListener l){
|
---|
66 | this.list.removeListSelectionListener(l);
|
---|
67 | }
|
---|
68 |
|
---|
69 | /**
|
---|
70 | * 繝「繝�Ν蜿門セ怜ァ碑ュイ
|
---|
71 | * @return
|
---|
72 | */
|
---|
73 | public ListModel getModel(){
|
---|
74 | return list.getModel();
|
---|
75 | }
|
---|
76 |
|
---|
77 | /**
|
---|
78 | * 鬆�岼霑ス蜉逕ィ繝�く繧ケ繝医ヵ繧」繝シ繝ォ繝牙叙蠕� |
---|
79 | * @return
|
---|
80 | */
|
---|
81 | public JTextField getTextField(){
|
---|
82 | if (textField == null){
|
---|
83 | textField = new JTextField();
|
---|
84 | textField.addActionListener(new ActionListener() {
|
---|
85 |
|
---|
86 | @Override
|
---|
87 | public void actionPerformed(ActionEvent e) {
|
---|
88 | addCurrentItem();
|
---|
89 | }
|
---|
90 | });
|
---|
91 | }
|
---|
92 | return textField;
|
---|
93 | }
|
---|
94 |
|
---|
95 |
|
---|
96 | private void initialize(){
|
---|
97 | this.setLayout(new BorderLayout());
|
---|
98 | this.add(list, BorderLayout.CENTER);
|
---|
99 | this.add(getTextField(), BorderLayout.SOUTH);
|
---|
100 | }
|
---|
101 |
|
---|
102 | /**
|
---|
103 | * 驕ゥ蛻�↑繧オ繧、繧コ繧定ソ斐☆��ODO 迴セ螳溯」�〒縺ッ讓ェ蟷�00蝗コ螳夲シ� |
---|
104 | */
|
---|
105 | public Dimension getPreferredSize(){
|
---|
106 | return new Dimension(400, (list.getModel().getSize()) * list.getFixedCellHeight() + getTextField().getPreferredSize().height);
|
---|
107 | }
|
---|
108 |
|
---|
109 | private void addCurrentItem(){
|
---|
110 | String word = getTextField().getText();
|
---|
111 |
|
---|
112 | if (word.trim().isEmpty()){
|
---|
113 | return;
|
---|
114 | }
|
---|
115 | boolean exists = false;
|
---|
116 | for (int i=0; i<list.getModel().getSize(); i++){
|
---|
117 | Object obj = list.getModel().getElementAt(i);
|
---|
118 | if (obj.toString().equals(word)){
|
---|
119 | exists = true;
|
---|
120 | break;
|
---|
121 | }
|
---|
122 | }
|
---|
123 | if (!exists){
|
---|
124 | ((DefaultListModel)list.getModel()).addElement(new EditableListItem(word, true));
|
---|
125 | getTextField().setText("");
|
---|
126 | }
|
---|
127 | }
|
---|
128 |
|
---|
129 | class EditableListList extends JList {
|
---|
130 | /**
|
---|
131 | *
|
---|
132 | */
|
---|
133 | private static final long serialVersionUID = -8207275088256642406L;
|
---|
134 | private EditableCellRenderer renderer;
|
---|
135 |
|
---|
136 | public EditableListList() {
|
---|
137 | super();
|
---|
138 |
|
---|
139 | this.putClientProperty("List.isFileList", Boolean.TRUE);
|
---|
140 | }
|
---|
141 |
|
---|
142 | public EditableListList(ListModel model) {
|
---|
143 | super(model);
|
---|
144 | this.putClientProperty("List.isFileList", Boolean.TRUE);
|
---|
145 | }
|
---|
146 |
|
---|
147 |
|
---|
148 | public void updateUI() {
|
---|
149 | setForeground(null);
|
---|
150 | setBackground(null);
|
---|
151 | setSelectionForeground(null);
|
---|
152 | setSelectionBackground(null);
|
---|
153 | if(renderer != null) {
|
---|
154 | removeMouseListener(renderer);
|
---|
155 | removeMouseMotionListener(renderer);
|
---|
156 | }
|
---|
157 | super.updateUI();
|
---|
158 | renderer = new EditableCellRenderer();
|
---|
159 | setCellRenderer(renderer);
|
---|
160 | addMouseListener(renderer);
|
---|
161 | addMouseMotionListener(renderer);
|
---|
162 | }
|
---|
163 |
|
---|
164 | private boolean pointOutsidePrefSize(Point p) {
|
---|
165 | int index = locationToIndex(p);
|
---|
166 | DefaultListModel m = (DefaultListModel)getModel();
|
---|
167 | if (index < 0){
|
---|
168 | return false;
|
---|
169 | }
|
---|
170 | Object n = m.get(index);
|
---|
171 | Component c = getCellRenderer().getListCellRendererComponent(
|
---|
172 | this, n, index, false, false);
|
---|
173 | c.doLayout();
|
---|
174 | Dimension d = c.getPreferredSize();
|
---|
175 | Rectangle rect = getCellBounds(index, index);
|
---|
176 | rect.width = d.width;
|
---|
177 | return index < 0 || !rect.contains(p);
|
---|
178 | }
|
---|
179 | @Override
|
---|
180 | protected void processMouseEvent(MouseEvent e) {
|
---|
181 | if(!pointOutsidePrefSize(e.getPoint())) {
|
---|
182 | super.processMouseEvent(e);
|
---|
183 | }
|
---|
184 | }
|
---|
185 | @Override
|
---|
186 | protected void processMouseMotionEvent(MouseEvent e) {
|
---|
187 | if(!pointOutsidePrefSize(e.getPoint())) {
|
---|
188 | super.processMouseMotionEvent(e);
|
---|
189 | }else{
|
---|
190 | e = new MouseEvent(
|
---|
191 | (Component)e.getSource(), MouseEvent.MOUSE_EXITED, e.getWhen(),
|
---|
192 | e.getModifiers(), e.getX(), e.getY(),
|
---|
193 | e.getXOnScreen(), e.getYOnScreen(),
|
---|
194 | e.getClickCount(), e.isPopupTrigger(), MouseEvent.NOBUTTON);
|
---|
195 | super.processMouseEvent(e);
|
---|
196 | }
|
---|
197 | }
|
---|
198 | }
|
---|
199 |
|
---|
200 | class EditableCellRenderer extends JPanel implements ListCellRenderer, MouseListener, MouseMotionListener {
|
---|
201 | /**
|
---|
202 | *
|
---|
203 | */
|
---|
204 | private static final long serialVersionUID = 5134902478737266926L;
|
---|
205 |
|
---|
206 | private final JRadioButton button;
|
---|
207 |
|
---|
208 | public EditableCellRenderer(){
|
---|
209 | button = new JRadioButton(new ImageIcon(getClass().getResource("x.png")));
|
---|
210 | button.setSelectedIcon(new ImageIcon(getClass().getResource("blank.png")));
|
---|
211 | }
|
---|
212 |
|
---|
213 | @Override
|
---|
214 | public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
|
---|
215 | Component ret;
|
---|
216 |
|
---|
217 | if(value instanceof EditableListItem) {
|
---|
218 | button.setSelected(!((EditableListItem)value).deleteable);
|
---|
219 | } else {
|
---|
220 | button.setSelected(false);
|
---|
221 | }
|
---|
222 |
|
---|
223 | button.setBackground(list.getBackground());
|
---|
224 | button.setForeground(list.getForeground());
|
---|
225 | button.setText(value.toString());
|
---|
226 | ret = button;
|
---|
227 |
|
---|
228 | return ret;
|
---|
229 | }
|
---|
230 |
|
---|
231 |
|
---|
232 | @Override
|
---|
233 | public void mouseExited(MouseEvent e) {
|
---|
234 | }
|
---|
235 |
|
---|
236 | @Override
|
---|
237 | public void mouseClicked(MouseEvent e) {
|
---|
238 | if(e.getButton()==MouseEvent.BUTTON1) {
|
---|
239 | JList l = (JList)e.getComponent();
|
---|
240 | DefaultListModel m = (DefaultListModel)l.getModel();
|
---|
241 | Point p = e.getPoint();
|
---|
242 | int index = l.locationToIndex(p);
|
---|
243 | if(index>=0) {
|
---|
244 | Object value = m.get(index);
|
---|
245 | if(value instanceof EditableListItem) {
|
---|
246 | if (!((EditableListItem)value).deleteable){
|
---|
247 | return;
|
---|
248 | }
|
---|
249 | }
|
---|
250 | ((DefaultListModel)l.getModel()).remove(index);
|
---|
251 | Rectangle rect = l.getCellBounds(index, index);
|
---|
252 | if (rect != null){
|
---|
253 | l.repaint(rect);
|
---|
254 | }
|
---|
255 | }
|
---|
256 | }
|
---|
257 | }
|
---|
258 |
|
---|
259 | @Override
|
---|
260 | public void mouseMoved(MouseEvent e) {
|
---|
261 | }
|
---|
262 |
|
---|
263 |
|
---|
264 |
|
---|
265 | @Override
|
---|
266 | public void mouseEntered(MouseEvent e) {}
|
---|
267 |
|
---|
268 | @Override
|
---|
269 | public void mousePressed(MouseEvent e) {}
|
---|
270 |
|
---|
271 | @Override
|
---|
272 | public void mouseReleased(MouseEvent e) {}
|
---|
273 |
|
---|
274 | @Override
|
---|
275 | public void mouseDragged(MouseEvent e) {}
|
---|
276 | }
|
---|
277 | }
|
---|