/** * Shows an edit midi event dialog. * * @param event The events to be edited. * @return <code>true</code> if the dialog has been shown. */ public static boolean showEditShortMessageEventDialog( MidiEvent event, TrackProxy track, MidiDescriptor midiDescriptor) { MidiEditPanel mep = new MidiEditPanel( track, event, SgEngine.getInstance().getResourceBundle().getString("midi.event.edit.event")); Object[] message = new Object[] {mep}; int option = JOptionPane.showConfirmDialog( getMainFrame(), message, SgEngine.getInstance().getResourceBundle().getString("midi.event.edit.title"), JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE, null); if (option == JOptionPane.OK_OPTION && mep.hasChanged()) { try { // create change edit and perform changes Object changeObj = new Object(); ChangeEventsEdit edit = new ChangeEventsEdit(track, new MidiEvent[] {event}, midiDescriptor, null, changeObj); mep.applyChanges(changeObj); edit.perform(); midiDescriptor.getUndoManager().addEdit(edit); } catch (InvalidMidiDataException imdex) { JOptionPane.showMessageDialog( getMainFrame(), imdex.getMessage(), SgEngine.getInstance().getResourceBundle().getString("error.invalidMidiData"), JOptionPane.ERROR_MESSAGE); } } return true; }
/** * Displays a dialog that allows the user to set the number of repeatings of the given MIDI note * on the given MIDI track. If the user confirms, an undoable edit is created and added to the * provided <code>MidiDescriptor</code>'s <code>UndoManager</code>. * * @param midiDescriptor The <code>MidiDescriptor</code>. * @param track The MIDI track. It is assumed that this track belongs to the given <code> * MidiDescriptor</code>'s <code>SgMidiSequence</code>. * @param events The events that describe the note to be repeated. It is assumed that these events * belong to the given MIDI track. */ public static void showRepeatNoteDialog( MidiDescriptor midiDescriptor, TrackProxy track, MidiEvent[] events) { ResourceBundle rb = SgEngine.getInstance().getResourceBundle(); SgMidiSequence sequence = null; try { sequence = midiDescriptor.getSequence(); } catch (InvalidMidiDataException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } if (sequence == null) { return; } RepeatNotePanel repeatNotePanel = new RepeatNotePanel(1, 4); Object[] message = new Object[] { repeatNotePanel, }; int option = JOptionPane.showConfirmDialog( getMainFrame(), message, rb.getString("midi.note.repeat.title"), JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE, null); if (option == JOptionPane.OK_OPTION) { int tactCount = repeatNotePanel.getTactCountValue(); int countPerTact = repeatNotePanel.getCountPerTactValue(); MidiEvent[] addedEvents = MidiToolkit.repeatEvents(sequence, events, countPerTact, tactCount); AddEventsEdit undoableEdit = new AddEventsEdit( track, addedEvents, midiDescriptor, rb.getString("edit.repeatEventsEdit"), UiToolkit.class); undoableEdit.perform(); midiDescriptor.getUndoManager().addEdit(undoableEdit); } }
/** * Displays a dialog where the user can set the tempo (in BPM or MPQ). * * @param midiDescriptor The <code>MidiDescriptor</code> for which the tempo is to be changed. * Must not be <code>null</code>. * @param tick The tick position where the tempo shall be set. Default is 0 (track tempo). */ public static void showSetTempoDialog(MidiDescriptor midiDescriptor, long tick) { ResourceBundle rb = SgEngine.getInstance().getResourceBundle(); SgMidiSequence seq = null; try { seq = midiDescriptor.getSequence(); } catch (InvalidMidiDataException e) { } catch (IOException e) { } if (seq == null) { return; } TrackProxy[] tracks = seq.getTrackProxies(); MidiEvent event = null; TrackProxy track = null; for (int i = 0; event == null && i < tracks.length; i++) { event = tracks[i].getTempoEvent(); track = tracks[i]; } if (event == null) { int option = JOptionPane.showConfirmDialog( getMainFrame(), rb.getString("midi.tempo.noTempoSet.message"), rb.getString("midi.tempo.noTempoSet.title"), JOptionPane.YES_NO_OPTION, JOptionPane.PLAIN_MESSAGE); if (option == JOptionPane.YES_OPTION) { TrackProxy tempTrack = null; try { tempTrack = midiDescriptor.getSequence().getSelectedTrackProxy(); } catch (InvalidMidiDataException e) { } catch (IOException e) { } if (tempTrack != null) { track = tempTrack; float mpq = MidiToolkit.bpmToMPQ(SgEngine.getInstance().getProperties().getDefaultMidiTempo()); event = MidiToolkit.createTempoEvent(mpq); final MidiDescriptor descriptor = midiDescriptor; AddEventsEdit edit = new AddEventsEdit( track, new MidiEvent[] {event}, midiDescriptor, rb.getString("midi.tempo.addTempoEvent"), UiToolkit.class) { private static final long serialVersionUID = 1; public void perform() { super.perform(); float mpq = MidiToolkit.getTempoInMPQ((MetaMessage) events[0].getMessage()); descriptor.setTempoInMpq(mpq); } public void undo() { super.undo(); descriptor.setTempoInMpq(-1); } }; edit.perform(); midiDescriptor.getUndoManager().addEdit(edit); } } } if (event != null) { float mpq = MidiToolkit.getTempoInMPQ((MetaMessage) event.getMessage()); final SetTempoPanel tempoPanel = new SetTempoPanel(mpq); Object[] message = new Object[] { tempoPanel, }; JOptionPane pane = new JOptionPane( message, JOptionPane.PLAIN_MESSAGE, JOptionPane.OK_CANCEL_OPTION, null, null, tempoPanel); JDialog d = pane.createDialog(UiToolkit.getMainFrame(), rb.getString("midi.tempo.title")); d.addComponentListener( new ComponentAdapter() { public void componentShown(ComponentEvent e) { tempoPanel.requestFocus(); } }); d.setVisible(true); Integer selectedValue = (Integer) pane.getValue(); if (selectedValue != null && selectedValue.intValue() == JOptionPane.OK_OPTION && tempoPanel.hasChanged()) { // TODO: improve this! float mpqVal = tempoPanel.getMPQValue(); ChangeTempoEdit undoableEdit = new ChangeTempoEdit(midiDescriptor, track, event, mpqVal, UiToolkit.class); undoableEdit.perform(); midiDescriptor.getUndoManager().addEdit(undoableEdit); } } }
/** * Displays a dialog that allows the user to edit the name of the given MIDI track. * * @param changeObj The change object for the undoable edit that is automatically created. * @param midiDescriptor The MIDI descriptor containing the sequence that contains the given * track. * @param track The track. * @param parent The component that shall be the parent of the created dialog. * @return The new track name, or <code>null</code> if the user chose not to set a track name at * all or aborted the dialog. */ public static String showEditTrackNameDialog( Object changeObj, MidiDescriptor midiDescriptor, TrackProxy track, Component parent) { if (track == null) { return null; } String trackName = track.getTrackName(); JCheckBox cb = new JCheckBox( SgEngine.getInstance() .getResourceBundle() .getString("midi.track.editTrackName.storeNameInTrack"), (trackName != null)); cb.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { JComponent c = (JComponent) e.getSource(); c = (JComponent) c.getParent(); Component[] comps = ((JComponent) c.getParent()).getComponents(); for (int i = 0; i < comps.length; i++) { if (comps[i] instanceof JPanel && comps[i] != c) { if (((JPanel) comps[i]).getComponent(0) instanceof JTextField) { ((JTextField) ((JPanel) comps[i]).getComponent(0)) .setEnabled(((JCheckBox) e.getSource()).isSelected()); } } } } }); JPanel panel = new JPanel(new BorderLayout()); JPanel cbPanel = new JPanel(new FlowLayout(FlowLayout.LEFT)); cbPanel.add(cb); cbPanel.setBorder( new TitledBorder( SgEngine.getInstance() .getResourceBundle() .getString("midi.track.editTrackName.setName"))); panel.add(cbPanel, BorderLayout.NORTH); JPanel p = new JPanel(new FlowLayout(FlowLayout.LEFT)); JTextField textField = new JTextField((trackName != null ? trackName : ""), 30); textField.setEnabled(cb.isSelected()); textField.selectAll(); p.add(textField); p.setBorder( new TitledBorder( SgEngine.getInstance() .getResourceBundle() .getString("midi.track.editTrackName.trackName"))); panel.add(p); Object[] message = new Object[] { panel, }; int option = JOptionPane.showConfirmDialog( parent, message, SgEngine.getInstance().getResourceBundle().getString("midi.track.editTrackName.title"), JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE, null); if (option == JOptionPane.OK_OPTION) { String name; if (cb.isSelected()) { name = textField.getText(); } else { name = null; } if (name == null && trackName == null || name != null && name.equals(trackName)) { System.out.println("track name left unchanged!"); // nothing to do... } else { ChangeTrackNameEdit edit = new ChangeTrackNameEdit(track, name, midiDescriptor, changeObj); edit.perform(); midiDescriptor.getUndoManager().addEdit(edit); return name; } } return null; }