]> ruin.nu Git - moosique.git/blob - MooNoteElement.java
hmm.. this doesn't want to work..
[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 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;
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                 addMouseListener(new MAdapter());
34                 setBorder(BorderFactory.createLineBorder(Color.black));
35                 setBackground(bgColor);
36                 textColor = Color.black;
37
38                 // Defines coordinates.
39                 pitchRect = new Rectangle(0, 0, 15, 10);
40                 veloRect = new Rectangle(20, 0, 40, 10);
41
42                 // Creates pop-up menu.
43                 popup = new JPopupMenu();
44                 PopupListener pList = new PopupListener();
45                 popupProp = new JMenuItem("Preferences...");
46                 popupProp.addActionListener(pList);
47                 popup.add(popupProp);
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);
57         }
58
59         /** 
60          * Returns the note of this element.
61          * @return the note
62          */
63         public MooNote getNote() {
64                 return note;
65         }
66
67         /** 
68          * Selects the current NoteElement.
69          */
70         public void select() {
71                 mtv.addSelected(this);
72                 setBackground(invBgColor);
73                 textColor = Color.white;
74                 repaint();
75         }
76
77         /** 
78          * Deselects the current NoteElement.
79          */
80         public void deselect() {
81                 // mtv.removeSelected(this);
82                 setBackground(bgColor);
83                 textColor = Color.black;
84                 repaint();
85         }
86
87         /**
88          * Draws the string that shows the note's properties.
89          * @param g     The Graphics object used to draw the strings.
90          */
91         public void paintComponent(Graphics g)
92         {
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));
98         /*
99                 switch(columns) {
100                         case 0:
101                         case 1:
102                         ...
103                 }
104         */
105                 
106                 g2.drawString(notePitch, 1, 8);
107                 g2.drawString("" + noteVelocity, 21, 8);
108         }
109
110         /**
111          * Calculate what the string that shows the note properties should look like.
112          */
113         protected void calculateString(){
114
115                 noteVelocity = ""; 
116                 notePitch = "";
117                 if(note == null) return;
118
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;
133                 }
134                 notePitch += pitch / 12;
135                 noteVelocity = ""+note.getVelocity();
136         }
137
138         /**
139          * Listener that checks the mouse actions on this element.
140          */
141         class MAdapter extends MouseAdapter {
142         
143                 public void mouseClicked(MouseEvent e) {
144                         select();
145                         // Play the note
146                 }
147
148                 /**
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.
153                  */
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);
161                                         }
162                                         calculateString();
163                                         repaint();
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);
169                                         }
170                                         calculateString();
171                                         repaint();
172                                 }
173                         } else maybeShowPopup(e);
174                 }
175
176                 public void mouseReleased(MouseEvent e) {
177                         maybeShowPopup(e);
178                 }
179
180                 /**
181                  * Shows the menu if an OS-specific popup-trigger was activated.
182                  */
183                 private void maybeShowPopup(MouseEvent e) {
184                         if (e.isPopupTrigger() && !e.isControlDown()) popup.show(e.getComponent(), e.getX(), e.getY());
185                 }
186         }
187
188         /**
189          * Listens on the actions made to the popupmenu.
190          */
191         class PopupListener implements ActionListener {
192                 public void actionPerformed(ActionEvent e) {
193                         Object source = e.getSource();
194                         if (source == popupProp) {
195                                 new MooDialog(note);
196                         } else if (source == popupRemove) {
197                                 remove();
198                         } else if (source == popupTransposeOctUp) {
199                                 note.setPitch(note.getPitch() + 12);
200                                 update();
201                         } else if (source == popupTransposeOctDown) {
202                                 note.setPitch(note.getPitch() - 12);
203                                 update();
204                         }
205                 }
206
207                 private void update() {
208                         calculateString();
209                         repaint();
210                 }
211         }
212
213         /**
214          * Asks the MooTrackView that the note element is painted on to remove this element and the note.
215          */
216         protected void remove(){
217                 mtv.removeNote(this);
218         }
219 }