View Javadoc

1   /*
2    * JarInspector - Copyright (C) 2004 Che Inc., Rosario Argentina
3    *
4    * This program is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Library General Public
6    * License as published by the Free Software Foundation; either
7    * version 2 of the License, or (at your option) any later version.
8    *
9    * This library is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12   * Library General Public License for more details.
13   *
14   * You should have received a copy of the GNU Library General Public
15   * License along with this library; if not, write to the Free
16   * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17   */
18  
19  package inc.che.jarinspector;
20  
21  // Imports
22  
23  import inc.che.common.config.Config;
24  import inc.che.common.gui.GuiUtil;
25  import inc.che.common.jar.JarUtil;
26  import inc.che.common.resource.ResourceManager;
27  
28  import java.awt.BorderLayout;
29  import java.awt.Dimension;
30  import java.awt.GridLayout;
31  import java.awt.event.WindowAdapter;
32  import java.awt.event.WindowEvent;
33  import java.io.File;
34  import java.io.IOException;
35  import java.util.ArrayList;
36  import java.util.Collections;
37  import java.util.Iterator;
38  import java.util.Properties;
39  import java.util.jar.JarFile;
40  import java.util.jar.Manifest;
41  
42  import javax.swing.BorderFactory;
43  import javax.swing.JDialog;
44  import javax.swing.JFrame;
45  import javax.swing.JLabel;
46  import javax.swing.JPanel;
47  import javax.swing.JScrollPane;
48  import javax.swing.JTabbedPane;
49  import javax.swing.JTable;
50  import javax.swing.table.TableModel;
51  
52  import org.apache.log4j.Logger;
53  
54  /***
55   *  <b>@todo</b>
56   * @version $Id: InfoDialog.java,v 1.1 2005/03/06 12:56:57 stevemcmee Exp $
57   * @author <address> Steve McMee &lt;stevemcmee@sourceforge.net&gt; </address>
58   */
59  
60  public class InfoDialog extends JDialog {
61  
62      /*** CVS ID of this file */
63      public static final String CVS_ID =
64          "$Id: InfoDialog.java,v 1.1 2005/03/06 12:56:57 stevemcmee Exp $";
65  
66      /*** logger instance for this class */
67  
68      private static Logger log = Logger.getLogger(InfoDialog.class);
69  
70      /***
71       * configuration instance for this class
72       */
73      private static Config config = Config.getInstance();
74  
75      /***
76      * The ResourceManager
77      */
78      private static ResourceManager resourceManager =
79          ResourceManager.getResourceManager(StringResources.TEXT_RESOURCES);
80  
81      private JTabbedPane tabbedPanel = null;
82  
83      public InfoDialog(JFrame owner) {
84          super(owner);
85          this.setModal(true);
86          this.setResizable(true);
87          tabbedPanel = new JTabbedPane();
88  
89          this.setSize(
90              new Dimension(
91                  config.getIntParameter("infodialog.width", 350),
92                  config.getIntParameter("infodialog.height", 200)));
93          tabbedPanel.add(resourceManager.getText("tab.info"), createInfoTab());
94          tabbedPanel.add(
95              resourceManager.getText("tab.sysinfo"),
96              createSystemInfoTab());
97          tabbedPanel.add(
98              resourceManager.getText("tab.jarinfo"),
99              createJarInfoTab());
100 
101         this.getContentPane().add(tabbedPanel);
102         this.setTitle("Info JarInspector " + VersionInfo.VERSION);
103         this.setDefaultCloseOperation(this.DO_NOTHING_ON_CLOSE);
104         this.addWindowListener(new WindowAdapter() {
105             public void windowClosing(WindowEvent e) {
106                 closeDialog();
107             }
108         });
109         GuiUtil.center(owner, this);
110     }
111 
112     private JPanel createInfoTab() {
113 
114         JPanel panel = new JPanel(new BorderLayout());
115         JPanel mainPanel = new JPanel(new BorderLayout(10, 10));
116         JPanel labelPanel = new JPanel(new GridLayout(4, 1, 10, 10));
117         mainPanel.setBorder(BorderFactory.createEtchedBorder());
118         labelPanel.add(new JLabel("JarInspector"));
119         labelPanel.add(
120             new JLabel(
121                 " Version: "
122                     + VersionInfo.VERSION
123                     + " vom "
124                     + VersionInfo.BUILDDATE));
125         labelPanel.add(new JLabel("(c) 2005 by Che Inc"));
126         mainPanel.add(BorderLayout.WEST, labelPanel);
127         mainPanel.add(
128             BorderLayout.EAST,
129             new JLabel(
130                 resourceManager.getIconByGifPath(StringResources.APP_LOGO)));
131         panel.add(BorderLayout.WEST, labelPanel);
132         panel.add(BorderLayout.EAST, mainPanel);
133 
134         return panel;
135     }
136 
137     private JPanel createSystemInfoTab() {
138         JPanel panel = new JPanel(new BorderLayout());
139         JTable propTable = new JTable(new SystemPropTableModel());
140         propTable.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN);
141         panel.add(BorderLayout.CENTER, new JScrollPane(propTable));
142         return panel;
143     }
144     private JPanel createJarInfoTab() {
145         JPanel panel = new JPanel(new BorderLayout());
146         JTable propTable = new JTable(new JarInfoTableModel());
147         propTable.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN);
148         panel.add(BorderLayout.CENTER, new JScrollPane(propTable));
149         return panel;
150     }
151 
152     protected void closeDialog() {
153         config.setIntParameter("infodialog.height", this.getHeight());
154         config.setIntParameter("infodialog.width", this.getWidth());
155         this.dispose();
156     }
157 }
158 
159 /***
160  *  <b>Modelklasse für Tabelle der Systemvariablen</b>
161  * @version $Id: InfoDialog.java,v 1.1 2005/03/06 12:56:57 stevemcmee Exp $
162  * @author          Stephan Melchior, <address> &lt;stephan.melchior@meta-level.de&gt;</address>
163  */
164 
165 class SystemPropTableModel implements TableModel {
166 
167     /*** CVS ID of this file */
168     public static final String CVS_ID =
169         "$Id: InfoDialog.java,v 1.1 2005/03/06 12:56:57 stevemcmee Exp $";
170 
171     /***
172      * The ResourceManager
173      */
174     private static ResourceManager resourceManager =
175         ResourceManager.getResourceManager(StringResources.TEXT_RESOURCES);
176 
177     private static Logger log = Logger.getLogger(SystemPropTableModel.class);
178 
179     private Properties sysProps = System.getProperties();
180     private ArrayList keys = new ArrayList();
181     private static final String[] TABLE_HEADERS =
182         new String[] {
183             resourceManager.getText("info.table.header.sysprop.key"),
184             resourceManager.getText("info.table.header.sysprop.value")};
185     private String[][] data = null;
186     /***
187      * Konstruktor
188      */
189     public SystemPropTableModel() {
190         for (Iterator it = sysProps.keySet().iterator(); it.hasNext();) {
191             keys.add(it.next());
192         }
193         Collections.sort(keys);
194         data = new String[keys.size()][2];
195     }
196     public void addTableModelListener(javax.swing.event.TableModelListener l) {
197     }
198 
199     /***
200      * Liefert Objekttyp der Spalteneinträge
201      * @param columnIndex Index der Spalte
202      * @return Objekttyp der Spalteneinträge der Spalte columnIndex
203      */
204     public Class getColumnClass(int columnIndex) {
205         return String.class;
206     }
207 
208     /***
209      * liefert Anzahl der Tabellenspalten
210      * @return Anzahl der Tabellenspalten (2)
211      */
212     public int getColumnCount() {
213         return 2;
214     }
215 
216     public String getColumnName(int columnIndex) {
217         return TABLE_HEADERS[columnIndex];
218     }
219 
220     public int getRowCount() {
221         return keys.size();
222     }
223 
224     public Object getValueAt(int rowIndex, int columnIndex) {
225         if (data[rowIndex][columnIndex] == null) {
226             if (log.isDebugEnabled()) {
227                 log.debug("getValueAt(" + rowIndex + ", " + columnIndex + ")");
228             }
229             if (columnIndex == 0) {
230                 data[rowIndex][columnIndex] = (String) keys.get(rowIndex);
231             }
232             if (columnIndex == 1) {
233                 data[rowIndex][columnIndex] =
234                     sysProps.getProperty((String) keys.get(rowIndex));
235             }
236         }
237         return data[rowIndex][columnIndex];
238     }
239 
240     public boolean isCellEditable(int rowIndex, int columnIndex) {
241         return false;
242     }
243 
244     public void removeTableModelListener(
245         javax.swing.event.TableModelListener l) {
246     }
247 
248     public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
249     }
250 
251 } // class SystemPropTableModel
252 
253 /***
254  *  <b>Modelklasse für Tabelle der Systemvariablen</b>
255  * @version $Id: InfoDialog.java,v 1.1 2005/03/06 12:56:57 stevemcmee Exp $
256  * @author          Stephan Melchior, <address> &lt;stephan.melchior@meta-level.de&gt;</address>
257  */
258 
259 class JarInfoTableModel implements TableModel {
260 
261     /*** CVS ID of this file */
262     public static final String CVS_ID =
263         "$Id: InfoDialog.java,v 1.1 2005/03/06 12:56:57 stevemcmee Exp $";
264 
265     /***
266      * The ResourceManager
267      */
268     private static ResourceManager resourceManager =
269         ResourceManager.getResourceManager(StringResources.TEXT_RESOURCES);
270 
271     /*** logger instance for this class */
272 
273     private static Logger log = Logger.getLogger(JarInfoTableModel.class);
274 
275     private JarFile[] jarFiles;
276     private static final String[] TABLE_HEADERS =
277         new String[] {
278             resourceManager.getText("info.table.header.jarfile.key"),
279             resourceManager.getText("info.table.header.jarfile.value")};
280     private String[][] data = null;
281     /***
282      * Konstruktor
283      */
284     public JarInfoTableModel() {
285         try {
286             jarFiles = JarUtil.getAllJarFiles();
287             data = new String[jarFiles.length][2];
288         } catch (Exception ex) {
289             ex.printStackTrace();
290             log.error(ex);
291         }
292     }
293     public void addTableModelListener(javax.swing.event.TableModelListener l) {
294     }
295 
296     /***
297      * Liefert Objekttyp der Spalteneinträge
298      * @param columnIndex Index der Spalte
299      * @return Objekttyp der Spalteneinträge der Spalte columnIndex
300      */
301     public Class getColumnClass(int columnIndex) {
302         return String.class;
303     }
304 
305     /***
306      * liefert Anzahl der Tabellenspalten
307      * @return Anzahl der Tabellenspalten (2)
308      */
309     public int getColumnCount() {
310         return 2;
311     }
312 
313     public String getColumnName(int columnIndex) {
314         return TABLE_HEADERS[columnIndex];
315     }
316 
317     public int getRowCount() {
318         return jarFiles.length;
319     }
320 
321     public Object getValueAt(int rowIndex, int columnIndex) {
322 
323         if (data[rowIndex][columnIndex] == null) {
324             if (log.isDebugEnabled()) {
325                 log.debug("getValueAt(" + rowIndex + ", " + columnIndex + ")");
326             }
327             if (columnIndex == 0) {
328                 data[rowIndex][columnIndex] =
329                     new File(jarFiles[rowIndex].getName()).getName();
330             }
331             if (columnIndex == 1) {
332                 Manifest manifest = null;
333                 try {
334                     manifest = jarFiles[rowIndex].getManifest();
335                 } catch (IOException ex) {
336                     ex.printStackTrace();
337                     log.error(ex);
338                 }
339                 if (manifest == null) {
340                     return resourceManager.getText("no.manifest");
341                 }
342 
343                 String impVersion =
344                     manifest.getMainAttributes().getValue(
345                         "Implementation-Version");
346                 if (impVersion != null) {
347                     data[rowIndex][columnIndex] = impVersion;
348                 } else {
349                     String packVersion =
350                         manifest.getMainAttributes().getValue(
351                             "Package-Version");
352                     if (packVersion != null) {
353                         data[rowIndex][columnIndex] = packVersion;
354                     } else {
355                         data[rowIndex][columnIndex] = "???";
356                     }
357                 }
358             }
359         }
360         return data[rowIndex][columnIndex];
361 
362     }
363 
364     public boolean isCellEditable(int rowIndex, int columnIndex) {
365         return false;
366     }
367 
368     public void removeTableModelListener(
369         javax.swing.event.TableModelListener l) {
370     }
371 
372     public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
373     }
374 
375 } // class JarInfoTableModel