123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219 |
- package uraxis.toolbar;
- import com.ur.urcap.api.contribution.toolbar.ToolbarContext;
- import com.ur.urcap.api.contribution.toolbar.swing.SwingToolbarContribution;
- import com.ur.urcap.api.domain.io.DigitalIO;
- import com.ur.urcap.api.domain.io.Register;
- import uraxis.Activator;
- import javax.swing.*;
- 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;
- private static final int HEADER_FONT_SIZE = 24;
- private final ToolbarContext context;
- private final IOHandler ioModel;
- private Register positionRegister;
- private int positionIndex=-1;
- private JLabel demoToolStatus;
- private Timer uiTimer;
- HashMap<String, JCheckBox> boxes=new HashMap<>();
- MyToolbarContribution(ToolbarContext context) {
- this.context = context;
- this.ioModel = new IOHandler(context.getAPIProvider().getApplicationAPI().getIOModel());
- }
- @Override
- public void openView() {
- this.positionRegister = this.ioModel.getRegisterIOCustom("position");
- this.positionIndex=IOHandler.getIOIndex(this.positionRegister);
- //UI updates from non-GUI threads must use EventQueue.invokeLater (or SwingUtilities.invokeLater)
- uiTimer = new Timer(true);
- uiTimer.schedule(new TimerTask() {
- @Override
- public void run() {
- EventQueue.invokeLater(new Runnable() {
- @Override
- public void run() {
- CompletableFuture<Integer> fPos = Activator.daemonInterface.getpos();
- CompletableFuture<Map<String, Object>> fStatus = Activator.daemonInterface.getstatus();
- CompletableFuture.allOf(fPos, fStatus).whenComplete((aVoid, throwable) -> {
- Date now = new Date();
- try {
- Map<String, Object> asd = fStatus.get();
- for(JCheckBox cb: boxes.values()) {
- String cbName=cb.getName();
- Object val= asd.get(cbName);
- if(val instanceof Boolean) {
- cb.setSelected((Boolean) val);
- }
- }
- //updateText(String.format("Position: %d, read at %tF %tT.<br/>%s", fPos.get(), now, now, fStatus.get()));
- updateText(String.format("Position: %d, read at %tF %tT.<br/>Reg: ", fPos.get(), now, now, positionRegister.getDefaultName()));
- } catch (InterruptedException|ExecutionException e) {
- e.printStackTrace();
- }
- });
- }
- });
- }
- }, 0, 1000);
- }
- @Override
- public void closeView() {
- uiTimer.cancel();
- }
- 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());
- jPanel.add(createCheckboxes());
- }
- private Box createCheckboxes() {
- Box hbox=Box.createHorizontalBox();
- hbox.setAlignmentY(Component.TOP_ALIGNMENT);
- {
- Box vbox = Box.createVerticalBox();
- vbox.setAlignmentY(Component.TOP_ALIGNMENT);
- vbox.add(newCheckbox("opm1"));
- vbox.add(newCheckbox("opm2"));
- vbox.add(newCheckbox("fct"));
- vbox.add(newCheckbox("rdyen"));
- vbox.add(newCheckbox("fault"));
- vbox.add(newCheckbox("warn"));
- vbox.add(newCheckbox("open"));
- vbox.add(newCheckbox("enabled"));
- hbox.add(vbox);
- }
- {
- Box vbox = Box.createVerticalBox();
- vbox.setAlignmentY(Component.TOP_ALIGNMENT);
- vbox.add(newCheckbox("ref"));
- vbox.add(newCheckbox("still"));
- vbox.add(newCheckbox("dev"));
- vbox.add(newCheckbox("mov"));
- vbox.add(newCheckbox("teach"));
- vbox.add(newCheckbox("mc"));
- vbox.add(newCheckbox("ack"));
- vbox.add(newCheckbox("halt"));
- hbox.add(vbox);
- }
- {
- Box vbox = Box.createVerticalBox();
- vbox.setAlignmentY(Component.TOP_ALIGNMENT);
- vbox.add(newCheckbox("func"));
- vbox.add(newCheckbox("fgrp"));
- vbox.add(newCheckbox("fnum"));
- vbox.add(newCheckbox("com"));
- vbox.add(newCheckbox("abs"));
- vbox.add(newCheckbox("istMoment"));
- vbox.add(newCheckbox("istPosition"));
- hbox.add(vbox);
- }
- return hbox;
- }
- private JCheckBox newCheckbox(String name) {
- JCheckBox cb=new JCheckBox(name);
- cb.setName(name);
- boxes.put(name, cb);
- return cb;
- }
- 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("<<<1000");
- left100.addActionListener(new AbstractAction() {
- @Override
- public void actionPerformed(ActionEvent e) {
- Activator.daemonInterface.rel(-1000, 20);
- }
- });
- box.add(left100);
- JButton left10 = new JButton("<<100");
- left10.addActionListener(new AbstractAction() {
- @Override
- public void actionPerformed(ActionEvent e) {
- Activator.daemonInterface.rel(-100, 20);
- }
- });
- box.add(left10);
- JButton left1 = new JButton("<10");
- left1.addActionListener(new AbstractAction() {
- @Override
- public void actionPerformed(ActionEvent e) {
- Activator.daemonInterface.rel(-10, 20);
- }
- });
- box.add(left1);
- JButton right1 = new JButton("10>");
- right1.addActionListener(new AbstractAction() {
- @Override
- public void actionPerformed(ActionEvent e) {
- Activator.daemonInterface.rel(10, 20);
- }
- });
- box.add(right1);
- JButton right10 = new JButton("100>>");
- right10.addActionListener(new AbstractAction() {
- @Override
- public void actionPerformed(ActionEvent e) {
- Activator.daemonInterface.rel(100, 20);
- }
- });
- box.add(right10);
- JButton right100 = new JButton("1000>>>");
- right100.addActionListener(new AbstractAction() {
- @Override
- public void actionPerformed(ActionEvent e) {
- Activator.daemonInterface.rel(1000, 20);
- }
- });
- box.add(right100);
- return box;
- }
- private Box createInfo() {
- Box infoBox = Box.createVerticalBox();
- infoBox.setAlignmentX(Component.CENTER_ALIGNMENT);
- demoToolStatus = new JLabel();
- 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 void updateText(String text) {
- SwingUtilities.invokeLater(() -> demoToolStatus.setText("<HTML>" + text +"</HTML>"));
- }
- }
|