Revert "Added a web browser"

This reverts commit 3f5a39ff8543017a55996bbd1f64142d1af65116.
This commit is contained in:
Robert Vokac 2024-04-30 18:29:57 +02:00
parent 3f5a39ff85
commit 63b59b562f
No known key found for this signature in database
GPG Key ID: C459E1E4B4A986BB
8 changed files with 170 additions and 233 deletions

View File

@ -72,17 +72,6 @@
<artifactId>flatlaf</artifactId>
<version>3.4.1</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>21</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-web</artifactId>
<version>21</version>
</dependency>
</dependencies>
</project>

View File

@ -426,12 +426,6 @@ public class TimeCalcConfiguration {
}
return mapOfProperties.get(timeCalcProperty);
}
public String getProperty(String key) {
return timeCalcProperties.getString(key);
}
public void setProperty(String key, String value) {
timeCalcProperties.setString(key, value);
}
public String print() {
StringBuilder sb = new StringBuilder();

View File

@ -83,14 +83,6 @@ public class TimeCalcProperties {
getDefaultStringValue(timeCalcProperty)));
}
public String getString(String key) {
return (String) properties.get(key);
}
public void setString(String key, String value) {
properties.put(key, value);
}
private String getDefaultStringValue(TimeCalcProperty timeCalcProperty) {
if (!defaultProperties
.containsKey(timeCalcProperty.getKey())) {

View File

@ -101,11 +101,7 @@ public class Widget extends JPanel implements
private WidgetMenu widgetMenu = null;
public Widget() {
setBackground(BACKGROUND_COLOR);
int timerDelay = getTimerDelay();
// if(timerDelay > 10) {
// timerDelay = 10;
// }
new Timer(timerDelay, e -> repaint()).start();
new Timer(getTimerDelay(), e -> repaint()).start();
this.addMouseMotionListener(new MouseMotionListener() {
@Override
public void mouseDragged(MouseEvent e) {
@ -212,7 +208,7 @@ public class Widget extends JPanel implements
}
public int getTimerDelay() {
return 10;
return 100;
}
// @Override

View File

@ -104,7 +104,7 @@ public class MainWindow extends TWindow {
private final TButton restartButton;
private final TButton focusButton;
private final TButton helpButton;
private final TButton webBrowserButton;
private final TButton weatherButton;
private final TButton commandButton;
private final TButton jokeButton;
private final AboutButton aboutButton;
@ -254,7 +254,7 @@ public class MainWindow extends TWindow {
this.exitButton = new TButton("Exit");
this.focusButton = new TButton(allowOnlyBasicFeaturesProperty.getValue() ? " " : "Focus");
this.helpButton = new TButton("Help");
this.webBrowserButton = new TButton("Browse");
this.weatherButton = new TButton("Weather");
this.commandButton = new TButton("Command");
this.jokeButton = new TButton("Joke");
this.aboutButton = new AboutButton();
@ -360,7 +360,7 @@ public class MainWindow extends TWindow {
add(walkingHumanProgress);
walkingHumanProgress.visibleProperty
.bindTo(timeCalcConfiguration.walkingHumanVisibleProperty);
webBrowserButton
weatherButton
.setBounds(SwingUtils.MARGIN, walkingHumanProgress.getY()
+ walkingHumanProgress.getHeight());
@ -631,7 +631,6 @@ public class MainWindow extends TWindow {
add(elapsedTextField);
add(remainingTextFieldLabel);
add(remainingTextField);
add(webBrowserButton);
}
if(!allowOnlyBasicFeaturesProperty.getValue()) {
add(saveButton);
@ -673,7 +672,6 @@ public class MainWindow extends TWindow {
configButton.setBoundsFromTop(weekLabel);
workDaysButton.setBoundsFromLeft(configButton);
activitiesButton.setBoundsFromLeft(workDaysButton);
webBrowserButton.setBoundsFromLeft(activitiesButton);
exitButton.setBounds(saveButton.getX() + saveButton.getWidth() - activitiesButton.getWidth(), workDaysButton.getY(), activitiesButton.getWidth(), activitiesButton.getHeight());
restartButton.setBounds(exitButton.getX() - SwingUtils.MARGIN - activitiesButton.getWidth(), activitiesButton.getY(), activitiesButton.getWidth(), activitiesButton.getHeight());
@ -696,8 +694,8 @@ public class MainWindow extends TWindow {
setTitle(getWindowTitle());
webBrowserButton
.addActionListener(e -> WebBrowser.show(timeCalcConfiguration));
weatherButton
.addActionListener(e -> new WeatherWindow().setVisible(true));
commandButton.addActionListener(
new CommandActionListener(timeCalcApp, timeCalcConfiguration));

View File

@ -0,0 +1,160 @@
package org.nanoboot.utils.timecalc.swing.windows;
import org.nanoboot.utils.timecalc.utils.common.Utils;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.io.StringWriter;
import java.net.HttpURLConnection;
import java.net.URL;
/**
* @author Robert Vokac
* @since 16.02.2024
*/
public class WeatherWindow extends JFrame {
public WeatherWindow() {
this.setSize(400, 300);
JEditorPane jep = new JEditorPane();
jep.setEditable(false);
JScrollPane scrollPane = new JScrollPane(jep);
scrollPane.setBounds(10, 10, 750, 550);
getContentPane().add(scrollPane);
// File proxyTxt = new File("proxy.txt");
// if (!proxyTxt.exists()) {
// jep.setText("Sorry, file proxy.txt was not found.");
// return;
// }
// final HttpProxy httpProxy;
// try {
// httpProxy = new HttpProxy(proxyTxt);
// } catch (RuntimeException e) {
// jep.setContentType("text/html");
// jep.setText(e.getMessage());
// return;
// }
try {
String pocasiHtml = null;
File pocasiHtmlFile = new File("pocasi.html");
try {
pocasiHtml = downloadFile("https://pocasi.seznam.cz/praha");
pocasiHtml = prettyFormatXml(pocasiHtml, 4);
Utils.writeTextToFile(pocasiHtmlFile, pocasiHtml);
} catch (Exception e) {
e.printStackTrace();
pocasiHtml = pocasiHtmlFile.exists()
? Utils.readTextFromFile(pocasiHtmlFile)
: "Sorry, pocasi.html was not found.";
}
{
StringBuilder sb = new StringBuilder();
boolean ogm_detailed_forecast_Started = false;
for (String line : pocasiHtml.split("\\r?\\n|\\r")) {
if (line.contains("ogm-detailed-forecast")) {
ogm_detailed_forecast_Started = true;
}
if (ogm_detailed_forecast_Started && line
.contains("<button aria-label=")) {
String l = line
.trim()
.replace("<button aria-label=\"", "")
.split("class")[0];
sb.append(l);
sb.append("<br>\n\n");
System.out.println("Found another line");
}
}
pocasiHtml = sb.toString();
}
jep.setContentType("text/html");
jep.setText("<html><head><meta charset=\"UTF-8\"></head><body>"
+ pocasiHtml + "</body></html>");
Utils.writeTextToFile(new File("aaa"),
"<html><head><meta charset=\"UTF-8\"></head><body>"
+ pocasiHtml + "</body></html>");
} catch (Exception e) {
e.printStackTrace();
jep.setContentType("text/html");
jep.setText("<html>Could not load " + e.getMessage() + "</html>");
}
}
private static String prettyFormatXml(final String input,
final int indent) {
try {
Source xmlInput = new StreamSource(new StringReader(input));
StringWriter stringWriter = new StringWriter();
StreamResult xmlOutput = new StreamResult(stringWriter);
TransformerFactory transformerFactory
= TransformerFactory.newInstance();
transformerFactory.setAttribute("indent-number", indent);
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.transform(xmlInput, xmlOutput);
return xmlOutput.getWriter().toString();
} catch (Throwable e) {
try {
Source xmlInput = new StreamSource(new StringReader(input));
StringWriter stringWriter = new StringWriter();
StreamResult xmlOutput = new StreamResult(stringWriter);
TransformerFactory transformerFactory
= TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(
"{http://xml.apache.org/xslt}indent-amount",
String.valueOf(indent));
transformer.transform(xmlInput, xmlOutput);
return xmlOutput.getWriter().toString();
} catch (Throwable t) {
return input;
}
}
}
public String downloadFile(String urlString)
throws IOException {
URL url = new URL(urlString);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.connect();
BufferedReader br = null;
StringBuilder sb = new StringBuilder();
if (conn.getResponseCode() == 200) {
br = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
String strCurrentLine;
while ((strCurrentLine = br.readLine()) != null) {
sb.append(strCurrentLine).append("\n");
}
} else {
br = new BufferedReader(
new InputStreamReader(conn.getErrorStream()));
String strCurrentLine;
while ((strCurrentLine = br.readLine()) != null) {
sb.append(strCurrentLine).append("\n");
}
}
return sb.toString();
}
}

View File

@ -1,192 +0,0 @@
package org.nanoboot.utils.timecalc.swing.windows;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.control.ChoiceDialog;
import javafx.scene.control.TextInputDialog;
import javafx.scene.input.KeyCode;
import javafx.scene.layout.VBox;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
import org.nanoboot.utils.timecalc.app.TimeCalcConfiguration;
import org.nanoboot.utils.timecalc.utils.common.Utils;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
/**
* @author pc00289
* @since 26.04.2024
*/
public class WebBrowser extends Application {
private static final String BROWSE = "browse.";
public static final String DIGIT = "DIGIT";
private static Stage stage = null;
private static Map<String, String> urls = null;
private static TimeCalcConfiguration timeCalcConfiguration;
public static void show(TimeCalcConfiguration timeCalcConfigurationIn) {
timeCalcConfiguration = timeCalcConfigurationIn;
if (urls == null) {
urls = new HashMap<>();
for (int i = 0; i <= 9; i++) {
String key = BROWSE + DIGIT + i;
String value = timeCalcConfiguration.getProperty(
key);
//System.out.println(key + "=" + value);
if (value != null && !value.isBlank()) {
urls.put(key, value);
}
}
}
if (stage == null) {
Thread thread = new Thread(WebBrowser::startBrowser);
thread.setDaemon(true);
thread.start();
} else {
Platform.runLater(() -> stage.show());
}
}
private static void startBrowser() {
launch(WebBrowser.class);
}
public void start(Stage primaryStage) {
primaryStage.setTitle("Web Browser");
WebView webView = new WebView();
webView.setContextMenuEnabled(true);
WebEngine engine = webView.getEngine();
engine.setOnError(event -> System.out.println(event.getMessage()));
engine.setOnAlert(event -> System.out.println(event.getData()));
// // local error console
// com.sun.javafx.webkit.WebConsoleListener.setDefaultListener(
// (webview, message, lineNumber, sourceId) -> System.out
// .println("Console: [" + sourceId + ":" + lineNumber + "] " + message));
//
// webView.getEngine().locationProperty().addListener(
// (observable, oldValue, newValue) -> System.out.println(newValue));
engine.load("https://pocasi.cz/praha");
VBox vBox = new VBox(webView);
Scene scene = new Scene(vBox, 1400, 800);
vBox.setMinHeight(800);
webView.setMinHeight(800);
scene.setOnKeyPressed(e -> {
if (e.getCode() == KeyCode.F5) {
engine.reload();
}
if (e.isControlDown() && e.getCode() == KeyCode.R) {
TextInputDialog dialog = new TextInputDialog();
dialog.setTitle("Run website");
Optional<String> result = dialog.showAndWait();
if (result.isPresent()) {
String value = result.get();
if (!value.startsWith("http")) {
value = "https://" + value;
System.out.println("Loading: " + value);
}
engine.load(value);
}
}
if (e.isControlDown() && e.getCode() == KeyCode.S) {
Integer[] digits = new Integer[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
ChoiceDialog<Integer>
choiceDialog = new ChoiceDialog<>(digits[0], digits);
choiceDialog.showAndWait();
Integer selectedItem = choiceDialog.getSelectedItem();
if (selectedItem != null) {
String key = BROWSE + DIGIT + selectedItem;
String value = engine.getLocation();
timeCalcConfiguration.setProperty(key, value);
if (urls.containsKey(key)) {
urls.replace(key, value);
} else {
urls.put(key, value);
}
}
}
if (e.isControlDown() && e.getCode() == KeyCode.W) {
StringBuilder sb = new StringBuilder("These urls are saved: ");
sb.append("\n");
List<String> list = new ArrayList<>();
for (String key : urls.keySet()) {
if (!key.contains(DIGIT)) {
continue;
}
String value = urls.get(key);
if (value == null || value.isBlank()) {
continue;
}
list.add(key.replace(BROWSE, "").replace(DIGIT, "") + "="
+ value + "\n");
}
Collections.sort(list);
for(String s :list) {
sb.append(s);
}
Utils.showNotification(sb.toString(), 30000, 400);
}
for (int i = 0; i <= 9; i++) {
if (!e.getCode().name().contains(DIGIT)) {
break;
}
if (e.getCode().name().equals(DIGIT + i)) {
String key = BROWSE + DIGIT + i;
if (urls.containsKey(key)) {
String value = urls.get(key);
if (value != null && !value.isBlank()) {
if (!value.startsWith("http")) {
value = "https://" + value;
}
engine.load(value);
}
}
}
}
});
primaryStage.setScene(scene);
primaryStage.setTitle("Web Browser");
stage = primaryStage;
stage.show();
Platform.setImplicitExit(false);
stage.setOnCloseRequest(event -> stage.hide());
// Thread t = new Thread(() -> {
// while (true) {
// System.out.println("ahoj" + stage.isShowing());
// try {
// Thread.sleep(100);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// }
// });
// t.setDaemon(true);
// t.start();
}
}

View File

@ -10,9 +10,9 @@
<properties>
<time-calc.version>0.1.0-SNAPSHOT</time-calc.version>
<java.version>21</java.version>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<java.version>1.8</java.version>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<lib.dir>${basedir}/lib</lib.dir>