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 com.gluonhq.charm.glisten.control.CharmListCell;
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> {
private final ListTile tile;
private final ImageView imageView;
//private final ImageView imageView;
private final Rectangle icon;
public NetworkServiceCell() {
this.tile = new ListTile();
imageView = new ImageView();
imageView.setFitHeight(15);
imageView.setFitWidth(25);
tile.setPrimaryGraphic(imageView);
//imageView = new ImageView();
//imageView.setFitHeight(15);
//imageView.setFitWidth(25);
//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() ); });
setText(null);
}
@ -24,13 +31,10 @@ public class NetworkServiceCell extends CharmListCell<NetworkService> {
public void updateItem(NetworkService item, boolean empty) {
super.updateItem(item, empty);
if (item != null && !empty) {
tile.textProperty().setAll(item.getName() + " (" + item.getType() + ")",
tile.textProperty().setAll(item.getName(),
"App: " + item.getApp(), "URL: " + item.getUrl()
);
//final Image image = Devices.getImage(item.getFlag());
/*if (image != null) {
imageView.setImage(image);
}*/
icon.setFill(item.getColor());
setGraphic(tile);
} else {
setGraphic(null);

View file

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

View file

@ -1,17 +1,21 @@
package biz.nellemann.mdexpl.model;
import javafx.scene.paint.Color;
public class NetworkService {
private String name;
private String type;
private String app;
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.type = type;
this.app = app;
this.url = url;
this.color = color;
}
public String getName() {
@ -47,6 +51,15 @@ public class NetworkService {
this.url = url;
}
public Color getColor() {
return color;
}
public void setColor(Color color) {
this.color = color;
}
@Override
public String toString() {
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.model.NetworkService;
import javafx.collections.ObservableList;
import javafx.scene.paint.Color;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -11,7 +12,9 @@ import javax.inject.Singleton;
import javax.jmdns.JmDNS;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Singleton
@ -23,12 +26,34 @@ public class DiscoveryService {
private JmDNS jmdns;
// From: http://www.dns-sd.org/serviceTypes.html + some guesses
private final List<String> services = Arrays.asList(
/*private final HashMap<String, Color> services = HashMap<>({
"http", "https", "upnp", "ssh", "sip", "rdp", "ntp", "rdp", "rtsp",
"ntp", "smb", "nfs", "llrp", "ftp", "ep", "daap", "ipp", "ipps",
"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
public void initialize() {
@ -40,18 +65,14 @@ public class DiscoveryService {
}
}
public void register() {
}
public void setObservableList(ObservableList<NetworkService> list) {
this.observableList = list;
services.forEach(service -> {
String serviceType = String.format("_%s._%s.local.", service, "tcp");
NetworkServiceListener networkServiceListener = new NetworkServiceListener(serviceType, observableList);
jmdns.addServiceListener(serviceType, networkServiceListener);
services.forEach((item, color) -> {
String service = String.format("_%s._%s.local.", item, "tcp");
NetworkServiceListener networkServiceListener = new NetworkServiceListener(service, observableList, color);
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) {
}

View file

@ -8,7 +8,7 @@
<?import javafx.scene.layout.VBox?>
<?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">
<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 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>
<CharmListView fx:id="charmListView" BorderPane.alignment="CENTER" />
</center>