]> ruin.nu Git - moosique.git/blob - MooNoteElement.java
Added some popup-menu items.
[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, 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                 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                 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);
56
57         }
58
59         /** 
60          * Returns true if the current NoteElement is selected, otherwise false.
61          * @return if the element is selected
62          */
63         public boolean isSelected() {
64                 return selected;
65         }
66
67         /** 
68          * Returns the note of this element.
69          * @return the note
70          */
71         public MooNote getNote() {
72                 return note;
73         }
74
75         /** 
76          * Selects the current NoteElement.
77          * @param state if the element should be selected
78          */
79         public void setSelected(boolean state) {
80                 selected = state;
81         }
82
83         /**
84          * Draws the string that shows the note's properties.
85          * @param g     The Graphics object used to draw the strings.
86          */
87         public void paintComponent(Graphics g)
88         {
89                 super.paintComponent(g);
90                 if (!(g instanceof Graphics2D)) return;
91                 Graphics2D g2 = (Graphics2D)g;
92                 g2.setFont(new Font("Helvetica", Font.PLAIN, 8));
93         /*
94                 switch(columns) {
95                         case 0:
96                         case 1:
97                         ...
98                 }
99         */
100                 
101                 g2.drawString(notePitch, 1, 8);
102                 g2.drawString("" + noteVelocity, 21, 8);
103         }
104
105         /**
106          * Calculate what the string that shows the note properties should look like.
107          */
108         protected void calculateString(){
109
110                 noteVelocity = ""; 
111                 notePitch = "";
112                 if(note == null) return;
113
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;
128                 }
129                 notePitch += pitch / 12;
130                 noteVelocity = ""+note.getVelocity();
131         }
132
133         /**
134          * Listener that checks the mouse actions on this element.
135          */
136         class MAdapter extends MouseAdapter {
137         
138                 public void mouseClicked(MouseEvent e) {
139                         // Play the note
140                 }
141
142                 /**
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.
147                  */
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);
155                                         }
156                                         calculateString();
157                                         repaint();
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);
163                                         }
164                                         calculateString();
165                                         repaint();
166                                 }
167                         } else maybeShowPopup(e);
168                 }
169
170                 public void mouseReleased(MouseEvent e) {
171                         maybeShowPopup(e);
172                 }
173
174                 /**
175                  * Shows the menu if an OS-specific popup-trigger was activated.
176                  */
177                 private void maybeShowPopup(MouseEvent e) {
178                         if (e.isPopupTrigger() && !e.isControlDown()) popup.show(e.getComponent(), e.getX(), e.getY());
179                 }
180         }
181
182         /**
183          * Listens on the actions made to the popupmenu.
184          */
185         class PopupListener implements ActionListener {
186                 public void actionPerformed(ActionEvent e) {
187                         Object source = e.getSource();
188                         if (source == popupProp) {
189                                 new MooDialog(note);
190                         } else if (source == popupRemove) {
191                                 remove();
192                         } else if (source == popupTransposeOctUp) {
193                                 note.setPitch(note.getPitch() + 12);
194                                 update();
195                         } else if (source == popupTransposeOctDown) {
196                                 note.setPitch(note.getPitch() - 12);
197                                 update();
198                         }
199                 }
200
201                 private void update() {
202                         calculateString();
203                         repaint();
204                 }
205         }
206
207         /**
208          * Asks the MooTrackView that the note element is painted on to remove this element and the note.
209          */
210         protected void remove(){
211                 mtv.removeNote(this);
212         }
213 }