Skip to content

Commit

Permalink
feat: add option to set initial mode
Browse files Browse the repository at this point in the history
  • Loading branch information
targos committed Jul 24, 2024
1 parent 61f17d5 commit c2a515c
Show file tree
Hide file tree
Showing 19 changed files with 404 additions and 351 deletions.
50 changes: 34 additions & 16 deletions examples/generic_editor/demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,39 @@
<script type="module" src="demo/demo.ts"></script>
</head>
<body>
<div class="flex flex-row gap-4 mb-4">
<button id="loadMolecule">Load molecule</button>
<button id="loadFragment">Load fragment</button>
<button id="loadReaction">Load reaction</button>
</div>
<div class="bg-green-300 p-3">
<div id="editor" class="w-[800px] h-[600px]"></div>
</div>
<div class="mt-4 grid grid-cols-2 gap-4">
<div>Change count</div>
<div id="changeCount"></div>
<div>ID Code</div>
<div id="idcode"></div>
<div>Molfile</div>
<div id="molfile"></div>
</div>
<h1>Demo - Generic editor</h1>
<section
class="grid gap-2"
style="
grid-template-rows: 30px 600px 30px;
grid-template-columns: 100px 600px;
grid-template-areas:
'. top-actions . '
'left-actions editor result'
'. . result';
"
>
<div class="flex flex-row gap-4" style="grid-area: top-actions">
<button id="loadMolecule">Load molecule</button>
<button id="loadFragment">Load fragment</button>
<button id="loadReaction">Load reaction</button>
</div>
<div
id="editor"
class="border border-blue-300"
style="grid-area: editor"
></div>
<div
class="grid auto-rows-min gap-2"
style="grid-area: result; grid-template-columns: fit-content(100%) 1fr"
>
<div>Change count</div>
<div id="changeCount"></div>
<div>ID Code</div>
<div id="idcode"></div>
<div>Molfile</div>
<div id="molfile"></div>
</div>
</section>
</body>
</html>
4 changes: 3 additions & 1 deletion examples/generic_editor/demo/demo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,9 @@ const molfileDiv = document.getElementById('molfile') as HTMLElement;
let changeCount = 0;

const editorElement = document.getElementById('editor') as HTMLElement;
const editor = new CanvasEditor(editorElement);
const editor = new CanvasEditor(editorElement, {
initialMode: 'molecule',
});

editor.setOnChangeListener(({ type, isUserEvent }) => {
if (isUserEvent && type === 'molecule') {
Expand Down
16 changes: 15 additions & 1 deletion lib/canvas_editor/create_editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function createEditor(
JavaEditorToolbar,
JavaUIHelper,
) {
const { readOnly = false } = options;
const { readOnly = false, initialMode = 'molecule' } = options;

const rootElement = document.createElement('div');
Object.assign(rootElement.style, {
Expand Down Expand Up @@ -56,6 +56,7 @@ function createEditor(
const uiHelper = new JavaUIHelper(new UIHelper(editorCanvas, JavaEditorArea));

const editorArea = new JavaEditorArea(
computeMode(initialMode, JavaEditorArea),
new EditorArea(editorCanvas, onChange),
uiHelper,
);
Expand Down Expand Up @@ -110,4 +111,17 @@ function createEditor(
return { editorArea, toolbar, uiHelper, destroy };
}

function computeMode(initialMode, JavaEditorArea) {
switch (initialMode) {
case 'molecule':
return 0;
case 'reaction':
return (
JavaEditorArea.MODE_REACTION | JavaEditorArea.MODE_MULTIPLE_FRAGMENTS
);
default:
throw new Error(`Invalid initial mode: ${initialMode}`);
}
}

module.exports = createEditor;
6 changes: 3 additions & 3 deletions src/com/actelion/research/gwt/gui/generic/JSCheckBox.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.actelion.research.gwt.gui.generic;

import com.actelion.research.gui.generic.*;
import com.actelion.research.gui.generic.GenericCheckBox;
import com.google.gwt.core.client.JavaScriptObject;

public class JSCheckBox extends JSComponent implements GenericCheckBox {
Expand All @@ -9,14 +9,14 @@ public JSCheckBox(JavaScriptObject jsCheckBox) {
}

@Override
public native boolean isSelected()
public native boolean isSelected()
/*-{
var component = [email protected]::getJsComponent()();
return component.isSelected();
}-*/;

@Override
public native void setSelected(boolean b)
public native void setSelected(boolean b)
/*-{
var component = [email protected]::getJsComponent()();
return component.setSelected(b);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package com.actelion.research.gwt.gui.generic;

import java.util.ArrayList;

import com.actelion.research.chem.MolfileParser;
import com.actelion.research.chem.StereoMolecule;
import com.actelion.research.chem.reaction.Reaction;
import com.actelion.research.gui.clipboard.IClipboardHandler;
import com.google.gwt.core.client.JavaScriptObject;

import java.util.ArrayList;

public class JSClipboardHandler implements IClipboardHandler {
private JavaScriptObject mJsHandler;
private JavaScriptObject mJsHandler;

public JSClipboardHandler(JavaScriptObject jsHandler) {
mJsHandler = jsHandler;
Expand Down Expand Up @@ -51,11 +51,11 @@ public Reaction pasteReaction() {
public boolean copyMolecule(String molfile) {
Object test = null;
System.out.println("test: " + test.toString());
StereoMolecule m = new StereoMolecule();
MolfileParser p = new MolfileParser();
p.parse(m, molfile);
StereoMolecule m = new StereoMolecule();
MolfileParser p = new MolfileParser();
p.parse(m, molfile);
return copyMolecule(m);
}
}

public native boolean copyMolecule(StereoMolecule mol)
/*-{
Expand Down
16 changes: 8 additions & 8 deletions src/com/actelion/research/gwt/gui/generic/JSComboBox.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.actelion.research.gwt.gui.generic;

import com.actelion.research.gui.generic.*;
import com.actelion.research.gui.generic.GenericComboBox;
import com.google.gwt.core.client.JavaScriptObject;

public class JSComboBox extends JSComponent implements GenericComboBox {
Expand All @@ -9,49 +9,49 @@ public JSComboBox(JavaScriptObject jsComboBox) {
}

@Override
public native void addItem(String item)
public native void addItem(String item)
/*-{
var component = [email protected]::getJsComponent()();
return component.addItem(item);
}-*/;

@Override
public native void removeAllItems()
public native void removeAllItems()
/*-{
var component = [email protected]::getJsComponent()();
return component.removeAllItems();
}-*/;

@Override
public native int getSelectedIndex()
public native int getSelectedIndex()
/*-{
var component = [email protected]::getJsComponent()();
return component.getSelectedIndex();
}-*/;

@Override
public native String getSelectedItem()
public native String getSelectedItem()
/*-{
var component = [email protected]::getJsComponent()();
return component.getSelectedItem();
}-*/;

@Override
public native void setSelectedIndex(int index)
public native void setSelectedIndex(int index)
/*-{
var component = [email protected]::getJsComponent()();
return component.setSelectedIndex(index);
}-*/;

@Override
public native void setSelectedItem(String item)
public native void setSelectedItem(String item)
/*-{
var component = [email protected]::getJsComponent()();
return component.setSelectedItem(item);
}-*/;

@Override
public native void setEditable(boolean b)
public native void setEditable(boolean b)
/*-{
var component = [email protected]::getJsComponent()();
return component.setEditable(b);
Expand Down
24 changes: 13 additions & 11 deletions src/com/actelion/research/gwt/gui/generic/JSComponent.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
package com.actelion.research.gwt.gui.generic;

import com.actelion.research.gui.generic.*;
import com.actelion.research.gui.generic.GenericActionEvent;
import com.actelion.research.gui.generic.GenericComponent;
import com.actelion.research.gui.generic.GenericEventListener;
import com.google.gwt.core.client.JavaScriptObject;

import java.util.ArrayList;

public class JSComponent implements GenericComponent {
private JavaScriptObject mJsComponent;
private ArrayList<GenericEventListener<GenericActionEvent>> mConsumerList;
private ArrayList<GenericEventListener<GenericActionEvent>> mConsumerList;

public JSComponent(JavaScriptObject jsComponent) {
mJsComponent = jsComponent;
mConsumerList = new ArrayList<>();
mConsumerList = new ArrayList<>();
setEventHandler(jsComponent);
}

Expand All @@ -32,26 +34,26 @@ public JavaScriptObject getJsComponent() {
}

@Override
public native void setEnabled(boolean b)
public native void setEnabled(boolean b)
/*-{
var component = [email protected]::getJsComponent()();
return component.setEnabled(b);
}-*/;

@Override
public void addEventConsumer(GenericEventListener<GenericActionEvent> consumer) {
mConsumerList.add(consumer);
public void addEventConsumer(GenericEventListener<GenericActionEvent> consumer) {
mConsumerList.add(consumer);
}

@Override
public void removeEventConsumer(GenericEventListener<GenericActionEvent> consumer) {
mConsumerList.remove(consumer);
public void removeEventConsumer(GenericEventListener<GenericActionEvent> consumer) {
mConsumerList.remove(consumer);
}

@Override
public void fireEvent(GenericActionEvent event) {
for (GenericEventListener<GenericActionEvent> consumer:mConsumerList) {
consumer.eventHappened(event);
public void fireEvent(GenericActionEvent event) {
for (GenericEventListener<GenericActionEvent> consumer:mConsumerList) {
consumer.eventHappened(event);
}
}
}
32 changes: 16 additions & 16 deletions src/com/actelion/research/gwt/gui/generic/JSDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,52 +20,52 @@ private JavaScriptObject getJsDialog() {
@Override
public native void setLayout(int[] hLayout, int[] vLayout)
/*-{
var dialog = [email protected]::getJsDialog()();
return dialog.setLayout(hLayout, vLayout);
var jsDialog = [email protected]::getJsDialog()();
return jsDialog.setLayout(hLayout, vLayout);
}-*/;

@Override
public native void add(GenericComponent c, int x, int y)
/*-{
var dialog = [email protected]::getJsDialog()();
return dialog.add(c, x, y);
var jsDialog = [email protected]::getJsDialog()();
return jsDialog.add(c, x, y);
}-*/;

@Override
public native void add(GenericComponent c, int x1, int y1, int x2, int y2)
/*-{
var dialog = [email protected]::getJsDialog()();
return dialog.add(c, x1, y1, x2, y2);
var jsDialog = [email protected]::getJsDialog()();
return jsDialog.add(c, x1, y1, x2, y2);
}-*/;

@Override
public native GenericCheckBox createCheckBox(String text)
/*-{
var dialog = [email protected]::getJsDialog()();
var jsDialog = [email protected]::getJsDialog()();
var checkBox = dialog.createCheckBox(text);
return @com.actelion.research.gwt.gui.generic.JSCheckBox::new(Lcom/google/gwt/core/client/JavaScriptObject;)(checkBox);
}-*/;

@Override
public native GenericComboBox createComboBox()
/*-{
var dialog = [email protected]::getJsDialog()();
var jsDialog = [email protected]::getJsDialog()();
var comboBox = dialog.createComboBox();
return @com.actelion.research.gwt.gui.generic.JSComboBox::new(Lcom/google/gwt/core/client/JavaScriptObject;)(comboBox);
}-*/;

@Override
public native GenericLabel createLabel(String text)
public native GenericLabel createLabel(String text)
/*-{
var dialog = [email protected]::getJsDialog()();
var jsDialog = [email protected]::getJsDialog()();
var label = dialog.createLabel(text);
return @com.actelion.research.gwt.gui.generic.JSLabel::new(Lcom/google/gwt/core/client/JavaScriptObject;)(label);
}-*/;

@Override
public native GenericTextField createTextField(int width, int height)
public native GenericTextField createTextField(int width, int height)
/*-{
var dialog = [email protected]::getJsDialog()();
var jsDialog = [email protected]::getJsDialog()();
var textField = dialog.createTextField(width, height);
return @com.actelion.research.gwt.gui.generic.JSTextField::new(Lcom/google/gwt/core/client/JavaScriptObject;)(textField);
}-*/;
Expand All @@ -74,7 +74,7 @@ public native GenericTextField createTextField(int width, int height)
public native void setEventConsumer(GenericEventListener<GenericActionEvent> consumer)
/*-{
[email protected]::mConsumer = consumer;
var dialog = [email protected]::getJsDialog()();
var jsDialog = [email protected]::getJsDialog()();
var that = this;
var jsConsumer = {
fireOk: function ok() {
Expand All @@ -90,21 +90,21 @@ public native void setEventConsumer(GenericEventListener<GenericActionEvent> con
@Override
public native void showDialog()
/*-{
var dialog = [email protected]::getJsDialog()();
var jsDialog = [email protected]::getJsDialog()();
dialog.showDialog();
}-*/;

@Override
public native void disposeDialog()
/*-{
var dialog = [email protected]::getJsDialog()();
var jsDialog = [email protected]::getJsDialog()();
dialog.disposeDialog();
}-*/;

@Override
public native void showMessage(String message)
/*-{
var dialog = [email protected]::getJsDialog()();
var jsDialog = [email protected]::getJsDialog()();
dialog.showMessage(message);
}-*/;

Expand Down
Loading

0 comments on commit c2a515c

Please sign in to comment.