View Javadoc

1   /*
2    * $Id$
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.awt.BorderLayout;
25  import java.io.File;
26  import java.util.List;
27  
28  import javax.swing.JCheckBox;
29  import javax.swing.JFileChooser;
30  import javax.swing.JFrame;
31  import javax.swing.JPanel;
32  import javax.swing.border.EmptyBorder;
33  import javax.swing.table.DefaultTableModel;
34  
35  import org.bounce.DefaultFileFilter;
36  import org.xmlhammer.model.preferences.Preferences.Catalogs;
37  import org.xmlhammer.model.preferences.Preferences.Catalogs.Catalog;
38  
39  /***
40   * This ClassPathPanel is used to ...
41   *
42   * @version $Revision$, $Date$
43   * @author Edwin Dankert <edankert@gmail.com>
44   */
45  public class CatalogsPanel extends PreferencesTablePanel {
46  
47      private static final long serialVersionUID = -6856643909200549168L;
48  
49      private JFileChooser chooser = null;
50      private JCheckBox preferPublicIdentifiersCheck = null;
51  
52  	public CatalogsPanel(JFrame parent, String name, String helpID) {
53  		super( parent, name, helpID);
54          preferPublicIdentifiersCheck = new JCheckBox("Prefer Public Identifiers");
55          preferPublicIdentifiersCheck.setBorder(new EmptyBorder(10, 10, 20, 10));
56  
57          JPanel panel = new JPanel(new BorderLayout());
58          panel.add(preferPublicIdentifiersCheck, BorderLayout.NORTH);
59          panel.add(getCenterPane(), BorderLayout.CENTER);
60          setCenterPane(panel);
61  	}
62  
63      protected DefaultTableModel getModel() {
64          if ( model == null) {
65              model = new ActivatableTableModel();
66              model.setColumnCount(2);
67              model.setColumnIdentifiers( new String[] {" ", "Catalog location"});
68          }
69          
70          return model;
71      }
72  	
73      private JFileChooser getFileChooser() {
74          if ( chooser == null) {
75              chooser = new JFileChooser();
76              chooser.setAcceptAllFileFilterUsed( true);
77              chooser.addChoosableFileFilter(new DefaultFileFilter( "cat,xcat", "XML Catalogs"));
78              chooser.addChoosableFileFilter(new DefaultFileFilter( "xml", "XML Documents"));
79          }
80          
81          return chooser;
82      }
83  
84      public void setCatalogs(Catalogs catalogs) {
85          List<Catalog> list = catalogs.getCatalog();
86          preferPublicIdentifiersCheck.setSelected(catalogs.isPreferPublicIdentifiers());
87  
88          while (getModel().getRowCount() > 0) {
89              getModel().removeRow(0);
90  		}
91  		
92  		for (Catalog catalog : list) {
93              getModel().addRow(new Object[] {catalog.isActive(), catalog.getSrc()});
94  		}
95  	}
96  	
97      protected int[] getColumnWidths() {
98          return new int[] {25, 1000};
99      }
100 
101     public Catalogs getCatalogs() {
102         Catalogs catalogs = new Catalogs();
103         catalogs.setPreferPublicIdentifiers(preferPublicIdentifiersCheck.isSelected());
104         
105 		for ( int i = 0; i < getModel().getRowCount(); i++) {
106             Catalog catalog = new Catalog();
107             catalog.setActive((Boolean)getModel().getValueAt(i, 0));
108             catalog.setSrc((String)getModel().getValueAt(i, 1));
109             catalogs.getCatalog().add(catalog);
110 		}
111         
112 		return catalogs;
113 	}
114 	
115     protected void addButtonPressed() {
116         
117         if (getModel().getRowCount() > 0) {
118             String selection = (String)getModel().getValueAt(getModel().getRowCount()-1, 1);
119             if ( selection != null && selection.length() > 0) {
120                 getFileChooser().setSelectedFile(new File(selection));
121             }
122         }
123 
124         int value = getFileChooser().showOpenDialog( getFrame());
125 
126         if ( value == JFileChooser.APPROVE_OPTION) {
127             File file = getFileChooser().getSelectedFile();
128 
129             if ( file.exists()) {
130                 getModel().addRow( new Object[] {Boolean.TRUE, file.getAbsolutePath()});
131                 getTable().scrollRectToVisible( getTable().getCellRect( getModel().getRowCount()-1, 1, false));
132             }
133         }
134     }
135 
136     @Override
137     protected Object[] getDefaultRowContent() {
138         return new Object[] {Boolean.TRUE, "New Catalog Location"};
139     }
140 
141     @Override
142     protected String getName(int row) {
143         return getModel().getValueAt(row, 1).toString();
144     }
145 }