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 addNote(new MooNote(title.getChannel(), 60, 100, timestamp, Moosique.getSequence().getResolution() / 4));
180 * Draws the grid that is on the background.
181 * @param g The Graphics object used to draw the grid.
183 public void paintComponent(Graphics g) {
184 super.paintComponent(g);
185 Graphics2D g2 = (Graphics2D)g;
186 for (int c = 0; c < viewLength || c < getHeight(); c += NOTE_HEIGHT) {
187 for (int r = 0; r < (10 * NOTE_WIDTH); r += NOTE_WIDTH) {
188 box = new Rectangle(r, c, NOTE_WIDTH, NOTE_HEIGHT);
189 g2.setColor(Color.gray);
196 * The adapter used to listen on mouse actions
198 class MAdapter extends MouseAdapter {
200 * Adds a standard note if doubleclicked.
202 public void mouseClicked(MouseEvent e) {
203 if (e.getClickCount() == 2) {
210 * Shows the menu if on standard poptriggers.
212 public void mousePressed(MouseEvent e) {
213 if (e.isPopupTrigger()) {
215 popup.show(e.getComponent(), e.getX(), e.getY());
220 * Grabs the focus when the mouse has entered.
222 public void mouseEntered(MouseEvent e) {
223 // Moosique.setActiveChannel(track.getChannel());
229 * Listens on actions on the popupmenu and executes the appropriate action.
231 class PopupListener implements ActionListener {
232 public void actionPerformed(ActionEvent e) {
233 Object source = e.getSource();
234 if (source == popupAdd) {
237 // new MooNote(int channel, int pitch, int velocity, long timestamp, int duration)