]> ruin.nu Git - moosique.git/blobdiff - MooTrackView.java
Fixed drag select in track view.
[moosique.git] / MooTrackView.java
index fafab990ba77b4fc47f260e40e929f1ee7e06610..a591d374853bd246647f16dd0c087ce38f58e335 100644 (file)
@@ -2,6 +2,7 @@ import javax.swing.*;
 import java.awt.*;
 import java.awt.event.*;
 import javax.sound.midi.*;
+import java.util.*;
 
 /**
  * Graphical representation of a MIDI track.
@@ -10,94 +11,424 @@ import javax.sound.midi.*;
  * @version 1
  */
 
-public class MooTrackView extends JPanel implements ActionListener {
+public class MooTrackView extends JPanel {
 
        private Track track;
        private MooTrackTitle title;
-       private NoteArea notes;
        private Rectangle box;
-       private JPopupMenu popup;
-       private JMenuItem menuItem;
 
-       public MooTrackView (Track track) {
+       private JPopupMenu popup, selPopup;
+       private JMenu selPopupTranspUp, selPopupTranspDown;
+       private JMenuItem popupAdd, popupPaste;
+       private JMenuItem selPopupCopy, selPopupCut, selPopupRemove, selPopupTranspUpOct, selPopupTranspDownOct;
+
+       private ArrayList coords, selection, copyBuffer; 
+       private Insets insets;
+       private int ticksPerSixteenth, popupY = 0;
+       private boolean leftMouseButtonPressed = false;
+       protected static int viewLength = 0;
+       protected 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);
                this.track = track;
-               setPreferredSize(new Dimension(200, 200));
-               setLayout(new BorderLayout());
-               this.setBorder(BorderFactory.createLineBorder(Color.black));
+               this.title = title;
+
+               // Creates instance variables
+               insets = getInsets();
+               coords = new ArrayList(track.size() / 2);
+               selection = new ArrayList();
+               copyBuffer = new ArrayList();
+
+               // Creates temporary variables
+               MidiEvent note;
+               MooNoteElement elem;
+               extraHeight = Toolkit.getDefaultToolkit().getScreenSize().height - 150;
+
+               // 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);
+               // 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));
 
-               notes = new NoteArea(track);    
-               notes.setBackground(Color.white);
-               notes.setBorder(BorderFactory.createLineBorder(Color.black));
+               }
 
+               // 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);
+               PopupListener pList = new PopupListener();
+               popupAdd = new JMenuItem("Add note...");
+               popupAdd.addActionListener(pList);
+               popup.add(popupAdd);
+               popupPaste = new JMenuItem("Paste");
+               popupPaste.addActionListener(pList);
+               popup.add(popupPaste);
+
+               // Creates selection pop-up menu.
+               selPopup = new JPopupMenu();
+               selPopupCopy = new JMenuItem("Copy selection");
+               selPopupCopy.addActionListener(pList);
+               selPopup.add(selPopupCopy);
+               selPopupCut = new JMenuItem("Cut selection");
+               selPopupCut.addActionListener(pList);
+               selPopup.add(selPopupCut);
+               selPopupRemove = new JMenuItem("Remove selection");
+               selPopupRemove.addActionListener(pList);
+               selPopup.add(selPopupRemove);
+               selPopupTranspUp = new JMenu("Transpose selection up");
+               selPopup.add(selPopupTranspUp);
+               selPopupTranspUpOct = new JMenuItem("One octave");
+               selPopupTranspUpOct.addActionListener(pList);
+               selPopupTranspUp.add(selPopupTranspUpOct);
+               selPopupTranspDown = new JMenu("Transpose selection down");
+               selPopup.add(selPopupTranspDown);
+               selPopupTranspDownOct = new JMenuItem("One octave");
+               selPopupTranspDownOct.addActionListener(pList);
+               selPopupTranspDown.add(selPopupTranspDownOct);
 
-               notes.addMouseListener(new PopupListener());
-               add(notes, BorderLayout.CENTER);
+               // Adds listeners for popup menu and keyboard synthesizer.
+               addMouseListener(new MAdapter());
+               addKeyListener(new MooKeyboard());
        }
 
-       public void actionPerformed(ActionEvent e) {}
+       /**
+        * 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.
+               ticksPerSixteenth = Moosique.getSequence().getResolution() / 4;
+               MooNote mn = elem.getNote();
+               int x, y, height;
+
+               // Calculates coordinates.
+               x = insets.left;
+               y = insets.top + (int)(mn.getTick() / ticksPerSixteenth) * NOTE_HEIGHT;
+               height = (mn.getDuration() / ticksPerSixteenth) * NOTE_HEIGHT;
+               if (height == 0) 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;
        }
-       
 
        /** 
-        * Updates the track view.
+        * Returns the title of this view.
+        * @return the title of this view
         */
-       public void update() {
+       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();
        }
 
-       class NoteArea extends JPanel {
+       /**
+        * 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));
+       }
 
-               public NoteArea(Track track) {
-                       MidiEvent note;
-                       for (int i = 0; i < track.size(); i++) {
-                               note = track.get(i);
-                               if (note instanceof MooNote) {
-                                       add(new MooNoteElement((MooNote)note));
-                               }
+       /** 
+        * 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();
+       }
+
+       /**
+        * Selects the given note
+        * @param the note to select
+        */
+       public void selectNote(MooNoteElement elem) {
+               selection.add(elem);
+       }
+
+       /**
+        * Deselects the given note
+        * @param the note to deselect
+        */
+       public void deselectNote(MooNoteElement elem) {
+               selection.remove(selection.indexOf(elem));
+       }
+
+       /**
+        * Deselects all notes.
+        */
+       public void deselectAllNotes() {
+               Iterator it = selection.iterator();
+               while(it.hasNext()) {
+                       ((MooNoteElement)it.next()).deselect();
+               }
+               selection.clear();
+       }
+
+       /**
+        * Determines if the given MooNoteElement is the only one in the track view that is selected.
+        * @return if the given element is the only selected one
+        */
+       public boolean isTheOnlySelected(MooNoteElement elem) {
+               Iterator it = selection.iterator();
+               while(it.hasNext()) {
+                       if (!it.next().equals(elem)) return false;
+               }
+               return true;
+       }
+
+       /**
+        * Copies the current selection.
+        */
+       public void copySelectedNotes() {
+               copyBuffer = new ArrayList(selection.size());
+               Iterator it = selection.iterator();
+               while(it.hasNext()) {
+                       copyBuffer.add(((MooNoteElement)it.next()).getNote().clone());
+               }
+               Collections.sort(copyBuffer, new Moosique.NoteComparator());
+       }
+
+       /**
+        * 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);
+               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);
+                               addNote(mn);
                        }
-                       validate();
                }
+       }
 
-               public void paintComponent(Graphics g) {
-                       super.paintComponent(g);
-                       Graphics2D g2 = (Graphics2D)g;
-                       for (int c = 0; c < 1000; c += 20) {
-                               for (int r = 0; r < 200; r += 20) {
-                                       box = new Rectangle(r, c, 20, 20);
-                                       g2.setColor(Color.gray);
-                                       g2.draw(box);
-                               }
+       /**
+        * 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);
+               }
+       }
+
+       /**
+        * 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) {
+               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)) {
+                               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.
+                */
                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();
+               }
+       }
+
+       /**
+        * Listens on actions on the popup menu and executes the appropriate action.
+        */
+       class PopupListener implements ActionListener {
+               public void actionPerformed(ActionEvent e) {
+                       Object source = e.getSource();
+                       // 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 == selPopupTranspUpOct) {
+                               transposeSelectedNotes(12);
+                       } else if (source == selPopupTranspDownOct) {
+                               transposeSelectedNotes(-12);
+                       }
+               }
        }
-}
\ No newline at end of file
+}