Skip to content

Commit

Permalink
Fixes #3189 and the issue of different heights of buttons in editor
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexisDrogoul committed Sep 12, 2021
1 parent 21416fd commit 8b5061d
Show file tree
Hide file tree
Showing 3 changed files with 341 additions and 104 deletions.
Expand Up @@ -10,6 +10,9 @@
********************************************************************************************************/
package msi.gama.lang.gaml.ui.editor;

import static msi.gama.common.interfaces.IKeyword.BATCH;
import static msi.gama.common.interfaces.IKeyword.MEMORIZE;

import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
Expand Down Expand Up @@ -53,6 +56,7 @@
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
Expand Down Expand Up @@ -127,7 +131,6 @@
import ummisco.gama.ui.resources.GamaIcons;
import ummisco.gama.ui.resources.IGamaColors;
import ummisco.gama.ui.resources.IGamaIcons;
import ummisco.gama.ui.utils.PlatformHelper;
import ummisco.gama.ui.utils.WorkbenchHelper;
import ummisco.gama.ui.views.IGamlEditor;
import ummisco.gama.ui.views.toolbar.GamaToolbar2;
Expand All @@ -149,11 +152,24 @@
public class GamlEditor extends XtextEditor implements IGamlBuilderListener, IGamlEditor, IBoxEnabledEditor,
IToolbarDecoratedView /* IToolbarDecoratedView.Sizable, ITooltipDisplayer */ {

/** The images. */
static Map<String, Image> images = new HashMap();

/** The max image height. */
static int maxImageHeight = 0;

/** The button padding. How much space between each experiment button */
static int buttonPadding = 4;
static {
final var store = EditorsUI.getPreferenceStore();
store.setDefault(AbstractDecoratedTextEditorPreferenceConstants.SHOW_RANGE_INDICATOR, false);
store.setDefault("spellingEnabled", false);
store.setValue("spellingEnabled", false);
images.put(IKeyword.BATCH, GamaIcons.create(IGamaIcons.BUTTON_BATCH).image());
images.put(IKeyword.MEMORIZE, GamaIcons.create(IGamaIcons.BUTTON_BACK).image());
images.put("regular", GamaIcons.create(IGamaIcons.BUTTON_GUI).image());
images.put("new", GamaIcons.create("small.plus").image());
for (Image im : images.values()) { maxImageHeight = Math.max(maxImageHeight, im.getBounds().height); }
}

/**
Expand Down Expand Up @@ -499,22 +515,21 @@ protected void handleCursorPositionChanged() {
* @param listener
* the listener
*/
private void enableButton(final int index, final String text, final SelectionListener listener) {
private void enableExperimentButton(final int index, final String text, final SelectionListener listener) {
if (text == null) return;
final var expType = state.types.get(index);
final var image = IKeyword.BATCH.equals(expType) ? GamaIcons.create(IGamaIcons.BUTTON_BATCH).image()
: IKeyword.MEMORIZE.equals(expType) ? GamaIcons.create(IGamaIcons.BUTTON_BACK).image()
: GamaIcons.create(IGamaIcons.BUTTON_GUI).image();

final var type = BATCH.equals(expType) ? "batch" : MEMORIZE.equals(expType) ? "memorize" : "regular";
final var image = images.get(type);
final var t = toolbar.button(IGamaColors.OK, text, image, SWT.LEFT);
final var type =
IKeyword.BATCH.equals(expType) ? "batch" : IKeyword.MEMORIZE.equals(expType) ? "memorize" : "regular";

t.getControl().setToolTipText("Executes the " + type + " experiment " + text);
((FlatButton) t.getControl()).addSelectionListener(listener);
t.setWidth(t.getWidth() + buttonPadding);
final FlatButton b = (FlatButton) t.getControl();
b.setRightPadding(buttonPadding);
b.setImageHeight(maxImageHeight);
b.setToolTipText("Executes the " + type + " experiment " + text);
b.addSelectionListener(listener);
t.setData("index", index);
((FlatButton) t.getControl()).setData("exp", text);
toolbar.sep(4, SWT.LEFT);
b.setData("exp", text);
// toolbar.sep(4, SWT.LEFT);
}

/**
Expand All @@ -530,7 +545,7 @@ private void updateToolbar(final GamlEditorState newState, final boolean forceSt
WorkbenchHelper.runInUI("Editor refresh", 50, m -> {
if (toolbar == null || toolbar.isDisposed()) return;
toolbar.wipe(SWT.LEFT, true);
if (PlatformHelper.isWindows()) { toolbar.sep(4, SWT.LEFT); }
// if (PlatformHelper.isWindows()) { toolbar.sep(4, SWT.LEFT); }

final var c = state.getColor();
var msg = state.getStatus();
Expand All @@ -557,19 +572,19 @@ private void updateToolbar(final GamlEditorState newState, final boolean forceSt
// without the following line, the display of the
// text "msg" is not updated
// correctly (at least for Windows OS)
toolbar.sep(4, SWT.LEFT);
// if (PlatformHelper.isWindows()) { toolbar.sep(4, SWT.LEFT); }
} else {
var i = 0;
if (newState.showExperiments) {
for (final String e : state.abbreviations) {
enableButton(i++, e, listener);
enableExperimentButton(i++, e, listener);

}
}
}
if (newState.showExperiments
&& !GamlFileExtension.isExperiment(getDocument().getAdapter(IFile.class).getName())) {
toolbar.button(IGamaColors.NEUTRAL, "Add experiment", GamaIcons.create("small.plus").image(),
toolbar.button(IGamaColors.NEUTRAL, "Add experiment", images.get("new"),
new CreateExperimentSelectionListener(GamlEditor.this, toolbar.getToolbar(SWT.LEFT)),
SWT.LEFT);
}
Expand All @@ -583,12 +598,6 @@ private void updateToolbar(final GamlEditorState newState, final boolean forceSt

@Override
public void validationEnded(final Iterable<? extends IDescription> newExperiments, final ValidationContext status) {
// final String platformString = getURI().toPlatformString(true);
// final IFile myFile = ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(platformString));
// final WrappedGamaFile file =
// (WrappedGamaFile) NavigatorRoot.getInstance().getManager().findWrappedInstanceOf(myFile);
// NavigatorRoot.getInstance().getManager().refreshResource(file);
// NavigatorRoot.getInstance().getManager().resourceChanged(null);
if (newExperiments == null && state != null) {
updateToolbar(state, true);
} else {
Expand Down
@@ -1,14 +1,13 @@
/*********************************************************************************************
/*******************************************************************************************************
*
* 'EditorNavigationControls.java, in plugin ummisco.gama.ui.modeling, is part of the source code of the GAMA modeling
* and simulation platform. (v. 1.8.1)
* EditorToolbar.java, in ummisco.gama.ui.modeling, is part of the source code of the GAMA modeling and simulation
* platform (v.1.8.2).
*
* (c) 2007-2020 UMI 209 UMMISCO IRD/UPMC & Partners
* (c) 2007-2021 UMI 209 UMMISCO IRD/SU & Partners (IRIT, MIAT, TLU, CTU)
*
* Visit https://github.com/gama-platform/gama for license information and developers contact.
* Visit https://github.com/gama-platform/gama for license information and contacts.
*
*
**********************************************************************************************/
********************************************************************************************************/
package msi.gama.lang.gaml.ui.editor.toolbar;

import static org.eclipse.swt.events.SelectionListener.widgetSelectedAdapter;
Expand Down Expand Up @@ -43,24 +42,58 @@
*/
public class EditorToolbar {

/** The previous. */
ToolItem next, previous;

/** The find. */
EditorSearchControls find;

/** The editor. */
final GamlEditor editor;

/** The searching. */
volatile boolean searching;

/**
* Selected.
*
* @param event
* the event
* @return the selection listener
*/
static SelectionListener selected(final Consumer<SelectionEvent> event) {
return widgetSelectedAdapter(event);
}

/** The global previous. */
final SelectionListener globalPrevious = selected(e -> executeCommand(NAVIGATE_BACKWARD_HISTORY));

/** The global next. */
final SelectionListener globalNext = selected(e -> executeCommand(NAVIGATE_FORWARD_HISTORY));

/** The search previous. */
final SelectionListener searchPrevious = selected(e -> find.findPrevious());

/** The search next. */
final SelectionListener searchNext = selected(e -> find.findNext());

/**
* Instantiates a new editor toolbar.
*
* @param editor
* the editor
*/
public EditorToolbar(final GamlEditor editor) {
this.editor = editor;
}

/**
* Fill.
*
* @param toolbar
* the toolbar
* @return the editor search controls
*/
public EditorSearchControls fill(final GamaToolbarSimple toolbar) {

previous = toolbar.button("editor.lastedit2", null, "Previous edit location", globalPrevious);
Expand All @@ -71,7 +104,7 @@ public EditorSearchControls fill(final GamaToolbarSimple toolbar) {
@Override
public void widgetSelected(final SelectionEvent e) {
// final GamlEditor editor = getEditor();
if (editor == null) { return; }
if (editor == null) return;
editor.openOutlinePopup();
}
});
Expand All @@ -86,6 +119,14 @@ public void widgetSelected(final SelectionEvent e) {
return find;
}

/**
* Hook to search.
*
* @param lastEdit
* the last edit
* @param nextEdit
* the next edit
*/
private void hookToSearch(final ToolItem lastEdit, final ToolItem nextEdit) {
find.getFindControl().addFocusListener(new FocusListener() {

Expand Down Expand Up @@ -117,6 +158,14 @@ public void focusLost(final FocusEvent e) {
});
}

/**
* Hook to commands.
*
* @param lastEdit
* the last edit
* @param nextEdit
* the next edit
*/
private void hookToCommands(final ToolItem lastEdit, final ToolItem nextEdit) {
WorkbenchHelper.runInUI("Hooking to commands", 0, m -> {
final Command nextCommand = getCommand(NAVIGATE_FORWARD_HISTORY);
Expand All @@ -135,53 +184,4 @@ private void hookToCommands(final ToolItem lastEdit, final ToolItem nextEdit) {

}

// GamlEditor getEditor() {
// return (GamlEditor) WorkbenchHelper.getActiveEditor();
// }

// minus = toolbar.button("editor.decrease2", "", "Decrease font size", new
// SelectionAdapter() {
//
// @Override
// public void widgetSelected(final SelectionEvent e) {
// final GamlEditor editor = getEditor();
// if (editor == null) { return; }
// editor.setFontAndCheckButtons(-1);
// minus.setEnabled(editor.getFont().getFontData()[0].height > 6);
// }
// });
// toolbar.button("editor.increase2", "", "Increase font size", new
// SelectionAdapter() {
//
// @Override
// public void widgetSelected(final SelectionEvent e) {
// final GamlEditor editor = getEditor();
// if (editor == null) { return; }
// editor.setFontAndCheckButtons(1);
// minus.setEnabled(editor.getFont().getFontData()[0].height > 6);
// }
// });
// toolbar.sep(12);
// toolbar.sep(10);
// toolbar.button("editor.format2", null, "Format", new SelectionAdapter() {
//
// @Override
// public void widgetSelected(final SelectionEvent e) {
// final GamlEditor editor = getEditor();
// if (editor == null) { return; }
// editor.getAction("Format").run();
// }
// });
//
// toolbar.button("editor.comment2", null, "Toggle comment", new
// SelectionAdapter() {
//
// @Override
// public void widgetSelected(final SelectionEvent e) {
// final GamlEditor editor = getEditor();
// if (editor == null) { return; }
// editor.getAction("ToggleComment").run();
// }
// });

}

0 comments on commit 8b5061d

Please sign in to comment.