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;
29 public MooTrackView (Track track, MooTrackTitle title) {
36 setBackground(Color.white);
37 setBorder(BorderFactory.createLineBorder(Color.black));
39 setPreferredSize(new Dimension(VIEW_WIDTH, 140 * NOTE_HEIGHT));
41 // Creates temporary variables
44 rects = new ArrayList(track.size() / 2);
45 extraHeight = Toolkit.getDefaultToolkit().getScreenSize().height - 150;
47 // Places note elements
48 for (int i = 0; i < track.size(); i++) {
50 if (note instanceof MooNote) {
51 // Adds the note element to the note area and moves it to the appropriate place.
52 MooNote mn = (MooNote)note;
53 elem = new MooNoteElement(this, mn);
55 layoutElement(elem, false);
57 setPreferredSize(new Dimension(VIEW_WIDTH, viewLength + extraHeight));
61 // Creates pop-up menu.
62 popup = new JPopupMenu();
63 PopupListener pList = new PopupListener();
64 popupAdd = new JMenuItem("Add note...");
65 popupAdd.addActionListener(pList);
68 // Adds listeners for popup menu and keyboard synthesizer.
69 addMouseListener(new MAdapter());
70 addKeyListener(new MooKeyboard());
73 public void layoutElement(MooNoteElement elem, boolean old){
74 // If the element is currently in the view, removes its coordinates from the list.
75 Rectangle r = new Rectangle();
77 r = elem.getBounds(r);
78 for (Iterator i = rects.iterator(); i.hasNext();){
87 // Creates temporary variables.
88 ticksPerSixteenth = Moosique.getSequence().getResolution() / 4;
89 MooNote mn = elem.getNote();
92 // Calculates coordinates.
94 y = insets.top + (int)(mn.getTick() / ticksPerSixteenth) * NOTE_HEIGHT;
95 height = (mn.getDuration() / ticksPerSixteenth) * NOTE_HEIGHT;
96 if (height == 0) height = NOTE_HEIGHT;
97 r = new Rectangle(x, y, NOTE_WIDTH, height);
99 // Places the element in the appropriate place.
100 while(isOccupied(r)) r.translate(NOTE_WIDTH, 0);
103 if (viewLength < (y + height)){
104 viewLength = y + height;
105 if(old)setPreferredSize(new Dimension(VIEW_WIDTH, viewLength + extraHeight));
110 * Returns the track of this view.
111 * @return the track of this view
113 public Track getTrack() {
118 * Returns the title of this view.
119 * @return the title of this view
121 public MooTrackTitle getTitle() {
125 private boolean isOccupied(Rectangle r) {
126 Iterator it = rects.iterator();
127 while (it.hasNext()) {
128 if(r.intersects((Rectangle)it.next())) return true;
134 * Adds the given note to the current track, and visualises it.
135 * @param mn the note to add
137 public void addNote(MooNote mn) {
139 MooNoteElement elem = new MooNoteElement(this, mn);
141 layoutElement(elem, false);
142 setPreferredSize(new Dimension(VIEW_WIDTH, viewLength + extraHeight));
147 * Removes the given note element from the view and its note from the current track.
148 * @param elem the note element to remove
150 public void removeNote(MooNoteElement elem) {
151 elem.getNote().removeFrom(track);
153 elem.getNote().removeFrom(track);
157 private void addStandardNote() {
158 long timestamp = (long)(ticksPerSixteenth * (popupY - insets.top) / NOTE_HEIGHT);
159 System.out.println(ticksPerSixteenth + ", " + popupY + ", " + insets.top + ", " + timestamp);
160 addNote(new MooNote(title.getChannel(), 60, 100, timestamp, Moosique.getSequence().getResolution() / 4));
163 public void paintComponent(Graphics g) {
164 super.paintComponent(g);
165 Graphics2D g2 = (Graphics2D)g;
166 for (int c = 0; c < viewLength || c < getHeight(); c += NOTE_HEIGHT) {
167 for (int r = 0; r < (10 * NOTE_WIDTH); r += NOTE_WIDTH) {
168 box = new Rectangle(r, c, NOTE_WIDTH, NOTE_HEIGHT);
169 g2.setColor(Color.gray);
175 class MAdapter extends MouseAdapter {
176 public void mouseClicked(MouseEvent e) {
177 if (e.getClickCount() == 2) {
183 public void mousePressed(MouseEvent e) {
184 if (e.isPopupTrigger()) {
186 popup.show(e.getComponent(), e.getX(), e.getY());
190 public void mouseEntered(MouseEvent e) {
191 // Moosique.setActiveChannel(track.getChannel());
196 class PopupListener implements ActionListener {
197 public void actionPerformed(ActionEvent e) {
198 Object source = e.getSource();
199 if (source == popupAdd) {
202 // new MooNote(int channel, int pitch, int velocity, long timestamp, int duration)