3 import java.awt.event.*;
4 import javax.sound.midi.*;
8 * Graphical representation of a MIDI track.
10 * @author Andersson , Andreen, Lanneskog, Pehrson
14 public class MooTrackView extends JPanel {
17 private MooTrackTitle title;
18 private MooKeyboard keyboard;
20 private JPopupMenu popup, selPopup;
21 private JMenu selPopupTranspUp, selPopupTranspDown;
22 private JMenuItem popupAdd, popupPaste;
23 private JMenuItem selPopupCopy, selPopupCut, selPopupRemove;
24 private JMenuItem[] selPopupTranspUpItems, selPopupTranspDownItems;
26 private ArrayList coords;
27 private TreeSet selection;
28 private Insets insets;
29 private Rectangle box;
30 private int ticksPerSixteenth, popupY = 0;
31 private boolean leftMouseButtonPressed = false;
32 protected static int viewLength = 0;
33 protected static int extraHeight = 0;
34 public static final int NOTE_HEIGHT = 10, NOTE_WIDTH = 40, VIEW_WIDTH = 200;
37 * Creates the trackview.
38 * @param track The track it represents graphically and operates on.
39 * @param title The object that is used to manipulate instrument, channel, solo, mute.
41 public MooTrackView (Track track, MooTrackTitle title) {
44 // Defines instance variables
50 setBackground(Color.white);
51 setBorder(BorderFactory.createLineBorder(Color.black));
53 setPreferredSize(new Dimension(VIEW_WIDTH, 140 * NOTE_HEIGHT));
57 // Creates panel pop-up menu.
58 popup = new JPopupMenu();
59 popupAdd = addMenuItem(popup, "Add note...");
60 popupPaste = addMenuItem(popup, "Paste");
62 // Creates selection pop-up menu.
63 selPopup = new JPopupMenu();
64 selPopupCopy = addMenuItem(selPopup, "Copy selection");
65 selPopupCut = addMenuItem(selPopup, "Cut selection");
66 selPopupRemove = addMenuItem(selPopup, "Remove selection");
67 selPopupTranspUpItems = new JMenuItem[12];
68 selPopupTranspDownItems = new JMenuItem[12];
69 selPopupTranspUp = createTransposeMenu(selPopup, selPopupTranspUpItems, "selection up");
70 selPopupTranspDown = createTransposeMenu(selPopup, selPopupTranspDownItems, "selection down");
72 // Adds listeners for popup menu and keyboard synthesizer.
73 addMouseListener(new MAdapter());
74 keyboard = new MooKeyboard(title);
75 addKeyListener(keyboard);
79 * Creates note elements for all MooNotes in the track, and places them in the appropriate place.
81 public void placeNoteElements() {
82 // Converts the track.
83 Moosique.convertTrack(track);
85 // Empties the container
87 coords = new ArrayList(track.size() / 2);
88 selection = new TreeSet();
90 // Creates temporary variables
93 extraHeight = Toolkit.getDefaultToolkit().getScreenSize().height - 150;
95 // Places note elements
96 for (int i = 0; i < track.size(); i++) {
98 if (note instanceof MooNote) {
99 // Adds the note element to the note area and moves it to the appropriate place.
100 MooNote mn = (MooNote)note;
101 elem = new MooNoteElement(this, mn);
103 layoutElement(elem, false);
105 setPreferredSize(new Dimension(VIEW_WIDTH, viewLength + extraHeight));
110 * Layouts the element to the right place.
111 * @param elem the element that will be layouted.
112 * @param old If true, this method will remove the old layout and set the new preferredSize for the trackview.
114 public void layoutElement(MooNoteElement elem, boolean old){
115 // If the element is currently in the view, removes its coordinates from the list.
116 Rectangle r = new Rectangle();
118 r = elem.getBounds(r);
119 for (Iterator i = coords.iterator(); i.hasNext();){
120 Object ob = i.next();
128 // Creates temporary variables.
129 ticksPerSixteenth = Moosique.getSequence().getResolution() / 4;
130 MooNote mn = elem.getNote();
133 // Calculates coordinates.
135 y = insets.top + (int)((mn.getTick() * NOTE_HEIGHT) / ticksPerSixteenth);
136 height = (mn.getDuration() * NOTE_HEIGHT) / ticksPerSixteenth;
137 System.out.println("y=i(" + insets.top + ")+t(" + mn.getTick() + ")/s(" + ticksPerSixteenth + ") *n(" + NOTE_HEIGHT + ") = " + y);
138 System.out.println("h=t(" + mn.getDuration() + ")/s(" + ticksPerSixteenth + ") *n(" + NOTE_HEIGHT + ") = " + height);
139 if (height == 0) height = NOTE_HEIGHT;
140 r = new Rectangle(x, y, NOTE_WIDTH, height);
142 // Places the element in the appropriate place.
143 while(isOccupied(r)) r.translate(NOTE_WIDTH, 0);
146 if (viewLength < (y + height)){
147 viewLength = y + height;
148 if(old)setPreferredSize(new Dimension(VIEW_WIDTH, viewLength + extraHeight));
157 * Returns the track of this view.
158 * @return the track of this view
160 public Track getTrack() {
165 * Returns the title of this view.
166 * @return the title of this view
168 public MooTrackTitle getTitle() {
173 * Checks if the element can be fully drawn as this position without inteferring with other elements.
174 * @return true if the position is occupied.
176 private boolean isOccupied(Rectangle r) {
177 Iterator it = coords.iterator();
178 while (it.hasNext()) {
179 if(r.intersects((Rectangle)it.next())) return true;
185 * Adds the given note to the current track, and visualises it.
186 * @param mn the note to add
188 public void addNote(MooNote mn) {
190 MooNoteElement elem = new MooNoteElement(this, mn);
192 layoutElement(elem, false);
193 setPreferredSize(new Dimension(VIEW_WIDTH, viewLength + extraHeight));
194 Moosique.setEdited();
200 * Adds a standard note to this track.
202 private void addStandardNote() {
203 int row = (popupY - insets.top) / NOTE_HEIGHT;
204 long timestamp = (long)(ticksPerSixteenth * row);
205 addNote(new MooNote(title.getChannel(), 60, 100, timestamp, Moosique.getSequence().getResolution() / 4));
209 * Removes the given note element from the view and its note from the current track.
210 * @param elem the note element to remove
212 public void removeNote(MooNoteElement elem) {
213 elem.getNote().removeFrom(track);
215 Rectangle r = new Rectangle();
216 r = elem.getBounds(r);
218 Moosique.setEdited();
224 * Selects the given note
225 * @param the note to select
227 public void selectNote(MooNoteElement elem) {
232 * Deselects the given note
233 * @param the note to deselect
235 public void deselectNote(MooNoteElement elem) {
236 selection.remove(elem);
240 * Deselects all notes.
242 public void deselectAllNotes() {
243 Iterator it = selection.iterator();
244 while(it.hasNext()) {
245 ((MooNoteElement)it.next()).deselect();
251 * Determines if the given MooNoteElement is the only one in the track view that is selected.
252 * @return if the given element is the only selected one
254 public boolean isTheOnlySelected(MooNoteElement elem) {
255 Iterator it = selection.iterator();
256 while(it.hasNext()) {
257 if (!it.next().equals(elem)) return false;
263 * Copies the current selection.
265 public void copySelectedNotes() {
266 ArrayList copyBuffer = new ArrayList(selection.size());
267 Iterator it = selection.iterator();
268 while(it.hasNext()) {
269 copyBuffer.add(((MooNoteElement)it.next()).getNote().clone());
271 Collections.sort(copyBuffer);
272 Moosique.setCopyBuffer(copyBuffer);
276 * Cuts the current selection.
278 public void cutSelectedNotes() {
280 removeSelectedNotes();
284 * Pastes the current copy buffer at the given timestamp.
286 public void pasteCopiedNotes() {
287 int row = (popupY - insets.top) / NOTE_HEIGHT;
288 long timestamp = (long)(ticksPerSixteenth * row);
289 ArrayList copyBuffer = Moosique.getCopyBuffer();
290 if (copyBuffer.size() > 0) {
291 long startTime = ((MooNote)copyBuffer.get(0)).getTick();
292 Iterator it = copyBuffer.iterator();
293 while(it.hasNext()) {
294 MooNote mn = (MooNote)((MooNote)it.next()).clone();
295 mn.setTick(mn.getTick() - startTime + timestamp);
296 mn.setChannel(title.getChannel());
303 * Removes the current selection.
305 public void removeSelectedNotes() {
306 Iterator it = selection.iterator();
307 while(it.hasNext()) {
308 removeNote((MooNoteElement)it.next());
314 * Transposes all selected notes the given number of halftones.
316 private void transposeSelectedNotes(int halftones) {
317 Iterator it = selection.iterator();
318 while(it.hasNext()) {
319 MooNoteElement elem = (MooNoteElement)it.next();
320 elem.transpose(halftones);
325 * Moves the current selection the given number of ticks.
326 * @param ticks the number of ticks to move the selection.
328 public void moveSelectedNotes(int ticks) {
330 // If the selection should be moved upwards, traverses the list in the natural order.
331 Iterator it = selection.iterator();
332 while(it.hasNext()) {
333 MooNoteElement elem = (MooNoteElement)it.next();
334 elem.getNote().setTick(elem.getNote().getTick() + ticks);
335 layoutElement(elem, true);
338 // If the selection should be moved downwards, traverses the list in the opposite order.
339 ArrayList selectedList = new ArrayList(selection);
340 ListIterator it = selectedList.listIterator(selectedList.size());
341 while(it.hasPrevious()) {
342 MooNoteElement elem = (MooNoteElement)it.previous();
343 elem.getNote().setTick(elem.getNote().getTick() + ticks);
344 layoutElement(elem, true);
350 * Moves the current selection, depending on the given delta y.
351 * @param y the number of pixels the selection is moved.
353 public void maybeMoveSelectedNotes(int y) {
354 moveSelectedNotes(ticksPerSixteenth * (y / NOTE_HEIGHT));
358 * Enables keyboard recording.
360 public void enableKeyboardRecording() {
361 keyboard.recordEnable();
365 * Disables keyboard recording.
367 public void disableKeyboardRecording() {
368 keyboard.recordDisable();
372 * Adds a menu item with the given command to the given popup menu.
374 private JMenuItem addMenuItem(JPopupMenu menu, String command) {
375 JMenuItem item = new JMenuItem(command);
376 item.addActionListener(new PopupListener());
382 * Adds a menu item with the given command to the given menu.
384 private JMenuItem addMenuItem(JMenu menu, String command) {
385 JMenuItem item = new JMenuItem(command);
386 item.addActionListener(new PopupListener());
392 * Creates a transpose sub menu with the given title in the given popup menu,
393 * inserting the items into the given array.
395 private JMenu createTransposeMenu(JPopupMenu menu, JMenuItem[] items, String title) {
396 JMenu trans = new JMenu("Transpose " + title);
398 items[0] = addMenuItem(trans, "One octave");
399 for (int i = 1; i < 12; i++) {
400 items[i] = addMenuItem(trans, (i) + " halftones");
406 * Shows a popup-menu with options for the current selection of note elements.
407 * @param c the component over which to display the menu
408 * @param x the x-coordinate in which to display the menu
409 * @param y the y-coordinate in which to display the menu
411 public void showSelectionPopup(Component c, int x, int y) {
412 selPopup.show(c, x, y);
416 * Draws the grid that is on the background.
417 * @param g The Graphics object used to draw the grid.
419 public void paintComponent(Graphics g) {
420 super.paintComponent(g);
421 Graphics2D g2 = (Graphics2D)g;
422 for (int c = 0; c < viewLength || c < getHeight(); c += NOTE_HEIGHT) {
423 for (int r = 0; r < (10 * NOTE_WIDTH); r += NOTE_WIDTH) {
424 box = new Rectangle(r, c, NOTE_WIDTH, NOTE_HEIGHT);
425 g2.setColor(Color.gray);
432 * Returns whether the left mouse button is currently pressed or not.
433 * @return true if the left mosue button is currently pressed
435 public boolean isLeftMouseButtonPressed() {
436 return leftMouseButtonPressed;
440 * The adapter used to listen on mouse actions
442 class MAdapter extends MouseAdapter {
445 * Deselects all note on click, adds a standard note on double click.
447 public void mouseClicked(MouseEvent e) {
448 if (SwingUtilities.isLeftMouseButton(e)) {
450 if (e.getClickCount() == 2) {
457 public void mousePressed(MouseEvent e) {
458 if (SwingUtilities.isLeftMouseButton(e)) leftMouseButtonPressed = true;
462 public void mouseReleased(MouseEvent e) {
463 if (SwingUtilities.isLeftMouseButton(e)) leftMouseButtonPressed = false;
468 * Shows the menu if an OS-specific popup-trigger was activated.
470 private void maybeShowPopup(MouseEvent e) {
471 if (e.isPopupTrigger()) {
473 popup.show(e.getComponent(), e.getX(), e.getY());
478 * Grabs the focus when the mouse has entered.
480 public void mouseEntered(MouseEvent e) {
481 Moosique.setActiveChannel(title.getChannel());
487 * Takes the appropriate action when a user selects an item on the popup menu.
489 class PopupListener implements ActionListener {
490 public void actionPerformed(ActionEvent e) {
491 Object source = e.getSource();
492 // Handling panel popup actions.
493 if (source == popupAdd) {
495 } else if (source == popupPaste) {
497 // Handling selection popup actions.
498 } else if (source == selPopupCopy) {
500 } else if (source == selPopupCut) {
502 } else if (source == selPopupRemove) {
503 removeSelectedNotes();
504 } else if (source == selPopupTranspUpItems[0]) {
505 transposeSelectedNotes(12);
506 } else if (source == selPopupTranspDownItems[0]) {
507 transposeSelectedNotes(-12);
509 for (int i = 1; i < 12; i++) {
510 if (source == selPopupTranspUpItems[i]) transposeSelectedNotes(i);
511 else if (source == selPopupTranspDownItems[i]) transposeSelectedNotes(-i);