-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.java
385 lines (373 loc) · 14.6 KB
/
Main.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
import java.util.*;
import java.io.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.Toolkit;
import javax.swing.event.*;
import javax.swing.tree.*;
import java.awt.Font;
import java.awt.Cursor;
import javax.swing.text.*;
import com.amazonaws.auth.*;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.Bucket;
import com.amazonaws.services.s3.transfer.*;
import com.amazonaws.services.s3.transfer.model.*;
import com.amazonaws.services.s3.model.ObjectListing;
import com.amazonaws.services.s3.model.S3Object;
import com.amazonaws.services.s3.model.S3ObjectSummary;
class BucketNode extends DefaultMutableTreeNode {
Bucket data = null;
BucketNode(Bucket bucket) {
data = bucket;
this.add(new DefaultMutableTreeNode(""));
}
public String toString() {
return data.getName();
}
}
class ObjectNode extends DefaultMutableTreeNode {
S3ObjectSummary data = null;
int nodeType = 0;
ObjectNode(S3ObjectSummary object, int type) {
data = object;
nodeType = type;
if (nodeType == 1) {
this.add(new DefaultMutableTreeNode(""));
}
}
public String toString() {
String s = data.getKey();
int tmp = s.length() - nodeType;
int t = s.lastIndexOf('/', tmp - 1);
return s.substring(t + 1, tmp);
}
}
class DataTree extends JTree implements TreeWillExpandListener, MouseListener {
private AWSCredentials myCredentials;
private AmazonS3 s3client;
private DefaultTreeModel e3Model;
private DefaultMutableTreeNode treeRoot;
private TreePath clipboard = null;
DataTree(String accessKey, String secretKey) {
myCredentials = new BasicAWSCredentials(accessKey, secretKey);
s3client = new AmazonS3Client(myCredentials);
this.setRootVisible(true);
this.addTreeWillExpandListener(this);
this.addMouseListener(this);
treeRoot = new DefaultMutableTreeNode("Buckets");
e3Model = new DefaultTreeModel(treeRoot);
this.setModel(e3Model);
try {
List<Bucket> bucketList = s3client.listBuckets();
for (Bucket i : bucketList) {
treeRoot.add(new BucketNode(i));
}
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, "Login Failed!");
}
}
public void mouseExited(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
if (e.isPopupTrigger()) {
JTree tree = (JTree)e.getComponent();
final TreePath path = tree.getPathForLocation(e.getX(), e.getY());
tree.setSelectionPath(path);
final Object node = path.getLastPathComponent();
JPopupMenu menu = new JPopupMenu();
JMenuItem iUpload = new JMenuItem("Upload");
iUpload.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
s3client = new AmazonS3Client(myCredentials);
TransferManager trans = new TransferManager(s3client);
JFileChooser fc = new JFileChooser();
int res = fc.showOpenDialog(null);
if (res == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
String key = "";
if (node instanceof ObjectNode) {
key += ((ObjectNode)node).data.getKey();
}
key += file.getName();
createBar(trans.upload(path.getPathComponent(1).toString(), key, file));
e3Model.nodeStructureChanged(treeRoot);
clipboard = null;
}
}
});
JMenuItem iCut = new JMenuItem("Cut");
iCut.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
clipboard = path;
}
});
JMenuItem iPaste = new JMenuItem("Paste");
iPaste.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
s3client = new AmazonS3Client(myCredentials);
String key = "";
Object node = path.getLastPathComponent();
if (node instanceof ObjectNode) {
key += ((ObjectNode)path.getLastPathComponent()).data.getKey();
}
key += clipboard.getLastPathComponent().toString();
Main.jf.setCursor(new Cursor(Cursor.WAIT_CURSOR));
s3client.copyObject(clipboard.getPathComponent(1).toString(), ((ObjectNode)clipboard.getLastPathComponent()).data.getKey(), path.getPathComponent(1).toString(), key);
s3client = new AmazonS3Client(myCredentials);
s3client.deleteObject(clipboard.getPathComponent(1).toString(), ((ObjectNode)clipboard.getLastPathComponent()).data.getKey());
clipboard = null;
Main.jf.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
e3Model.nodeStructureChanged(treeRoot);
}
});
JMenuItem iDownload = new JMenuItem("Download");
iDownload.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
s3client = new AmazonS3Client(myCredentials);
TransferManager trans = new TransferManager(s3client);
JFileChooser fc = new JFileChooser();
int res = fc.showSaveDialog(null);
if (res == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
createBar(trans.download(path.getPathComponent(1).toString(), ((ObjectNode)node).data.getKey(), file));
}
}
});
JMenuItem iDelete = new JMenuItem("Delete");
iDelete.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
s3client = new AmazonS3Client(myCredentials);
Main.jf.setCursor(new Cursor(Cursor.WAIT_CURSOR));
s3client.deleteObject(path.getPathComponent(1).toString(), ((ObjectNode)node).data.getKey());
JOptionPane.showMessageDialog(null, "Finish");
Main.jf.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
e3Model.nodeStructureChanged(treeRoot);
clipboard = null;
}
});
if (node instanceof BucketNode) {
menu.add(iUpload);
if (clipboard != null) {
menu.add(iPaste);
}
menu.show(tree, e.getX(), e.getY());
} else if (node instanceof ObjectNode) {
if (((ObjectNode)node).nodeType == 0) {
menu.add(iCut);
menu.add(iDownload);
menu.add(iDelete);
menu.show(tree, e.getX(), e.getY());
} else {
menu.add(iUpload);
menu.add(iDelete);
if (clipboard != null) {
menu.add(iPaste);
}
menu.show(tree, e.getX(), e.getY());
}
}
}
}
void createBar(Transfer trans) {
JDialog jd = new JDialog();
JProgressBar jp = new JProgressBar();
TransBar tb = new TransBar(jp, trans, jd, Main.jf);
Thread thread = new Thread(tb);
thread.start();
jd.setSize(240, 80);
jd.setLocation(300, 200);
jp.setSize(200, 15);
jp.setLocation(10, 5);
jd.setLayout(null);
jp.setVisible(true);
jd.add(jp);
jp.setStringPainted(true);
jp.setMinimum(0);
jp.setMaximum((int)trans.getProgress().getTotalBytesToTransfer());
jd.setVisible(true);
Main.jf.setEnabled(false);
}
public void mousePressed(MouseEvent e) {
}
public void mouseClicked(MouseEvent e) {
}
public void treeWillCollapse(TreeExpansionEvent e) {
}
public void treeWillExpand(TreeExpansionEvent e) {
Main.jf.setCursor(new Cursor(Cursor.WAIT_CURSOR));
TreePath path = e.getPath();
Object node = path.getLastPathComponent();
if (node instanceof BucketNode) {
BucketNode bucketNode = (BucketNode)(node);
bucketNode.removeAllChildren();
s3client = new AmazonS3Client(myCredentials);
List<S3ObjectSummary> obj = s3client.listObjects(bucketNode.toString()).getObjectSummaries();
for (S3ObjectSummary i : obj) {
String key = i.getKey();
int tmp = key.indexOf('/');
if (tmp == key.length() - 1) {
bucketNode.add(new ObjectNode(i, 1));
} else {
if (tmp == -1) {
bucketNode.add(new ObjectNode(i, 0));
}
}
}
} else if (node instanceof ObjectNode) {
ObjectNode objectNode = (ObjectNode)(node);
objectNode.removeAllChildren();
s3client = new AmazonS3Client(myCredentials);
List<S3ObjectSummary> obj = s3client.listObjects(path.getPathComponent(1).toString()).getObjectSummaries();
String nowPath = objectNode.data.getKey();
for (S3ObjectSummary i : obj) {
String key = i.getKey();
if (key.startsWith(nowPath) && !key.equals(nowPath)) {
objectNode.add(new ObjectNode(i, key.charAt(key.length() - 1) == '/' ? 1 : 0));
}
}
}
Main.jf.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
}
}
class TransBar implements Runnable{
private JProgressBar jp;
private Transfer trans;
private JDialog jd;
private JFrame jf;
TransBar(JProgressBar jp, Transfer trans, JDialog jd, JFrame jf) {
this.jp = jp;
this.trans = trans;
this.jd = jd;
this.jf = jf;
}
public void run() {
Runnable runner = new Runnable() {
public void run() {
TransferProgress tp = trans.getProgress();
jp.setValue((int)trans.getProgress().getBytesTransfered());
}
};
while(!trans.isDone()) {
try {
SwingUtilities.invokeAndWait(runner);
Thread.sleep(100);
} catch (Exception e) {
e.printStackTrace();
}
}
jp.setValue(jp.getMaximum());
JOptionPane.showMessageDialog(null, "Finish");
jf.setEnabled(true);
jd.dispose();
}
}
class LoginButton extends JButton implements ActionListener {
LoginButton() {
this.setLocation(20, 160);
this.setSize(90, 30);
this.setText("Login");
this.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
Main.jf.setCursor(new Cursor(Cursor.WAIT_CURSOR));
Main.jf.dataTree = new DataTree(Main.jf.af.getText(), Main.jf.sf.getText());
Main.jf.js.setViewportView(Main.jf.dataTree);
Main.jf.outButton.setEnabled(true);
Main.jf.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
this.setEnabled(false);
}
}
class LogoutButton extends JButton implements ActionListener {
LogoutButton() {
this.setLocation(135, 160);
this.setSize(90, 30);
this.setText("Logout");
this.addActionListener(this);
this.setEnabled(false);
}
public void actionPerformed(ActionEvent e) {
Main.jf.dataTree = null;
Main.jf.js.setViewportView(Main.jf.dataTree);
Main.jf.inButton.setEnabled(true);
this.setEnabled(false);
}
}
class AccessKeyIdField extends JTextField {
AccessKeyIdField() {
this.setSize(124, 20);
this.setLocation(20, 50);
this.setFont(new Font("Monospaced", Font.PLAIN, 12));
this.setDocument(new PlainDocument() {
public void insertString(int n, String s, AttributeSet a) throws BadLocationException {
if (this.getLength() + s.length() > 20) {
} else {
super.insertString(n, s, a);
Toolkit.getDefaultToolkit().beep();
}
}
});
this.setText("AKIAJJFM2OICN5ORSXFQ");
}
}
class SecretKeyField extends JTextField {
SecretKeyField() {
this.setSize(205, 20);
this.setLocation(20, 110);
this.setFont(new Font("Monospaced", Font.PLAIN, 10));
this.setDocument(new PlainDocument() {
public void insertString(int n, String s, AttributeSet a) throws BadLocationException {
if (this.getLength() + s.length() > 40) {
} else {
super.insertString(n, s, a);
Toolkit.getDefaultToolkit().beep();
}
}
});
this.setText("dwG4N7uwByASYD/d93RB9KT8EhbyMF/gATOnGfCE");
}
}
class MainFrame extends JFrame{
DataTree dataTree = null;
AccessKeyIdField af = new AccessKeyIdField();
SecretKeyField sf = new SecretKeyField();
JScrollPane js = new JScrollPane();
LoginButton inButton = new LoginButton();
LogoutButton outButton = new LogoutButton();
MainFrame() {
this.setTitle("Amazon S3 Client");
this.setSize(480, 300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(null);
Toolkit tk = Toolkit.getDefaultToolkit();
int width = tk.getScreenSize().width;
int height = tk.getScreenSize().height;
this.setLocation((tk.getScreenSize().width - 480) / 2, (tk.getScreenSize().height - 300) / 2);
JLabel jl0 = new JLabel("AWSAccessKeyID");
JLabel jl1 = new JLabel("AWSSecretKey");
jl0.setLocation(20, 25);
jl0.setSize(120, 20);
jl1.setLocation(20, 85);
jl1.setSize(120, 20);
this.add(jl0);
this.add(jl1);
this.add(inButton);
this.add(outButton);
this.add(af);
this.add(sf);
js.setSize(200, 200);
js.setLocation(240, 30);
this.add(js);
this.setVisible(true);
}
}
public class Main {
static MainFrame jf;
public static void main(String[] args) {
MainFrame mainFrame = new MainFrame();
jf = mainFrame;
}
}