Color the categories.

This commit is contained in:
Mark Nellemann 2023-08-09 07:59:24 +02:00
parent f041684909
commit 43ad3afe55
7 changed files with 73 additions and 49 deletions

View file

@ -3,19 +3,26 @@ package biz.nellemann.mdexpl;
import biz.nellemann.mdexpl.model.NetworkService; import biz.nellemann.mdexpl.model.NetworkService;
import com.gluonhq.charm.glisten.control.CharmListCell; import com.gluonhq.charm.glisten.control.CharmListCell;
import com.gluonhq.charm.glisten.control.ListTile; import com.gluonhq.charm.glisten.control.ListTile;
import javafx.scene.image.ImageView; import javafx.scene.Node;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
public class NetworkServiceCell extends CharmListCell<NetworkService> { public class NetworkServiceCell extends CharmListCell<NetworkService> {
private final ListTile tile; private final ListTile tile;
private final ImageView imageView; //private final ImageView imageView;
private final Rectangle icon;
public NetworkServiceCell() { public NetworkServiceCell() {
this.tile = new ListTile(); this.tile = new ListTile();
imageView = new ImageView(); //imageView = new ImageView();
imageView.setFitHeight(15); //imageView.setFitHeight(15);
imageView.setFitWidth(25); //imageView.setFitWidth(25);
tile.setPrimaryGraphic(imageView); //tile.setPrimaryGraphic(imageView);
icon = new Rectangle();
icon.setHeight(25);
icon.setWidth(25);
tile.setPrimaryGraphic(icon);
tile.setOnMouseClicked(e -> { System.out.println("Selected -> " + itemProperty().get().getName() ); }); tile.setOnMouseClicked(e -> { System.out.println("Selected -> " + itemProperty().get().getName() ); });
setText(null); setText(null);
} }
@ -24,13 +31,10 @@ public class NetworkServiceCell extends CharmListCell<NetworkService> {
public void updateItem(NetworkService item, boolean empty) { public void updateItem(NetworkService item, boolean empty) {
super.updateItem(item, empty); super.updateItem(item, empty);
if (item != null && !empty) { if (item != null && !empty) {
tile.textProperty().setAll(item.getName() + " (" + item.getType() + ")", tile.textProperty().setAll(item.getName(),
"App: " + item.getApp(), "URL: " + item.getUrl() "App: " + item.getApp(), "URL: " + item.getUrl()
); );
//final Image image = Devices.getImage(item.getFlag()); icon.setFill(item.getColor());
/*if (image != null) {
imageView.setImage(image);
}*/
setGraphic(tile); setGraphic(tile);
} else { } else {
setGraphic(null); setGraphic(null);

View file

@ -3,6 +3,7 @@ package biz.nellemann.mdexpl;
import biz.nellemann.mdexpl.model.NetworkService; import biz.nellemann.mdexpl.model.NetworkService;
import javafx.application.Platform; import javafx.application.Platform;
import javafx.collections.ObservableList; import javafx.collections.ObservableList;
import javafx.scene.paint.Color;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -14,14 +15,16 @@ public class NetworkServiceListener implements ServiceListener {
private static final Logger log = LoggerFactory.getLogger(NetworkServiceListener.class); private static final Logger log = LoggerFactory.getLogger(NetworkServiceListener.class);
private final String serviceType; private final String service;
private final ObservableList<NetworkService> observableList; private final ObservableList<NetworkService> observableList;
public NetworkServiceListener(String type, ObservableList<NetworkService> observableList) { private final Color color;
log.info("NetworkServiceListener() - type: {}", type);
this.serviceType = type;
this.observableList = observableList;
public NetworkServiceListener(String service, ObservableList<NetworkService> observableList, Color color) {
log.info("NetworkServiceListener() - type: {}", service);
this.service = service;
this.observableList = observableList;
this.color = color;
} }
@Override @Override
@ -34,7 +37,7 @@ public class NetworkServiceListener implements ServiceListener {
if (serviceInfo != null) { if (serviceInfo != null) {
String name = serviceInfo.getName(); String name = serviceInfo.getName();
log.info("serviceRemoved() - Service: " + name); log.info("serviceRemoved() - Service: " + name);
NetworkService oldNetworkService = new NetworkService(name, serviceType, serviceInfo.getApplication(), serviceInfo.getURLs()[0]); NetworkService oldNetworkService = new NetworkService(name, service, serviceInfo.getApplication(), serviceInfo.getURLs()[0], color);
Platform.runLater(() -> { Platform.runLater(() -> {
observableList.remove(oldNetworkService); observableList.remove(oldNetworkService);
}); });
@ -49,7 +52,7 @@ public class NetworkServiceListener implements ServiceListener {
String name = serviceInfo.getName(); String name = serviceInfo.getName();
String app = serviceInfo.getApplication(); String app = serviceInfo.getApplication();
log.info("serviceResolved() - Service: {} - {} with url {}", app, name, url); log.info("serviceResolved() - Service: {} - {} with url {}", app, name, url);
NetworkService newNetworkService = new NetworkService(name, serviceType, app, url); NetworkService newNetworkService = new NetworkService(name, service, app, url, color);
Platform.runLater(() -> { Platform.runLater(() -> {
if(!observableList.contains(newNetworkService)) { if(!observableList.contains(newNetworkService)) {
observableList.add(newNetworkService); observableList.add(newNetworkService);

View file

@ -1,17 +1,21 @@
package biz.nellemann.mdexpl.model; package biz.nellemann.mdexpl.model;
import javafx.scene.paint.Color;
public class NetworkService { public class NetworkService {
private String name; private String name;
private String type; private String type;
private String app; private String app;
private String url; private String url;
private Color color;
public NetworkService(String name, String type, String app, String url) { public NetworkService(String name, String type, String app, String url, Color color) {
this.name = name; this.name = name;
this.type = type; this.type = type;
this.app = app; this.app = app;
this.url = url; this.url = url;
this.color = color;
} }
public String getName() { public String getName() {
@ -47,6 +51,15 @@ public class NetworkService {
this.url = url; this.url = url;
} }
public Color getColor() {
return color;
}
public void setColor(Color color) {
this.color = color;
}
@Override @Override
public String toString() { public String toString() {
return name + " (" + type + "), app=" + app + ", url=" + url; return name + " (" + type + "), app=" + app + ", url=" + url;

View file

@ -3,6 +3,7 @@ package biz.nellemann.mdexpl.service;
import biz.nellemann.mdexpl.NetworkServiceListener; import biz.nellemann.mdexpl.NetworkServiceListener;
import biz.nellemann.mdexpl.model.NetworkService; import biz.nellemann.mdexpl.model.NetworkService;
import javafx.collections.ObservableList; import javafx.collections.ObservableList;
import javafx.scene.paint.Color;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -11,7 +12,9 @@ import javax.inject.Singleton;
import javax.jmdns.JmDNS; import javax.jmdns.JmDNS;
import java.io.IOException; import java.io.IOException;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
@Singleton @Singleton
@ -23,12 +26,34 @@ public class DiscoveryService {
private JmDNS jmdns; private JmDNS jmdns;
// From: http://www.dns-sd.org/serviceTypes.html + some guesses /*private final HashMap<String, Color> services = HashMap<>({
private final List<String> services = Arrays.asList(
"http", "https", "upnp", "ssh", "sip", "rdp", "ntp", "rdp", "rtsp", "http", "https", "upnp", "ssh", "sip", "rdp", "ntp", "rdp", "rtsp",
"ntp", "smb", "nfs", "llrp", "ftp", "ep", "daap", "ipp", "ipps", "ntp", "smb", "nfs", "llrp", "ftp", "ep", "daap", "ipp", "ipps",
"googlecast", "sonos", "airplay", "smartenergy", "skype", "bittorrent" "googlecast", "sonos", "airplay", "smartenergy", "skype", "bittorrent"
); );*/
// From: http://www.dns-sd.org/serviceTypes.html + some guesses
private final Map<String, Color> services = new HashMap<>() {{
put("http", Color.BLUE);
put("https", Color.DARKBLUE);
put("googlecast", Color.RED);
put("airplay", Color.SLATEGRAY);
put("sonos", Color.SANDYBROWN);
put("ipp", Color.LIGHTGRAY);
put("ipps", Color.LIGHTGRAY);
put("nfs", Color.CORAL);
put("smb", Color.CORAL);
put("cifs", Color.CORAL);
put("smartenergy", Color.LIGHTGREEN);
put("sip", Color.YELLOW);
put("skype", Color.YELLOW);
}};
@PostConstruct @PostConstruct
public void initialize() { public void initialize() {
@ -40,18 +65,14 @@ public class DiscoveryService {
} }
} }
public void register() {
}
public void setObservableList(ObservableList<NetworkService> list) { public void setObservableList(ObservableList<NetworkService> list) {
this.observableList = list; this.observableList = list;
services.forEach(service -> { services.forEach((item, color) -> {
String serviceType = String.format("_%s._%s.local.", service, "tcp"); String service = String.format("_%s._%s.local.", item, "tcp");
NetworkServiceListener networkServiceListener = new NetworkServiceListener(serviceType, observableList); NetworkServiceListener networkServiceListener = new NetworkServiceListener(service, observableList, color);
jmdns.addServiceListener(serviceType, networkServiceListener); jmdns.addServiceListener(service, networkServiceListener);
}); });
} }
} }

View file

@ -68,12 +68,6 @@ public class MainPresenter {
} }
@FXML
protected void onButtonRefresh() {
log.info("onButtonRefresh()");
}
public void onEventShowing(LifecycleEvent lifecycleEvent) { public void onEventShowing(LifecycleEvent lifecycleEvent) {
} }

View file

@ -8,7 +8,7 @@
<?import javafx.scene.layout.VBox?> <?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?> <?import javafx.scene.text.Font?>
<View fx:id="about" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" xmlns="http://javafx.com/javafx/20.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="biz.nellemann.mdexpl.view.AboutPresenter"> <View fx:id="about" xmlns="http://javafx.com/javafx/20.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="biz.nellemann.mdexpl.view.AboutPresenter">
<VBox alignment="CENTER"> <VBox alignment="CENTER">
<Label alignment="CENTER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="-Infinity" minWidth="-Infinity" text="mDNS Explorer"> <Label alignment="CENTER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="-Infinity" minWidth="-Infinity" text="mDNS Explorer">

View file

@ -7,19 +7,8 @@
<?import com.gluonhq.charm.glisten.mvc.View?> <?import com.gluonhq.charm.glisten.mvc.View?>
<?import javafx.scene.layout.BorderPane?> <?import javafx.scene.layout.BorderPane?>
<View fx:id="main" onHiding="#onEventHiding" onShowing="#onEventShowing" prefHeight="800.0" prefWidth="1280.0" xmlns="http://javafx.com/javafx/20.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="biz.nellemann.mdexpl.view.MainPresenter"> <View fx:id="main" onHiding="#onEventHiding" onShowing="#onEventShowing" xmlns="http://javafx.com/javafx/20.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="biz.nellemann.mdexpl.view.MainPresenter">
<bottom>
<BottomNavigation>
<BottomNavigationButton onAction="#onButtonRefresh" selected="true" text="Refresh">
<graphic>
<Icon content="REFRESH" />
</graphic>
</BottomNavigationButton>
</BottomNavigation>
</bottom>
<center> <center>
<CharmListView fx:id="charmListView" BorderPane.alignment="CENTER" /> <CharmListView fx:id="charmListView" BorderPane.alignment="CENTER" />
</center> </center>