package de.prob2.ui.consoles.b;

import java.io.File;
import java.util.ResourceBundle;

import com.google.inject.Inject;
import com.google.inject.Singleton;

import de.prob2.ui.config.Config;
import de.prob2.ui.config.ConfigData;
import de.prob2.ui.config.ConfigListener;
import de.prob2.ui.consoles.Console;
import de.prob2.ui.internal.FXMLInjected;
import de.prob2.ui.prob2fx.CurrentTrace;

@FXMLInjected
@Singleton
public final class BConsole extends Console {
	@Inject
	private BConsole(BInterpreter bInterpreter, ResourceBundle bundle, CurrentTrace currentTrace, Config config) {
		super(bundle, bundle.getString("consoles.b.header"), bundle.getString("consoles.b.prompt.classicalB"), bInterpreter);
		
		currentTrace.stateSpaceProperty().addListener((o, from, to) -> {
			final String message;
			if (to == null) {
				message = bundle.getString("consoles.b.message.modelUnloaded");
			} else {
				final File modelFile = to.getModel().getModelFile();
				final String name = modelFile == null ? to.getMainComponent().toString() : modelFile.getName();
				message = String.format(bundle.getString("consoles.b.message.modelLoaded"), name);
			}
			final int oldCaretPos = this.getCaretPosition();
			final String line = message + '\n';
			this.insertText(this.getLineNumber(), 0, line);
			this.moveTo(oldCaretPos + line.length());
			this.requestFollowCaret();
		});
		
		config.addListener(new ConfigListener() {
			@Override
			public void loadConfig(final ConfigData configData) {
				if (configData.bConsoleInstructions != null) {
					loadInstructions(configData.bConsoleInstructions);
				}
			}
			
			@Override
			public void saveConfig(final ConfigData configData) {
				configData.bConsoleInstructions = saveInstructions();
			}
		});
	}
}

@@ -30,63 +30,8 @@
 b;%0A%0A
-import java.io.File;%0Aimport java.util.ResourceBundle;%0A%0A
 impo
@@ -59,16 +59,16 @@
 Inject;%0A
+
 import c
@@ -95,17 +95,16 @@
 gleton;%0A
-%0A
 import d
@@ -330,16 +330,71 @@
 Trace;%0A%0A
+import java.io.File;%0Aimport java.util.ResourceBundle;%0A%0A
 @FXMLInj
@@ -456,16 +456,44 @@
 nsole %7B%0A
+%0A%09private boolean typedIn;%0A%0A
 %09@Inject
@@ -723,24 +723,45 @@
 rpreter);%0A%09%09
+this.typedIn = false;
 %0A%09%09currentTr
@@ -816,16 +816,51 @@
 o) -%3E %7B%0A
+%09%09%09if(!typedIn) %7B%0A%09%09%09%09return;%0A%09%09%09%7D%0A
 %09%09%09final
@@ -1827,13 +1827,132 @@
 %7D%0A%09%09%7D);%0A
+%09%09this.textProperty().addListener((observable, from, to) -%3E %7B%0A%09%09%09if(!from.equals(to)) %7B%0A%09%09%09%09typedIn = true;%0A%09%09%09%7D%0A%09%09%7D);%0A
 %09%7D%0A%7D%0A

Solve PROB2UI-327