001    /*
002     *  Licensed under the Apache License, Version 2.0 (the "License");
003     *  you may not use this file except in compliance with the License.
004     *  You may obtain a copy of the License at
005     *
006     *      http://www.apache.org/licenses/LICENSE-2.0
007     *
008     *  Unless required by applicable law or agreed to in writing, software
009     *  distributed under the License is distributed on an "AS IS" BASIS,
010     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
011     *  See the License for the specific language governing permissions and
012     *  limitations under the License.
013     */
014    /*
015     * This file has been modified by Chris Pheby in accordance with Section 4.2
016     * of the Apache Software License
017     */
018    package org.jadira.repositorysnapshot;
019    
020    import java.awt.Cursor;
021    import java.io.File;
022    import java.io.IOException;
023    import java.util.zip.ZipException;
024    
025    import javax.swing.JFileChooser;
026    import javax.swing.JFrame;
027    
028    import org.jadira.dependencynavigator.DependencyNavigator;
029    import org.jadira.dependencynavigator.gui.ProgressMeter;
030    import org.jadira.dependencynavigator.implementations.Repository;
031    import org.jadira.dependencynavigator.implementations.Workspace;
032    import org.jadira.dependencynavigator.implementations.local.LocalDiskRepository;
033    import org.jadira.dependencynavigator.implementations.local.LocalDiskWorkspace;
034    
035    public class RepositorySnapshotFactory {
036    
037        private Workspace workspace;
038        private Repository repository;
039        private String repositoryType;
040        private JFrame frame;
041    
042        public boolean isSnapshotEnabled() {
043            return DependencyNavigator.TYPE_LOCAL.equals(repositoryType);
044        }
045    
046        public void createSnapshot(ProgressMeter progressMeter) {
047            
048            JFileChooser fc = new JFileChooser() {
049    
050                private static final long serialVersionUID = 5044149990493116910L;
051    
052                @Override
053                public void updateUI() {
054                    // workaround for bug
055                    // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6317789
056                    putClientProperty("FileChooser.useShellFolder", Boolean.FALSE);
057                    super.updateUI();
058                }
059            };
060            fc.setDialogTitle("Save repository snapshot file");
061            int returnVal = fc.showSaveDialog(frame);
062            File snapshotFile = fc.getSelectedFile();
063            if (snapshotFile != null) {
064                if (!snapshotFile.getName().endsWith(".zip")) {
065                    snapshotFile = new File(snapshotFile.getParentFile(), snapshotFile.getName() + ".zip");
066                }
067                if (returnVal == JFileChooser.APPROVE_OPTION) {
068                    LocalDiskRepository localDiskRepository = (LocalDiskRepository) repository;
069                    LocalDiskWorkspace localDiskWorkspace = (LocalDiskWorkspace) workspace;
070                    RepositorySnapshot snapshotCreator = new RepositorySnapshot(localDiskRepository.getRepositoryRoot(), snapshotFile, localDiskWorkspace.getWorkspaceDirectory());
071                    Thread snapshotCreatorThread = new Thread(new SnapshotCreatorThread(snapshotCreator, progressMeter));
072                    snapshotCreatorThread.setDaemon(true);
073                    snapshotCreatorThread.setName("snapshot-creator");
074                    snapshotCreatorThread.start();
075                }
076            }
077        }
078    
079        private class SnapshotCreatorThread implements Runnable {
080    
081            private RepositorySnapshot snapshotCreator;
082            private ProgressMeter progressMeter;
083    
084            public SnapshotCreatorThread(RepositorySnapshot snapshotCreator, ProgressMeter progressMeter) {
085                this.snapshotCreator = snapshotCreator;
086                this.progressMeter = progressMeter;
087            }
088    
089            public void run() {
090                frame.setCursor(new Cursor(Cursor.WAIT_CURSOR));
091                try {
092                    snapshotCreator.createSnapshot(progressMeter);
093                    progressMeter.snapshotComplete();
094                } catch (ZipException e) {
095                    progressMeter.error(e);
096                } catch (IOException e) {
097                    progressMeter.error(e);
098                }
099                frame.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
100            }
101        }
102    }