Skip to content

Commit

Permalink
i dont like long methods
Browse files Browse the repository at this point in the history
  • Loading branch information
klesun authored and klesun committed Jan 10, 2016
1 parent 0d47b47 commit ae1b0c4
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 41 deletions.
17 changes: 9 additions & 8 deletions src/org/klesun_model/IKeyHandler.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
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
// 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

Expand All @@ -16,8 +17,8 @@

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
int ctrl = KeyEvent.CTRL_MASK;
KeyEvent k = new KeyEvent(new JPanel(),0,0,0,0,'h'); // just for constants

default Explain handleKey(Combo combo)
{
Expand All @@ -37,7 +38,7 @@ default Explain handleKey(Combo combo)
}

// we use this to (de)generate [File,Menu,Edit]-like menu that would
// (with few decorative exclusions) completely correspond whole model key mapping.
// (with few esthetic 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();
Expand Down
1 change: 1 addition & 0 deletions src/org/shmidusic/MainPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public MainPanel() {
northPanel.add(statusField, BorderLayout.EAST);
}

/** @TODO: muah. collect all this copypaste somewhere please */
public void replaceSheetMusic(SheetMusic sheetMusic)
{
this.sheetContainer = new SheetMusicComponent(sheetMusic, this);
Expand Down
67 changes: 35 additions & 32 deletions src/org/shmidusic/MajesticWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ public enum cardEnum {
public MainPanel shmidusicPanel;
public JTextArea terminal;

public MajesticWindow() {
public MajesticWindow()
{
super("Да будет такая музыка!");
this.setIconImage(ImageStorage.openImageUncached("midusic.png"));
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
Expand Down Expand Up @@ -80,51 +81,53 @@ private void addMenuBar() {
private void addMenuItems(IComponent fakeModelForClassMethods)
{
LinkedHashMap<Combo, ContextAction> actionMap = fakeModelForClassMethods.getHandler().getMyClassActionMap();

Class<? extends IComponent> contextClass = fakeModelForClassMethods.getClass();

if (actionMap.values().stream().anyMatch(a -> !a.omitMenuBar())) {

JMenu modelMenu = new JMenu(fakeModelForClassMethods.getModel().getClass().getSimpleName());
modelMenu.setToolTipText("Enabled");

for (Combo key : actionMap.keySet()) {
ContextAction action = actionMap.get(key);
actionMap.entrySet().stream()
.filter(e -> !e.getValue().omitMenuBar())
.map(e -> makeActionMenuItem(e.getKey(), e.getValue(), contextClass))
.forEach(modelMenu::add);

if (action.omitMenuBar()) {
continue;
}

String caption = action.getCaption() != null ? action.getCaption() : "Do Action:";
JMenuItem eMenuItem = new TruMenuItem(caption);
eMenuItem.setToolTipText("No description");
eMenuItem.setAccelerator(key.toKeystroke());

eMenuItem.addActionListener(event -> {
Class<? extends IComponent> cls = fakeModelForClassMethods.getClass();
IComponent context = findeFocusedByClass(cls);
if (context != null) {
Explain explain = action.redo(context);
if (explain.isSuccess()) {
updateMenuBar();
} else {
JOptionPane.showMessageDialog(this, explain.getExplanation());
}
} else {
JOptionPane.showMessageDialog(this, "Cant perform action, " + cls.getSimpleName() + " class instance not focused!");
}
});

modelMenu.add(eMenuItem);
}
menuBar.add(modelMenu);
menus.put(fakeModelForClassMethods.getClass(), modelMenu);
menus.put(contextClass, modelMenu);
}

for (IComponent child: makeFakePossibleChildListForClassMethods(fakeModelForClassMethods)) {
addMenuItems(child);
}
}

private JMenuItem makeActionMenuItem(Combo key, ContextAction action, Class<? extends IComponent> contextClass)
{
String caption = action.getCaption() != null ? action.getCaption() : "Do Action:";
JMenuItem eMenuItem = new TruMenuItem(caption);
eMenuItem.setToolTipText("No description");
eMenuItem.setAccelerator(key.toKeystroke());

eMenuItem.addActionListener(event ->
{
IComponent context = findFocusedByClass(contextClass);
if (context != null) {
Explain explain = action.redo(context);
if (explain.isSuccess()) {
updateMenuBar();
} else {
JOptionPane.showMessageDialog(this, explain.getExplanation());
}
} else {
JOptionPane.showMessageDialog(this, "Cant perform action, " + contextClass.getSimpleName() + " class instance not focused!");
}
});

return eMenuItem;
}

// retarded language does not support overridable class methods
private static List<IComponent> makeFakePossibleChildListForClassMethods(IComponent parent) {
if (parent.getClass() == SheetMusicComponent.class) {
return Arrays.asList(new StaffComponent(new Staff(), (SheetMusicComponent)parent));
Expand All @@ -137,7 +140,7 @@ private static List<IComponent> makeFakePossibleChildListForClassMethods(ICompon
}
}

private IComponent findeFocusedByClass(Class<? extends IComponent> cls)
private IComponent findFocusedByClass(Class<? extends IComponent> cls)
{
IComponent result = shmidusicPanel.sheetContainer;
while (result != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

import javax.swing.*;

public class TruMenuItem extends JMenuItem {
// sorry for that abomination, but it kinda was best solution
// i found on Stack Overflow how to include key combination caption
// ... or was it to prevent action on the key press since we handle it ourself...

public class TruMenuItem extends JMenuItem
{
public TruMenuItem(String caption) {
super(caption);
}
Expand Down

0 comments on commit ae1b0c4

Please sign in to comment.