[jmsl] EditStateProvider bug?

jmsl at music.columbia.edu jmsl at music.columbia.edu
Sun Jun 22 10:41:56 EDT 2008


Good eye Peter; this took me some time to trace through, and I am not 
sure how this crept in. 
Currently ScoreEditPanel calls  editStateChanged(EditStateProvider esp) 
on its EditStateListeners when the active track index changes.
The listener is ScoreCanvas which makes the static change to 
Staff.setActiveTrackIndex()
EditManager references this static condition instead of asking its 
EditStateProvider what the active track index is (which it ought to, as 
you describe below).
I'll make the change but I need to make sure this indirection does not 
have unexpected side effects elsewhere, so please give me a minute to 
scrutinize!
Thanks very much for pointing this out,
Nick Didkovsky

jmsl at music.columbia.edu wrote:
> Hi all,
>
> I've extended EditStateProvider by adding setter methods.   It seems 
> that EditStateProvider's getActiveTrackIndex() is not observed by the 
> EditManager.  It looks like Staff.getActiveTrackIndex() is being 
> called instead.
>
> To demonstrate:
> Run ActiveTrackComboBox, then click to add notes.  It should be 
> inputting eighth notes in track 1, but instead it is one track 0 (try 
> inserting notes between notes)
>
> Changing the ActiveTrackIndex through the static 
> Staff.setActiveTrackIndex(trackNum) works (it's commented in the below 
> code), but I'd like to be able to have different ActiveTrackIndex 
> values with multiple scores open.  Is ScoreEditPanel setting the 
> static variable?
>
> thanks,
> Peter McCulloch
>
> -------------ActiveTrackComboBox----------------
> package com.petermcculloch.megalo.gui.viewer;
>
> import java.awt.BorderLayout;
> import java.awt.event.ItemEvent;
> import java.awt.event.ItemListener;
> import java.awt.event.WindowAdapter;
> import java.awt.event.WindowEvent;
>
> import javax.swing.DefaultComboBoxModel;
> import javax.swing.JComboBox;
> import javax.swing.JFrame;
> import javax.swing.JOptionPane;
>
> import com.petermcculloch.megalo.MegaloEditStateProvider;
> import com.softsynth.jmsl.JMSL;
> import com.softsynth.jmsl.score.EditManager;
> import com.softsynth.jmsl.score.Note;
> import com.softsynth.jmsl.score.NoteFactory;
> import com.softsynth.jmsl.score.NumTracksPerStaffChangedListener;
> import com.softsynth.jmsl.score.Score;
> import com.softsynth.jmsl.score.Staff;
>
> public class ActiveTrackComboBox extends JComboBox implements 
> NumTracksPerStaffChangedListener {
>
>     Score                    score;
>
>     MegaloEditStateProvider    mesp;
>
>     public ActiveTrackComboBox(Score score, MegaloEditStateProvider 
> mesp) {
>         this.score = score;
>         this.mesp = mesp;
>         configureComboBox(score);
>
>     }
>
>     /**
>      *@paramscore
>      */
>     private void configureComboBox(Score score) {
>         String[] names = new String[score.getNumTracksPerStaff()];
>
>         for (int i = 0; i < names.length; i++) {
>             names[i] = "Track " + (i + 1);
>         }
>
>         this.setModel(new DefaultComboBoxModel(names));
>
>         
> this.setSelectedIndex(score.getEditManager().getEditStateProvider().getActiveTrackIndex()); 
>
>
>         this.addItemListener(new ItemListener() {
>
>             @Override
>             public void itemStateChanged(ItemEvent e) {
>                 updateTrackNumber(((JComboBox) 
> e.getSource()).getSelectedIndex());
>             }
>
>         });
>     }
>
>     private void updateTrackNumber(int trackNum) {
>         System.out.println("Setting to track number: " + trackNum);
>
>         // This works but is just using the static access method to 
> Staff.setActiveTrackIndex(trackNum)
> //        for (int i = 0; i < score.getNumStaves(); i++) {
> //            
> score.getMeasure(0).getStaff(i).setActiveTrackIndex(trackNum);
> //        }
>
>         mesp.setActiveTrackIndex(trackNum);
>
>         // ComboBox only works if this is uncommented!
>         // Staff.setActiveTrackIndex(trackNum);
>
>     }
>
>     /**
>      *@paramargs
>      */
>     public static void main(String[] args) {
>         JFrame frame = new JFrame();
>
>         Score score = new Score(2, 900, 300);
>         score.addMeasures(5, 3, 4);
>
>         MegaloEditStateProvider mesp = new MegaloEditStateProvider();
>
>         // // remove old editManager
>         
> score.getScoreCanvas().removeScoreCanvasListener(score.getEditManager());
>         // // add new editManager
>         score.getScoreCanvas().addScoreCanvasListener(new 
> EditManager(score, mesp));
>
>         score.getEditManager().setEditStateProvider(mesp);
>
>         mesp.setActiveTrackIndex(0);
>
>         // Verify that we have the correct EditStateProvider and Track
>         System.out.println("EditStateProvider:\t"
>             + score.getEditManager().getEditStateProvider().toString() 
> + "\tActive Track PRE:\t"
>             + 
> score.getEditManager().getEditStateProvider().getActiveTrackIndex());
>
>         for (int i = 0; i < 8; i++) {
>             score.addNote(NoteFactory.makeNote(1, 60 + (i * 2), 1., 
> 0.8));
>         }
>
>         // We should now be inputting EighthNotes on Track 1
>         mesp.setActiveTrackIndex(1);
>         mesp.setDuration(Note.EIGHTH);
>
>         // Verify that we have the correct EditStateProvider and Track
>         System.out.println("EditStateProvider:\t"
>             + score.getEditManager().getEditStateProvider().toString() 
> + "\tActive Track POST:\t"
>             + 
> score.getEditManager().getEditStateProvider().getActiveTrackIndex());
>
>         frame.setLayout(new BorderLayout());
>
>         ActiveTrackComboBox actb = new ActiveTrackComboBox(score, mesp);
>         
> frame.getContentPane().add(score.getScoreCanvas().getComponent(), 
> BorderLayout.CENTER);
>         frame.getContentPane().add(actb, BorderLayout.SOUTH);
>
>         frame.addWindowStateListener(new WindowAdapter() {
>             @Override
>             public void windowClosed(WindowEvent e) {
>                 JMSL.closeMusicDevices();
>                 System.exit(0);
>             }
>         });
>         frame.pack();
>         frame.setVisible(true);
>
>     }
>
>     @Override
>     public void notifyNumTracksPerStaff(Score score) {
>         configureComboBox(score);
>     }
>
> }
>
>
>
> -------------MegaloEditStateProvider-------------
>
> package com.petermcculloch.megalo;
>
> import java.util.prefs.BackingStoreException;
> import java.util.prefs.Preferences;
>
> import javax.swing.JFrame;
> import javax.swing.JOptionPane;
>
> import com.softsynth.jmsl.score.EditStateProvider;
> import com.softsynth.jmsl.score.Note;
>
> /**
>  * @author peter This class provides an implementation of 
> EditStateProvider that
>  *         is also editable.
>  */
> public class MegaloEditStateProvider implements EditStateProvider {
>
>     private static final String    ACCIDENTAL            = "accidental";
>     private static final String    ACTIVE_TRACK_INDEX    = 
> "activeTrackIndex";
>     private static final String    DURATION            = "duration";
>     private static final String    DOTS                = "dots";
>     private static final String    EDIT_MODE            = "editMode";
>     private static final String    NOTE_ELSE_REST        = 
> "noteElseRest";
>     private static final String    TUPLET                = "tuplet";
>
>     private static final String    TO_STRING            = 
> "MegaloEditStateProvider";
>
>     int                            accidental, activeTrackIndex, dots, 
> duration, editMode, tuplet;
>     boolean                        noteElseRest;
>
>     Preferences                    prefs                =
>                                                         
> Preferences.userNodeForPackage(this
>                                                             .getClass());
>
>     public MegaloEditStateProvider() {
>         accidental = prefs.getInt(ACCIDENTAL, Note.ACC_NATURAL);
>         activeTrackIndex = prefs.getInt(ACTIVE_TRACK_INDEX, 0);
>         dots = prefs.getInt(DOTS, 0);
>         duration = prefs.getInt(DURATION, Note.QUARTER);
>         editMode = prefs.getInt(EDIT_MODE, 1);
>         tuplet = prefs.getInt(TUPLET, 0);
>         noteElseRest = prefs.getBoolean(NOTE_ELSE_REST, true);
>     }
>
>     public int getAccidental() {
>         return accidental;
>     }
>
>     public void setAccidental(int accidental) {
>         this.accidental = accidental;
>         prefs.putInt(ACCIDENTAL, accidental);
>         flush();
>     }
>
>     public int getActiveTrackIndex() {
>         System.out.println("getActiveTrackIndex called!");
>         return activeTrackIndex;
>     }
>
>     public void setActiveTrackIndex(int activeTrackIndex) {
>         this.activeTrackIndex = activeTrackIndex;
>
>     }
>
>     public int getDots() {
>         return dots;
>     }
>
>     public void setDots(int dots) {
>         this.dots = dots;
>         prefs.putInt(DOTS, dots);
>         flush();
>     }
>
>     public int getDuration() {
>         return duration;
>     }
>
>     public void setDuration(int duration) {
>         this.duration = duration;
>         prefs.putInt(DURATION, duration);
>         flush();
>     }
>
>     public int getEditMode() {
>         return editMode;
>     }
>
>     public void setEditMode(int editMode) {
>         this.editMode = editMode;
>         prefs.putInt(EDIT_MODE, editMode);
>         flush();
>     }
>
>     public int getTuplet() {
>         return tuplet;
>     }
>
>     public void setTuplet(int tuplet) {
>         this.tuplet = tuplet;
>         prefs.putInt(TUPLET, tuplet);
>         flush();
>     }
>
>     public boolean isNoteElseRest() {
>         return noteElseRest;
>     }
>
>     public boolean getNoteElseRest() {
>         return isNoteElseRest();
>     }
>
>     public void setNoteElseRest(boolean noteElseRest) {
>         this.noteElseRest = noteElseRest;
>         prefs.putBoolean(NOTE_ELSE_REST, noteElseRest);
>     }
>
>     public Preferences getPrefs() {
>         return prefs;
>     }
>
>     public void setPrefs(Preferences prefs) {
>         this.prefs = prefs;
>     }
>
>     public void flush() {
>         try {
>             prefs.flush();
>         } catch (BackingStoreException e) {
>             JOptionPane.showMessageDialog(new JFrame(), "Error: No 
> backing store");
>             e.printStackTrace();
>         }
>
>     }
>
>     public String toString() {
>         return TO_STRING;
>     }
>
> }
>
> _______________________________________________
> jmsl mailing list
> jmsl at music.columbia.edu
> http://music.columbia.edu/mailman/listinfo/jmsl


More information about the jmsl mailing list