View Javadoc

1   /*
2    * $Id: JVotController.java,v 1.2 2004/12/07 01:33:05 jdt Exp $
3    *
4    * Copyright 2003 AstroGrid. All rights reserved.
5    *
6    * This software is published under the terms of the AstroGrid Software License,
7    * a copy of which has been included with this distribution in the LICENSE.txt file.
8    */
9   
10  package org.astrogrid.ui.votable;
11  
12  import java.io.*;
13  
14  import VOTableUtil.Votable;
15  import com.tbf.xml.XmlElement;
16  import com.tbf.xml.XmlParser;
17  import java.awt.event.ActionListener;
18  import java.util.Vector;
19  import javax.swing.JFileChooser;
20  import org.apache.commons.logging.Log;
21  import org.apache.commons.logging.LogFactory;
22  import org.astrogrid.ui.ExtensionFileFilter;
23  
24  /***
25   * A controller for the JVot Panel
26   *
27   * @author mch
28   */
29  
30  public class JVotController implements ActionListener
31  {
32     private JVot votView = null;
33     public static final String SAVEAS_CMD = "SaveAs";
34     public static final String LOAD_CMD = "Load";
35  
36     private JFileChooser chooser = new JFileChooser();
37  
38     public static final ExtensionFileFilter votFileFilter = new ExtensionFileFilter(new String[] {"vot"},"VOTable");
39     public static final ExtensionFileFilter xmlFileFilter = new ExtensionFileFilter(new String[] {"xml"},"XML");
40  
41     Log log = LogFactory.getLog(JVotController.class);
42     
43     public JVotController(JVot view)
44     {
45        this.votView = view;
46        
47        chooser.addChoosableFileFilter(xmlFileFilter);
48        chooser.addChoosableFileFilter(votFileFilter);
49     }
50  
51  
52     public void loadVot(InputStream in) throws IOException
53     {
54           //horrible horrible Javot requiring a particular parser...
55           XmlParser parser = new XmlParser();
56           XmlElement rootNode = parser.parse(new BufferedInputStream(in));
57  
58           if (rootNode == null)
59           {
60              throw new IOException(parser.getLastError());
61           }
62           
63           Votable votable = new Votable(rootNode);
64           throwValidationErrors(votable.getValidationErrors());
65  
66           if (votable.getResourceCount()==0)
67           {
68              throw new IOException("Table has no Resource");
69           }
70  //         Resource resource = votable.getResourceAt(0);
71  //         throwValidationErrors(resource.getValidationErrors());
72  
73  //         Table table = resource.getTableAt(0);
74  //         throwValidationErrors(table.getValidationErrors());
75  
76           votView.setVotModel(votable);
77  
78     }
79  
80     private void throwValidationErrors(Vector errors) throws IOException
81     {
82        if ((errors != null) && (errors.size() > 0))
83        {
84           throw new IOException( ""+errors.elementAt(0)); //just get first for now
85        }
86     }
87  
88     public void saveVot(OutputStream out) throws IOException
89     {
90        votView.getVotModel().toXml(out);
91     }
92  
93  
94     /***
95      * Carries out actions based on button or menu/etc events.  This means that
96      * a UI that contains this component can create controls with the commands
97      * given by this component (eg LOAD_CMD) and set this as the action listener,
98      * eg:
99      * <pre>
100     *     JButton loadButton = new JButton("Load");
101     *     loadButton.setActionCommand(JVot.LOAD_CMD);
102     *     loadButton.addActionListener(votTable);
103     *     toolbar.add(loadButton);
104     * </pre>
105     */
106    public void actionPerformed(java.awt.event.ActionEvent e)
107    {
108       if (e.getActionCommand().equals(SAVEAS_CMD))
109       {
110          if (chooser.showSaveDialog(votView) == chooser.APPROVE_OPTION)
111          {
112             try
113             {
114                saveVot(new FileOutputStream(chooser.getSelectedFile()));
115             }
116             catch (IOException ioe)
117             {
118                log.error("Could not save VOTable to '"+chooser.getSelectedFile()+"'",ioe);
119             }
120          }
121       }
122 
123       if (e.getActionCommand().equals(LOAD_CMD))
124       {
125          if (chooser.showOpenDialog(votView) == chooser.APPROVE_OPTION)
126          {
127             try
128             {
129                loadVot(new FileInputStream(chooser.getSelectedFile()));
130             }
131             catch (IOException ioe)
132             {
133                log.error("Could not load VOTable '"+chooser.getSelectedFile()+"'\n"+
134                             "Are you sure it is a valid VOTable?",
135                             ioe);
136             }
137          }
138       }
139    }
140 
141 
142 
143 
144    /***
145     * See JVotBox for a useful test harness
146     *
147    public static void main(String [] args)
148     /**/
149 
150 }
151 
152 /*
153 $Log: JVotController.java,v $
154 Revision 1.2  2004/12/07 01:33:05  jdt
155 Merge from PAL_Itn07
156 
157 Revision 1.1.116.1  2004/11/30 01:16:54  mch
158 switched to commons logging
159 
160 Revision 1.1  2004/04/15 16:34:53  mch
161 Tidied up, introduced stuff from datacenter ui
162 
163 Revision 1.1  2004/03/03 17:40:58  mch
164 Moved ui package
165 
166 Revision 1.1  2004/02/17 16:04:06  mch
167 New Desktop GUI
168 
169 Revision 1.1  2004/02/15 23:25:30  mch
170 Datacenter and MySpace desktop client GUIs
171 
172  */