commit b3fe8e732a4350b870b15f442392ecf4909f1998 Author: Michal Date: Thu May 23 15:18:59 2024 +0200 inport diff --git a/jguard_client/app/putty.exe b/jguard_client/app/putty.exe new file mode 100644 index 0000000..9b0c98a Binary files /dev/null and b/jguard_client/app/putty.exe differ diff --git a/jguard_client/app/winbox.exe b/jguard_client/app/winbox.exe new file mode 100644 index 0000000..a1ec449 Binary files /dev/null and b/jguard_client/app/winbox.exe differ diff --git a/jguard_client/build.xml b/jguard_client/build.xml new file mode 100644 index 0000000..c1183a2 --- /dev/null +++ b/jguard_client/build.xml @@ -0,0 +1,73 @@ + + + + + + + + + + + Builds, tests, and runs the project jGuardClient. + + + diff --git a/jguard_client/config b/jguard_client/config new file mode 100644 index 0000000..c4bb296 --- /dev/null +++ b/jguard_client/config @@ -0,0 +1,10 @@ +#Sun Oct 25 22:01:12 CET 2020 +autoconnect=1 +debug=1 +port=1225 +server=localhost +user=michal +volume=50 +start_minimized=0 +password=123555 +sound=1 diff --git a/jguard_client/img/add.png b/jguard_client/img/add.png new file mode 100644 index 0000000..4a5bf9f Binary files /dev/null and b/jguard_client/img/add.png differ diff --git a/jguard_client/img/app.png b/jguard_client/img/app.png new file mode 100644 index 0000000..8de69c3 Binary files /dev/null and b/jguard_client/img/app.png differ diff --git a/jguard_client/img/arrow.png b/jguard_client/img/arrow.png new file mode 100644 index 0000000..2af7b7f Binary files /dev/null and b/jguard_client/img/arrow.png differ diff --git a/jguard_client/img/browser.png b/jguard_client/img/browser.png new file mode 100644 index 0000000..32829c6 Binary files /dev/null and b/jguard_client/img/browser.png differ diff --git a/jguard_client/img/connect.png b/jguard_client/img/connect.png new file mode 100644 index 0000000..5c8e6e3 Binary files /dev/null and b/jguard_client/img/connect.png differ diff --git a/jguard_client/img/cursor_connection.png b/jguard_client/img/cursor_connection.png new file mode 100644 index 0000000..6492f7f Binary files /dev/null and b/jguard_client/img/cursor_connection.png differ diff --git a/jguard_client/img/flag_gray.png b/jguard_client/img/flag_gray.png new file mode 100644 index 0000000..a7e38db Binary files /dev/null and b/jguard_client/img/flag_gray.png differ diff --git a/jguard_client/img/flag_green.png b/jguard_client/img/flag_green.png new file mode 100644 index 0000000..def17cc Binary files /dev/null and b/jguard_client/img/flag_green.png differ diff --git a/jguard_client/img/flag_orange.png b/jguard_client/img/flag_orange.png new file mode 100644 index 0000000..edd3cef Binary files /dev/null and b/jguard_client/img/flag_orange.png differ diff --git a/jguard_client/img/flag_red.png b/jguard_client/img/flag_red.png new file mode 100644 index 0000000..e99a587 Binary files /dev/null and b/jguard_client/img/flag_red.png differ diff --git a/jguard_client/img/lock.png b/jguard_client/img/lock.png new file mode 100644 index 0000000..67bb215 Binary files /dev/null and b/jguard_client/img/lock.png differ diff --git a/jguard_client/img/log.png b/jguard_client/img/log.png new file mode 100644 index 0000000..7d60021 Binary files /dev/null and b/jguard_client/img/log.png differ diff --git a/jguard_client/img/online_clients.png b/jguard_client/img/online_clients.png new file mode 100644 index 0000000..becc247 Binary files /dev/null and b/jguard_client/img/online_clients.png differ diff --git a/jguard_client/img/ping.png b/jguard_client/img/ping.png new file mode 100644 index 0000000..97f86be Binary files /dev/null and b/jguard_client/img/ping.png differ diff --git a/jguard_client/img/platform_android.png b/jguard_client/img/platform_android.png new file mode 100644 index 0000000..3347635 Binary files /dev/null and b/jguard_client/img/platform_android.png differ diff --git a/jguard_client/img/platform_pc.png b/jguard_client/img/platform_pc.png new file mode 100644 index 0000000..f5bad40 Binary files /dev/null and b/jguard_client/img/platform_pc.png differ diff --git a/jguard_client/img/server.png b/jguard_client/img/server.png new file mode 100644 index 0000000..ab747f7 Binary files /dev/null and b/jguard_client/img/server.png differ diff --git a/jguard_client/img/server_offline.png b/jguard_client/img/server_offline.png new file mode 100644 index 0000000..7bfd254 Binary files /dev/null and b/jguard_client/img/server_offline.png differ diff --git a/jguard_client/img/server_online.png b/jguard_client/img/server_online.png new file mode 100644 index 0000000..38abcc6 Binary files /dev/null and b/jguard_client/img/server_online.png differ diff --git a/jguard_client/img/settings.png b/jguard_client/img/settings.png new file mode 100644 index 0000000..f45c23f Binary files /dev/null and b/jguard_client/img/settings.png differ diff --git a/jguard_client/img/ssh.png b/jguard_client/img/ssh.png new file mode 100644 index 0000000..d35b274 Binary files /dev/null and b/jguard_client/img/ssh.png differ diff --git a/jguard_client/img/trash.png b/jguard_client/img/trash.png new file mode 100644 index 0000000..0e634f4 Binary files /dev/null and b/jguard_client/img/trash.png differ diff --git a/jguard_client/img/unlock.png b/jguard_client/img/unlock.png new file mode 100644 index 0000000..fdcb342 Binary files /dev/null and b/jguard_client/img/unlock.png differ diff --git a/jguard_client/img/winbox.png b/jguard_client/img/winbox.png new file mode 100644 index 0000000..492d23e Binary files /dev/null and b/jguard_client/img/winbox.png differ diff --git a/jguard_client/log.txt b/jguard_client/log.txt new file mode 100644 index 0000000..a872fb2 --- /dev/null +++ b/jguard_client/log.txt @@ -0,0 +1,75 @@ +01.11.20 18:27:18 I Client start +01.11.20 18:27:18 I Loading config ... +01.11.20 18:27:18 I successfully +01.11.20 18:27:18 I Automaticke pripojovani +01.11.20 18:27:18 I Pripojuji .... +01.11.20 18:27:20 I Automaticke pripojovani ukonceno +01.11.20 18:27:20 D -> Message type: 1 +01.11.20 18:27:20 D -> Message type: 2 +01.11.20 18:27:20 D -> Message type: 4 +01.11.20 18:27:20 I Pripojeno +01.11.20 18:27:20 D -> Message type: 6 +01.11.20 18:27:20 D -> Message type: 8 +01.11.20 18:27:20 D -> Message type: 9 +01.11.20 18:27:20 D -> Message type: 100 +01.11.20 18:27:20 D Prijata infomace o mape +01.11.20 18:27:20 D -> Message type: 100 +01.11.20 18:27:20 D Prijata infomace o mape +01.11.20 18:27:20 D -> Message type: 100 +01.11.20 18:27:20 D Prijata infomace o mape +01.11.20 18:27:20 D -> Message type: 100 +01.11.20 18:27:20 D Prijata infomace o mape +01.11.20 18:27:20 D -> Message type: 100 +01.11.20 18:27:20 D Prijata infomace o mape +01.11.20 18:27:20 D -> Message type: 206 +01.11.20 18:28:18 D -> Message type: 204 +01.11.20 18:28:18 D -> Message type: 204 +01.11.20 18:28:26 I Server nedostupny: Stávající připojení bylo vynuceně ukončeno vzdáleným hostitelem +01.11.20 18:28:26 I Automaticke pripojovani +01.11.20 18:28:26 I Pripojuji .... +01.11.20 18:28:28 E Client exception: Connection refused: no further information: localhost/127.0.0.1:1225 +01.11.20 18:28:28 I Odpojeno +01.11.20 18:28:31 I Pripojuji .... +01.11.20 18:28:33 E Client exception: Connection refused: no further information: localhost/127.0.0.1:1225 +01.11.20 18:28:33 I Odpojeno +01.11.20 18:28:36 I Pripojuji .... +01.11.20 18:28:36 I Automaticke pripojovani ukonceno +01.11.20 18:28:36 I Odpojeno +01.11.20 18:28:36 D -> Message type: 1 +01.11.20 18:28:36 D -> Message type: 2 +01.11.20 18:28:36 D -> Message type: 4 +01.11.20 18:28:36 I Pripojeno +01.11.20 18:28:36 D -> Message type: 6 +01.11.20 18:28:36 D -> Message type: 8 +01.11.20 18:28:36 D -> Message type: 9 +01.11.20 18:28:36 D -> Message type: 100 +01.11.20 18:28:36 D Prijata infomace o mape +01.11.20 18:28:36 D -> Message type: 100 +01.11.20 18:28:36 D Prijata infomace o mape +01.11.20 18:28:36 D -> Message type: 100 +01.11.20 18:28:36 D Prijata infomace o mape +01.11.20 18:28:36 D -> Message type: 100 +01.11.20 18:28:36 D Prijata infomace o mape +01.11.20 18:28:36 D -> Message type: 100 +01.11.20 18:28:36 D Prijata infomace o mape +01.11.20 18:28:37 D -> Message type: 206 +01.11.20 18:28:38 D -> Message type: 204 +01.11.20 18:28:39 D -> Message type: 204 +01.11.20 18:28:40 D -> Message type: 204 +01.11.20 18:28:40 D -> Message type: 204 +01.11.20 18:28:40 D -> Message type: 204 +01.11.20 18:28:40 D -> Message type: 204 +01.11.20 18:28:40 D -> Message type: 204 +01.11.20 18:28:40 D -> Message type: 204 +01.11.20 18:28:40 D -> Message type: 204 +01.11.20 18:28:40 D -> Message type: 204 +01.11.20 18:28:40 D -> Message type: 204 +01.11.20 18:28:40 D -> Message type: 204 +01.11.20 18:28:40 D -> Message type: 204 +01.11.20 18:28:40 D -> Message type: 204 +01.11.20 18:28:40 D -> Message type: 204 +01.11.20 18:28:40 D -> Message type: 204 +01.11.20 18:28:40 D -> Message type: 204 +01.11.20 18:28:40 D -> Message type: 204 +01.11.20 18:28:40 D -> Message type: 204 +01.11.20 18:28:40 D -> Message type: 204 diff --git a/jguard_client/manifest.mf b/jguard_client/manifest.mf new file mode 100644 index 0000000..328e8e5 --- /dev/null +++ b/jguard_client/manifest.mf @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +X-COMMENT: Main-Class will be added automatically by build + diff --git a/jguard_client/nbproject/build-impl.xml b/jguard_client/nbproject/build-impl.xml new file mode 100644 index 0000000..5e6347b --- /dev/null +++ b/jguard_client/nbproject/build-impl.xml @@ -0,0 +1,1436 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set src.dir + Must set test.src.dir + Must set build.dir + Must set dist.dir + Must set build.classes.dir + Must set dist.javadoc.dir + Must set build.test.classes.dir + Must set build.test.results.dir + Must set build.classes.excludes + Must set dist.jar + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set javac.includes + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + No tests executed. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set JVM to use for profiling in profiler.info.jvm + Must set profiler agent JVM arguments in profiler.info.jvmargs.agent + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select some files in the IDE or set javac.includes + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + To run this application from the command line without Ant, try: + + java -jar "${dist.jar.resolved}" + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set run.class + + + + Must select one file in the IDE or set run.class + + + + + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set debug.class + + + + + Must select one file in the IDE or set debug.class + + + + + Must set fix.includes + + + + + + + + + + This target only works when run from inside the NetBeans IDE. + + + + + + + + + Must select one file in the IDE or set profile.class + This target only works when run from inside the NetBeans IDE. + + + + + + + + + This target only works when run from inside the NetBeans IDE. + + + + + + + + + + + + + This target only works when run from inside the NetBeans IDE. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set run.class + + + + + + Must select some files in the IDE or set test.includes + + + + + Must select one file in the IDE or set run.class + + + + + Must select one file in the IDE or set applet.url + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select some files in the IDE or set javac.includes + + + + + + + + + + + + + + + + + + + + Some tests failed; see details above. + + + + + + + + + Must select some files in the IDE or set test.includes + + + + Some tests failed; see details above. + + + + Must select some files in the IDE or set test.class + Must select some method in the IDE or set test.method + + + + Some tests failed; see details above. + + + + + Must select one file in the IDE or set test.class + + + + Must select one file in the IDE or set test.class + Must select some method in the IDE or set test.method + + + + + + + + + + + + + + Must select one file in the IDE or set applet.url + + + + + + + + + Must select one file in the IDE or set applet.url + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/jguard_client/nbproject/genfiles.properties b/jguard_client/nbproject/genfiles.properties new file mode 100644 index 0000000..6a7334e --- /dev/null +++ b/jguard_client/nbproject/genfiles.properties @@ -0,0 +1,8 @@ +build.xml.data.CRC32=0d9d7eb9 +build.xml.script.CRC32=9dc7d76a +build.xml.stylesheet.CRC32=8064a381@1.80.1.48 +# This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml. +# Do not edit this file. You may delete it but then the IDE will never regenerate such files for you. +nbproject/build-impl.xml.data.CRC32=0d9d7eb9 +nbproject/build-impl.xml.script.CRC32=393f3f72 +nbproject/build-impl.xml.stylesheet.CRC32=830a3534@1.80.1.48 diff --git a/jguard_client/nbproject/project.properties b/jguard_client/nbproject/project.properties new file mode 100644 index 0000000..9729db4 --- /dev/null +++ b/jguard_client/nbproject/project.properties @@ -0,0 +1,85 @@ +annotation.processing.enabled=true +annotation.processing.enabled.in.editor=false +annotation.processing.processors.list= +annotation.processing.run.all.processors=true +annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output +application.title=jGuardClient +application.vendor=Michal +build.classes.dir=${build.dir}/classes +build.classes.excludes=**/*.java,**/*.form +# This directory is removed when the project is cleaned: +build.dir=build +build.generated.dir=${build.dir}/generated +build.generated.sources.dir=${build.dir}/generated-sources +# Only compile against the classpath explicitly listed here: +build.sysclasspath=ignore +build.test.classes.dir=${build.dir}/test/classes +build.test.results.dir=${build.dir}/test/results +# Uncomment to specify the preferred debugger connection transport: +#debug.transport=dt_socket +debug.classpath=\ + ${run.classpath} +debug.test.classpath=\ + ${run.test.classpath} +# Files in build.classes.dir which should be excluded from distribution jar +dist.archive.excludes= +# This directory is removed when the project is cleaned: +dist.dir=dist +dist.jar=${dist.dir}/jGuardClient.jar +dist.javadoc.dir=${dist.dir}/javadoc +endorsed.classpath= +excludes= +file.reference.commons-io-2.6.jar=C:\\Users\\Michal\\Documents\\NetBeansProjects\\JAR lib\\commons-io-2.6.jar +file.reference.netty-all-4.1.33.Final.jar=C:\\Users\\Michal\\Documents\\NetBeansProjects\\JAR lib\\netty-all-4.1.33.Final.jar +file.reference.snmp4j-2.3.4.jar=C:\\Users\\Michal\\Documents\\NetBeansProjects\\JAR lib\\snmp4j-2.3.4.jar +includes=** +jar.compress=false +javac.classpath=\ + ${reference.jGuard.jar}:\ + ${file.reference.netty-all-4.1.33.Final.jar}:\ + ${file.reference.snmp4j-2.3.4.jar}:\ + ${file.reference.commons-io-2.6.jar} +# Space-separated list of extra javac options +javac.compilerargs= +javac.deprecation=false +javac.external.vm=true +javac.processorpath=\ + ${javac.classpath} +javac.source=1.8 +javac.target=1.8 +javac.test.classpath=\ + ${javac.classpath}:\ + ${build.classes.dir} +javac.test.processorpath=\ + ${javac.test.classpath} +javadoc.additionalparam= +javadoc.author=false +javadoc.encoding=${source.encoding} +javadoc.noindex=false +javadoc.nonavbar=false +javadoc.notree=false +javadoc.private=false +javadoc.splitindex=true +javadoc.use=true +javadoc.version=false +javadoc.windowtitle= +main.class=jguard.client.Client +manifest.file=manifest.mf +meta.inf.dir=${src.dir}/META-INF +mkdist.disabled=false +platform.active=default_platform +project.jGuard=../jGuard +reference.jGuard.jar=${project.jGuard}/dist/jGuard.jar +run.classpath=\ + ${javac.classpath}:\ + ${build.classes.dir} +# Space-separated list of JVM arguments used when running the project. +# You may also define separate properties like run-sys-prop.name=value instead of -Dname=value. +# To set system properties for unit tests define test-sys-prop.name=value: +run.jvmargs= +run.test.classpath=\ + ${javac.test.classpath}:\ + ${build.test.classes.dir} +source.encoding=UTF-8 +src.dir=src +test.src.dir=test diff --git a/jguard_client/nbproject/project.xml b/jguard_client/nbproject/project.xml new file mode 100644 index 0000000..3e9fcb7 --- /dev/null +++ b/jguard_client/nbproject/project.xml @@ -0,0 +1,25 @@ + + + org.netbeans.modules.java.j2seproject + + + jGuardClient + + + + + + + + + + jGuard + jar + + jar + clean + jar + + + + diff --git a/jguard_client/sound/8-bit-select-menu-select.wav b/jguard_client/sound/8-bit-select-menu-select.wav new file mode 100644 index 0000000..1b0d4a4 Binary files /dev/null and b/jguard_client/sound/8-bit-select-menu-select.wav differ diff --git a/jguard_client/sound/ding_a.wav b/jguard_client/sound/ding_a.wav new file mode 100644 index 0000000..dc39853 Binary files /dev/null and b/jguard_client/sound/ding_a.wav differ diff --git a/jguard_client/src/jguard/client/AppLauncher.java b/jguard_client/src/jguard/client/AppLauncher.java new file mode 100644 index 0000000..18cdf94 --- /dev/null +++ b/jguard_client/src/jguard/client/AppLauncher.java @@ -0,0 +1,69 @@ +package jguard.client; + +import java.awt.Desktop; +import java.io.IOException; +import java.net.URISyntaxException; +import java.net.URL; +import java.util.ArrayList; +import java.util.List; + +public class AppLauncher { + + public static void winbox(String ip, String port, String user, String password) { + if (port.equals("8291")) { + runApp(new String[]{"app/winbox.exe", ip, user, password}); + } else { + runApp(new String[]{"app/winbox.exe", ip + ":" + port, user, password}); + } + + } + + public static void openWebpage(String address, String port, int verze) { + String urlString = "http://"+address+":"+port; + if (verze == 1) { + urlString = "https://"+address+":"+port; + } + try { + Desktop.getDesktop().browse(new URL(urlString).toURI()); + } catch (URISyntaxException | IOException e) { + e.printStackTrace(); + } + } + + public static void ssh(String address, String username, String password, int port){ + runPutty("-ssh", address, username, password, port); + } + + private static void runPutty(String versionParam, String address, String username, String password, Integer port) { + List params = new ArrayList(); + params.add("app/putty.exe"); + params.add(versionParam); + if (port != null) { + params.add("-P"); + params.add(port.toString()); + } + if (username.equals("")) { + params.add(address); + } else { + params.add(username + "@" + address); + if (!password.equals("")) { + params.add("-pw"); + params.add(password); + } + } + String[] aParams = new String[params.size()]; + runApp((String[]) params.toArray(aParams)); + } + + private static void runApp(String[] run) { + Thread t = new Thread(new Runnable() { + public void run() { + try { + Process p = Runtime.getRuntime().exec(run); + } catch (Exception ex) { + } + } + }); + t.start(); + } +} diff --git a/jguard_client/src/jguard/client/AutoConnect.java b/jguard_client/src/jguard/client/AutoConnect.java new file mode 100644 index 0000000..6d701cc --- /dev/null +++ b/jguard_client/src/jguard/client/AutoConnect.java @@ -0,0 +1,35 @@ +package jguard.client; + +import cucky.jGuard.lib.LogFile; +import java.util.logging.Level; +import java.util.logging.Logger; +import jguard.client.network.NetworkClient; + +public class AutoConnect extends Thread { + + private boolean exit = false; + + @Override + public void run() { + super.run(); + + LogFile.printInfo("Automaticke pripojovani"); + while (!exit) { + Client.client = new NetworkClient(Client.config.getProperty("server"), Integer.valueOf(Client.config.getProperty("port"))); + Client.client.start(); + if (Client.client.isConnected()) { + exit = true; + } else { + try { + Thread.sleep(3000); + } catch (InterruptedException ex) { + Logger.getLogger(AutoConnect.class.getName()).log(Level.SEVERE, null, ex); + } + } + } + + LogFile.printInfo("Automaticke pripojovani ukonceno"); + + } + +} diff --git a/jguard_client/src/jguard/client/Client.java b/jguard_client/src/jguard/client/Client.java new file mode 100644 index 0000000..1dab178 --- /dev/null +++ b/jguard_client/src/jguard/client/Client.java @@ -0,0 +1,123 @@ +package jguard.client; + +import jguard.client.gui.Window; +import cucky.jGuard.lib.LogFile; +import cucky.jGuard.lib.object.ObjectType; +import cucky.jGuard.lib.object.Log; +import cucky.jGuard.lib.object.Map; +import cucky.jGuard.lib.object.OnlineClients; +import cucky.jGuard.lib.object.ServerSettings; +import cucky.jGuard.lib.object.User; +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Properties; +import javax.imageio.ImageIO; +import jguard.client.gui.Tray; +import jguard.client.gui.dialog.LoginDialog; +import jguard.client.network.NetworkClient; + + +public class Client { + + // TODO nazev aplikace jNet + + public static final String APP_NAME = "jGuard Client"; + public static final int VERSION = 26; + + public static Properties config; + // nastaveni serveru + public static ServerSettings serverSettings; + // podrobnosti o uzivateli, např opravnění + public static User user; + // seznam map + public static ArrayList map = new ArrayList(); + // seznam pripojenych klientu + public static ArrayList onlineClients = new ArrayList(); + // log + public static ArrayList log = new ArrayList(); + // pocet offline zarizeni + public static int offline = 0; + + // seznam typu zarizeni + public static ArrayList deviceType; + + + + + public static NetworkClient client = new NetworkClient("", 0); + + public static Window okno = new Window(); + + + + + public static void main(String[] args) { + + LogFile.clear(); + LogFile.printInfo("Client start"); + + LogFile.printInfo("Loading config ..."); + config = new Config().loadAll(); // TODO toto postrada vyznam kdyz se pak pri dotazu znovu otevira soubor + if (config != null) { + LogFile.printInfo(" successfully"); + } else { + LogFile.printInfo(" fail"); + System.exit(0); + } + + /// + /// tray icon + /// + new Tray().createAndShowTray(); + + /// + /// okno + /// + okno.setSize(1024, 800); + okno.setLocationRelativeTo(null); + + /// + /// nastavit ikonu + /// + try { + okno.setIconImage(ImageIO.read(new File("img/app.png"))); + } catch (IOException ex) { + LogFile.printErr("APP icon exception"); + } + + /// + /// spustit minimalozovane ? + /// + if (config.getProperty("start_minimized").equals("1")) { + okno.setVisible(false); + } else { + okno.setVisible(true); + } + /// + /// automatické připojení + /// + if (config.getProperty("autoconnect").equals("1")) { + + Thread t = new AutoConnect(); + t.run(); + /* + Thread t = new Thread(){ + @Override + public void run() { + super.run(); + LogFile.printInfo("Automaticke pripojovani"); + + client = new NetworkClient(config.getProperty("server"), Integer.valueOf(config.getProperty("port"))); + + client.start(); + } + }; + t.run(); +*/ + } else { + new LoginDialog().setVisible(true); + } + } + +} diff --git a/jguard_client/src/jguard/client/ClientMessageParser.java b/jguard_client/src/jguard/client/ClientMessageParser.java new file mode 100644 index 0000000..53684ae --- /dev/null +++ b/jguard_client/src/jguard/client/ClientMessageParser.java @@ -0,0 +1,210 @@ +package jguard.client; + +import cucky.jGuard.lib.LogFile; +import cucky.jGuard.lib.Message; +import cucky.jGuard.lib.Status; +import cucky.jGuard.lib.object.ObjectType; +import cucky.jGuard.lib.object.Map; +import cucky.jGuard.lib.object.MapObject; +import cucky.jGuard.lib.object.ObjectConnection; +import cucky.jGuard.lib.object.OnlineClients; +import cucky.jGuard.lib.object.ServerSettings; +import cucky.jGuard.lib.object.User; +import java.io.IOException; +import java.util.ArrayList; +import static jguard.client.Client.config; +import jguard.client.gui.MapTree; +import jguard.client.gui.MapView; +import jguard.client.gui.Tray; +import jguard.client.gui.Window; +import jguard.client.gui.dialog.LoginDialog; + +public class ClientMessageParser { + + Map map; + MapObject mo; + + public void parse(Message msg) { + + LogFile.printDebug("-> Message type: " + msg.getType()); + + switch (msg.getType()) { + + case Message.CLIENT_VERSION: + Client.client.send(Message.CLIENT_VERSION, Client.VERSION); + break; + + + case Message.UPDATE: + LogFile.printInfo("Přijat požadavek na aktualizaci"); + String[] run = {"java", "-jar", "Updater.jar"}; + // TODO po přijetí pozadavku dialogove okno s informaci ze dojde k aktualizace a odpoctem k jejimu provedeni + try { + // odpoji se + Client.client.disconnect(); + // spusti updater + Runtime.getRuntime().exec(run); + // vypne program + System.exit(0); + } catch (IOException ex) { + LogFile.printErr("Client update exception: " + ex.getMessage()); + } + break; + + + case Message.AUTH_REQUEST: + Window.setStatusBarMessage("Přihlašuji ..."); + String[] userPass = {config.getProperty("user"), config.getProperty("password"), String.valueOf(OnlineClients.PLATFORM_PC)}; + Client.client.send(Message.AUTH, userPass); + break; + + + case Message.AUTH_SUCEFULL: + Window.setStatusBarMessage("Připojeno"); + LogFile.printInfo("Pripojeno"); + // smazání seznamu map + Client.map.clear(); + break; + + + case Message.AUTH_FAIL: + Window.setStatusBarMessage("Chyba autorizace"); + new LoginDialog().setVisible(true); + break; + + + case Message.SERVER_MESSAGE: + Window.showMessage((String) msg.getMsg()); + break; + + + case Message.MAP: + map = (Map) msg.getMsg(); + LogFile.printDebug("Prijata infomace o mape"); + // pridani do arraylistu + Client.map.add(map); + // pridani do stromu + MapTree.add(); + // spocitani offline zarizeni + for (MapObject obj : map.getObjects()) { + if (obj.getStatus() == Status.OFFLINE) { + Client.offline++; + } + } + // aktualizace tray icon + Tray.setState(); + break; + + + case Message.ONLINE_CLIENTS: + // TODO pri kompilaci zpusobuje chybu uses unchecked or unsafe operations. ale nevim proc + Client.onlineClients = (ArrayList) msg.getMsg(); + Window.reloadOnlineClinets(); + break; + + + case Message.SERVER_SETTINGS: + Client.serverSettings = (ServerSettings) msg.getMsg(); + break; + + + case Message.USER_INFO: + Client.user = (User) msg.getMsg(); + break; + + + case Message.SET_MAP_LOCK: + int[] data_lock = (int[]) msg.getMsg(); + MapManager.setLock(data_lock[0], data_lock[1]); + break; + + + case Message.REMOVE_MAP: + int map_deleted = (int) msg.getMsg(); + // odebrani z lokalniho seznamu + for (Map m : Client.map) { + if (m.getId() == map_deleted) { + Client.map.remove(m); + break; + } + } + // aktualizace stromu map + break; + + + case Message.UPDATE_OBJECT: + mo = (MapObject) msg.getMsg(); + // aktualizace v lokalnim seznamu + for (Map map : Client.map) { + map.updateObject(mo); + } + break; + + + case Message.STATE_UPDATE: + int[] data = (int[]) msg.getMsg(); + // aktualizace stavu u objektu + MapManager.setObjectStatus(data[0], data[1], data[2]); + // aktualizace stromu map + MapTree.reload(); + // aktualizace poctu offline zarizeni + if (data[2] == Status.OFFLINE) { + Client.offline++; + } else if (Client.offline < 1) { + Client.offline--; + } + // aktualizace tray icon + Tray.setState(); + break; + + + case Message.ADD_OBJECT_TYPE: + Client.deviceType.add((ObjectType) msg.getMsg()); + System.out.println("jguard.client.ClientMessageParser.parse()"); + break; + + + case Message.ADD_OBJECT: + mo = (MapObject) msg.getMsg(); + // prida do lokalniho seznamu + for (Map m : Client.map) { + if (m.getId() == mo.getMap()) { + m.addObject(mo); + break; + } + } + // je-li mapa otevrena prida ji i tam + if (Client.map.get(Window.currentMapIndex).getId() == mo.getMap()) { + MapView.addObject(mo); + } + break; + + + case Message.LIST_OBJECT_TYPE: + Client.deviceType = (ArrayList) msg.getMsg(); + break; + + + case Message.NEW_CONNECTION: + ObjectConnection oc = (ObjectConnection) msg.getMsg(); + for (Map map : Client.map) { + if (map.getId() == oc.getMap()) { + // pridani do arraylistu + map.addObjectConnection(oc); + break; + } + } + // je-li mapa otevrena prida propojeni i tam + if (Client.map.get(Window.currentMapIndex).getId() == oc.getMap()) { + MapView.addObjectConnection(oc); + } + break; + + + default: + LogFile.printErr("Neznamy typ zpravy"); + } + + } + +} diff --git a/jguard_client/src/jguard/client/Config.java b/jguard_client/src/jguard/client/Config.java new file mode 100644 index 0000000..46607ee --- /dev/null +++ b/jguard_client/src/jguard/client/Config.java @@ -0,0 +1,137 @@ +package jguard.client; + +import cucky.jGuard.lib.LogFile; +import cucky.jGuard.lib.OSValidator; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.util.HashMap; +import java.util.Map; +import java.util.Properties; + +public class Config { + + private Properties prop = new Properties(); + private String configFile = "config"; + private OutputStream output = null; + private InputStream input = null; + + public static Map conf = new HashMap(); + + public Config() { + + } + + private InputStream getFile(String configFile) throws Exception{ + if (OSValidator.isWindows()) { + return new FileInputStream(configFile); + } else if (OSValidator.isUnix()) { + return new FileInputStream("/jGuard/"+configFile); + } + return null; + } + + public String get(String name) { + + try { + + input = getFile(configFile); + + // load a properties file + prop.load(input); + + return prop.getProperty(name); + + } catch (Exception ex) { + LogFile.printErr("Config file exception: " + ex.getLocalizedMessage()); + } finally { + if (input != null) { + try { + input.close(); + } catch (IOException e) { + LogFile.printErr("Config file exception: " + e.getLocalizedMessage()); + } + } + } + return ""; + } + + public void set(String name, String value) { + try { + + output = new FileOutputStream(configFile); + + // set the properties value + prop.setProperty(name, value); + + // save properties to project root folder + prop.store(output, null); + + } catch (IOException io) { + LogFile.printErr("Config file exception: " + io.getLocalizedMessage()); + } finally { + if (output != null) { + try { + output.close(); + } catch (IOException e) { + LogFile.printErr("Config file exception: " + e.getLocalizedMessage()); + } + } + } + } + + public Properties loadAll(){ + try { + + input = getFile(configFile); + + if (input == null) { + LogFile.printErr("Config file error: input is null"); + return null; + } + prop.load(input); + + return prop; + } catch (Exception ex) { + LogFile.printErr("Config file exception: " + ex.getLocalizedMessage()); + return null; + } finally { + if (input != null) { + try { + input.close(); + } catch (IOException e) { + LogFile.printErr("Config file exception: " + e.getLocalizedMessage()); + } + } + } + } + + public void StoreLogin(){ + + } + + public void store(Properties prop){ + try { + + output = new FileOutputStream(configFile); + + // save properties to project root folder + prop.store(output, null); + + } catch (IOException io) { + System.err.println("Config file exception: " + io.getLocalizedMessage()); + } finally { + if (output != null) { + try { + output.close(); + } catch (IOException e) { + System.err.println("Config file exception: " + e.getLocalizedMessage()); + } + } + + } + } + +} diff --git a/jguard_client/src/jguard/client/MapManager.java b/jguard_client/src/jguard/client/MapManager.java new file mode 100644 index 0000000..1ff6d23 --- /dev/null +++ b/jguard_client/src/jguard/client/MapManager.java @@ -0,0 +1,53 @@ +package jguard.client; + +import cucky.jGuard.lib.Status; +import cucky.jGuard.lib.object.Map; +import cucky.jGuard.lib.object.MapObject; +import jguard.client.gui.MapTree; +import jguard.client.gui.Tray; + + +public class MapManager { + + + private static Map getMapById(int id){ + for (Map map : Client.map) { + if (map.getId() == id) { + return map; + } + } + return null; + } + + public static void addMap(Map m) { + // pridani do seznamu + Client.map.add(m); + // pridání do stromu map + MapTree.add(); + } + + public static void setLock(int map, int state) { + for (Map mp : Client.map) { + if (mp.getId() == map) { + mp.setLock((state == 1)); + } + } + } + + + public static void setObjectStatus(int mapId, int objId, int status) { + for (Map map : Client.map) { + for (MapObject obj : map.getObjects()) { + // aktualizace stavu u objektu + if (obj.getId() == objId) { + obj.setStatus(status); + break; + } + } + } + } + + + + +} diff --git a/jguard_client/src/jguard/client/gui/MapTree.java b/jguard_client/src/jguard/client/gui/MapTree.java new file mode 100644 index 0000000..4cb4c53 --- /dev/null +++ b/jguard_client/src/jguard/client/gui/MapTree.java @@ -0,0 +1,131 @@ +package jguard.client.gui; + +import cucky.jGuard.lib.LogFile; +import cucky.jGuard.lib.MapComparator; +import cucky.jGuard.lib.Status; +import cucky.jGuard.lib.object.Map; +import java.awt.Component; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; +import java.awt.event.MouseListener; +import java.awt.image.BufferedImage; +import java.io.File; +import java.util.Collections; +import javax.imageio.ImageIO; +import javax.swing.ImageIcon; +import javax.swing.JLabel; +import javax.swing.JTree; +import javax.swing.border.EmptyBorder; +import javax.swing.tree.*; +import jguard.client.Client; + +public class MapTree extends JTree { + + static DefaultTreeModel model; + static DefaultMutableTreeNode root = new DefaultMutableTreeNode("Mapy"); + + public MapTree() { + + // neni videt root element + this.setRootVisible(false); + // povoli pouze jeden vybrany radek + this.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION); + + model = new DefaultTreeModel(root); + this.setModel(model); + + this.setCellRenderer(new MapTreeCellRenderer()); + + MouseListener ml = new MouseAdapter() { + public void mousePressed(MouseEvent e) { + DefaultMutableTreeNode node = (DefaultMutableTreeNode) getLastSelectedPathComponent(); + if (!node.isRoot()) { + if (e.getClickCount() == 1) { // jednoduchy klik + + } else if (e.getClickCount() == 2) { // dvojklik + Window.setMapView(node.getParent().getIndex(node)); + } + } + } + + }; + + addMouseListener(ml); + + } + + public static void add() { + root.removeAllChildren(); + Collections.sort(Client.map, new MapComparator()); + for (Map map : Client.map) { + DefaultMutableTreeNode node = new DefaultMutableTreeNode(map); + root.add(node); + } + try { + model.reload(); + } catch (Exception e) { + LogFile.printErr("Map tree error: " +e); + } + + } + + public static void clear(){ + root.removeAllChildren(); + model.reload(); + } + + + public static void reload(){ + model.reload(); + } + + private class MapTreeCellRenderer extends DefaultTreeCellRenderer { + + private JLabel label; + private BufferedImage image; + + public MapTreeCellRenderer() { + label = new JLabel(); + + } + + @Override + public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) { + label=(JLabel)super.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus); + Object o = ((DefaultMutableTreeNode) value).getUserObject(); + if (o instanceof Map) { + try { + Map m = (Map) o; + switch (m.getMapStatus()) { + case Status.OK: // online + image = ImageIO.read(new File("img/flag_green.png")); + break; + case Status.WARNING: // warning + image = ImageIO.read(new File("img/flag_orange.png")); + break; + case Status.OFFLINE: // offline + image = ImageIO.read(new File("img/flag_red.png")); + break; + default: // other + image = ImageIO.read(new File("img/flag_gray.png")); + } + if (image != null) { + label.setIcon(new ImageIcon(image)); + } + label.setText(m.getName()); + } catch (Exception ex) { + System.err.println("Error tree icon: " + ex.getMessage()); + } + } else { + label.setIcon(null); + label.setText(String.valueOf(value)); + } + + // EmptyBorder(int top, int left, int bottom, int right) + EmptyBorder border = new EmptyBorder(7, 25, 3, 3); + label.setBorder(border); + return label; + } + } + +} diff --git a/jguard_client/src/jguard/client/gui/MapView.java b/jguard_client/src/jguard/client/gui/MapView.java new file mode 100644 index 0000000..29803e8 --- /dev/null +++ b/jguard_client/src/jguard/client/gui/MapView.java @@ -0,0 +1,112 @@ +package jguard.client.gui; + +import cucky.jGuard.lib.LogFile; +import cucky.jGuard.lib.object.MapObject; +import cucky.jGuard.lib.object.ObjectConnection; +import java.awt.Color; +import java.awt.Graphics; +import java.awt.Graphics2D; +import java.awt.event.ActionEvent; +import java.awt.geom.Line2D; +import java.awt.image.BufferedImage; +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import javax.imageio.ImageIO; +import javax.swing.JPanel; +import javax.swing.Timer; +import jguard.client.Client; + + +public class MapView extends JPanel{ + + public static List items; + private static List connection; + + public void prekreslit() { + repaint(); + } + + public MapView() { + + this.setBackground(Color.white); + + addMouseMotionListener(new MapViewAction()); + addMouseListener(new MapViewAction()); + + + // nacteni objektů + items = new ArrayList<>(); + for (MapObject obj : Client.map.get(Window.currentMapIndex).getObjects()) { + items.add(new PaintObject(obj.getId())); + } + + // nacteni propojeni + connection = new ArrayList<>(); + for (ObjectConnection c : Client.map.get(Window.currentMapIndex).getConnection()) { + connection.add(new PaintConnection(c.getId())); + } + + + // periodicke prekreslovani + Timer timer = new Timer(100, (ActionEvent ae) -> { + repaint(); + }); + timer.start(); + + } + + @Override + protected void paintComponent(Graphics g) { + super.paintComponent(g); + Graphics2D g2 = (Graphics2D) g.create(); + + // vykresleni lock/unlock mapy + try { + BufferedImage image; + if (Client.map.get(Window.currentMapIndex).isLock()) { + image = ImageIO.read(new File("img/lock.png")); + } else { + image = ImageIO.read(new File("img/unlock.png")); + } + g.drawImage(image, 0, 0, this); + } catch (IOException ex) { + LogFile.printErr("File not found: " + ex); + } + + // kreslení nového propojeni + if (MapViewAction.createConnection && MapViewAction.startConMapObject != null) { + g2.draw(new Line2D.Double(MapViewAction.startConMapObject.getPoint(), MapViewAction.endConnPoint)); + } + + // vykresleni stavajicich propojeni + for (PaintConnection c : connection) { + c.paint(g2); + } + + // vykresleni objektu + for (PaintObject i : items) { + i.paint(g2); + } + + g2.dispose(); + } + + public static void addObject(MapObject mo) { + items.add(new PaintObject(mo.getId())); + } + + static void removeObject(int id) { + for (PaintObject i : items) { + if (i.getId() == id) { + items.remove(i); + break; + } + } + } + + public static void addObjectConnection(ObjectConnection oc) { + connection.add(new PaintConnection(oc.getId())); + } +} diff --git a/jguard_client/src/jguard/client/gui/MapViewAction.java b/jguard_client/src/jguard/client/gui/MapViewAction.java new file mode 100644 index 0000000..4d7623c --- /dev/null +++ b/jguard_client/src/jguard/client/gui/MapViewAction.java @@ -0,0 +1,289 @@ +package jguard.client.gui; + +import jguard.client.gui.dialog.ObjectConnectionDialog; +import cucky.jGuard.lib.Message; +import cucky.jGuard.lib.object.Map; +import cucky.jGuard.lib.object.MapObject; +import java.awt.Point; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.MouseEvent; +import java.awt.event.MouseListener; +import java.awt.event.MouseMotionListener; +import javax.swing.ImageIcon; +import javax.swing.JMenuItem; +import javax.swing.JPopupMenu; +import jguard.client.AppLauncher; +import jguard.client.Client; +import static jguard.client.gui.MapView.items; +import jguard.client.gui.dialog.ObjectDialog; + +public class MapViewAction implements MouseListener, MouseMotionListener { + + private static PaintObject selectedObject; + private static Point offset; + public static boolean createConnection = false; + public static PaintObject startConMapObject; + public static PaintObject endConMapObject; + public static Point endConnPoint; + + + @Override + public void mouseClicked(MouseEvent e) { + // vola se pri kliknuti v mape + } + + @Override + public void mousePressed(MouseEvent e) { // vola se pri stisknuti tlacitka + // vyhledani jestli se kliklo na nejaky objekt + for (PaintObject node : items) { + // zruseni vybrani na vsech objektech + node.setSelected(false); + if (node.contains(e.getPoint())) { + if (createConnection) { + // vytvareni propojeni - startovni body + startConMapObject = node; + endConnPoint = e.getPoint(); + } else { + selectedObject = node; + // nastaveni vybraneho objektu + offset = new Point( + (int) selectedObject.getBoxX() - e.getX(), + (int) selectedObject.getBoxY() - e.getY() + ); + // zvyrazneni vybraneho objektu + selectedObject.setSelected(true); + } + } + } + } + + @Override + public void mouseReleased(MouseEvent e) { // vola se pri uvolneni tlacitka + if (selectedObject != null) { + // presouvani objektu + if (!Client.map.get(Window.currentMapIndex).isLock()) {// povoli upravy jen kdyz neni mapa zamcena + // odeslani nove pozice objektu + // TODO doresit kontrolu souradnic aby se zbytecne neodesilala zmena kdyz k ni nedojde + int[] data = { + Client.map.get(Window.currentMapIndex).getId(), + selectedObject.getId(), + (int) selectedObject.getX(), + (int) selectedObject.getY() + }; + Client.client.send(Message.MOVE_OBJECT, data); + } + // dvojklik na objekt + if (e.getClickCount() == 2) { + doubleClickOnObject(selectedObject); + } + // prave kliknuti na objekt + if (e.getButton() == MouseEvent.BUTTON3) { + rightClickOnObject(e, selectedObject); + } + } else { + // prave kliknuti do volneho mista v mape + if (e.getButton() == MouseEvent.BUTTON3) { + rigthClick(e); + } + } + selectedObject = null; + offset = null; + + if (createConnection) { + for (PaintObject node : items) { + if (node.contains(e.getPoint())) { + endConMapObject = node; + if (startConMapObject.getId() != endConMapObject.getId()) { + new ObjectConnectionDialog(startConMapObject.getId(), endConMapObject.getId()); + } + } + } + // zrusi vytvareni propojeni + createConnection = false; + } + } + + @Override + public void mouseEntered(MouseEvent e) { + // vola se pri vstupu kurzoru + } + + @Override + public void mouseExited(MouseEvent e) { + // vola se pri opusteni kurzoru + } + + + @Override + public void mouseDragged(MouseEvent e) { + //jen pokud neni mapa zamcena + if (!Client.map.get(Window.currentMapIndex).isLock()) { + // vytvareni propojeni + if (createConnection) { + for (PaintObject node : items) { + if (node.contains(e.getPoint())) { + endConnPoint = node.getPoint(); + break; + } else { + endConnPoint = e.getPoint(); + } + } + } else { + // presunovani objektu + if (this.selectedObject != null && this.offset != null) { + // ziskani novych souradnic + Point to = e.getPoint(); + to.x += offset.x; + to.y += offset.y; + //nastaveni nove pozice + selectedObject.setPosition(to); + } + } + } + } + + @Override + public void mouseMoved(MouseEvent e) { + + } + + + + + private void doubleClickOnObject(PaintObject selectedObject) { + int id = selectedObject.getId(); + for (Map m : Client.map) { + for (MapObject ob : m.getObjects()) { + if (ob.getId() == id) { + ObjectDialog objectDialog = new ObjectDialog(ob, false); + break; + } + } + } + } + + private void rigthClick(MouseEvent me) { + JPopupMenu menu = new JPopupMenu(); + if (!Client.map.get(Window.currentMapIndex).isLock()) { + // polozka pridat objekt + if (Client.user.isAddObject()) { + JMenuItem itemAdd = new JMenuItem("Přidat objekt", new ImageIcon("img/add.png")); + itemAdd.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + MapObject mo = new MapObject(); + mo.setX(me.getX()); + mo.setY(me.getY()); + mo.setMap(Client.map.get(Window.currentMapIndex).getId()); + new ObjectDialog(mo, true); + } + }); + menu.add(itemAdd); + } + // polozka pridani propojeni + JMenuItem itemPropoj = new JMenuItem("Přidat propojení", new ImageIcon("img/arrow.png")); + itemPropoj.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + startConMapObject = null; + endConnPoint = null; + createConnection = true; + } + }); + menu.add(itemPropoj); + + } + // zamek mapy + JMenuItem itemLock; + boolean currentState = Client.map.get(Window.currentMapIndex).isLock(); + if (currentState) { + itemLock = new JMenuItem("Odemknout mapu", new ImageIcon("img/unlock.png")); + } else { + itemLock = new JMenuItem("Zamknout mapu", new ImageIcon("img/lock.png")); + } + itemLock.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (currentState) { + int[] d = {Client.map.get(Window.currentMapIndex).getId(), 0}; + Client.client.send(Message.SET_MAP_LOCK, d); + } else { + int[] d = {Client.map.get(Window.currentMapIndex).getId(), 1}; + Client.client.send(Message.SET_MAP_LOCK, d); + } + } + }); + menu.add(itemLock); + menu.show(me.getComponent(), me.getX(), me.getY()); + } + + private void rightClickOnObject(MouseEvent e, PaintObject obj) { + JPopupMenu menu = new JPopupMenu(); + if (obj.getObject().getService().isWinbox()) { + JMenuItem itemWinbox = new JMenuItem("Winbox", new ImageIcon("img/winbox.png")); + itemWinbox.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + AppLauncher.winbox(obj.getObject().getIp(), obj.getObject().getService().getPortWinbox(), obj.getObject().getUser(), obj.getObject().getPassword()); + } + }); + menu.add(itemWinbox); + } + if (obj.getObject().getService().isWeb()) { + JMenuItem itemWeb = new JMenuItem("Otevřít v prohlížeči", new ImageIcon("img/browser.png")); + itemWeb.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + AppLauncher.openWebpage(obj.getObject().getIp(), obj.getObject().getService().getPortWeb(), obj.getObject().getService().getVerzeWeb()); + } + }); + menu.add(itemWeb); + } + if (obj.getObject().getService().isSsh()) { + JMenuItem itemSsh = new JMenuItem("SSH", new ImageIcon("img/ssh.png")); + itemSsh.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + AppLauncher.ssh(obj.getObject().getIp(), obj.getObject().getUser(), obj.getObject().getPassword(), Integer.parseInt(obj.getObject().getService().getPortSsh())); + } + }); + menu.add(itemSsh); + } + + /* + JMenuItem itemWalk = new JMenuItem("SNMP walk"); + itemWalk.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + } + }); + menu.add(itemWalk); + */ + // ping na objekt + JMenuItem itemPing = new JMenuItem("Ping", new ImageIcon("img/ping.png")); + itemPing.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + } + }); + menu.add(itemPing); + + // odebrani objektu + if (!Client.map.get(Window.currentMapIndex).isLock() && Client.user.isRemoveMap()) { + JMenuItem itemRemove = new JMenuItem("Odebrat", new ImageIcon("img/trash.png")); + itemRemove.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + //Main.client.send(Message.TYPE_REMOVE_OBJECT, obj.getId()); + } + }); + menu.add(itemRemove); + } + + // zobrazeni menu + menu.show(e.getComponent(), e.getX(), e.getY()); + } + + +} diff --git a/jguard_client/src/jguard/client/gui/MenuPanel.java b/jguard_client/src/jguard/client/gui/MenuPanel.java new file mode 100644 index 0000000..9a9bd27 --- /dev/null +++ b/jguard_client/src/jguard/client/gui/MenuPanel.java @@ -0,0 +1,111 @@ +package jguard.client.gui; + +import cucky.jGuard.lib.LogFile; +import java.awt.Dimension; +import java.awt.FlowLayout; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.image.BufferedImage; +import java.io.File; +import java.io.IOException; +import javax.imageio.ImageIO; +import javax.swing.*; +import jguard.client.Client; +import jguard.client.gui.dialog.LoginDialog; + +class MenuPanel extends JPanel { + + public MenuPanel() { + + setPreferredSize(new Dimension(50, 50)); + setLayout(new FlowLayout(FlowLayout.LEFT)); + + // tlacitko pripojit + // TODO pokud je pripojeno změnit tlacítko na odpojit + try { + BufferedImage buttonIcon = ImageIO.read(new File("img/connect.png")); + JButton bLogin = new JButton(new ImageIcon(buttonIcon)); + bLogin.setFocusPainted(false); + bLogin.setContentAreaFilled(false); + bLogin.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (Client.client.isConnected()) { + int reply = JOptionPane.showConfirmDialog(null, "Odpojit od serveru ?", "Odpojit", JOptionPane.YES_NO_OPTION); + if (reply == JOptionPane.YES_OPTION) { + // odpojeni od serveru + Client.client.disconnect(); + } + } else { + new LoginDialog().setVisible(true); + } + } + }); + add(bLogin); + } catch (IOException ex) { + LogFile.printErr("File not found: image connect.png"); + } + + // tlacitko nastaveni serveru + try { + BufferedImage buttonIcon = ImageIO.read(new File("img/server.png")); + JButton bSettings = new JButton(new ImageIcon(buttonIcon)); + bSettings.setFocusPainted(false); + bSettings.setContentAreaFilled(false); + bSettings.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (Client.client.isConnected()) { + if (Client.user.isSetServer()) { + Window.showServerSettings(); + } else { + JOptionPane.showMessageDialog(null, "Nedostatečné oprávnění"); + } + } else { + JOptionPane.showMessageDialog(null, "Odpojeno od serveru"); + } + + } + }); + add(bSettings); + } catch (IOException ex) { + LogFile.printErr("File not found: image server.png"); + } + + // tlacitko nastaveni + try { + BufferedImage buttonIcon = ImageIO.read(new File("img/settings.png")); + JButton bSettings = new JButton(new ImageIcon(buttonIcon)); + bSettings.setFocusPainted(false); + bSettings.setContentAreaFilled(false); + bSettings.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + Window.showSettings(); + } + }); + add(bSettings); + } catch (IOException ex) { + LogFile.printErr("File not found: image settings.png"); + } + + // tlacitko log + try { + BufferedImage buttonIcon = ImageIO.read(new File("img/log.png")); + JButton bLogin = new JButton(new ImageIcon(buttonIcon)); + bLogin.setFocusPainted(false); + bLogin.setContentAreaFilled(false); + bLogin.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + + } + }); + add(bLogin); + } catch (IOException ex) { + LogFile.printErr("File not found: image log.png"); + } + + } + +} diff --git a/jguard_client/src/jguard/client/gui/PaintConnection.java b/jguard_client/src/jguard/client/gui/PaintConnection.java new file mode 100644 index 0000000..b2d9cf1 --- /dev/null +++ b/jguard_client/src/jguard/client/gui/PaintConnection.java @@ -0,0 +1,193 @@ +package jguard.client.gui; + +import cucky.jGuard.lib.object.Map; +import cucky.jGuard.lib.object.ObjectConnection; +import java.awt.BasicStroke; +import java.awt.Canvas; +import java.awt.Color; +import java.awt.Font; +import java.awt.FontMetrics; +import java.awt.Graphics; +import java.awt.Graphics2D; +import java.awt.Point; +import java.awt.Rectangle; +import java.awt.RenderingHints; +import java.awt.geom.Line2D; +import java.awt.geom.RoundRectangle2D; +import java.text.DecimalFormat; +import jguard.client.Client; + +public class PaintConnection extends Rectangle { + + private Point from = null; + private Point to = null; + private Font font = new Font("Helvetica", Font.PLAIN, 10); + + private int idConnection; + private ObjectConnection connection; + private boolean selected = true; + + private int xObdelnik, x2Obdelnik, yObdelnik, y2Obdelnik; + + public PaintConnection(int idConnection) { + this.idConnection = idConnection; + refresh_data(); + + } + + private void refresh_data() { + // ziskani objektu + for (Map map : Client.map) { + for (ObjectConnection c : map.getConnection()) { + if (c.getId() == this.idConnection) { + // nastaveni objektu + this.connection = c; + // nasteveni souradnic + for (PaintObject i : MapView.items) { + if (i.getId() == c.getFrom()) { + this.from = i.getPoint(); + } + if (i.getId() == c.getTo()) { + this.to = i.getPoint(); + } + } + } + } + } + } + + public void paint(Graphics g) { + Graphics2D g2 = (Graphics2D) g.create(); + g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); + + refresh_data(); + + // vykresli line + // TODO typ cary podle typu spojeni (1000, 100, 10, vlan, ...) + switch (connection.getType()) { + case ObjectConnection.TYPE_WIRELLES: + float[] dashingPattern = {2f, 2f}; + g2.setStroke(new BasicStroke(2f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 1.0f, dashingPattern, 2.0f)); + break; + case ObjectConnection.TYPE_GE: + g2.setStroke(new BasicStroke(5)); + break; + case ObjectConnection.TYPE_FE: + g2.setStroke(new BasicStroke(3)); + break; + case ObjectConnection.TYPE_E: + g2.setStroke(new BasicStroke(1)); + break; + case ObjectConnection.TYPE_10G_FIBER: + g2.setStroke(new BasicStroke(5)); // TODO doresit tvar cary + break; + case ObjectConnection.TYPE_1G_FIBER: + g2.setStroke(new BasicStroke(5)); // TODO doresit tvar cary + break; + case ObjectConnection.TYPE_100M_FIBER: + g2.setStroke(new BasicStroke(5)); // TODO doresit tvar cary + break; + } + + if (selected) { + // TODO zvyrazneni cary pri vybrani propojeni + } + + g2.draw(new Line2D.Double(this.from, this.to)); + + // vypocita stred cary + Point centerPoint = midpoint(this.from.getX(), this.to.getX(), this.from.getY(), this.to.getY()); + + // zmeni na sedou barvu + g2.setPaint(new Color(170, 170, 170)); + + if (connection.isRead()) { + + // vyska, sirka + int sirka = 80; + int vyska = 30; + + // vypocitani pocatecnich bodu + int x = centerPoint.x - sirka / 2; + int y = centerPoint.y - vyska / 2; + + // TODO az bude vyresni detekce kliku na caru tak tyto promenen uplne zrusit + xObdelnik = x; + x2Obdelnik = x + sirka; + yObdelnik = y; + y2Obdelnik = y + vyska; + + // vykresli oblast pro rx, tx + RoundRectangle2D.Double box = new RoundRectangle2D.Double(x, y, sirka, vyska, 10, 10); + g2.fill(box); + + // zmeni barvu na cernou + g2.setPaint(new Color(0, 0, 0)); + + // nastavi font + g2.setFont(font); + // vypise TX + String tx = "Tx: " + formatFileSize(connection.getTx()); + g2.drawString(tx, x + (int) (box.getWidth() / 2) - (getStringWidth(tx) / 2), y + 12); + + // vypise RX + String rx = "Rx: " + formatFileSize(connection.getRx()); + g2.drawString(rx, x + (int) (box.getWidth() / 2) - (getStringWidth(rx) / 2), y + 24); + } + } + + private Point midpoint(double x1, double x2, double y1, double y2) { + Point p = new Point((int) (x1 + x2) / 2, (int) (y1 + y2) / 2); + return p; + } + + private int getStringWidth(String text) { + //return g.getFontMetrics().stringWidth(text); + Canvas c = new Canvas(); + FontMetrics fm = c.getFontMetrics(font); + return fm.stringWidth(text); + } + + public void setSelected(Boolean value) { + this.selected = value; + } + + public ObjectConnection getObjectConnection() { + return connection; + } + + @Override + public boolean contains(Point p) { + if ((xObdelnik < p.getX() && p.getX() < x2Obdelnik) && (yObdelnik < p.getY() && p.getY() < y2Obdelnik)) { + return true; + } else { + return false; + } + } + + public static String formatFileSize(long size) { + String hrSize = null; + double b = size; + double k = size / 1024.0; + double m = ((size / 1024.0) / 1024.0); + double g = (((size / 1024.0) / 1024.0) / 1024.0); + double t = ((((size / 1024.0) / 1024.0) / 1024.0) / 1024.0); + + DecimalFormat dec = new DecimalFormat("0.00"); + + if (t > 1) { + hrSize = dec.format(t).concat(" TB"); + } else if (g > 1) { + hrSize = dec.format(g).concat(" GB"); + } else if (m > 1) { + hrSize = dec.format(m).concat(" MB"); + } else if (k > 1) { + hrSize = dec.format(k).concat(" KB"); + } else { + hrSize = dec.format(b).concat(" Bytes"); + } + + return hrSize; + } + +} diff --git a/jguard_client/src/jguard/client/gui/PaintObject.java b/jguard_client/src/jguard/client/gui/PaintObject.java new file mode 100644 index 0000000..2396642 --- /dev/null +++ b/jguard_client/src/jguard/client/gui/PaintObject.java @@ -0,0 +1,274 @@ +package jguard.client.gui; + +import cucky.jGuard.lib.LogFile; +import cucky.jGuard.lib.object.Map; +import cucky.jGuard.lib.object.MapObject; +import cucky.jGuard.lib.object.ObjectType; +import java.awt.BasicStroke; +import java.awt.Canvas; +import java.awt.Color; +import java.awt.Font; +import java.awt.FontMetrics; +import java.awt.Graphics; +import java.awt.Graphics2D; +import java.awt.Image; +import java.awt.Point; +import java.awt.Rectangle; +import java.awt.RenderingHints; +import java.awt.geom.RoundRectangle2D; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Base64; +import java.util.Collections; +import java.util.List; +import javax.imageio.ImageIO; +import jguard.client.Client; + +public class PaintObject extends Rectangle { + + private int objId; + private MapObject obj; + + private Image icon; + private int x; + private int y; + private int boxX; + private int boxY; + + private int paddingBoxu = 20; + + private int sirka; + private int vyska; + private RoundRectangle2D box; + private Graphics2D g2; + private Boolean selected = false; + + private Font fontNormal12 = new Font("Arial", Font.PLAIN, 12); + private Font fontBold12 = new Font("Arial", Font.BOLD, 12); + private Font fontNormalSmall = new Font("Arial", Font.PLAIN, 10); + + public PaintObject(int objId) { + this.objId = objId; + + refresh_data(); + + prepocitat_rozmery(); + + } + + private void refresh_data() { + // ziskani objektu + for (Map map : Client.map) { + for (MapObject object : map.getObjects()) { + if (object.getId() == objId) { + this.obj = object; + + } + } + } + } + + private void prepocitat_rozmery() { + // vyska boxu + vyska = 5; // padding top + if (this.icon != null) { + vyska += this.icon.getHeight(null); // vyska obrazku + } + vyska += getStringHeight(fontBold12); // vyska prvniho radku + if (!obj.getIp().equals("")) { + vyska += 5; // mezera mezi radky + vyska += getStringHeight(fontNormal12); // vyska druhyho radku + } +/* + for (SnmpProbe snmpProbe : obj.getSnmpProbe()) { + if (snmpProbe.isView()) { + vyska += 5; // mezera mezi radky + vyska += getStringHeight(fontNormalSmall); + } + } +*/ + vyska += 5; // padding bottom + + //sirka boxu + List sList = new ArrayList(); + sList.add(getStringWidth(obj.getName(), fontBold12) + paddingBoxu); + sList.add(getStringWidth(obj.getIp(), fontNormal12) + paddingBoxu); +/* + for (SnmpProbe snmpProbe : obj.getSnmpProbe()) { + if (snmpProbe.isView()) { + sList.add(getStringWidth(snmpProbe.getName() + "+ " + snmpProbe.getValue(), fontNormal12) + paddingBoxu); + } + } +*/ + sirka = Collections.max(sList); + } + + + public void paint(Graphics g) { + g2 = (Graphics2D) g.create(); + g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); + + // nastaveni pozice x, y objektu + this.x = obj.getX(); + this.y = obj.getY(); + + refresh_data(); + + // nastaveni ikony objektu + try { + for (ObjectType objectType : Client.deviceType) { + if (objectType.getId() == obj.getType()) { + byte[] decodedBytes = Base64.getDecoder().decode(objectType.getImgStr()); + ByteArrayInputStream bis = new ByteArrayInputStream(decodedBytes); + this.icon = ImageIO.read(bis); + break; + } + } + } catch (IOException ex) { + LogFile.printErr("Object icon exception: " + ex.getMessage()); + } + + prepocitat_rozmery(); + + /* TODO box gradient + GradientPaint redtowhite = new GradientPaint(x, y, Color.red, 200, y,Color.blue); + g2.setPaint(redtowhite); + */ + // status objektu + switch (obj.getStatus()) { + case 1: // online + g2.setPaint(new Color(83, 241, 83)); + break; + case 2: // warning + g2.setPaint(new Color(255, 131, 0)); + break; + case 3: // offline + g2.setPaint(new Color(221, 63, 63)); + break; + default: // other + g2.setPaint(new Color(172, 172, 172)); + } + + // vypocitani pocatecnich souradnic + boxX = x - (sirka / 2); + boxY = y - (vyska / 2); + + // vykresleni boxu + box = new RoundRectangle2D.Double(boxX, boxY, sirka, vyska, 10, 10); + g2.fill(box); + + if (selected) { + g2.setStroke(new BasicStroke(1.5f)); + g2.setColor(Color.BLACK); + g2.draw(box); + } + // + + // nacteni ikonka zarizeni + if (icon != null) { + int imgX = boxX + sirka / 2 - (icon.getWidth(null) / 2); + int imgY = boxY + 5; + g.drawImage(icon, imgX, imgY, null); + boxY += icon.getHeight(null); // pricte vysku ikonky + } else { + + } + + // vypsani nazvu prvku + g2.setPaint(Color.BLACK); + g2.setFont(fontBold12); + g2.drawString(obj.getName(), boxX + (int) (box.getWidth() / 2) - (getStringWidth(obj.getName(), g2.getFont()) / 2), boxY + getStringHeight(g2.getFont())); + + // vypsani ip prvku + boxY += 20; + g2.setFont(fontNormal12); + g2.drawString(obj.getIp(), boxX + (int) (box.getWidth() / 2) - (getStringWidth(obj.getIp(), g2.getFont()) / 2), boxY + getStringHeight(g2.getFont())); + + // vypsani snmp sond + g2.setFont(fontNormalSmall); +/* + for (SnmpProbe snmpProbe : obj.getSnmpProbe()) { + if (snmpProbe.isView()) { + boxY += 20; + String str = snmpProbe.getName() + ": " + snmpProbe.getValue(); + g2.drawString(str, boxX + (int) (box.getWidth() / 2) - (getStringWidth(str, g2.getFont()) / 2), boxY + getStringHeight(g2.getFont())); + } + } +*/ + this.setBounds(boxX, boxY, sirka, vyska); + + } + + public int getBoxX() { + return (int) box.getX(); + } + + public int getBoxY() { + return (int) box.getY(); + } + + @Override + public double getX() { + return x; + } + + @Override + public double getY() { + return y; + } + + public int getBoxWidth() { + return sirka; + } + + public int getBoxHeight() { + return vyska; + } + + public void setSelected(Boolean value) { + this.selected = value; + } + + private int getStringWidth(String text, Font font) { + Canvas c = new Canvas(); + FontMetrics fm = c.getFontMetrics(font); + return fm.stringWidth(text); + } + + private int getStringHeight(Font font) { + Canvas c = new Canvas(); + FontMetrics fm = c.getFontMetrics(font); + return fm.getHeight(); + } + + @Override + public boolean contains(Point p) { + if ((box.getMinX() < p.getX() && p.getX() < box.getMaxX()) && (box.getMinY() < p.getY() && p.getY() < box.getMaxY())) { + return true; + } else { + return false; + } + } + + public void setPosition(Point p) { + obj.setX(p.x + sirka / 2); + obj.setY(p.y + vyska / 2); + } + + public int getId() { + return obj.getId(); + } + + public MapObject getObject() { + return obj; + } + + public Point getPoint() { + Point p = new Point(); + p.x = this.x; + p.y = this.y; + return p; + } + +} diff --git a/jguard_client/src/jguard/client/gui/ServerSettingsPanel.java b/jguard_client/src/jguard/client/gui/ServerSettingsPanel.java new file mode 100644 index 0000000..24e377b --- /dev/null +++ b/jguard_client/src/jguard/client/gui/ServerSettingsPanel.java @@ -0,0 +1,161 @@ +package jguard.client.gui; + +import cucky.jGuard.lib.object.ObjectType; +import cucky.jGuard.lib.object.SnmpProfile; +import cucky.jGuard.lib.object.User; +import java.awt.BorderLayout; +import java.awt.Component; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import javax.swing.JButton; +import javax.swing.JLabel; +import javax.swing.JPanel; +import javax.swing.JScrollPane; +import javax.swing.JTabbedPane; +import javax.swing.JTable; +import javax.swing.JTextField; +import javax.swing.table.DefaultTableModel; +import jguard.client.Client; +import jguard.client.gui.dialog.AddDeviceType; + +public class ServerSettingsPanel extends JTabbedPane { + + public static Component zarizeni; + + + public ServerSettingsPanel() { + + + this.addTab("Základní", tabZakladni()); + this.addTab("Typy zařízení", tabZarizeni()); + this.addTab("SNMP", tabSnmpProfile()); + this.addTab("Uživatelé", tabUsers()); + + // vola se pri zmene zalozky + /* + this.addChangeListener(new ChangeListener() { + public void stateChanged(ChangeEvent e) { + System.out.println("Tab: " + tabbedPane.getSelectedIndex()); + } + }); + */ + + } + + private Component tabZakladni() { + + JPanel panel = new JPanel(); + + panel.setLayout(null); + + JLabel nestLabel = new JLabel("Úroveň nestability (ms)"); + nestLabel.setBounds(20, 20, 150, 25); + panel.add(nestLabel); + JTextField nestField = new JTextField(); + nestField.setBounds(170, 20, 80, 25); + panel.add(nestField); + + JLabel timeoutLabel = new JLabel("Úroveň timeout (ms)"); + timeoutLabel.setBounds(20, 60, 150, 25); + panel.add(timeoutLabel); + JTextField timeoutField = new JTextField(); + timeoutField.setBounds(170, 60, 80, 25); + panel.add(timeoutField); + + return new JScrollPane(panel); + } + + private Component tabZarizeni() { + JButton button = new JButton("Přidat typ"); + button.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + new AddDeviceType().show(); + } + }); + + JPanel panel = new JPanel(); + panel.setLayout(new BorderLayout()); + JTable tableDeviceType = new TableDeviceType(); + panel.add(new JScrollPane(tableDeviceType), BorderLayout.CENTER); + panel.add(button, BorderLayout.PAGE_END); + return panel; + } + + private Component tabUsers() { + + JTable table = new JTable(new TableModelUsers()); + return new JScrollPane(table); + } + + private Component tabSnmpProfile() { + + JTable table = new JTable(new TableModelSnmpProfile()); + return new JScrollPane(table); + } + + private static class TableModelSnmpProfile extends DefaultTableModel { + + public TableModelSnmpProfile() { + + // sloupce + addColumn("Nazev"); + addColumn("Verze"); + addColumn("Community"); + addColumn("Port"); + + // radky + for (SnmpProfile sp : Client.serverSettings.getSnmp()) { + addRow(new Object[]{ + sp.getName(), + sp.getVersion(), + sp.getCommunity(), + sp.getPort()}); + } + + } + } + + private static class TableModelUsers extends DefaultTableModel { + + public TableModelUsers() { + + // sloupce + addColumn("Jméno"); + addColumn("Nastaveni serveru"); + addColumn("Přidat mapu"); + addColumn("Odebrat mapu"); + + // radky + for (User sp : Client.serverSettings.getUsers()) { + addRow(new Object[]{ + sp.getUsername(), + sp.isSetServer(), + sp.isAddMap(), + sp.isRemoveMap() + }); + } + + } + } + + private static class TableModelDeviceType extends DefaultTableModel { + + public TableModelDeviceType() { + + // sloupce + addColumn("Jméno"); + addColumn("Ikona"); + + // radky + for (ObjectType ot : Client.deviceType) { + addRow(new Object[]{ + ot.getName(), + ot.getImgStr() + }); + } + + } + } + +} diff --git a/jguard_client/src/jguard/client/gui/SettingsPanel.java b/jguard_client/src/jguard/client/gui/SettingsPanel.java new file mode 100644 index 0000000..514cb30 --- /dev/null +++ b/jguard_client/src/jguard/client/gui/SettingsPanel.java @@ -0,0 +1,178 @@ +package jguard.client.gui; + +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.util.Properties; +import javax.swing.JButton; +import javax.swing.JCheckBox; +import javax.swing.JLabel; +import javax.swing.JPanel; +import javax.swing.JPasswordField; +import javax.swing.JSlider; +import javax.swing.JTextField; +import javax.swing.event.ChangeEvent; +import javax.swing.event.ChangeListener; +import jguard.client.Config; + +public class SettingsPanel extends JPanel { + + private JTextField serverText = new JTextField(); + private JTextField portText = new JTextField(); + private JTextField userText = new JTextField(); + private JPasswordField passwordText = new JPasswordField(); + private JCheckBox autoConnect = new JCheckBox("Automaticky přihlasit při spuštění"); + private JCheckBox runMinimalized = new JCheckBox("Spouštět minimalizovaně"); + private JCheckBox enableDebug = new JCheckBox("Debug režim"); + private JCheckBox enableSounds = new JCheckBox("Povolit zvuky"); + private JSlider volumeSlider = new JSlider(JSlider.HORIZONTAL, 0, 100, 50); + + public SettingsPanel() { + + try { + serverText.setText(new Config().get("server")); + portText.setText(new Config().get("port")); + userText.setText(new Config().get("user")); + passwordText.setText(new Config().get("password")); + autoConnect.setSelected((new Config().get("autoconnect").equals("1")? true : false)); + runMinimalized.setSelected((new Config().get("start_minimized").equals("1")? true : false)); + enableDebug.setSelected((new Config().get("debug").equals("1")? true : false)); + enableSounds.setSelected((new Config().get("sound").equals("1")? true : false)); + volumeSlider.setValue(Integer.valueOf(new Config().get("volume"))); + } catch(Exception e){ + System.err.println("Nepodařilo se nahrat nastaveni"); + } + + this.setLayout(null); + + JPanel p1 = loginSettings(); + p1.setBounds(20, 10, 400, 170); + this.add(p1); + + JPanel p2 = settings(); + p2.setBounds(20, getNextY(p1), 400, 150); + this.add(p2); + + JPanel p3 = soundSettings(); + p3.setBounds(20, getNextY(p2), 400, 100); + this.add(p3); + + + JButton b = new JButton("Uložit"); + b.setBounds(20, getNextY(p3), 100, 30); + b.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + Properties prop = new Properties(); + prop.setProperty("server", serverText.getText()); + prop.setProperty("port", portText.getText()); + prop.setProperty("user", userText.getText()); + prop.setProperty("password", new String(passwordText.getPassword())); + prop.setProperty("autoconnect", (autoConnect.isSelected() ? "1" : "0")); + prop.setProperty("start_minimized", (runMinimalized.isSelected() ? "1" : "0")); + prop.setProperty("debug", (enableDebug.isSelected() ? "1" : "0")); + prop.setProperty("sound", (enableSounds.isSelected() ? "1" : "0")); + prop.setProperty("volume", String.valueOf(volumeSlider.getValue())); + + new Config().store(prop); + } + }); + this.add(b); + + } + + private int getNextY(JPanel p){ + return p.getY() + p.getHeight() + 30; + } + + private JPanel loginSettings(){ + JPanel panel = new JPanel(null); + + JLabel nadpisLabel = new JLabel("Server"); + nadpisLabel.setBounds(0, 0, 80, 25); + nadpisLabel.setFont(nadpisLabel.getFont().deriveFont(20f)); + panel.add(nadpisLabel); + + JLabel serverLabel = new JLabel("Adresa"); + serverLabel.setBounds(0, 40, 80, 25); + panel.add(serverLabel); + + serverText.setBounds(110, 40, 160, 25); + panel.add(serverText); + + JLabel portLabel = new JLabel("Port"); + portLabel.setBounds(0, 70, 80, 25); + panel.add(portLabel); + + portText.setBounds(110, 70, 160, 25); + panel.add(portText); + + JLabel userLabel = new JLabel("Uživatel"); + userLabel.setBounds(0, 100, 80, 25); + panel.add(userLabel); + + userText.setBounds(110, 100, 160, 25); + panel.add(userText); + + JLabel passwordLabel = new JLabel("Heslo"); + passwordLabel.setBounds(0, 130, 80, 25); + panel.add(passwordLabel); + + passwordText.setBounds(110, 130, 160, 25); + panel.add(passwordText); + + return panel; + } + + private JPanel settings(){ + JPanel panel = new JPanel(null); + + JLabel nadpisLabel = new JLabel("Obecné nastavení"); + nadpisLabel.setBounds(0, 0, 400, 25); + nadpisLabel.setFont(nadpisLabel.getFont().deriveFont(20f)); + panel.add(nadpisLabel); + + autoConnect.setBounds(0, 40, 400, 25); + panel.add(autoConnect); + + runMinimalized.setBounds(0, 70, 400, 25); + panel.add(runMinimalized); + + enableDebug.setBounds(0, 100, 400, 25); + panel.add(enableDebug); + + return panel; + } + + private JPanel soundSettings(){ + JPanel panel = new JPanel(null); + + JLabel nadpisLabel = new JLabel("Zvuky"); + nadpisLabel.setBounds(0, 0, 400, 25); + nadpisLabel.setFont(nadpisLabel.getFont().deriveFont(20f)); + panel.add(nadpisLabel); + + enableSounds.setBounds(0, 40, 400, 25); + panel.add(enableSounds); + + JLabel hlasLabel = new JLabel("Hlasitost"); + hlasLabel.setBounds(0, 70, 80, 25); + panel.add(hlasLabel); + + JLabel sliderValue = new JLabel(volumeSlider.getValue() + " %"); + sliderValue.setBounds(240, 70, 50, 25); + panel.add(sliderValue); + + volumeSlider.setBounds(110, 70, 110, 25); + volumeSlider.addChangeListener(new ChangeListener() { + @Override + public void stateChanged(ChangeEvent e) { + sliderValue.setText(volumeSlider.getValue() + " %"); + } + }); + panel.add(volumeSlider); + + return panel; + } + + +} diff --git a/jguard_client/src/jguard/client/gui/StatusBar.java b/jguard_client/src/jguard/client/gui/StatusBar.java new file mode 100644 index 0000000..823c21c --- /dev/null +++ b/jguard_client/src/jguard/client/gui/StatusBar.java @@ -0,0 +1,66 @@ +package jguard.client.gui; + +import cucky.jGuard.lib.LogFile; +import java.awt.Dimension; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.image.BufferedImage; +import java.io.File; +import java.io.IOException; +import javax.imageio.ImageIO; +import javax.swing.ImageIcon; +import javax.swing.JButton; +import javax.swing.JLabel; +import javax.swing.JPanel; +import jguard.client.Client; +import jguard.client.gui.dialog.OnlineClientsDialog; + +public class StatusBar extends JPanel { + + private JLabel message; + private JLabel online; + private JButton btnOnline; + + public StatusBar() { + super(); + super.setPreferredSize(new Dimension(100, 35)); + + setLayout(null); + + message = new JLabel("Odpojeno"); + message.setBounds(10, 0, 100, 30); + add(message); + + online = new JLabel(""); + online.setBounds(250, 0, 100, 30); + add(online); + + // tlacitko zobrazujici klienty + try { + BufferedImage buttonIcon = ImageIO.read(new File("img/online_clients.png")); + btnOnline = new JButton(new ImageIcon(buttonIcon)); + btnOnline.setFocusPainted(false); + btnOnline.setContentAreaFilled(false); + btnOnline.setBorderPainted(false); + btnOnline.addActionListener(new ActionListener(){ + @Override + public void actionPerformed(ActionEvent e) { + new OnlineClientsDialog(); + } + }); + btnOnline.setBounds(200, 0, 32, 32); + add(btnOnline); + } catch (IOException ex) { + LogFile.printErr("File not found: image online_clinets.png"); + } + + } + + public void setMessage(String text) { + message.setText(text); + } + + public void setOnlineCount(){ + online.setText("Klientu online: " + Client.onlineClients.size()); + } +} diff --git a/jguard_client/src/jguard/client/gui/TableDeviceType.java b/jguard_client/src/jguard/client/gui/TableDeviceType.java new file mode 100644 index 0000000..4acb6ee --- /dev/null +++ b/jguard_client/src/jguard/client/gui/TableDeviceType.java @@ -0,0 +1,81 @@ +package jguard.client.gui; + +import cucky.jGuard.lib.object.ObjectType; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.util.Base64; +import java.util.logging.Level; +import java.util.logging.Logger; +import javax.imageio.ImageIO; +import javax.swing.ImageIcon; +import javax.swing.JTable; +import javax.swing.table.DefaultTableCellRenderer; +import javax.swing.table.DefaultTableModel; +import javax.swing.table.TableColumn; +import jguard.client.Client; + +public class TableDeviceType extends JTable { + + private static final String[] COLUMN_NAMES = { + "Ikona", + "Název zařízení"}; + + public TableDeviceType() { + + tableSetup(); + + } + + @Override + public Class getColumnClass(int column) { + if (column == 0) { + return ImageIcon.class; + } else { + return String.class; + } + } + + private void tableSetup() { + + DefaultTableModel model = new DefaultTableModel(COLUMN_NAMES, Client.deviceType.size()) { + @Override + public boolean isCellEditable(int row, int column) { + //all cells false + return false; + } + }; + + this.setModel(model); + + DefaultTableCellRenderer dtcr = new DefaultTableCellRenderer(); + dtcr.setHorizontalAlignment(DefaultTableCellRenderer.CENTER); + this.setDefaultRenderer(String.class, dtcr); + + int row = 0; + for (ObjectType obj : Client.deviceType) { + try { + byte[] decodedBytes = Base64.getDecoder().decode(obj.getImgStr()); + ByteArrayInputStream bis = new ByteArrayInputStream(decodedBytes); + ImageIcon icon = new ImageIcon(ImageIO.read(bis)); + this.setValueAt(icon, row, 0); + this.setValueAt(obj.getName(), row, 1); + row++; + } catch (IOException ex) { + Logger.getLogger(TableDeviceType.class.getName()).log(Level.SEVERE, null, ex); + } + } + + // nastavení vysky radku + for (int i = 0; i < Client.deviceType.size(); i++) { + this.setRowHeight(i, this.getRowHeight(i) + 40); + } + + // nastavení sirky sloupce + for (int i = 0; i < COLUMN_NAMES.length; i++) { + TableColumn column = this.getColumnModel().getColumn(i); + column.setPreferredWidth(column.getPreferredWidth() + 80); + } + + this.setVisible(true); + } +} diff --git a/jguard_client/src/jguard/client/gui/Tray.java b/jguard_client/src/jguard/client/gui/Tray.java new file mode 100644 index 0000000..c558157 --- /dev/null +++ b/jguard_client/src/jguard/client/gui/Tray.java @@ -0,0 +1,103 @@ +package jguard.client.gui; + +import cucky.jGuard.lib.LogFile; +import java.awt.AWTException; +import java.awt.MenuItem; +import java.awt.PopupMenu; +import java.awt.SystemTray; +import java.awt.TrayIcon; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.image.BufferedImage; +import java.io.File; +import java.io.IOException; +import javax.imageio.ImageIO; +import jguard.client.Client; +import jguard.client.network.NetworkClient; + +public class Tray { + + private SystemTray tray = SystemTray.getSystemTray(); + private static TrayIcon trayIcon; + private static PopupMenu menu = new PopupMenu(); + + public void createAndShowTray() { + try { + + //Check the SystemTray is supported + if (!SystemTray.isSupported()) { + LogFile.printErr("SystemTray is not supported"); + return; + } + + // tray menu + MenuItem exitItem = new MenuItem("Ukončit"); + exitItem.addActionListener(new ActionListener() { + + @Override + public void actionPerformed(ActionEvent e) { + LogFile.printInfo("Exit appliaction from tray icon"); + System.exit(0); + } + }); + menu.add(exitItem); + + trayIcon = new TrayIcon(ImageIO.read(new File("img/flag_gray.png")), Client.APP_NAME, menu); + trayIcon.addActionListener(new ActionListener() { + @Override + public void actionPerformed( ActionEvent e ) { + //Double click - obnovit/minimalizovat + if (Client.okno.isVisible()) { + Client.okno.setVisible(false); + } else { + Client.okno.setVisible(true); + } + }} + ); + + tray = SystemTray.getSystemTray(); + tray.add(trayIcon); + + } catch (IOException ex) { + LogFile.printErr("TrayIcon error: " + ex.getMessage()); + } catch (AWTException ex) { + LogFile.printErr("TrayIcon error: " + ex.getMessage()); + } + } + + public static void setIcon(String icon) { + try { + BufferedImage image = ImageIO.read(new File("img/"+icon)); + trayIcon.setImage(image); + } catch (IOException ex) { + LogFile.printErr("TrayIcon error: " + ex.getMessage()); + } + + } + + + public static void setStatusOffline() { + setIcon("flag_red.png"); + } + + public static void setStatusOnline() { + setIcon("flag_green.png"); + } + + public static void setStatusWarning() { + setIcon("flag_orange.png"); + } + + public static void setStatusDisconnected() { + setIcon("flag_gray.png"); + } + + public static void setState() { + if (Client.offline > 0) { + setStatusOffline(); + } else { + setStatusOnline(); + } + } + +} diff --git a/jguard_client/src/jguard/client/gui/Window.java b/jguard_client/src/jguard/client/gui/Window.java new file mode 100644 index 0000000..50e22d8 --- /dev/null +++ b/jguard_client/src/jguard/client/gui/Window.java @@ -0,0 +1,152 @@ +package jguard.client.gui; + +import cucky.jGuard.lib.LogFile; +import java.awt.BorderLayout; +import java.awt.Color; +import java.awt.Dimension; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.WindowAdapter; +import java.awt.event.WindowEvent; +import javax.swing.JButton; +import javax.swing.JFrame; +import javax.swing.JOptionPane; +import javax.swing.JPanel; +import javax.swing.JScrollPane; +import javax.swing.JSplitPane; +import javax.swing.JTabbedPane; +import jguard.client.Client; +import jguard.client.gui.dialog.NewMapDialog; + +public class Window extends JFrame { + + public static int currentMapIndex; + + private static StatusBar statusBar; + private static JSplitPane jsp; + private static JTabbedPane serverSettings; + + public Window() { + super(Client.APP_NAME); //titulek okna + + this.addWindowListener(new WindowAdapter() { + + @Override + public void windowClosing(WindowEvent windowEvent) { + // vola se pri zavreni okna krizkem + Object[] options = { + "Ukončit", + "Minimalizovat" + }; + int n = JOptionPane.showOptionDialog(windowEvent.getWindow(), + "Ukončit nebo minimalozovat do lišty ?", + "Ukončit", + JOptionPane.YES_NO_OPTION, + JOptionPane.QUESTION_MESSAGE, + null, //do not use a custom Icon + options, //the titles of buttons + options[1]); //default button title + if (n == 0) { + LogFile.printInfo("Ukončit"); + System.exit(0); + } + } + + @Override + public void windowDeiconified(WindowEvent e) { + // vola se pri obnoveni z listy + } + + @Override + public void windowIconified(WindowEvent e) { + // pri minimalizaci + setVisible(false); + } + + }); + + this.setLayout(new BorderLayout()); + + this.add(new MenuPanel(), BorderLayout.NORTH); + + // oddelovac leve a prave strany + jsp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT); + jsp.setDividerSize(5); + + // levy panel + JPanel leftPanel = new JPanel(); + leftPanel.setBackground(Color.WHITE); + leftPanel.setPreferredSize(new Dimension(200, 250)); + leftPanel.setLayout(new BorderLayout()); + leftPanel.add(new MapTree(), BorderLayout.PAGE_START); + //leftPanel.add(new MapList(), BorderLayout.PAGE_START); + + + JButton buttonAddMap = new JButton("Přidat mapu"); + buttonAddMap.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + new NewMapDialog().setVisible(true); + } + }); + + leftPanel.add(buttonAddMap, BorderLayout.PAGE_END); + jsp.add(leftPanel, JSplitPane.LEFT); + jsp.add(new JPanel(), JSplitPane.RIGHT); + + this.add(jsp, BorderLayout.CENTER); + + statusBar = new StatusBar(); + this.add(statusBar, BorderLayout.SOUTH); + + this.pack(); + + } + + public void blinkTaskBar() { + toFront(); + } + + public static void setStatusBarMessage(String message) { + statusBar.setMessage(message); + } + + public static void reloadOnlineClinets() { + statusBar.setOnlineCount(); + } + + // prepnuti mapy + public static void setMapView(int index) { + // nastaveni map id + currentMapIndex = index; + + MapView mv = new MapView(); + JScrollPane scroll = new JScrollPane(mv); + jsp.add(scroll, JSplitPane.RIGHT); + + } + + // prepnuti na stranku nastaveni + public static void showSettings() { + jsp.add(new SettingsPanel(), JSplitPane.RIGHT); + } + + // prepnuti na stranku s nastavenim serveru + public static void showServerSettings() { + serverSettings = new ServerSettingsPanel(); + JPanel serverSet = new JPanel(new BorderLayout()); + serverSet.add(serverSettings, BorderLayout.CENTER); + jsp.add(serverSet, JSplitPane.RIGHT); + } + + public static void reloadServerSettings() { + serverSettings = new ServerSettingsPanel(); + } + + public static void showMessage(String text){ + // TODO co s tim ? nechat nebo vytvořit dialog ? + //new ServerMessageDialog(text); + JOptionPane.showMessageDialog(null, text); + } + +} diff --git a/jguard_client/src/jguard/client/gui/dialog/AddDeviceType.java b/jguard_client/src/jguard/client/gui/dialog/AddDeviceType.java new file mode 100644 index 0000000..ffd59fd --- /dev/null +++ b/jguard_client/src/jguard/client/gui/dialog/AddDeviceType.java @@ -0,0 +1,146 @@ +package jguard.client.gui.dialog; + +import cucky.jGuard.lib.LogFile; +import cucky.jGuard.lib.Message; +import cucky.jGuard.lib.object.ObjectType; +import java.awt.Color; +import java.awt.Image; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.image.BufferedImage; +import java.io.File; +import java.io.FileInputStream; +import java.util.Base64; +import javax.imageio.ImageIO; +import javax.swing.JButton; +import javax.swing.JDialog; +import javax.swing.JFileChooser; +import javax.swing.JLabel; +import javax.swing.JPanel; +import javax.swing.JTextField; +import javax.swing.filechooser.FileNameExtensionFilter; +import jguard.client.Client; +import jguard.client.gui.ServerSettingsPanel; +import jguard.client.gui.Window; +import jguard.client.network.NetworkClient; +import org.apache.commons.io.FilenameUtils; + +public class AddDeviceType extends JDialog { + + private JLabel info = new JLabel("PNG obrazek o šířce 32px"); + private JTextField nazevText = new JTextField(); + private String img_str = ""; + + public AddDeviceType() { + setTitle("Přidat typ zařízení"); + setSize(300, 180); + setLocationRelativeTo(null); + setAlwaysOnTop(false); + JPanel panel = new JPanel(); + add(panel); + placeComponents(panel); + + } + + private void placeComponents(JPanel panel) { + + panel.setLayout(null); + + JLabel serverLabel = new JLabel("Název:"); + serverLabel.setBounds(20, 10, 80, 25); + panel.add(serverLabel); + + nazevText.setBounds(110, 10, 160, 25); + panel.add(nazevText); + + JLabel portLabel = new JLabel("Obrázek"); + portLabel.setBounds(20, 40, 80, 25); + panel.add(portLabel); + + JButton btnBrowse = new JButton("Browse"); + btnBrowse.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + browseButtonActionPerformed(e); + } + }); + btnBrowse.setBounds(110, 40, 160, 25); + panel.add(btnBrowse); + + info.setBounds(20, 70, 200, 25); + panel.add(info); + + JButton addButton = new JButton("Přidat"); + addButton.setBounds(20, 100, 90, 25); + addButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (nazevText.getText().length() > 3) { + ObjectType ot = new ObjectType(nazevText.getText(), img_str); + Client.client.send(Message.ADD_OBJECT_TYPE, ot); + // refresh tabulky + //TODO + Window.showServerSettings(); + System.out.println(".actionPerformed()"); + // zavreni dialogu + dispose(); + } else { + info.setForeground(Color.RED); + info.setText("Název je moc krátký"); + } + + } + }); + panel.add(addButton); + + JButton closeButton = new JButton("Storno"); + closeButton.setBounds(185, 100, 80, 25); + closeButton.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + dispose(); + } + }); + panel.add(closeButton); + + } + + private void browseButtonActionPerformed(ActionEvent evt) { + JFileChooser fc = new JFileChooser("."); + fc.addChoosableFileFilter(new FileNameExtensionFilter("Image Files PNG", "png")); + + int res = fc.showOpenDialog(null); + try { + if (res == JFileChooser.APPROVE_OPTION) { + File file = fc.getSelectedFile(); + String ext = "png"; + // TODO doresit overeni správného typu souboru + //String ext = FilenameUtils.getExtension("C:\\Users\\Michal\\Documents\\NetBeansProjects\\jGuardClient\\img\\app.png"); // returns "exe" + if (ext.equals("png")) { + Image image = ImageIO.read(file); + BufferedImage bimg = ImageIO.read(file); + if (bimg.getWidth() == 32) { + info.setText(""); + img_str = encodeFileToBase64Binary(file); + } else { + info.setForeground(Color.RED); + info.setText("Chybný rozměr obrázku"); + } + } else { + info.setForeground(Color.RED); + info.setText("Chybný formát obrázku"); + } + + } + } catch (Exception e) { + LogFile.printErr("File choose exception: " + e.getMessage()); + } + + } + + private static String encodeFileToBase64Binary(File file) throws Exception { + FileInputStream fileInputStreamReader = new FileInputStream(file); + byte[] bytes = new byte[(int) file.length()]; + fileInputStreamReader.read(bytes); + return new String(Base64.getEncoder().encode(bytes), "UTF-8"); + } + +} diff --git a/jguard_client/src/jguard/client/gui/dialog/LoginDialog.java b/jguard_client/src/jguard/client/gui/dialog/LoginDialog.java new file mode 100644 index 0000000..770a71f --- /dev/null +++ b/jguard_client/src/jguard/client/gui/dialog/LoginDialog.java @@ -0,0 +1,102 @@ +package jguard.client.gui.dialog; + +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import javax.swing.JButton; +import javax.swing.JDialog; +import javax.swing.JLabel; +import javax.swing.JPanel; +import javax.swing.JPasswordField; +import javax.swing.JTextField; +import jguard.client.Config; +import jguard.client.Client; +import static jguard.client.Client.config; +import jguard.client.network.NetworkClient; + +public class LoginDialog extends JDialog{ + + public LoginDialog() { + + setTitle("Přihlásit"); + setSize(300,250); + setLocationRelativeTo(null); + setAlwaysOnTop(true); + JPanel panel = new JPanel(); + add(panel); + placeComponents(panel); + + } + + @Override + public void setVisible(boolean b) { + super.setVisible(b); + } + + private void placeComponents(JPanel panel) { + + panel.setLayout(null); + + JLabel serverLabel = new JLabel("Server"); + serverLabel.setBounds(20, 10, 80, 25); + panel.add(serverLabel); + + JTextField serverText = new JTextField(new Config().get("server")); + serverText.setBounds(110, 10, 160, 25); + panel.add(serverText); + + JLabel portLabel = new JLabel("Port"); + portLabel.setBounds(20, 40, 80, 25); + panel.add(portLabel); + + JTextField portText = new JTextField(new Config().get("port")); + portText.setBounds(110, 40, 160, 25); + panel.add(portText); + + JLabel userLabel = new JLabel("Uživatel"); + userLabel.setBounds(20, 70, 80, 25); + panel.add(userLabel); + + JTextField userText = new JTextField(new Config().get("user")); + userText.setBounds(110, 70, 160, 25); + panel.add(userText); + + JLabel passwordLabel = new JLabel("Heslo"); + passwordLabel.setBounds(20, 100, 80, 25); + panel.add(passwordLabel); + + JPasswordField passwordText = new JPasswordField(new Config().get("password")); + passwordText.setBounds(110, 100, 160, 25); + panel.add(passwordText); + + JButton loginButton = new JButton("Přihlásit"); + loginButton.setBounds(20, 155, 90, 25); + loginButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + // pripojeni klienta + Client.client = new NetworkClient(serverText.getText(), Integer.parseInt(portText.getText())); + Client.client.start(); + // ulozeni prihlasovacich udaju + config.setProperty("server", serverText.getText()); + config.setProperty("port", new String(portText.getText())); + config.setProperty("user", userText.getText()); + config.setProperty("password", new String(passwordText.getPassword())); + new Config().store(config); + // zavreni dialogu + dispose(); + } + }); + panel.add(loginButton); + + JButton closeButton = new JButton("Storno"); + closeButton.setBounds(185, 155, 80, 25); + closeButton.addActionListener(new ActionListener(){ + public void actionPerformed(ActionEvent e){ + dispose(); + } + }); + panel.add(closeButton); + + } + +} diff --git a/jguard_client/src/jguard/client/gui/dialog/NewMapDialog.java b/jguard_client/src/jguard/client/gui/dialog/NewMapDialog.java new file mode 100644 index 0000000..c01bb4e --- /dev/null +++ b/jguard_client/src/jguard/client/gui/dialog/NewMapDialog.java @@ -0,0 +1,73 @@ +package jguard.client.gui.dialog; + +import cucky.jGuard.lib.Message; +import java.awt.Color; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import javax.swing.JButton; +import javax.swing.JDialog; +import javax.swing.JLabel; +import javax.swing.JPanel; +import javax.swing.JTextField; +import jguard.client.Client; + + +public class NewMapDialog extends JDialog { + + public NewMapDialog() { + + setTitle("Přidat mapu"); + setSize(320,180); + setLocationRelativeTo(null); + setAlwaysOnTop(true); + setResizable(false); + JPanel panel = new JPanel(); + add(panel); + placeComponents(panel); + + } + + @Override + public void setVisible(boolean b) { + super.setVisible(b); + } + + private JPanel placeComponents(JPanel panel) { + + panel.setLayout(null); + + JLabel serverLabel = new JLabel("Název"); + serverLabel.setBounds(20, 20, 80, 25); + panel.add(serverLabel); + + JTextField nameText = new JTextField(); + nameText.setBounds(110, 20, 160, 25); + panel.add(nameText); + + JLabel error = new JLabel(""); + error.setBounds(110, 45, 160, 25); + error.setForeground(Color.red); + panel.add(error); + + JButton button = new JButton("Přidat"); + button.setBounds(120, 80, 80, 25); + button.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (!nameText.getText().equals("")) { + Client.client.send(Message.ADD_MAP, nameText.getText()); + dispose(); + } else { + error.setText("Název nesmí být prázdný"); + } + } + }); + panel.add(button); + + return panel; + + } + + + +} diff --git a/jguard_client/src/jguard/client/gui/dialog/ObjectConnectionDialog.java b/jguard_client/src/jguard/client/gui/dialog/ObjectConnectionDialog.java new file mode 100644 index 0000000..99d6309 --- /dev/null +++ b/jguard_client/src/jguard/client/gui/dialog/ObjectConnectionDialog.java @@ -0,0 +1,211 @@ +package jguard.client.gui.dialog; + +import cucky.jGuard.lib.Message; +import cucky.jGuard.lib.object.Map; +import cucky.jGuard.lib.object.MapObject; +import cucky.jGuard.lib.object.ObjectConnection; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.util.ArrayList; +import javax.swing.DefaultComboBoxModel; +import javax.swing.JButton; +import javax.swing.JCheckBox; +import javax.swing.JComboBox; +import javax.swing.JDialog; +import javax.swing.JLabel; +import javax.swing.JPanel; +import jguard.client.Client; + +public class ObjectConnectionDialog extends JDialog { + + private static ObjectConnection connection; + private MapObject sObj; + private MapObject eObj; + private JComboBox typComboBox = new JComboBox(new DefaultComboBoxModel(new String[]{"Neznámé", "1000 Mbit/s", "100 Mbit/s", "10 Mbit/s", "VLAN", "Bezdrát", "10G Optic", "1G Optic", "100M Optic" })); + private static JComboBox zdrojComboBox = new JComboBox(); + private static int zdrojSelectedIndex = 0; + private static JComboBox rozhraniComboBox = new JComboBox(); + private JCheckBox read = new JCheckBox("", false); + private boolean edit = false; + + public ObjectConnectionDialog(ObjectConnection c) { + this.edit = true; + this.connection = c; + + // nastavani hodnot do polí + typComboBox.setSelectedIndex(this.connection.getType()); + if (this.connection.isRead()) { + read.setSelected(true); + zdrojComboBox.setEnabled(true); + rozhraniComboBox.setEnabled(true); + } else { + read.setSelected(false); + zdrojComboBox.setEnabled(false); + rozhraniComboBox.setEnabled(false); + } + + getPanel(); + } + + public ObjectConnectionDialog(int startObjId, int endObjId) { + + this.connection = new ObjectConnection(); + this.connection.setFrom(startObjId); + this.connection.setTo(endObjId); + + zdrojComboBox.setEnabled(false); + rozhraniComboBox.setEnabled(false); + + getPanel(); + } + + private void getPanel() { + + setTitle("Vlastnosti propojení"); + setSize(450, 400); + setResizable(false); + setLocationRelativeTo(null); + + // ziskani informaci o objektech + for (Map map : Client.map) { + for (MapObject object : map.getObjects()) { + if (object.getId() == this.connection.getFrom()) { + sObj = object; + if (object.getId() == this.connection.getSourceObjectId()) { + zdrojSelectedIndex = 0; +// Main.client.send(Message.TYPE_SNMP_INTERFACE, sObj); + } + } + if (object.getId() == this.connection.getTo()) { + eObj = object; + if (object.getId() == this.connection.getSourceObjectId()) { + zdrojSelectedIndex = 1; +// Main.client.send(Message.TYPE_SNMP_INTERFACE, eObj); + } + } + } + } + + JPanel panel = new JPanel(); + panel.setLayout(null); + + int stred = getWidth() / 2; + + JLabel firstLabel = new JLabel("1: " + sObj.getName()); + firstLabel.setBounds(20, 10, 500, 25); + panel.add(firstLabel); + + JLabel secondLabel = new JLabel("2: " + eObj.getName()); + secondLabel.setBounds(20, 40, 500, 25); + panel.add(secondLabel); + + JLabel typLabel = new JLabel("Typ spojení"); + typLabel.setBounds(20, 100, 140, 25); + panel.add(typLabel); + + typComboBox.setBounds(200, 100, 200, 25); + panel.add(typComboBox); + + JLabel cistLabel = new JLabel("Čtení dat"); + cistLabel.setBounds(20, 170, 200, 25); + panel.add(cistLabel); + + read.setBounds(200, 170, 50, 25); + read.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (read.isSelected()) { + zdrojComboBox.setEnabled(true); + rozhraniComboBox.setEnabled(true); + } else { + zdrojComboBox.setEnabled(false); + rozhraniComboBox.setEnabled(false); + } + } + }); + panel.add(read); + + JLabel zdrojLabel = new JLabel("Zdrojové zařízení"); + zdrojLabel.setBounds(20, 200, 200, 25); + panel.add(zdrojLabel); + + zdrojComboBox.setModel(new DefaultComboBoxModel(new String[]{sObj.getName(), eObj.getName()})); + zdrojComboBox.setSelectedIndex(zdrojSelectedIndex); + zdrojComboBox.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + if (zdrojComboBox.getSelectedIndex() == 0) { +// Main.client.send(Message.TYPE_SNMP_INTERFACE, sObj); + } else { +// Main.client.send(Message.TYPE_SNMP_INTERFACE, eObj); + } + // dokud se nenactou hodnoty vypne combobox + rozhraniComboBox.setEnabled(false); + } + }); + zdrojComboBox.setBounds(200, 200, 200, 25); + panel.add(zdrojComboBox); + + JLabel rozhraniLabel = new JLabel("Rozhraní"); + rozhraniLabel.setBounds(20, 230, 200, 25); + panel.add(rozhraniLabel); + + rozhraniComboBox.setModel(new DefaultComboBoxModel(new String[]{" "})); + rozhraniComboBox.setBounds(200, 230, 200, 25); + panel.add(rozhraniComboBox); + + JButton saveButton = new JButton("Uložit"); + saveButton.setBounds(stred - 120, 300, 90, 25); + saveButton.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + if (zdrojComboBox.getSelectedIndex() == 0) { + connection.setSourceObjectId(sObj.getId()); + } else { + connection.setSourceObjectId(sObj.getId()); + } + connection.setType(typComboBox.getSelectedIndex()); + connection.setRead(read.isSelected()); + connection.setMap(sObj.getMap()); + connection.setFrom(sObj.getId()); + connection.setTo(eObj.getId()); + connection.setIface(rozhraniComboBox.getSelectedIndex()); + + if (edit) { +// Main.client.send(Message.TYPE_EDIT_CONNECTION, connection); + } else { + Client.client.send(Message.NEW_CONNECTION, connection); + } + + dispose(); + } + }); + panel.add(saveButton); + + JButton closeButton = new JButton("Storno"); + closeButton.setBounds(stred + 30, 300, 90, 25); + closeButton.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + dispose(); + } + }); + panel.add(closeButton); + + add(panel); + + setVisible(true); + setAlwaysOnTop(true); + } + + public static void reloadInterfaceList(ArrayList list) { + rozhraniComboBox.removeAllItems(); + for (String string : list) { + rozhraniComboBox.addItem(string); + } + if (zdrojComboBox.getSelectedIndex() == zdrojSelectedIndex && !list.isEmpty()) { + rozhraniComboBox.setSelectedIndex(connection.getIface()); + } + rozhraniComboBox.setEnabled(true); + } + + +} diff --git a/jguard_client/src/jguard/client/gui/dialog/ObjectDialog.java b/jguard_client/src/jguard/client/gui/dialog/ObjectDialog.java new file mode 100644 index 0000000..a7662c6 --- /dev/null +++ b/jguard_client/src/jguard/client/gui/dialog/ObjectDialog.java @@ -0,0 +1,587 @@ +package jguard.client.gui.dialog; + +import cucky.jGuard.lib.LogComparator; +import cucky.jGuard.lib.LogFile; +import cucky.jGuard.lib.Message; +import cucky.jGuard.lib.Status; +import cucky.jGuard.lib.object.Log; +import cucky.jGuard.lib.object.MapObject; +import cucky.jGuard.lib.object.ObjectServices; +import cucky.jGuard.lib.object.ObjectType; +import cucky.jGuard.lib.object.SnmpProbe; +import cucky.jGuard.lib.object.SnmpProfile; +import cucky.jGuard.snmp.SNMPManager; +import java.awt.BorderLayout; +import java.awt.Color; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Date; +import java.util.HashMap; +import java.util.Map; +import java.util.Map.Entry; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import javax.swing.BorderFactory; +import javax.swing.DefaultComboBoxModel; +import javax.swing.JButton; +import javax.swing.JCheckBox; +import javax.swing.JComboBox; +import javax.swing.JDialog; +import javax.swing.JLabel; +import javax.swing.JPanel; +import javax.swing.JPasswordField; +import javax.swing.JScrollPane; +import javax.swing.JTabbedPane; +import javax.swing.JTable; +import javax.swing.JTextArea; +import javax.swing.JTextField; +import javax.swing.table.DefaultTableModel; +import jguard.client.Client; +import org.snmp4j.smi.OID; + +public class ObjectDialog extends JDialog { + + private MapObject obj; + private Boolean newObject; + + private JTextField nazevText = new JTextField(); + private JLabel nazevWarning = new JLabel("Název musi mit min. 3 znaky"); + private JTextField ipText = new JTextField(); + private JLabel ipWarning = new JLabel("Neplatná IP adresa"); + private JComboBox typeComboBox = new JComboBox(); + private JTextField userText = new JTextField(); + private JPasswordField hesloText = new JPasswordField(); + private DefaultComboBoxModel snmpmodel = new DefaultComboBoxModel(); + private JCheckBox disableCheBox = new JCheckBox(); + private JTextField locationText = new JTextField(); + private JTextArea descriptionText = new JTextArea(); + + private JCheckBox winboxBox = new JCheckBox("Winbox"); + private JTextField portWinbox = new JTextField("8291"); + private JCheckBox sshBox = new JCheckBox("SSH"); + private JTextField portSsh = new JTextField("22"); + private JCheckBox wwwBox = new JCheckBox("Web"); + private JTextField portWww = new JTextField("80"); + private JComboBox wwwVerzeComboBox = new JComboBox(); + private JCheckBox telnetBox = new JCheckBox("Telnet"); + private JTextField portTelnet = new JTextField("23"); + private JCheckBox smstBox = new JCheckBox("SMS"); + private JTextField portSms = new JTextField("5020"); + private JComboBox smsVerzeComboBox = new JComboBox(); + private JComboBox snmpProfileComboBox = new JComboBox(); + + public ObjectDialog(MapObject obj, boolean newObject) { + + this.obj = obj; + this.newObject = newObject; + + setTitle("Vlastnosti objektu"); + setSize(600, 520); + setLocationRelativeTo(null); + setVisible(true); + setAlwaysOnTop(true); + + setLayout(new BorderLayout()); + + JTabbedPane tabbedPane = new JTabbedPane(); + + JScrollPane tabNastaveni = new JScrollPane(tabNastaveni(), + JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); + + tabbedPane.addTab("Informace", tabInfo()); + tabbedPane.addTab("Nastaveni", tabNastaveni); + tabbedPane.addTab("Služby", tabSluzby()); + tabbedPane.addTab("Události", tabUdalosti()); + tabbedPane.addTab("Sondy", tabSondy()); + + // pokud se jedna o novy objekt + if (newObject) { + tabbedPane.setSelectedIndex(1); // prepne na kartu nastaveni + tabbedPane.setEnabledAt(0, false); // vypne kartu informace + tabbedPane.setEnabledAt(3, false); // vypne kartu udalosti + } + + add(tabbedPane, BorderLayout.CENTER); + + JPanel pane = new JPanel(); + JButton save = new JButton("Ulozit"); + save.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + // kontrola chyb + if (nazevText.getText().length() < 3) { + nazevWarning.setVisible(true); + return; + } + nazevWarning.setVisible(false); + if (!validateIp(ipText.getText())) { + ipWarning.setVisible(true); + return; + } + ipWarning.setVisible(false); + // ulozeni do objektu + obj.setName(nazevText.getText()); + obj.setIp(ipText.getText()); + obj.setUser(userText.getText()); + obj.setPassword(new String(hesloText.getPassword())); + for (int i = 0; i < Client.deviceType.size(); i++) { + if (typeComboBox.getSelectedIndex() == i) { + obj.setType(Client.deviceType.get(i).getId()); + } + } + obj.setSnmpProfile(Client.serverSettings.getSnmp().get(snmpProfileComboBox.getSelectedIndex()).getId()); + obj.setActive(disableCheBox.isSelected()); + obj.setDescription(descriptionText.getText()); + obj.setLocation(locationText.getText()); + + ObjectServices service = new ObjectServices(); + service.setWinbox(winboxBox.isSelected()); + service.setPortWinbox(portWinbox.getText()); + service.setSsh(sshBox.isSelected()); + service.setPortSsh(portSsh.getText()); + service.setWeb(wwwBox.isSelected()); + service.setPortWeb(portWww.getText()); + service.setVerzeWeb(wwwVerzeComboBox.getSelectedIndex()); + service.setTelnet(telnetBox.isSelected()); + service.setPortTelnet(portTelnet.getText()); + service.setSms(smstBox.isSelected()); + service.setPortSms(portSms.getText()); + service.setVerzeSms(smsVerzeComboBox.getSelectedIndex()); + obj.setService(service); + + ArrayList probe = new ArrayList<>(); + if (!newObject) { + probe.addAll(obj.getSnmpProbe()); + } + obj.setSnmpProbe(probe); + + if (newObject) { + Client.client.send(Message.ADD_OBJECT, obj); + } else { + Client.client.send(Message.UPDATE_OBJECT, obj); + } + + // zavreni dialogu + dispose(); + } + }); + pane.add(save); + + JButton cancel = new JButton("Zrusit"); + cancel.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent e) { + dispose(); + } + }); + pane.add(cancel); + + add(pane, BorderLayout.PAGE_END); + + + + // nacteni snmp profilu + for (SnmpProfile sp : Client.serverSettings.getSnmp()) { + snmpmodel.addElement(sp.getName()); + } + + // doplneni poli dialogu podle vybraneho objektu + if (!newObject) { + nazevText.setText(obj.getName()); + ipText.setText(obj.getIp()); + userText.setText(obj.getUser()); + hesloText.setText(obj.getPassword()); + for (int i = 0; i < Client.deviceType.size(); i++) { + if (obj.getType() == Client.deviceType.get(i).getId()) { + typeComboBox.setSelectedIndex(i); + } + } + + for (int i = 0; i < Client.serverSettings.getSnmp().size(); i++) { + if (Client.serverSettings.getSnmp().get(i).getId() == obj.getSnmpProfile()) { + snmpProfileComboBox.setSelectedIndex(i); + } + } + disableCheBox.setSelected(obj.isActive()); + descriptionText.setText(obj.getDescription()); + locationText.setText(obj.getLocation()); + winboxBox.setSelected(obj.getService().isWinbox()); + portWinbox.setText(String.valueOf(obj.getService().getPortWinbox())); + sshBox.setSelected(obj.getService().isSsh()); + portSsh.setText(String.valueOf(obj.getService().getPortSsh())); + wwwBox.setSelected(obj.getService().isWeb()); + portWww.setText(String.valueOf(obj.getService().getPortWeb())); + wwwVerzeComboBox.setSelectedIndex(obj.getService().getVerzeWeb()); + telnetBox.setSelected(obj.getService().isTelnet()); + portTelnet.setText(String.valueOf(obj.getService().getPortTelnet())); + smstBox.setSelected(obj.getService().isSms()); + portSms.setText(String.valueOf(obj.getService().getPortSms())); + smsVerzeComboBox.setSelectedIndex(obj.getService().getVerzeSms()); + } else { + disableCheBox.setSelected(true); + } + + } + + private JPanel tabInfo() { + JPanel p = new JPanel(null); + + Map label = new HashMap(); + label.put("Popis", "1.3.6.1.2.1.1.1.0"); + label.put("Uptime", "1.3.6.1.2.1.1.3.0"); + label.put("Contact", "1.3.6.1.2.1.1.4.0"); + label.put("Name", "1.3.6.1.2.1.1.5.0"); + + if(!newObject){ /// TODO u objektu u kterych neni snmp protokol povolen vraci system err null + Thread ti = new Thread() { + @Override + public void run() { + super.run(); + // nacteni snmp profilu + String port = "161"; + String community = "public"; + int version = SNMPManager.version1; + for (SnmpProfile sp : Client.serverSettings.getSnmp()) { + if (obj.getSnmpProfile() == sp.getId()) { + port = sp.getPort(); + community = sp.getCommunity(); + version = sp.getVersion(); + break; + } + } + SNMPManager mngr = new SNMPManager(obj.getIp(), port, community, version); + try { + mngr.start(); + + } catch (Exception e) { + } + int y = 30; + for (Entry entry : label.entrySet()) { + try { + JLabel label = new JLabel(entry.getKey()); + label.setBounds(20, y, 90, 25); + JLabel value = new JLabel(mngr.getAsString(new OID(entry.getValue()))); + value.setBounds(100, y, 200, 25); + p.add(label); + p.add(value); + y += 30; + p.revalidate(); + p.repaint(); + } catch (Exception e) { + LogFile.printErr("Object dialog - SNMP error: " + e); + } + } + } + }; + ti.start(); + } + return p; + } + + private JPanel tabNastaveni() { + JPanel p = new JPanel(); + + p.setLayout(null); + + JLabel nazevLabel = new JLabel("Název"); + nazevLabel.setBounds(20, 30, 80, 25); + p.add(nazevLabel); + + nazevText.setBounds(110, 30, 160, 25); + p.add(nazevText); + nazevWarning.setForeground(Color.red); + nazevWarning.setBounds(110, 55, 160, 25); + nazevWarning.setVisible(false); + p.add(nazevWarning); + + JLabel ipLabel = new JLabel("IP adresa"); + ipLabel.setBounds(300, 30, 80, 25); + p.add(ipLabel); + + ipText.setBounds(390, 30, 160, 25); + p.add(ipText); + ipWarning.setForeground(Color.red); + ipWarning.setBounds(390, 55, 160, 25); + ipWarning.setVisible(false); + p.add(ipWarning); + + JLabel typLabel = new JLabel("Typ"); + typLabel.setBounds(20, 130, 80, 25); + p.add(typLabel); + + DefaultComboBoxModel model = new DefaultComboBoxModel(); + for (ObjectType obj : Client.deviceType) { + model.addElement(obj.getName()); + } + + typeComboBox.setModel(model); + typeComboBox.setBounds(110, 130, 160, 25); + p.add(typeComboBox); + + JLabel userLabel = new JLabel("Uživatel"); + userLabel.setBounds(20, 80, 80, 25); + p.add(userLabel); + + userText.setBounds(110, 80, 160, 25); + p.add(userText); + + JLabel hesloLabel = new JLabel("Heslo"); + hesloLabel.setBounds(300, 80, 80, 25); + p.add(hesloLabel); + + hesloText.setBounds(390, 80, 160, 25); + p.add(hesloText); + + JLabel snmpLabel = new JLabel("SNMP profil"); + snmpLabel.setBounds(300, 130, 80, 25); + p.add(snmpLabel); + + snmpProfileComboBox.setModel(snmpmodel); + snmpProfileComboBox.setBounds(390, 130, 160, 25); + p.add(snmpProfileComboBox); + + JLabel disableLabel = new JLabel("Aktivní"); + disableLabel.setBounds(20, 180, 80, 25); + p.add(disableLabel); + + disableCheBox.setBounds(110, 180, 160, 25); + p.add(disableCheBox); + + JButton snmpTest = new JButton("Test SNMP"); + snmpTest.setBounds(390, 180, 160, 25); + snmpTest.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + /* TODO + Thread testSnmp = new Thread() { + @Override + public void run() { + + try { + // nacteni snmp profilu + String port = "161"; + String community = "public"; + int version = SNMPManager.version1; + for (int i = 0; i < Client.serverSettings.getSnmp().size(); i++) { + if (snmpProfileComboBox.getSelectedIndex() == i) { + port = Client.serverSettings.getSnmp().get(i).getPort(); + community = Client.serverSettings.getSnmp().get(i).getCommunity(); + version = Client.serverSettings.getSnmp().get(i).getVersion(); + break; + } + } + SNMPManager mngr = new SNMPManager(ipText.getText(), port, community, version); + mngr.start(); + String desc = mngr.getSysDescr(); + JOptionPane.showMessageDialog(ObjectDialog.this, "Odpoved: " + desc, "SNMP test", JOptionPane.INFORMATION_MESSAGE); + } catch (Exception ex) { + JOptionPane.showMessageDialog(ObjectDialog.this, "SNMP se nepodařilo načíst", "SNMP test", JOptionPane.ERROR_MESSAGE); + System.err.println("SNMP test error: " + ex.getMessage()); + } + + } + }; + testSnmp.start(); + */ + } + }); + p.add(snmpTest); + + JLabel locLabel = new JLabel("Umístění"); + locLabel.setBounds(20, 230, 80, 25); + p.add(locLabel); + + locationText.setBounds(110, 230, 440, 25); + p.add(locationText); + + JLabel descLabel = new JLabel("Poznámka"); + descLabel.setBounds(20, 280, 80, 25); + p.add(descLabel); + + descriptionText.setBounds(110, 280, 440, 100); + descriptionText.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createLineBorder(Color.BLACK), BorderFactory.createEmptyBorder(10, 10, 10, 10))); + p.add(descriptionText); + + return p; + } + + private JPanel tabSluzby() { + JPanel p = new JPanel(); + + p.setLayout(null); + + winboxBox.setBounds(20, 30, 80, 25); + p.add(winboxBox); + + JLabel winbox_port = new JLabel("port:"); + winbox_port.setBounds(130, 30, 60, 25); + p.add(winbox_port); + + portWinbox.setBounds(180, 30, 60, 25); + p.add(portWinbox); + + sshBox.setBounds(20, 60, 80, 25); + p.add(sshBox); + + JLabel ssh_port = new JLabel("port:"); + ssh_port.setBounds(130, 60, 60, 25); + p.add(ssh_port); + + portSsh.setBounds(180, 60, 60, 25); + p.add(portSsh); + + wwwBox.setBounds(20, 90, 80, 25); + p.add(wwwBox); + + JLabel www_port = new JLabel("port:"); + www_port.setBounds(130, 90, 60, 25); + p.add(www_port); + + portWww.setBounds(180, 90, 60, 25); + p.add(portWww); + + JLabel www_verze = new JLabel("verze:"); + www_verze.setBounds(260, 90, 60, 25); + p.add(www_verze); + + wwwVerzeComboBox.setModel(new DefaultComboBoxModel(new String[]{"HTTP", "HTTPS"})); + wwwVerzeComboBox.setBounds(320, 90, 80, 25); + p.add(wwwVerzeComboBox); + + telnetBox.setBounds(20, 120, 80, 25); + p.add(telnetBox); + + JLabel telnet_port = new JLabel("port:"); + telnet_port.setBounds(130, 120, 60, 25); + p.add(telnet_port); + + portTelnet.setBounds(180, 120, 60, 25); + p.add(portTelnet); + + smstBox.setBounds(20, 150, 80, 25); + p.add(smstBox); + + JLabel sms_port = new JLabel("port:"); + sms_port.setBounds(130, 150, 60, 25); + p.add(sms_port); + + portSms.setBounds(180, 150, 60, 25); + p.add(portSms); + + JLabel sms_verze = new JLabel("verze:"); + sms_verze.setBounds(260, 150, 60, 25); + p.add(sms_verze); + + smsVerzeComboBox.setModel(new DefaultComboBoxModel(new String[]{"SDV", "BT"})); + smsVerzeComboBox.setBounds(320, 150, 80, 25); + p.add(smsVerzeComboBox); + + return p; + } + + private JScrollPane tabUdalosti() { + + String column[] = {"Začátek", "Konec", "Stav"}; + DefaultTableModel tableModel = new DefaultTableModel(column, 0) { + + @Override + public boolean isCellEditable(int row, int column) { + //all cells false + return false; + } + }; + + try { + // TODO pri vytvareni noveho objektu to vyhodi chybu + Collections.sort(Client.log, new LogComparator()); + Collections.reverse(Client.log); + if (!newObject) { + for (Log l : Client.log) { + if (l.getDevice() == obj.getId()) { + Object[] d = { + getDate(l.getFrom()), + getDate(l.getTo()), + getStatus(l.getStatus())}; + tableModel.addRow(d); + } + + } + } + + } catch (Exception e) { + LogFile.printErr("Object log error: " + e.getMessage()); + } + JTable table = new JTable(tableModel); + + return new JScrollPane(table); + } + + private JPanel tabSondy() { + JPanel panel = new JPanel(); + + panel.setLayout(new BorderLayout()); + + String column[] = {"Popis", "OID", "Aktivni", "Zobrazit"}; + DefaultTableModel tableModel = new DefaultTableModel(column, 0); + if (!newObject) { + try { + for (SnmpProbe p : obj.getSnmpProbe()) { + Object[] d = {p.getName(), p.getOid(), "", ""}; + tableModel.addRow(d); + } + } catch (Exception e) { + LogFile.printErr("Probe list " + e.getMessage()); + } + } + + JScrollPane table = new JScrollPane(); + table.add(new JTable(tableModel)); + panel.add(new JTable(tableModel), BorderLayout.CENTER); + + JPanel jp = new JPanel(); + jp.add(new JLabel("Přidat: ")); + panel.add(jp, BorderLayout.PAGE_END); + + return panel; + } + + public boolean validateIp(String ip) { + Pattern pattern = Pattern.compile("^(([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\.){3}([01]?\\d\\d?|2[0-4]\\d|25[0-5])$"); + Matcher matcher = pattern.matcher(ip); + return matcher.matches(); + } + + public boolean validatePort(String port) { + try { + int pNum = Integer.parseInt(port); + if ((pNum > 0) && (pNum < 65536)) { + return true; + } + } catch (NumberFormatException e) { + } + return false; + } + + private String getDate(long date) { + if (date != 0) { + SimpleDateFormat ft = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss"); + return ft.format(new Date(date)); + } + return ""; + } + + private String getStatus(int status) { + switch (status) { + case Status.OK: // online + return "online"; + case Status.WARNING: // warning + return "varování"; + case Status.OFFLINE: // offline + return "offline"; + default: // other + return "n/a"; + } + } + +} diff --git a/jguard_client/src/jguard/client/gui/dialog/OnlineClientsDialog.java b/jguard_client/src/jguard/client/gui/dialog/OnlineClientsDialog.java new file mode 100644 index 0000000..336eae5 --- /dev/null +++ b/jguard_client/src/jguard/client/gui/dialog/OnlineClientsDialog.java @@ -0,0 +1,57 @@ +package jguard.client.gui.dialog; + +import cucky.jGuard.lib.LogFile; +import cucky.jGuard.lib.object.OnlineClients; +import java.awt.Dimension; +import java.awt.FlowLayout; +import java.io.File; +import java.io.IOException; +import javax.imageio.ImageIO; +import javax.swing.Box; +import javax.swing.BoxLayout; +import javax.swing.ImageIcon; +import javax.swing.JDialog; +import javax.swing.JLabel; +import javax.swing.JPanel; +import javax.swing.border.EmptyBorder; +import jguard.client.Client; + +public class OnlineClientsDialog extends JDialog { + + public OnlineClientsDialog() { + setTitle("Online klienti"); + setLocationRelativeTo(null); + setAlwaysOnTop(true); + + setLayout(new FlowLayout(FlowLayout.CENTER)); + + JPanel panel = new JPanel(); + panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS)); + panel.setBorder(new EmptyBorder(10, 25, 10, 25)); + if (Client.onlineClients.size() > 0) { + for (OnlineClients oc : Client.onlineClients) { + try { + JLabel line = new JLabel(" " + oc.getUsername() + " (" + oc.getIp() + ")"); + switch (oc.getPlatform()) { + case OnlineClients.PLATFORM_MOBILE: + line.setIcon(new ImageIcon(ImageIO.read(new File("img/platform_android.png")))); + break; + case OnlineClients.PLATFORM_PC: + line.setIcon(new ImageIcon(ImageIO.read(new File("img/platform_pc.png")))); + break; + } + panel.add(line); + panel.add(Box.createRigidArea(new Dimension(5, 10))); + } catch (IOException ex) { + LogFile.printErr("Online Clients dialog exception: " + ex.getMessage()); + } + } + } + + add(panel); + pack(); + + setVisible(true); + } + +} diff --git a/jguard_client/src/jguard/client/gui/dialog/ServerMessageDialog.java b/jguard_client/src/jguard/client/gui/dialog/ServerMessageDialog.java new file mode 100644 index 0000000..4134af5 --- /dev/null +++ b/jguard_client/src/jguard/client/gui/dialog/ServerMessageDialog.java @@ -0,0 +1,43 @@ +package jguard.client.gui.dialog; + +import javax.swing.JDialog; +import javax.swing.JLabel; +import javax.swing.JPanel; + + +public class ServerMessageDialog extends JDialog { + + private String text; + + public ServerMessageDialog(String text) { + + this.text = text; + + + placeComponents(); + } + + private void placeComponents() { + + setTitle("Zpráva serveru"); + + setSize(400,300); + + setLocationRelativeTo(null); + + setAlwaysOnTop(true); + + JPanel panel = new JPanel(); + + JLabel l = new JLabel(text); + + panel.add(l); + + this.add(panel); + + this.setVisible(true); + + } + + +} diff --git a/jguard_client/src/jguard/client/network/ClientAdapterHandler.java b/jguard_client/src/jguard/client/network/ClientAdapterHandler.java new file mode 100644 index 0000000..1f7eb33 --- /dev/null +++ b/jguard_client/src/jguard/client/network/ClientAdapterHandler.java @@ -0,0 +1,45 @@ +package jguard.client.network; + +import io.netty.channel.Channel; +import io.netty.channel.ChannelHandlerContext; +import io.netty.channel.SimpleChannelInboundHandler; +import cucky.jGuard.lib.LogFile; +import cucky.jGuard.lib.Message; +import jguard.client.AutoConnect; +import jguard.client.Client; +import jguard.client.ClientMessageParser; +import jguard.client.gui.Window; + +public class ClientAdapterHandler extends SimpleChannelInboundHandler { + + @Override + protected void channelRead0(ChannelHandlerContext chc, Message msg) throws Exception { + Channel currentChannel = chc.channel(); + new ClientMessageParser().parse(msg); + } + + @Override + public void channelRegistered(ChannelHandlerContext ctx) throws Exception { + super.channelRegistered(ctx); + Channel currentChannel = ctx.channel(); + } + + @Override + public void channelUnregistered(ChannelHandlerContext chc) throws Exception { + super.channelUnregistered(chc); + LogFile.printInfo("Odpojeno"); + Window.setStatusBarMessage("Odpojeno"); + } + + @Override + public void channelInactive(ChannelHandlerContext ctx) throws Exception { + super.channelInactive(ctx); + } + + @Override + public void exceptionCaught(ChannelHandlerContext chc, Throwable cause) throws Exception { + LogFile.printInfo("Server nedostupny: " + cause.getMessage()); + new AutoConnect().run(); + } + +} \ No newline at end of file diff --git a/jguard_client/src/jguard/client/network/NetworkClient.java b/jguard_client/src/jguard/client/network/NetworkClient.java new file mode 100644 index 0000000..3174adc --- /dev/null +++ b/jguard_client/src/jguard/client/network/NetworkClient.java @@ -0,0 +1,94 @@ +package jguard.client.network; + +import io.netty.bootstrap.Bootstrap; +import io.netty.channel.Channel; +import io.netty.channel.ChannelInitializer; +import io.netty.channel.ChannelPipeline; +import io.netty.channel.EventLoopGroup; +import io.netty.channel.nio.NioEventLoopGroup; +import io.netty.channel.socket.nio.NioSocketChannel; +import io.netty.handler.codec.serialization.ClassResolvers; +import io.netty.handler.codec.serialization.ObjectDecoder; +import io.netty.handler.codec.serialization.ObjectEncoder; +import io.netty.handler.ssl.SslContext; +import io.netty.handler.ssl.SslContextBuilder; +import io.netty.handler.ssl.util.InsecureTrustManagerFactory; +import cucky.jGuard.lib.LogFile; +import cucky.jGuard.lib.Message; +import io.netty.channel.ChannelOption; +import jguard.client.gui.Window; + +public class NetworkClient { + + private String server; + private int port; + + private Channel channel; + + public NetworkClient(String server, int port) { + this.server = server; + this.port = port; + } + + + public void start() { + + LogFile.printInfo("Pripojuji ...."); + Window.setStatusBarMessage("Připojuji ..."); + + EventLoopGroup group = new NioEventLoopGroup(); + + try { + SslContext sslCtx = SslContextBuilder.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE).build(); + + Bootstrap bootstrap = new Bootstrap().group(group) + .option(ChannelOption.SO_KEEPALIVE, true) + .channel(NioSocketChannel.class) + .handler(new ChannelInitializer() { + @Override + protected void initChannel(Channel channel) throws Exception { + ChannelPipeline pipeline = channel.pipeline(); + // pro zabezpecene pripojeni + pipeline.addLast("ssl", sslCtx.newHandler(channel.alloc(), NetworkClient.this.server, NetworkClient.this.port)); + + pipeline.addLast("decoder", new ObjectDecoder(ClassResolvers.weakCachingConcurrentResolver(getClass().getClassLoader()))); + pipeline.addLast("encoder", new ObjectEncoder()); + + pipeline.addLast("handler", new ClientAdapterHandler()); + } + + }); + + channel = bootstrap.connect(server, port).sync().channel(); + + } catch (Exception e) { + LogFile.printErr("Client exception: " + e.getMessage()); + } finally { + //group.shutdownGracefully(); // pokud se odkomentuje nejde se připojit k serveru + } + + } + + public void send(int type, Object obj) { + channel.writeAndFlush(new Message(type, obj)); + } + + public Channel getChannel() { + return channel; + } + + public void disconnect() { + if (channel != null) { + channel.disconnect(); + channel.close(); + } + } + + public boolean isConnected() { + try { + return channel.isRegistered(); + } catch (Exception e) { + return false; + } + } +}