Browse Source

set ui status async

Martin Kunz 4 years ago
parent
commit
15d148d73f

+ 1 - 1
.idea/compiler.xml

@@ -10,7 +10,7 @@
       </profile>
     </annotationProcessing>
     <bytecodeTargetLevel>
-      <module name="uraxis" target="1.7" />
+      <module name="uraxis" target="1.8" />
     </bytecodeTargetLevel>
   </component>
 </project>

+ 4 - 4
pom.xml

@@ -19,8 +19,8 @@
 		<!--********************************************************************-->
 		<!--******************* BEGINNING OF URCAP META DATA *******************-->
 		<urcap.symbolicname>cdp.uraxis</urcap.symbolicname>
-		<urcap.vendor>URCaps R us Inc.</urcap.vendor>
-		<urcap.contactAddress>123 URCap Street</urcap.contactAddress>
+		<urcap.vendor>CDP</urcap.vendor>
+		<urcap.contactAddress>Seestadtstraße 27</urcap.contactAddress>
 		<urcap.copyright>Copyright notice (C)</urcap.copyright>
 		<urcap.description>This is a description of the URCap</urcap.description>
 		<urcap.licenseType>License type</urcap.licenseType>
@@ -49,8 +49,8 @@
 				<artifactId>maven-compiler-plugin</artifactId>
 				<version>3.8.1</version>
 				<configuration>
-					<source>1.7</source>
-					<target>1.7</target>
+					<source>1.8</source>
+					<target>1.8</target>
 				</configuration>
 			</plugin>
 			<plugin>

+ 65 - 20
src/main/java/uraxis/MyDaemonInterface.java

@@ -3,7 +3,11 @@ package uraxis;
 import java.net.MalformedURLException;
 import java.net.URL;
 import java.util.ArrayList;
+import java.util.concurrent.CompletableFuture;
+
 import org.apache.xmlrpc.XmlRpcException;
+import org.apache.xmlrpc.XmlRpcRequest;
+import org.apache.xmlrpc.client.AsyncCallback;
 import org.apache.xmlrpc.client.XmlRpcClient;
 import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;
 
@@ -43,42 +47,83 @@ public class MyDaemonInterface {
 		return "";
 	}
 
-	public int getpos() {
+	public CompletableFuture<Integer> getpos() {
+		CompletableFuture<Integer> cf = new CompletableFuture();
 		try {
-			Object res= client.execute("getpos", new Object[]{});
-			return (int) res;
+			client.executeAsync("getpos", new Object[]{}, new AsyncCallback() {
+				@Override
+				public void handleResult(XmlRpcRequest xmlRpcRequest, Object o) {
+					cf.complete((Integer) o);
+				}
+
+				@Override
+				public void handleError(XmlRpcRequest xmlRpcRequest, Throwable throwable) {
+					cf.completeExceptionally(throwable);
+				}
+			});
 		} catch (XmlRpcException e) {
-			e.printStackTrace();
-			return -1;
+			cf.completeExceptionally(e);
 		}
+		return cf;
 	}
-	public String getstatus() {
+	public CompletableFuture<String> getstatus() {
+		CompletableFuture<String> cf = new CompletableFuture();
 		try {
-			String res= (String) client.execute("status", new Object[]{});
-			return res;
+			client.executeAsync("status", new Object[]{}, new AsyncCallback() {
+				@Override
+				public void handleResult(XmlRpcRequest xmlRpcRequest, Object o) {
+					cf.complete((String) o);
+				}
+
+				@Override
+				public void handleError(XmlRpcRequest xmlRpcRequest, Throwable throwable) {
+					cf.completeExceptionally(throwable);
+				}
+			});
 		} catch (XmlRpcException e) {
-			e.printStackTrace();
-			return "";
+			cf.completeExceptionally(e);
 		}
+		return cf;
 	}
 
-	public boolean rel(int pos, int speed) {
+	public CompletableFuture<Boolean> rel(int pos, int speed) {
+		CompletableFuture<Boolean> cf = new CompletableFuture();
 		try {
-			boolean success= (boolean) client.execute("rel", new Object[]{pos, speed});
-			return success;
+			client.executeAsync("rel", new Object[]{pos, speed}, new AsyncCallback() {
+				@Override
+				public void handleResult(XmlRpcRequest xmlRpcRequest, Object o) {
+					cf.complete((Boolean) o);
+				}
+
+				@Override
+				public void handleError(XmlRpcRequest xmlRpcRequest, Throwable throwable) {
+					cf.completeExceptionally(throwable);
+				}
+			});
 		} catch (XmlRpcException e) {
-			e.printStackTrace();
-			return false;
+			cf.completeExceptionally(e);
 		}
+		return cf;
 	}
 
-	public boolean abs(int pos, int speed) {
+	public CompletableFuture<Boolean> abs(int pos, int speed) {
+		CompletableFuture<Boolean> cf = new CompletableFuture();
+
 		try {
-			boolean success= (boolean) client.execute("abs", new Object[]{pos, speed});
-			return success;
+			client.executeAsync("abs", new Object[]{pos, speed}, new AsyncCallback() {
+				@Override
+				public void handleResult(XmlRpcRequest xmlRpcRequest, Object o) {
+					cf.complete((Boolean) o);
+				}
+
+				@Override
+				public void handleError(XmlRpcRequest xmlRpcRequest, Throwable throwable) {
+					cf.completeExceptionally(throwable);
+				}
+			});
 		} catch (XmlRpcException e) {
-			e.printStackTrace();
-			return false;
+			cf.completeExceptionally(e);
 		}
+		return cf;
 	}
 }

+ 15 - 22
src/main/java/uraxis/toolbar/MyToolbarContribution.java

@@ -9,6 +9,8 @@ import java.awt.*;
 import java.awt.event.ActionEvent;
 import java.util.*;
 import java.util.Timer;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutionException;
 
 class MyToolbarContribution implements SwingToolbarContribution {
     private static final int VERTICAL_SPACE = 10;
@@ -25,7 +27,6 @@ class MyToolbarContribution implements SwingToolbarContribution {
 
     @Override
     public void openView() {
-
         //UI updates from non-GUI threads must use EventQueue.invokeLater (or SwingUtilities.invokeLater)
         uiTimer = new Timer(true);
         uiTimer.schedule(new TimerTask() {
@@ -34,8 +35,16 @@ class MyToolbarContribution implements SwingToolbarContribution {
                 EventQueue.invokeLater(new Runnable() {
                     @Override
                     public void run() {
-                        demoToolStatus.setText("<HTML>" + get3rdPartyStatus() + "</HTML>");
-
+                        CompletableFuture<Integer> fPos = Activator.daemonInterface.getpos();
+                        CompletableFuture<String> fStatus = Activator.daemonInterface.getstatus();
+                        CompletableFuture.allOf(fPos, fStatus).whenComplete((aVoid, throwable) -> {
+                            Date now = new Date();
+                            try {
+                                updateText(String.format("Position: %d, read at %tF %tT.<br/>%s", fPos.get(), now, now, fStatus.get()));
+                            } catch (InterruptedException|ExecutionException e) {
+                                e.printStackTrace();
+                            }
+                        });
                     }
                 });
             }
@@ -173,21 +182,9 @@ class MyToolbarContribution implements SwingToolbarContribution {
     private Box createInfo() {
         Box infoBox = Box.createVerticalBox();
         infoBox.setAlignmentX(Component.CENTER_ALIGNMENT);
-//        JLabel pane1 = new JLabel();
-//        pane1.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
-//        pane1.setText("<HTML>This is a sample URCap Toolbar contribution. Feel free to use this as an example for creating new contributions.</HTML>");
-//        pane1.setBackground(infoBox.getBackground());
-//        infoBox.add(pane1);
-//
-//        JLabel pane2 = new JLabel();
-//        Locale locale = context.getAPIProvider().getSystemAPI().getSystemSettings().getLocalization().getLocale();
-//        pane2.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
-//        pane2.setText("<HTML>Currently, the robot is configured to use the Locale: " + locale.getDisplayName() + "</HTML>");
-//        infoBox.add(pane2);
-
         demoToolStatus = new JLabel();
-        demoToolStatus.setText("<HTML>" + get3rdPartyStatus() +"</HTML>");
         demoToolStatus.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
+
         infoBox.add(demoToolStatus);
         return infoBox;
     }
@@ -196,11 +193,7 @@ class MyToolbarContribution implements SwingToolbarContribution {
         return Box.createRigidArea(new Dimension(0, VERTICAL_SPACE));
     }
 
-    private String get3rdPartyStatus() {
-        Date now = new Date();
-        int curPos=Activator.daemonInterface.getpos();
-        String curStatus=Activator.daemonInterface.getstatus();
-
-        return  String.format("Position: %d, read at %tF %tT.<br/>%s", curPos, now, now, curStatus);
+    private void updateText(String text) {
+        SwingUtilities.invokeLater(() -> demoToolStatus.setText("<HTML>" + text +"</HTML>"));
     }
 }

+ 4 - 4
uraxis.iml

@@ -5,20 +5,20 @@
       <configuration manifestGenerationMode="OsmorcControlled" manifestLocation="" jarfileLocation="uraxis-1.0-SNAPSHOT.jar" outputPathType="CompilerOutputPath" bndFileLocation="" bundlorFileLocation="" bundleActivator="uraxis.Activator" bundleSymbolicName="cdp.uraxis" bundleVersion="1.0.0.SNAPSHOT" ignoreFilePattern="" useProjectDefaultManifestFileLocation="true" alwaysRebuildBundleJAR="false" doNotSynchronizeWithMaven="false">
         <additionalProperties>
           <property key="Bundle-Category" value="URCap" />
-          <property key="Bundle-Vendor" value="URCaps R us Inc." />
-          <property key="Bundle-ContactAddress" value="123 URCap Street" />
+          <property key="Bundle-Vendor" value="CDP" />
+          <property key="Bundle-ContactAddress" value="Seestadtstraße 27" />
           <property key="Bundle-Copyright" value="Copyright notice (C)" />
           <property key="Bundle-LicenseType" value="License type" />
           <property key="Bundle-Description" value="This is a description of the URCap" />
           <property key="Import-Package" value="com.ur.urcap.api*;version=&quot;[1.7.0,2.0.0)&quot;,*" />
           <property key="Bundle-Name" value="uraxis" />
-          <property key="Include-Resource" value="icons/acme_logo.png=$MODULE_DIR$/src/main/resources/icons/acme_logo.png,uraxis/installation.html=$MODULE_DIR$/src/main/resources/uraxis/installation.html" />
+          <property key="Include-Resource" value="icons/logo.png=$MODULE_DIR$/src/main/resources/icons/logo.png,uraxis/installation.html=$MODULE_DIR$/src/main/resources/uraxis/installation.html" />
         </additionalProperties>
         <additionalJARContents />
       </configuration>
     </facet>
   </component>
-  <component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_7">
+  <component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
     <output url="file://$MODULE_DIR$/target/classes" />
     <output-test url="file://$MODULE_DIR$/target/test-classes" />
     <content url="file://$MODULE_DIR$">