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 Insets insets;
24 private int ticksPerSixteenth, popupY = 0;
25 protected static int viewLength = 0;
26 protected static int extraHeight = 0;
27 public static final int NOTE_HEIGHT = 10, NOTE_WIDTH = 40, VIEW_WIDTH = 200;
30 * Creates the trackview.
31 * @param track The track it represents graphically and operates on.
32 * @param title The object that is used to manipulate instrument, channel, solo, mute.
34 public MooTrackView (Track track, MooTrackTitle title) {
41 setBackground(Color.white);
42 setBorder(BorderFactory.createLineBorder(Color.black));
44 setPreferredSize(new Dimension(VIEW_WIDTH, 140 * NOTE_HEIGHT));
46 // Creates temporary variables
49 rects = new ArrayList(track.size() / 2);
50 extraHeight = Toolkit.getDefaultToolkit().getScreenSize().height - 150;
52 // Places note elements
53 for (int i = 0; i < track.size(); i++) {
55 if (note instanceof MooNote) {
56 // Adds the note element to the note area and moves it to the appropriate place.
57 MooNote mn = (MooNote)note;
58 elem = new MooNoteElement(this, mn);
60 layoutElement(elem, false);
62 setPreferredSize(new Dimension(VIEW_WIDTH, viewLength + extraHeight));
66 // Creates pop-up menu.
67 popup = new JPopupMenu();
68 PopupListener pList = new PopupListener();
69 popupAdd = new JMenuItem("Add note...");
70 popupAdd.addActionListener(pList);
73 // Adds listeners for popup menu and keyboard synthesizer.
74 addMouseListener(new MAdapter());
75 addKeyListener(new MooKeyboard());
79 * Layouts the element to the right place.
80 * @param elem the element that will be layouted.
81 * @param old If true, this method will remove the old layout and set the new preferredSize for the trackview.
83 public void layoutElement(MooNoteElement elem, boolean old){
84 // If the element is currently in the view, removes its coordinates from the list.
85 Rectangle r = new Rectangle();
87 r = elem.getBounds(r);
88 for (Iterator i = rects.iterator(); i.hasNext();){
97 // Creates temporary variables.
98 ticksPerSixteenth = Moosique.getSequence().getResolution() / 4;
99 MooNote mn = elem.getNote();
102 // Calculates coordinates.
104 y = insets.top + (int)(mn.getTick() / ticksPerSixteenth) * NOTE_HEIGHT;
105 height = (mn.getDuration() / ticksPerSixteenth) * NOTE_HEIGHT;
106 if (height == 0) height = NOTE_HEIGHT;
107 r = new Rectangle(x, y, NOTE_WIDTH, height);
109 // Places the element in the appropriate place.
110 while(isOccupied(r)) r.translate(NOTE_WIDTH, 0);
113 if (viewLength < (y + height)){
114 viewLength = y + height;
115 if(old)setPreferredSize(new Dimension(VIEW_WIDTH, viewLength + extraHeight));
120 * Returns the track of this view.
121 * @return the track of this view
123 public Track getTrack() {
128 * Returns the title of this view.
129 * @return the title of this view
131 public MooTrackTitle getTitle() {
136 * Checks if the element can be fully drawn as this position without inteferring with other elements.
137 * @return true if the position is occupied.
139 private boolean isOccupied(Rectangle r) {
140 Iterator it = rects.iterator();
141 while (it.hasNext()) {
142 if(r.intersects((Rectangle)it.next())) return true;
148 * Adds the given note to the current track, and visualises it.
149 * @param mn the note to add
151 public void addNote(MooNote mn) {
153 MooNoteElement elem = new MooNoteElement(this, mn);
155 layoutElement(elem, false);
156 setPreferredSize(new Dimension(VIEW_WIDTH, viewLength + extraHeight));
161 * Removes the given note element from the view and its note from the current track.
162 * @param elem the note element to remove
164 public void removeNote(MooNoteElement elem) {
165 elem.getNote().removeFrom(track);
167 elem.getNote().removeFrom(track);
172 * Adds a standard note to this track.
174 private void addStandardNote() {
175 long timestamp = (long)(ticksPerSixteenth * (popupY - insets.top) / NOTE_HEIGHT);
176 System.out.println(ticksPerSixteenth + ", " + popupY + ", " + insets.top + ", " + timestamp);
177 addNote(new MooNote(title.getChannel(), 60, 100, timestamp, Moosique.getSequence().getResolution() / 4));
181 * Draws the grid that is on the background.
182 * @param g The Graphics object used to draw the grid.
184 public void paintComponent(Graphics g) {
185 super.paintComponent(g);
186 Graphics2D g2 = (Graphics2D)g;
187 for (int c = 0; c < viewLength || c < getHeight(); c += NOTE_HEIGHT) {
188 for (int r = 0; r < (10 * NOTE_WIDTH); r += NOTE_WIDTH) {
189 box = new Rectangle(r, c, NOTE_WIDTH, NOTE_HEIGHT);
190 g2.setColor(Color.gray);
197 * The adapter used to listen on mouse actions
199 class MAdapter extends MouseAdapter {
201 * Adds a standard note if doubleclicked.
203 public void mouseClicked(MouseEvent e) {
204 if (e.getClickCount() == 2) {
211 * Shows the menu if on standard poptriggers.
213 public void mousePressed(MouseEvent e) {
214 if (e.isPopupTrigger()) {
216 popup.show(e.getComponent(), e.getX(), e.getY());
221 * Grabs the focus when the mouse has entered.
223 public void mouseEntered(MouseEvent e) {
224 // Moosique.setActiveChannel(track.getChannel());
230 * Listens on actions on the popupmenu and executes the appropriate action.
232 class PopupListener implements ActionListener {
233 public void actionPerformed(ActionEvent e) {
234 Object source = e.getSource();
235 if (source == popupAdd) {
238 // new MooNote(int channel, int pitch, int velocity, long timestamp, int duration)