Skip to content

Commit

Permalink
not sure i did a good thing - now each model implicitly must declare …
Browse files Browse the repository at this point in the history
…constructor that accepts JSONObject, did it, so it would be clear, that reconstructFromJson() can be called only once - and only upon instance instantiation (cuz it may have some final fields); also added a test demonstrating how to draw gui with html and communicate with javascript
  • Loading branch information
klesun authored and klesun committed Jan 12, 2016
1 parent 46a4116 commit c01f8b3
Show file tree
Hide file tree
Showing 29 changed files with 9,562 additions and 562 deletions.
4 changes: 3 additions & 1 deletion src/org/klesun_model/AbstractModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,20 @@
// AbstractModel is used to store field list, since an interface can't have properties...


import org.json.JSONObject;
import org.klesun_model.field.Arr;
import org.klesun_model.field.Field;
import org.klesun_model.field.IField;

import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.TreeMap;
import java.util.function.Function;

public abstract class AbstractModel implements IModel
{
private Map<String, IField> fieldStorage = new TreeMap<>();
private Map<String, IField> fieldStorage = new LinkedHashMap<>();

@Override
public String toString() {
Expand Down
6 changes: 6 additions & 0 deletions src/org/klesun_model/IModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ public interface IModel
{
Map<String, IField> getFieldStorage();

// please, define the constructor in every deriving class
// we rely on this when reconstructing model structure
// default IModel(JSONObject state) {
// reconstructFromJson(state);
// }

default JSONObject getJsonRepresentation()
{
JSONObject dict = new JSONObject();
Expand Down
21 changes: 6 additions & 15 deletions src/org/klesun_model/field/Arr.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.klesun_model.field;

import org.json.JSONObject;
import org.klesun_model.IModel;
import org.shmidusic.stuff.tools.Logger;
import org.json.JSONArray;
Expand All @@ -19,9 +20,6 @@ public class Arr<ELEM_CLASS extends IModel> implements IField, Iterable<ELEM_CLA
Class<ELEM_CLASS> elemClass;

final Collection<ELEM_CLASS> elements;

private Boolean omitDefaultFromJson = false;

public Arr(Collection<ELEM_CLASS> value, Class<ELEM_CLASS> elemClass)
{
elements = value;
Expand All @@ -32,7 +30,7 @@ public Arr(Collection<ELEM_CLASS> value, Class<ELEM_CLASS> elemClass)
public JSONArray getJsonValue() {
JSONArray arr = new JSONArray("[]");
for (IModel el: elements) {
if (el.getJsonRepresentation().keySet().size() != 0 || !omitDefaultFromJson()) {
if (el.mustBeStored()) {
arr.put(el.getJsonRepresentation());
}
}
Expand All @@ -46,12 +44,14 @@ public void setJsonValue(Object jsonValue) {
for (int i = 0; i < arr.length(); ++i) {
ELEM_CLASS el = null;
try {
el = elemClass.newInstance();
el = elemClass.getDeclaredConstructor(JSONObject.class).newInstance(arr.getJSONObject(i));
} catch (Exception e) {
Logger.fatal(e, "Come on, every class has an empty constructor in java! {" + elemClass.getSimpleName() + "}");
}

el.reconstructFromJson(arr.getJSONObject(i)); // it's important to do reconstructFromJson before add, cuz the Collection may be a set
// will throw an exception in case final field was not set during initialisation - just as planned
el.getFieldStorage().values().stream().filter(IField::isFinal).forEach(IField::getJsonValue);

elements.add(el);
}
}
Expand Down Expand Up @@ -89,15 +89,6 @@ public void setFromList(Collection<ELEM_CLASS> list)
list.forEach(elements::add);
}

public Arr<ELEM_CLASS> setOmitDefaultFromJson(Boolean value) {
this.omitDefaultFromJson = value;
return this;
}

public Boolean omitDefaultFromJson() {
return this.omitDefaultFromJson;
}

public Boolean mustBeStored() {
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion src/org/shmidusic/MajesticWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ private static List<IComponent> makeFakePossibleChildListForClassMethods(ICompon
} else if (parent.getClass() == StaffComponent.class) {
return Arrays.asList(new ChordComponent(new Chord(), parent));
} else if (parent.getClass() == ChordComponent.class) {
return Arrays.asList(new NoteComponent(new Note(), (ChordComponent)parent));
return Arrays.asList(new NoteComponent(new Note(0,0), (ChordComponent)parent));
} else {
return new ArrayList<>();
}
Expand Down
3 changes: 2 additions & 1 deletion src/org/shmidusic/sheet_music/SheetMusic.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ public class SheetMusic extends AbstractModel
{
public Arr<Staff> staffList = add("staffList", new ArrayList<>(), Staff.class);

/** use this constructor when restoring object from json */
public SheetMusic(JSONObject state) {
this();
reconstructFromJson(state);
}

/** use this constructor when creating new object */
public SheetMusic()
{
Staff staff = new Staff();
Expand Down
23 changes: 9 additions & 14 deletions src/org/shmidusic/sheet_music/staff/Staff.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,25 @@ public class Staff extends AbstractModel
public enum aMode { insert, passive }
public static aMode mode = aMode.insert;

public StaffConfig staffConfig = null;
final public StaffConfig staffConfig;

public Arr<Chord> chordList = add("chordList", new ArrayList<>(), Chord.class);
private List<Tact> tactList = new ArrayList<>();
public int focusedIndex = -1;

/** use this constructor when creating new object */
public Staff()
{
this.staffConfig = new StaffConfig();
}

/** use this constructor when restoring object from json */
public Staff(JSONObject state) {
reconstructFromJson(state);
this.staffConfig = new StaffConfig(state.getJSONObject("staffConfig")); // TODO: MODELIZE
accordListChanged();
}

public Chord addNewAccord()
{
return addNewAccord(chordList.size());
Expand Down Expand Up @@ -177,19 +185,6 @@ public Optional<Chord> getChord(int index) {
: Optional.empty();
}
}

@Override
public Staff reconstructFromJson(JSONObject jsObject) throws JSONException
{
super.reconstructFromJson(jsObject);

JSONObject configJson = jsObject.getJSONObject("staffConfig");
this.getConfig().reconstructFromJson(configJson);

accordListChanged();

return this;
}

// getters

Expand Down
2 changes: 1 addition & 1 deletion src/org/shmidusic/sheet_music/staff/StaffHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ private static Explain<List<Chord>> constructChordList(String chordListJs)
List<Chord> result = new ArrayList<>();
try {
for (int i = 0; i < jsArr.length(); ++i) {
result.add((Chord)new Chord().reconstructFromJson(jsArr.getJSONObject(i)));
result.add(new Chord(jsArr.getJSONObject(i)));
}
return new Explain<>(result);
} catch (JSONException exc) {
Expand Down
9 changes: 9 additions & 0 deletions src/org/shmidusic/sheet_music/staff/chord/Chord.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.json.JSONObject;
import org.klesun_model.AbstractModel;
import org.klesun_model.field.Arr;
import org.klesun_model.field.Field;
Expand All @@ -19,6 +20,14 @@ public class Chord extends AbstractModel
public Field<String> slog = add("slog", "").setOmitDefaultFromJson(true);
public Arr<Note> noteList = add("noteList", new TreeSet<>(), Note.class);

/** use this constructor when creating new object */
public Chord() { }

/** use this constructor when restoring object from json */
public Chord(JSONObject state) {
reconstructFromJson(state);
}

// getters/setters
public Stream<Note> noteStream(Predicate<Note> filterLambda) {
return noteList.stream().filter(filterLambda);
Expand Down
6 changes: 5 additions & 1 deletion src/org/shmidusic/sheet_music/staff/chord/note/Note.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,12 @@ public class Note extends AbstractModel implements INote

// </editor-fold>

public Note() {}
/** use this constructor when restoring object from json */
public Note(JSONObject state) {
reconstructFromJson(state);
}

/** use this constructor when creating new object */
public Note(int tuneValue, int channelValue) {
tune.set(tuneValue);
channel.set(channelValue);
Expand Down
13 changes: 8 additions & 5 deletions src/org/shmidusic/sheet_music/staff/chord/note/NoteHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,14 @@ synchronized private static void changeChannel(NoteComponent note, int channel)
}
}

public static void play(NoteComponent comp) {
DeviceEbun.openNote(comp.note);
int tempo = comp.getParentComponent().getParentComponent().staff.getConfig().getTempo();
int millis = comp.note.getTimeMilliseconds(tempo);
public static void play(NoteComponent comp)
{
if (!comp.note.isPause()) {
DeviceEbun.openNote(comp.note);
int tempo = comp.getParentComponent().getParentComponent().staff.getConfig().getTempo();
int millis = comp.note.getTimeMilliseconds(tempo);

Fp.setTimeout(() -> DeviceEbun.closeNote(comp.note), millis);
Fp.setTimeout(() -> DeviceEbun.closeNote(comp.note), millis);
}
}
}
26 changes: 13 additions & 13 deletions src/org/shmidusic/sheet_music/staff/staff_config/Channel.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ public class Channel extends AbstractModel implements Comparable<Channel> {

final public static int CHANNEL_COUNT = 16; // 1-15. 0th ignores volume change; 16th throws MidiDataChannelOutOfRangeBlaBla exception

// TODO: 0-th channel does not exist - do something with that
// TODO: 0-th channel does not exist sometimes - do something with that
final public Field<Integer> channelNumber = add("channelNumber", Integer.class);
final private Field<Integer> instrument = add("instrument", 0, i -> limit(i, 0, 127)).setOmitDefaultFromJson(true);
final private Field<Integer> volume = add("volume", 50, v -> limit(v, 0, 127)).setOmitDefaultFromJson(true);
final private Field<Boolean> isMuted = add("isMuted", false);
final public Field<Integer> instrument = add("instrument", 0, i -> limit(i, 0, 127)).setOmitDefaultFromJson(true);
final public Field<Integer> volume = add("volume", 50, v -> limit(v, 0, 127)).setOmitDefaultFromJson(true);
final public Field<Integer> modulation = add("modulation", 0, v -> limit(v, 0, 127)).setOmitDefaultFromJson(true);
final public Field<Boolean> isMuted = add("isMuted", false);

public Channel setInstrument(int value) { instrument.set(value); return this; }
public Channel setVolume(int value) { volume.set(value); return this; }
Expand All @@ -23,15 +24,14 @@ public class Channel extends AbstractModel implements Comparable<Channel> {
public Integer getVolume() { return volume.get(); }
public Boolean getIsMuted() { return isMuted.get(); }

@Override
public JSONObject getJsonRepresentation() {
JSONObject result = super.getJsonRepresentation();
if (result.keySet().size() == 1 && result.has("channelNumber")) {
// no useful information
return new JSONObject();
} else {
return result;
}
/** use this constructor when creating new object */
public Channel(int channelNumber) {
this.channelNumber.set(channelNumber);
}

/** use this constructor when restoring object from json */
public Channel(JSONObject state) {
reconstructFromJson(state);
}

@Override
Expand Down
Loading

0 comments on commit c01f8b3

Please sign in to comment.