Skip to content

Commit

Permalink
Fixes #166: cut, copy, paste with mouse.
Browse files Browse the repository at this point in the history
  • Loading branch information
erikmafo committed Aug 28, 2022
1 parent 4aafb4d commit 99ba69f
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 10 deletions.
10 changes: 5 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,11 @@
<artifactId>google-cloud-bigtable-emulator</artifactId>
<version>0.148.0</version>
</dependency>
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>5.1.0</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
Expand Down Expand Up @@ -264,11 +269,6 @@
<version>4.7.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>5.1.0</version>
</dependency>
<dependency>
<groupId>org.controlsfx</groupId>
<artifactId>controlsfx</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.erikmafo.btviewer.sql.SqlQuery;
import com.erikmafo.btviewer.ui.timer.TimerView;
import com.erikmafo.btviewer.ui.util.AlertUtil;
import com.erikmafo.btviewer.ui.util.FontAwesomeUtil;
import com.erikmafo.btviewer.util.StringUtil;
import javafx.application.Platform;
import javafx.beans.binding.Bindings;
Expand All @@ -18,9 +19,14 @@
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.MenuItem;
import javafx.scene.control.ProgressBar;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import org.controlsfx.glyphfont.FontAwesome;
import org.fxmisc.richtext.CodeArea;
import org.fxmisc.richtext.LineNumberFactory;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -67,6 +73,7 @@ public QueryBoxController(BigtableQueryService bigtableQueryService) {
public void initialize() {
progressBar.visibleProperty().bind(bigtableQueryService.runningProperty());
progressBar.progressProperty().bind(bigtableQueryService.progressProperty());
codeArea.setOnMousePressed(this::onMousePressed);
codeArea.setParagraphGraphicFactory(LineNumberFactory.get(codeArea));
codeArea.multiPlainChanges()
.successionEnds(Duration.ofMillis(100))
Expand Down Expand Up @@ -145,4 +152,48 @@ private BigtableTable createTable() {
}

private void onInstanceChanged() { queryResult.clear(); }

private void onMousePressed(@NotNull MouseEvent event) {
if (event.getButton() == MouseButton.SECONDARY) {
codeArea.setContextMenu(getOrCreateContextMenu());
} else {
codeArea.hideContextMenu();
}
}

@NotNull
private ContextMenu getOrCreateContextMenu() {
ContextMenu contextMenu = codeArea.getContextMenu();
if (contextMenu == null) {
contextMenu = new ContextMenu();
contextMenu.setAutoHide(true);
contextMenu.setHideOnEscape(true);
contextMenu.getItems().addAll(getCutMenuItem(), getCopyMenuItem(), getPastMenuItem());
}
return contextMenu;
}

@NotNull
private MenuItem getPastMenuItem() {
var paste = new MenuItem("Paste");
paste.setGraphic(FontAwesomeUtil.create(FontAwesome.Glyph.PASTE));
paste.setOnAction(e -> codeArea.paste());
return paste;
}

@NotNull
private MenuItem getCopyMenuItem() {
var copy = new MenuItem("Copy");
copy.setGraphic(FontAwesomeUtil.create(FontAwesome.Glyph.COPY));
copy.setOnAction(e -> codeArea.copy());
return copy;
}

@NotNull
private MenuItem getCutMenuItem() {
var cut = new MenuItem("Cut");
cut.setGraphic(FontAwesomeUtil.create(FontAwesome.Glyph.CUT));
cut.setOnAction(e -> codeArea.cut());
return cut;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ public class FontAwesomeUtil {
private static final GlyphFont fontAwesome = GlyphFontRegistry.font("FontAwesome");

public static Glyph create(Enum<?> glyph) {
return fontAwesome.create(glyph);
var glyphNode = fontAwesome.create(glyph);
glyphNode.setStyle("-fx-font-family: FontAwesome;");
return glyphNode;
}
}
9 changes: 9 additions & 0 deletions src/main/resources/css/query_box.css
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
@import "main.css";

.code-area {
-fx-font-size: 14;
-fx-font-weight: 690;
}

.keyword {
-fx-fill: blue;
-fx-font-weight: bold;
Expand All @@ -26,3 +31,7 @@
.paragraph-box:has-caret {
-fx-background-color: #f2f9fc;
}

.controls {
-fx-padding: 5;
}
8 changes: 4 additions & 4 deletions src/main/resources/fxml/query_box.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.layout.VBox?>
<?import org.fxmisc.richtext.CodeArea?>
<VBox styleClass="queryBox, has-spacing"
<VBox styleClass="queryBox"
stylesheets="/css/query_box.css"
xmlns:fx="http://javafx.com/fxml"
fx:controller="com.erikmafo.btviewer.ui.querybox.QueryBoxController">

<CodeArea fx:id="codeArea" onKeyPressed="#onKeyPressedInCodeArea"/>
<HBox alignment="BASELINE_LEFT" styleClass="has-spacing">
<CodeArea fx:id="codeArea" onKeyPressed="#onKeyPressedInCodeArea" VBox.vgrow="ALWAYS"/>
<HBox alignment="BASELINE_LEFT" styleClass="has-spacing, controls" VBox.vgrow="NEVER">
<Button fx:id="executeQueryButton"
text="Execute"
styleClass="btn-success"
Expand All @@ -23,7 +23,7 @@
styleClass="btn-danger"
onAction="#onCancelQueryButtonPressed"/>
<ProgressBar fx:id="progressBar" styleClass="progress-bar-success"/>
<Pane HBox.hgrow="ALWAYS" />
<Pane HBox.hgrow="ALWAYS"/>
<TimerView fx:id="timer"/>
</HBox>

Expand Down

0 comments on commit 99ba69f

Please sign in to comment.