Source code: jmx/util/Read.java
1 /*
2
3 <This Java Class is part of the jMusic API version 1.0,Sun Feb 25 18:35:25 2001
4
5 Copyright (C) 2000 Andrew Sorensen & Andrew Brown
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or any
10 later version.
11
12 This program is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20
21 */
22
23 package jmx.util;
24
25 import java.awt.Component;
26 import java.io.File;
27 import java.io.FileInputStream;
28 import java.io.FileOutputStream;
29 import java.io.IOException;
30 import java.io.ObjectInputStream;
31 import java.io.ObjectOutputStream;
32 import java.io.Serializable;
33 import java.io.StreamCorruptedException;
34 import javax.swing.JOptionPane;
35
36 import jm.midi.SMF;
37 import jm.music.data.*;
38 import jm.JMC;
39 import jm.gui.show.*;
40 import jm.gui.cpn.*;
41 import jm.gui.sketch.*;
42 import jm.audio.Instrument;
43 import jm.audio.Audio;
44
45 /**
46 * Supplements the {@link jm.util.Read} class, by providing methods while output
47 * errors to Swing dialog boxes.
48 *
49 * @author Unascribed, Adam Kirby
50 * @version 0.0.0.1, 27th February 2001
51 */
52 public class Read extends jm.util.Read implements JMC{
53
54 //--------- Simultaneous midi and jm reading with error messaging --------//
55
56 /**
57 * Returns a Score read from a MIDI or JM file, displaying errors in a
58 * {@link javax.swing.JDialog}.
59 *
60 * @param file File to read
61 * @param owner Component whose control is to be suspended while the error
62 * messages are displayed
63 * @return Score read from file, or null if an error occured
64 *
65 * @see #midiOrJmWithSwingMessaging(String, String, Component)
66 */
67 public static Score midiOrJmWithSwingMessaging(final File file,
68 final Component owner) {
69 JmMidiProcessor processor = new JmMidiProcessor(file);
70 displayErrorJDialog(owner, processor.getMessage());
71 return processor.getScore();
72 }
73
74 /**
75 * Returns a Score read from a MIDI or JM file, displaying errors in a
76 * {@link javax.swing.JDialog}.
77 *
78 * <P>The path of the file is separated into <CODE>directory</CODE> and
79 * <CODE>filename</CODE> so that the latter can be used as the title of the
80 * score. If <CODE>directory</CODE> is null then this method attempts
81 * to read the file specified by <CODE>filename</CODE>.
82 *
83 * @param directory String describing the directory structure of the file to
84 * be read, which must include the terminating separator
85 * @param filename String describing the file name
86 * @param owner Component whose control is to be suspended while the
87 * error messages are displayed
88 * @return Score read from file, or null if an error occured
89 *
90 * @see #midiOrJmWithSwingMessaging(File, Component)
91 */
92 public static Score midiOrJmWithSwingMessaging(final String directory,
93 final String filename,
94 final Component owner) {
95 JmMidiProcessor processor = new JmMidiProcessor(directory, filename);
96 displayErrorJDialog(owner, processor.getMessage());
97 return processor.getScore();
98 }
99
100 /**
101 * Displays an error message in a {@link javax.swing.JDialog}. The message displayed is
102 * retrieved from the class variable {@link #message}. This method is
103 * designed for use by the {@link #midiOrWithSwingMessaging()} methods.
104 *
105 * @param owner Component whose control is to be suspended while the
106 * error messages are displayed
107 * @param message String to display
108 *
109 * @see #midiOrJmWithSwingMessaging(File, Component);
110 * @see #midiOrJmWithSwingMessaging(String, String, Component);
111 */
112 private static void displayErrorJDialog(final Component owner,
113 final String message) {
114 if (message == null) {
115 return;
116 }
117 JOptionPane.showMessageDialog(owner, message,
118 "Not a valid MIDI or jMusic File",
119 JOptionPane.ERROR_MESSAGE);
120 }
121 }
122