]> ruin.nu Git - moosique.git/blob - MooNoteElement.java
6f372349e5372c2e657f71333366075c0d4dfe50
[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          * Selects the current NoteElement.
64          * @param state if the element should be selected
65          */
66         public void setSelected(boolean state) {
67                 selected = state;
68         }
69
70         /**
71          * Draws the string that shows the note's properties.
72          * @param g     The Graphics object used to draw the strings.
73          */
74         public void paintComponent(Graphics g)
75         {
76                 super.paintComponent(g);
77                 if (!(g instanceof Graphics2D)) return;
78                 Graphics2D g2 = (Graphics2D)g;
79                 g2.setFont(new Font("Helvetica", Font.PLAIN, 8));
80         /*
81                 switch(columns) {
82                         case 0:
83                         case 1:
84                         ...
85                 }
86         */
87                 
88                 g2.drawString(notePitch, 1, 9);
89                 g2.drawString("" + noteVelocity, 21, 9);
90         }
91
92         /**
93          * Calculate what the string that shows the note properties should look like.
94          */
95         protected void calculateString(){
96
97                 noteVelocity = ""; 
98                 notePitch = "";
99                 if(note == null) return;
100
101                 int pitch = note.getPitch();
102                 switch (pitch % 12) {
103                         case  0: notePitch = "C";  break;
104                         case  1: notePitch = "C#"; break;
105                         case  2: notePitch = "D";  break;
106                         case  3: notePitch = "D#"; break;
107                         case  4: notePitch = "E";  break;
108                         case  5: notePitch = "F";  break;
109                         case  6: notePitch = "F#"; break;
110                         case  7: notePitch = "G";  break;
111                         case  8: notePitch = "G#"; break;
112                         case  9: notePitch = "A";  break;
113                         case 10: notePitch = "A#"; break;
114                         case 11: notePitch = "B";  break;
115                 }
116                 notePitch += pitch / 12;
117                 noteVelocity = ""+note.getVelocity();
118         }
119
120         /**
121          * Gets the note that is element represents
122          * @return the MooNote object.
123          */
124         public MooNote getNote(){
125                 return note;
126         }
127
128         /**
129          * Listener that checks the mouse actions on this element.
130          */
131         class MAdapter extends MouseAdapter {
132                 /**
133                  * Checks if the mouse is pressed.
134                  * Pops up the menu if right mousebutton is used.
135                  * Increases the pitch or velocity if the right mousebutton is pressed while holding ctrl down.
136                  * Decreases the pitch or velocity if the left mousebutton is pressed while holding ctrl down.
137                  * @param e The events recieved.
138                  */
139                 public void mousePressed(MouseEvent e) {
140                         if (e.isControlDown()) {
141                                 if (pitchRect.contains(e.getPoint())) {
142                                         if (SwingUtilities.isRightMouseButton(e)) {
143                                                 note.setPitch(note.getPitch() + 1);
144                                         } else if (SwingUtilities.isLeftMouseButton(e)) {
145                                                 note.setPitch(note.getPitch() - 1);
146                                         }
147                                         calculateString();
148                                 } else if (veloRect.contains(e.getPoint())) {
149                                         if (SwingUtilities.isRightMouseButton(e)) {
150                                                 note.setVelocity(note.getVelocity() + 1);
151                                         } else if (SwingUtilities.isLeftMouseButton(e)) {
152                                                 note.setVelocity(note.getVelocity() - 1);
153                                         }
154                                         calculateString();
155                                 }
156                                 e.getComponent().repaint();
157                         } else if (e.isPopupTrigger()) {
158                                 popup.show(e.getComponent(), e.getX(), e.getY());
159                         }
160                 }
161         }
162
163         /**
164          * Listens on the actions made to the popupmenu.
165          */
166         class PopupListener implements ActionListener {
167                 public void actionPerformed(ActionEvent e) {
168                         Object source = e.getSource();
169                         if  (source == popupProp) {
170                                 new MooDialog(note);
171                         } else if  (source == popupRemove) {
172                                 remove();
173                         }
174                 }
175         }
176
177         /**
178          * Asks the MooTrackView that it's painted on to remove this element and the note.
179          */
180         protected void remove(){
181                 mtv.removeNote(this, note);
182         }
183
184 }