]> ruin.nu Git - moosique.git/blob - MooTrackView.java
inte helt klar
[moosique.git] / MooTrackView.java
1 import javax.swing.*;
2 import java.awt.*;
3 import java.awt.event.*;
4 import javax.sound.midi.*;
5 import java.util.*;
6
7 /**
8  * Graphical representation of a MIDI track.
9  * 
10  * @author  Andersson , Andreen, Lanneskog, Pehrson
11  * @version 1
12  */
13
14 public class MooTrackView extends JPanel {
15
16         private Track track;
17         private Rectangle box;
18
19         private JPopupMenu popup;
20         private JMenuItem menuItem;
21         private ArrayList rects;
22         protected static int viewLength = 0;
23         protected static int extraHeight = 0;
24         public static final int NOTE_HEIGHT = 10, NOTE_WIDTH = 40, VIEW_WIDTH = 200;
25
26         public MooTrackView (Track track) {
27                 super(true);
28                 this.track = track;
29
30                 extraHeight = Toolkit.getDefaultToolkit().getScreenSize().height - 150;
31                 // Configures panel
32                 setBackground(Color.white);
33                 setBorder(BorderFactory.createLineBorder(Color.black));
34                 setLayout(null);
35                 setPreferredSize(new Dimension(VIEW_WIDTH, 140 * NOTE_HEIGHT));
36
37                 // Creates temporary variables
38                 MidiEvent note;
39                 MooNoteElement elem;
40                 rects = new ArrayList(track.size() / 2);
41
42                 // Places note elements
43                 for (int i = 0; i < track.size(); i++) {
44                         note = track.get(i);
45                         if (note instanceof MooNote) {
46                                 // Adds the note element to the note area.
47                                 MooNote mn = (MooNote)note;
48                                 elem = new MooNoteElement(this, mn);
49                                 add(elem);
50
51                                 layoutElement(elem,false);
52
53                                 // Moves the note element to the appropriate place.
54                         }
55                         setPreferredSize(new Dimension(VIEW_WIDTH, viewLength + extraHeight));
56
57                 }
58                 validate();
59
60                 // Creates pop-up menu.
61                 popup = new JPopupMenu();
62                 menuItem = new JMenuItem("Add...");
63                 // menuItem.addActionListener();
64                 popup.add(menuItem);
65
66                 addMouseListener(new PopupListener());
67         }
68
69         public void layoutElement(MooNoteElement elem, boolean old){
70                 Rectangle r = new Rectangle();
71                 if(old){
72                         r =     elem.getBounds(r);
73                         for (Iterator i = rects.iterator(); i.hasNext();){
74                                 Object ob = i.next();
75                                 if (r.equals(ob)){
76                                         rects.remove(ob);
77                                         break;
78                                 }
79                         }
80                 }
81
82                 int ticksPerSixteenth = Moosique.getSequence().getResolution() / 4;
83                 MooNote mn = elem.getNote();
84                 Insets insets = getInsets();
85                 int x, y, height;
86                 x = insets.left;
87                 y = insets.top + (int)(mn.getTick() / ticksPerSixteenth) * NOTE_HEIGHT;
88                 height = (mn.getDuration() / ticksPerSixteenth) * NOTE_HEIGHT;
89                 if (height == 0) height = NOTE_HEIGHT;
90                 r = new Rectangle(x, y, NOTE_WIDTH, height);
91                 while(isOccupied(r)) r.translate(NOTE_WIDTH, 0);
92                 elem.setBounds(r);
93                 rects.add(r);
94                 if (viewLength < (y + height)){
95                         viewLength = y + height;
96                         if(old)setPreferredSize(new Dimension(VIEW_WIDTH, viewLength + extraHeight));
97                 }
98
99         }
100
101         public Track getTrack() {
102                 return track;
103         }
104
105
106         /** 
107          * Updates the track view.
108          */
109         public void update(long tickPosition) {
110                 repaint();
111         }
112
113         private boolean isOccupied(Rectangle r) {
114                 Iterator it = rects.iterator();
115                 while (it.hasNext()) {
116                         if(r.intersects((Rectangle)it.next())) return true;
117                 }
118                 return false;
119         }
120         
121         public void remove(MooNoteElement elem) {
122                 remove((Component)elem);
123                 elem.getNote().removeFrom(track);
124                 validate();
125                 repaint();
126         }
127
128         public void paintComponent(Graphics g) {
129                 super.paintComponent(g);
130                 Graphics2D g2 = (Graphics2D)g;
131                 for (int c = 0; c < viewLength || c < getHeight(); c += NOTE_HEIGHT) {
132                         for (int r = 0; r < (10 * NOTE_WIDTH); r += NOTE_WIDTH) {
133                                 box = new Rectangle(r, c, NOTE_WIDTH, NOTE_HEIGHT);
134                                 g2.setColor(Color.gray);
135                                 g2.draw(box);
136                         }
137                 }
138         }
139
140         class PopupListener extends MouseAdapter {
141                 public void mousePressed(MouseEvent e) {
142                         if (e.isPopupTrigger()) {
143                                 popup.show(e.getComponent(), e.getX(), e.getY());
144                         }
145                 }
146         }
147 }