3 import java.awt.event.*;
6 * Graphical representation of a MIDI note.
8 * @author Andersson, Andreen, Lanneskog, Pehrson
12 public class MooNoteElement extends JPanel {
14 private MooTrackView mtv;
16 private Rectangle pitchRect, veloRect;
17 public Color textColor;
18 public static final Color bgColor = new Color(160, 218, 255);
19 public static final Color invBgColor = new Color(96, 38, 0);
20 private String notePitch, noteVelocity;
21 private JPopupMenu popup;
22 private JMenuItem popupRemove, popupProp, popupTransposeOctUp, popupTransposeOctDown;
25 * Creates a new note element.
26 * @param parent The MooTrackView that this element will be painted on.
27 * @param mn the note that will be graphically represented
29 public MooNoteElement (MooTrackView parent, MooNote mn) {
33 addMouseListener(new MAdapter());
34 setBorder(BorderFactory.createLineBorder(Color.black));
35 setBackground(bgColor);
36 textColor = Color.black;
38 // Defines coordinates.
39 pitchRect = new Rectangle(0, 0, 15, 10);
40 veloRect = new Rectangle(20, 0, 40, 10);
42 // Creates pop-up menu.
43 popup = new JPopupMenu();
44 PopupListener pList = new PopupListener();
45 popupProp = new JMenuItem("Preferences...");
46 popupProp.addActionListener(pList);
48 popupRemove = new JMenuItem("Remove");
49 popupRemove.addActionListener(pList);
50 popup.add(popupRemove);
51 popupTransposeOctUp = new JMenuItem("Transpose one octave up");
52 popupTransposeOctUp.addActionListener(pList);
53 popup.add(popupTransposeOctUp);
54 popupTransposeOctDown = new JMenuItem("Transpose one octave down");
55 popupTransposeOctDown.addActionListener(pList);
56 popup.add(popupTransposeOctDown);
60 * Returns the note of this element.
63 public MooNote getNote() {
68 * Selects the current NoteElement.
70 public void select() {
71 mtv.addSelected(this);
72 setBackground(invBgColor);
73 textColor = Color.white;
78 * Deselects the current NoteElement.
80 public void deselect() {
81 // mtv.removeSelected(this);
82 setBackground(bgColor);
83 textColor = Color.black;
88 * Draws the string that shows the note's properties.
89 * @param g The Graphics object used to draw the strings.
91 public void paintComponent(Graphics g)
93 super.paintComponent(g);
94 if (!(g instanceof Graphics2D)) return;
95 Graphics2D g2 = (Graphics2D)g;
96 g2.setColor(textColor);
97 g2.setFont(new Font("Helvetica", Font.PLAIN, 8));
106 g2.drawString(notePitch, 1, 8);
107 g2.drawString("" + noteVelocity, 21, 8);
111 * Calculate what the string that shows the note properties should look like.
113 protected void calculateString(){
117 if(note == null) return;
119 int pitch = note.getPitch();
120 switch (pitch % 12) {
121 case 0: notePitch = "C"; break;
122 case 1: notePitch = "C#"; break;
123 case 2: notePitch = "D"; break;
124 case 3: notePitch = "D#"; break;
125 case 4: notePitch = "E"; break;
126 case 5: notePitch = "F"; break;
127 case 6: notePitch = "F#"; break;
128 case 7: notePitch = "G"; break;
129 case 8: notePitch = "G#"; break;
130 case 9: notePitch = "A"; break;
131 case 10: notePitch = "A#"; break;
132 case 11: notePitch = "B"; break;
134 notePitch += pitch / 12;
135 noteVelocity = ""+note.getVelocity();
139 * Listener that checks the mouse actions on this element.
141 class MAdapter extends MouseAdapter {
143 public void mouseClicked(MouseEvent e) {
149 * Checks if the mouse is pressed.
150 * Increases the pitch or velocity if the right mouse button is pressed while holding CTRL.
151 * Decreases the pitch or velocity if the left mouse button is pressed while holding CTRL.
152 * @param e the event recieved.
154 public void mousePressed(MouseEvent e) {
155 if (e.isControlDown()) {
156 if (pitchRect.contains(e.getPoint())) {
157 if (SwingUtilities.isRightMouseButton(e)) {
158 note.setPitch(note.getPitch() + 1);
159 } else if (SwingUtilities.isLeftMouseButton(e)) {
160 note.setPitch(note.getPitch() - 1);
164 } else if (veloRect.contains(e.getPoint())) {
165 if (SwingUtilities.isRightMouseButton(e)) {
166 note.setVelocity(note.getVelocity() + 1);
167 } else if (SwingUtilities.isLeftMouseButton(e)) {
168 note.setVelocity(note.getVelocity() - 1);
173 } else maybeShowPopup(e);
176 public void mouseReleased(MouseEvent e) {
181 * Shows the menu if an OS-specific popup-trigger was activated.
183 private void maybeShowPopup(MouseEvent e) {
184 if (e.isPopupTrigger() && !e.isControlDown()) popup.show(e.getComponent(), e.getX(), e.getY());
189 * Listens on the actions made to the popupmenu.
191 class PopupListener implements ActionListener {
192 public void actionPerformed(ActionEvent e) {
193 Object source = e.getSource();
194 if (source == popupProp) {
196 } else if (source == popupRemove) {
198 } else if (source == popupTransposeOctUp) {
199 note.setPitch(note.getPitch() + 12);
201 } else if (source == popupTransposeOctDown) {
202 note.setPitch(note.getPitch() - 12);
207 private void update() {
214 * Asks the MooTrackView that the note element is painted on to remove this element and the note.
216 protected void remove(){
217 mtv.removeNote(this);