package jmslexamples; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.IOException; import java.io.OutputStream; import com.softsynth.jmsl.score.Score; import com.softsynth.jmsl.score.ScoreFrame; import com.softsynth.jmsl.score.util.SAPScoreWriter; import com.softsynth.jmsl.score.util.SAPScoreWriterDialog; /** * Export a JScore to a TextArea in San Andreas Press's SCORE file format
* * Enter notes into the score, then select menu Score -> Export -> San Andreas * Score and see output in TextArea * * @author Nick Didkovsky, copyright 2000 Nick Didkovsky */ public class ScoreExportSAPDemo extends java.applet.Applet implements ActionListener { Score score; SAPScoreFrame scoreFrame; int numStaves; public static TextArea textArea; Button visibleButton; public void init() { // JMSL.setIsApplet(true); textArea = new TextArea(20, 80); visibleButton = new Button("Show Score"); visibleButton.addActionListener(this); setLayout(new BorderLayout()); } public void start() { add("North", textArea); Panel p = new Panel(); p.setLayout(new FlowLayout()); p.add(new Label("Your score has been opened in a Frame outside this browser")); p.add(visibleButton); add("South", p); String msg = "Enter some notes in the JScore window, then select Score -> Export San Andreas Press.\n"; msg += "The SCORE output will appear in this TextArea. From here, you can copy and paste the output to Notepad,\n"; msg += "save to disk, and import into SCORE (res inputfile.txt)\n\n"; msg += "The standalone JScore application saves this output to a file directly. Applets (like this one) are not permitted\n"; msg += "to access your local drive for saving, so we use this TextArea instead for output"; textArea.setText(msg); int w = 600; int h = 400; numStaves = 2; score = new Score(numStaves, w, h, "JMSL Score Demo by Nick Didkovsky, copyright 2000 Nick Didkovsky"); score.addMeasure(); scoreFrame = new SAPScoreFrame("JMSL Score Demo by Nick Didkovsky, copyright 2000 Nick Didkovsky"); scoreFrame.addScore(score); scoreFrame.noSound(); score.getScoreLayoutManager().setZoom(0.5); score.getControlPanel().setZoom(0.5); // force netscape to size window ok scoreFrame.setSize(980, 500); // scoreFrame.pack(); scoreFrame.setVisible(true); } public void actionPerformed(ActionEvent e) { scoreFrame.setVisible(true); // scoreFrame.show(); } public ScoreFrame getScoreFrame() { return scoreFrame; } public void stop() { removeAll(); scoreFrame.setVisible(false); scoreFrame.dispose(); } } class SAPScoreFrame extends ScoreFrame { public SAPScoreFrame(String title) { super(title); } public void handleExportScoreMUS() { try { java.io.PrintWriter out = new java.io.PrintWriter(new TextAreaOutputStream()); ScoreExportSAPDemo.textArea.setText(""); SAPScoreWriter scoreWriter = new SAPScoreWriter(getCurrentScore(), out, getTemplateName(), getMusPrefix()); SAPScoreWriterDialog dialog = new SAPScoreWriterDialog((Frame) getComponent(), scoreWriter); dialog.setVisible(true); scoreWriter.write(); out.flush(); out.close(); if (scoreWriter.getGoAhead()) { alert("JScore exported successfully, see TextArea in applet"); setTemplateName(scoreWriter.getTemplateName()); setMusPrefix(scoreWriter.getMusPrefix()); } } catch (java.io.IOException e) { alert("Trouble: " + e); } } } class TextAreaOutputStream extends OutputStream { StringBuffer buf; public TextAreaOutputStream() throws IOException { super(); buf = new StringBuffer(); } public void write(int b) throws IOException { buf.append("" + (char) b); } public void flush() { System.out.println("FLUSH"); ScoreExportSAPDemo.textArea.setText(buf.toString()); } }