]> ruin.nu Git - moosique.git/blob - MooTrackView.java
no message
[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 implements ActionListener {
15
16         private Track track;
17         private MooTrackTitle title;
18         private NoteArea notes;
19         private Rectangle box;
20         private JPopupMenu popup;
21         private JMenuItem menuItem;
22         protected static int viewLength = 0;
23         
24
25         public MooTrackView (Track track) {
26                 super(true);
27                 this.track = track;
28                 //setPreferredSize(new Dimension(200, 9000));
29                 setLayout(new BorderLayout());
30                 this.setBorder(BorderFactory.createLineBorder(Color.black));
31
32                 //title = new MooTrackTitle(track);
33                 //title.setBorder(BorderFactory.createLineBorder(Color.black));
34                 //add(title, BorderLayout.NORTH);
35
36                 notes = new NoteArea(track);    
37                 notes.setBackground(Color.white);
38                 notes.setBorder(BorderFactory.createLineBorder(Color.black));
39
40                 popup = new JPopupMenu();
41                 menuItem = new JMenuItem("Add...");
42                 menuItem.addActionListener(this);
43                 popup.add(menuItem);
44                 menuItem = new JMenuItem("Preferences...");
45                 menuItem.addActionListener(this);
46                 popup.add(menuItem);
47
48                 notes.addMouseListener(new PopupListener());
49                 add(notes, BorderLayout.CENTER);
50         }
51
52         public void actionPerformed(ActionEvent e) {}
53
54         public Track getTrack() {
55                 return track;
56         }
57         
58
59         /** 
60          * Updates the track view.
61          */
62         public void update() {
63                 repaint();
64         }
65
66         class NoteArea extends JPanel {
67                 public static final int NOTE_HEIGHT = 10;
68                 public static final int NOTE_WIDTH = 40;
69                 private int trackLength;
70                 private ArrayList rects;
71
72                 public NoteArea(Track track) {
73                         // Configuring panel
74                         super(true);
75                         setLayout(null);
76                         trackLength = 140;
77                         setPreferredSize(new Dimension(200, 140 * NOTE_HEIGHT));
78
79                         // Creating temporary variables
80                         MidiEvent note;
81                         MooNoteElement elem;
82                         int x, y, height;
83                         rects = new ArrayList(track.size() / 2);
84
85                         // Placing note elements
86                         Insets insets = getInsets();
87                         for (int i = 0; i < track.size(); i++) {
88                                 note = track.get(i);
89                                 if (note instanceof MooNote) {
90                                         // Adds the note element to the note area.
91                                         MooNote mn = (MooNote)note;
92                                         elem = new MooNoteElement(mn);
93                                         add(elem);
94
95                                         // Moves the note element to the appropriate place.
96                                         x = insets.left;
97                                         y = insets.top + (int)(mn.getTick() / 24) * NOTE_HEIGHT;
98                                         height = (mn.getDuration() / 24) * NOTE_HEIGHT;
99                                         if (height == 0) height = NOTE_HEIGHT;
100                                         Rectangle r = new Rectangle(x, y, NOTE_WIDTH, height);
101                                         while(isOccupied(r)) r.translate(NOTE_WIDTH, 0);
102                                         elem.setBounds(r);
103                                         rects.add(r);
104                                         if (viewLength < (y + height)) viewLength = y + height;
105
106                                         // while(findComponentAt(x, y) instanceof MooNoteElement ||
107                                         //       findComponentAt(x, y + height - 1) instanceof MooNoteElement) x += NOTE_WIDTH;
108                                 }
109                                 setPreferredSize(new Dimension(200, viewLength));
110                         }
111                         validate();
112                 }
113                 
114                 private boolean isOccupied(Rectangle r) {
115                         Iterator it = rects.iterator();
116                         while (it.hasNext()) {
117                                 if(r.intersects((Rectangle)it.next())) return true;
118                         }
119                         return false;
120                 }
121
122                 public void paintComponent(Graphics g) {
123                         super.paintComponent(g);
124                         Graphics2D g2 = (Graphics2D)g;
125                         for (int c = 0; c < viewLength ; c += NOTE_HEIGHT) {
126                                 for (int r = 0; r < (10*NOTE_WIDTH); r += NOTE_WIDTH) {
127                                         box = new Rectangle(r, c, NOTE_WIDTH, NOTE_HEIGHT);
128                                         g2.setColor(Color.gray);
129                                         g2.draw(box);
130                                 }
131                         }
132                 }
133         }
134
135         class PopupListener extends MouseAdapter {
136                 public void mousePressed(MouseEvent e) {
137                         maybeShowPopup(e);
138                 }
139
140                 public void mouseReleased(MouseEvent e) {
141                         maybeShowPopup(e);
142                 }
143
144                 private void maybeShowPopup(MouseEvent e) {
145                         if (e.isPopupTrigger()) {
146                                 popup.show(e.getComponent(), e.getX(), e.getY());
147                         }
148                 }
149         }
150 }