1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.jadira.repositorysnapshot;
19
20 import java.awt.Cursor;
21 import java.io.File;
22 import java.io.IOException;
23 import java.util.zip.ZipException;
24
25 import javax.swing.JFileChooser;
26 import javax.swing.JFrame;
27
28 import org.jadira.dependencynavigator.DependencyNavigator;
29 import org.jadira.dependencynavigator.gui.ProgressMeter;
30 import org.jadira.dependencynavigator.implementations.Repository;
31 import org.jadira.dependencynavigator.implementations.Workspace;
32 import org.jadira.dependencynavigator.implementations.local.LocalDiskRepository;
33 import org.jadira.dependencynavigator.implementations.local.LocalDiskWorkspace;
34
35 public class RepositorySnapshotFactory {
36
37 private Workspace workspace;
38 private Repository repository;
39 private String repositoryType;
40 private JFrame frame;
41
42 public boolean isSnapshotEnabled() {
43 return DependencyNavigator.TYPE_LOCAL.equals(repositoryType);
44 }
45
46 public void createSnapshot(ProgressMeter progressMeter) {
47
48 JFileChooser fc = new JFileChooser() {
49
50 private static final long serialVersionUID = 5044149990493116910L;
51
52 @Override
53 public void updateUI() {
54
55
56 putClientProperty("FileChooser.useShellFolder", Boolean.FALSE);
57 super.updateUI();
58 }
59 };
60 fc.setDialogTitle("Save repository snapshot file");
61 int returnVal = fc.showSaveDialog(frame);
62 File snapshotFile = fc.getSelectedFile();
63 if (snapshotFile != null) {
64 if (!snapshotFile.getName().endsWith(".zip")) {
65 snapshotFile = new File(snapshotFile.getParentFile(), snapshotFile.getName() + ".zip");
66 }
67 if (returnVal == JFileChooser.APPROVE_OPTION) {
68 LocalDiskRepository localDiskRepository = (LocalDiskRepository) repository;
69 LocalDiskWorkspace localDiskWorkspace = (LocalDiskWorkspace) workspace;
70 RepositorySnapshot snapshotCreator = new RepositorySnapshot(localDiskRepository.getRepositoryRoot(), snapshotFile, localDiskWorkspace.getWorkspaceDirectory());
71 Thread snapshotCreatorThread = new Thread(new SnapshotCreatorThread(snapshotCreator, progressMeter));
72 snapshotCreatorThread.setDaemon(true);
73 snapshotCreatorThread.setName("snapshot-creator");
74 snapshotCreatorThread.start();
75 }
76 }
77 }
78
79 private class SnapshotCreatorThread implements Runnable {
80
81 private RepositorySnapshot snapshotCreator;
82 private ProgressMeter progressMeter;
83
84 public SnapshotCreatorThread(RepositorySnapshot snapshotCreator, ProgressMeter progressMeter) {
85 this.snapshotCreator = snapshotCreator;
86 this.progressMeter = progressMeter;
87 }
88
89 public void run() {
90 frame.setCursor(new Cursor(Cursor.WAIT_CURSOR));
91 try {
92 snapshotCreator.createSnapshot(progressMeter);
93 progressMeter.snapshotComplete();
94 } catch (ZipException e) {
95 progressMeter.error(e);
96 } catch (IOException e) {
97 progressMeter.error(e);
98 }
99 frame.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
100 }
101 }
102 }