View Javadoc

1   /*
2    *  Licensed under the Apache License, Version 2.0 (the "License");
3    *  you may not use this file except in compliance with the License.
4    *  You may obtain a copy of the License at
5    *
6    *      http://www.apache.org/licenses/LICENSE-2.0
7    *
8    *  Unless required by applicable law or agreed to in writing, software
9    *  distributed under the License is distributed on an "AS IS" BASIS,
10   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11   *  See the License for the specific language governing permissions and
12   *  limitations under the License.
13   */
14  /*
15   * This file has been modified by Chris Pheby in accordance with Section 4.2
16   * of the Apache Software License
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                  // workaround for bug
55                  // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6317789
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 }