OSDN Git Service

置換文字設定を削除
[coroid/inqubus.git] / frontend / src / yukihane / inqubus / gui / NgRegisterPanel.java
1 package yukihane.inqubus.gui;
2
3 import java.awt.event.ActionEvent;
4 import java.awt.event.ActionListener;
5 import java.util.ArrayList;
6 import java.util.List;
7 import javax.swing.DefaultListModel;
8 import javax.swing.GroupLayout;
9 import javax.swing.GroupLayout.Alignment;
10 import javax.swing.JButton;
11 import javax.swing.JList;
12 import javax.swing.JPanel;
13 import javax.swing.JScrollPane;
14 import javax.swing.JTextField;
15 import javax.swing.LayoutStyle.ComponentPlacement;
16 import org.apache.commons.lang.StringUtils;
17
18 /**
19  *
20  * @author yuki
21  */
22 class NgRegisterPanel extends JPanel {
23     private static final long serialVersionUID = 1L;
24     private final DefaultListModel<String> mdlNgWord = new DefaultListModel<>();
25     private final JList<String> lstNgWord = new JList<>();
26     private final JTextField fldNgWord = new JTextField();
27     private final JButton btnNgWordAdd = new JButton("追加");
28     private final JButton btnNgWordDel = new JButton("削除");
29
30
31     void init(List<String> ngWords) {
32         mdlNgWord.clear();
33         for (String s : ngWords) {
34             mdlNgWord.addElement(s);
35         }
36     }
37
38     List<String> getAllElements() {
39         List<String> res = new ArrayList<>(mdlNgWord.getSize());
40         for (int i = 0; i < mdlNgWord.getSize(); i++) {
41             res.add(mdlNgWord.get(i));
42         }
43         return res;
44     }
45
46     NgRegisterPanel() {
47         final JScrollPane scrNgWord = new JScrollPane();
48         lstNgWord.setModel(mdlNgWord);
49         scrNgWord.setViewportView(lstNgWord);
50
51
52         btnNgWordDel.addActionListener(new DelActionListener());
53         final AddActionListener aal = new AddActionListener();
54         btnNgWordAdd.addActionListener(aal);
55         fldNgWord.addActionListener(aal);
56
57         final GroupLayout gl = new GroupLayout(this);
58         setLayout(gl);
59         gl.setHorizontalGroup(
60             gl.createParallelGroup(Alignment.LEADING)
61             .addGroup(gl.createSequentialGroup()
62                 .addContainerGap()
63                 .addGroup(gl.createParallelGroup(Alignment.LEADING)
64                     .addComponent(scrNgWord, GroupLayout.DEFAULT_SIZE, 395, Short.MAX_VALUE)
65                     .addGroup(gl.createSequentialGroup()
66                         .addComponent(fldNgWord, GroupLayout.DEFAULT_SIZE, 256, Short.MAX_VALUE)
67                         .addPreferredGap(ComponentPlacement.RELATED)
68                         .addComponent(btnNgWordAdd)
69                         .addGap(50)
70                         .addComponent(btnNgWordDel)))
71                 .addContainerGap())
72         );
73         gl.setVerticalGroup(
74             gl.createParallelGroup(Alignment.LEADING)
75             .addGroup(gl.createSequentialGroup()
76                 .addContainerGap()
77                 .addComponent(scrNgWord, GroupLayout.PREFERRED_SIZE, 100, Short.MAX_VALUE)
78                 .addPreferredGap(ComponentPlacement.RELATED)
79                 .addGroup(gl.createParallelGroup(Alignment.BASELINE)
80                     .addComponent(fldNgWord, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
81                     .addComponent(btnNgWordDel)
82                     .addComponent(btnNgWordAdd))
83                 .addContainerGap())
84         );
85
86     }
87
88     private class AddActionListener implements ActionListener {
89
90         @Override
91         public void actionPerformed(ActionEvent e) {
92             final String w = fldNgWord.getText();
93             if (StringUtils.isBlank(w)) {
94                 return;
95             }
96             mdlNgWord.addElement(w);
97             lstNgWord.ensureIndexIsVisible(mdlNgWord.getSize() - 1);
98             fldNgWord.setText("");
99         }
100     }
101
102     private class DelActionListener implements ActionListener {
103
104         @Override
105         public void actionPerformed(ActionEvent e) {
106             final int idx = lstNgWord.getSelectedIndex();
107             if(idx < 0) {
108                 return;
109             }
110             mdlNgWord.remove(idx);
111         }
112     }
113 }