]> ruin.nu Git - moosique.git/blobdiff - MooTrackView.java
no message
[moosique.git] / MooTrackView.java
index 0e5d09c21f5b48c1675da312d7ce26a48b811775..2f96085e25f6a362ea22b4d34e28045ac7177736 100644 (file)
@@ -15,14 +15,21 @@ public class MooTrackView extends JPanel {
 
        private Track track;
        private MooTrackTitle title;
-       private Rectangle box;
+       private MooKeyboard keyboard;
+
+       private JPopupMenu popup, selPopup;
+       private JMenu selPopupTranspUp, selPopupTranspDown;
+       private JMenuItem popupAdd, popupPaste;
+       private JMenuItem selPopupCopy, selPopupCut, selPopupRemove;
+       private JMenuItem[] selPopupTranspUpItems, selPopupTranspDownItems;
 
-       private JPopupMenu popup;
-       private JMenuItem popupAdd;
-       private ArrayList rects;
-       private ArrayList selected;
+       private ArrayList coords; 
+       private TreeSet selection;
        private Insets insets;
+       private Rectangle box;
        private int ticksPerSixteenth, popupY = 0;
+       private boolean leftMouseButtonPressed = false;
+       private static boolean snapToSixteenths = true;
        protected static int viewLength = 0;
        protected static int extraHeight = 0;
        public static final int NOTE_HEIGHT = 10, NOTE_WIDTH = 40, VIEW_WIDTH = 200;
@@ -34,10 +41,11 @@ public class MooTrackView extends JPanel {
         */
        public MooTrackView (Track track, MooTrackTitle title) {
                super(true);
+
+               // Defines instance variables
                this.track = track;
                this.title = title;
                insets = getInsets();
-               selected = new ArrayList();
 
                // Configures panel
                setBackground(Color.white);
@@ -45,10 +53,41 @@ public class MooTrackView extends JPanel {
                setLayout(null);
                setPreferredSize(new Dimension(VIEW_WIDTH, 140 * NOTE_HEIGHT));
 
+               placeNoteElements();
+
+               // Creates panel pop-up menu.
+               popup = new JPopupMenu();
+               popupAdd = addMenuItem(popup, "Add note...");
+               popupPaste = addMenuItem(popup, "Paste");
+
+               // Creates selection pop-up menu.
+               selPopup = new JPopupMenu();
+               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 track, and places them in the appropriate place.
+        */
+       public void placeNoteElements() {
+               // Empties the container
+               removeAll();
+               coords = new ArrayList(track.size() / 2);
+               selection = new TreeSet();
+
                // Creates temporary variables
                MidiEvent note;
                MooNoteElement elem;
-               rects = new ArrayList(track.size() / 2);
                extraHeight = Toolkit.getDefaultToolkit().getScreenSize().height - 150;
 
                // Places note elements
@@ -62,20 +101,8 @@ public class MooTrackView extends JPanel {
                                layoutElement(elem, false);
                        }
                        setPreferredSize(new Dimension(VIEW_WIDTH, viewLength + extraHeight));
-
                }
-
-               // Creates pop-up menu.
-               popup = new JPopupMenu();
-               PopupListener pList = new PopupListener();
-               popupAdd = new JMenuItem("Add note...");
-               popupAdd.addActionListener(pList);
-               popup.add(popupAdd);
-
-               // Adds listeners for popup menu and keyboard synthesizer.
-               addMouseListener(new MAdapter());
-               addKeyListener(new MooKeyboard());
-       }
+       }       
 
        /**
         * Layouts the element to the right place.
@@ -87,10 +114,10 @@ public class MooTrackView extends JPanel {
                Rectangle r = new Rectangle();
                if (old){
                        r = elem.getBounds(r);
-                       for (Iterator i = rects.iterator(); i.hasNext();){
+                       for (Iterator i = coords.iterator(); i.hasNext();){
                                Object ob = i.next();
                                if (r.equals(ob)){
-                                       rects.remove(ob);
+                                       coords.remove(ob);
                                        break;
                                }
                        }
@@ -103,20 +130,24 @@ public class MooTrackView extends JPanel {
 
                // Calculates coordinates.
                x = insets.left;
-               y = insets.top + (int)(mn.getTick() / ticksPerSixteenth) * NOTE_HEIGHT;
-               height = (mn.getDuration() / ticksPerSixteenth) * NOTE_HEIGHT;
+               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);
-               rects.add(r);
+               coords.add(r);
                if (viewLength < (y + height)){
                        viewLength = y + height;
                        if(old)setPreferredSize(new Dimension(VIEW_WIDTH, viewLength + extraHeight));
                }
-               if(old)repaint();
+               if (old) {
+                       validate();
+                       repaint();
+               }
        }
 
        /** 
@@ -140,7 +171,7 @@ public class MooTrackView extends JPanel {
         * @return true if the position is occupied.
         */
        private boolean isOccupied(Rectangle r) {
-               Iterator it = rects.iterator();
+               Iterator it = coords.iterator();
                while (it.hasNext()) {
                        if(r.intersects((Rectangle)it.next())) return true;
                }
@@ -158,9 +189,19 @@ public class MooTrackView extends JPanel {
                layoutElement(elem, false);
                setPreferredSize(new Dimension(VIEW_WIDTH, viewLength + extraHeight));
                Moosique.setEdited();
+               validate();
                repaint();
        }
 
+       /**
+        * Adds a standard note to this track.
+        */
+       private void addStandardNote() {
+               int row = (popupY - insets.top) / NOTE_HEIGHT;
+               long timestamp = (long)(ticksPerSixteenth * row);
+               addNote(new MooNote(title.getChannel(), 60, 100, timestamp, Moosique.getSequence().getResolution() / 4));
+       }
+
        /** 
         * Removes the given note element from the view and its note from the current track.
         * @param elem  the note element to remove
@@ -168,43 +209,39 @@ public class MooTrackView extends JPanel {
        public void removeNote(MooNoteElement elem) {
                elem.getNote().removeFrom(track);
                remove(elem);
-               elem.getNote().removeFrom(track);
+               Rectangle r = new Rectangle();
+               r = elem.getBounds(r);
+               coords.remove(r);
                Moosique.setEdited();
+               validate();
                repaint();
        }
 
        /**
-        * Adds a standard note to this track.
+        * Selects the given note
+        * @param the note to select
         */
-       private void addStandardNote() {
-               int row =  (popupY - insets.top) / NOTE_HEIGHT;
-               long timestamp = (long)(ticksPerSixteenth * row);
-               addNote(new MooNote(title.getChannel(), 60, 100, timestamp, Moosique.getSequence().getResolution() / 4));
+       public void selectNote(MooNoteElement elem) {
+               selection.add(elem);
        }
 
        /**
-        * Deselects all notes.
+        * Deselects the given note
+        * @param the note to deselect
         */
-       public void addSelected(MooNoteElement elem) {
-               selected.add(elem);
+       public void deselectNote(MooNoteElement elem) {
+               selection.remove(elem);
        }
 
        /**
         * Deselects all notes.
         */
-       public void removeSelected(MooNoteElement elem) {
-               selected.remove(selected.indexOf(elem));
-       }
-
-       /**
-        * Deselects all notes.
-        */
-       public void deselectAll() {
-               Iterator it = selected.iterator();
+       public void deselectAllNotes() {
+               Iterator it = selection.iterator();
                while(it.hasNext()) {
                        ((MooNoteElement)it.next()).deselect();
                }
-               selected.clear();
+               selection.clear();
        }
 
        /**
@@ -212,13 +249,156 @@ public class MooTrackView extends JPanel {
         * @return if the given element is the only selected one
         */
        public boolean isTheOnlySelected(MooNoteElement elem) {
-               Iterator it = selected.iterator();
+               Iterator it = selection.iterator();
                while(it.hasNext()) {
                        if (!it.next().equals(elem)) return false;
                }
                return true;
        }
 
+       /**
+        * Copies the current selection.
+        */
+       public void copySelectedNotes() {
+               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);
+                       }
+               }
+       }
+
+       /**
+        * Removes the current selection.
+        */
+       public void removeSelectedNotes() {
+               Iterator it = selection.iterator();
+               while(it.hasNext()) {
+                       removeNote((MooNoteElement)it.next());
+               }
+               selection.clear();
+       }
+
+       /**
+        * Transposes all selected notes the given number of halftones.
+        */
+       private void transposeSelectedNotes(int halftones) {
+               Iterator it = selection.iterator();
+               while(it.hasNext()) {
+                       MooNoteElement elem = (MooNoteElement)it.next();
+                       elem.transpose(halftones);
+               }
+       }
+
+       /**
+        * Moves the current selection the given number of ticks.
+        * @param ticks         the number of ticks to move the selection.
+        */
+       public void moveSelectedNotes(int ticks) {
+               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);
+                       }
+               }
+       }
+
+       /**
+        * 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));
+       }
+
+       /**
+        * 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
@@ -226,7 +406,7 @@ public class MooTrackView extends JPanel {
         * @param y     the y-coordinate in which to display the menu
         */
        public void showSelectionPopup(Component c, int x, int y) {
-               
+               selPopup.show(c, x, y);
        }
 
        /**
@@ -245,6 +425,14 @@ public class MooTrackView extends JPanel {
                }
        }
        
+       /**
+        * 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
         */
@@ -254,28 +442,25 @@ public class MooTrackView extends JPanel {
                 * Deselects all note on click, adds a standard note on double click.
                 */
                public void mouseClicked(MouseEvent e) {
-                       deselectAll();
-                       if (e.getClickCount() == 2) {
-                               popupY = e.getY();
-                               addStandardNote();
+                       if (SwingUtilities.isLeftMouseButton(e)) {
+                               deselectAllNotes();
+                               if (e.getClickCount() == 2) {
+                                       popupY = e.getY();
+                                       addStandardNote();
+                               }
                        }
                }
        
                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);
                }
 
-               /**
-                * Selects the notes within the area that was selected.
-                */
-               public void mouseDragged(MouseEvent e) {
-                       
-               }
-
                /**
                 * Shows the menu if an OS-specific popup-trigger was activated.
                 */
@@ -290,21 +475,39 @@ public class MooTrackView extends JPanel {
                 * Grabs the focus when the mouse has entered.
                 */
                public void mouseEntered(MouseEvent e) {
-                       // Moosique.setActiveChannel(track.getChannel());
+                       Moosique.setActiveChannel(title.getChannel());
                        grabFocus();
                }
        }
 
        /**
-        * Listens on actions on the popupmenu and executes the appropriate action.
+        * 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();
-                       if  (source == popupAdd) {
+                       // Handling panel popup actions.
+                       if (source == popupAdd) {
                                addStandardNote();
+                       } else if (source == popupPaste) {
+                               pasteCopiedNotes();
+                       // Handling selection popup actions.
+                       } 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);
+                               }
                        }
-                       // new MooNote(int channel, int pitch, int velocity, long timestamp, int duration)
                }
        }
 }