]> ruin.nu Git - moosique.git/blobdiff - MooTrackView.java
menu for elements..
[moosique.git] / MooTrackView.java
index 864ed07b2fe6a30a4431d12084973ac9399ace09..cba92728796318289308ea79601c293a6989fdf0 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.
@@ -17,11 +18,13 @@ public class MooTrackView extends JPanel implements ActionListener {
        private NoteArea notes;
        private Rectangle box;
        private JPopupMenu popup;
+       private JPopupMenu notePopup;
        private JMenuItem menuItem;
        protected static int viewLength = 0;
        
 
        public MooTrackView (Track track) {
+               super(true);
                this.track = track;
                //setPreferredSize(new Dimension(200, 9000));
                setLayout(new BorderLayout());
@@ -39,9 +42,12 @@ public class MooTrackView extends JPanel implements ActionListener {
                menuItem = new JMenuItem("Add...");
                menuItem.addActionListener(this);
                popup.add(menuItem);
+
+
+               notePopup = new JPopupMenu();
                menuItem = new JMenuItem("Preferences...");
                menuItem.addActionListener(this);
-               popup.add(menuItem);
+               notePopup.add(menuItem);
 
                notes.addMouseListener(new PopupListener());
                add(notes, BorderLayout.CENTER);
@@ -57,7 +63,7 @@ public class MooTrackView extends JPanel implements ActionListener {
        /** 
         * Updates the track view.
         */
-       public void update() {
+       public void update(long tickPosition) {
                repaint();
        }
 
@@ -65,17 +71,20 @@ public class MooTrackView extends JPanel implements ActionListener {
                public static final int NOTE_HEIGHT = 10;
                public static final int NOTE_WIDTH = 40;
                private int trackLength;
+               private ArrayList rects;
 
                public NoteArea(Track track) {
                        // Configuring panel
+                       super(true);
                        setLayout(null);
                        trackLength = 140;
                        setPreferredSize(new Dimension(200, 140 * NOTE_HEIGHT));
 
-                       // Temporary variables
+                       // Creating temporary variables
                        MidiEvent note;
                        MooNoteElement elem;
                        int x, y, height;
+                       rects = new ArrayList(track.size() / 2);
 
                        // Placing note elements
                        Insets insets = getInsets();
@@ -92,15 +101,27 @@ public class MooTrackView extends JPanel implements ActionListener {
                                        y = insets.top + (int)(mn.getTick() / 24) * NOTE_HEIGHT;
                                        height = (mn.getDuration() / 24) * NOTE_HEIGHT;
                                        if (height == 0) height = NOTE_HEIGHT;
-                                       while(findComponentAt(x, y) instanceof MooNoteElement ||
-                                             findComponentAt(x, y + height - 1) instanceof MooNoteElement) x += NOTE_WIDTH;
-                                       elem.setBounds(x, y, NOTE_WIDTH, height);
+                                       Rectangle r = new Rectangle(x, y, NOTE_WIDTH, height);
+                                       while(isOccupied(r)) r.translate(NOTE_WIDTH, 0);
+                                       elem.setBounds(r);
+                                       rects.add(r);
                                        if (viewLength < (y + height)) viewLength = y + height;
+
+                                       // while(findComponentAt(x, y) instanceof MooNoteElement ||
+                                       //       findComponentAt(x, y + height - 1) instanceof MooNoteElement) x += NOTE_WIDTH;
                                }
-                               setPreferredSize(new Dimension(200,viewLength));
+                               setPreferredSize(new Dimension(200, viewLength));
                        }
                        validate();
                }
+               
+               private boolean isOccupied(Rectangle r) {
+                       Iterator it = rects.iterator();
+                       while (it.hasNext()) {
+                               if(r.intersects((Rectangle)it.next())) return true;
+                       }
+                       return false;
+               }
 
                public void paintComponent(Graphics g) {
                        super.paintComponent(g);
@@ -126,7 +147,10 @@ public class MooTrackView extends JPanel implements ActionListener {
 
                private void maybeShowPopup(MouseEvent e) {
                        if (e.isPopupTrigger()) {
-                               popup.show(e.getComponent(), e.getX(), e.getY());
+                               if (findComponentAt(e.getX(), e.getY()) instanceof MooNoteElement)
+                                       notePopup.show(e.getComponent(), e.getX(), e.getY());
+                               else
+                                       popup.show(e.getComponent(), e.getX(), e.getY());
                        }
                }
        }