Browse Source

remove hardcoded stuff (ip, port)
add swing based nodes (wip)

Martin Kunz 4 years ago
parent
commit
b9ba3b257c

+ 6 - 0
src/main/java/uraxis/Activator.java

@@ -1,16 +1,20 @@
 package uraxis;
 
+import com.ur.urcap.api.contribution.toolbar.swing.SwingToolbarService;
 import org.osgi.framework.BundleActivator;
 import org.osgi.framework.BundleContext;
 import com.ur.urcap.api.contribution.InstallationNodeService;
 import com.ur.urcap.api.contribution.ProgramNodeService;
 import com.ur.urcap.api.contribution.DaemonService;
+import uraxis.toolbar.MyToolbarService;
 
 /**
  * Hello world activator for the OSGi bundle URCAPS contribution
  *
  */
 public class Activator implements BundleActivator {
+	public static MyDaemonInterface daemonInterface;
+
 	@Override
 	public void start(BundleContext context) throws Exception {
 		System.out.println("Activator says Hello World!");
@@ -20,6 +24,8 @@ public class Activator implements BundleActivator {
 		context.registerService(InstallationNodeService.class, installationNodeService, null);
 		context.registerService(ProgramNodeService.class, new MyDaemonProgramNodeService(), null);
 		context.registerService(DaemonService.class, daemonService, null);
+		context.registerService(SwingToolbarService.class, new MyToolbarService(), null);
+
 	}
 
 	@Override

+ 9 - 4
src/main/java/uraxis/MyDaemonInstallationNodeContribution.java

@@ -32,8 +32,9 @@ public class MyDaemonInstallationNodeContribution implements InstallationNodeCon
 	public MyDaemonInstallationNodeContribution(MyDaemonDaemonService daemonService, DataModel model) {
 		// this.daemonService = daemonService;
 		this.model = model;
-		daemonInterface = new MyDaemonInterface("10.0.31.42", 8082);
+		daemonInterface = new MyDaemonInterface(getIP(), Integer.parseInt(getPort()));
 		// applyDesiredDaemonStatus();
+        Activator.daemonInterface=daemonInterface;
 	}
 
 	@Input(id = POPUPTITLE_KEY)
@@ -95,7 +96,6 @@ public class MyDaemonInstallationNodeContribution implements InstallationNodeCon
 		}
 	}
 
-
 	@Input(id = "btnEnableDaemon")
 	public void onStartClick(InputEvent event) {
 		if (event.getEventType() == InputEvent.EventType.ON_CHANGE) {
@@ -118,6 +118,7 @@ public class MyDaemonInstallationNodeContribution implements InstallationNodeCon
 		disableDaemonButton.setText("Stop daemon");
 		popupTitleField.setText(getPopupTitle());
 		refTextIP.setText(getIP());
+		refTextPort.setText(getPort());
 
 		//UI updates from non-GUI threads must use EventQueue.invokeLater (or SwingUtilities.invokeLater)
 		uiTimer = new Timer(true);
@@ -190,7 +191,7 @@ public class MyDaemonInstallationNodeContribution implements InstallationNodeCon
 
 	@Override
 	public void generateScript(ScriptWriter writer) {
-		writer.assign(XMLRPC_VARIABLE, "rpc_factory(\"xmlrpc\", \"http://"+ model.get(IP,"127.0.0.1")+":"+ model.get(PORT,8082)+"\")");
+		writer.assign(XMLRPC_VARIABLE, "rpc_factory(\"xmlrpc\", \"http://"+ getIP()+":"+ getPort()+"\")");
 	}
 
 	public String getPopupTitle() {
@@ -221,7 +222,7 @@ public class MyDaemonInstallationNodeContribution implements InstallationNodeCon
 	}
 
 	public String getPort() {
-		return  model.get(PORT, "127.0.0.1");
+		return  model.get(PORT, "8080");
 	}
 
 	public void setPort(String ip) {
@@ -288,4 +289,8 @@ public class MyDaemonInstallationNodeContribution implements InstallationNodeCon
 	}
 
 	public MyDaemonInterface getDaemonInterface() {return daemonInterface; }
+
+    public boolean isDefined() {
+		return true;
+    }
 }

+ 23 - 0
src/main/java/uraxis/MyDaemonInterface.java

@@ -42,6 +42,29 @@ public class MyDaemonInterface {
 		return "";
 	}
 
+	public boolean rel(int val) {
+		try {
+			ArrayList l = new ArrayList();
+			l.add(val);
+
+			client.execute("rel", l);
+			return true;
+		} catch (XmlRpcException e) {
+			return false;
+		}
+	}
+
+	public boolean abs(int val) {
+		try {
+			ArrayList l = new ArrayList();
+			l.add(val);
+			client.execute("abs", l);
+			return true;
+		} catch (XmlRpcException e) {
+			return false;
+		}
+	}
+
 //	public String getTitle() throws XmlRpcException, UnknownResponseException {
 //		Object result = client.execute("get_title", new ArrayList<String>());
 //		return processString(result);

+ 15 - 9
src/main/java/uraxis/MyDaemonProgramNodeContribution.java

@@ -82,11 +82,12 @@ public class MyDaemonProgramNodeContribution implements ProgramNodeContribution
 		if(radioAbsButton.isSelected())
 			model.set(ABSOLUTE, true);
 		else model.set(ABSOLUTE, false);
+		System.out.println("closeView " + radioAbsButton.isSelected());
 	}
 
 	@Override
 	public String getTitle() {
-		return "My Daemon: " + (model.isSet(POS) ? getPos() : "");
+		return "Axis " + (isAbsolute()?"Abs":"Rel") + (model.isSet(POS) ? getPos() : "");
 	}
 
 	@Override
@@ -99,13 +100,18 @@ public class MyDaemonProgramNodeContribution implements ProgramNodeContribution
 	public void generateScript(ScriptWriter writer) {
 		// Interact with the daemon process through XML-RPC calls
 		// Note, alternatively plain sockets can be used.
-//		if(radioRelButton.isSelected()) {
-//			writer.appendLine( getInstallation().getXMLRPCVariable() + ".rel(\"" + getName() + "\")");
-//		}
-//		else if(radioAbsButton.isSelected()) {
-//			writer.appendLine( getInstallation().getXMLRPCVariable() + ".abs(\"" + getName() + "\")");
-//		}
-		writer.appendLine( getInstallation().getXMLRPCVariable() + ".rel(\"" + getPos() + "\")");
+        //		if(radioRelButton.isSelected()) {
+        //			writer.appendLine( getInstallation().getXMLRPCVariable() + ".rel(\"" + getName() + "\")");
+        //		}
+        //		else if(radioAbsButton.isSelected()) {
+        //			writer.appendLine( getInstallation().getXMLRPCVariable() + ".abs(\"" + getName() + "\")");
+        //		}
+		if(isAbsolute()) {
+			writer.appendLine( getInstallation().getXMLRPCVariable() + ".abs(\"" + getPos() + "\")");
+
+		}
+		else
+			writer.appendLine( getInstallation().getXMLRPCVariable() + ".rel(\"" + getPos() + "\")");
 		writer.writeChildren();
 	}
 
@@ -118,7 +124,7 @@ public class MyDaemonProgramNodeContribution implements ProgramNodeContribution
 		return model.get(POS, "0");
 	}
 
-	private void setPos(String name) {
+    private void setPos(String name) {
 		if ("".equals(name)){
 			model.remove(POS);
 		}else{

+ 2 - 2
src/main/java/uraxis/MyDaemonProgramNodeService.java

@@ -14,12 +14,12 @@ public class MyDaemonProgramNodeService implements ProgramNodeService {
 
 	@Override
 	public String getId() {
-		return "MyDaemonNode";
+		return "FestoAxisNode";
 	}
 
 	@Override
 	public String getTitle() {
-		return "My Daemon";
+		return "Axis";
 	}
 
 	@Override

+ 109 - 0
src/main/java/uraxis/programnodes/AbsoluteNodeContribution.java

@@ -0,0 +1,109 @@
+package uraxis.programnodes;
+
+import com.ur.urcap.api.contribution.ProgramNodeContribution;
+import com.ur.urcap.api.contribution.program.ProgramAPIProvider;
+import com.ur.urcap.api.domain.ProgramAPI;
+import com.ur.urcap.api.domain.data.DataModel;
+import com.ur.urcap.api.domain.script.ScriptWriter;
+import com.ur.urcap.api.domain.undoredo.UndoRedoManager;
+import com.ur.urcap.api.domain.undoredo.UndoableChanges;
+import com.ur.urcap.api.domain.userinteraction.keyboard.KeyboardInputCallback;
+import com.ur.urcap.api.domain.userinteraction.keyboard.KeyboardInputFactory;
+import com.ur.urcap.api.domain.userinteraction.keyboard.KeyboardTextInput;
+import uraxis.MyDaemonInstallationNodeContribution;
+
+public class AbsoluteNodeContribution implements ProgramNodeContribution {
+    private static final String NAME = "name";
+
+    private final ProgramAPI programAPI;
+    private final UndoRedoManager undoRedoManager;
+    private final KeyboardInputFactory keyboardFactory;
+
+    private final AbsoluteNodeView view;
+    private final DataModel model;
+
+    public AbsoluteNodeContribution(ProgramAPIProvider apiProvider, AbsoluteNodeView view, DataModel model) {
+        this.programAPI = apiProvider.getProgramAPI();
+        this.undoRedoManager = apiProvider.getProgramAPI().getUndoRedoManager();
+        this.keyboardFactory = apiProvider.getUserInterfaceAPI().getUserInteraction().getKeyboardInputFactory();
+
+        this.view = view;
+        this.model = model;
+    }
+
+    @Override
+    public void openView() {
+        view.setPopupText(getName());
+        updatePopupMessageAndPreview();
+    }
+
+    @Override
+    public void closeView() {
+    }
+
+    @Override
+    public String getTitle() {
+        return "Hello World Swing: " + (model.isSet(NAME) ? getName() : "");
+    }
+
+    @Override
+    public boolean isDefined() {
+        return getInstallation().isDefined() && !getName().isEmpty();
+    }
+
+    @Override
+    public void generateScript(ScriptWriter writer) {
+        // Directly generate this Program Node's popup message + access the popup title through a global variable
+        writer.appendLine("popup(\"" + generatePopupMessage() + "\", hello_world_swing_popup_title, False, False, blocking=True)");
+        writer.writeChildren();
+    }
+
+    public KeyboardTextInput getKeyboardForTextField() {
+        KeyboardTextInput keyboardInput = keyboardFactory.createStringKeyboardInput();
+        keyboardInput.setInitialValue(getName());
+        return keyboardInput;
+    }
+
+    public KeyboardInputCallback<String> getCallbackForTextField() {
+        return new KeyboardInputCallback<String>() {
+            @Override
+            public void onOk(String value) {
+                setPopupTitle(value);
+                view.setPopupText(value);
+            }
+        };
+    }
+
+    public void setPopupTitle(final String value) {
+        undoRedoManager.recordChanges(new UndoableChanges() {
+            @Override
+            public void executeChanges() {
+                if ("".equals(value)) {
+                    model.remove(NAME);
+                } else {
+                    model.set(NAME, value);
+                }
+            }
+        });
+
+        updatePopupMessageAndPreview();
+    }
+
+    private String generatePopupMessage() {
+        return model.isSet(NAME) ? "Hello " + getName() + ", welcome to PolyScope!" : "No name set";
+    }
+
+    private void updatePopupMessageAndPreview() {
+        view.setMessagePreview(generatePopupMessage());
+        view.setTitlePreview(getInstallation().isDefined() ? getInstallation().getPopupTitle() : "No title set");
+    }
+
+    private String getName() {
+        return model.get(NAME, "");
+    }
+
+    private MyDaemonInstallationNodeContribution getInstallation() {
+        return programAPI.getInstallationNode(MyDaemonInstallationNodeContribution.class);
+    }
+
+}

+ 116 - 0
src/main/java/uraxis/programnodes/AbsoluteNodeView.java

@@ -0,0 +1,116 @@
+package uraxis.programnodes;
+
+import com.ur.urcap.api.contribution.ContributionProvider;
+import com.ur.urcap.api.contribution.program.swing.SwingProgramNodeView;
+import com.ur.urcap.api.domain.userinteraction.keyboard.KeyboardTextInput;
+
+import javax.swing.Box;
+import javax.swing.BoxLayout;
+import javax.swing.JLabel;
+import javax.swing.JPanel;
+import javax.swing.JTextField;
+import java.awt.Component;
+import java.awt.Dimension;
+import java.awt.Font;
+import java.awt.event.MouseAdapter;
+import java.awt.event.MouseEvent;
+
+public class AbsoluteNodeView implements SwingProgramNodeView<AbsoluteNodeContribution>{
+
+    private final Style style;
+    private JTextField jTextField;
+    private JLabel previewTitle;
+    private JLabel previewMessage;
+
+    public AbsoluteNodeView(Style style) {
+        this.style = style;
+    }
+
+    @Override
+    public void buildUI(JPanel jPanel, final ContributionProvider<AbsoluteNodeContribution> provider) {
+        jPanel.setLayout(new BoxLayout(jPanel, BoxLayout.Y_AXIS));
+
+        jPanel.add(createInfo());
+        jPanel.add(createVerticalSpacing(style.getVerticalSpacing()));
+        jPanel.add(createInput(provider));
+        jPanel.add(createVerticalSpacing(style.getExtraLargeVerticalSpacing()));
+        jPanel.add(createPreview());
+    }
+
+    private Box createInfo() {
+        Box infoBox = Box.createHorizontalBox();
+        infoBox.setAlignmentX(Component.LEFT_ALIGNMENT);
+        infoBox.add(new JLabel("This program node will open a popup on execution."));
+        return infoBox;
+    }
+
+    private Box createInput(final ContributionProvider<AbsoluteNodeContribution> provider) {
+        Box inputBox = Box.createHorizontalBox();
+        inputBox.setAlignmentX(Component.LEFT_ALIGNMENT);
+        inputBox.add(new JLabel("Enter your name:"));
+        inputBox.add(createHorizontalSpacing());
+
+        jTextField = new JTextField();
+        jTextField.setFocusable(false);
+        jTextField.setPreferredSize(style.getInputfieldSize());
+        jTextField.setMaximumSize(jTextField.getPreferredSize());
+        jTextField.addMouseListener(new MouseAdapter() {
+            @Override
+            public void mousePressed(MouseEvent e) {
+                KeyboardTextInput keyboardInput = provider.get().getKeyboardForTextField();
+                keyboardInput.show(jTextField, provider.get().getCallbackForTextField());
+            }
+        });
+
+        inputBox.add(jTextField);
+        return inputBox;
+    }
+
+    private Box createPreview() {
+        Box previewBox = Box.createVerticalBox();
+        JLabel preview = new JLabel("Preview");
+        preview.setFont(preview.getFont().deriveFont(Font.BOLD, style.getSmallHeaderFontSize()));
+
+        Box titleBox = Box.createHorizontalBox();
+        titleBox.setAlignmentX(Component.LEFT_ALIGNMENT);
+        titleBox.add(new JLabel("Title:"));
+        titleBox.add(createHorizontalSpacing());
+        previewTitle = new JLabel("my title");
+        titleBox.add(previewTitle);
+
+        Box messageBox = Box.createHorizontalBox();
+        messageBox.setAlignmentX(Component.LEFT_ALIGNMENT);
+        messageBox.add(new JLabel("Message:"));
+        messageBox.add(createHorizontalSpacing());
+        previewMessage = new JLabel("my message");
+        messageBox.add(previewMessage);
+
+        previewBox.add(preview);
+        previewBox.add(createVerticalSpacing(style.getLargeVerticalSpacing()));
+        previewBox.add(titleBox);
+        previewBox.add(createVerticalSpacing(style.getVerticalSpacing()));
+        previewBox.add(messageBox);
+
+        return previewBox;
+    }
+
+    private Component createVerticalSpacing(int height) {
+        return Box.createRigidArea(new Dimension(0, height));
+    }
+
+    private Component createHorizontalSpacing() {
+        return Box.createRigidArea(new Dimension(style.getHorizontalSpacing(), 0));
+    }
+
+    public void setPopupText(String popupText) {
+        jTextField.setText(popupText);
+    }
+
+    public void setMessagePreview(String message) {
+        previewMessage.setText(message);
+    }
+
+    public void setTitlePreview(String title) {
+        previewTitle.setText(title);
+    }
+}

+ 33 - 0
src/main/java/uraxis/programnodes/Style.java

@@ -0,0 +1,33 @@
+package uraxis.programnodes;
+
+import java.awt.*;
+
+public abstract class Style {
+    private static final int HORIZONTAL_SPACING = 10;
+    private static final int VERTICAL_SPACING = 10;
+    private static final int LARGE_VERTICAL_SPACING = 25;
+    private static final int XLARGE_VERTICAL_SPACING = 50;
+    private static final int SMALL_HEADER_FONT_SIZE = 16;
+
+    public int getHorizontalSpacing() {
+        return HORIZONTAL_SPACING;
+    }
+
+    public int getVerticalSpacing() {
+        return VERTICAL_SPACING;
+    }
+
+    public int getExtraLargeVerticalSpacing() {
+        return XLARGE_VERTICAL_SPACING;
+    }
+
+    public int getLargeVerticalSpacing() {
+        return LARGE_VERTICAL_SPACING;
+    }
+
+    public int getSmallHeaderFontSize() {
+        return SMALL_HEADER_FONT_SIZE;
+    }
+
+    public abstract Dimension getInputfieldSize();
+}

+ 138 - 0
src/main/java/uraxis/toolbar/MyToolbarContribution.java

@@ -0,0 +1,138 @@
+package uraxis.toolbar;
+
+import com.ur.urcap.api.contribution.toolbar.ToolbarContext;
+import com.ur.urcap.api.contribution.toolbar.swing.SwingToolbarContribution;
+import uraxis.Activator;
+
+import javax.swing.*;
+import java.awt.Component;
+import java.awt.Dimension;
+import java.awt.Font;
+import java.awt.event.ActionEvent;
+import java.util.Date;
+import java.util.Locale;
+import java.util.Random;
+
+class MyToolbarContribution implements SwingToolbarContribution {
+    private static final int VERTICAL_SPACE = 10;
+    private static final int HEADER_FONT_SIZE = 24;
+
+    private final ToolbarContext context;
+    private JLabel demoToolStatus;
+
+    MyToolbarContribution(ToolbarContext context) {
+        this.context = context;
+    }
+
+    @Override
+    public void openView() {
+        demoToolStatus.setText("<HTML>" + get3rdPartyStatus() + "</HTML>");
+    }
+
+    @Override
+    public void closeView() {
+    }
+
+    public void buildUI(JPanel jPanel) {
+        jPanel.setLayout(new BoxLayout(jPanel, BoxLayout.Y_AXIS));
+        jPanel.add(createHeader());
+        jPanel.add(createVerticalSpace());
+        jPanel.add(createButtons());
+        jPanel.add(createInfo());
+    }
+
+    private Box createHeader() {
+        Box headerBox = Box.createHorizontalBox();
+        headerBox.setAlignmentX(Component.CENTER_ALIGNMENT);
+
+        JLabel header = new JLabel("Festo Axis Control");
+        header.setFont(header.getFont().deriveFont(Font.BOLD, HEADER_FONT_SIZE));
+        headerBox.add(header);
+        return headerBox;
+    }
+
+    private Box createButtons() {
+        Box box = Box.createHorizontalBox();
+        JButton left100 = new JButton("<<<100");
+        left100.addActionListener(new AbstractAction() {
+            @Override
+            public void actionPerformed(ActionEvent e) {
+                Activator.daemonInterface.rel(-100);
+            }
+        });
+        box.add(left100);
+        JButton left10 = new JButton("<<10");
+        left10.addActionListener(new AbstractAction() {
+            @Override
+            public void actionPerformed(ActionEvent e) {
+                Activator.daemonInterface.rel(-10);
+            }
+        });
+        box.add(left10);
+        JButton left1 = new JButton("<1");
+        left1.addActionListener(new AbstractAction() {
+            @Override
+            public void actionPerformed(ActionEvent e) {
+                Activator.daemonInterface.rel(-1);
+            }
+        });
+        box.add(left1);
+        JButton right1 = new JButton("1>");
+        right1.addActionListener(new AbstractAction() {
+            @Override
+            public void actionPerformed(ActionEvent e) {
+                Activator.daemonInterface.rel(1);
+            }
+        });
+        box.add(right1);
+        JButton right10 = new JButton("10>>");
+        right10.addActionListener(new AbstractAction() {
+            @Override
+            public void actionPerformed(ActionEvent e) {
+                Activator.daemonInterface.rel(10);
+            }
+        });
+        box.add(right10);
+        JButton right100 = new JButton("100>>>");
+        right100.addActionListener(new AbstractAction() {
+            @Override
+            public void actionPerformed(ActionEvent e) {
+                Activator.daemonInterface.rel(100);
+            }
+        });
+        box.add(right100);
+        return box;
+    }
+
+    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;
+    }
+
+    private Component createVerticalSpace() {
+        return Box.createRigidArea(new Dimension(0, VERTICAL_SPACE));
+    }
+
+    private String get3rdPartyStatus() {
+        Date now = new Date();
+        int number = new Random().nextInt(10) + 20;
+        return  String.format("Tool status reading: %d, read at %tF %tT.", number, now, now);
+    }
+}

+ 27 - 0
src/main/java/uraxis/toolbar/MyToolbarService.java

@@ -0,0 +1,27 @@
+package uraxis.toolbar;
+
+import com.ur.urcap.api.contribution.toolbar.ToolbarConfiguration;
+import com.ur.urcap.api.contribution.toolbar.ToolbarContext;
+import com.ur.urcap.api.contribution.toolbar.swing.SwingToolbarContribution;
+import com.ur.urcap.api.contribution.toolbar.swing.SwingToolbarService;
+
+import javax.swing.Icon;
+import javax.swing.ImageIcon;
+
+public class MyToolbarService implements SwingToolbarService {
+
+    @Override
+    public Icon getIcon() {
+        return new ImageIcon(getClass().getResource("/icons/acme_logo.png"));
+    }
+
+    @Override
+    public void configureContribution(ToolbarConfiguration configuration) {
+        configuration.setToolbarHeight(200);
+    }
+
+    @Override
+    public SwingToolbarContribution createToolbar(ToolbarContext context) {
+        return new MyToolbarContribution(context);
+    }
+}

BIN
src/main/resources/icons/acme_logo.png


+ 2 - 2
src/main/resources/uraxis/installation.html

@@ -29,15 +29,15 @@
 			<label  id="lblStatus" style="width: 400px;height: 200px;vertical-align: top"></label>
 			<label id="lblDaemonStatus" style="width: 200px">Status of daemon</label>
 			<br>
+			<label for="textIP">Daemon IP</label>
 			<input id="textIP" type="text" style="font-size: 18px;" />
+			<label for="textIP">Daemon Port</label>
 			<input id="textPort" type="text" style="font-size: 18px;" />
 			<br>
 			<input id="btnRef" type="button" style="font-size: 18px;" value="ref"/>
 			<br>
 			<input id="numberPos" type="number"  style="font-size: 18px;"/>
 			<input id="btnPos" type="button" style="font-size: 18px;" value="pos"/>
-
-
 			<br>
 		</form>
 	</body>