OSDN Git Service

NGリストのイベント処理実装
authoryukihane <yukihane.feather@gmail.com>
Wed, 31 Aug 2011 08:30:50 +0000 (17:30 +0900)
committeryukihane <yukihane.feather@gmail.com>
Wed, 31 Aug 2011 08:44:44 +0000 (17:44 +0900)
frontend/src/yukihane/inqubus/gui/NgRegisterPanel.java

index d901a6e..acbd2cf 100644 (file)
@@ -1,15 +1,19 @@
 package yukihane.inqubus.gui;
 
+import java.awt.event.ActionEvent;
+import java.awt.event.ActionListener;
 import java.util.ArrayList;
 import java.util.List;
 import javax.swing.DefaultListModel;
 import javax.swing.GroupLayout;
+import javax.swing.GroupLayout.Alignment;
 import javax.swing.JButton;
 import javax.swing.JList;
 import javax.swing.JPanel;
 import javax.swing.JScrollPane;
 import javax.swing.JTextField;
 import javax.swing.LayoutStyle.ComponentPlacement;
+import org.apache.commons.lang.StringUtils;
 
 /**
  *
@@ -23,53 +27,86 @@ class NgRegisterPanel extends JPanel {
     private final JButton btnNgWordAdd = new JButton("追加");
     private final JButton btnNgWordDel = new JButton("削除");
 
+
+    void init(List<String> ngWords) {
+        mdlNgWord.clear();
+        for (String s : ngWords) {
+            mdlNgWord.addElement(s);
+        }
+    }
+
+    List<String> getAllElements() {
+        List<String> res = new ArrayList<>(mdlNgWord.getSize());
+        for (int i = 0; i < mdlNgWord.getSize(); i++) {
+            res.add(mdlNgWord.get(i));
+        }
+        return res;
+    }
+
     NgRegisterPanel() {
         final JScrollPane scrNgWord = new JScrollPane();
         lstNgWord.setModel(mdlNgWord);
         scrNgWord.setViewportView(lstNgWord);
 
 
-        GroupLayout gl_pnlNgWord = new GroupLayout(this);
-        setLayout(gl_pnlNgWord);
-        gl_pnlNgWord.setHorizontalGroup(
-            gl_pnlNgWord.createParallelGroup(GroupLayout.Alignment.LEADING)
-            .addGroup(gl_pnlNgWord.createSequentialGroup()
+        btnNgWordDel.addActionListener(new DelActionListener());
+        final AddActionListener aal = new AddActionListener();
+        btnNgWordAdd.addActionListener(aal);
+        fldNgWord.addActionListener(aal);
+
+        final GroupLayout gl = new GroupLayout(this);
+        setLayout(gl);
+        gl.setHorizontalGroup(
+            gl.createParallelGroup(Alignment.LEADING)
+            .addGroup(gl.createSequentialGroup()
                 .addContainerGap()
-                .addGroup(gl_pnlNgWord.createParallelGroup(GroupLayout.Alignment.LEADING)
+                .addGroup(gl.createParallelGroup(Alignment.LEADING)
                     .addComponent(scrNgWord, GroupLayout.DEFAULT_SIZE, 395, Short.MAX_VALUE)
-                    .addGroup(gl_pnlNgWord.createSequentialGroup()
+                    .addGroup(gl.createSequentialGroup()
                         .addComponent(fldNgWord, GroupLayout.DEFAULT_SIZE, 256, Short.MAX_VALUE)
                         .addPreferredGap(ComponentPlacement.RELATED)
                         .addComponent(btnNgWordAdd)
-                        .addGap(18, 18, 18)
+                        .addGap(50)
                         .addComponent(btnNgWordDel)))
                 .addContainerGap())
         );
-        gl_pnlNgWord.setVerticalGroup(
-            gl_pnlNgWord.createParallelGroup(GroupLayout.Alignment.LEADING)
-            .addGroup(gl_pnlNgWord.createSequentialGroup()
+        gl.setVerticalGroup(
+            gl.createParallelGroup(Alignment.LEADING)
+            .addGroup(gl.createSequentialGroup()
+                .addContainerGap()
                 .addComponent(scrNgWord, GroupLayout.PREFERRED_SIZE, 100, GroupLayout.PREFERRED_SIZE)
                 .addPreferredGap(ComponentPlacement.RELATED)
-                .addGroup(gl_pnlNgWord.createParallelGroup(GroupLayout.Alignment.BASELINE)
+                .addGroup(gl.createParallelGroup(Alignment.BASELINE)
                     .addComponent(fldNgWord, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                     .addComponent(btnNgWordDel)
                     .addComponent(btnNgWordAdd))
-                .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
+                .addContainerGap())
         );
 
     }
 
-    void init(List<String> ngWords) {
-        for (String s : ngWords) {
-            mdlNgWord.addElement(s);
+    private class AddActionListener implements ActionListener {
+
+        @Override
+        public void actionPerformed(ActionEvent e) {
+        final String w = fldNgWord.getText();
+        if (StringUtils.isBlank(w)) {
+            return;
+        }
+        mdlNgWord.addElement(w);
+        fldNgWord.setText("");
         }
     }
 
-    List<String> getAllElements() {
-        List<String> res = new ArrayList<>(mdlNgWord.getSize());
-        for (int i = 0; i < mdlNgWord.getSize(); i++) {
-            res.add(mdlNgWord.get(i));
+    private class DelActionListener implements ActionListener {
+
+        @Override
+        public void actionPerformed(ActionEvent e) {
+            final int idx = lstNgWord.getSelectedIndex();
+            if(idx < 0) {
+                return;
+            }
+            mdlNgWord.remove(idx);
         }
-        return res;
     }
 }