+public class MooTrackView extends JPanel {
+
+ private Track track;
+ private MooTrackTitle title;
+ private MooKeyboard keyboard;
+
+ private JPopupMenu popup, selPopup;
+ private JMenu popupAdd, selPopupTranspUp, selPopupTranspDown;
+ private JMenuItem popupAddItemsCustom, popupAddItemsLast, popupPaste;
+ private JMenuItem popupAddItemsWhole, popupAddItemsHalf, popupAddItemsQuarter, popupAddItemsEighth, popupAddItemsSixteenth;
+ private JMenuItem selPopupProps, selPopupCopy, selPopupCut, selPopupRemove;
+ private JMenuItem[] selPopupTranspUpItems, selPopupTranspDownItems;
+
+ private ArrayList coords;
+ private Insets insets;
+ private Rectangle box;
+ private int ticksPerSixteenth, popupY = 0, lastNoteLength = 2;
+ private boolean leftMouseButtonPressed = false;
+ private static boolean snapToSixteenths = true;
+ private static int viewLength = 0;
+ private static int extraHeight = 0;
+ public static final int NOTE_HEIGHT = 10, NOTE_WIDTH = 40, VIEW_WIDTH = 200;
+
+ /**
+ * Creates the trackview.
+ * @param track The track it represents graphically and operates on.
+ * @param title The object that is used to manipulate instrument, channel, solo, mute.
+ */
+ public MooTrackView (Track track, MooTrackTitle title) {
+ super(true);
+
+ // Defines instance variables
+ this.track = track;
+ this.title = title;
+ ticksPerSixteenth = Moosique.getSequence().getResolution() / 4;
+ insets = getInsets();
+ coords = new ArrayList(track.size() / 2);
+
+ // Configures panel
+ setBackground(Color.white);
+ setBorder(BorderFactory.createLineBorder(Color.black));
+ setLayout(null);
+ setPreferredSize(new Dimension(VIEW_WIDTH, 140 * NOTE_HEIGHT));
+
+ // Creates temporary variables
+ MidiEvent note;
+ MooNoteElement elem;
+ extraHeight = Toolkit.getDefaultToolkit().getScreenSize().height - 150;
+
+ // Places note elements
+ for (int i = 0; i < track.size(); i++) {
+ note = track.get(i);
+ if (note instanceof MooNote) {
+ // Adds the note element to the note area and moves it to the appropriate place.
+ MooNote mn = (MooNote)note;
+ elem = new MooNoteElement(this, mn);
+ add(elem);
+ layoutElement(elem, false);
+ }
+ }
+ setPreferredSize(new Dimension(VIEW_WIDTH, viewLength + extraHeight));
+
+ // Creates panel pop-up menu.
+ popup = new JPopupMenu();
+ popupAdd = new JMenu("Add note");
+ popup.add(popupAdd);
+ popupAddItemsCustom = addMenuItem(popupAdd, "Custom...");
+ popupAddItemsLast = addMenuItem(popupAdd, "As last added");
+ popupAdd.addSeparator();
+ popupAddItemsWhole = addMenuItem(popupAdd, "Whole");
+ popupAddItemsHalf = addMenuItem(popupAdd, "Half");
+ popupAddItemsQuarter = addMenuItem(popupAdd, "Quarter");
+ popupAddItemsEighth = addMenuItem(popupAdd, "Eighth");
+ popupAddItemsSixteenth = addMenuItem(popupAdd, "Sixteenth");
+ popupPaste = addMenuItem(popup, "Paste");
+
+ // Creates selection pop-up menu.
+ selPopup = new JPopupMenu();
+ selPopupProps = addMenuItem(selPopup, "Properties...");
+ selPopupCopy = addMenuItem(selPopup, "Copy selection");
+ selPopupCut = addMenuItem(selPopup, "Cut selection");
+ selPopupRemove = addMenuItem(selPopup, "Remove selection");
+ selPopupTranspUpItems = new JMenuItem[12];
+ selPopupTranspDownItems = new JMenuItem[12];
+ selPopupTranspUp = createTransposeMenu(selPopup, selPopupTranspUpItems, "selection up");
+ selPopupTranspDown = createTransposeMenu(selPopup, selPopupTranspDownItems, "selection down");
+
+ // Adds listeners for popup menu and keyboard synthesizer.
+ addMouseListener(new MAdapter());
+ keyboard = new MooKeyboard(title);
+ addKeyListener(keyboard);
+ }
+
+ /**
+ * Creates note elements for all MooNotes in the given list, and places them in the appropriate place.
+ */
+ public void placeNewNotes(java.util.List notes) {
+ // Creates temporary variables
+ MidiEvent note;
+ MooNoteElement elem;
+ extraHeight = Toolkit.getDefaultToolkit().getScreenSize().height - 150;
+
+ // Places note elements
+ for (int i = 0; i < notes.size(); i++) {
+ note = (MidiEvent)notes.get(i);
+ if (note instanceof MooNote) {
+ // Adds the note element to the note area and moves it to the appropriate place.
+ MooNote mn = (MooNote)note;
+ elem = new MooNoteElement(this, mn);
+ add(elem);
+ layoutElement(elem, false);
+ }
+ }
+ setPreferredSize(new Dimension(VIEW_WIDTH, viewLength + extraHeight));
+ }
+
+ /**
+ * Layouts the element to the right place.
+ * @param elem the element that will be layouted.
+ * @param old If true, this method will remove the old layout and set the new preferredSize for the trackview.
+ */
+ public void layoutElement(MooNoteElement elem, boolean old){
+ // If the element is currently in the view, removes its coordinates from the list.
+ Rectangle r = new Rectangle();
+ if (old){
+ r = elem.getBounds(r);
+ for (Iterator i = coords.iterator(); i.hasNext();){
+ Object ob = i.next();
+ if (r.equals(ob)){
+ coords.remove(ob);
+ break;
+ }
+ }
+ }
+
+ // Creates temporary variables.
+ MooNote mn = elem.getNote();
+ int x, y, height;
+
+ // Calculates coordinates.
+ x = insets.left;
+ y = insets.top + (int)((mn.getTick() * NOTE_HEIGHT) / ticksPerSixteenth);
+ height = (mn.getDuration() * NOTE_HEIGHT) / ticksPerSixteenth;
+ if (height == 0) height = NOTE_HEIGHT;
+ if (snapToSixteenths && height < NOTE_HEIGHT) height = NOTE_HEIGHT;
+ r = new Rectangle(x, y, NOTE_WIDTH, height);
+
+ // Places the element in the appropriate place.
+ while(isOccupied(r)) r.translate(NOTE_WIDTH, 0);
+ elem.setBounds(r);
+ coords.add(r);
+ if (viewLength < (y + height)){
+ viewLength = y + height;
+ if(old)setPreferredSize(new Dimension(VIEW_WIDTH, viewLength + extraHeight));
+ }
+ if (old) {
+ validate();
+ repaint();
+ }
+ }
+
+ /**
+ * Returns the track of this view.
+ * @return the track of this view
+ */
+ public Track getTrack() {
+ return track;
+ }
+
+ /**
+ * Returns the title of this view.
+ * @return the title of this view
+ */
+ public MooTrackTitle getTitle() {
+ return title;
+ }
+
+ /**
+ * Checks if the element can be fully drawn as this position without inteferring with other elements.
+ * @return true if the position is occupied.
+ */
+ private boolean isOccupied(Rectangle r) {
+ Iterator it = coords.iterator();
+ while (it.hasNext()) {
+ if(r.intersects((Rectangle)it.next())) return true;
+ }
+ return false;
+ }
+
+ /**
+ * Adds the given note to the current track, and visualises it.
+ * @param mn the note to add
+ */
+ public void addNote(MooNote mn) {
+ mn.addTo(track);
+ MooNoteElement elem = new MooNoteElement(this, mn);
+ add(elem);
+ layoutElement(elem, false);
+ setPreferredSize(new Dimension(VIEW_WIDTH, viewLength + extraHeight));
+ Moosique.setEdited();
+ validate();
+ repaint();
+ }
+
+ /**
+ * Adds a standard note to this track.
+ */
+ private void addNoteAtClickY(int length) {
+ lastNoteLength = length;
+ int row = (popupY - insets.top) / NOTE_HEIGHT;
+ long timestamp = (long)(ticksPerSixteenth * row);
+ addNote(new MooNote(title.getChannel(), 60, 100, timestamp, ticksPerSixteenth * length));
+ }
+
+ /**
+ * Removes the given note element from the view and its note from the current track.
+ * @param elem the note element to remove
+ */
+ public void removeNote(MooNoteElement elem) {
+ elem.getNote().removeFrom(track);
+ remove(elem);
+ Rectangle r = new Rectangle();
+ r = elem.getBounds(r);
+ coords.remove(r);
+ Moosique.setEdited();
+ validate();
+ repaint();
+ }
+
+ /**
+ * Copies the current selection.
+ */
+ public void copySelectedNotes() {
+ TreeSet selection = Moosique.getSelection();
+ ArrayList copyBuffer = new ArrayList(selection.size());
+ Iterator it = selection.iterator();
+ while(it.hasNext()) {
+ copyBuffer.add(((MooNoteElement)it.next()).getNote().clone());
+ }
+ Collections.sort(copyBuffer);
+ Moosique.setCopyBuffer(copyBuffer);
+ }
+
+ /**
+ * Cuts the current selection.