Skip to content

Commit

Permalink
Reorganization of the value editors (addition of EditorToolbar)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexisDrogoul committed Jul 18, 2021
1 parent 6e8ece3 commit fdf0773
Show file tree
Hide file tree
Showing 12 changed files with 219 additions and 219 deletions.
Expand Up @@ -25,10 +25,6 @@
@SuppressWarnings ({ "rawtypes" })
public interface IParameterEditor<T> extends IScoped {

enum item {
PLUS, MINUS, EDIT, INSPECT, BROWSE, CHANGE, REVERT, DEFINE, VALUE;
}

IType getExpectedType();

boolean isValueModified();
Expand Down
Expand Up @@ -12,7 +12,6 @@
package ummisco.gama.ui.parameters;

import static msi.gama.common.util.StringUtils.toGaml;
import static ummisco.gama.ui.resources.GamaIcons.create;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -26,14 +25,11 @@
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;

import com.google.common.primitives.Ints;

Expand All @@ -51,55 +47,12 @@
import msi.gaml.variables.Variable;
import ummisco.gama.ui.interfaces.EditorListener;
import ummisco.gama.ui.interfaces.IParameterEditor;
import ummisco.gama.ui.resources.GamaIcons;
import ummisco.gama.ui.resources.IGamaColors;
import ummisco.gama.ui.resources.IGamaIcons;
import ummisco.gama.ui.utils.WorkbenchHelper;

public abstract class AbstractEditor<T>
implements SelectionListener, ModifyListener, Comparable<AbstractEditor<T>>, IParameterEditor<T> {

private class ItemSelectionListener extends SelectionAdapter {

private final int code;

ItemSelectionListener(final int code) {
this.code = code;
}

@Override
public void widgetSelected(final SelectionEvent e) {
switch (code) {
case REVERT:
modifyAndDisplayValue(applyRevert());
break;
case PLUS:
modifyAndDisplayValue(applyPlus());
break;
case MINUS:
modifyAndDisplayValue(applyMinus());
break;
case EDIT:
applyEdit();
break;
case INSPECT:
applyInspect();
break;
case BROWSE:
applyBrowse();
break;
case CHANGE:
if (e.detail != SWT.ARROW) return;
applyChange();
break;
case DEFINE:
applyDefine();
break;
}
}

}

private static int ORDER;
private final int order = ORDER++;
private final EditorListener<T> listener;
Expand All @@ -113,15 +66,18 @@ public void widgetSelected(final SelectionEvent e) {
protected T originalValue, currentValue, minValue, maxValue, stepValue;

// Properties
protected boolean isCombo = false, isEditable = true, isSubParameter = false, noScope = false, acceptNull = true;
protected boolean noScope = false, acceptNull = true;
protected final boolean isCombo;
protected /* almost final */ boolean isSubParameter;
protected final boolean isEditable;
protected volatile boolean internalModification;

// UI Components
protected Combo combo;
protected EditorLabel titleLabel;
private CLabel fixedValue;
protected Composite composite, parent, toolbar;
protected final ToolItem[] items = new ToolItem[9];
protected Composite composite, parent;
final EditorToolbar toolbar;

public AbstractEditor(final IScope scope, final IParameter variable) {
this(scope, null, variable, null);
Expand Down Expand Up @@ -164,7 +120,7 @@ public AbstractEditor(final IScope scope, final IAgent a, final IParameter varia
name = "";
}
listener = l;

toolbar = new EditorToolbar(this);
}

@Override
Expand All @@ -183,15 +139,8 @@ public void setActive(final Boolean active) {
titleLabel.setInactive();
}
}
if (!active) {
for (final ToolItem t : items) {
if (t == null) { continue; }
t.setEnabled(false);
}
} else {
checkButtons();
}

toolbar.setActive(active);
if (active) { updateToolbar(); }
this.getEditor().setEnabled(active);
}

Expand Down Expand Up @@ -264,29 +213,25 @@ protected Control createEditorControl(final Composite comp) {
public void createComposite(final Composite comp) {
// Necessary to force SWT to "reskin" and give the right background to the composite (issue in the CSS engine)
comp.computeSize(SWT.DEFAULT, SWT.DEFAULT);
this.parent = comp;
internalModification = true;
titleLabel = new EditorLabel(comp, name, computeLabelTooltip(), isSubParameter);
try {
setOriginalValue(getParameterValue());
currentValue = getOriginalValue();
} catch (final GamaRuntimeException e1) {
e1.addContext("Impossible to obtain the value of " + name);
GAMA.reportError(GAMA.getRuntimeScope(), e1, false);
}
currentValue = getOriginalValue();
parent = comp;
internalModification = true;
titleLabel = new EditorLabel(comp, name, computeLabelTooltip(), isSubParameter);
composite = new Composite(comp, SWT.NONE);
composite.setBackground(getNormalBackground());
final var data = new GridData(SWT.FILL, SWT.CENTER, true, false);
// data.minimumWidth = 150;
composite.setLayoutData(data);

final var layout = new GridLayout(2, false);
layout.marginWidth = 5;

composite.setLayout(layout);
createEditorControl(composite);
toolbar = createToolbar2();

toolbar.build(composite);
if (isEditable && !isCombo) { displayParameterValueAndCheckButtons(); }
internalModification = false;
comp.layout();
Expand Down Expand Up @@ -329,74 +274,6 @@ protected String typeToDisplay() {
return param.getType().serialize(false);
}

protected Composite createToolbar2() {
final var t = new ToolBar(composite, SWT.NONE);
final var d = new GridData(SWT.END, SWT.FILL, false, false);
t.setLayoutData(d);
if (isEditable) {
final var codes = this.getToolItems();
for (final int i : codes) {
ToolItem item = null;
switch (i) {
case REVERT:
item = createItem(t, "Revert to original value", create("small.revert").image());
break;
case PLUS:
item = createPlusItem(t);
break;
case MINUS:
item = createMinusItem(t);
break;
case EDIT:
item = createItem(t, "Edit the parameter", create("small.edit").image());
break;
case INSPECT:
item = createItem(t, "Inspect the agent", create("small.inspect").image());
break;
case BROWSE:
item = createItem(t, "Browse the list of agents", create("small.browse").image());
break;
case CHANGE:
item = createItem(t, "Choose another agent", create("small.change").image());
break;
case DEFINE:
item = createItem(t, "Set the parameter to undefined", create("small.undefine").image());
break;
case VALUE:
item = createItem(t, "Value of the parameter", null);
}
if (item != null) {
items[i] = item;
item.addSelectionListener(new ItemSelectionListener(i));

}
}
}
return t;

}

protected ToolItem createPlusItem(final ToolBar t) {
return createItem(t, "Increment " + (stepValue == null ? "" : " by " + stepValue),
GamaIcons.create(IGamaIcons.SMALL_PLUS).image());
}

protected ToolItem createMinusItem(final ToolBar t) {
return createItem(t, "Decrement " + (stepValue == null ? "" : " by " + stepValue),
GamaIcons.create(IGamaIcons.SMALL_MINUS).image());
}

/**
* @param string
* @param image
*/
private ToolItem createItem(final ToolBar t, final String string, final Image image) {
final var i = new ToolItem(t, SWT.FLAT | SWT.PUSH);
i.setToolTipText(string);
i.setImage(image);
return i;
}

@SuppressWarnings ("unchecked")
protected T getParameterValue() throws GamaRuntimeException {
Object result;
Expand Down Expand Up @@ -484,12 +361,6 @@ public void widgetSelected(final SelectionEvent me) {

protected abstract void displayParameterValue();

protected void checkButtons() {
final var revert = items[REVERT];
if (revert == null || revert.isDisposed()) return;
revert.setEnabled(isValueDifferent(originalValue));
}

@Override
public boolean isValueModified() {
return isValueDifferent(getOriginalValue());
Expand Down Expand Up @@ -561,7 +432,7 @@ public void forceUpdateValueAsynchronously() {
combo.select(possibleValues.indexOf(newVal));
} else {
displayParameterValue();
checkButtons();
updateToolbar();
}
composite.update();
internalModification = false;
Expand All @@ -573,7 +444,7 @@ public void forceUpdateValueAsynchronously() {
private void displayParameterValueAndCheckButtons() {
WorkbenchHelper.run(() -> {
displayParameterValue();
checkButtons();
updateToolbar();
});

}
Expand All @@ -595,12 +466,16 @@ protected final void modifyAndDisplayValue(final T val) {
} else {
WorkbenchHelper.asyncRun(() -> {
displayParameterValue();
checkButtons();
updateToolbar();
});
}

}

protected void updateToolbar() {
toolbar.update();
}

protected IAgent getAgent() {
if (agent != null) return agent;
if (scope == null) return null;
Expand Down
@@ -1,13 +1,12 @@
/*********************************************************************************************
*
* 'AgentEditor.java, in plugin ummisco.gama.ui.shared, is part of the source code of the
* GAMA modeling and simulation platform.
* (v. 1.8.1)
*
* 'AgentEditor.java, in plugin ummisco.gama.ui.shared, is part of the source code of the GAMA modeling and simulation
* platform. (v. 1.8.1)
*
* (c) 2007-2020 UMI 209 UMMISCO IRD/UPMC & Partners
*
* Visit https://github.com/gama-platform/gama for license information and developers contact.
*
*
*
**********************************************************************************************/
package ummisco.gama.ui.parameters;
Expand All @@ -18,6 +17,7 @@
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;

import msi.gama.kernel.experiment.IParameter;
import msi.gama.metamodel.agent.IAgent;
Expand All @@ -30,7 +30,7 @@
import ummisco.gama.ui.resources.IGamaIcons;
import ummisco.gama.ui.utils.WorkbenchHelper;

@SuppressWarnings({ "unchecked", "rawtypes" })
@SuppressWarnings ({ "unchecked", "rawtypes" })
public class AgentEditor extends ExpressionBasedEditor {

// Label agentDisplayer;
Expand Down Expand Up @@ -67,11 +67,10 @@ public class AgentEditor extends ExpressionBasedEditor {

@Override
public void applyChange() {
final Menu old = items[CHANGE].getParent().getShell().getMenu();
items[CHANGE].getParent().getShell().setMenu(null);
if (old != null) {
old.dispose();
}
Shell shell = toolbar.getItem(CHANGE).getParent().getShell();
final Menu old = shell.getMenu();
shell.setMenu(null);
if (old != null) { old.dispose(); }
// FIXME Not adapted to multiple scales !

final MenuAction action = new MenuAction(new SelectionAdapter() {
Expand All @@ -80,23 +79,22 @@ public void applyChange() {
public void widgetSelected(final SelectionEvent e) {
final MenuItem mi = (MenuItem) e.widget;
final IAgent a = (IAgent) mi.getData("agent");
if (a != null && !a.dead()) {
modifyAndDisplayValue(a);
}
if (a != null && !a.dead()) { modifyAndDisplayValue(a); }
}

}, GamaIcons.create(IGamaIcons.MENU_AGENT).image(), "Choose");

final Menu dropMenu = new Menu(items[CHANGE].getParent().getShell());
final Menu dropMenu = new Menu(shell);
final IAgent a = (IAgent) (currentValue instanceof IAgent ? currentValue : null);
if (a != null) {
final IAgentMenuFactory factory = WorkbenchHelper.getService(IAgentMenuFactory.class);
if (factory != null)
if (factory != null) {
factory.fillPopulationSubMenu(dropMenu, a.getScope().getSimulation().getMicroPopulation(species), null,
action);
}
}
final Rectangle rect = items[CHANGE].getBounds();
final Point pt = items[CHANGE].getParent().toDisplay(new Point(rect.x, rect.y));
final Rectangle rect = toolbar.getItem(CHANGE).getBounds();
final Point pt = toolbar.getItem(CHANGE).getParent().toDisplay(new Point(rect.x, rect.y));
dropMenu.setLocation(pt.x, pt.y + rect.height);
dropMenu.setVisible(true);

Expand All @@ -122,7 +120,7 @@ public IType getExpectedType() {

/**
* Method getToolItems()
*
*
* @see ummisco.gama.ui.parameters.AbstractEditor#getToolItems()
*/
@Override
Expand All @@ -135,9 +133,7 @@ protected void applyInspect() {

if (currentValue instanceof IAgent) {
final IAgent a = (IAgent) currentValue;
if (!a.dead()) {
getScope().getGui().setSelectedAgent(a);
}
if (!a.dead()) { getScope().getGui().setSelectedAgent(a); }
}

}
Expand Down

0 comments on commit fdf0773

Please sign in to comment.