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.dependencynavigator.implementations.zip; 019 020 import java.awt.event.MouseAdapter; 021 import java.awt.event.MouseEvent; 022 import java.io.IOException; 023 import java.util.ArrayList; 024 import java.util.Enumeration; 025 import java.util.List; 026 import java.util.zip.ZipEntry; 027 import java.util.zip.ZipException; 028 import java.util.zip.ZipFile; 029 030 import javax.swing.BoxLayout; 031 import javax.swing.JDialog; 032 import javax.swing.JFrame; 033 import javax.swing.JList; 034 import javax.swing.JScrollPane; 035 036 import org.jadira.dependencynavigator.implementations.Workspace; 037 import org.jadira.dependencynavigator.implementations.WorkspaceLoader; 038 import org.jadira.dependencynavigator.implementations.zip.ZipFileWorkspace; 039 import org.jadira.dependencynavigator.model.ArtifactInitialisationException; 040 import org.jadira.dependencynavigator.model.RootPom; 041 042 public class ZipFileWorkspaceLoader implements WorkspaceLoader { 043 044 private Workspace workspace; 045 private JFrame frame; 046 047 public void loadPom() { 048 try { 049 String selectedPom = open(); 050 if (selectedPom != null) { 051 zipWorkspace().loadRootPom(selectedPom); 052 } 053 } catch (ZipException e) { 054 e.printStackTrace(); 055 } catch (IOException e) { 056 e.printStackTrace(); 057 } catch (ArtifactInitialisationException e) { 058 e.printStackTrace(); 059 } 060 } 061 062 public RootPom getRoot() { 063 return workspace.getRoot(); 064 } 065 066 public void reloadPom() throws ArtifactInitialisationException { 067 workspace.reloadPomFile(); 068 } 069 070 private ZipFileWorkspace zipWorkspace() { 071 if (workspace instanceof ZipFileWorkspace) { 072 return (ZipFileWorkspace) workspace; 073 } 074 throw new RuntimeException("Expected workspace of type " + ZipFileWorkspace.class.getName() + " not " + workspace.getClass().getName()); 075 } 076 077 private String open() throws ZipException, IOException { 078 final JDialog d = new JDialog(frame, true); 079 final StringBuffer selectedBuffer = new StringBuffer(); 080 d.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE); 081 d.getContentPane().setLayout(new BoxLayout(d.getContentPane(), BoxLayout.PAGE_AXIS)); 082 083 List<String> poms = new ArrayList<String>(); 084 ZipFile zipFile = zipWorkspace().getZipFile(); 085 Enumeration<? extends ZipEntry> zipFileEntries = zipFile.entries(); 086 while (zipFileEntries.hasMoreElements()) { 087 ZipEntry zipEntry = zipFileEntries.nextElement(); 088 if (zipEntry.getName().startsWith(ZipFileWorkspace.PREFIX_WORKSPACE)) { 089 poms.add(zipEntry.getName().substring(ZipFileWorkspace.PREFIX_WORKSPACE.length())); 090 } 091 } 092 JList list = new JList(poms.toArray()); 093 list.addMouseListener(new MouseAdapter() { 094 095 @Override 096 public void mouseClicked(MouseEvent e) { 097 if (e.getClickCount() > 1) { 098 JList l = (JList) e.getSource(); 099 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 }