View Javadoc

1   /*
2    * $Id: ClasspathPanel.java,v 1.9 2007/07/11 12:31:18 edankert Exp $
3    *
4    * The contents of this file are subject to the Mozilla Public License 
5    * Version 1.1 (the "License"); you may not use this file except in 
6    * compliance with the License. You may obtain a copy of the License at 
7    * http://www.mozilla.org/MPL/ 
8    *
9    * Software distributed under the License is distributed on an "AS IS" basis, 
10   * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
11   * for the specific language governing rights and limitations under the License.
12   *
13   * The Original Code is XML Hammer code. (org.xmlhammer.*)
14   *
15   * The Initial Developer of the Original Code is Edwin Dankert. Portions created 
16   * by the Initial Developer are Copyright (C) 2002 - 2006 the Initial Developer. 
17   * All Rights Reserved.
18   *
19   * Contributor(s): Edwin Dankert <edankert@gmail.com>
20   */
21  
22  package org.xmlhammer.gui.preferences;
23  
24  import java.io.File;
25  
26  import javax.swing.JFileChooser;
27  import javax.swing.JFrame;
28  import javax.swing.table.DefaultTableModel;
29  
30  import org.bounce.DefaultFileFilter;
31  import org.xmlhammer.model.preferences.Preferences.Classpath;
32  import org.xmlhammer.model.preferences.Preferences.Classpath.Jar;
33  
34  /***
35   * This ClassPathPanel is used to ...
36   *
37   * @version $Revision: 1.9 $, $Date: 2007/07/11 12:31:18 $
38   * @author Edwin Dankert <edankert@gmail.com>
39   */
40  public class ClasspathPanel extends PreferencesTablePanel {
41  
42      private static final long serialVersionUID = -405344774107634146L;
43  
44      private JFileChooser chooser = null;
45  
46  	public ClasspathPanel(JFrame parent, String name, String helpID) {
47  		super(parent, name, helpID);
48  	}
49  
50      protected DefaultTableModel getModel() {
51          if ( model == null) {
52              model = new ActivatableTableModel();
53              model.setColumnCount(2);
54              model.setColumnIdentifiers( new String[] {" ", "Jar location"});
55          }
56          
57          return model;
58      }
59  	
60      private JFileChooser getFileChooser() {
61          if ( chooser == null) {
62              chooser = new JFileChooser();
63              chooser.setAcceptAllFileFilterUsed(true);
64              chooser.addChoosableFileFilter(new DefaultFileFilter("jar", "Java Archive"));
65          }
66          
67          return chooser;
68      }
69  
70      public void setClasspath(Classpath classpath) {
71  		while ( getModel().getRowCount() > 0) {
72              getModel().removeRow(0);
73  		}
74  		
75  		for (Jar jar : classpath.getJar()) {
76              getModel().addRow(new Object[] {jar.isActive(), jar.getSrc()});
77  		}
78  	}
79  	
80      protected int[] getColumnWidths() {
81          return new int[] {25, 1000};
82      }
83  
84      public Classpath getClasspath() {
85          Classpath path = new Classpath();
86  
87  		for ( int i = 0; i < getModel().getRowCount(); i++) {
88              Jar jar = new Jar();
89              jar.setActive((Boolean)getModel().getValueAt(i, 0));
90              jar.setSrc((String)getModel().getValueAt(i, 1));
91  
92              path.getJar().add(jar);
93  		}
94  		
95  		return path;
96  	}
97  	
98      protected void addButtonPressed() {
99          
100         if (getModel().getRowCount() > 0) {
101             String selection = (String)getModel().getValueAt(getModel().getRowCount()-1, 1);
102             if ( selection != null && selection.length() > 0) {
103                 getFileChooser().setSelectedFile(new File(selection));
104             }
105         }
106 
107         int value = getFileChooser().showOpenDialog( getFrame());
108 
109         if ( value == JFileChooser.APPROVE_OPTION) {
110             File file = getFileChooser().getSelectedFile();
111 
112             if ( file.exists()) {
113                 getModel().addRow( new Object[] {Boolean.TRUE, file.getAbsolutePath()});
114                 getTable().scrollRectToVisible( getTable().getCellRect( getModel().getRowCount()-1, 1, false));
115             }
116         }
117     }
118 
119     @Override
120     protected Object[] getDefaultRowContent() {
121         return new Object[] {Boolean.TRUE, "New .jar Location"};
122     }
123 
124     @Override
125     protected String getName(int row) {
126         return getModel().getValueAt(row, 1).toString();
127     }
128 }