3 import java.awt.event.*;
4 import javax.sound.midi.*;
8 * The GUI-class representing the add-, delete-, copy- or movetrack popup dialogs.
10 * @author Björn Lanneskog
13 public class MooDialog extends JDialog {
15 private JLabel name, whichToEdit;
16 private JTextField textField;
17 private JComboBox trackListA, trackListB;
18 private JButton okButton, cancelButton;
19 public static final int ADD_TRACK = 1,
25 * Create the parts that the dialogs have in common.
27 public MooDialog(int type) {
28 super(Moosique.getGUI(), "Add track", false);
30 Container pane = getContentPane();
33 Track[] tracks = Moosique.getSequence().getTracks();
38 // Create the content of the add dialog and put them in a container.
39 name = new JLabel("Name of track", JLabel.CENTER);
41 textField = new JTextField();
43 whichToEdit = new JLabel("Add after", JLabel.CENTER);
44 pane.add(whichToEdit);
45 trackListA = new JComboBox();
46 for (int i = 1; i <= tracks.length; i++) trackListA.addItem("Track " + i);
48 cancelButton = new JButton("Cancel");
49 pane.add(cancelButton);
50 okButton = new JButton("OK");
53 // Set the layout properties of the dialog.
54 name.setBounds(50, 10, 100, 20);
55 textField.setBounds(40, 35, 120, 20);
56 whichToEdit.setBounds(50, 70, 100, 20);
57 trackListA.setBounds(40, 95, 120, 20);
58 cancelButton.setBounds(10, 150, 80, 30);
59 okButton.setBounds(120, 150, 60, 30);
61 setLocation((Toolkit.getDefaultToolkit().getScreenSize().width - this.getWidth()) / 2,
62 (Toolkit.getDefaultToolkit().getScreenSize().height - this.getHeight()) / 2);
71 // Create the contents of the delete dialog and put them in a container.
72 whichToEdit = new JLabel("Delete track", JLabel.CENTER);
73 pane.add(whichToEdit);
74 trackListA = new JComboBox();
75 for (int i = 1; i <= tracks.length; i++) trackListA.addItem("Track " + i);
77 cancelButton = new JButton("Cancel");
78 pane.add(cancelButton);
79 okButton = new JButton("OK");
82 // Set the layout properties of the dialog.
83 whichToEdit.setBounds(50, 10, 100, 20);
84 trackListA.setBounds(40, 35, 120, 20);
85 cancelButton.setBounds(10, 90, 80, 30);
86 okButton.setBounds(120, 90, 60, 30);
88 setLocation((Toolkit.getDefaultToolkit().getScreenSize().width - this.getWidth()) / 2,
89 (Toolkit.getDefaultToolkit().getScreenSize().height - this.getHeight()) / 2);
98 // Create the content of the copy dialog and put them in a container.
99 name = new JLabel("Track to copy", JLabel.CENTER);
101 trackListA = new JComboBox();
102 for (int i = 1; i <= tracks.length; i++) trackListA.addItem("Track " + i);
103 pane.add(trackListA);
104 whichToEdit = new JLabel("Insert after", JLabel.CENTER);
105 pane.add(whichToEdit);
106 trackListB = new JComboBox();
107 for (int i = 1; i <= tracks.length; i++) trackListB.addItem("Track " + i);
108 pane.add(trackListB);
109 cancelButton = new JButton("Cancel");
110 pane.add(cancelButton);
111 okButton = new JButton("OK");
114 // Set the layout properties of the dialog.
115 name.setBounds(50, 10, 100, 20);
116 trackListA.setBounds(40, 35, 120, 20);
117 whichToEdit.setBounds(50, 70, 100, 20);
118 trackListB.setBounds(40, 95, 120, 20);
119 cancelButton.setBounds(10, 150, 80, 30);
120 okButton.setBounds(120, 150, 60, 30);
122 setLocation((Toolkit.getDefaultToolkit().getScreenSize().width - this.getWidth()) / 2,
123 (Toolkit.getDefaultToolkit().getScreenSize().height - this.getHeight()) / 2);
132 // Create the content of the move dialog and put them in a container.
133 name = new JLabel("Track to move", JLabel.CENTER);
135 trackListA = new JComboBox();
136 for (int i = 1; i <= tracks.length; i++) trackListA.addItem("Track " + i);
137 pane.add(trackListA);
138 whichToEdit = new JLabel("Insert after", JLabel.CENTER);
139 pane.add(whichToEdit);
140 trackListB = new JComboBox();
141 for (int i = 1; i <= tracks.length; i++) trackListB.addItem("Track " + i);
142 pane.add(trackListB);
143 cancelButton = new JButton("Cancel");
144 pane.add(cancelButton);
145 okButton = new JButton("OK");
148 // Set the layout properties of the dialog.
149 name.setBounds(40, 10, 120, 20);
150 trackListA.setBounds(40, 35, 120, 20);
151 whichToEdit.setBounds(50, 70, 100, 20);
152 trackListB.setBounds(40, 95, 120, 20);
153 cancelButton.setBounds(10, 150, 80, 30);
154 okButton.setBounds(120, 150, 60, 30);
156 setLocation((Toolkit.getDefaultToolkit().getScreenSize().width - this.getWidth()) / 2,
157 (Toolkit.getDefaultToolkit().getScreenSize().height - this.getHeight()) / 2);
167 private MooNote note;
168 private JOptionPane optionPane;
169 private JTextField pitch;
170 private JTextField velocity;
171 private JTextField length;
174 * Creates a new note preference dialog.
175 * @param mn the note that will be graphically represented
177 public MooDialog(MooNote mn) {
178 super(Moosique.getGUI(), "Note properties", false);
180 pitch = new JTextField(new Integer(note.getPitch()).toString(),3);
181 JPanel pitchpanel = new JPanel();
182 pitchpanel.add(new Label("Pitch: "));
183 pitchpanel.add(pitch);
186 velocity = new JTextField(new Integer(note.getVelocity()).toString(),3);
187 JPanel velocitypanel = new JPanel();
188 velocitypanel.add(new Label("Velocity: "));
189 velocitypanel.add(velocity);
191 length = new JTextField(new Integer(note.getDuration()).toString(),5);
192 JPanel lengthpanel = new JPanel();
193 lengthpanel.add(new Label("Length: "));
194 lengthpanel.add(length);
196 Object[] array = {"Set the note properties",
201 final String btnString1 = "Enter";
202 final String btnString2 = "Cancel";
203 Object[] options = {btnString1, btnString2};
205 optionPane = new JOptionPane(array,
206 JOptionPane.QUESTION_MESSAGE,
207 JOptionPane.YES_NO_OPTION,
211 setContentPane(optionPane);
212 setDefaultCloseOperation(DISPOSE_ON_CLOSE);
214 ActionListener intValidator = new ActionListener() {
215 public void actionPerformed(ActionEvent e) {
216 if (e.getSource() instanceof JTextField){
217 JTextField s = (JTextField)e.getSource();
218 int num = Integer.parseInt(s.getText());
221 else if (num > 127 && s != length)
223 s.setText(new Integer(num).toString());
227 pitch.addActionListener(intValidator);
228 velocity.addActionListener(intValidator);
229 length.addActionListener(intValidator);
231 optionPane.addPropertyChangeListener(new PropertyChangeListener() {
232 public void propertyChange(PropertyChangeEvent e) {
233 String prop = e.getPropertyName();
236 && (e.getSource() == optionPane)
237 && (prop.equals(JOptionPane.VALUE_PROPERTY) ||
238 prop.equals(JOptionPane.INPUT_VALUE_PROPERTY))) {
239 Object value = optionPane.getValue();
241 if (value == JOptionPane.UNINITIALIZED_VALUE) {
246 // Reset the JOptionPane's value.
247 // If you don't do this, then if the user
248 // presses the same button next time, no
249 // property change event will be fired.
251 JOptionPane.UNINITIALIZED_VALUE);
253 if (value.equals(btnString1)) {
254 note.setPitch(Integer.parseInt(pitch.getText()));
255 note.setVelocity(Integer.parseInt(velocity.getText()));
256 note.setDuration(Integer.parseInt(length.getText()));
259 } else { // user closed dialog or clicked cancel