ConnectHandheld.java 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. package Connection;
  2. import java.awt.BorderLayout;
  3. import java.awt.Dimension;
  4. import java.awt.image.BufferedImage;
  5. import java.io.IOException;
  6. import java.net.BindException;
  7. import java.text.NumberFormat;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10. import java.util.concurrent.TimeUnit;
  11. import javax.swing.ImageIcon;
  12. import javax.swing.JButton;
  13. import javax.swing.JFormattedTextField;
  14. import javax.swing.JFrame;
  15. import javax.swing.JLabel;
  16. import javax.swing.JOptionPane;
  17. import javax.swing.JPanel;
  18. import javax.swing.JScrollPane;
  19. import javax.swing.JSplitPane;
  20. import javax.swing.JTextArea;
  21. import javax.swing.text.NumberFormatter;
  22. import api.AddOn;
  23. import classes.AbstractCpsObject;
  24. import classes.CpsUpperNode;
  25. import classes.HolonObject;
  26. import Connection.ConnectPhysical.HolonObjectStatus;
  27. import Connection.ConnectPhysical.PhysicalLinkWrapper;
  28. import Connection.socket.Server;
  29. import ui.controller.Control;
  30. import ui.view.Console;
  31. public class ConnectHandheld implements AddOn{
  32. //Holeg
  33. Control control;
  34. private HolonObject observed;
  35. //Gui
  36. private JPanel content = new JPanel();
  37. private JTextArea textArea;
  38. //TCP
  39. int port = 4242;
  40. Server server;
  41. private Console console;
  42. private boolean holonObjectConnected;
  43. private JLabel houseLabel;
  44. public static void main(String[] args)
  45. {
  46. JFrame newFrame = new JFrame("exampleWindow");
  47. ConnectHandheld instance = new ConnectHandheld();
  48. newFrame.setContentPane(instance.getPanel());
  49. newFrame.pack();
  50. newFrame.setVisible(true);
  51. newFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  52. }
  53. public ConnectHandheld() {
  54. content.setLayout(new BorderLayout());
  55. textArea = new JTextArea();
  56. textArea.setEditable(false);
  57. console = new Console();
  58. JScrollPane scrollPane = new JScrollPane(console);
  59. JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
  60. createSettingsPanel() , scrollPane);
  61. splitPane.setResizeWeight(0.0);
  62. content.add(splitPane, BorderLayout.CENTER);
  63. content.setPreferredSize(new Dimension(400,600));
  64. }
  65. private JPanel createSettingsPanel() {
  66. JPanel settingsPanel = new JPanel(null);
  67. settingsPanel.setPreferredSize(new Dimension(400, 400));
  68. settingsPanel.setMinimumSize(new Dimension(400, 225));
  69. NumberFormat format = NumberFormat.getIntegerInstance();
  70. format.setGroupingUsed(false);
  71. format.setParseIntegerOnly(true);
  72. NumberFormatter integerFormatter = new NumberFormatter(format);
  73. integerFormatter.setMinimum(0);
  74. integerFormatter.setMaximum(65535);
  75. integerFormatter.setCommitsOnValidEdit(true);
  76. JLabel portLabel = new JLabel("Port:");
  77. portLabel.setBounds(10, 20, 35, 30);
  78. settingsPanel.add(portLabel);
  79. JFormattedTextField portTF = new JFormattedTextField(integerFormatter);
  80. portTF.setText(""+port);
  81. portTF.setBounds(55 ,20, 80, 30);
  82. portTF.addPropertyChangeListener(propertyChange ->{
  83. String text = portTF.getValue().toString();
  84. text = text.replaceAll("\\s", "");
  85. port = Integer.parseInt((text));
  86. });
  87. settingsPanel.add(portTF);
  88. houseLabel = new JLabel("House Status: " + (holonObjectConnected?"Connected":"Not selected"));
  89. houseLabel.setBounds(10, 90, 220, 30);
  90. settingsPanel.add(houseLabel);
  91. JButton selectRoom1Button = new JButton("Select");
  92. selectRoom1Button.setBounds(230,95, 90, 20);
  93. selectRoom1Button.addActionListener(actionEvent -> this.selectHolonObject());
  94. settingsPanel.add(selectRoom1Button);
  95. JButton connectButton = new JButton("Start Server");
  96. connectButton.setBounds(100 ,175, 200, 50);
  97. connectButton.addActionListener(actionEvent -> connect());
  98. settingsPanel.add(connectButton);
  99. return settingsPanel;
  100. }
  101. private void connect() {
  102. if(!holonObjectConnected) {
  103. console.println("Select a HolonObject");
  104. return;
  105. }
  106. try {
  107. if(server != null) {
  108. server.stopServer();
  109. }
  110. console.println("Start Server on Port:" + port);
  111. server = new Server(port, console, observed, control);
  112. }catch(BindException e){
  113. console.println(e.getMessage());
  114. }
  115. catch (IOException e) {
  116. // TODO Auto-generated catch block
  117. e.printStackTrace();
  118. }
  119. }
  120. @Override
  121. public JPanel getPanel() {
  122. return content;
  123. }
  124. @Override
  125. public void setController(Control control) {
  126. this.control = control;
  127. }
  128. private void selectHolonObject() {
  129. List<HolonObject> holonObjectList = new ArrayList<HolonObject>();
  130. addObjectToList(control.getModel().getObjectsOnCanvas(),holonObjectList);
  131. Object[] possibilities = holonObjectList.stream().map(aCps -> new Handle<HolonObject>(aCps)).toArray();
  132. @SuppressWarnings("unchecked")
  133. Handle<HolonObject> selected = (Handle<HolonObject>) JOptionPane.showInputDialog(content, "Select HolonObject:", "HolonObject?", JOptionPane.OK_OPTION,new ImageIcon(new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB)) , possibilities, "");
  134. if(selected != null) {
  135. console.println("Selected: " + selected);
  136. observed = selected.object;
  137. holonObjectConnected = true;
  138. if(server != null) {
  139. server.setObserved(selected.object);
  140. }
  141. updateSelectionLabel();
  142. }
  143. }
  144. private void updateSelectionLabel() {
  145. houseLabel.setText("House Status: " + (holonObjectConnected?"Connected":"Not selected"));
  146. }
  147. private void addObjectToList(List<AbstractCpsObject> listToSearch, List<HolonObject> listToAdd){
  148. for (AbstractCpsObject aCps : listToSearch) {
  149. if (aCps instanceof HolonObject) listToAdd.add((HolonObject) aCps);
  150. else if(aCps instanceof CpsUpperNode) {
  151. addObjectToList(((CpsUpperNode)aCps).getNodes(),listToAdd);
  152. }
  153. }
  154. }
  155. private class Handle<T>{
  156. public T object;
  157. Handle(T object){
  158. this.object = object;
  159. }
  160. public String toString() {
  161. return object.toString();
  162. }
  163. }
  164. }