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 JPopupMenu popup;
17 private JMenuItem popupRemove, popupProp, popupTranspOctUp, popupTranspOctDown;
18 private Rectangle pitchRect, veloRect;
19 private String notePitch, noteVelocity;
20 private boolean selected = false;
21 public Color textColor;
22 public static final Color bgColor = new Color(160, 218, 255);
23 public static final Color invBgColor = new Color(96, 38, 0);
26 * Creates a new note element.
27 * @param parent The MooTrackView that this element will be painted on.
28 * @param mn the note that will be graphically represented
30 public MooNoteElement (MooTrackView parent, MooNote mn) {
34 addMouseListener(new MAdapter());
35 setBorder(BorderFactory.createLineBorder(Color.black));
36 setBackground(bgColor);
37 textColor = Color.black;
39 // Defines coordinates.
40 pitchRect = new Rectangle(0, 0, 15, 10);
41 veloRect = new Rectangle(20, 0, 40, 10);
43 // Creates pop-up menu.
44 popup = new JPopupMenu();
45 PopupListener pList = new PopupListener();
46 popupProp = new JMenuItem("Preferences...");
47 popupProp.addActionListener(pList);
49 popupRemove = new JMenuItem("Remove");
50 popupRemove.addActionListener(pList);
51 popup.add(popupRemove);
52 popupTranspOctUp = new JMenuItem("Transpose one octave up");
53 popupTranspOctUp.addActionListener(pList);
54 popup.add(popupTranspOctUp);
55 popupTranspOctDown = new JMenuItem("Transpose one octave down");
56 popupTranspOctDown.addActionListener(pList);
57 popup.add(popupTranspOctDown);
61 * Returns the note of this element.
64 public MooNote getNote() {
69 * Selects the current NoteElement.
71 public void select() {
74 setBackground(invBgColor);
75 textColor = Color.white;
80 * Deselects the current NoteElement.
82 public void deselect() {
84 // mtv.deselectNote(this);
85 setBackground(bgColor);
86 textColor = Color.black;
91 * Draws the string that shows the note's properties.
92 * @param g The Graphics object used to draw the strings.
94 public void paintComponent(Graphics g)
96 super.paintComponent(g);
97 if (!(g instanceof Graphics2D)) return;
98 Graphics2D g2 = (Graphics2D)g;
99 g2.setColor(textColor);
100 g2.setFont(new Font("Helvetica", Font.PLAIN, 8));
109 g2.drawString(notePitch, 1, 8);
110 g2.drawString("" + noteVelocity, 21, 8);
114 * Calculate what the string that shows the note properties should look like.
116 protected void calculateString(){
120 if(note == null) return;
122 int pitch = note.getPitch();
123 switch (pitch % 12) {
124 case 0: notePitch = "C"; break;
125 case 1: notePitch = "C#"; break;
126 case 2: notePitch = "D"; break;
127 case 3: notePitch = "D#"; break;
128 case 4: notePitch = "E"; break;
129 case 5: notePitch = "F"; break;
130 case 6: notePitch = "F#"; break;
131 case 7: notePitch = "G"; break;
132 case 8: notePitch = "G#"; break;
133 case 9: notePitch = "A"; break;
134 case 10: notePitch = "A#"; break;
135 case 11: notePitch = "B"; break;
137 notePitch += pitch / 12 - 1;
138 noteVelocity = ""+note.getVelocity();
143 * Asks the MooTrackView that the note element is painted on to remove this element and the note.
145 protected void remove(){
146 mtv.removeNote(this);
150 * Updates the graphical content of the element and repaints it.
152 public void update() {
158 * layout this changed elemnt
160 protected void newLayout(){
161 mtv.layoutElement(this,true);
164 * Listener that checks the mouse actions on this element.
166 class MAdapter extends MouseAdapter {
169 * If left mouse button is clicked, selects the note and plays it.
171 public void mouseClicked(MouseEvent e) {
172 if (SwingUtilities.isLeftMouseButton(e) && !e.isControlDown()) {
179 * Checks if the mouse is pressed.
180 * Increases the pitch or velocity if the right mouse button is pressed while holding CTRL.
181 * Decreases the pitch or velocity if the left mouse button is pressed while holding CTRL.
182 * @param e the event recieved.
184 public void mousePressed(MouseEvent e) {
185 if (e.isControlDown()) {
186 if (pitchRect.contains(e.getPoint())) {
187 if (SwingUtilities.isRightMouseButton(e)) {
189 } else if (SwingUtilities.isLeftMouseButton(e)) {
192 Moosique.setEdited();
195 } else if (veloRect.contains(e.getPoint())) {
196 if (SwingUtilities.isRightMouseButton(e)) {
197 note.setVelocity(note.getVelocity() + 1);
198 } else if (SwingUtilities.isLeftMouseButton(e)) {
199 note.setVelocity(note.getVelocity() - 1);
201 Moosique.setEdited();
205 } else maybeShowPopup(e);
208 public void mouseReleased(MouseEvent e) {
213 * Shows the menu if an OS-specific popup-trigger was activated.
215 private void maybeShowPopup(MouseEvent e) {
216 if (e.isPopupTrigger() && !e.isControlDown()) {
217 if (!selected || mtv.isTheOnlySelected((MooNoteElement)e.getComponent())) popup.show(e.getComponent(), e.getX(), e.getY());
218 else mtv.showSelectionPopup(e.getComponent(), e.getX(), e.getY());
224 * Listens on the actions made to the popupmenu.
226 class PopupListener implements ActionListener {
227 public void actionPerformed(ActionEvent e) {
228 Object source = e.getSource();
229 if (source == popupProp) {
232 } else if (source == popupRemove) {
234 } else if (source == popupTranspOctUp) {
237 } else if (source == popupTranspOctDown) {