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