Skip to content

Commit

Permalink
WPR-11 - Add radio, datalink link16 and icons
Browse files Browse the repository at this point in the history
  • Loading branch information
nadav-yo committed Jan 23, 2024
1 parent 64ae4b7 commit 90ec6ec
Show file tree
Hide file tree
Showing 16 changed files with 259 additions and 10 deletions.
27 changes: 27 additions & 0 deletions src/main/java/org/faulty/wpreplace/models/UnitDataLink.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.faulty.wpreplace.models;

import jakarta.annotation.Nullable;
import lombok.Data;
import org.faulty.wpreplace.utils.LuaWriter;
import org.luaj.vm2.LuaTable;
import org.luaj.vm2.LuaValue;

@Data
public class UnitDataLink {

private final String type;
private final String Link16;

@Nullable
public static UnitDataLink fromLuaGroup(LuaTable luaUnit) {
LuaValue datalinks = luaUnit.get("datalinks");
if (datalinks.isnil()) {
return null;
}
LuaTable link16 = datalinks.checktable().get("Link16").checktable();
return new UnitDataLink(
luaUnit.get("type").tojstring(),
LuaWriter.luaTableToString(link16)
);
}
}
46 changes: 46 additions & 0 deletions src/main/java/org/faulty/wpreplace/models/UnitRadio.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package org.faulty.wpreplace.models;

import lombok.Data;
import org.luaj.vm2.LuaTable;
import org.luaj.vm2.LuaValue;

import java.util.ArrayList;
import java.util.List;

@Data
public class UnitRadio {
private final String type;
private final List<Channel> channels1;
private final List<Channel> channels2;

public static UnitRadio fromLuaGroup(LuaTable luaUnit) {
LuaValue radio = luaUnit.get("Radio");
if (radio.isnil()) {
return null;
}
LuaTable radios = radio.checktable();
return new UnitRadio(
luaUnit.get("type").tojstring(),
getChannels(radios, 1),
getChannels(radios, 2)
);
}

private static List<Channel> getChannels(LuaTable unitRadios, int radio) {
List<Channel> radioChannels = new ArrayList<>();
LuaTable channels = unitRadios.get(radio).checktable().get("channels").checktable();
for (LuaValue key : channels.keys()) {
LuaValue channel = channels.get(key);
if (channel.isint()) {
radioChannels.add(new Channel(key.toint(), channel.toint()));
}
}
return radioChannels;
}

@Data
public static final class Channel {
private final int index;
private final int frequency;
}
}
8 changes: 8 additions & 0 deletions src/main/java/org/faulty/wpreplace/ui/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.stage.Stage;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
Expand All @@ -24,9 +25,16 @@ public void start(Stage primaryStage) throws IOException {
Scene scene = new Scene(fxmlLoader.load(), 320, 240);
primaryStage.setTitle("Select Source Mission");
primaryStage.setScene(scene);
addIcons(primaryStage);
primaryStage.show();
}

public static void addIcons(Stage stage) {
stage.getIcons().add(new Image("/org/faulty/wpreplace/ui/icon256.ico"));
stage.getIcons().add(new Image("/org/faulty/wpreplace/ui/icon48.png"));
stage.getIcons().add(new Image("/org/faulty/wpreplace/ui/icon32.png"));
}


@Override
public void init() {
Expand Down
28 changes: 28 additions & 0 deletions src/main/java/org/faulty/wpreplace/ui/DataLinkController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.faulty.wpreplace.ui;

import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.text.Text;
import lombok.Data;
import org.faulty.wpreplace.models.UnitDataLink;

public class DataLinkController {
@FXML
public Label typeLabel;
public Text link16Details;


public void initialize(UnitDataLink data) {
typeLabel.setText("Type: " + data.getType());

link16Details.setText("Link16:" + data.getLink16());

}

@Data
public static final class Item {
private final String type;
private final int quantity;

}
}
83 changes: 75 additions & 8 deletions src/main/java/org/faulty/wpreplace/ui/GroupDetailsController.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
import javafx.stage.Stage;
import javafx.util.Callback;
import lombok.extern.log4j.Log4j2;
import org.faulty.wpreplace.models.UnitDataLink;
import org.faulty.wpreplace.models.UnitDetails;
import org.faulty.wpreplace.models.UnitPayload;
import org.faulty.wpreplace.models.UnitRadio;
import org.faulty.wpreplace.services.MissionMizService;
import org.faulty.wpreplace.utils.MessageUtils;
import org.faulty.wpreplace.utils.RouteUtils;
Expand All @@ -27,6 +29,8 @@

import java.io.IOException;
import java.math.BigDecimal;
import java.net.URL;
import java.util.function.BiConsumer;

@Log4j2
@Component
Expand Down Expand Up @@ -72,25 +76,37 @@ public void setGroup(String coalition, int countryId, String unitType, int group
TableColumn<UnitDetails, Float> speedColumn = new TableColumn<>("Speed");
speedColumn.setCellValueFactory(new PropertyValueFactory<>("speed"));

TableColumn<UnitDetails, Void> viewColumn = new TableColumn<>("View Payload");
viewColumn.setCellFactory(createButtonCellFactory());
TableColumn<UnitDetails, Void> viewPayload = new TableColumn<>("Payload");
viewPayload.setCellFactory(createButtonCellFactory("View Payload",
getClass().getResource("PayloadDetails.fxml"), this::viewPayloadButtonClick));

dataTable.getColumns().addAll(idColumn, nameColumn, skillColumn, callSignsColumn, unitColumn, xColumn, yColumn, altColumn, headingColumn, speedColumn, viewColumn);
TableColumn<UnitDetails, Void> viewRadios = new TableColumn<>("Radio");
viewRadios.setCellFactory(createButtonCellFactory("View Radio",
getClass().getResource("RadioDetails.fxml"), this::viewRadioButtonClick));

TableColumn<UnitDetails, Void> viewDataLink = new TableColumn<>("DataLink");
viewDataLink.setCellFactory(createButtonCellFactory("View DataLink",
getClass().getResource("DataLinkDetails.fxml"), this::viewDataLinkButtonClick));

dataTable.getColumns().addAll(idColumn, nameColumn, skillColumn, callSignsColumn, unitColumn, xColumn, yColumn,
altColumn, headingColumn, speedColumn, viewPayload, viewRadios, viewDataLink);

dataTable.setItems(routes);
idColumn.setSortType(TableColumn.SortType.ASCENDING);
dataTable.getSortOrder().add(idColumn);
dataTable.sort();
}

private Callback<TableColumn<UnitDetails, Void>, TableCell<UnitDetails, Void>> createButtonCellFactory() {
private Callback<TableColumn<UnitDetails, Void>, TableCell<UnitDetails, Void>> createButtonCellFactory(
String buttonName, URL resource,
BiConsumer<URL, UnitDetails> function) {
return (param) -> new TableCell<>() {
private final Button viewButton = new Button("View Payload");
private final Button viewButton = new Button(buttonName);

{
viewButton.setOnAction(event -> {
UnitDetails data = getTableView().getItems().get(getIndex());
handleViewButtonClick(data);
function.accept(resource, data);
});
}

Expand All @@ -106,9 +122,9 @@ protected void updateItem(Void item, boolean empty) {
};
}

private void handleViewButtonClick(UnitDetails data) {
private void viewPayloadButtonClick(URL resource, UnitDetails data) {
try {
FXMLLoader loader = new FXMLLoader(getClass().getResource("PayloadDetails.fxml"));
FXMLLoader loader = new FXMLLoader(resource);
Parent root = loader.load();

UnitPayload unitPayload = UnitPayload.fromLuaGroup(data.getLuaUnit());
Expand All @@ -119,6 +135,57 @@ private void handleViewButtonClick(UnitDetails data) {
stage.setTitle("Payload for " + data.getName());
stage.initModality(Modality.APPLICATION_MODAL);
stage.setScene(new Scene(root, 800, 600));
App.addIcons(stage);
stage.showAndWait();
} catch (IOException e) {
log.error("Error loading payload", e);
MessageUtils.showError("Error!", "Error loading payload. Check logs for more info.");
}
}

private void viewRadioButtonClick(URL resource, UnitDetails data) {
try {
FXMLLoader loader = new FXMLLoader(resource);
Parent root = loader.load();

UnitRadio unitRadio = UnitRadio.fromLuaGroup(data.getLuaUnit());
if (unitRadio == null) {
MessageUtils.showWarn("NO DATA", "Unit has no configured radio channels");
return;
}
RadioController detailsController = loader.getController();
detailsController.initialize(unitRadio);

Stage stage = new Stage();
stage.setTitle("Radio for " + data.getName());
stage.initModality(Modality.APPLICATION_MODAL);
stage.setScene(new Scene(root, 800, 600));
App.addIcons(stage);
stage.showAndWait();
} catch (IOException e) {
log.error("Error loading payload", e);
MessageUtils.showError("Error!", "Error loading payload. Check logs for more info.");
}
}

private void viewDataLinkButtonClick(URL resource, UnitDetails data) {
try {
FXMLLoader loader = new FXMLLoader(resource);
Parent root = loader.load();

UnitDataLink unitDataLink = UnitDataLink.fromLuaGroup(data.getLuaUnit());
if (unitDataLink == null) {
MessageUtils.showWarn("NO DATA", "Unit has no configured data link");
return;
}
DataLinkController detailsController = loader.getController();
detailsController.initialize(unitDataLink);

Stage stage = new Stage();
stage.setTitle("Radio for " + data.getName());
stage.initModality(Modality.APPLICATION_MODAL);
stage.setScene(new Scene(root, 800, 600));
App.addIcons(stage);
stage.showAndWait();
} catch (IOException e) {
log.error("Error loading payload", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,8 @@ public void loadGroup() {
groupDetailsController.setGroup(coalition, countryId, unitType, groupId);
Stage stage = new Stage();
stage.setTitle("Group Details");
stage.setScene(new Scene(root, 800, 600));
stage.setScene(new Scene(root, 1024, 600));
App.addIcons(stage);
stage.show();
} catch (IOException e) {
throw new RuntimeException(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ private void loadSelectRoute() {
stage.setTitle("Source Details");
stage.setScene(new Scene(root, 800, 600));
Stage currentStage = (Stage) messageText.getScene().getWindow();
App.addIcons(stage);
currentStage.hide();

stage.show();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,5 @@ public void initialize(UnitPayload data) {
public static final class Item {
private final String type;
private final int quantity;

}
}
38 changes: 38 additions & 0 deletions src/main/java/org/faulty/wpreplace/ui/RadioController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.faulty.wpreplace.ui;

import javafx.collections.FXCollections;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import org.faulty.wpreplace.models.UnitRadio;

import java.util.List;

public class RadioController {
@FXML
public Label typeLabel;

@FXML
private TableView<UnitRadio.Channel> radio1ListView;
@FXML
private TableView<UnitRadio.Channel> radio2ListView;


public void initialize(UnitRadio data) {
typeLabel.setText("Type: " + data.getType());

addRadioChannels(radio1ListView, data.getChannels1());
addRadioChannels(radio2ListView, data.getChannels2());
}

private void addRadioChannels(TableView<UnitRadio.Channel> tableView, List<UnitRadio.Channel> channels) {
TableColumn<UnitRadio.Channel, Integer> indexColumn = new TableColumn<>("Channel #");
indexColumn.setCellValueFactory(new PropertyValueFactory<>("index"));
TableColumn<UnitRadio.Channel, Integer> freqColumn = new TableColumn<>("Channel Freq");
freqColumn.setCellValueFactory(new PropertyValueFactory<>("frequency"));
tableView.getColumns().addAll(indexColumn, freqColumn);
tableView.setItems(FXCollections.observableArrayList(channels));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public void copyRoute() {
Stage stage = new Stage();
stage.setTitle("Copy Route to destinations");
stage.setScene(new Scene(root, 400, 300));
App.addIcons(stage);
stage.show();
} catch (IOException e) {
throw new RuntimeException(e);
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/org/faulty/wpreplace/utils/MessageUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ public static void showInfo(String title, String message) {
public static void showError(String title, String message) {
showMessage(Alert.AlertType.ERROR, title, message);
}
public static void showWarn(String title, String message) {
showMessage(Alert.AlertType.WARNING, title, message);
}

private static void showMessage(Alert.AlertType type, String title, String message) {
Alert alert = new Alert(type);
Expand Down
13 changes: 13 additions & 0 deletions src/main/resources/org/faulty/wpreplace/ui/DataLinkDetails.fxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Text?>
<VBox xmlns="http://javafx.com/javafx/17.0.2-ea" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="org.faulty.wpreplace.ui.DataLinkController">
<HBox alignment="CENTER" spacing="10">
<Label fx:id="typeLabel" alignment="CENTER"/>
</HBox>
<Text fx:id="link16Details" text="Link 16 details"/>
</VBox>
17 changes: 17 additions & 0 deletions src/main/resources/org/faulty/wpreplace/ui/RadioDetails.fxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Text?>
<VBox xmlns="http://javafx.com/javafx/17.0.2-ea" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="org.faulty.wpreplace.ui.RadioController">
<HBox alignment="CENTER" spacing="10">
<Label fx:id="typeLabel" alignment="CENTER"/>
</HBox>
<Text text="Radio 1 Channels"/>
<TableView fx:id="radio1ListView" maxHeight="100"/>
<Text text="Radio 2 Channels"/>
<TableView fx:id="radio2ListView" maxHeight="100"/>
</VBox>
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 90ec6ec

Please sign in to comment.