import java.awt.*;
import java.awt.event.*;
import javax.sound.midi.*;
+import java.util.*;
/**
* Graphical representation of a MIDI track.
* @version 1
*/
-public class MooTrackView extends JPanel implements ActionListener {
+public class MooTrackView extends JPanel {
private Track track;
private MooTrackTitle title;
- private NoteArea notes;
+ 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 JPopupMenu popup;
- private JMenuItem menuItem;
- protected static int viewLength = 0;
-
+ 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;
- public MooTrackView (Track track) {
+ /**
+ * 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;
- //setPreferredSize(new Dimension(200, 9000));
- setLayout(new BorderLayout());
- this.setBorder(BorderFactory.createLineBorder(Color.black));
+ 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));
- title = new MooTrackTitle(track);
- title.setBorder(BorderFactory.createLineBorder(Color.black));
- add(title, BorderLayout.NORTH);
+ // Creates temporary variables
+ MidiEvent note;
+ MooNoteElement elem;
+ extraHeight = Toolkit.getDefaultToolkit().getScreenSize().height - 150;
- notes = new NoteArea(track);
- notes.setBackground(Color.white);
- notes.setBorder(BorderFactory.createLineBorder(Color.black));
+ // 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();
- menuItem = new JMenuItem("Add...");
- menuItem.addActionListener(this);
- popup.add(menuItem);
- menuItem = new JMenuItem("Preferences...");
- menuItem.addActionListener(this);
- popup.add(menuItem);
+ 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");
- notes.addMouseListener(new PopupListener());
- add(notes, BorderLayout.CENTER);
+ // Adds listeners for popup menu and keyboard synthesizer.
+ addMouseListener(new MAdapter());
+ keyboard = new MooKeyboard(title);
+ addKeyListener(keyboard);
}
- public void actionPerformed(ActionEvent e) {}
+ /**
+ * 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));
+ }
/**
- * Updates the track view.
+ * Removes the given note element from the view and its note from the current track.
+ * @param elem the note element to remove
*/
- public void update() {
+ 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();
}
- class NoteArea extends JPanel {
- public static final int NOTE_HEIGHT = 10;
- public static final int NOTE_WIDTH = 40;
- private int trackLength;
-
- public NoteArea(Track track) {
- System.out.println("Creating track view...");
- setLayout(null);
- trackLength = 140;
- setPreferredSize(new Dimension(200,140*NOTE_HEIGHT));
- MidiEvent note;
- MooNoteElement elem;
- boolean isOccupied;
- int x, y, height;
- Insets insets = getInsets();
- for (int i = 0; i < track.size(); i++) {
- note = track.get(i);
- if (note instanceof MooNote) {
- // Adds the note element to the note area.
- MooNote mn = (MooNote)note;
- elem = new MooNoteElement(mn);
- add(elem);
-
- // Places the note element in the appropriate place.
- x = insets.left;
- y = insets.top + (int)(mn.getTick() / 24) * NOTE_HEIGHT;
- height = NOTE_HEIGHT;
- // height = (mn.getDuration() / 24) * NOTE_HEIGHT;
- // System.out.println("Comp at: " + x + ", " + y + " is: " + findComponentAt(x + 10, y + 10));
- while(findComponentAt(x, y) instanceof MooNoteElement ||
- findComponentAt(x, y + height - 1) instanceof MooNoteElement) x += NOTE_WIDTH;
- elem.setBounds(x, y, NOTE_WIDTH, height);
- if (viewLength < (y+height)) viewLength = y+height;
- }
- setPreferredSize(new Dimension(200,viewLength));
+ /**
+ * 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.
+ */
+ public void cutSelectedNotes() {
+ copySelectedNotes();
+ removeSelectedNotes();
+ }
+
+ /**
+ * Pastes the current copy buffer at the given timestamp.
+ */
+ public void pasteCopiedNotes() {
+ int row = (popupY - insets.top) / NOTE_HEIGHT;
+ long timestamp = (long)(ticksPerSixteenth * row);
+ ArrayList copyBuffer = Moosique.getCopyBuffer();
+ if (copyBuffer.size() > 0) {
+ long startTime = ((MooNote)copyBuffer.get(0)).getTick();
+ Iterator it = copyBuffer.iterator();
+ while(it.hasNext()) {
+ MooNote mn = (MooNote)((MooNote)it.next()).clone();
+ mn.setTick(mn.getTick() - startTime + timestamp);
+ mn.setChannel(title.getChannel());
+ addNote(mn);
}
- validate();
+ Moosique.setEdited();
}
+ }
- public void paintComponent(Graphics g) {
- super.paintComponent(g);
- Graphics2D g2 = (Graphics2D)g;
-//(trackLength*NOTE_HEIGHT)
- for (int c = 0; c < viewLength ; c += NOTE_HEIGHT) {
- for (int r = 0; r < (10*NOTE_WIDTH); r += NOTE_WIDTH) {
- box = new Rectangle(r, c, NOTE_WIDTH, NOTE_HEIGHT);
- g2.setColor(Color.gray);
- g2.draw(box);
- }
+ /**
+ * Removes the current selection.
+ */
+ public void removeSelectedNotes() {
+ TreeSet selection = Moosique.getSelection();
+ Iterator it = selection.iterator();
+ while(it.hasNext()) {
+ removeNote((MooNoteElement)it.next());
+ }
+ selection.clear();
+ Moosique.setEdited();
+ }
+
+ /**
+ * Transposes all selected notes the given number of halftones.
+ */
+ private void transposeSelectedNotes(int halftones) {
+ TreeSet selection = Moosique.getSelection();
+ Iterator it = selection.iterator();
+ while(it.hasNext()) {
+ MooNoteElement elem = (MooNoteElement)it.next();
+ elem.transpose(halftones);
+ }
+ Moosique.setEdited();
+ }
+
+ /**
+ * Moves the current selection the given number of ticks.
+ * @param ticks the number of ticks to move the selection.
+ */
+ public void moveSelectedNotes(int ticks) {
+ TreeSet selection = Moosique.getSelection();
+ if (ticks < 0) {
+ // If the selection should be moved upwards, traverses the list in the natural order.
+ Iterator it = selection.iterator();
+ while(it.hasNext()) {
+ MooNoteElement elem = (MooNoteElement)it.next();
+ elem.getNote().setTick(elem.getNote().getTick() + ticks);
+ layoutElement(elem, true);
+ }
+ } else {
+ // If the selection should be moved downwards, traverses the list in the opposite order.
+ ArrayList selectedList = new ArrayList(selection);
+ ListIterator it = selectedList.listIterator(selectedList.size());
+ while(it.hasPrevious()) {
+ MooNoteElement elem = (MooNoteElement)it.previous();
+ elem.getNote().setTick(elem.getNote().getTick() + ticks);
+ layoutElement(elem, true);
+ }
+ }
+ Moosique.setEdited();
+ }
+
+ /**
+ * Enables keyboard recording.
+ */
+ public void enableKeyboardRecording() {
+ keyboard.recordEnable();
+ }
+
+ /**
+ * Disables keyboard recording.
+ */
+ public void disableKeyboardRecording() {
+ keyboard.recordDisable();
+ }
+
+ /**
+ * Adds a menu item with the given command to the given popup menu.
+ */
+ private JMenuItem addMenuItem(JPopupMenu menu, String command) {
+ JMenuItem item = new JMenuItem(command);
+ item.addActionListener(new PopupListener());
+ menu.add(item);
+ return item;
+ }
+
+ /**
+ * Adds a menu item with the given command to the given menu.
+ */
+ private JMenuItem addMenuItem(JMenu menu, String command) {
+ JMenuItem item = new JMenuItem(command);
+ item.addActionListener(new PopupListener());
+ menu.add(item);
+ return item;
+ }
+
+ /**
+ * Creates a transpose sub menu with the given title in the given popup menu,
+ * inserting the items into the given array.
+ */
+ private JMenu createTransposeMenu(JPopupMenu menu, JMenuItem[] items, String title) {
+ JMenu trans = new JMenu("Transpose " + title);
+ menu.add(trans);
+ items[0] = addMenuItem(trans, "One octave");
+ for (int i = 1; i < 12; i++) {
+ items[i] = addMenuItem(trans, (i) + " halftones");
+ }
+ return trans;
+ }
+
+ /**
+ * Shows a popup-menu with options for the current selection of note elements.
+ * @param c the component over which to display the menu
+ * @param x the x-coordinate in which to display the menu
+ * @param y the y-coordinate in which to display the menu
+ */
+ public void showSelectionPopup(Component c, int x, int y) {
+ // Determines whether the "Properties" item should be available.
+ selPopupProps.setEnabled(Moosique.getSelection().size() == 1);
+ selPopup.show(c, x, y);
+ }
+
+ /**
+ * Draws the grid that is on the background.
+ * @param g The Graphics object used to draw the grid.
+ */
+ public void paintComponent(Graphics g) {
+ super.paintComponent(g);
+ Graphics2D g2 = (Graphics2D)g;
+ for (int c = 0; c < viewLength || c < getHeight(); c += NOTE_HEIGHT) {
+ for (int r = 0; r < (10 * NOTE_WIDTH); r += NOTE_WIDTH) {
+ box = new Rectangle(r, c, NOTE_WIDTH, NOTE_HEIGHT);
+ g2.setColor(Color.gray);
+ g2.draw(box);
}
}
}
+
+ /**
+ * Returns whether the left mouse button is currently pressed or not.
+ * @return true if the left mosue button is currently pressed
+ */
+ public boolean isLeftMouseButtonPressed() {
+ return leftMouseButtonPressed;
+ }
+
+ /**
+ * The adapter used to listen on mouse actions
+ */
+ class MAdapter extends MouseAdapter {
- class PopupListener extends MouseAdapter {
+ /**
+ * Deselects all note on click, adds a standard note on double click.
+ */
+ public void mouseClicked(MouseEvent e) {
+ if (SwingUtilities.isLeftMouseButton(e)) {
+ Moosique.deselectAllNotes();
+ if (e.getClickCount() == 2) {
+ popupY = e.getY();
+ addNoteAtClickY(lastNoteLength);
+ }
+ }
+ }
+
public void mousePressed(MouseEvent e) {
+ if (SwingUtilities.isLeftMouseButton(e)) leftMouseButtonPressed = true;
maybeShowPopup(e);
}
public void mouseReleased(MouseEvent e) {
+ if (SwingUtilities.isLeftMouseButton(e)) leftMouseButtonPressed = false;
maybeShowPopup(e);
}
+ /**
+ * Shows the menu if an OS-specific popup-trigger was activated.
+ */
private void maybeShowPopup(MouseEvent e) {
if (e.isPopupTrigger()) {
+ popupY = e.getY();
popup.show(e.getComponent(), e.getX(), e.getY());
}
}
+
+ /**
+ * Grabs the focus when the mouse has entered.
+ */
+ public void mouseEntered(MouseEvent e) {
+ Moosique.setActiveChannel(title.getChannel());
+ grabFocus();
+ }
+ }
+
+ /**
+ * Takes the appropriate action when a user selects an item on the popup menu.
+ */
+ class PopupListener implements ActionListener {
+ public void actionPerformed(ActionEvent e) {
+ Object source = e.getSource();
+ // Handling panel popup actions.
+ if (source == popupAddItemsLast) {
+ addNoteAtClickY(lastNoteLength);
+ } else if (source == popupAddItemsCustom) {
+ /* Show the user a dialog (identical to note preferences...
+ then call addNote(new MooNote(
+ title.getChannel(), pitch, velocity, timestamp, duration));
+ */
+ } else if (source == popupAddItemsWhole) {
+ addNoteAtClickY(16);
+ } else if (source == popupAddItemsHalf) {
+ addNoteAtClickY(8);
+ } else if (source == popupAddItemsQuarter) {
+ addNoteAtClickY(4);
+ } else if (source == popupAddItemsEighth) {
+ addNoteAtClickY(2);
+ } else if (source == popupAddItemsSixteenth) {
+ addNoteAtClickY(1);
+ } else if (source == popupPaste) {
+ pasteCopiedNotes();
+ // Handling selection popup actions.
+ } else if (source == selPopupProps) {
+ new MooDialog(((MooNoteElement)Moosique.getSelection().first()).getNote());
+ } else if (source == selPopupCopy) {
+ copySelectedNotes();
+ } else if (source == selPopupCut) {
+ cutSelectedNotes();
+ } else if (source == selPopupRemove) {
+ removeSelectedNotes();
+ } else if (source == selPopupTranspUpItems[0]) {
+ transposeSelectedNotes(12);
+ } else if (source == selPopupTranspDownItems[0]) {
+ transposeSelectedNotes(-12);
+ } else {
+ for (int i = 1; i < 12; i++) {
+ if (source == selPopupTranspUpItems[i]) transposeSelectedNotes(i);
+ else if (source == selPopupTranspDownItems[i]) transposeSelectedNotes(-i);
+ }
+ }
+ }
}
}