Skip to content

Commit

Permalink
Add query timer. (#63)
Browse files Browse the repository at this point in the history
* Add query timer
  • Loading branch information
erikmafo authored Apr 5, 2021
1 parent c4108de commit c646615
Show file tree
Hide file tree
Showing 21 changed files with 235 additions and 35 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package com.erikmafo.btviewer.ui.controllers;
package com.erikmafo.btviewer.ui;

import com.erikmafo.btviewer.sql.SqlQuery;
import com.erikmafo.btviewer.ui.components.QueryBoxController;
import com.erikmafo.btviewer.ui.components.QueryResultViewController;
import com.erikmafo.btviewer.ui.menubar.MenuBarController;
import com.erikmafo.btviewer.ui.projectexplorer.ProjectExplorerController;
import com.erikmafo.btviewer.ui.querybox.QueryBoxController;
import com.erikmafo.btviewer.ui.queryresult.QueryResultViewController;
import javafx.fxml.FXML;

/**
Expand All @@ -24,7 +25,7 @@ public class MainController {
private QueryResultViewController queryResultViewController;

@FXML
private void initialize() {
public void initialize() {
queryResultViewController.tableProperty().bind(queryBoxController.tableProperty());
queryResultViewController.setRows(queryBoxController.getQueryResult());
queryBoxController.instanceProperty().bind(projectExplorerController.selectedInstanceProperty());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.erikmafo.btviewer.ui.controllers;
package com.erikmafo.btviewer.ui.menubar;

import com.erikmafo.btviewer.services.credential.LoadCredentialsPathService;
import com.erikmafo.btviewer.services.credential.SaveCredentialsPathService;
import com.erikmafo.btviewer.ui.components.CredentialsPathDialog;
import com.erikmafo.btviewer.ui.shared.CredentialsPathDialog;
import com.sun.javafx.PlatformUtil;
import javafx.concurrent.WorkerStateEvent;
import javafx.event.ActionEvent;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.erikmafo.btviewer.ui.components;
package com.erikmafo.btviewer.ui.projectexplorer;

import com.erikmafo.btviewer.model.BigtableInstance;
import com.erikmafo.btviewer.util.FXMLLoaderUtil;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.erikmafo.btviewer.model.BigtableTable;
import com.erikmafo.btviewer.services.instance.SaveInstanceService;
import com.erikmafo.btviewer.services.project.RemoveProjectService;
import com.erikmafo.btviewer.ui.components.AddInstanceDialog;
import com.erikmafo.btviewer.util.AlertUtil;
import com.google.inject.Provider;
import javafx.beans.property.ReadOnlyObjectProperty;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package com.erikmafo.btviewer.ui.components;
package com.erikmafo.btviewer.ui.querybox;

import com.erikmafo.btviewer.model.BigtableInstance;
import com.erikmafo.btviewer.model.BigtableTable;
import com.erikmafo.btviewer.model.QueryResultRow;
import com.erikmafo.btviewer.services.query.BigtableQueryService;
import com.erikmafo.btviewer.sql.SqlParser;
import com.erikmafo.btviewer.sql.SqlQuery;
import com.erikmafo.btviewer.ui.timer.TimerView;
import com.erikmafo.btviewer.util.AlertUtil;
import com.erikmafo.btviewer.util.StringUtil;
import javafx.application.Platform;
import javafx.beans.binding.Bindings;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
Expand All @@ -30,7 +30,7 @@
import java.time.Duration;
import java.util.regex.Pattern;

import static com.erikmafo.btviewer.ui.components.SyntaxHighlightingUtil.computeSyntaxHighlighting;
import static com.erikmafo.btviewer.ui.querybox.SyntaxHighlightingUtil.computeSyntaxHighlighting;

public class QueryBoxController {

Expand All @@ -50,6 +50,9 @@ public class QueryBoxController {
@FXML
private CodeArea codeArea;

@FXML
private TimerView timer;

private final ObjectProperty<BigtableInstance> instance = new SimpleObjectProperty<>();
private final ObjectProperty<BigtableTable> table = new SimpleObjectProperty<>();
private final ObjectProperty<SqlQuery> query = new SimpleObjectProperty<>();
Expand All @@ -69,11 +72,12 @@ public void initialize() {
.successionEnds(Duration.ofMillis(100))
.subscribe(ignore -> codeArea.setStyleSpans(0, computeSyntaxHighlighting(codeArea.getText())));
table.bind(Bindings.createObjectBinding(this::createTable, instance, query));
instance.addListener(this::onInstanceChanged);
instance.addListener((observable, prevInstance, newInstance) -> onInstanceChanged());
executeQueryButton.disableProperty().bind(Bindings
.createBooleanBinding(() ->
StringUtil.isNullOrEmpty(codeArea.textProperty().getValue()), codeArea.textProperty()));
cancelQueryButton.disableProperty().bind(bigtableQueryService.runningProperty().not());
bigtableQueryService.runningProperty().addListener((obs, wasRunning, isRunning) -> updateTimer(isRunning));
}

public ObjectProperty<BigtableTable> tableProperty() { return table; }
Expand Down Expand Up @@ -119,6 +123,14 @@ public void onKeyPressedInCodeArea(@NotNull KeyEvent keyEvent) {
}
}

private void updateTimer(boolean isRunningQuery) {
if (isRunningQuery) {
timer.startFromZero();
} else {
timer.stop();
}
}

@Nullable
private BigtableTable createTable() {
var instance = this.instance.get();
Expand All @@ -129,7 +141,5 @@ private BigtableTable createTable() {
return null;
}

private void onInstanceChanged(ObservableValue<? extends BigtableInstance> obs, BigtableInstance prev, BigtableInstance current) {
queryResult.clear();
}
private void onInstanceChanged() { queryResult.clear(); }
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.erikmafo.btviewer.ui.components;
package com.erikmafo.btviewer.ui.querybox;

import com.erikmafo.btviewer.sql.SqlToken;
import com.erikmafo.btviewer.sql.SqlTokenType;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.erikmafo.btviewer.ui.components;
package com.erikmafo.btviewer.ui.queryresult;

import com.erikmafo.btviewer.model.QueryResultRow;
import javafx.collections.ObservableList;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.erikmafo.btviewer.ui.components;
package com.erikmafo.btviewer.ui.queryresult;

import com.erikmafo.btviewer.model.BigtableCell;
import com.erikmafo.btviewer.model.BigtableValueConverter;
Expand Down Expand Up @@ -37,7 +37,7 @@ public CellView() {
}

@FXML
private void initialize() {
public void initialize() {
valueLabel.textProperty().bind(Bindings
.createStringBinding(this::getDisplayValue, cell, valueConverter));
versionLabel.textProperty().bind(Bindings
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.erikmafo.btviewer.ui.components;
package com.erikmafo.btviewer.ui.queryresult;

import com.erikmafo.btviewer.model.BigtableCell;
import com.erikmafo.btviewer.model.BigtableColumn;
Expand All @@ -8,6 +8,7 @@
import com.erikmafo.btviewer.model.QueryResultRow;
import com.erikmafo.btviewer.services.table.LoadTableSettingsService;
import com.erikmafo.btviewer.services.table.SaveTableSettingsService;
import com.erikmafo.btviewer.ui.shared.TableSettingsDialog;
import com.erikmafo.btviewer.util.AlertUtil;
import com.sun.javafx.PlatformUtil;
import javafx.beans.binding.Bindings;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.erikmafo.btviewer.ui.components;
package com.erikmafo.btviewer.ui.queryresult;

import com.erikmafo.btviewer.util.FXMLLoaderUtil;
import javafx.fxml.FXML;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.erikmafo.btviewer.ui.components;
package com.erikmafo.btviewer.ui.shared;

import com.erikmafo.btviewer.util.FXMLLoaderUtil;
import javafx.event.ActionEvent;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.erikmafo.btviewer.ui.components;
package com.erikmafo.btviewer.ui.shared;

import com.erikmafo.btviewer.model.BigtableColumn;
import com.erikmafo.btviewer.model.BigtableTableSettings;
Expand Down
95 changes: 95 additions & 0 deletions src/main/java/com/erikmafo/btviewer/ui/timer/TimerView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package com.erikmafo.btviewer.ui.timer;

import com.erikmafo.btviewer.util.FXMLLoaderUtil;
import javafx.animation.Animation;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.beans.property.Property;
import javafx.beans.property.SimpleObjectProperty;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.layout.Pane;
import javafx.util.Duration;
import org.jetbrains.annotations.NotNull;

import static javafx.beans.binding.Bindings.createIntegerBinding;

public class TimerView extends Pane {

private static final double KEY_FRAME_DURATION_MILLIS = 1;
private static final String TWO_DIGITS = "%02d";
private static final String TREE_DIGITS = "%03d";

private final Property<Duration> time = new SimpleObjectProperty<>(Duration.ZERO);
private final Timeline timeline = new Timeline();

@FXML
private Label hoursLabel;

@FXML
private Label minutesLabel;

@FXML
private Label secondsLabel;

@FXML
private Label millisLabel;

public TimerView() { FXMLLoaderUtil.loadFxml("/fxml/timer_view.fxml", this); }

@FXML
public void initialize() {
timeline.setCycleCount(Animation.INDEFINITE);
hoursLabel.textProperty().bind(createIntegerBinding(this::getHoursPart, time).asString(TWO_DIGITS));
minutesLabel.textProperty().bind(createIntegerBinding(this::getMinutesPart, time).asString(TWO_DIGITS));
secondsLabel.textProperty().bind(createIntegerBinding(this::getSecondsPart, time).asString(TWO_DIGITS));
millisLabel.textProperty().bind(createIntegerBinding(this::getMillisPart, time).asString(TREE_DIGITS));
}

public void setTime(Duration duration) {
time.setValue(duration);
}

public void startFromZero() {
reset();
start();
}

public void start() {
timeline.getKeyFrames()
.setAll(new KeyFrame(Duration.millis(KEY_FRAME_DURATION_MILLIS), event -> increaseTime()));
timeline.play();
}

public void stop() { timeline.stop(); }

public void reset() {
stop();
setTime(Duration.ZERO);
}

private void increaseTime() {
time.setValue(time.getValue().add(Duration.ONE));
}

private int getMillisPart() {
return toJavaDuration(time.getValue()).toMillisPart();
}

private int getSecondsPart() {
return toJavaDuration(time.getValue()).toSecondsPart();
}

private int getMinutesPart() {
return toJavaDuration(time.getValue()).toMinutesPart();
}

private int getHoursPart() {
return toJavaDuration(time.getValue()).toHoursPart();
}

private java.time.Duration toJavaDuration(@NotNull Duration duration) {
return java.time.Duration.ofMillis((long) duration.toMillis());
}
}

2 changes: 1 addition & 1 deletion src/main/resources/fxml/bigtable_menu_bar.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<?import javafx.scene.control.MenuItem?>
<MenuBar xmlns:fx="http://javafx.com/fxml"
fx:id="menuBar"
fx:controller="com.erikmafo.btviewer.ui.controllers.MenuBarController">
fx:controller="com.erikmafo.btviewer.ui.menubar.MenuBarController">
<menus>
<Menu text="File">
<items>
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/fxml/main.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.VBox?>
<BorderPane xmlns:fx="http://javafx.com/fxml"
fx:controller="com.erikmafo.btviewer.ui.controllers.MainController"
fx:controller="com.erikmafo.btviewer.ui.MainController"
stylesheets="/css/main.css"
styleClass="pane-view">
<top>
Expand Down
16 changes: 8 additions & 8 deletions src/main/resources/fxml/query_box.fxml
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>

<?import com.erikmafo.btviewer.ui.timer.TimerView?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ProgressBar?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.layout.VBox?>
<?import org.fxmisc.richtext.CodeArea?>
<VBox styleClass="queryBox, has-spacing"
stylesheets="/css/query_box.css"
xmlns:fx="http://javafx.com/fxml"
fx:controller="com.erikmafo.btviewer.ui.components.QueryBoxController">
fx:controller="com.erikmafo.btviewer.ui.querybox.QueryBoxController">

<CodeArea fx:id="codeArea" onKeyPressed="#onKeyPressedInCodeArea"/>
<HBox alignment="BASELINE_LEFT" styleClass="has-spacing">
<Button
fx:id="executeQueryButton"
<Button fx:id="executeQueryButton"
text="Execute"
styleClass="btn-success"
onAction="#onExecuteQueryButtonPressed"/>
<Button
fx:id="cancelQueryButton"
<Button fx:id="cancelQueryButton"
text="Cancel"
styleClass="btn-danger"
onAction="#onCancelQueryButtonPressed"/>
<ProgressBar
fx:id="progressBar"
styleClass="progress-bar-success"/>
<ProgressBar fx:id="progressBar" styleClass="progress-bar-success"/>
<Pane HBox.hgrow="ALWAYS" />
<TimerView fx:id="timer"/>
</HBox>

</VBox>
2 changes: 1 addition & 1 deletion src/main/resources/fxml/query_result_view.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<?import javafx.scene.layout.Region?>
<?import javafx.scene.layout.VBox?>
<VBox xmlns:fx="http://javafx.com/fxml"
fx:controller="com.erikmafo.btviewer.ui.components.QueryResultViewController"
fx:controller="com.erikmafo.btviewer.ui.queryresult.QueryResultViewController"
fx:id="vBox"
stylesheets="/css/query_result_view.css"
styleClass="has-spacing">
Expand Down
20 changes: 20 additions & 0 deletions src/main/resources/fxml/timer_view.fxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.Pane?>
<fx:root type="javafx.scene.layout.Pane"
xmlns="http://javafx.com/javafx"
xmlns:fx="http://javafx.com/fxml">
<GridPane>
<children>
<Label fx:id="hoursLabel" GridPane.rowIndex="0" GridPane.columnIndex="0" text="01"/>
<Label GridPane.rowIndex="0" GridPane.columnIndex="1" text=":"/>
<Label fx:id="minutesLabel" GridPane.rowIndex="0" GridPane.columnIndex="2" text="12"/>
<Label GridPane.rowIndex="0" GridPane.columnIndex="3" text=":"/>
<Label fx:id="secondsLabel" GridPane.rowIndex="0" GridPane.columnIndex="4" text="56"/>
<Label GridPane.rowIndex="0" GridPane.columnIndex="5" text=":"/>
<Label fx:id="millisLabel" GridPane.rowIndex="0" GridPane.columnIndex="6" text="999"/>
</children>
</GridPane>
</fx:root>
2 changes: 1 addition & 1 deletion src/test/java/com/erikmafo/btviewer/ui/MainAppTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.erikmafo.btviewer.MainApp;
import com.erikmafo.btviewer.config.AppConfig;
import com.erikmafo.btviewer.ui.components.AddInstanceDialog;
import com.erikmafo.btviewer.ui.projectexplorer.AddInstanceDialog;
import javafx.scene.input.MouseButton;
import javafx.stage.Stage;
import org.junit.Test;
Expand Down
Loading

0 comments on commit c646615

Please sign in to comment.