Skip to content

Commit

Permalink
Addresses #3180, #3138, #3184 and #2990
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexisDrogoul committed Sep 10, 2021
1 parent 67740f8 commit 3aa8de6
Show file tree
Hide file tree
Showing 9 changed files with 516 additions and 189 deletions.
4 changes: 3 additions & 1 deletion msi.gama.application/META-INF/MANIFEST.MF
Expand Up @@ -23,7 +23,9 @@ Require-Bundle: org.eclipse.core.runtime,
org.eclipse.e4.ui.css.swt;bundle-version="0.13.1100",
org.eclipse.e4.ui.workbench.addons.swt;bundle-version="1.3.1100",
org.eclipse.e4.ui.workbench.renderers.swt;bundle-version="0.14.1300",
org.eclipse.e4.ui.workbench.swt;bundle-version="0.14.1100"
org.eclipse.e4.ui.workbench.swt;bundle-version="0.14.1100",
org.eclipse.e4.ui.css.core;bundle-version="0.13.0",
org.w3c.css.sac;bundle-version="1.3.1"
Bundle-Vendor: UMMISCO
Bundle-Version: 1.8.1.qualifier
Bundle-ManifestVersion: 2
Expand Down
38 changes: 29 additions & 9 deletions msi.gama.application/plugin.xml
Expand Up @@ -139,6 +139,34 @@
</property>
</product>
</extension>
<extension
point="org.eclipse.e4.ui.css.swt.theme">
<stylesheet
uri="css/tabs.css">
<themeid
refid="org.eclipse.e4.ui.css.theme.e4_default">
</themeid>
<themeid
refid="org.eclipse.e4.ui.css.theme.e4_classic">
</themeid>
<themeid
refid="org.eclipse.e4.ui.css.theme.e4_dark">
</themeid>
<themeid
refid="org.eclipse.e4.ui.css.theme.e4_system">
</themeid>
</stylesheet>
</extension>
<extension
id="gama.theming"
name="Dark theme for GAMA"
point="org.eclipse.e4.ui.css.swt.theme">
<theme
basestylesheeturi="msi.gama.application.theme1"
id="gama.dark.theme"
label="Gama Dark Theme">
</theme>
</extension>
<extension
id="testNature"
name="GAMA test project"
Expand All @@ -152,13 +180,5 @@
id="msi.gama.application.gamaNature">
</requires-nature>
</extension>
<extension
point="org.eclipse.e4.ui.css.swt.theme">
<stylesheet
uri="css/tabs.css">
<themeid
refid="org.eclipse.e4.ui.css.theme.e4_default">
</themeid>
</stylesheet>
</extension>

</plugin>
61 changes: 48 additions & 13 deletions msi.gama.application/src/msi/gama/application/Application.java
@@ -1,14 +1,13 @@
/*********************************************************************************************
/*******************************************************************************************************
*
* 'Application.java, in plugin msi.gama.application, is part of the source code of the GAMA modeling and simulation
* platform. (v. 1.8.1)
* Application.java, in msi.gama.application, 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.application;

import static java.lang.System.setProperty;
Expand Down Expand Up @@ -48,35 +47,60 @@
import msi.gama.application.workspace.WorkspaceModelsManager;
import msi.gama.application.workspace.WorkspacePreferences;
import ummisco.gama.dev.utils.DEBUG;
import ummisco.gama.dev.utils.FLAGS;

/** This class controls all aspects of the application's execution */
public class Application implements IApplication {

{
DEBUG.OFF();
if (FLAGS.USE_PRECISE_AUTOSCALE) { System.setProperty("swt.autoScale", "quarter"); }
}

/** The processor. */
public static OpenDocumentEventProcessor processor;

/** The Constant CLEAR_WORKSPACE. */
public static final String CLEAR_WORKSPACE = "clearWorkspace";

/**
* Clear workspace.
*
* @param clear
* the clear
*/
public static void ClearWorkspace(final boolean clear) {
getInternalPreferenceStore().setValue(CLEAR_WORKSPACE, Boolean.toString(clear));
saveInternalPrefs();
}

/**
* Checks if is clear workspace.
*
* @return true, if successful
*/
public static boolean IsClearWorkspace() {
final boolean result = getInternalPreferenceStore().getBoolean(CLEAR_WORKSPACE);
DEBUG.OUT("Value of clearWorkspace pref: " + result);
return result;
}

/**
* The Class OpenDocumentEventProcessor.
*/
public static class OpenDocumentEventProcessor extends DelayedEventsProcessor {

/**
* Instantiates a new open document event processor.
*
* @param display
* the display
*/
OpenDocumentEventProcessor(final Display display) {
super(display);
}

/** The files to open. */
private final ArrayList<String> filesToOpen = new ArrayList<>(1);

@Override
Expand All @@ -94,12 +118,13 @@ public void catchUp(final Display display) {
final String[] filePaths = filesToOpen.toArray(new String[filesToOpen.size()]);
filesToOpen.clear();

for (final String path : filePaths) {
WorkspaceModelsManager.instance.openModelPassedAsArgument(path);
}
for (final String path : filePaths) { WorkspaceModelsManager.instance.openModelPassedAsArgument(path); }
}
}

/**
* Creates the processor.
*/
public static void createProcessor() {
final Display display = Display.getDefault();
if (display == null) return;
Expand Down Expand Up @@ -145,6 +170,9 @@ public Object start(final IApplicationContext context) throws Exception {

}

/**
* Check workbench XMI.
*/
private void checkWorkbenchXMI() {
final boolean removeWorkbenchXMI = IsClearWorkspace();
if (removeWorkbenchXMI) {
Expand All @@ -154,6 +182,15 @@ private void checkWorkbenchXMI() {

}

/**
* Check workspace.
*
* @return the object
* @throws IOException
* Signals that an I/O exception has occurred.
* @throws MalformedURLException
* the malformed URL exception
*/
public static Object checkWorkspace() throws IOException, MalformedURLException {
final Location instanceLoc = Platform.getInstanceLocation();
if (instanceLoc == null) {
Expand Down Expand Up @@ -221,9 +258,7 @@ public void stop() {
final IWorkbench workbench = getWorkbench();
if (workbench == null) return;
final Display display = workbench.getDisplay();
display.syncExec(() -> {
if (!display.isDisposed()) { workbench.close(); }
});
display.syncExec(() -> { if (!display.isDisposed()) { workbench.close(); } });
}

}
Expand Up @@ -15,22 +15,36 @@
import static msi.gama.common.preferences.GamaPreferences.Interface.NAME;
import static org.eclipse.swt.widgets.Display.isSystemDarkTheme;

import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.core.runtime.preferences.InstanceScope;
import org.eclipse.e4.core.contexts.IEclipseContext;
import org.eclipse.e4.core.services.events.IEventBroker;
import org.eclipse.e4.ui.css.core.dom.ExtendedDocumentCSS;
import org.eclipse.e4.ui.css.core.engine.CSSEngine;
import org.eclipse.e4.ui.css.swt.internal.theme.ThemeEngine;
import org.eclipse.e4.ui.css.swt.theme.ITheme;
import org.eclipse.e4.ui.css.swt.theme.IThemeEngine;
import org.eclipse.e4.ui.css.swt.theme.IThemeManager;
import org.eclipse.e4.ui.workbench.UIEvents;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.internal.Workbench;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.FrameworkUtil;
import org.osgi.framework.ServiceReference;
import org.osgi.service.event.EventHandler;
import org.osgi.service.prefs.BackingStoreException;
import org.w3c.css.sac.CSSParseException;
import org.w3c.dom.stylesheets.StyleSheet;
import org.w3c.dom.stylesheets.StyleSheetList;

import msi.gama.common.preferences.Pref;
import msi.gaml.types.IType;
Expand Down Expand Up @@ -68,6 +82,12 @@ public class ThemeHelper {
/** The Constant listeners. */
private static final List<IThemeListener> listeners = new ArrayList<>();

/** The engine. */
private static IThemeEngine engine;

/** The bundle. */
private static Bundle bundle = Platform.getBundle("msi.gama.application");

static {
DEBUG.ON();
}
Expand Down Expand Up @@ -176,6 +196,7 @@ public static void install() {
eventBroker.subscribe(IThemeEngine.Events.THEME_CHANGED, themeChangedHandler);
}
chooseThemeBasedOnPreferences();
// injectCSS(".MPartStack {\n" + " swt-tab-renderer: null;\n" + " swt-simple: true;\n" + "}");
}

/**
Expand Down Expand Up @@ -223,11 +244,9 @@ private static boolean changeTo(final String id) {
} catch (final BackingStoreException e) {
e.printStackTrace();
}
final var themeEngine = getContext().get(IThemeEngine.class);
if (themeEngine == null) return true;
final var theme = themeEngine.getActiveTheme();
final var theme = getEngine().getActiveTheme();
if (theme != null && theme.getId().startsWith(id)) return false;
themeEngine.setTheme(id, true);
getEngine().setTheme(id, true);
return true;
}

Expand All @@ -251,11 +270,77 @@ public static void removeListener(final IThemeListener l) {
listeners.remove(l);
}

/**
* Gets the engine.
*
* @return the engine
*/
public static IThemeEngine getEngine() {
if (engine == null) { engine = getThemeEngine(); }
return engine;
}

/**
* Gets the theme engine.
*
* @return the theme engine
*/
private static ThemeEngine getThemeEngine() {
BundleContext context = bundle.getBundleContext();
ServiceReference ref = context.getServiceReference(IThemeManager.class.getName());
IThemeManager manager = (IThemeManager) context.getService(ref);
return (ThemeEngine) manager.getEngineForDisplay(PlatformUI.getWorkbench().getActiveWorkbenchWindow() == null
? Display.getCurrent() : PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell().getDisplay());
}

/**
* Inject CSS.
*
* @param cssText
* the css text
*/
public static void injectCSS(final String cssText) {
StringBuilder sb = new StringBuilder();
// FIXME: expose these new protocols: resetCurrentTheme() and
// getCSSEngines()
getThemeEngine().resetCurrentTheme();

int count = 0;
for (CSSEngine engine : getThemeEngine().getCSSEngines()) {
if (count++ > 0) { sb.append("\n\n"); }
sb.append("Engine[").append(engine.getClass().getSimpleName()).append("]");
ExtendedDocumentCSS doc = (ExtendedDocumentCSS) engine.getDocumentCSS();
List<StyleSheet> sheets = new ArrayList<>();
StyleSheetList list = doc.getStyleSheets();
for (int i = 0; i < list.getLength(); i++) { sheets.add(list.item(i)); }

try {
Reader reader = new StringReader(cssText);
sheets.add(0, engine.parseStyleSheet(reader));
doc.removeAllStyleSheets();
for (StyleSheet sheet : sheets) { doc.addStyleSheet(sheet); }
engine.reapply();

} catch (CSSParseException e) {
sb.append("\nError: line ").append(e.getLineNumber()).append(" col ").append(e.getColumnNumber())
.append(": ").append(e.getLocalizedMessage());
} catch (IOException e) {
sb.append("\nError: ").append(e.getLocalizedMessage());
}
}
}

/**
* The Class WorkbenchThemeChangedHandler.
*/
public static class WorkbenchThemeChangedHandler implements EventHandler {

/**
* Handle event.
*
* @param event
* the event
*/
@Override
public void handleEvent(final org.osgi.service.event.Event event) {
final var theme = getTheme(event);
Expand All @@ -276,10 +361,7 @@ public void handleEvent(final org.osgi.service.event.Event event) {
*/
protected ITheme getTheme(final org.osgi.service.event.Event event) {
var theme = (ITheme) event.getProperty(IThemeEngine.Events.THEME);
if (theme == null) {
final var themeEngine = getContext().get(IThemeEngine.class);
theme = themeEngine != null ? themeEngine.getActiveTheme() : null;
}
if (theme == null) { theme = getEngine().getActiveTheme(); }
return theme;
}
}
Expand Down

0 comments on commit 3aa8de6

Please sign in to comment.