001/*
002 * Copyright (c) 2010 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: crawl $'
006 * '$Date: 2012-09-11 22:16:15 +0000 (Tue, 11 Sep 2012) $' 
007 * '$Revision: 30633 $'
008 * 
009 * Permission is hereby granted, without written agreement and without
010 * license or royalty fees, to use, copy, modify, and distribute this
011 * software and its documentation for any purpose, provided that the above
012 * copyright notice and the following two paragraphs appear in all copies
013 * of this software.
014 *
015 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
016 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
017 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
018 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
019 * SUCH DAMAGE.
020 *
021 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
022 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
023 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
024 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
025 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
026 * ENHANCEMENTS, OR MODIFICATIONS.
027 *
028 */
029
030/**
031 * 
032 */
033package org.kepler.gui.kar;
034
035import java.awt.BorderLayout;
036import java.awt.Color;
037import java.awt.HeadlessException;
038import java.awt.event.ActionEvent;
039import java.awt.event.ActionListener;
040import java.io.InputStream;
041import java.io.InputStreamReader;
042import java.io.Reader;
043import java.util.jar.JarFile;
044import java.util.zip.ZipEntry;
045
046import javax.swing.BorderFactory;
047import javax.swing.JButton;
048import javax.swing.JFrame;
049import javax.swing.JLabel;
050import javax.swing.JPanel;
051import javax.swing.JScrollPane;
052import javax.swing.JTextArea;
053import javax.swing.JTextField;
054
055import org.apache.commons.logging.Log;
056import org.apache.commons.logging.LogFactory;
057import org.kepler.kar.KARFile;
058import org.kepler.objectmanager.lsid.KeplerLSID;
059
060/**
061 * @author Aaron Schultz
062 * 
063 */
064public class KarManifestViewer extends JFrame implements ActionListener {
065        private static final long serialVersionUID = -739762970203187753L;
066        private static final Log log = LogFactory.getLog(KarManifestViewer.class
067                        .getName());
068        private static final boolean isDebugging = log.isDebugEnabled();
069
070        private KARFile _karFile;
071        private JTextField _lsidField;
072        private JTextArea _manifestField;
073        private JScrollPane _manifestScrollPane;
074
075        /**
076         * @throws HeadlessException
077         */
078        public KarManifestViewer() throws HeadlessException {
079                this("KAR Manifest Viewer");
080        }
081
082        /**
083         * @param title
084         * @throws HeadlessException
085         */
086        public KarManifestViewer(String title) throws HeadlessException {
087                super(title);
088        }
089
090        public void initialize(KARFile karFile) {
091                if (isDebugging) {
092                        log.debug("initialize( " + karFile + " )");
093                }
094                _karFile = karFile;
095
096                JPanel layoutPanel = new JPanel();
097                layoutPanel.setLayout(new BorderLayout());
098
099                JPanel lsidPanel = new JPanel();
100                lsidPanel.setLayout(new BorderLayout());
101
102                JLabel lsidLabel = new JLabel("KAR LSID:");
103                lsidPanel.add(lsidLabel, BorderLayout.WEST);
104
105                _lsidField = new JTextField("");
106                _lsidField.setEditable(false);
107                lsidPanel.add(_lsidField, BorderLayout.CENTER);
108
109                JPanel dfPanel = new JPanel();
110                dfPanel.setLayout(new BorderLayout());
111
112                _manifestField = new JTextArea("");
113                _manifestField.setEditable(false);
114                _manifestScrollPane = new JScrollPane(_manifestField);
115                _manifestScrollPane
116                                .setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
117                _manifestScrollPane.setBorder(BorderFactory.createCompoundBorder(
118                                BorderFactory.createCompoundBorder(BorderFactory
119                                                .createTitledBorder("Manifest"), BorderFactory
120                                                .createEmptyBorder(5, 5, 5, 5)), _manifestScrollPane
121                                                .getBorder()));
122
123                dfPanel.add(_manifestScrollPane, BorderLayout.CENTER);
124
125                JPanel buttonPanel = new JPanel();
126                buttonPanel.setLayout(new BorderLayout());
127
128                String infStr = _karFile.getName();
129                JLabel objInfo = new JLabel(infStr);
130                buttonPanel.add(objInfo, BorderLayout.CENTER);
131
132                JButton refreshButton = new JButton("Refresh");
133                refreshButton.addActionListener(this);
134                buttonPanel.add(refreshButton, BorderLayout.EAST);
135
136                layoutPanel.add(lsidPanel, BorderLayout.NORTH);
137                layoutPanel.add(dfPanel, BorderLayout.CENTER);
138                layoutPanel.add(buttonPanel, BorderLayout.SOUTH);
139
140                getContentPane().add(layoutPanel);
141
142                refreshValues();
143
144                getContentPane().setBackground(Color.WHITE);
145        }
146
147        /**
148         * Update the textfield and textarea to reflect the current LSID and LSID
149         * referral list values.
150         */
151        public void refreshValues() {
152
153                KeplerLSID lsid = _karFile.getLSID();
154
155                StringBuilder manifest = new StringBuilder();
156                try {
157                        ZipEntry mane = _karFile.getEntry(JarFile.MANIFEST_NAME);
158
159                        final char[] buffer = new char[0x10000];
160                        InputStream is = _karFile.getInputStream(mane);
161                    Reader in = null;
162                    try {
163                        in = new InputStreamReader(is);
164                        int read;
165                        do {
166                                read = in.read(buffer, 0, buffer.length);
167                                if (read > 0) {
168                                        manifest.append(buffer, 0, read);
169                                }
170                        } while (read >= 0);
171                    } finally {
172                        if(in != null) {
173                            in.close();
174                        }
175                    }
176                } catch (Exception e) {
177                    e.printStackTrace();
178                }
179
180                if (isDebugging) {
181                        log.debug(lsid.toString());
182                        log.debug(manifest.toString());
183                }
184                _lsidField.setText(lsid.toString());
185                _manifestField.setText(manifest.toString());
186
187                // not sure why this doesn't work
188                // _manifestScrollPane.getVerticalScrollBar().setValue(0);
189        }
190
191        /*
192         * (non-Javadoc)
193         * 
194         * @see
195         * java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
196         */
197        public void actionPerformed(ActionEvent e) {
198                Object o = e.getSource();
199                if (o instanceof JButton) {
200                        JButton b = (JButton) o;
201                        if (b.getText().equals("Refresh")) {
202                                log.debug("Refresh");
203                                refreshValues();
204                        }
205                }
206        }
207
208}