Skip to content

Commit

Permalink
no point to keep this logic in AbstractHandler, when it is used only …
Browse files Browse the repository at this point in the history
…in Chord with two lines of code
  • Loading branch information
klesun authored and klesun committed Jan 10, 2016
1 parent 35dc56e commit 355760d
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 81 deletions.
68 changes: 5 additions & 63 deletions src/org/klesun_model/AbstractHandler.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package org.klesun_model;

import org.json.JSONObject;
import org.shmidusic.Main;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

abstract public class AbstractHandler implements KeyListener, MouseListener, MouseMotionListener {

abstract public class AbstractHandler implements KeyListener
{
// constants
final public static int ctrl = KeyEvent.CTRL_MASK;
final public static KeyEvent k = new KeyEvent(new JPanel(),0,0,0,0,'h'); // just for constants
Expand All @@ -35,6 +36,8 @@ final public void keyPressed(KeyEvent e)
final public void keyTyped(KeyEvent e) {}
final public void keyReleased(KeyEvent e) {}

private Queue<JSONObject> undoQueue = new LinkedList<>();

final public Explain handleKey(Combo combo) {
Explain result = null;

Expand All @@ -54,65 +57,4 @@ final public Explain handleKey(Combo combo) {
public IComponent getContext() {
return this.context;
}

//---------
// mouse
//--------

// override us, please!
public Boolean mouseDraggedFinal(ComboMouse combo) { return false; } // TODO: remove "final" from name somehow, it's ugly, they are almost abstract
public Boolean mousePressedFinal(ComboMouse combo) { return false; }
public Boolean mouseReleasedFinal(ComboMouse combo) { return false; }
public Boolean mouseMovedFinal(ComboMouse combo) { return false; }

final public Boolean mousePressed(ComboMouse combo) {
if (mousePressedFinal(combo)) {
this.mouseLocation = combo.getPoint();
return true;
} else {
int x = Component.class.cast(getContext()).getX();
int y = Component.class.cast(getContext()).getY();
combo.getPoint().move(x, y);
return (getContext().getModelParent() != null && getContext().getModelParent().getHandler().mousePressed(combo));
}
}

final public Boolean mouseReleased(ComboMouse combo) {
if (mouseReleasedFinal(combo)) {
this.mouseLocation = combo.getPoint();
return true;
} else {
int x = Component.class.cast(getContext()).getX();
int y = Component.class.cast(getContext()).getY();
combo.getPoint().move(x, y);
return (getContext().getModelParent() != null && getContext().getModelParent().getHandler().mouseReleased(combo));
}
}

final public Boolean mouseDragged(ComboMouse combo) {
if (mouseDraggedFinal(combo)) {
this.mouseLocation = combo.getPoint();
return true;
} else {
return (getContext().getModelParent() != null && getContext().getModelParent().getHandler().mouseDragged(combo));
}
}

final public Boolean mouseMoved(ComboMouse combo) {
// TODO: maybe do it as in mouseDragged() if got issues
Boolean result = mouseMovedFinal(combo) ||
(getContext().getModelParent() != null && getContext().getModelParent().getHandler().mouseMoved(combo));
this.mouseLocation = combo.getPoint();
return result;
}

// implementing mouse listeners
final public void mousePressed(MouseEvent e) { mousePressed(new ComboMouse(e).setOrigin(getContext())); }
final public void mouseReleased(MouseEvent e) { mouseReleased(new ComboMouse(e).setOrigin(getContext())); }
final public void mouseDragged(MouseEvent e) { mouseDragged(new ComboMouse(e, mouseLocation).setOrigin(getContext())); }
final public void mouseMoved(MouseEvent e) { mouseMoved(new ComboMouse(e, mouseLocation).setOrigin(getContext())); }
// useless for now - so i put final
final public void mouseEntered(MouseEvent e) {}
final public void mouseExited(MouseEvent e) {}
final public void mouseClicked(MouseEvent e) {}
}
7 changes: 1 addition & 6 deletions src/org/shmidusic/sheet_music/staff/Staff.java
Original file line number Diff line number Diff line change
Expand Up @@ -203,12 +203,7 @@ public Chord getFocusedAccord() {

@Deprecated // well, you see...
public List<Chord> getChordList() {
return chordStream().collect(Collectors.toList());
}

public Stream<Chord> chordStream()
{
return StreamSupport.stream(chordList.spliterator(), false);
return chordList.stream().collect(Collectors.toList());
}

public List<List<Chord>> getAccordRowList(int rowSize)
Expand Down
3 changes: 2 additions & 1 deletion src/org/shmidusic/sheet_music/staff/StaffComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public StaffComponent(Staff staff, SheetMusicComponent parent) {

this.initLayout();

staff.chordStream().forEach(this::addComponent);
staff.chordList.forEach(this::addComponent);
this.setBackground(Color.WHITE);
this.revalidate();
}
Expand Down Expand Up @@ -240,6 +240,7 @@ public Explain moveFocus(int n)

public StaffComponent setFocus(ChordComponent comp)
{
cancelSelection();
setFocus(staff.chordList.indexOf(comp.chord));

playback.interrupt();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.shmidusic.sheet_music.staff.staff_config.StaffConfig;
import org.shmidusic.stuff.graphics.Settings;
import org.shmidusic.stuff.midi.DeviceEbun;
import org.shmidusic.stuff.tools.Fp;

import javax.swing.*;
import java.awt.*;
Expand All @@ -41,7 +42,10 @@ public ChordComponent(Chord chord, IComponent parent)
this.chord = chord;
chord.noteList.forEach(this::addComponent);

this.addMouseListener(handler);
this.addMouseListener(Fp.onClick(e -> {
getParentComponent().setFocus(this);
getParentComponent().getModelParent().requestFocus();
}));
}

public NoteComponent addNewNote(int tune, int channel)
Expand Down
8 changes: 0 additions & 8 deletions src/org/shmidusic/sheet_music/staff/chord/ChordHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,4 @@ private static ContextAction<ChordComponent> mkFailableAction(Function<ChordComp
ContextAction<ChordComponent> action = new ContextAction<>();
return action.setRedo(lambda);
}

@Override
public Boolean mousePressedFinal(ComboMouse combo)
{
getContext().getParentComponent().setFocus(getContext());
getContext().getParentComponent().getModelParent().requestFocus();
return true;
}
}
4 changes: 2 additions & 2 deletions src/org/shmidusic/stuff/midi/SimpleMidiParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ public static SMF sheetMusicToSmf(SheetMusic sheetMusic)
Function<Channel, Event> mapChannelInstrument = c -> new PChange(c.getInstrument().shortValue(), c.channelNumber.get().shortValue(), 0);
Function<Channel, Event> mapChannelVolume = c -> new CChange(DeviceEbun.CONTROL_CHANGE_VOLUME, c.getVolume().shortValue(), c.channelNumber.get().shortValue(), 0);

Set<Integer> usedChannels = staff.chordStream()
Set<Integer> usedChannels = staff.chordList.stream()
.map(c -> c.noteStream().map(INote::getChannel))
.flatMap(s -> s)
.distinct()
.collect(Collectors.toSet());

List<Channel> channels = StreamSupport.stream(staff.getConfig().channelList.spliterator(), false)
List<Channel> channels = staff.getConfig().channelList.stream()
.filter(c -> usedChannels.contains(c.channelNumber.get()))
.collect(Collectors.toList());

Expand Down

0 comments on commit 355760d

Please sign in to comment.