Skip to content

Commit

Permalink
Merge pull request #46 from Terracotta-OSS/issue/cursor-position
Browse files Browse the repository at this point in the history
Several fixes around text areas
  • Loading branch information
anthonydahanne authored Apr 24, 2019
2 parents 3501b21 + 22dd8f1 commit 3d6ea91
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 34 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.terracotta</groupId>
<artifactId>tinypounder</artifactId>
<version>1.1.1</version>
<version>1.2.0</version>
<packaging>jar</packaging>

<name>tinypounder</name>
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/terracotta/tinypounder/ProcUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
public class ProcUtils {
private static String OS = System.getProperty("os.name").toLowerCase();

static AnyProcess run(File workDir, String command, Queue<String> consoleLines, Consumer<String> onNewLine, Runnable onStop) {
static AnyProcess run(File workDir, String command, Queue<String> consoleLines, Consumer<String> onNewLine, Runnable onTerminated) {
final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
final OutputStream out = new OutputStream() {
@Override
Expand Down Expand Up @@ -53,7 +53,7 @@ public void write(int b) throws IOException {
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
onStop.run();
onTerminated.run();
}, "Process (" + command + ")");
waiter.setDaemon(true);
waiter.start();
Expand Down
14 changes: 6 additions & 8 deletions src/main/java/org/terracotta/tinypounder/RunningServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,23 @@ class RunningServer {
private final String serverName;
private final TextArea console;
private final ArrayBlockingQueue<String> lines;
private final Runnable onStop;
private final Runnable onTerminated;
private final Consumer<String> onState;
private final Consumer<Long> onPID;
private long pid;

RunningServer(File workDir, File stripeconfig, String serverName, TextArea console, int maxLines, Runnable onStop, Consumer<String> onState, Consumer<Long> onPID) {
RunningServer(File workDir, File stripeconfig, String serverName, TextArea console, int maxLines, Runnable onTerminated, Consumer<String> onState, Consumer<Long> onPID) {
this.workDir = workDir;
this.stripeconfig = stripeconfig;
this.serverName = serverName;
this.lines = new ArrayBlockingQueue<>(maxLines);
this.console = console;
this.onStop = onStop;
this.onTerminated = onTerminated;
this.onState = onState;
this.onPID = onPID;
}

void stop() {
void kill() {
try {
ProcUtils.kill(pid);
} catch (InterruptedException e) {
Expand Down Expand Up @@ -73,13 +73,11 @@ void start() {
}
}
},
onStop);
onTerminated);
}

void refreshConsole() {
String text = String.join("", lines);
console.setValue(text);
console.setCursorPosition(text.length());
TinyPounderMainUI.updateTextArea(console, lines);
}

}
62 changes: 40 additions & 22 deletions src/main/java/org/terracotta/tinypounder/TinyPounderMainUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,12 @@
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.Queue;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
Expand Down Expand Up @@ -181,7 +183,7 @@ private void addExitCloseTab() {
mainLayout.addSelectedTabChangeListener(tabEvent -> {
if (tabEvent.getTabSheet().getSelectedTab().equals(tab.getComponent())) {
new Thread(() -> {
runningServers.values().forEach(RunningServer::stop);
runningServers.values().forEach(RunningServer::kill);
consoleRefresher.cancel(true);
SpringApplication.exit(appContext);
}).start();
Expand Down Expand Up @@ -355,7 +357,7 @@ private void addVoltronCommandsControls() {
Button clusterStopBtn = new Button();
clusterStopBtn.addStyleName("align-bottom");
clusterStopBtn.setCaption("Stop cluster");
clusterStopBtn.setData("stop");
clusterStopBtn.setData("stop-cluster");
clusterStopBtn.addClickListener((Button.ClickListener) this::executeClusterToolCommand);

row1.addComponents(clusterNameTF, clusterConfigBtn, clusterReConfigBtn, clusterBackupBtn, clusterDumpBtn, clusterStopBtn, clusterStatusBtn);
Expand All @@ -364,7 +366,13 @@ private void addVoltronCommandsControls() {
voltronControlLayout.addComponentsAndExpand(row1);

consoles = new TabSheet();
consoles.addSelectedTabChangeListener((TabSheet.SelectedTabChangeListener) event -> {
TextArea textArea = (TextArea) event.getTabSheet().getSelectedTab();
textArea.setCursorPosition(textArea.getValue().length());
});

mainConsole = addConsole("Main", "main");

voltronControlLayout.addComponentsAndExpand(consoles);
}

Expand Down Expand Up @@ -405,7 +413,7 @@ private void executeClusterToolCommand(Button.ClickEvent event) {
case "dump":
case "status":
case "backup":
case "stop": {
case "stop-cluster": {
ProcUtils.run(
workDir,
script + " " + command + " -n " + clusterNameTF.getValue() + " " + hostPortList.stream().collect(Collectors.joining(" ")),
Expand Down Expand Up @@ -439,9 +447,7 @@ private String getHostPortFromServerName(String serverName) {
}

private void updateMainConsole(Queue<String> consoleLines) {
String text = String.join("", consoleLines);
mainConsole.setValue(text);
mainConsole.setCursorPosition(text.length());
updateTextArea(mainConsole, consoleLines);
}

private TextArea addConsole(String title, String key) {
Expand Down Expand Up @@ -484,12 +490,12 @@ private void updateServerControls() {
startBT.setStyleName("align-top");
serverControls.addComponent(startBT);

Button stopBT = new Button();
stopBT.setEnabled(false);
stopBT.setCaption("STOP");
stopBT.setStyleName("align-top");
stopBT.setData(serverName);
serverControls.addComponent(stopBT);
Button killBT = new Button();
killBT.setEnabled(false);
killBT.setCaption("KILL");
killBT.setStyleName("align-top");
killBT.setData(serverName);
serverControls.addComponent(killBT);

Button statusBT = new Button();
statusBT.setEnabled(false);
Expand All @@ -507,10 +513,10 @@ private void updateServerControls() {
addConsole(serverName, stripeName + "-" + serverName);

startBT.addClickListener((Button.ClickListener) event -> {
startServer(stripeName, (String) event.getButton().getData(), startBT, stopBT, statusBT, state, pid);
startServer(stripeName, (String) event.getButton().getData(), startBT, killBT, statusBT, state, pid);
});
stopBT.addClickListener((Button.ClickListener) event -> {
stopServer(stripeName, (String) event.getButton().getData(), stopBT, statusBT);
killBT.addClickListener((Button.ClickListener) event -> {
killServer(stripeName, (String) event.getButton().getData(), killBT, statusBT);
});
statusBT.addClickListener((Button.ClickListener) this::executeClusterToolCommand);
}
Expand Down Expand Up @@ -550,17 +556,17 @@ private List<String> getServerNameList() {
return servers;
}

private void stopServer(String stripeName, String serverName, Button stopBT, Button statusBT) {
private void killServer(String stripeName, String serverName, Button killBT, Button statusBT) {
RunningServer runningServer = runningServers.get(stripeName + "-" + serverName);
if (runningServer != null) {
runningServer.stop();
stopBT.setEnabled(false);
runningServer.kill();
killBT.setEnabled(false);
statusBT.setEnabled(false);
runningServer.refreshConsole();
}
}

private void startServer(String stripeName, String serverName, Button startBT, Button stopBT, Button statusBT, Label stateLBL, Label pidLBL) {
private void startServer(String stripeName, String serverName, Button startBT, Button killBT, Button statusBT, Label stateLBL, Label pidLBL) {
File stripeconfig = tcConfigLocationPerStripe.get(stripeName);
if (stripeconfig == null) {
generateXML(false);
Expand All @@ -576,11 +582,11 @@ private void startServer(String stripeName, String serverName, Button startBT, B
() -> {
runningServers.remove(key);
access(() -> {
stopBT.setEnabled(false);
killBT.setEnabled(false);
statusBT.setEnabled(false);
startBT.setEnabled(true);
pidLBL.setValue("");
stateLBL.setValue("STOPPED");
stateLBL.setValue("TERMINATED");
});
},
newState -> access(() -> stateLBL.setValue("STATE: " + newState)),
Expand All @@ -596,7 +602,7 @@ private void startServer(String stripeName, String serverName, Button startBT, B
stateLBL.setValue("STARTING");
runningServer.start();
startBT.setEnabled(false);
stopBT.setEnabled(true);
killBT.setEnabled(true);
statusBT.setEnabled(true);
runningServer.refreshConsole();
}
Expand Down Expand Up @@ -1639,6 +1645,7 @@ private void addClientSecurityControls() {
clientSecurityLayout.addComponent(clientSecurityControls);

}

private void addDatasetManagerControls() {

datasetManagerControls = new VerticalLayout();
Expand Down Expand Up @@ -1982,5 +1989,16 @@ private static String getVersion() {
return v == null ? "dev" : v;
}

static void updateTextArea(TextArea textArea, Collection<String> lines) {
synchronized (textArea) {
String text = String.join("", lines);
if (!Objects.equals(textArea.getValue(), text)) {
int position = textArea.getCursorPosition();
boolean autoscroll = position >= textArea.getValue().length() - 1;
textArea.setValue(text);
textArea.setCursorPosition(autoscroll ? text.length() : Math.min(position, text.length()));
}
}
}

}
2 changes: 1 addition & 1 deletion src/main/resources/banner.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
/_/ /_/_/ /_/\__, / /_/ \____/\__,_/_/ /_/\__,_/\___/_/
/____/

Please don't use quotes around it !
Please don't use quotes around it !

0 comments on commit 3d6ea91

Please sign in to comment.