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;
23 private JMenuItem selPopupRemove, selPopupTranspUpOct, selPopupTranspDownOct;
25 private ArrayList rects;
26 private ArrayList selected;
27 private Insets insets;
28 private int ticksPerSixteenth, popupY = 0;
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 selected = new ArrayList();
46 setBackground(Color.white);
47 setBorder(BorderFactory.createLineBorder(Color.black));
49 setPreferredSize(new Dimension(VIEW_WIDTH, 140 * NOTE_HEIGHT));
51 // Creates temporary variables
54 rects = new ArrayList(track.size() / 2);
55 extraHeight = Toolkit.getDefaultToolkit().getScreenSize().height - 150;
57 // Places note elements
58 for (int i = 0; i < track.size(); i++) {
60 if (note instanceof MooNote) {
61 // Adds the note element to the note area and moves it to the appropriate place.
62 MooNote mn = (MooNote)note;
63 elem = new MooNoteElement(this, mn);
65 layoutElement(elem, false);
67 setPreferredSize(new Dimension(VIEW_WIDTH, viewLength + extraHeight));
71 // Creates panel pop-up menu.
72 popup = new JPopupMenu();
73 PopupListener pList = new PopupListener();
74 popupAdd = new JMenuItem("Add note...");
75 popupAdd.addActionListener(pList);
78 // Creates selection pop-up menu.
79 selPopup = new JPopupMenu();
80 selPopupRemove = new JMenuItem("Remove selection");
81 selPopupRemove.addActionListener(pList);
82 selPopup.add(selPopupRemove);
83 selPopupTranspUp = new JMenu("Transpose selection up");
84 selPopup.add(selPopupTranspUp);
85 selPopupTranspUpOct = new JMenuItem("One octave");
86 selPopupTranspUpOct.addActionListener(pList);
87 selPopupTranspUp.add(selPopupTranspUpOct);
88 selPopupTranspDown = new JMenu("Transpose selection down");
89 selPopup.add(selPopupTranspDown);
90 selPopupTranspDownOct = new JMenuItem("One octave");
91 selPopupTranspDownOct.addActionListener(pList);
92 selPopupTranspDown.add(selPopupTranspDownOct);
94 // Adds listeners for popup menu and keyboard synthesizer.
95 addMouseListener(new MAdapter());
96 addKeyListener(new MooKeyboard());
100 * Layouts the element to the right place.
101 * @param elem the element that will be layouted.
102 * @param old If true, this method will remove the old layout and set the new preferredSize for the trackview.
104 public void layoutElement(MooNoteElement elem, boolean old){
105 // If the element is currently in the view, removes its coordinates from the list.
106 Rectangle r = new Rectangle();
108 r = elem.getBounds(r);
109 for (Iterator i = rects.iterator(); i.hasNext();){
110 Object ob = i.next();
118 // Creates temporary variables.
119 ticksPerSixteenth = Moosique.getSequence().getResolution() / 4;
120 MooNote mn = elem.getNote();
123 // Calculates coordinates.
125 y = insets.top + (int)(mn.getTick() / ticksPerSixteenth) * NOTE_HEIGHT;
126 height = (mn.getDuration() / ticksPerSixteenth) * NOTE_HEIGHT;
127 if (height == 0) height = NOTE_HEIGHT;
128 r = new Rectangle(x, y, NOTE_WIDTH, height);
130 // Places the element in the appropriate place.
131 while(isOccupied(r)) r.translate(NOTE_WIDTH, 0);
134 if (viewLength < (y + height)){
135 viewLength = y + height;
136 if(old)setPreferredSize(new Dimension(VIEW_WIDTH, viewLength + extraHeight));
142 * Returns the track of this view.
143 * @return the track of this view
145 public Track getTrack() {
150 * Returns the title of this view.
151 * @return the title of this view
153 public MooTrackTitle getTitle() {
158 * Checks if the element can be fully drawn as this position without inteferring with other elements.
159 * @return true if the position is occupied.
161 private boolean isOccupied(Rectangle r) {
162 Iterator it = rects.iterator();
163 while (it.hasNext()) {
164 if(r.intersects((Rectangle)it.next())) return true;
170 * Adds the given note to the current track, and visualises it.
171 * @param mn the note to add
173 public void addNote(MooNote mn) {
175 MooNoteElement elem = new MooNoteElement(this, mn);
177 layoutElement(elem, false);
178 setPreferredSize(new Dimension(VIEW_WIDTH, viewLength + extraHeight));
179 Moosique.setEdited();
184 * Adds a standard note to this track.
186 private void addStandardNote() {
187 int row = (popupY - insets.top) / NOTE_HEIGHT;
188 long timestamp = (long)(ticksPerSixteenth * row);
189 addNote(new MooNote(title.getChannel(), 60, 100, timestamp, Moosique.getSequence().getResolution() / 4));
193 * Removes the given note element from the view and its note from the current track.
194 * @param elem the note element to remove
196 public void removeNote(MooNoteElement elem) {
197 elem.getNote().removeFrom(track);
199 Rectangle r = new Rectangle();
200 r = elem.getBounds(r);
202 Moosique.setEdited();
207 * Deselects all notes.
209 public void selectNote(MooNoteElement elem) {
214 * Deselects all notes.
216 public void deselectNote(MooNoteElement elem) {
217 selected.remove(selected.indexOf(elem));
221 * Deselects all notes.
223 public void deselectAllNotes() {
224 Iterator it = selected.iterator();
225 while(it.hasNext()) {
226 ((MooNoteElement)it.next()).deselect();
232 * Determines if the given MooNoteElement is the only one in the track view that is selected.
233 * @return if the given element is the only selected one
235 public boolean isTheOnlySelected(MooNoteElement elem) {
236 Iterator it = selected.iterator();
237 while(it.hasNext()) {
238 if (!it.next().equals(elem)) return false;
244 * Shows a popup-menu with options for the current selection of note elements.
245 * @param c the component over which to display the menu
246 * @param x the x-coordinate in which to display the menu
247 * @param y the y-coordinate in which to display the menu
249 public void showSelectionPopup(Component c, int x, int y) {
250 selPopup.show(c, x, y);
254 * Transposes all selected notes the given number of halftones.
256 private void transposeSelectedNotes(int halftones) {
257 Iterator it = selected.iterator();
258 while(it.hasNext()) {
259 MooNoteElement elem = (MooNoteElement)it.next();
260 elem.getNote().transpose(halftones);
266 * Draws the grid that is on the background.
267 * @param g The Graphics object used to draw the grid.
269 public void paintComponent(Graphics g) {
270 super.paintComponent(g);
271 Graphics2D g2 = (Graphics2D)g;
272 for (int c = 0; c < viewLength || c < getHeight(); c += NOTE_HEIGHT) {
273 for (int r = 0; r < (10 * NOTE_WIDTH); r += NOTE_WIDTH) {
274 box = new Rectangle(r, c, NOTE_WIDTH, NOTE_HEIGHT);
275 g2.setColor(Color.gray);
282 * The adapter used to listen on mouse actions
284 class MAdapter extends MouseAdapter {
287 * Deselects all note on click, adds a standard note on double click.
289 public void mouseClicked(MouseEvent e) {
290 if (SwingUtilities.isLeftMouseButton(e)) {
292 if (e.getClickCount() == 2) {
299 public void mousePressed(MouseEvent e) {
303 public void mouseReleased(MouseEvent e) {
308 * Selects the notes within the area that was selected.
310 public void mouseDragged(MouseEvent e) {
315 * Shows the menu if an OS-specific popup-trigger was activated.
317 private void maybeShowPopup(MouseEvent e) {
318 if (e.isPopupTrigger()) {
320 popup.show(e.getComponent(), e.getX(), e.getY());
325 * Grabs the focus when the mouse has entered.
327 public void mouseEntered(MouseEvent e) {
328 // Moosique.setActiveChannel(track.getChannel());
334 * Listens on actions on the popup menu and executes the appropriate action.
336 class PopupListener implements ActionListener {
337 public void actionPerformed(ActionEvent e) {
338 Object source = e.getSource();
339 if (source == popupAdd) {
341 } else if (source == selPopupRemove) {
342 Iterator it = selected.iterator();
343 while(it.hasNext()) {
344 removeNote((MooNoteElement)it.next());
347 } else if (source == selPopupTranspUpOct) {
348 transposeSelectedNotes(12);
349 } else if (source == selPopupTranspDownOct) {
350 transposeSelectedNotes(-12);