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;
21 private JMenuItem popupAdd;
22 private ArrayList rects;
23 private ArrayList selected;
24 private Insets insets;
25 private int ticksPerSixteenth, popupY = 0;
26 protected static int viewLength = 0;
27 protected static int extraHeight = 0;
28 public static final int NOTE_HEIGHT = 10, NOTE_WIDTH = 40, VIEW_WIDTH = 200;
31 * Creates the trackview.
32 * @param track The track it represents graphically and operates on.
33 * @param title The object that is used to manipulate instrument, channel, solo, mute.
35 public MooTrackView (Track track, MooTrackTitle title) {
40 selected = new ArrayList();
43 setBackground(Color.white);
44 setBorder(BorderFactory.createLineBorder(Color.black));
46 setPreferredSize(new Dimension(VIEW_WIDTH, 140 * NOTE_HEIGHT));
48 // Creates temporary variables
51 rects = new ArrayList(track.size() / 2);
52 extraHeight = Toolkit.getDefaultToolkit().getScreenSize().height - 150;
54 // Places note elements
55 for (int i = 0; i < track.size(); i++) {
57 if (note instanceof MooNote) {
58 // Adds the note element to the note area and moves it to the appropriate place.
59 MooNote mn = (MooNote)note;
60 elem = new MooNoteElement(this, mn);
62 layoutElement(elem, false);
64 setPreferredSize(new Dimension(VIEW_WIDTH, viewLength + extraHeight));
68 // Creates pop-up menu.
69 popup = new JPopupMenu();
70 PopupListener pList = new PopupListener();
71 popupAdd = new JMenuItem("Add note...");
72 popupAdd.addActionListener(pList);
75 // Adds listeners for popup menu and keyboard synthesizer.
76 addMouseListener(new MAdapter());
77 addKeyListener(new MooKeyboard());
81 * Layouts the element to the right place.
82 * @param elem the element that will be layouted.
83 * @param old If true, this method will remove the old layout and set the new preferredSize for the trackview.
85 public void layoutElement(MooNoteElement elem, boolean old){
86 // If the element is currently in the view, removes its coordinates from the list.
87 Rectangle r = new Rectangle();
89 r = elem.getBounds(r);
90 for (Iterator i = rects.iterator(); i.hasNext();){
99 // Creates temporary variables.
100 ticksPerSixteenth = Moosique.getSequence().getResolution() / 4;
101 MooNote mn = elem.getNote();
104 // Calculates coordinates.
106 y = insets.top + (int)(mn.getTick() / ticksPerSixteenth) * NOTE_HEIGHT;
107 height = (mn.getDuration() / ticksPerSixteenth) * NOTE_HEIGHT;
108 if (height == 0) height = NOTE_HEIGHT;
109 r = new Rectangle(x, y, NOTE_WIDTH, height);
111 // Places the element in the appropriate place.
112 while(isOccupied(r)) r.translate(NOTE_WIDTH, 0);
115 if (viewLength < (y + height)){
116 viewLength = y + height;
117 if(old)setPreferredSize(new Dimension(VIEW_WIDTH, viewLength + extraHeight));
122 * Returns the track of this view.
123 * @return the track of this view
125 public Track getTrack() {
130 * Returns the title of this view.
131 * @return the title of this view
133 public MooTrackTitle getTitle() {
138 * Checks if the element can be fully drawn as this position without inteferring with other elements.
139 * @return true if the position is occupied.
141 private boolean isOccupied(Rectangle r) {
142 Iterator it = rects.iterator();
143 while (it.hasNext()) {
144 if(r.intersects((Rectangle)it.next())) return true;
150 * Adds the given note to the current track, and visualises it.
151 * @param mn the note to add
153 public void addNote(MooNote mn) {
155 MooNoteElement elem = new MooNoteElement(this, mn);
157 layoutElement(elem, false);
158 setPreferredSize(new Dimension(VIEW_WIDTH, viewLength + extraHeight));
159 Moosique.setEdited();
164 * Removes the given note element from the view and its note from the current track.
165 * @param elem the note element to remove
167 public void removeNote(MooNoteElement elem) {
168 elem.getNote().removeFrom(track);
170 elem.getNote().removeFrom(track);
171 Moosique.setEdited();
176 * Adds a standard note to this track.
178 private void addStandardNote() {
179 int row = (popupY - insets.top) / NOTE_HEIGHT;
180 long timestamp = (long)(ticksPerSixteenth * row);
181 addNote(new MooNote(title.getChannel(), 60, 100, timestamp, Moosique.getSequence().getResolution() / 4));
185 * Deselects all notes.
187 public void addSelected(MooNoteElement elem) {
192 * Deselects all notes.
194 public void removeSelected(MooNoteElement elem) {
195 selected.remove(selected.indexOf(elem));
199 * Deselects all notes.
201 public void deselectAll() {
202 Iterator it = selected.iterator();
203 while(it.hasNext()) {
204 ((MooNoteElement)it.next()).deselect();
210 * Determines if the given MooNoteElement is the only one in the track view that is selected.
211 * @return if the given element is the only selected one
213 public boolean isTheOnlySelected(MooNoteElement elem) {
214 Iterator it = selected.iterator();
215 while(it.hasNext()) {
216 if (!it.next().equals(elem)) return false;
222 * Shows a popup-menu with options for the current selection of note elements.
223 * @param c the component over which to display the menu
224 * @param x the x-coordinate in which to display the menu
225 * @param y the y-coordinate in which to display the menu
227 public void showSelectionPopup(Component c, int x, int y) {
232 * Draws the grid that is on the background.
233 * @param g The Graphics object used to draw the grid.
235 public void paintComponent(Graphics g) {
236 super.paintComponent(g);
237 Graphics2D g2 = (Graphics2D)g;
238 for (int c = 0; c < viewLength || c < getHeight(); c += NOTE_HEIGHT) {
239 for (int r = 0; r < (10 * NOTE_WIDTH); r += NOTE_WIDTH) {
240 box = new Rectangle(r, c, NOTE_WIDTH, NOTE_HEIGHT);
241 g2.setColor(Color.gray);
248 * The adapter used to listen on mouse actions
250 class MAdapter extends MouseAdapter {
253 * Deselects all note on click, adds a standard note on double click.
255 public void mouseClicked(MouseEvent e) {
257 if (e.getClickCount() == 2) {
263 public void mousePressed(MouseEvent e) {
267 public void mouseReleased(MouseEvent e) {
272 * Selects the notes within the area that was selected.
274 public void mouseDragged(MouseEvent e) {
279 * Shows the menu if an OS-specific popup-trigger was activated.
281 private void maybeShowPopup(MouseEvent e) {
282 if (e.isPopupTrigger()) {
284 popup.show(e.getComponent(), e.getX(), e.getY());
289 * Grabs the focus when the mouse has entered.
291 public void mouseEntered(MouseEvent e) {
292 // Moosique.setActiveChannel(track.getChannel());
298 * Listens on actions on the popupmenu and executes the appropriate action.
300 class PopupListener implements ActionListener {
301 public void actionPerformed(ActionEvent e) {
302 Object source = e.getSource();
303 if (source == popupAdd) {
306 // new MooNote(int channel, int pitch, int velocity, long timestamp, int duration)