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.dependencynavigator.implementations.zip;
19  
20  import java.awt.event.MouseAdapter;
21  import java.awt.event.MouseEvent;
22  import java.io.IOException;
23  import java.util.ArrayList;
24  import java.util.Enumeration;
25  import java.util.List;
26  import java.util.zip.ZipEntry;
27  import java.util.zip.ZipException;
28  import java.util.zip.ZipFile;
29  
30  import javax.swing.BoxLayout;
31  import javax.swing.JDialog;
32  import javax.swing.JFrame;
33  import javax.swing.JList;
34  import javax.swing.JScrollPane;
35  
36  import org.jadira.dependencynavigator.implementations.Workspace;
37  import org.jadira.dependencynavigator.implementations.WorkspaceLoader;
38  import org.jadira.dependencynavigator.implementations.zip.ZipFileWorkspace;
39  import org.jadira.dependencynavigator.model.ArtifactInitialisationException;
40  import org.jadira.dependencynavigator.model.RootPom;
41  
42  public class ZipFileWorkspaceLoader implements WorkspaceLoader {
43  
44      private Workspace workspace;
45      private JFrame frame;
46  
47      public void loadPom() {
48          try {
49              String selectedPom = open();
50              if (selectedPom != null) {
51                  zipWorkspace().loadRootPom(selectedPom);
52              }
53          } catch (ZipException e) {
54              e.printStackTrace();
55          } catch (IOException e) {
56              e.printStackTrace();
57          } catch (ArtifactInitialisationException e) {
58              e.printStackTrace();
59          }
60      }
61  
62      public RootPom getRoot() {
63          return workspace.getRoot();
64      }
65  
66      public void reloadPom() throws ArtifactInitialisationException {
67          workspace.reloadPomFile();
68      }
69  
70      private ZipFileWorkspace zipWorkspace() {
71          if (workspace instanceof ZipFileWorkspace) {
72              return (ZipFileWorkspace) workspace;
73          }
74          throw new RuntimeException("Expected workspace of type " + ZipFileWorkspace.class.getName() + " not " + workspace.getClass().getName());
75      }
76  
77      private String open() throws ZipException, IOException {
78          final JDialog d = new JDialog(frame, true);
79          final StringBuffer selectedBuffer = new StringBuffer();
80          d.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
81          d.getContentPane().setLayout(new BoxLayout(d.getContentPane(), BoxLayout.PAGE_AXIS));
82  
83          List<String> poms = new ArrayList<String>();
84          ZipFile zipFile = zipWorkspace().getZipFile();
85          Enumeration<? extends ZipEntry> zipFileEntries = zipFile.entries();
86          while (zipFileEntries.hasMoreElements()) {
87              ZipEntry zipEntry = zipFileEntries.nextElement();
88              if (zipEntry.getName().startsWith(ZipFileWorkspace.PREFIX_WORKSPACE)) {
89                  poms.add(zipEntry.getName().substring(ZipFileWorkspace.PREFIX_WORKSPACE.length()));
90              }
91          }
92          JList list = new JList(poms.toArray());
93          list.addMouseListener(new MouseAdapter() {
94  
95              @Override
96              public void mouseClicked(MouseEvent e) {
97                  if (e.getClickCount() > 1) {
98                      JList l = (JList) e.getSource();
99                      selectedBuffer.append((String) l.getSelectedValue());
100                     d.dispose();
101                 }
102             }
103 
104         });
105 
106         d.getContentPane().add(new JScrollPane(list));
107         d.pack();
108         d.setVisible(true);
109         String selectedPom = selectedBuffer.toString().trim();
110         if (selectedPom.length() == 0) {
111             return null;
112         } else {
113             return selectedPom;
114         }
115     }
116 }