3 import java.awt.event.*;
6 * Graphical representation of a MIDI note.
8 * @author Andersson, Andreen, Lanneskog, Pehrson
12 public class MooNoteElement extends JPanel implements Comparable{
14 private MooTrackView mtv;
16 private Rectangle pitchRect, veloRect;
17 private String notePitch, noteVelocity;
18 private boolean selected = false, leftMouseButtonPressed = false, mouseIn = false;
19 public Color textColor;
20 public static final Color bgColor = new Color(160, 218, 255);
21 public static final Color invBgColor = new Color(96, 38, 0);
24 * Creates a new note element.
25 * @param parent The MooTrackView that this element will be painted on.
26 * @param mn the note that will be graphically represented
28 public MooNoteElement (MooTrackView parent, MooNote mn) {
32 addMouseListener(new MAdapter());
33 setBorder(BorderFactory.createLineBorder(Color.black));
34 setBackground(bgColor);
35 textColor = Color.black;
37 // Defines coordinates.
38 pitchRect = new Rectangle(0, 0, 15, 10);
39 veloRect = new Rectangle(20, 0, 40, 10);
43 * Returns the note of this element.
46 public MooNote getNote() {
51 * Compares the note of this element to that of another element.
52 * @return a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object
54 public int compareTo(Object o) {
55 return note.compareTo(((MooNoteElement)o).getNote());
59 * Selects the current NoteElement.
61 public void select() {
63 Moosique.selectNote(this);
64 setBackground(invBgColor);
65 textColor = Color.white;
70 * Deselects the current NoteElement.
72 public void deselect() {
74 // mtv.deselectNote(this);
75 setBackground(bgColor);
76 textColor = Color.black;
81 * Transposes the current note element the given number of halftones.
82 * @param halftones the number of halftones to transpose - positive for up, negative for down
84 public void transpose(int halftones) {
85 note.transpose(halftones);
90 * Draws the string that shows the note's properties.
91 * @param g The Graphics object used to draw the strings.
93 public void paintComponent(Graphics g)
95 super.paintComponent(g);
96 if (!(g instanceof Graphics2D)) return;
97 Graphics2D g2 = (Graphics2D)g;
98 g2.setColor(textColor);
99 g2.setFont(new Font("Helvetica", Font.PLAIN, 8));
108 g2.drawString(notePitch, 1, 8);
109 g2.drawString("" + noteVelocity, 21, 8);
113 * Calculate what the string that shows the note properties should look like.
115 protected void calculateString(){
119 if(note == null) return;
121 int pitch = note.getPitch();
122 switch (pitch % 12) {
123 case 0: notePitch = "C"; break;
124 case 1: notePitch = "C#"; break;
125 case 2: notePitch = "D"; break;
126 case 3: notePitch = "D#"; break;
127 case 4: notePitch = "E"; break;
128 case 5: notePitch = "F"; break;
129 case 6: notePitch = "F#"; break;
130 case 7: notePitch = "G"; break;
131 case 8: notePitch = "G#"; break;
132 case 9: notePitch = "A"; break;
133 case 10: notePitch = "A#"; break;
134 case 11: notePitch = "B"; break;
136 notePitch += pitch / 12 - 1;
137 noteVelocity = ""+note.getVelocity();
142 * Asks the MooTrackView that the note element is painted on to remove this element and the note.
144 protected void remove(){
145 mtv.removeNote(this);
149 * Updates the graphical content of the element and repaints it.
151 public void update() {
157 * Layout this changed elemnt.
159 protected void newLayout(){
160 mtv.layoutElement(this,true);
164 * Listener that checks the mouse actions on this element.
166 class MAdapter extends MouseAdapter {
169 * Plays the note on double-click.
171 public void mouseClicked(MouseEvent e) {
172 if (SwingUtilities.isLeftMouseButton(e) && e.getClickCount() == 2)
173 Moosique.getReceiver().send(note.getMessage(), -1);
177 * Selects the note if mouse entered with the left mouse button pressed.
179 public void mouseEntered(MouseEvent e) {
181 if (mtv.isLeftMouseButtonPressed()) {
187 * Registers mouse exited.
189 public void mouseExited(MouseEvent e) {
191 Moosique.getReceiver().send(note.getNoteOffEvent().getMessage(), -1);
195 * Checks if the mouse is pressed.
196 * Increases the pitch or velocity if the right mouse button is pressed while holding CTRL.
197 * Decreases the pitch or velocity if the left mouse button is pressed while holding CTRL.
198 * @param e the event recieved.
200 public void mousePressed(MouseEvent e) {
201 if (e.isControlDown()) {
202 if (pitchRect.contains(e.getPoint())) {
203 if (SwingUtilities.isRightMouseButton(e)) {
205 } else if (SwingUtilities.isLeftMouseButton(e)) {
208 Moosique.setEdited();
211 } else if (veloRect.contains(e.getPoint())) {
212 if (SwingUtilities.isRightMouseButton(e)) {
213 note.setVelocity(note.getVelocity() + 1);
214 } else if (SwingUtilities.isLeftMouseButton(e)) {
215 note.setVelocity(note.getVelocity() - 1);
217 Moosique.setEdited();
227 public void mouseReleased(MouseEvent e) {
228 if (!maybeShowPopup(e) && !mouseIn) {
230 if (y < 0) mtv.moveSelectedNotes((Moosique.getSequence().getResolution() / 4)* (int)Math.floor((double)y / MooTrackView.NOTE_HEIGHT));
231 if (y > getHeight()) mtv.moveSelectedNotes((Moosique.getSequence().getResolution() / 4) * (int)Math.ceil(((double)y - getHeight()) / MooTrackView.NOTE_HEIGHT));
236 * Shows the menu if an OS-specific popup-trigger was activated.
238 private boolean maybeShowPopup(MouseEvent e) {
239 if (!e.isPopupTrigger()) return false;
240 if (!e.isControlDown()) mtv.showSelectionPopup(e.getComponent(), e.getX(), e.getY());