Skip to content

Commit

Permalink
mml generator: exception (MML Text model)
Browse files Browse the repository at this point in the history
  • Loading branch information
fourthline committed Nov 1, 2014
1 parent 729796f commit b8fd643
Show file tree
Hide file tree
Showing 23 changed files with 196 additions and 121 deletions.
1 change: 1 addition & 0 deletions properties/appResource.properties
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ error.needDls=DLS file is required
error.read=Failed to read
error.nofile=File not found
error.invalid_file=Invalid file format
fail.saveFile=Failed to save

####### message #######
message.throw=Change up to now will be destroyed, OK?
Expand Down
1 change: 1 addition & 0 deletions properties/appResource_ja.properties
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ error.needDls=DLSファイルが必要です
error.read=読み込みに失敗しました
error.nofile=指定されたファイルがありません
error.invalid_file=ファイル形式が不正です
fail.saveFile=ファイルの保存に失敗しました

####### message #######
message.throw=いままでの変更が破棄されますが、よろしいですか?
Expand Down
23 changes: 18 additions & 5 deletions src/fourthline/mabiicco/ActionDispatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import fourthline.mabiicco.ui.MainFrame;
import fourthline.mabiicco.ui.editor.MMLTranspose;
import fourthline.mmlTools.MMLScore;
import fourthline.mmlTools.UndefinedTickException;
import fourthline.mmlTools.parser.IMMLFileParser;
import fourthline.mmlTools.parser.MMLFile;
import fourthline.mmlTools.parser.MMLParseException;
Expand Down Expand Up @@ -182,7 +183,7 @@ private void initializeActionMap() {
actionMap.put(ADD_MEASURE, mmlSeqView::addMeasure);
actionMap.put(REMOVE_MEASURE, mmlSeqView::removeMeasure);
actionMap.put(NOTE_PROPERTY, editState::noteProperty);
actionMap.put(TRANSPOSE, () -> new MMLTranspose().execute(mainFrame, mmlSeqView));
actionMap.put(TRANSPOSE, () -> new MMLTranspose(mmlSeqView.getFileState()).execute(mainFrame, mmlSeqView));
actionMap.put(ABOUT, () -> new About().show(mainFrame));
actionMap.put(MIDI_EXPORT, this::midiExportAction);
actionMap.put(FILE_IMPORT, this::fileImportAction);
Expand Down Expand Up @@ -261,15 +262,24 @@ public void reloadMMLFileAction() {
}
}

private void saveMMLFile(File file) {
/**
* @param file
* @return 保存に成功した場合は true, 失敗した場合は false を返す.
*/
private boolean saveMMLFile(File file) {
try {
// generateできないデータは保存させない.
mmlSeqView.getMMLScore().generateAll();
FileOutputStream outputStream = new FileOutputStream(file);
mmlSeqView.getMMLScore().writeToOutputStream(outputStream);
mainFrame.setTitleAndFileName(file.getName());
fileState.setOriginalBase();
notifyUpdateFileState();
MabiIccoProperties.getInstance().setRecentFile(file.getPath());
} catch (Exception e) {}
} catch (UndefinedTickException | IOException e) {
return false;
}
return true;
}

private void newMMLFileAction() {
Expand Down Expand Up @@ -351,8 +361,11 @@ private boolean showDialogSaveFile() {
saveFileChooser.setFileFilter(mmiFilter);
File file = showSaveDialog(saveFileChooser, "mmi");
if (file != null) {
saveMMLFile(file);
openedFile = file;
if (saveMMLFile(file)) {
openedFile = file;
} else {
JOptionPane.showMessageDialog(mainFrame, AppResource.appText("fail.saveFile"), "ERROR", JOptionPane.ERROR_MESSAGE);
}
return true;
}
return false;
Expand Down
1 change: 1 addition & 0 deletions src/fourthline/mabiicco/IFileState.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public interface IFileState {
public boolean canUndo();
public boolean canRedo();
public void saveState();
public void revertState();

public void setOriginalBase();
public void setFileStateObserver(IFileStateObserver observer);
Expand Down
7 changes: 4 additions & 3 deletions src/fourthline/mabiicco/ui/ColumnPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import javax.swing.JPopupMenu;
import javax.swing.SwingUtilities;

import fourthline.mabiicco.IFileState;
import fourthline.mabiicco.MabiIccoProperties;
import fourthline.mabiicco.midi.MabiDLS;
import fourthline.mabiicco.ui.editor.IEditAlign;
Expand All @@ -46,16 +47,16 @@ public final class ColumnPanel extends JPanel implements MouseListener, MouseMot
private final JPopupMenu popupMenu = new JPopupMenu();
private final ArrayList<IMarkerEditor> markerEditor = new ArrayList<>();

public ColumnPanel(PianoRollView pianoRollView, IMMLManager mmlManager, IEditAlign editAlign) {
public ColumnPanel(PianoRollView pianoRollView, IMMLManager mmlManager, IEditAlign editAlign, IFileState fileState) {
super();
this.pianoRollView = pianoRollView;
this.mmlManager = mmlManager;
this.editAlign = editAlign;
addMouseListener(this);
addMouseMotionListener(this);

markerEditor.add( new MMLTempoEditor(mmlManager, editAlign) );
markerEditor.add( new MarkerEditor(mmlManager, editAlign) );
markerEditor.add( new MMLTempoEditor(mmlManager, editAlign, fileState) );
markerEditor.add( new MarkerEditor(mmlManager, editAlign, fileState) );

// popupMenu に各MenuItemを登録する.
markerEditor.forEach(t -> t.getMenuItems().forEach(popupMenu::add));
Expand Down
4 changes: 2 additions & 2 deletions src/fourthline/mabiicco/ui/MMLSeqView.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ public MMLSeqView() {
panel.add(tabbedPane, BorderLayout.SOUTH);

// create mml editor
editor = new MMLEditor(keyboardView, pianoRollView, this);
editor = new MMLEditor(keyboardView, pianoRollView, this, undoEdit);
pianoRollView.addMouseInputListener(editor);
columnView = new ColumnPanel(pianoRollView, this, editor);
columnView = new ColumnPanel(pianoRollView, this, editor, undoEdit);

scrollPane.setRowHeaderView(keyboardView);
scrollPane.setColumnHeaderView(columnView);
Expand Down
17 changes: 13 additions & 4 deletions src/fourthline/mabiicco/ui/editor/AbstractMarkerEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
import javax.swing.JMenuItem;

import fourthline.mabiicco.AppResource;
import fourthline.mabiicco.IFileState;
import fourthline.mabiicco.ui.IMMLManager;
import fourthline.mmlTools.MMLEvent;
import fourthline.mmlTools.UndefinedTickException;

/**
* Marker (for MMLEvent) Editor
Expand All @@ -37,18 +39,20 @@ public abstract class AbstractMarkerEditor<T extends MMLEvent> implements IMarke
protected final String deleteCommand;

private final IEditAlign editAlign;
private final IFileState fileState;
protected final IMMLManager mmlManager;

protected T targetEvent;
protected int targetTick;

public AbstractMarkerEditor(String suffix, IMMLManager mmlManager, IEditAlign editAlign) {
public AbstractMarkerEditor(String suffix, IMMLManager mmlManager, IEditAlign editAlign, IFileState fileState) {
this.suffix = suffix;
this.insertCommand = "insert_" + suffix;
this.editCommand = "edit_" + suffix;
this.deleteCommand = "delete_" + suffix;
this.mmlManager = mmlManager;
this.editAlign = editAlign;
this.fileState = fileState;

insertMenu = newMenuItem(AppResource.appText("edit."+insertCommand));
insertMenu.setActionCommand(insertCommand);
Expand Down Expand Up @@ -99,16 +103,21 @@ private T getTempoEventOnTick(int baseTick, int delta) {
}

@Override
public void actionPerformed(ActionEvent e) {
String actionCommand = e.getActionCommand();
public void actionPerformed(ActionEvent event) {
String actionCommand = event.getActionCommand();
if (actionCommand.equals(insertCommand)) {
insertAction();
} else if (actionCommand.equals(editCommand)) {
editAction();
} else if (actionCommand.equals(deleteCommand)) {
deleteAction();
}


try {
mmlManager.getMMLScore().generateAll();
} catch (UndefinedTickException e) {
fileState.revertState();
}
mmlManager.updateActivePart();
}

Expand Down
28 changes: 22 additions & 6 deletions src/fourthline/mabiicco/ui/editor/MMLEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import fourthline.mabiicco.AppResource;
import fourthline.mabiicco.IEditState;
import fourthline.mabiicco.IEditStateObserver;
import fourthline.mabiicco.IFileState;
import fourthline.mabiicco.midi.MabiDLS;
import fourthline.mabiicco.ui.IMMLManager;
import fourthline.mabiicco.ui.KeyboardView;
Expand Down Expand Up @@ -54,6 +55,7 @@ public final class MMLEditor implements MouseInputListener, IEditState, IEditCon
private final PianoRollView pianoRollView;
private final KeyboardView keyboardView;
private final IMMLManager mmlManager;
private final IFileState fileState;

private final JPopupMenu popupMenu = new JPopupMenu();

Expand Down Expand Up @@ -86,10 +88,11 @@ public static NoteAlign[] createAlignList() {
return list.toArray(new NoteAlign[list.size()]);
}

public MMLEditor(KeyboardView keyboard, PianoRollView pianoRoll, IMMLManager mmlManager) {
public MMLEditor(KeyboardView keyboard, PianoRollView pianoRoll, IMMLManager mmlManager, IFileState fileState) {
this.keyboardView = keyboard;
this.pianoRollView = pianoRoll;
this.mmlManager = mmlManager;
this.fileState = fileState;

pianoRoll.setSelectNote(selectedNote);

Expand Down Expand Up @@ -168,6 +171,19 @@ private void selectMultipleNote(MMLNoteEvent noteEvent1, MMLNoteEvent noteEvent2
}
}

/**
* 編集結果をMML文字列に反映させる. 失敗時はREVERT.
*/
private void mmlGenerate() {
try {
mmlManager.getMMLScore().generateAll();
} catch (UndefinedTickException e) {
System.out.println("REVERT: " + e.getMessage());
fileState.revertState();
}
mmlManager.updateActivePart();
}

/**
* @param point nullのときはクリアする.
*/
Expand Down Expand Up @@ -281,7 +297,7 @@ public void applyEditNote(boolean select) {
if (!select) {
selectNote(null);
}
mmlManager.updateActivePart();
mmlGenerate();
keyboardView.offNote();
}

Expand Down Expand Up @@ -460,7 +476,7 @@ public void paste(long startTick) {
}

editObserver.notifyUpdateEditState();
mmlManager.updateActivePart();
mmlGenerate();
}

@Override
Expand All @@ -473,7 +489,7 @@ public void selectedCut() {

selectNote(null);
editObserver.notifyUpdateEditState();
mmlManager.updateActivePart();
mmlGenerate();
}

@Override
Expand All @@ -494,7 +510,7 @@ public void selectedDelete() {

selectNote(null);
editObserver.notifyUpdateEditState();
mmlManager.updateActivePart();
mmlGenerate();
}

@Override
Expand All @@ -504,7 +520,7 @@ public void noteProperty() {
}

new MMLNotePropertyPanel(selectedNote.toArray(new MMLNoteEvent[selectedNote.size()]), editEventList).showDialog();
mmlManager.updateActivePart();
mmlGenerate();
}

public void changePart(MMLEventList from, MMLEventList to, boolean useSelectedNoteList, ChangePartAction action) {
Expand Down
6 changes: 6 additions & 0 deletions src/fourthline/mabiicco/ui/editor/MMLScoreUndoEdit.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ public void saveState() {
System.out.println("saveState() "+undoState.size());
}

@Override
public void revertState() {
MMLScore score = mmlManager.getMMLScore();
score.putObjectState(undoState.lastElement());
}

@Override
public void undo() throws CannotUndoException {
super.undo();
Expand Down
5 changes: 3 additions & 2 deletions src/fourthline/mabiicco/ui/editor/MMLTempoEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import javax.swing.SpinnerNumberModel;

import fourthline.mabiicco.AppResource;
import fourthline.mabiicco.IFileState;
import fourthline.mabiicco.ui.IMMLManager;
import fourthline.mmlTools.MMLTempoEvent;

Expand All @@ -27,8 +28,8 @@
*/
public final class MMLTempoEditor extends AbstractMarkerEditor<MMLTempoEvent> {

public MMLTempoEditor(IMMLManager mmlManager, IEditAlign editAlign) {
super("tempo", mmlManager, editAlign);
public MMLTempoEditor(IMMLManager mmlManager, IEditAlign editAlign, IFileState fileState) {
super("tempo", mmlManager, editAlign, fileState);
}

private int showTempoInputDialog(String title, int tempo) {
Expand Down
13 changes: 13 additions & 0 deletions src/fourthline/mabiicco/ui/editor/MMLTranspose.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,22 @@
import javax.swing.SpinnerNumberModel;

import fourthline.mabiicco.AppResource;
import fourthline.mabiicco.IFileState;
import fourthline.mabiicco.midi.InstType;
import fourthline.mabiicco.midi.MabiDLS;
import fourthline.mabiicco.ui.IMMLManager;
import fourthline.mmlTools.MMLEventList;
import fourthline.mmlTools.MMLNoteEvent;
import fourthline.mmlTools.MMLTrack;
import fourthline.mmlTools.UndefinedTickException;

public final class MMLTranspose {
private final IFileState fileState;

public MMLTranspose(IFileState fileState) {
this.fileState = fileState;
}

public void execute(Frame parentFrame, IMMLManager mmlManager) {
int transpose = showTransposeDialog(parentFrame);
if (transpose == 0) {
Expand All @@ -41,6 +49,11 @@ public void execute(Frame parentFrame, IMMLManager mmlManager) {
}
}

try {
mmlManager.getMMLScore().generateAll();
} catch (UndefinedTickException e) {
fileState.revertState();
}
mmlManager.updateActivePart();
}

Expand Down
5 changes: 3 additions & 2 deletions src/fourthline/mabiicco/ui/editor/MarkerEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import javax.swing.JTextField;

import fourthline.mabiicco.AppResource;
import fourthline.mabiicco.IFileState;
import fourthline.mabiicco.ui.IMMLManager;
import fourthline.mmlTools.Marker;

Expand All @@ -27,8 +28,8 @@
*/
public final class MarkerEditor extends AbstractMarkerEditor<Marker> {

public MarkerEditor(IMMLManager mmlManager, IEditAlign editAlign) {
super("marker", mmlManager, editAlign);
public MarkerEditor(IMMLManager mmlManager, IEditAlign editAlign, IFileState fileState) {
super("marker", mmlManager, editAlign, fileState);
}

private String showTextInputDialog(String title, String text) {
Expand Down
2 changes: 1 addition & 1 deletion src/fourthline/mmlTools/MMLEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@ public int getTickOffset() {

public abstract String toString();

public abstract String toMMLString();
public abstract String toMMLString() throws UndefinedTickException;
}
Loading

0 comments on commit b8fd643

Please sign in to comment.