1 | /* |
---|
2 | * $Id: PanelModelEditor.java,v 1.2 2003/08/22 23:02:35 dwd Exp $ |
---|
3 | * |
---|
4 | * This software is provided by NOAA for full, free and open release. It is |
---|
5 | * understood by the recipient/user that NOAA assumes no liability for any |
---|
6 | * errors contained in the code. Although this software is released without |
---|
7 | * conditions or restrictions in its use, it is expected that appropriate |
---|
8 | * credit be given to its author and to the National Oceanic and Atmospheric |
---|
9 | * Administration should the software be included by the recipient as an |
---|
10 | * element in other product development. |
---|
11 | */ |
---|
12 | |
---|
13 | package gov.noaa.pmel.sgt.beans; |
---|
14 | |
---|
15 | import java.awt.*; |
---|
16 | import java.io.InvalidObjectException; |
---|
17 | import java.io.File; |
---|
18 | import java.io.FileNotFoundException; |
---|
19 | import java.io.FileOutputStream; |
---|
20 | import java.io.BufferedOutputStream; |
---|
21 | import java.io.BufferedInputStream; |
---|
22 | import java.io.FileInputStream; |
---|
23 | import javax.swing.*; |
---|
24 | import javax.swing.filechooser.*; |
---|
25 | import java.awt.event.*; |
---|
26 | |
---|
27 | import gov.noaa.pmel.util.SimpleFileFilter; |
---|
28 | |
---|
29 | /** |
---|
30 | * Run-time and stand-alone editor to modify a <code>PanelModel</code>. |
---|
31 | * |
---|
32 | * @author Donald Denbo |
---|
33 | * @version $Revision: 1.2 $, $Date: 2003/08/22 23:02:35 $ |
---|
34 | * @since 3.0 |
---|
35 | * @see PanelModelCustomizer |
---|
36 | **/ |
---|
37 | public class PanelModelEditor extends JFrame { |
---|
38 | private PanelModel panelModel_ = null; |
---|
39 | private static boolean isApp_ = false; |
---|
40 | private boolean throwClosing = true; |
---|
41 | private String openPath_ = null; |
---|
42 | private static String title_ = "Panel Model Editor"; |
---|
43 | // |
---|
44 | private JFileChooser fileChooser_ = null; |
---|
45 | private JPanel pmcPanel = new JPanel(); |
---|
46 | private JPanel buttonPanel = new JPanel(); |
---|
47 | private JMenuBar menuBar = new JMenuBar(); |
---|
48 | private JButton okButton = new JButton(); |
---|
49 | private PanelModelCustomizer pmc = new PanelModelCustomizer(); |
---|
50 | private BorderLayout borderLayout1 = new BorderLayout(); |
---|
51 | private JMenu fileMenu = new JMenu(); |
---|
52 | private JMenuItem openFileMI = new JMenuItem(); |
---|
53 | private JMenuItem saveFileMI = new JMenuItem(); |
---|
54 | private JMenuItem exitFileMI = new JMenuItem(); |
---|
55 | private JMenuItem saveAsFileMI = new JMenuItem(); |
---|
56 | private JMenuItem newFileMI = new JMenuItem(); |
---|
57 | |
---|
58 | /** |
---|
59 | * <code>PanelModelEditor</code> constructor. |
---|
60 | * @param pModel PanelModel |
---|
61 | */ |
---|
62 | public PanelModelEditor(PanelModel pModel) { |
---|
63 | this(); |
---|
64 | setPanelModel(pModel); |
---|
65 | } |
---|
66 | |
---|
67 | /** |
---|
68 | * Default constructor. |
---|
69 | */ |
---|
70 | public PanelModelEditor() { |
---|
71 | try { |
---|
72 | jbInit(); |
---|
73 | pack(); |
---|
74 | } |
---|
75 | catch(Exception e) { |
---|
76 | e.printStackTrace(); |
---|
77 | } |
---|
78 | } |
---|
79 | |
---|
80 | /** |
---|
81 | * Main method for stand-alone application to create/modify <code>PanelModel</code>s |
---|
82 | * that have been serialized using <code>XMLEncoder</code>. |
---|
83 | * @param args Arguement String |
---|
84 | * @see java.beans.XMLEncoder |
---|
85 | */ |
---|
86 | public static void main(String[] args) { |
---|
87 | isApp_ = true; |
---|
88 | PanelModelEditor pme = new PanelModelEditor(new PanelModel()); |
---|
89 | pme.setTitle(title_); |
---|
90 | pme.setVisible(true); |
---|
91 | } |
---|
92 | |
---|
93 | private void jbInit() throws Exception { |
---|
94 | okButton.setText("OK"); |
---|
95 | okButton.addActionListener(new java.awt.event.ActionListener() { |
---|
96 | public void actionPerformed(ActionEvent e) { |
---|
97 | okButton_actionPerformed(e); |
---|
98 | } |
---|
99 | }); |
---|
100 | pmcPanel.setLayout(borderLayout1); |
---|
101 | fileMenu.setText("File"); |
---|
102 | openFileMI.setActionCommand("Open"); |
---|
103 | openFileMI.setText("Open..."); |
---|
104 | openFileMI.addActionListener(new java.awt.event.ActionListener() { |
---|
105 | public void actionPerformed(ActionEvent e) { |
---|
106 | openFileMI_actionPerformed(e); |
---|
107 | } |
---|
108 | }); |
---|
109 | saveFileMI.setActionCommand("Save"); |
---|
110 | saveFileMI.setText("Save"); |
---|
111 | saveFileMI.addActionListener(new java.awt.event.ActionListener() { |
---|
112 | public void actionPerformed(ActionEvent e) { |
---|
113 | saveFileMI_actionPerformed(e); |
---|
114 | } |
---|
115 | }); |
---|
116 | exitFileMI.setText("Exit"); |
---|
117 | exitFileMI.addActionListener(new java.awt.event.ActionListener() { |
---|
118 | public void actionPerformed(ActionEvent e) { |
---|
119 | exitFileMI_actionPerformed(e); |
---|
120 | } |
---|
121 | }); |
---|
122 | saveAsFileMI.setActionCommand("SaveAs"); |
---|
123 | saveAsFileMI.setText("Save As..."); |
---|
124 | saveAsFileMI.addActionListener(new java.awt.event.ActionListener() { |
---|
125 | public void actionPerformed(ActionEvent e) { |
---|
126 | saveAsFileMI_actionPerformed(e); |
---|
127 | } |
---|
128 | }); |
---|
129 | if(isApp_)this.setJMenuBar(menuBar); |
---|
130 | newFileMI.setText("New"); |
---|
131 | newFileMI.addActionListener(new java.awt.event.ActionListener() { |
---|
132 | public void actionPerformed(ActionEvent e) { |
---|
133 | newFileMI_actionPerformed(e); |
---|
134 | } |
---|
135 | }); |
---|
136 | this.getContentPane().add(pmcPanel, BorderLayout.CENTER); |
---|
137 | if(!isApp_) this.getContentPane().add(buttonPanel, BorderLayout.SOUTH); |
---|
138 | buttonPanel.add(okButton, null); |
---|
139 | pmcPanel.add(pmc, BorderLayout.CENTER); |
---|
140 | menuBar.add(fileMenu); |
---|
141 | fileMenu.add(newFileMI); |
---|
142 | fileMenu.addSeparator(); |
---|
143 | fileMenu.add(openFileMI); |
---|
144 | fileMenu.add(saveFileMI); |
---|
145 | fileMenu.add(saveAsFileMI); |
---|
146 | fileMenu.addSeparator(); |
---|
147 | fileMenu.add(exitFileMI); |
---|
148 | |
---|
149 | addWindowListener(new java.awt.event.WindowAdapter() { |
---|
150 | public void windowClosing(WindowEvent e) { |
---|
151 | if(throwClosing) pmf_windowClosing(e); |
---|
152 | } |
---|
153 | }); |
---|
154 | Dimension dim = pmc.getPreferredSize(); |
---|
155 | setSize(new Dimension(473, 354)); |
---|
156 | //Center the window |
---|
157 | Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); |
---|
158 | Dimension frameSize = getSize(); |
---|
159 | if (frameSize.height > screenSize.height) { |
---|
160 | frameSize.height = screenSize.height; |
---|
161 | } |
---|
162 | if (frameSize.width > screenSize.width) { |
---|
163 | frameSize.width = screenSize.width; |
---|
164 | } |
---|
165 | setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2); |
---|
166 | } |
---|
167 | |
---|
168 | void pmf_windowClosing(WindowEvent e) { |
---|
169 | if(Page.DEBUG) System.out.println("windowClosing"); |
---|
170 | throwClosing = false; |
---|
171 | setVisible(false); |
---|
172 | if(panelModel_ != null) panelModel_.setBatch(false); |
---|
173 | dispose(); |
---|
174 | if(isApp_) System.exit(0); |
---|
175 | } |
---|
176 | |
---|
177 | void okButton_actionPerformed(ActionEvent e) { |
---|
178 | throwClosing = false; |
---|
179 | pmf_windowClosing(null); |
---|
180 | } |
---|
181 | |
---|
182 | /** |
---|
183 | * Get <code>PanelModel</code>. |
---|
184 | * @return PanelModel |
---|
185 | */ |
---|
186 | public PanelModel getPanelModel() { |
---|
187 | return panelModel_; |
---|
188 | } |
---|
189 | |
---|
190 | /** |
---|
191 | * Set <code>PanelModel</code>. |
---|
192 | * @param panelModel PanelModel |
---|
193 | */ |
---|
194 | public void setPanelModel(PanelModel panelModel) { |
---|
195 | panelModel_ = panelModel; |
---|
196 | if(panelModel_ != null) { |
---|
197 | panelModel_.setBatch(true); |
---|
198 | pmc.setObject(panelModel_); |
---|
199 | } |
---|
200 | } |
---|
201 | |
---|
202 | void openFileMI_actionPerformed(ActionEvent e) { |
---|
203 | PanelModel pModel = null; |
---|
204 | File file; |
---|
205 | String ext; |
---|
206 | String[] xmlFile = new String[]{".xml"}; |
---|
207 | SimpleFileFilter xmlFilt = new SimpleFileFilter(xmlFile, "XML Serialized Bean"); |
---|
208 | |
---|
209 | if(fileChooser_ == null) { |
---|
210 | fileChooser_ = new JFileChooser(); |
---|
211 | } |
---|
212 | fileChooser_.setFileFilter(fileChooser_.getAcceptAllFileFilter()); |
---|
213 | fileChooser_.resetChoosableFileFilters(); |
---|
214 | fileChooser_.addChoosableFileFilter(xmlFilt); |
---|
215 | fileChooser_.setFileFilter(xmlFilt); |
---|
216 | |
---|
217 | int result = fileChooser_.showOpenDialog(this); |
---|
218 | if(result == JFileChooser.APPROVE_OPTION) { |
---|
219 | file = fileChooser_.getSelectedFile(); |
---|
220 | openPath_ = file.getPath(); |
---|
221 | setTitle(title_ + ": " + openPath_); |
---|
222 | try { |
---|
223 | pModel = PanelModel.loadFromXML(new BufferedInputStream( |
---|
224 | new FileInputStream(openPath_))); |
---|
225 | setPanelModel(pModel); |
---|
226 | } catch (FileNotFoundException fnfe) { |
---|
227 | JOptionPane.showMessageDialog(this, "Error openning file", |
---|
228 | "File Open Error", JOptionPane.ERROR_MESSAGE); |
---|
229 | } catch (InvalidObjectException ioe) { |
---|
230 | JOptionPane.showMessageDialog(this, "File does not contain a PanelModel", |
---|
231 | "PanelModel Not Found", |
---|
232 | JOptionPane.ERROR_MESSAGE); |
---|
233 | } |
---|
234 | } |
---|
235 | } |
---|
236 | |
---|
237 | void saveAsFileMI_actionPerformed(ActionEvent e) { |
---|
238 | File file; |
---|
239 | FileFilter filt; |
---|
240 | String ext; |
---|
241 | String outpath; |
---|
242 | String[] xmlFile = new String[]{".xml"}; |
---|
243 | SimpleFileFilter xmlFilt = new SimpleFileFilter(xmlFile, "XML Serialized Bean"); |
---|
244 | |
---|
245 | if(fileChooser_ == null) { |
---|
246 | fileChooser_ = new JFileChooser(); |
---|
247 | } |
---|
248 | fileChooser_.setFileFilter(fileChooser_.getAcceptAllFileFilter()); |
---|
249 | fileChooser_.resetChoosableFileFilters(); |
---|
250 | fileChooser_.addChoosableFileFilter(xmlFilt); |
---|
251 | fileChooser_.setFileFilter(xmlFilt); |
---|
252 | |
---|
253 | int result = fileChooser_.showSaveDialog(this); |
---|
254 | if(result == JFileChooser.APPROVE_OPTION) { |
---|
255 | file = fileChooser_.getSelectedFile(); |
---|
256 | filt = fileChooser_.getFileFilter(); |
---|
257 | outpath = file.getPath(); |
---|
258 | if(filt instanceof SimpleFileFilter) { |
---|
259 | ext = ((SimpleFileFilter)filt).getExtension(); |
---|
260 | String name = file.getName().toLowerCase(); |
---|
261 | if(!name.endsWith(ext)) { |
---|
262 | outpath += ext; |
---|
263 | } |
---|
264 | } |
---|
265 | if(new File(outpath).exists()) { |
---|
266 | JOptionPane.showMessageDialog(this, "File already exists, rename and try again", |
---|
267 | "File Save Error", JOptionPane.ERROR_MESSAGE); |
---|
268 | return; |
---|
269 | } |
---|
270 | try { |
---|
271 | panelModel_.saveToXML(new BufferedOutputStream( |
---|
272 | new FileOutputStream(outpath, false))); |
---|
273 | } catch (FileNotFoundException fnfe) { |
---|
274 | JOptionPane.showMessageDialog(this, "Error creating file, rename and try again", |
---|
275 | "File Save Error", JOptionPane.ERROR_MESSAGE); |
---|
276 | return; |
---|
277 | } |
---|
278 | } |
---|
279 | } |
---|
280 | |
---|
281 | void saveFileMI_actionPerformed(ActionEvent e) { |
---|
282 | if(openPath_ == null) return; |
---|
283 | try { |
---|
284 | panelModel_.saveToXML(new BufferedOutputStream( |
---|
285 | new FileOutputStream(openPath_, false))); |
---|
286 | } catch (FileNotFoundException fnfe) { |
---|
287 | JOptionPane.showMessageDialog(this, "Error creating file, rename and try again", |
---|
288 | "File Save Error", JOptionPane.ERROR_MESSAGE); |
---|
289 | return; |
---|
290 | } |
---|
291 | } |
---|
292 | |
---|
293 | void exitFileMI_actionPerformed(ActionEvent e) { |
---|
294 | throwClosing = false; |
---|
295 | pmf_windowClosing(null); |
---|
296 | } |
---|
297 | |
---|
298 | void newFileMI_actionPerformed(ActionEvent e) { |
---|
299 | setPanelModel(new PanelModel()); |
---|
300 | } |
---|
301 | } |
---|