1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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 }