Skip to content

Commit

Permalink
aaaaahhhhh, interfaces are so greatly better than abstract classes wh…
Browse files Browse the repository at this point in the history
…ere interfaces is enough... There is no more ununderstandable AbstractHandler in the project, is only IKeyHandler interface with 1 small default method and two abstract methods
  • Loading branch information
klesun authored and klesun committed Jan 10, 2016
1 parent 355760d commit 0d47b47
Show file tree
Hide file tree
Showing 12 changed files with 92 additions and 107 deletions.
60 changes: 0 additions & 60 deletions src/org/klesun_model/AbstractHandler.java

This file was deleted.

2 changes: 1 addition & 1 deletion src/org/klesun_model/IComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public interface IComponent
IModel getModel();
IComponent getModelParent();
IComponent getFocusedChild();
AbstractHandler getHandler();
IKeyHandler getHandler();

default Component getFirstAwtParent() {
IComponent context = this;
Expand Down
46 changes: 46 additions & 0 deletions src/org/klesun_model/IKeyHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package org.klesun_model;

// the IKeyHandler always comes in bound with Model's IComponent the idea of IKeyHandler is
// such that the Model root Handler's handleKey() is called when we got the key
// event from GUI; IKeyHandler checks the deepest focused child, whether it listens
// for this key, if not - it's parent, if it's neither - it's parent, and so on, til
// we get to the root we started from, and, if the root does not listen neither - we ignore event

// we handle event ONLY ONCE - in the deepest focused child (+ root) we found

import org.shmidusic.Main;

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

public interface IKeyHandler
{
final static int ctrl = KeyEvent.CTRL_MASK;
final static KeyEvent k = new KeyEvent(new JPanel(),0,0,0,0,'h'); // just for constants

default Explain handleKey(Combo combo)
{
Explain result = null;

if (getContext().getFocusedChild() != null) {
result = getContext().getFocusedChild().getHandler().handleKey(combo);
}

if ((result == null || !result.isSuccess()) && getMyClassActionMap().containsKey(combo)) {

result = getMyClassActionMap().get(combo).redo(getContext());
Main.window.updateMenuBar();
}

return result != null ? result : new Explain(false, "No Action For This Combination").setImplicit(true);
}

// we use this to (de)generate [File,Menu,Edit]-like menu that would
// (with few decorative exclusions) completely correspond whole model key mapping.
// the sad thing is, due to Java not supporting abstract class methods, we generate
// every IModel field structure on the fly upon instance creation - so we call this from fake instances
LinkedHashMap<Combo, ContextAction> getMyClassActionMap();

IComponent getContext();
}
15 changes: 11 additions & 4 deletions src/org/shmidusic/sheet_music/SheetMusicComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class SheetMusicComponent extends JPanel implements IComponent
{
final public MainPanel mainPanel;
final public SheetMusic sheetMusic;
final AbstractHandler handler;
final IKeyHandler handler;

private Set<StaffComponent> staffComponentSet = new HashSet<>();

Expand All @@ -43,7 +43,8 @@ public SheetMusicComponent(SheetMusic sheetMusic, MainPanel mainPanel)

this.setFocusable(true);

this.handler = new AbstractHandler(this) {
IComponent self = this;
this.handler = new IKeyHandler() {
public LinkedHashMap<Combo, ContextAction> getMyClassActionMap() {
return new TruMap<>()
.p(new Combo(ctrl, k.VK_L), mkFailableAction(SheetMusicComponent::splitFocusedStaff))
Expand All @@ -58,8 +59,14 @@ public LinkedHashMap<Combo, ContextAction> getMyClassActionMap() {
.p(new Combo(ctrl, k.VK_E), mkFailableAction(FileProcessor::savePNG).setCaption("Export png"))
;
}
public IComponent getContext() { return self; };
};
this.addKeyListener(handler);
this.addKeyListener(Fp.onKey(e -> {
Explain result = handler.handleKey(new Combo(e));
if (!result.isSuccess() && !result.isImplicit()) {
JOptionPane.showMessageDialog(this, result.getExplanation());
}
}));
addMouseListener(Fp.onClick(e -> requestFocus()));
}

Expand Down Expand Up @@ -147,7 +154,7 @@ public StaffComponent getFocusedChild() {
}

@Override
public AbstractHandler getHandler() {
public IKeyHandler getHandler() {
return this.handler;
}

Expand Down
12 changes: 4 additions & 8 deletions src/org/shmidusic/sheet_music/staff/MidianaComponent.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
package org.shmidusic.sheet_music.staff;

import org.shmidusic.MainPanel;
import org.klesun_model.IKeyHandler;
import org.shmidusic.stuff.graphics.ImageStorage;
import org.shmidusic.stuff.graphics.Settings;
import org.klesun_model.AbstractHandler;
import org.klesun_model.IComponent;

import java.awt.*;
import java.awt.event.FocusListener;

@Deprecated // sorry, forgot the reason
abstract public class MidianaComponent implements IComponent
{

final private IComponent parent;
final private AbstractHandler eventHandler;
final private IKeyHandler eventHandler;

abstract public MidianaComponent getFocusedChild();
abstract protected AbstractHandler makeHandler();
abstract protected IKeyHandler makeHandler();
/** @return int - position of bottomest drawn pixel */

// TODO: separate Model from Event handler, i wanna be able to instantiate Note without Staff!
Expand All @@ -28,7 +24,7 @@ public MidianaComponent(IComponent parent) {

// TODO: rename to getComponentParent()
public IComponent getModelParent() { return this.parent; }
public AbstractHandler getHandler() { return this.eventHandler; }
public IKeyHandler getHandler() { return this.eventHandler; }

final public Settings getSettings() {
return Settings.inst();
Expand Down
6 changes: 3 additions & 3 deletions src/org/shmidusic/sheet_music/staff/StaffComponent.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.shmidusic.sheet_music.staff;

import org.klesun_model.AbstractHandler;
import org.klesun_model.Explain;
import org.klesun_model.IComponent;
import org.shmidusic.sheet_music.SheetMusicComponent;
Expand Down Expand Up @@ -30,7 +29,8 @@ public class StaffComponent extends JPanel implements IComponent
private boolean isSelectionActive = false;
private int selectionStart = -1;

public StaffComponent(Staff staff, SheetMusicComponent parent) {
public StaffComponent(Staff staff, SheetMusicComponent parent)
{
this.parent = parent;
this.staff = staff;
this.handler = new StaffHandler(this);
Expand Down Expand Up @@ -174,7 +174,7 @@ public ChordComponent getFocusedChild()
}

@Override
public AbstractHandler getHandler() {
public StaffHandler getHandler() {
return this.handler;
}

Expand Down
8 changes: 5 additions & 3 deletions src/org/shmidusic/sheet_music/staff/StaffHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;

public class StaffHandler extends AbstractHandler {
public class StaffHandler implements IKeyHandler
{
final private StaffComponent context;

public StaffHandler(StaffComponent context) { super(context); }
public StaffHandler(StaffComponent context) { this.context = context; }
public StaffComponent getContext() {
return (StaffComponent)super.getContext();
return context;
}

private static TruMap<Combo, ContextAction<StaffComponent>> actionMap = new TruMap<>();
Expand Down
7 changes: 2 additions & 5 deletions src/org/shmidusic/sheet_music/staff/chord/ChordComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

import org.apache.commons.math3.fraction.Fraction;
import org.json.JSONObject;
import org.klesun_model.AbstractHandler;
import org.klesun_model.Explain;
import org.klesun_model.IComponent;
import org.klesun_model.IModel;
import org.klesun_model.*;
import org.klesun_model.field.IField;
import org.shmidusic.sheet_music.staff.Staff;
import org.shmidusic.sheet_music.staff.chord.note.Note;
Expand Down Expand Up @@ -187,7 +184,7 @@ public NoteComponent getFocusedChild() {
}

@Override
public AbstractHandler getHandler() {
public IKeyHandler getHandler() {
return this.handler;
}

Expand Down
12 changes: 6 additions & 6 deletions src/org/shmidusic/sheet_music/staff/chord/ChordHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@
import java.util.*;
import java.util.function.*;

public class ChordHandler extends AbstractHandler {

public class ChordHandler implements IKeyHandler
{
final public static int ACCORD_EPSILON = Note.getTimeMilliseconds(new Fraction(1, 16), 120); // 0.125 sec

final private ChordComponent context;

public ChordHandler(ChordComponent context) {
super(context);
this.context = context;
}

@Override
public ChordComponent getContext() {
return (ChordComponent)super.getContext();
return context;
}

private static TruMap<Combo, ContextAction<ChordComponent>> actionMap = new TruMap<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.shmidusic.sheet_music.staff.chord.note;

import org.klesun_model.AbstractHandler;
import org.klesun_model.Explain;
import org.klesun_model.IKeyHandler;
import org.shmidusic.sheet_music.staff.MidianaComponent;
import org.shmidusic.sheet_music.staff.chord.ChordComponent;
import org.shmidusic.sheet_music.staff.staff_config.KeySignature;
Expand Down Expand Up @@ -34,7 +34,7 @@ public MidianaComponent getFocusedChild() {
}

@Override
protected AbstractHandler makeHandler() {
protected IKeyHandler makeHandler() {
return new NoteHandler(this);
}

Expand Down
16 changes: 6 additions & 10 deletions src/org/shmidusic/sheet_music/staff/chord/note/NoteHandler.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package org.shmidusic.sheet_music.staff.chord.note;

import org.klesun_model.AbstractHandler;
import org.klesun_model.Combo;
import org.klesun_model.ContextAction;
import org.klesun_model.Explain;
import org.klesun_model.*;
import org.klesun_model.field.Field;
import org.klesun_model.field.IField;
import org.shmidusic.stuff.graphics.Settings;
Expand All @@ -19,10 +16,12 @@
import java.util.function.Consumer;
import java.util.function.Function;

public class NoteHandler extends AbstractHandler {
public class NoteHandler implements IKeyHandler
{
final private NoteComponent context;

public NoteHandler(NoteComponent context) {
super(context);
this.context = context;
}
private static TruMap<Combo, ContextAction<NoteComponent>> actionMap = new TruMap<>();
static {
Expand Down Expand Up @@ -71,12 +70,9 @@ private static Consumer<NoteComponent> modelRecalcTacts(Consumer<Note> modelLamb
};
}

@Override
public NoteComponent getContext() {
return (NoteComponent)super.getContext();
return context;
}

@Override
public LinkedHashMap<Combo, ContextAction> getMyClassActionMap() { return actionMap; }

public static LinkedHashMap<Combo, ContextAction<NoteComponent>> getClassActionMap() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package org.shmidusic.sheet_music.staff.staff_config;

import org.klesun_model.AbstractHandler;
import org.klesun_model.Combo;
import org.klesun_model.ContextAction;
import org.klesun_model.*;
import org.shmidusic.sheet_music.staff.MidianaComponent;
import org.shmidusic.sheet_music.staff.StaffComponent;
import org.shmidusic.stuff.OverridingDefaultClasses.TruMap;
Expand Down Expand Up @@ -32,11 +30,14 @@ public StaffConfig getModel() {
public MidianaComponent getFocusedChild() { return null; }

@Override
protected AbstractHandler makeHandler() {
return new AbstractHandler(this) {
protected IKeyHandler makeHandler()
{
IComponent self = this;
return new IKeyHandler() {
public LinkedHashMap<Combo, ContextAction> getMyClassActionMap() {
return new TruMap<>();
}
public IComponent getContext() { return self; }
};
}

Expand Down

0 comments on commit 0d47b47

Please sign in to comment.