1
2
3
4
5
6
7
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
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
71
72
73
74
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));
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172