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 boolean selected;
17 private Rectangle pitchRect, veloRect;
18 public static final Color bgColor = new Color(160, 218, 255);
19 private String notePitch;
20 private String 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 setBorder(BorderFactory.createLineBorder(Color.black));
34 setBackground(bgColor);
35 addMouseListener(new MAdapter());
37 // Defines coordinates.
38 pitchRect = new Rectangle(0, 0, 15, 10);
39 veloRect = new Rectangle(20, 0, 40, 10);
41 // Creates pop-up menu.
42 popup = new JPopupMenu();
43 PopupListener pList = new PopupListener();
44 popupProp = new JMenuItem("Preferences...");
45 popupProp.addActionListener(pList);
47 popupRemove = new JMenuItem("Remove");
48 popupRemove.addActionListener(pList);
49 popup.add(popupRemove);
50 popupTransposeOctUp = new JMenuItem("Transpose one octave up");
51 popupTransposeOctUp.addActionListener(pList);
52 popup.add(popupTransposeOctUp);
53 popupTransposeOctDown = new JMenuItem("Transpose one octave down");
54 popupTransposeOctDown.addActionListener(pList);
55 popup.add(popupTransposeOctDown);
60 * Returns true if the current NoteElement is selected, otherwise false.
61 * @return if the element is selected
63 public boolean isSelected() {
68 * Returns the note of this element.
71 public MooNote getNote() {
76 * Selects the current NoteElement.
77 * @param state if the element should be selected
79 public void setSelected(boolean state) {
84 * Draws the string that shows the note's properties.
85 * @param g The Graphics object used to draw the strings.
87 public void paintComponent(Graphics g)
89 super.paintComponent(g);
90 if (!(g instanceof Graphics2D)) return;
91 Graphics2D g2 = (Graphics2D)g;
92 g2.setFont(new Font("Helvetica", Font.PLAIN, 8));
101 g2.drawString(notePitch, 1, 8);
102 g2.drawString("" + noteVelocity, 21, 8);
106 * Calculate what the string that shows the note properties should look like.
108 protected void calculateString(){
112 if(note == null) return;
114 int pitch = note.getPitch();
115 switch (pitch % 12) {
116 case 0: notePitch = "C"; break;
117 case 1: notePitch = "C#"; break;
118 case 2: notePitch = "D"; break;
119 case 3: notePitch = "D#"; break;
120 case 4: notePitch = "E"; break;
121 case 5: notePitch = "F"; break;
122 case 6: notePitch = "F#"; break;
123 case 7: notePitch = "G"; break;
124 case 8: notePitch = "G#"; break;
125 case 9: notePitch = "A"; break;
126 case 10: notePitch = "A#"; break;
127 case 11: notePitch = "B"; break;
129 notePitch += pitch / 12;
130 noteVelocity = ""+note.getVelocity();
134 * Listener that checks the mouse actions on this element.
136 class MAdapter extends MouseAdapter {
138 public void mouseClicked(MouseEvent e) {
143 * Checks if the mouse is pressed.
144 * Increases the pitch or velocity if the right mousebutton is pressed while holding ctrl down.
145 * Decreases the pitch or velocity if the left mousebutton is pressed while holding ctrl down.
146 * @param e the event recieved.
148 public void mousePressed(MouseEvent e) {
149 if (e.isControlDown()) {
150 if (pitchRect.contains(e.getPoint())) {
151 if (SwingUtilities.isRightMouseButton(e)) {
152 note.setPitch(note.getPitch() + 1);
153 } else if (SwingUtilities.isLeftMouseButton(e)) {
154 note.setPitch(note.getPitch() - 1);
158 } else if (veloRect.contains(e.getPoint())) {
159 if (SwingUtilities.isRightMouseButton(e)) {
160 note.setVelocity(note.getVelocity() + 1);
161 } else if (SwingUtilities.isLeftMouseButton(e)) {
162 note.setVelocity(note.getVelocity() - 1);
167 } else maybeShowPopup(e);
170 public void mouseReleased(MouseEvent e) {
175 * Shows the menu if an OS-specific popup-trigger was activated.
177 private void maybeShowPopup(MouseEvent e) {
178 if (e.isPopupTrigger() && !e.isControlDown()) popup.show(e.getComponent(), e.getX(), e.getY());
183 * Listens on the actions made to the popupmenu.
185 class PopupListener implements ActionListener {
186 public void actionPerformed(ActionEvent e) {
187 Object source = e.getSource();
188 if (source == popupProp) {
190 } else if (source == popupRemove) {
192 } else if (source == popupTransposeOctUp) {
193 note.setPitch(note.getPitch() + 12);
195 } else if (source == popupTransposeOctDown) {
196 note.setPitch(note.getPitch() - 12);
201 private void update() {
208 * Asks the MooTrackView that the note element is painted on to remove this element and the note.
210 protected void remove(){
211 mtv.removeNote(this);