* @author Einar Pehrson
*/
-public class MooNote extends MidiEvent implements Cloneable {
+public class MooNote extends MidiEvent implements Cloneable, Comparable {
private MidiEvent noteOffEvent;
private ShortMessage noteOnMsg, noteOffMsg;
new MidiEvent((ShortMessage)noteOffEvent.getMessage().clone(), noteOffEvent.getTick())
);
}
+
+ /**
+ * Compares this note to another note.
+ * @return a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object
+ */
+ public int compareTo(Object o) {
+ return (int)(((MidiEvent)o).getTick() - getTick());
+ }
+
}
* @version 1
*/
-public class MooNoteElement extends JPanel {
+public class MooNoteElement extends JPanel implements Comparable{
private MooTrackView mtv;
private MooNote note;
return note;
}
+ /**
+ * Compares the note of this element to that of another note.
+ * @return a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object
+ */
+ public int compareTo(Object o) {
+ return note.compareTo(((MooNoteElement)o).getNote());
+ }
+
/**
* Selects the current NoteElement.
*/
}
public void mouseReleased(MouseEvent e) {
- if (!maybeShowPopup(e) && !mouseIn) mtv.maybeMoveSelectedNotes(getY(), getY() + e.getY());
+ if (!maybeShowPopup(e) && !mouseIn) {
+ int y = e.getY();
+ if (y < 0) mtv.maybeMoveSelectedNotes((int)Math.floor((double)y / MooTrackView.NOTE_HEIGHT) * MooTrackView.NOTE_HEIGHT);
+ if (y > getHeight()) mtv.maybeMoveSelectedNotes((int)Math.ceil(((double)y - getHeight()) / MooTrackView.NOTE_HEIGHT) * MooTrackView.NOTE_HEIGHT);
+
+ }
}
/**
private JMenuItem popupAdd, popupPaste;
private JMenuItem selPopupCopy, selPopupCut, selPopupRemove, selPopupTranspUpOct, selPopupTranspDownOct;
- private ArrayList coords, selection, copyBuffer;
+ private ArrayList coords, copyBuffer;
+ private TreeSet selection;
private Insets insets;
private int ticksPerSixteenth, popupY = 0;
private boolean leftMouseButtonPressed = false;
// Creates instance variables
insets = getInsets();
coords = new ArrayList(track.size() / 2);
- selection = new ArrayList();
+ selection = new TreeSet();
copyBuffer = new ArrayList();
// Creates temporary variables
* @param the note to deselect
*/
public void deselectNote(MooNoteElement elem) {
- selection.remove(selection.indexOf(elem));
+ selection.remove(elem);
}
/**
while(it.hasNext()) {
copyBuffer.add(((MooNoteElement)it.next()).getNote().clone());
}
- Collections.sort(copyBuffer, new Moosique.NoteComparator());
+ Collections.sort(copyBuffer);
}
/**
}
/**
- * Moves the current selection, if the mouse was pressed on a note element
- * and then released in another row.
- * @param srcY the y-coordinate of the point in which the mouse button was pressed
- * @param destY the y-coordinate of the point in which the mouse button was released
+ * Moves the current selection the given number of ticks.
+ * @param ticks the number of ticks to move the selection.
*/
- public void maybeMoveSelectedNotes(int srcY, int destY) {
- int srcRow = (srcY - insets.top) / NOTE_HEIGHT;
- int destRow = (destY - insets.top) / NOTE_HEIGHT;
- long timeDiff = (long)(ticksPerSixteenth * (destRow - srcRow));
+ public void moveSelectedNotes(int ticks) {
Iterator it = selection.iterator();
while(it.hasNext()) {
MooNoteElement elem = (MooNoteElement)it.next();
- elem.getNote().setTick(elem.getNote().getTick() + timeDiff);
+ elem.getNote().setTick(elem.getNote().getTick() + ticks);
layoutElement(elem, true);
}
}
+ /**
+ * Moves the current selection, depending on the given delta y.
+ * @param y the number of pixels the selection is moved.
+ */
+ public void maybeMoveSelectedNotes(int y) {
+ moveSelectedNotes(ticksPerSixteenth * (y / NOTE_HEIGHT));
+ }
+
/**
* Shows a popup-menu with options for the current selection of note elements.
* @param c the component over which to display the menu
if (noteOns.size() == 0) emptyTracks.add(tracks[i]);
// Sorts the note lists by tick position.
- Comparator c = new NoteComparator();
+ Comparator c = new MidiEventComparator();
Collections.sort(noteOns, c);
Collections.sort(noteOffs, c);
/**
* A Comparator for sorting lists of MidiEvents.
*/
- public static class NoteComparator implements Comparator {
+ public static class MidiEventComparator implements Comparator {
public int compare(Object o1, Object o2) {
return (int)(((MidiEvent)o1).getTick() - ((MidiEvent)o2).getTick());
}