OSDN Git Service

置換文字設定を削除
[coroid/inqubus.git] / frontend / src / yukihane / inqubus / gui / DirectoryRegisterPanel.java
1 package yukihane.inqubus.gui;
2
3 import java.awt.datatransfer.DataFlavor;
4 import java.awt.datatransfer.Transferable;
5 import java.awt.datatransfer.UnsupportedFlavorException;
6 import java.awt.event.ActionEvent;
7 import java.awt.event.ActionListener;
8 import java.io.File;
9 import java.io.IOException;
10 import java.util.ArrayList;
11 import java.util.List;
12 import org.slf4j.Logger;
13 import org.slf4j.LoggerFactory;
14 import javax.swing.DefaultListModel;
15 import javax.swing.GroupLayout;
16 import javax.swing.JButton;
17 import javax.swing.JFileChooser;
18 import javax.swing.JList;
19 import javax.swing.JPanel;
20 import javax.swing.JScrollPane;
21 import javax.swing.LayoutStyle.ComponentPlacement;
22 import javax.swing.TransferHandler;
23
24 /**
25  *
26  * @author yuki
27  */
28 class DirectoryRegisterPanel extends JPanel {
29
30     private static final long serialVersionUID = 1L;
31     private final DefaultListModel<String> mdlSearchVideo = new DefaultListModel<>();
32     private final JList<String> lstSearchVideo = new JList<>(mdlSearchVideo);
33     private final JButton btnSearchVideoAdd = new JButton("追加...");
34     private final JButton btnSearchVideoDel = new JButton("削除");
35
36     List<String> getAllElements() {
37         List<String> res = new ArrayList<>(mdlSearchVideo.getSize());
38         for (int i = 0; i < mdlSearchVideo.getSize(); i++) {
39             res.add(mdlSearchVideo.get(i));
40         }
41         return res;
42     }
43
44     void init(List<String> dirs) {
45         mdlSearchVideo.clear();
46         for (String s : dirs) {
47             mdlSearchVideo.addElement(s);
48         }
49     }
50
51     DirectoryRegisterPanel() {
52         lstSearchVideo.setTransferHandler(new DirectoryTransferHandler(mdlSearchVideo));
53         btnSearchVideoAdd.addActionListener(new AddActionListener());
54         btnSearchVideoDel.addActionListener(new DelActionListener());
55
56         final JScrollPane srcSearchVideo = new JScrollPane();
57         srcSearchVideo.setViewportView(lstSearchVideo);
58
59         GroupLayout gl_pnlSearchVideo = new GroupLayout(this);
60         setLayout(gl_pnlSearchVideo);
61         gl_pnlSearchVideo.setHorizontalGroup(
62             gl_pnlSearchVideo.createParallelGroup(GroupLayout.Alignment.LEADING)
63             .addComponent(srcSearchVideo, GroupLayout.DEFAULT_SIZE, 419, Short.MAX_VALUE)
64             .addGroup(GroupLayout.Alignment.TRAILING, gl_pnlSearchVideo.createSequentialGroup()
65                 .addContainerGap(269, Short.MAX_VALUE)
66                 .addComponent(btnSearchVideoAdd)
67                 .addGap(18, 18, 18)
68                 .addComponent(btnSearchVideoDel)
69                 .addContainerGap())
70         );
71         gl_pnlSearchVideo.setVerticalGroup(
72             gl_pnlSearchVideo.createParallelGroup(GroupLayout.Alignment.LEADING)
73             .addGroup(gl_pnlSearchVideo.createSequentialGroup()
74                 .addComponent(srcSearchVideo, GroupLayout.PREFERRED_SIZE, 100, Short.MAX_VALUE)
75                 .addPreferredGap(ComponentPlacement.RELATED)
76                 .addGroup(gl_pnlSearchVideo.createParallelGroup(GroupLayout.Alignment.BASELINE)
77                     .addComponent(btnSearchVideoDel)
78                     .addComponent(btnSearchVideoAdd))
79             )
80         );
81     }
82
83     private class AddActionListener implements ActionListener {
84
85         @Override
86         public void actionPerformed(ActionEvent e) {
87             final JFileChooser ch = new JFileChooser();
88             ch.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
89             final int selected = ch.showOpenDialog(DirectoryRegisterPanel.this);
90             if (selected == JFileChooser.APPROVE_OPTION) {
91                 final File dir = ch.getSelectedFile();
92                 mdlSearchVideo.addElement(dir.getPath());
93             }
94         }
95     }
96
97     private class DelActionListener implements ActionListener {
98
99         @Override
100         public void actionPerformed(ActionEvent e) {
101             final int idx = lstSearchVideo.getSelectedIndex();
102             mdlSearchVideo.removeElementAt(idx);
103         }
104     }
105 }
106
107 class DirectoryTransferHandler extends TransferHandler {
108
109     private static final long serialVersionUID = 1L;
110     private static final Logger logger = LoggerFactory.getLogger(DirectoryTransferHandler.class);
111     private final DefaultListModel<String> model;
112
113     DirectoryTransferHandler(DefaultListModel<String> model) {
114         this.model = model;
115     }
116
117     @Override
118     public boolean canImport(TransferHandler.TransferSupport support) {
119         Transferable transferable = support.getTransferable();
120         if (transferable.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
121             return true;
122         }
123         return false;
124     }
125
126     @Override
127     public boolean importData(TransferHandler.TransferSupport support) {
128         boolean res = false;
129         try {
130             Transferable transferable = support.getTransferable();
131             if (transferable.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
132                 @SuppressWarnings("unchecked")
133                 final List<File> data = (List<File>) transferable.getTransferData(DataFlavor.javaFileListFlavor);
134                 for (File f : data) {
135                     if (f.isDirectory()) {
136                         model.addElement(f.getPath());
137                         res = true;
138                     }
139                 }
140             }
141             return res;
142         } catch (UnsupportedFlavorException | IOException e) {
143             logger.error(null, e);
144             return res;
145         }
146     }
147 }