]> ruin.nu Git - moosique.git/blob - MooNoteElement.java
play hangs when changing duration on a note.
[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 JPopupMenu popup;
17         private JMenuItem popupRemove, popupProp, popupTransposeOctUp, popupTransposeOctDown;
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);
24
25         /** 
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
29          */
30         public MooNoteElement (MooTrackView parent, MooNote mn) {
31                 mtv = parent;
32                 note = mn;
33                 calculateString();
34                 addMouseListener(new MAdapter());
35                 setBorder(BorderFactory.createLineBorder(Color.black));
36                 setBackground(bgColor);
37                 textColor = Color.black;
38
39                 // Defines coordinates.
40                 pitchRect = new Rectangle(0, 0, 15, 10);
41                 veloRect = new Rectangle(20, 0, 40, 10);
42
43                 // Creates pop-up menu.
44                 popup = new JPopupMenu();
45                 PopupListener pList = new PopupListener();
46                 popupProp = new JMenuItem("Preferences...");
47                 popupProp.addActionListener(pList);
48                 popup.add(popupProp);
49                 popupRemove = new JMenuItem("Remove");
50                 popupRemove.addActionListener(pList);
51                 popup.add(popupRemove);
52                 popupTransposeOctUp = new JMenuItem("Transpose one octave up");
53                 popupTransposeOctUp.addActionListener(pList);
54                 popup.add(popupTransposeOctUp);
55                 popupTransposeOctDown = new JMenuItem("Transpose one octave down");
56                 popupTransposeOctDown.addActionListener(pList);
57                 popup.add(popupTransposeOctDown);
58         }
59
60         /** 
61          * Returns the note of this element.
62          * @return the note
63          */
64         public MooNote getNote() {
65                 return note;
66         }
67
68         /** 
69          * Selects the current NoteElement.
70          */
71         public void select() {
72                 selected = true;
73                 mtv.addSelected(this);
74                 setBackground(invBgColor);
75                 textColor = Color.white;
76                 repaint();
77         }
78
79         /** 
80          * Deselects the current NoteElement.
81          */
82         public void deselect() {
83                 selected = false;
84                 // mtv.removeSelected(this);
85                 setBackground(bgColor);
86                 textColor = Color.black;
87                 repaint();
88         }
89
90         /**
91          * Draws the string that shows the note's properties.
92          * @param g     The Graphics object used to draw the strings.
93          */
94         public void paintComponent(Graphics g)
95         {
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));
101         /*
102                 switch(columns) {
103                         case 0:
104                         case 1:
105                         ...
106                 }
107         */
108                 
109                 g2.drawString(notePitch, 1, 8);
110                 g2.drawString("" + noteVelocity, 21, 8);
111         }
112
113         /**
114          * Calculate what the string that shows the note properties should look like.
115          */
116         protected void calculateString(){
117
118                 noteVelocity = ""; 
119                 notePitch = "";
120                 if(note == null) return;
121
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;
136                 }
137                 notePitch += pitch / 12;
138                 noteVelocity = ""+note.getVelocity();
139         }
140
141
142         /**
143          * Asks the MooTrackView that the note element is painted on to remove this element and the note.
144          */
145         protected void remove(){
146                 mtv.removeNote(this);
147         }
148
149         /**
150          * layout this changed elemnt
151          */
152         protected void newLayout(){
153                 mtv.layoutElement(this,true);
154         }
155         /**
156          * Listener that checks the mouse actions on this element.
157          */
158         class MAdapter extends MouseAdapter {
159         
160                 /**
161                  * If left mouse button is clicked, selects the note and plays it.
162                  */
163                 public void mouseClicked(MouseEvent e) {
164                         if (SwingUtilities.isLeftMouseButton(e) && !e.isControlDown()) {
165                                 select();
166                                 // Play the note
167                         }
168                 }
169
170                 /**
171                  * Checks if the mouse is pressed.
172                  * Increases the pitch or velocity if the right mouse button is pressed while holding CTRL.
173                  * Decreases the pitch or velocity if the left mouse button is pressed while holding CTRL.
174                  * @param e the event recieved.
175                  */
176                 public void mousePressed(MouseEvent e) {
177                         if (e.isControlDown()) {
178                                 if (pitchRect.contains(e.getPoint())) {
179                                         if (SwingUtilities.isRightMouseButton(e)) {
180                                                 note.setPitch(note.getPitch() + 1);
181                                         } else if (SwingUtilities.isLeftMouseButton(e)) {
182                                                 note.setPitch(note.getPitch() - 1);
183                                         }
184                                         Moosique.setEdited();
185                                         calculateString();
186                                         repaint();
187                                 } else if (veloRect.contains(e.getPoint())) {
188                                         if (SwingUtilities.isRightMouseButton(e)) {
189                                                 note.setVelocity(note.getVelocity() + 1);
190                                         } else if (SwingUtilities.isLeftMouseButton(e)) {
191                                                 note.setVelocity(note.getVelocity() - 1);
192                                         }
193                                         Moosique.setEdited();
194                                         calculateString();
195                                         repaint();
196                                 }
197                         } else maybeShowPopup(e);
198                 }
199
200                 public void mouseReleased(MouseEvent e) {
201                         maybeShowPopup(e);
202                 }
203
204                 /**
205                  * Shows the menu if an OS-specific popup-trigger was activated.
206                  */
207                 private void maybeShowPopup(MouseEvent e) {
208                         if (e.isPopupTrigger() && !e.isControlDown()) {
209                                 if (!selected || mtv.isTheOnlySelected((MooNoteElement)e.getComponent())) popup.show(e.getComponent(), e.getX(), e.getY());
210                                 else mtv.showSelectionPopup(e.getComponent(), e.getX(), e.getY());
211                         }
212                 }
213         }
214
215         /**
216          * Listens on the actions made to the popupmenu.
217          */
218         class PopupListener implements ActionListener {
219                 public void actionPerformed(ActionEvent e) {
220                         Object source = e.getSource();
221                         if (source == popupProp) {
222                                 new MooDialog(note);
223                                 newLayout();
224                         } else if (source == popupRemove) {
225                                 remove();
226                         } else if (source == popupTransposeOctUp) {
227                                 note.setPitch(note.getPitch() + 12);
228                                 update();
229                         } else if (source == popupTransposeOctDown) {
230                                 note.setPitch(note.getPitch() - 12);
231                                 update();
232                         }
233                 }
234
235                 private void update() {
236                         calculateString();
237                         repaint();
238                 }
239
240         }
241 }