Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor pane builder #873

Merged
merged 2 commits into from
Oct 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions common/src/main/java/org/astraea/common/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,17 @@ private static Object reflectionAttribute(Class<?> clz, Object object, String at
TreeMap::new);
}

public static <T, K, U> Collector<T, ?, LinkedHashMap<K, U>> toLinkedHashMap(
Function<? super T, K> keyMapper, Function<? super T, U> valueMapper) {
return Collectors.toMap(
keyMapper,
valueMapper,
(x, y) -> {
throw new IllegalStateException("Duplicate key");
},
LinkedHashMap::new);
}

public static Set<String> constants(Class<?> clz, Predicate<String> variableNameFilter) {
return Arrays.stream(clz.getFields())
.filter(field -> variableNameFilter.test(field.getName()))
Expand Down
50 changes: 25 additions & 25 deletions gui/src/main/java/org/astraea/gui/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
*/
package org.astraea.gui;

import java.util.List;
import javafx.application.Application;
import javafx.geometry.Side;
import javafx.scene.Scene;
import javafx.scene.control.TabPane;
import javafx.stage.Stage;
import org.astraea.gui.pane.TabPane;
import org.astraea.gui.tab.AboutTab;
import org.astraea.gui.tab.BalancerTab;
import org.astraea.gui.tab.BrokerTab;
Expand Down Expand Up @@ -56,32 +56,32 @@ public static class App extends Application {
@Override
public void start(Stage stage) {
var context = new Context();
var rootPane =
new TabPane(
SettingTab.of(context),
BrokerTab.of(context),
MetricsTab.of(context),
TopicTab.of(context),
PartitionTab.of(context),
ConfigTab.of(context),
ConsumerTab.of(context),
ProducerTab.of(context),
TransactionTab.of(context),
MovingReplicaTab.of(context),
CreateTopicTab.of(context),
UpdateTopicTab.of(context),
MoveTopicTab.of(context),
UpdateBrokerTab.of(context),
BalancerTab.of(context),
TruncateRecordTab.of(context),
AboutTab.of(context));
rootPane.setSide(Side.BOTTOM);
rootPane.setTabClosingPolicy(TabPane.TabClosingPolicy.UNAVAILABLE);
var scene = new Scene(rootPane, 300, 300);
stage.setTitle("Astraea");
stage.setHeight(1000);
stage.setWidth(1200);
stage.setScene(scene);
stage.setScene(
new Scene(
TabPane.of(
List.of(
SettingTab.of(context),
BrokerTab.of(context),
MetricsTab.of(context),
TopicTab.of(context),
PartitionTab.of(context),
ConfigTab.of(context),
ConsumerTab.of(context),
ProducerTab.of(context),
TransactionTab.of(context),
MovingReplicaTab.of(context),
CreateTopicTab.of(context),
UpdateTopicTab.of(context),
MoveTopicTab.of(context),
UpdateBrokerTab.of(context),
BalancerTab.of(context),
TruncateRecordTab.of(context),
AboutTab.of(context))),
300,
300));
stage.show();
}

Expand Down
14 changes: 1 addition & 13 deletions gui/src/main/java/org/astraea/gui/box/HBox.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,7 @@ public static HBox of(Pos pos, Node... nodes) {
return pane;
}

public HBox() {
super();
}

public HBox(double spacing) {
private HBox(double spacing) {
super(spacing);
}

public HBox(Node... children) {
super(children);
}

public HBox(double spacing, Node... children) {
super(spacing, children);
}
}
14 changes: 1 addition & 13 deletions gui/src/main/java/org/astraea/gui/box/VBox.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,7 @@ public static VBox of(Pos pos, Node... nodes) {
return pane;
}

public VBox() {
super();
}

public VBox(double spacing) {
private VBox(double spacing) {
super(spacing);
}

public VBox(Node... children) {
super(children);
}

public VBox(double spacing, Node... children) {
super(spacing, children);
}
}
6 changes: 5 additions & 1 deletion gui/src/main/java/org/astraea/gui/button/Button.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@

public class Button extends javafx.scene.control.Button {

public Button(String topic) {
public static Button of(String name) {
return new Button(name);
}

private Button(String topic) {
super(topic);
}

Expand Down
2 changes: 1 addition & 1 deletion gui/src/main/java/org/astraea/gui/button/RadioButton.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public static List<RadioButton> single(List<? extends RadioButtonAble> objs) {

private final RadioButtonAble obj;

public RadioButton(RadioButtonAble obj) {
private RadioButton(RadioButtonAble obj) {
super(obj.display());
this.obj = obj;
}
Expand Down
71 changes: 71 additions & 0 deletions gui/src/main/java/org/astraea/gui/pane/GridPane.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.astraea.gui.pane;

import java.util.Map;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Node;

public class GridPane extends javafx.scene.layout.GridPane {

public static GridPane of(Map<? extends Node, ? extends Node> nodePair, int maxPairOneLine) {
var pane = new GridPane();
pane.setAlignment(Pos.CENTER);
var row = 0;
var column = 0;
var count = 0;
for (var pair : nodePair.entrySet()) {
if (count >= maxPairOneLine) {
count = 0;
column = 0;
row++;
}
GridPane.setHalignment(pair.getKey(), HPos.RIGHT);
GridPane.setMargin(pair.getKey(), new Insets(10, 5, 10, 15));
pane.add(pair.getKey(), column++, row);
GridPane.setHalignment(pair.getValue(), HPos.LEFT);
GridPane.setMargin(pair.getValue(), new Insets(10, 15, 10, 5));
pane.add(pair.getValue(), column++, row);
count++;
}
return pane;
}

public static GridPane singleColumn(
Map<? extends Node, ? extends Node> nodePair, int maxPairOneLine) {
var pane = new GridPane();
pane.setAlignment(Pos.CENTER);
var row = 0;
var column = 0;
var count = 0;
for (var pair : nodePair.entrySet()) {
GridPane.setHalignment(pair.getKey(), HPos.RIGHT);
GridPane.setMargin(pair.getKey(), new Insets(10, 5, 10, 15));
pane.add(pair.getKey(), 0, row);

GridPane.setHalignment(pair.getValue(), HPos.LEFT);
GridPane.setMargin(pair.getValue(), new Insets(10, 15, 10, 5));
pane.add(pair.getValue(), 1, row);
row++;
}
return pane;
}

private GridPane() {}
}
111 changes: 27 additions & 84 deletions gui/src/main/java/org/astraea/gui/pane/PaneBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,23 @@
import java.util.Set;
import java.util.concurrent.CompletionStage;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.control.Label;
import javafx.scene.input.KeyCode;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Pane;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import org.astraea.common.LinkedHashMap;
import org.astraea.common.Utils;
import org.astraea.gui.Logger;
import org.astraea.gui.box.HBox;
import org.astraea.gui.box.VBox;
import org.astraea.gui.button.Button;
import org.astraea.gui.button.RadioButton;
import org.astraea.gui.button.RadioButtonAble;
import org.astraea.gui.table.TableView;
import org.astraea.gui.text.Label;
import org.astraea.gui.text.TextArea;
import org.astraea.gui.text.TextField;

Expand All @@ -57,21 +52,19 @@ public static PaneBuilder of() {
return new PaneBuilder();
}

private static final int MAX_NUMBER_OF_TEXT_FIELD_ONE_LINE = 3;

private List<RadioButton> radioButtons = new ArrayList<>();

private final Set<String> textKeys = new LinkedHashSet<>();
private final Map<String, Boolean> textPriority = new LinkedHashMap<>();
private final Map<String, Boolean> textNumberOnly = new LinkedHashMap<>();
private Label searchLabel = null;
private final TextField searchField = new TextField();
private final TextField searchField = TextField.of();

private Button actionButton = new Button("SEARCH");
private Button actionButton = Button.of("SEARCH");

private TableView tableView = null;

private final TextArea console = new TextArea();
private final TextArea console = TextArea.of();

private BiFunction<Input, Logger, CompletionStage<List<Map<String, Object>>>> buttonAction = null;
private BiFunction<Input, Logger, CompletionStage<Void>> buttonListener = null;
Expand Down Expand Up @@ -115,12 +108,12 @@ public PaneBuilder input(Set<String> keys) {
}

public PaneBuilder searchField(String hint) {
searchLabel = new Label(hint);
searchLabel = Label.of(hint);
return this;
}

public PaneBuilder buttonName(String name) {
actionButton = new Button(name);
actionButton = Button.of(name);
return this;
}

Expand All @@ -140,12 +133,27 @@ public PaneBuilder buttonListener(
public Pane build() {
var nodes = new ArrayList<Node>();
if (!radioButtons.isEmpty()) nodes.add(HBox.of(Pos.CENTER, radioButtons.toArray(Node[]::new)));
var textFields = new LinkedHashMap<String, Supplier<String>>();
Map<String, Supplier<String>> textFields;
if (!textKeys.isEmpty()) {
var paneAndFields = pane(textKeys, textPriority, textNumberOnly);
nodes.add(paneAndFields.getKey());
textFields.putAll(paneAndFields.getValue());
}
var pairs =
textKeys.stream()
.collect(
Utils.toLinkedHashMap(
key ->
textPriority.getOrDefault(key, false)
? Label.highlight(key)
: Label.of(key),
key ->
textNumberOnly.getOrDefault(key, false)
? TextField.onlyNumber()
: TextField.of()));
var gridPane = pairs.size() <= 3 ? GridPane.singleColumn(pairs, 3) : GridPane.of(pairs, 3);
nodes.add(gridPane);
textFields =
pairs.entrySet().stream()
.collect(
Utils.toLinkedHashMap(e -> e.getKey().key(), e -> () -> e.getValue().getText()));
} else textFields = Map.of();
if (searchLabel != null) nodes.add(HBox.of(Pos.CENTER, searchLabel, searchField, actionButton));
else nodes.add(actionButton);
if (tableView != null) nodes.add(tableView);
Expand Down Expand Up @@ -252,71 +260,6 @@ public boolean matchSearch(String word) {
return VBox.of(Pos.CENTER, nodes.toArray(Node[]::new));
}

private static Map.Entry<Pane, LinkedHashMap<String, Supplier<String>>> pane(
Set<String> textKeys,
Map<String, Boolean> textPriority,
Map<String, Boolean> textNumberOnly) {
Function<String, Label> labelFunction =
(key) -> {
if (textPriority.get(key)) {
var label = new Label(key + "*");
label.setFont(Font.font("Verdana", FontWeight.EXTRA_BOLD, 12));
return label;
}
return new Label(key);
};

Function<String, TextField> textFunction =
(key) -> textNumberOnly.get(key) ? TextField.onlyNumber() : new TextField();

var textFields = new LinkedHashMap<String, Supplier<String>>();
if (textKeys.size() <= 3) {
var pane = new GridPane();
pane.setAlignment(Pos.CENTER);
var row = 0;
for (var key : textKeys) {
var label = labelFunction.apply(key);
var textField = textFunction.apply(key);
textFields.put(key, textField::getText);
GridPane.setHalignment(label, HPos.RIGHT);
GridPane.setMargin(label, new Insets(10, 5, 10, 15));
pane.add(label, 0, row);

GridPane.setHalignment(textField, HPos.LEFT);
GridPane.setMargin(textField, new Insets(10, 15, 10, 5));
pane.add(textField, 1, row);
row++;
}
return Map.entry(pane, textFields);
}

var pane = new GridPane();
pane.setAlignment(Pos.CENTER);
var row = 0;
var column = 0;
var count = 0;
for (var key : textKeys) {
if (count >= MAX_NUMBER_OF_TEXT_FIELD_ONE_LINE) {
count = 0;
column = 0;
row++;
}

var label = labelFunction.apply(key);
var textField = textFunction.apply(key);
textFields.put(key, textField::getText);
GridPane.setHalignment(label, HPos.RIGHT);
GridPane.setMargin(label, new Insets(10, 5, 10, 15));
pane.add(label, column++, row);

GridPane.setHalignment(textField, HPos.LEFT);
GridPane.setMargin(textField, new Insets(10, 15, 10, 5));
pane.add(textField, column++, row);
count++;
}
return Map.entry(pane, textFields);
}

static Pattern wildcardToPattern(String string) {
return Pattern.compile(
string.replaceAll("\\?", ".").replaceAll("\\*", ".*"), Pattern.CASE_INSENSITIVE);
Expand Down
Loading