]> 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 {
15
16         private Track track;
17         private MooTrackTitle title;
18         private Rectangle box;
19
20         private JPopupMenu popup;
21         private JMenuItem popupAdd;
22         private ArrayList rects;
23         private Insets insets;
24         private int ticksPerSixteenth, popupY = 0;
25         protected static int viewLength = 0;
26         protected static int extraHeight = 0;
27         public static final int NOTE_HEIGHT = 10, NOTE_WIDTH = 40, VIEW_WIDTH = 200;
28
29         /**
30          * Creates the trackview.
31          * @param track The track it represents graphically and operates on.
32          * @param title The object that is used to manipulate instrument, channel, solo, mute.
33          */
34         public MooTrackView (Track track, MooTrackTitle title) {
35                 super(true);
36                 this.track = track;
37                 this.title = title;
38                 insets = getInsets();
39
40                 // Configures panel
41                 setBackground(Color.white);
42                 setBorder(BorderFactory.createLineBorder(Color.black));
43                 setLayout(null);
44                 setPreferredSize(new Dimension(VIEW_WIDTH, 140 * NOTE_HEIGHT));
45
46                 // Creates temporary variables
47                 MidiEvent note;
48                 MooNoteElement elem;
49                 rects = new ArrayList(track.size() / 2);
50                 extraHeight = Toolkit.getDefaultToolkit().getScreenSize().height - 150;
51
52                 // Places note elements
53                 for (int i = 0; i < track.size(); i++) {
54                         note = track.get(i);
55                         if (note instanceof MooNote) {
56                                 // Adds the note element to the note area and moves it to the appropriate place.
57                                 MooNote mn = (MooNote)note;
58                                 elem = new MooNoteElement(this, mn);
59                                 add(elem);
60                                 layoutElement(elem, false);
61                         }
62                         setPreferredSize(new Dimension(VIEW_WIDTH, viewLength + extraHeight));
63
64                 }
65
66                 // Creates pop-up menu.
67                 popup = new JPopupMenu();
68                 PopupListener pList = new PopupListener();
69                 popupAdd = new JMenuItem("Add note...");
70                 popupAdd.addActionListener(pList);
71                 popup.add(popupAdd);
72
73                 // Adds listeners for popup menu and keyboard synthesizer.
74                 addMouseListener(new MAdapter());
75                 addKeyListener(new MooKeyboard());
76         }
77
78         /**
79          * Layouts the element to the right place.
80          * @param elem  the element that will be layouted.
81          * @param old   If true, this method will remove the old layout and set the new preferredSize for the trackview.
82          */
83         public void layoutElement(MooNoteElement elem, boolean old){
84                 // If the element is currently in the view, removes its coordinates from the list.
85                 Rectangle r = new Rectangle();
86                 if (old){
87                         r = elem.getBounds(r);
88                         for (Iterator i = rects.iterator(); i.hasNext();){
89                                 Object ob = i.next();
90                                 if (r.equals(ob)){
91                                         rects.remove(ob);
92                                         break;
93                                 }
94                         }
95                 }
96
97                 // Creates temporary variables.
98                 ticksPerSixteenth = Moosique.getSequence().getResolution() / 4;
99                 MooNote mn = elem.getNote();
100                 int x, y, height;
101
102                 // Calculates coordinates.
103                 x = insets.left;
104                 y = insets.top + (int)(mn.getTick() / ticksPerSixteenth) * NOTE_HEIGHT;
105                 height = (mn.getDuration() / ticksPerSixteenth) * NOTE_HEIGHT;
106                 if (height == 0) height = NOTE_HEIGHT;
107                 r = new Rectangle(x, y, NOTE_WIDTH, height);
108
109                 // Places the element in the appropriate place.
110                 while(isOccupied(r)) r.translate(NOTE_WIDTH, 0);
111                 elem.setBounds(r);
112                 rects.add(r);
113                 if (viewLength < (y + height)){
114                         viewLength = y + height;
115                         if(old)setPreferredSize(new Dimension(VIEW_WIDTH, viewLength + extraHeight));
116                 }
117         }
118
119         /** 
120          * Returns the track of this view.
121          * @return the track of this view
122          */
123         public Track getTrack() {
124                 return track;
125         }
126
127         /** 
128          * Returns the title of this view.
129          * @return the title of this view
130          */
131         public MooTrackTitle getTitle() {
132                 return title;
133         }
134
135         /**
136          * Checks if the element can be fully drawn as this position without inteferring with other elements.
137          * @return true if the position is occupied.
138          */
139         private boolean isOccupied(Rectangle r) {
140                 Iterator it = rects.iterator();
141                 while (it.hasNext()) {
142                         if(r.intersects((Rectangle)it.next())) return true;
143                 }
144                 return false;
145         }
146         
147         /** 
148          * Adds the given note to the current track, and visualises it.
149          * @param mn    the note to add
150          */
151         public void addNote(MooNote mn) {
152                 mn.addTo(track);
153                 MooNoteElement elem = new MooNoteElement(this, mn);
154                 add(elem);
155                 layoutElement(elem, false);
156                 setPreferredSize(new Dimension(VIEW_WIDTH, viewLength + extraHeight));
157                 repaint();
158         }
159
160         /** 
161          * Removes the given note element from the view and its note from the current track.
162          * @param elem  the note element to remove
163          */
164         public void removeNote(MooNoteElement elem) {
165                 elem.getNote().removeFrom(track);
166                 remove(elem);
167                 elem.getNote().removeFrom(track);
168                 repaint();
169         }
170
171         /**
172          * Adds a standard note to this track.
173          */
174         private void addStandardNote() {
175                 long timestamp = (long)(ticksPerSixteenth * (popupY - insets.top) / NOTE_HEIGHT);
176                 System.out.println(ticksPerSixteenth + ", " + popupY + ", " + insets.top + ", " + timestamp);
177                 addNote(new MooNote(title.getChannel(), 60, 100, timestamp, Moosique.getSequence().getResolution() / 4));
178         }
179
180         /**
181          * Draws the grid that is on the background.
182          * @param g The Graphics object used to draw the grid.
183          */
184         public void paintComponent(Graphics g) {
185                 super.paintComponent(g);
186                 Graphics2D g2 = (Graphics2D)g;
187                 for (int c = 0; c < viewLength || c < getHeight(); c += NOTE_HEIGHT) {
188                         for (int r = 0; r < (10 * NOTE_WIDTH); r += NOTE_WIDTH) {
189                                 box = new Rectangle(r, c, NOTE_WIDTH, NOTE_HEIGHT);
190                                 g2.setColor(Color.gray);
191                                 g2.draw(box);
192                         }
193                 }
194         }
195         
196         /**
197          * The adapter used to listen on mouse actions
198          */
199         class MAdapter extends MouseAdapter {
200                 /**
201                  * Adds a standard note if doubleclicked.
202                  */
203                 public void mouseClicked(MouseEvent e) {
204                         if (e.getClickCount() == 2) {
205                                 popupY = e.getY();
206                                 addStandardNote();
207                         }
208                 }
209         
210                 /**
211                  * Shows the menu if on standard poptriggers.
212                  */
213                 public void mousePressed(MouseEvent e) {
214                         if (e.isPopupTrigger()) {
215                                 popupY = e.getY();
216                                 popup.show(e.getComponent(), e.getX(), e.getY());
217                         }
218                 }
219
220                 /**
221                  * Grabs the focus when the mouse has entered.
222                  */
223                 public void mouseEntered(MouseEvent e) {
224                         // Moosique.setActiveChannel(track.getChannel());
225                         grabFocus();
226                 }
227         }
228
229         /**
230          * Listens on actions on the popupmenu and executes the appropriate action.
231          */
232         class PopupListener implements ActionListener {
233                 public void actionPerformed(ActionEvent e) {
234                         Object source = e.getSource();
235                         if  (source == popupAdd) {
236                                 addStandardNote();
237                         }
238                         // new MooNote(int channel, int pitch, int velocity, long timestamp, int duration)
239                 }
240         }
241 }