Skip to content

Commit 68f589b

Browse files
committed
feat: add replace instrument functionality and update context menu
1 parent 4c0e916 commit 68f589b

File tree

1 file changed

+65
-7
lines changed

1 file changed

+65
-7
lines changed

blue-ui-core/src/main/java/blue/ui/core/orchestra/ArrangementEditPanel.java

Lines changed: 65 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -365,21 +365,54 @@ public void copyInstrument() {
365365
}
366366

367367
public void pasteInstrument() {
368-
Object obj = CopyBuffer.getBufferedObject(CopyBuffer.INSTRUMENT);
368+
Instrument clone = getBufferedInstrumentClone();
369+
370+
if (clone == null) {
371+
return;
372+
}
373+
374+
addInstrument(clone);
375+
}
376+
377+
public void replaceInstrument() {
378+
int selectedRow = arrangementTable.getSelectedRow();
379+
if (arrangement == null || selectedRow < 0) {
380+
return;
381+
}
382+
383+
Instrument replacement = getBufferedInstrumentClone();
369384

370-
if (!(obj instanceof Instrument)) {
385+
if (replacement == null) {
371386
return;
372387
}
373388

374-
Instrument instr = (Instrument) obj;
389+
String instrumentId = (String) arrangement.getValueAt(selectedRow, 1);
390+
Instrument currentInstrument = arrangement.getInstrument(selectedRow);
391+
392+
arrangement.replaceInstrument(instrumentId, replacement);
393+
394+
OrchestraEdit edit = new OrchestraEdit(arrangement, currentInstrument,
395+
replacement, instrumentId, OrchestraEdit.REPLACE);
396+
BlueUndoManager.setUndoManager("orchestra");
397+
BlueUndoManager.addEdit(edit);
398+
399+
arrangementTable.getSelectionModel().setSelectionInterval(selectedRow, selectedRow);
400+
}
401+
402+
private Instrument getBufferedInstrumentClone() {
403+
Object obj = CopyBuffer.getBufferedObject(CopyBuffer.INSTRUMENT);
404+
405+
if (!(obj instanceof Instrument instr)) {
406+
return null;
407+
}
375408

376409
Instrument clone = instr.deepCopy();
377410

378411
if (clone instanceof BlueSynthBuilder blueSynthBuilder) {
379412
blueSynthBuilder.clearParameters();
380413
}
381414

382-
addInstrument(clone);
415+
return clone;
383416
}
384417

385418
// public void instrumentRemoved(Instrument instr) {
@@ -603,6 +636,9 @@ class InstrumentTablePopup extends JPopupMenu {
603636
JMenuItem pasteMenuItem = new JMenuItem(BlueSystem
604637
.getString("instrument.paste"));
605638

639+
JMenuItem replaceMenuItem = new JMenuItem(BlueSystem
640+
.getString("instrument.replace"));
641+
606642
JMenuItem convertToBSB = new JMenuItem();
607643

608644
Action exportItem;
@@ -620,6 +656,9 @@ public InstrumentTablePopup() {
620656
pasteMenuItem.addActionListener((ActionEvent e) -> {
621657
pasteInstrument();
622658
});
659+
replaceMenuItem.addActionListener((ActionEvent e) -> {
660+
replaceInstrument();
661+
});
623662

624663
convertToBSB.setText("Convert to BlueSynthBuilder");
625664
convertToBSB.addActionListener((ActionEvent e) -> {
@@ -727,6 +766,7 @@ public void actionPerformed(ActionEvent e) {
727766
this.add(cutMenuItem);
728767
this.add(copyMenuItem);
729768
this.add(pasteMenuItem);
769+
this.add(replaceMenuItem);
730770
this.addSeparator();
731771
this.add(convertToBSB);
732772
this.addSeparator();
@@ -761,6 +801,7 @@ public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
761801
Object bufferedObj = CopyBuffer.getBufferedObject(CopyBuffer.INSTRUMENT);
762802
boolean bufferFull = (bufferedObj instanceof Instrument);
763803
pasteMenuItem.setEnabled(bufferFull);
804+
replaceMenuItem.setEnabled(selected && bufferFull);
764805
}
765806

766807
@Override
@@ -829,19 +870,30 @@ private static class OrchestraEdit extends AbstractUndoableEdit {
829870

830871
public static final int REMOVE = 1;
831872

873+
public static final int REPLACE = 2;
874+
832875
private final Arrangement arrangement;
833876

834877
private final int type;
835878

836879
private final Instrument instr;
837880

881+
private final Instrument replacementInstr;
882+
838883
private final String instrId;
839884

840885
public OrchestraEdit(Arrangement arrangement, Instrument instr,
841886
String instrId, int type) {
842887

888+
this(arrangement, instr, null, instrId, type);
889+
}
890+
891+
public OrchestraEdit(Arrangement arrangement, Instrument instr,
892+
Instrument replacementInstr, String instrId, int type) {
893+
843894
this.arrangement = arrangement;
844895
this.instr = instr;
896+
this.replacementInstr = replacementInstr;
845897
this.instrId = instrId;
846898
this.type = type;
847899
}
@@ -852,8 +904,10 @@ public void redo() throws CannotRedoException {
852904

853905
if (this.type == ADD) {
854906
arrangement.addInstrumentWithId(instr, instrId);
855-
} else {
907+
} else if (this.type == REMOVE) {
856908
arrangement.removeInstrument(instrId);
909+
} else {
910+
arrangement.replaceInstrument(instrId, replacementInstr);
857911
}
858912
}
859913

@@ -863,17 +917,21 @@ public void undo() throws CannotUndoException {
863917

864918
if (this.type == ADD) {
865919
arrangement.removeInstrument(instrId);
866-
} else {
920+
} else if (this.type == REMOVE) {
867921
arrangement.addInstrumentWithId(instr, instrId);
922+
} else {
923+
arrangement.replaceInstrument(instrId, instr);
868924
}
869925
}
870926

871927
@Override
872928
public String getPresentationName() {
873929
if (this.type == ADD) {
874930
return "Add Instrument";
931+
} else if (this.type == REMOVE) {
932+
return "Remove Instrument";
875933
}
876-
return "Remove Instrument";
934+
return "Replace Instrument";
877935
}
878936
}
879937
}

0 commit comments

Comments
 (0)