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 Rectangle box;
20 private JPopupMenu popup, selPopup;
21 private JMenu selPopupTranspUp, selPopupTranspDown;
22 private JMenuItem popupAdd, popupPaste;
23 private JMenuItem selPopupCopy, selPopupCut, selPopupRemove, selPopupTranspUpOct, selPopupTranspDownOct;
25 private ArrayList coords, selection, copyBuffer;
26 private Insets insets;
27 private int ticksPerSixteenth, popupY = 0;
28 private boolean leftMouseButtonPressed = false;
29 protected static int viewLength = 0;
30 protected static int extraHeight = 0;
31 public static final int NOTE_HEIGHT = 10, NOTE_WIDTH = 40, VIEW_WIDTH = 200;
34 * Creates the trackview.
35 * @param track The track it represents graphically and operates on.
36 * @param title The object that is used to manipulate instrument, channel, solo, mute.
38 public MooTrackView (Track track, MooTrackTitle title) {
43 // Creates instance variables
45 coords = new ArrayList(track.size() / 2);
46 selection = new ArrayList();
47 copyBuffer = new ArrayList();
49 // Creates temporary variables
52 extraHeight = Toolkit.getDefaultToolkit().getScreenSize().height - 150;
55 setBackground(Color.white);
56 setBorder(BorderFactory.createLineBorder(Color.black));
58 setPreferredSize(new Dimension(VIEW_WIDTH, 140 * NOTE_HEIGHT));
60 // Places note elements
61 for (int i = 0; i < track.size(); i++) {
63 if (note instanceof MooNote) {
64 // Adds the note element to the note area and moves it to the appropriate place.
65 MooNote mn = (MooNote)note;
66 elem = new MooNoteElement(this, mn);
68 layoutElement(elem, false);
70 setPreferredSize(new Dimension(VIEW_WIDTH, viewLength + extraHeight));
74 // Creates panel pop-up menu.
75 popup = new JPopupMenu();
76 PopupListener pList = new PopupListener();
77 popupAdd = new JMenuItem("Add note...");
78 popupAdd.addActionListener(pList);
80 popupPaste = new JMenuItem("Paste");
81 popupPaste.addActionListener(pList);
82 popup.add(popupPaste);
84 // Creates selection pop-up menu.
85 selPopup = new JPopupMenu();
86 selPopupCopy = new JMenuItem("Copy selection");
87 selPopupCopy.addActionListener(pList);
88 selPopup.add(selPopupCopy);
89 selPopupCut = new JMenuItem("Cut selection");
90 selPopupCut.addActionListener(pList);
91 selPopup.add(selPopupCut);
92 selPopupRemove = new JMenuItem("Remove selection");
93 selPopupRemove.addActionListener(pList);
94 selPopup.add(selPopupRemove);
95 selPopupTranspUp = new JMenu("Transpose selection up");
96 selPopup.add(selPopupTranspUp);
97 selPopupTranspUpOct = new JMenuItem("One octave");
98 selPopupTranspUpOct.addActionListener(pList);
99 selPopupTranspUp.add(selPopupTranspUpOct);
100 selPopupTranspDown = new JMenu("Transpose selection down");
101 selPopup.add(selPopupTranspDown);
102 selPopupTranspDownOct = new JMenuItem("One octave");
103 selPopupTranspDownOct.addActionListener(pList);
104 selPopupTranspDown.add(selPopupTranspDownOct);
106 // Adds listeners for popup menu and keyboard synthesizer.
107 addMouseListener(new MAdapter());
108 addKeyListener(new MooKeyboard());
112 * Layouts the element to the right place.
113 * @param elem the element that will be layouted.
114 * @param old If true, this method will remove the old layout and set the new preferredSize for the trackview.
116 public void layoutElement(MooNoteElement elem, boolean old){
117 // If the element is currently in the view, removes its coordinates from the list.
118 Rectangle r = new Rectangle();
120 r = elem.getBounds(r);
121 for (Iterator i = coords.iterator(); i.hasNext();){
122 Object ob = i.next();
130 // Creates temporary variables.
131 ticksPerSixteenth = Moosique.getSequence().getResolution() / 4;
132 MooNote mn = elem.getNote();
135 // Calculates coordinates.
137 y = insets.top + Math.round(mn.getTick() / ticksPerSixteenth) * NOTE_HEIGHT;
138 height = (mn.getDuration() / ticksPerSixteenth) * NOTE_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(selection.indexOf(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 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, new Moosique.NoteComparator());
275 * Cuts the current selection.
277 public void cutSelectedNotes() {
279 removeSelectedNotes();
283 * Pastes the current copy buffer at the given timestamp.
285 public void pasteCopiedNotes() {
286 int row = (popupY - insets.top) / NOTE_HEIGHT;
287 long timestamp = (long)(ticksPerSixteenth * row);
288 if (copyBuffer.size() > 0) {
289 long startTime = ((MooNote)copyBuffer.get(0)).getTick();
290 Iterator it = copyBuffer.iterator();
291 while(it.hasNext()) {
292 MooNote mn = (MooNote)((MooNote)it.next()).clone();
293 mn.setTick(mn.getTick() - startTime + timestamp);
300 * Removes the current selection.
302 public void removeSelectedNotes() {
303 Iterator it = selection.iterator();
304 while(it.hasNext()) {
305 removeNote((MooNoteElement)it.next());
311 * Transposes all selected notes the given number of halftones.
313 private void transposeSelectedNotes(int halftones) {
314 Iterator it = selection.iterator();
315 while(it.hasNext()) {
316 MooNoteElement elem = (MooNoteElement)it.next();
317 elem.transpose(halftones);
322 * Moves the current selection, if the mouse was pressed on a note element
323 * and then released in another row.
324 * @param srcY the y-coordinate of the point in which the mouse button was pressed
325 * @param destY the y-coordinate of the point in which the mouse button was released
327 public void maybeMoveSelectedNotes(int srcY, int destY) {
328 int srcRow = (srcY - insets.top) / NOTE_HEIGHT;
329 int destRow = (destY - insets.top) / NOTE_HEIGHT;
330 long timeDiff = (long)(ticksPerSixteenth * (destRow - srcRow));
331 Iterator it = selection.iterator();
332 while(it.hasNext()) {
333 MooNoteElement elem = (MooNoteElement)it.next();
334 elem.getNote().setTick(elem.getNote().getTick() + timeDiff);
335 layoutElement(elem, true);
340 * Shows a popup-menu with options for the current selection of note elements.
341 * @param c the component over which to display the menu
342 * @param x the x-coordinate in which to display the menu
343 * @param y the y-coordinate in which to display the menu
345 public void showSelectionPopup(Component c, int x, int y) {
346 selPopup.show(c, x, y);
350 * Draws the grid that is on the background.
351 * @param g The Graphics object used to draw the grid.
353 public void paintComponent(Graphics g) {
354 super.paintComponent(g);
355 Graphics2D g2 = (Graphics2D)g;
356 for (int c = 0; c < viewLength || c < getHeight(); c += NOTE_HEIGHT) {
357 for (int r = 0; r < (10 * NOTE_WIDTH); r += NOTE_WIDTH) {
358 box = new Rectangle(r, c, NOTE_WIDTH, NOTE_HEIGHT);
359 g2.setColor(Color.gray);
366 * Returns whether the left mouse button is currently pressed or not.
367 * @return true if the left mosue button is currently pressed
369 public boolean isLeftMouseButtonPressed() {
370 return leftMouseButtonPressed;
374 * The adapter used to listen on mouse actions
376 class MAdapter extends MouseAdapter {
379 * Deselects all note on click, adds a standard note on double click.
381 public void mouseClicked(MouseEvent e) {
382 if (SwingUtilities.isLeftMouseButton(e)) {
384 if (e.getClickCount() == 2) {
391 public void mousePressed(MouseEvent e) {
392 if (SwingUtilities.isLeftMouseButton(e)) leftMouseButtonPressed = true;
396 public void mouseReleased(MouseEvent e) {
397 if (SwingUtilities.isLeftMouseButton(e)) leftMouseButtonPressed = false;
402 * Shows the menu if an OS-specific popup-trigger was activated.
404 private void maybeShowPopup(MouseEvent e) {
405 if (e.isPopupTrigger()) {
407 popup.show(e.getComponent(), e.getX(), e.getY());
412 * Grabs the focus when the mouse has entered.
414 public void mouseEntered(MouseEvent e) {
415 Moosique.setActiveChannel(title.getChannel());
421 * Listens on actions on the popup menu and executes the appropriate action.
423 class PopupListener implements ActionListener {
424 public void actionPerformed(ActionEvent e) {
425 Object source = e.getSource();
426 // Handling panel popup actions.
427 if (source == popupAdd) {
429 } else if (source == popupPaste) {
431 // Handling selection popup actions.
432 } else if (source == selPopupCopy) {
434 } else if (source == selPopupCut) {
436 } else if (source == selPopupRemove) {
437 removeSelectedNotes();
438 } else if (source == selPopupTranspUpOct) {
439 transposeSelectedNotes(12);
440 } else if (source == selPopupTranspDownOct) {
441 transposeSelectedNotes(-12);