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 popupAdd, selPopupTranspUp, selPopupTranspDown;
22 private JMenuItem popupAddItemsCustom, popupAddItemsLast, popupPaste;
23 private JMenuItem popupAddItemsWhole, popupAddItemsHalf, popupAddItemsQuarter, popupAddItemsEighth, popupAddItemsSixteenth;
24 private JMenuItem selPopupProps, selPopupCopy, selPopupCut, selPopupRemove;
25 private JMenuItem[] selPopupTranspUpItems, selPopupTranspDownItems;
27 private ArrayList coords;
28 private Insets insets;
29 private Rectangle box;
30 private int ticksPerSixteenth, popupY = 0, lastNoteLength = 2;
31 private boolean leftMouseButtonPressed = false;
32 private static boolean snapToSixteenths = true;
33 private static int viewLength = 0;
34 private static int extraHeight = 0;
35 public static final int NOTE_HEIGHT = 10, NOTE_WIDTH = 40, VIEW_WIDTH = 200;
38 * Creates the trackview.
39 * @param track The track it represents graphically and operates on.
40 * @param title The object that is used to manipulate instrument, channel, solo, mute.
42 public MooTrackView (Track track, MooTrackTitle title) {
45 // Defines instance variables
48 ticksPerSixteenth = Moosique.getSequence().getResolution() / 4;
50 coords = new ArrayList(track.size() / 2);
53 setBackground(Color.white);
54 setBorder(BorderFactory.createLineBorder(Color.black));
56 setPreferredSize(new Dimension(VIEW_WIDTH, 140 * NOTE_HEIGHT));
58 // Creates temporary variables
61 extraHeight = Toolkit.getDefaultToolkit().getScreenSize().height - 150;
63 // Places note elements
64 for (int i = 0; i < track.size(); i++) {
66 if (note instanceof MooNote) {
67 // Adds the note element to the note area and moves it to the appropriate place.
68 MooNote mn = (MooNote)note;
69 elem = new MooNoteElement(this, mn);
71 layoutElement(elem, false);
74 setPreferredSize(new Dimension(VIEW_WIDTH, viewLength + extraHeight));
76 // Creates panel pop-up menu.
77 popup = new JPopupMenu();
78 popupAdd = new JMenu("Add note");
80 popupAddItemsCustom = addMenuItem(popupAdd, "Custom...");
81 popupAddItemsLast = addMenuItem(popupAdd, "As last added");
82 popupAdd.addSeparator();
83 popupAddItemsWhole = addMenuItem(popupAdd, "Whole");
84 popupAddItemsHalf = addMenuItem(popupAdd, "Half");
85 popupAddItemsQuarter = addMenuItem(popupAdd, "Quarter");
86 popupAddItemsEighth = addMenuItem(popupAdd, "Eighth");
87 popupAddItemsSixteenth = addMenuItem(popupAdd, "Sixteenth");
88 popupPaste = addMenuItem(popup, "Paste");
90 // Creates selection pop-up menu.
91 selPopup = new JPopupMenu();
92 selPopupProps = addMenuItem(selPopup, "Properties...");
93 selPopupCopy = addMenuItem(selPopup, "Copy selection");
94 selPopupCut = addMenuItem(selPopup, "Cut selection");
95 selPopupRemove = addMenuItem(selPopup, "Remove selection");
96 selPopupTranspUpItems = new JMenuItem[12];
97 selPopupTranspDownItems = new JMenuItem[12];
98 selPopupTranspUp = createTransposeMenu(selPopup, selPopupTranspUpItems, "selection up");
99 selPopupTranspDown = createTransposeMenu(selPopup, selPopupTranspDownItems, "selection down");
101 // Adds listeners for popup menu and keyboard synthesizer.
102 addMouseListener(new MAdapter());
103 keyboard = new MooKeyboard(title);
104 addKeyListener(keyboard);
108 * Creates note elements for all MooNotes in the given list, and places them in the appropriate place.
110 public void placeNewNotes(java.util.List notes) {
111 // Creates temporary variables
114 extraHeight = Toolkit.getDefaultToolkit().getScreenSize().height - 150;
116 // Places note elements
117 for (int i = 0; i < notes.size(); i++) {
118 note = (MidiEvent)notes.get(i);
119 if (note instanceof MooNote) {
120 // Adds the note element to the note area and moves it to the appropriate place.
121 MooNote mn = (MooNote)note;
122 elem = new MooNoteElement(this, mn);
124 layoutElement(elem, false);
127 setPreferredSize(new Dimension(VIEW_WIDTH, viewLength + extraHeight));
131 * Layouts the element to the right place.
132 * @param elem the element that will be layouted.
133 * @param old If true, this method will remove the old layout and set the new preferredSize for the trackview.
135 public void layoutElement(MooNoteElement elem, boolean old){
136 // If the element is currently in the view, removes its coordinates from the list.
137 Rectangle r = new Rectangle();
139 r = elem.getBounds(r);
140 for (Iterator i = coords.iterator(); i.hasNext();){
141 Object ob = i.next();
149 // Creates temporary variables.
150 MooNote mn = elem.getNote();
153 // Calculates coordinates.
155 y = insets.top + (int)((mn.getTick() * NOTE_HEIGHT) / ticksPerSixteenth);
156 height = (mn.getDuration() * NOTE_HEIGHT) / ticksPerSixteenth;
157 if (height == 0) height = NOTE_HEIGHT;
158 if (snapToSixteenths && height < NOTE_HEIGHT) height = NOTE_HEIGHT;
159 r = new Rectangle(x, y, NOTE_WIDTH, height);
161 // Places the element in the appropriate place.
162 while(isOccupied(r)) r.translate(NOTE_WIDTH, 0);
165 if (viewLength < (y + height)){
166 viewLength = y + height;
167 if(old)setPreferredSize(new Dimension(VIEW_WIDTH, viewLength + extraHeight));
176 * Returns the track of this view.
177 * @return the track of this view
179 public Track getTrack() {
184 * Returns the title of this view.
185 * @return the title of this view
187 public MooTrackTitle getTitle() {
192 * Checks if the element can be fully drawn as this position without inteferring with other elements.
193 * @return true if the position is occupied.
195 private boolean isOccupied(Rectangle r) {
196 Iterator it = coords.iterator();
197 while (it.hasNext()) {
198 if(r.intersects((Rectangle)it.next())) return true;
204 * Adds the given note to the current track, and visualises it.
205 * @param mn the note to add
207 public void addNote(MooNote mn) {
209 MooNoteElement elem = new MooNoteElement(this, mn);
211 layoutElement(elem, false);
212 setPreferredSize(new Dimension(VIEW_WIDTH, viewLength + extraHeight));
213 Moosique.setEdited();
219 * Adds a standard note to this track.
221 private void addNoteAtClickY(int length) {
222 lastNoteLength = length;
223 int row = (popupY - insets.top) / NOTE_HEIGHT;
224 long timestamp = (long)(ticksPerSixteenth * row);
225 addNote(new MooNote(title.getChannel(), 60, 100, timestamp, ticksPerSixteenth * length));
229 * Removes the given note element from the view and its note from the current track.
230 * @param elem the note element to remove
232 public void removeNote(MooNoteElement elem) {
233 elem.getNote().removeFrom(track);
235 Rectangle r = new Rectangle();
236 r = elem.getBounds(r);
238 Moosique.setEdited();
244 * Copies the current selection.
246 public void copySelectedNotes() {
247 TreeSet selection = Moosique.getSelection();
248 ArrayList copyBuffer = new ArrayList(selection.size());
249 Iterator it = selection.iterator();
250 while(it.hasNext()) {
251 copyBuffer.add(((MooNoteElement)it.next()).getNote().clone());
253 Collections.sort(copyBuffer);
254 Moosique.setCopyBuffer(copyBuffer);
258 * Cuts the current selection.
260 public void cutSelectedNotes() {
262 removeSelectedNotes();
266 * Pastes the current copy buffer at the given timestamp.
268 public void pasteCopiedNotes() {
269 int row = (popupY - insets.top) / NOTE_HEIGHT;
270 long timestamp = (long)(ticksPerSixteenth * row);
271 ArrayList copyBuffer = Moosique.getCopyBuffer();
272 if (copyBuffer.size() > 0) {
273 long startTime = ((MooNote)copyBuffer.get(0)).getTick();
274 Iterator it = copyBuffer.iterator();
275 while(it.hasNext()) {
276 MooNote mn = (MooNote)((MooNote)it.next()).clone();
277 mn.setTick(mn.getTick() - startTime + timestamp);
278 mn.setChannel(title.getChannel());
281 Moosique.setEdited();
286 * Removes the current selection.
288 public void removeSelectedNotes() {
289 TreeSet selection = Moosique.getSelection();
290 Iterator it = selection.iterator();
291 while(it.hasNext()) {
292 removeNote((MooNoteElement)it.next());
295 Moosique.setEdited();
299 * Transposes all selected notes the given number of halftones.
301 private void transposeSelectedNotes(int halftones) {
302 TreeSet selection = Moosique.getSelection();
303 Iterator it = selection.iterator();
304 while(it.hasNext()) {
305 MooNoteElement elem = (MooNoteElement)it.next();
306 elem.transpose(halftones);
308 Moosique.setEdited();
312 * Moves the current selection the given number of ticks.
313 * @param ticks the number of ticks to move the selection.
315 public void moveSelectedNotes(int ticks) {
316 TreeSet selection = Moosique.getSelection();
318 // If the selection should be moved upwards, traverses the list in the natural order.
319 Iterator it = selection.iterator();
320 while(it.hasNext()) {
321 MooNoteElement elem = (MooNoteElement)it.next();
322 elem.getNote().setTick(elem.getNote().getTick() + ticks);
323 layoutElement(elem, true);
326 // If the selection should be moved downwards, traverses the list in the opposite order.
327 ArrayList selectedList = new ArrayList(selection);
328 ListIterator it = selectedList.listIterator(selectedList.size());
329 while(it.hasPrevious()) {
330 MooNoteElement elem = (MooNoteElement)it.previous();
331 elem.getNote().setTick(elem.getNote().getTick() + ticks);
332 layoutElement(elem, true);
335 Moosique.setEdited();
339 * Enables keyboard recording.
341 public void enableKeyboardRecording() {
342 keyboard.recordEnable();
346 * Disables keyboard recording.
348 public void disableKeyboardRecording() {
349 keyboard.recordDisable();
353 * Adds a menu item with the given command to the given popup menu.
355 private JMenuItem addMenuItem(JPopupMenu menu, String command) {
356 JMenuItem item = new JMenuItem(command);
357 item.addActionListener(new PopupListener());
363 * Adds a menu item with the given command to the given menu.
365 private JMenuItem addMenuItem(JMenu menu, String command) {
366 JMenuItem item = new JMenuItem(command);
367 item.addActionListener(new PopupListener());
373 * Creates a transpose sub menu with the given title in the given popup menu,
374 * inserting the items into the given array.
376 private JMenu createTransposeMenu(JPopupMenu menu, JMenuItem[] items, String title) {
377 JMenu trans = new JMenu("Transpose " + title);
379 items[0] = addMenuItem(trans, "One octave");
380 for (int i = 1; i < 12; i++) {
381 items[i] = addMenuItem(trans, (i) + " halftones");
387 * Shows a popup-menu with options for the current selection of note elements.
388 * @param c the component over which to display the menu
389 * @param x the x-coordinate in which to display the menu
390 * @param y the y-coordinate in which to display the menu
392 public void showSelectionPopup(Component c, int x, int y) {
393 // Determines whether the "Properties" item should be available.
394 selPopupProps.setEnabled(Moosique.getSelection().size() == 1);
395 selPopup.show(c, x, y);
399 * Draws the grid that is on the background.
400 * @param g The Graphics object used to draw the grid.
402 public void paintComponent(Graphics g) {
403 super.paintComponent(g);
404 Graphics2D g2 = (Graphics2D)g;
405 for (int c = 0; c < viewLength || c < getHeight(); c += NOTE_HEIGHT) {
406 for (int r = 0; r < (10 * NOTE_WIDTH); r += NOTE_WIDTH) {
407 box = new Rectangle(r, c, NOTE_WIDTH, NOTE_HEIGHT);
408 g2.setColor(Color.gray);
415 * Returns whether the left mouse button is currently pressed or not.
416 * @return true if the left mosue button is currently pressed
418 public boolean isLeftMouseButtonPressed() {
419 return leftMouseButtonPressed;
423 * The adapter used to listen on mouse actions
425 class MAdapter extends MouseAdapter {
428 * Deselects all note on click, adds a standard note on double click.
430 public void mouseClicked(MouseEvent e) {
431 if (SwingUtilities.isLeftMouseButton(e)) {
432 Moosique.deselectAllNotes();
433 if (e.getClickCount() == 2) {
435 addNoteAtClickY(lastNoteLength);
440 public void mousePressed(MouseEvent e) {
441 if (SwingUtilities.isLeftMouseButton(e)) leftMouseButtonPressed = true;
445 public void mouseReleased(MouseEvent e) {
446 if (SwingUtilities.isLeftMouseButton(e)) leftMouseButtonPressed = false;
451 * Shows the menu if an OS-specific popup-trigger was activated.
453 private void maybeShowPopup(MouseEvent e) {
454 if (e.isPopupTrigger()) {
456 popup.show(e.getComponent(), e.getX(), e.getY());
461 * Grabs the focus when the mouse has entered.
463 public void mouseEntered(MouseEvent e) {
464 Moosique.setActiveChannel(title.getChannel());
470 * Takes the appropriate action when a user selects an item on the popup menu.
472 class PopupListener implements ActionListener {
473 public void actionPerformed(ActionEvent e) {
474 Object source = e.getSource();
475 // Handling panel popup actions.
476 if (source == popupAddItemsLast) {
477 addNoteAtClickY(lastNoteLength);
478 } else if (source == popupAddItemsCustom) {
479 /* Show the user a dialog (identical to note preferences...
480 then call addNote(new MooNote(
481 title.getChannel(), pitch, velocity, timestamp, duration));
483 } else if (source == popupAddItemsWhole) {
485 } else if (source == popupAddItemsHalf) {
487 } else if (source == popupAddItemsQuarter) {
489 } else if (source == popupAddItemsEighth) {
491 } else if (source == popupAddItemsSixteenth) {
493 } else if (source == popupPaste) {
495 // Handling selection popup actions.
496 } else if (source == selPopupProps) {
497 new MooDialog(((MooNoteElement)Moosique.getSelection().first()).getNote());
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);