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, quantizeRecording = 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));
55 placeNoteElements(false);
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(boolean quantize) {
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 if (quantizeRecording) {
138 y = insets.top + (int)((mn.getTick() * NOTE_HEIGHT) / ticksPerSixteenth);
139 height = (mn.getDuration() * NOTE_HEIGHT) / ticksPerSixteenth;
141 if (height == 0) height = NOTE_HEIGHT;
142 r = new Rectangle(x, y, NOTE_WIDTH, height);
144 // Places the element in the appropriate place.
145 while(isOccupied(r)) r.translate(NOTE_WIDTH, 0);
148 if (viewLength < (y + height)){
149 viewLength = y + height;
150 if(old)setPreferredSize(new Dimension(VIEW_WIDTH, viewLength + extraHeight));
159 * Returns the track of this view.
160 * @return the track of this view
162 public Track getTrack() {
167 * Returns the title of this view.
168 * @return the title of this view
170 public MooTrackTitle getTitle() {
175 * Checks if the element can be fully drawn as this position without inteferring with other elements.
176 * @return true if the position is occupied.
178 private boolean isOccupied(Rectangle r) {
179 Iterator it = coords.iterator();
180 while (it.hasNext()) {
181 if(r.intersects((Rectangle)it.next())) return true;
187 * Adds the given note to the current track, and visualises it.
188 * @param mn the note to add
190 public void addNote(MooNote mn) {
192 MooNoteElement elem = new MooNoteElement(this, mn);
194 layoutElement(elem, false);
195 setPreferredSize(new Dimension(VIEW_WIDTH, viewLength + extraHeight));
196 Moosique.setEdited();
202 * Adds a standard note to this track.
204 private void addStandardNote() {
205 int row = (popupY - insets.top) / NOTE_HEIGHT;
206 long timestamp = (long)(ticksPerSixteenth * row);
207 addNote(new MooNote(title.getChannel(), 60, 100, timestamp, Moosique.getSequence().getResolution() / 4));
211 * Removes the given note element from the view and its note from the current track.
212 * @param elem the note element to remove
214 public void removeNote(MooNoteElement elem) {
215 elem.getNote().removeFrom(track);
217 Rectangle r = new Rectangle();
218 r = elem.getBounds(r);
220 Moosique.setEdited();
226 * Selects the given note
227 * @param the note to select
229 public void selectNote(MooNoteElement elem) {
234 * Deselects the given note
235 * @param the note to deselect
237 public void deselectNote(MooNoteElement elem) {
238 selection.remove(elem);
242 * Deselects all notes.
244 public void deselectAllNotes() {
245 Iterator it = selection.iterator();
246 while(it.hasNext()) {
247 ((MooNoteElement)it.next()).deselect();
253 * Determines if the given MooNoteElement is the only one in the track view that is selected.
254 * @return if the given element is the only selected one
256 public boolean isTheOnlySelected(MooNoteElement elem) {
257 Iterator it = selection.iterator();
258 while(it.hasNext()) {
259 if (!it.next().equals(elem)) return false;
265 * Copies the current selection.
267 public void copySelectedNotes() {
268 ArrayList copyBuffer = new ArrayList(selection.size());
269 Iterator it = selection.iterator();
270 while(it.hasNext()) {
271 copyBuffer.add(((MooNoteElement)it.next()).getNote().clone());
273 Collections.sort(copyBuffer);
274 Moosique.setCopyBuffer(copyBuffer);
278 * Cuts the current selection.
280 public void cutSelectedNotes() {
282 removeSelectedNotes();
286 * Pastes the current copy buffer at the given timestamp.
288 public void pasteCopiedNotes() {
289 int row = (popupY - insets.top) / NOTE_HEIGHT;
290 long timestamp = (long)(ticksPerSixteenth * row);
291 ArrayList copyBuffer = Moosique.getCopyBuffer();
292 if (copyBuffer.size() > 0) {
293 long startTime = ((MooNote)copyBuffer.get(0)).getTick();
294 Iterator it = copyBuffer.iterator();
295 while(it.hasNext()) {
296 MooNote mn = (MooNote)((MooNote)it.next()).clone();
297 mn.setTick(mn.getTick() - startTime + timestamp);
298 mn.setChannel(title.getChannel());
305 * Removes the current selection.
307 public void removeSelectedNotes() {
308 Iterator it = selection.iterator();
309 while(it.hasNext()) {
310 removeNote((MooNoteElement)it.next());
316 * Transposes all selected notes the given number of halftones.
318 private void transposeSelectedNotes(int halftones) {
319 Iterator it = selection.iterator();
320 while(it.hasNext()) {
321 MooNoteElement elem = (MooNoteElement)it.next();
322 elem.transpose(halftones);
327 * Moves the current selection the given number of ticks.
328 * @param ticks the number of ticks to move the selection.
330 public void moveSelectedNotes(int ticks) {
332 // If the selection should be moved upwards, traverses the list in the natural order.
333 Iterator it = selection.iterator();
334 while(it.hasNext()) {
335 MooNoteElement elem = (MooNoteElement)it.next();
336 elem.getNote().setTick(elem.getNote().getTick() + ticks);
337 layoutElement(elem, true);
340 // If the selection should be moved downwards, traverses the list in the opposite order.
341 ArrayList selectedList = new ArrayList(selection);
342 ListIterator it = selectedList.listIterator(selectedList.size());
343 while(it.hasPrevious()) {
344 MooNoteElement elem = (MooNoteElement)it.previous();
345 elem.getNote().setTick(elem.getNote().getTick() + ticks);
346 layoutElement(elem, true);
352 * Moves the current selection, depending on the given delta y.
353 * @param y the number of pixels the selection is moved.
355 public void maybeMoveSelectedNotes(int y) {
356 moveSelectedNotes(ticksPerSixteenth * (y / NOTE_HEIGHT));
360 * Enables keyboard recording.
362 public void enableKeyboardRecording() {
363 keyboard.recordEnable();
367 * Disables keyboard recording.
369 public void disableKeyboardRecording() {
370 keyboard.recordDisable();
374 * Adds a menu item with the given command to the given popup menu.
376 private JMenuItem addMenuItem(JPopupMenu menu, String command) {
377 JMenuItem item = new JMenuItem(command);
378 item.addActionListener(new PopupListener());
384 * Adds a menu item with the given command to the given menu.
386 private JMenuItem addMenuItem(JMenu menu, String command) {
387 JMenuItem item = new JMenuItem(command);
388 item.addActionListener(new PopupListener());
394 * Creates a transpose sub menu with the given title in the given popup menu,
395 * inserting the items into the given array.
397 private JMenu createTransposeMenu(JPopupMenu menu, JMenuItem[] items, String title) {
398 JMenu trans = new JMenu("Transpose " + title);
400 items[0] = addMenuItem(trans, "One octave");
401 for (int i = 1; i < 12; i++) {
402 items[i] = addMenuItem(trans, (i) + " halftones");
408 * Shows a popup-menu with options for the current selection of note elements.
409 * @param c the component over which to display the menu
410 * @param x the x-coordinate in which to display the menu
411 * @param y the y-coordinate in which to display the menu
413 public void showSelectionPopup(Component c, int x, int y) {
414 selPopup.show(c, x, y);
418 * Draws the grid that is on the background.
419 * @param g The Graphics object used to draw the grid.
421 public void paintComponent(Graphics g) {
422 super.paintComponent(g);
423 Graphics2D g2 = (Graphics2D)g;
424 for (int c = 0; c < viewLength || c < getHeight(); c += NOTE_HEIGHT) {
425 for (int r = 0; r < (10 * NOTE_WIDTH); r += NOTE_WIDTH) {
426 box = new Rectangle(r, c, NOTE_WIDTH, NOTE_HEIGHT);
427 g2.setColor(Color.gray);
434 * Returns whether the left mouse button is currently pressed or not.
435 * @return true if the left mosue button is currently pressed
437 public boolean isLeftMouseButtonPressed() {
438 return leftMouseButtonPressed;
442 * The adapter used to listen on mouse actions
444 class MAdapter extends MouseAdapter {
447 * Deselects all note on click, adds a standard note on double click.
449 public void mouseClicked(MouseEvent e) {
450 if (SwingUtilities.isLeftMouseButton(e)) {
452 if (e.getClickCount() == 2) {
459 public void mousePressed(MouseEvent e) {
460 if (SwingUtilities.isLeftMouseButton(e)) leftMouseButtonPressed = true;
464 public void mouseReleased(MouseEvent e) {
465 if (SwingUtilities.isLeftMouseButton(e)) leftMouseButtonPressed = false;
470 * Shows the menu if an OS-specific popup-trigger was activated.
472 private void maybeShowPopup(MouseEvent e) {
473 if (e.isPopupTrigger()) {
475 popup.show(e.getComponent(), e.getX(), e.getY());
480 * Grabs the focus when the mouse has entered.
482 public void mouseEntered(MouseEvent e) {
483 Moosique.setActiveChannel(title.getChannel());
489 * Takes the appropriate action when a user selects an item on the popup menu.
491 class PopupListener implements ActionListener {
492 public void actionPerformed(ActionEvent e) {
493 Object source = e.getSource();
494 // Handling panel popup actions.
495 if (source == popupAdd) {
497 } else if (source == popupPaste) {
499 // Handling selection popup actions.
500 } else if (source == selPopupCopy) {
502 } else if (source == selPopupCut) {
504 } else if (source == selPopupRemove) {
505 removeSelectedNotes();
506 } else if (source == selPopupTranspUpItems[0]) {
507 transposeSelectedNotes(12);
508 } else if (source == selPopupTranspDownItems[0]) {
509 transposeSelectedNotes(-12);
511 for (int i = 1; i < 12; i++) {
512 if (source == selPopupTranspUpItems[i]) transposeSelectedNotes(i);
513 else if (source == selPopupTranspDownItems[i]) transposeSelectedNotes(-i);