]> ruin.nu Git - moosique.git/blob - MooNoteElement.java
no message
[moosique.git] / MooNoteElement.java
1 import javax.swing.*;
2 import java.awt.*;
3 import java.awt.event.*;
4
5 /**
6  * Graphical representation of a MIDI note.
7  * 
8  * @author  Andersson, Andreen, Lanneskog, Pehrson
9  * @version 1
10  */
11  
12 public class MooNoteElement extends JPanel {
13
14         private MooTrackView mtv;
15         private MooNote note;
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;
23
24         /** 
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
28          */
29         public MooNoteElement (MooTrackView parent, MooNote mn) {
30                 mtv = parent;
31                 note = mn;
32                 calculateString();
33                 setBorder(BorderFactory.createLineBorder(Color.black));
34                 setBackground(bgColor);
35                 addMouseListener(new MAdapter());
36
37                 // Defines coordinates.
38                 pitchRect = new Rectangle(0, 0, 15, 10);
39                 veloRect = new Rectangle(20, 0, 40, 10);
40
41                 // Creates pop-up menu.
42                 popup = new JPopupMenu();
43                 PopupListener pList = new PopupListener();
44                 popupProp = new JMenuItem("Preferences...");
45                 popupProp.addActionListener(pList);
46                 popup.add(popupProp);
47
48                 popupRemove = new JMenuItem("Remove");
49                 popupRemove.addActionListener(pList);
50                 popup.add(popupRemove);
51
52         }
53
54         /** 
55          * Returns true if the current NoteElement is selected, otherwise false.
56          * @return if the element is selected
57          */
58         public boolean isSelected() {
59                 return selected;
60         }
61
62         /** 
63          * Returns the note of this element.
64          * @return the note
65          */
66         public MooNote getNote() {
67                 return note;
68         }
69
70         /** 
71          * Selects the current NoteElement.
72          * @param state if the element should be selected
73          */
74         public void setSelected(boolean state) {
75                 selected = state;
76         }
77
78         /**
79          * Draws the string that shows the note's properties.
80          * @param g     The Graphics object used to draw the strings.
81          */
82         public void paintComponent(Graphics g)
83         {
84                 super.paintComponent(g);
85                 if (!(g instanceof Graphics2D)) return;
86                 Graphics2D g2 = (Graphics2D)g;
87                 g2.setFont(new Font("Helvetica", Font.PLAIN, 8));
88         /*
89                 switch(columns) {
90                         case 0:
91                         case 1:
92                         ...
93                 }
94         */
95                 
96                 g2.drawString(notePitch, 1, 8);
97                 g2.drawString("" + noteVelocity, 21, 8);
98         }
99
100         /**
101          * Calculate what the string that shows the note properties should look like.
102          */
103         protected void calculateString(){
104
105                 noteVelocity = ""; 
106                 notePitch = "";
107                 if(note == null) return;
108
109                 int pitch = note.getPitch();
110                 switch (pitch % 12) {
111                         case  0: notePitch = "C";  break;
112                         case  1: notePitch = "C#"; break;
113                         case  2: notePitch = "D";  break;
114                         case  3: notePitch = "D#"; break;
115                         case  4: notePitch = "E";  break;
116                         case  5: notePitch = "F";  break;
117                         case  6: notePitch = "F#"; break;
118                         case  7: notePitch = "G";  break;
119                         case  8: notePitch = "G#"; break;
120                         case  9: notePitch = "A";  break;
121                         case 10: notePitch = "A#"; break;
122                         case 11: notePitch = "B";  break;
123                 }
124                 notePitch += pitch / 12;
125                 noteVelocity = ""+note.getVelocity();
126         }
127
128         /**
129          * Listener that checks the mouse actions on this element.
130          */
131         class MAdapter extends MouseAdapter {
132         
133                 public void mouseClicked(MouseEvent e) {
134                         
135                 }
136
137                 /**
138                  * Checks if the mouse is pressed.
139                  * Pops up the menu if right mousebutton is used.
140                  * Increases the pitch or velocity if the right mousebutton is pressed while holding ctrl down.
141                  * Decreases the pitch or velocity if the left mousebutton is pressed while holding ctrl down.
142                  * @param e The events recieved.
143                  */
144                 public void mousePressed(MouseEvent e) {
145                         if (e.isControlDown()) {
146                                 if (pitchRect.contains(e.getPoint())) {
147                                         if (SwingUtilities.isRightMouseButton(e)) {
148                                                 note.setPitch(note.getPitch() + 1);
149                                         } else if (SwingUtilities.isLeftMouseButton(e)) {
150                                                 note.setPitch(note.getPitch() - 1);
151                                         }
152                                         calculateString();
153                                 } else if (veloRect.contains(e.getPoint())) {
154                                         if (SwingUtilities.isRightMouseButton(e)) {
155                                                 note.setVelocity(note.getVelocity() + 1);
156                                         } else if (SwingUtilities.isLeftMouseButton(e)) {
157                                                 note.setVelocity(note.getVelocity() - 1);
158                                         }
159                                         calculateString();
160                                 }
161                                 e.getComponent().repaint();
162                         } else if (e.isPopupTrigger()) {
163                                 popup.show(e.getComponent(), e.getX(), e.getY());
164                         }
165                 }
166         }
167
168         /**
169          * Listens on the actions made to the popupmenu.
170          */
171         class PopupListener implements ActionListener {
172                 public void actionPerformed(ActionEvent e) {
173                         Object source = e.getSource();
174                         if  (source == popupProp) {
175                                 new MooDialog(note);
176                         } else if  (source == popupRemove) {
177                                 remove();
178                         }
179                 }
180         }
181
182         /**
183          * Asks the MooTrackView that it's painted on to remove this element and the note.
184          */
185         protected void remove(){
186                 mtv.removeNote(this);
187         }
188
189 }