OSDN Git Service

2476a53e7ab263f0631785ebacabe3a1f25e95d9
[coroid/inqubus.git] / frontend / src / saccubus / worker / TestFrame.java
1 /*
2  * To change this template, choose Tools | Templates
3  * and open the template in the editor.
4  */
5 package saccubus.worker;
6
7 import java.awt.event.ActionEvent;
8 import java.awt.event.ActionListener;
9 import java.io.File;
10 import java.util.List;
11 import java.util.concurrent.ExecutionException;
12 import javax.swing.GroupLayout;
13 import javax.swing.JButton;
14 import javax.swing.JFrame;
15 import javax.swing.JPanel;
16 import javax.swing.JTextField;
17 import javax.swing.SwingUtilities;
18 import saccubus.worker.profile.CommentProfile;
19 import saccubus.worker.profile.GeneralProfile;
20 import saccubus.worker.profile.LoginProfile;
21 import saccubus.worker.profile.OutputProfile;
22 import saccubus.worker.profile.Profile;
23 import saccubus.worker.profile.ProxyProfile;
24 import saccubus.worker.profile.VideoProfile;
25
26 /**
27  *
28  * @author yuki
29  */
30 public class TestFrame extends JFrame {
31
32     private static final long serialVersionUID = 1L;
33     private final JTextField fldVideoId = new JTextField();
34     private final JButton btnDownload = new JButton("DOWNLOAD");
35     private final JButton btnCancel = new JButton("Cancel");
36     private final JTextField fldStatus = new JTextField();
37     private Download downloadWorker;
38
39     public TestFrame() {
40         JPanel panel = new JPanel();
41         GroupLayout lo = new GroupLayout(panel);
42         panel.setLayout(lo);
43
44         lo.setHorizontalGroup(lo.createParallelGroup()
45                 .addGroup(lo.createSequentialGroup()
46                     .addComponent(fldVideoId).addComponent(btnDownload))
47                 .addGroup(lo.createSequentialGroup()
48                     .addComponent(fldStatus).addComponent(btnCancel)));
49
50         lo.setVerticalGroup(lo.createSequentialGroup()
51                 .addGroup(lo.createParallelGroup()
52                     .addComponent(fldVideoId).addComponent(btnDownload))
53                 .addGroup(lo.createParallelGroup()
54                     .addComponent(fldStatus).addComponent(btnCancel)));
55
56         setContentPane(panel);
57         pack();
58
59
60         btnDownload.addActionListener(new DownloadListener());
61         btnCancel.addActionListener(new CancelListener());
62     }
63
64     private class CancelListener implements ActionListener {
65
66         @Override
67         public void actionPerformed(ActionEvent e) {
68             if(downloadWorker != null){
69                 downloadWorker.cancel(true);
70             }
71         }
72     }
73
74     private class DownloadListener implements ActionListener {
75         @Override
76         public void actionPerformed(ActionEvent e) {
77             downloadWorker = new Download(new MyProfile(), fldVideoId.getText()){
78                 @Override
79                 protected void process(List<DownloadProgress> chunks){
80                     DownloadProgress chunk = chunks.get(chunks.size()-1);
81                     fldStatus.setText(chunk.getMessage());
82                 }
83
84                 @Override
85                 protected void done(){
86                     btnDownload.setEnabled(true);
87                     try {
88                         DownloadResult res = get();
89                     } catch (InterruptedException ex) {
90                         ex.printStackTrace();
91                     } catch (ExecutionException ex) {
92                         ex.printStackTrace();
93                     }
94                 }
95             };
96
97             btnDownload.setEnabled(false);
98             downloadWorker.execute();
99
100         }
101
102     }
103
104     public static void main(String[] args){
105         SwingUtilities.invokeLater(new Runnable() {
106
107             @Override
108             public void run() {
109                 TestFrame view = new TestFrame();
110                 view.setDefaultCloseOperation(EXIT_ON_CLOSE);
111                 view.setVisible(true);
112             }
113         });
114     }
115
116     private static class MyProfile implements Profile{
117
118         @Override
119         public LoginProfile getLoginInfo() {
120             return new LoginProfile() {
121
122                 @Override
123                 public String getMail() {
124                     return "yamamoto5_5963@hotmail.com";
125                 }
126
127                 @Override
128                 public String getPassword() {
129                     return "piyopiyo";
130                 }
131             };
132         }
133
134         @Override
135         public ProxyProfile getProxySetting() {
136             return new ProxyProfile() {
137
138                 @Override
139                 public boolean use() {
140                     return false;
141                 }
142
143                 @Override
144                 public String getHost() {
145                     throw new UnsupportedOperationException("Not supported yet.");
146                 }
147
148                 @Override
149                 public int getPort() {
150                     throw new UnsupportedOperationException("Not supported yet.");
151                 }
152             };
153         }
154
155         @Override
156         public VideoProfile getVideoSetting() {
157             return new VideoProfile() {
158
159                 @Override
160                 public boolean isDownload() {
161                     return true;
162                 }
163
164                 @Override
165                 public File getDir() {
166                     return new File("out");
167                 }
168
169                 @Override
170                 public String getFileName() {
171                     return "{id}_{title}";
172                 }
173
174                 @Override
175                 public File getLocalFile() {
176                     throw new UnsupportedOperationException("Not supported yet.");
177                 }
178             };
179         }
180
181         @Override
182         public CommentProfile getCommentSetting() {
183             return new CommentProfile() {
184
185                 @Override
186                 public int getLengthRelatedCommentSize() {
187                     return -1;
188                 }
189
190                 @Override
191                 public boolean isDisablePerMinComment() {
192                     return false;
193                 }
194
195                 @Override
196                 public int getPerMinCommentSize() {
197                     return -1;
198                 }
199
200                 @Override
201                 public long getBackLogPoint() {
202                     return -1;
203                 }
204
205                 @Override
206                 public boolean isDownload() {
207                     return true;
208                 }
209
210                 @Override
211                 public File getDir() {
212                     return new File("out");
213                 }
214
215                 @Override
216                 public String getFileName() {
217                     return "{id}_{title}";
218                 }
219
220                 @Override
221                 public File getLocalFile() {
222                     throw new UnsupportedOperationException("Not supported yet.");
223                 }
224             };
225         }
226
227         @Override
228         public OutputProfile getOutputFileSetting() {
229             return new OutputProfile() {
230
231                 @Override
232                 public boolean isConvert() {
233                     return false;
234                 }
235
236                 @Override
237                 public boolean isAddComment() {
238                     throw new UnsupportedOperationException("Not supported yet.");
239                 }
240
241                 @Override
242                 public File getDir() {
243                     throw new UnsupportedOperationException("Not supported yet.");
244                 }
245
246                 @Override
247                 public String getFileName() {
248                     throw new UnsupportedOperationException("Not supported yet.");
249                 }
250             };
251         }
252
253         @Override
254         public GeneralProfile getGeneralSetting() {
255             return new GeneralProfile() {
256
257                 @Override
258                 public String getReplaceFrom() {
259                     return "<>\\/#";
260                 }
261
262                 @Override
263                 public String getReplaceTo() {
264                     return "_";
265                 }
266             };
267         }
268
269     }
270
271 }