LinkEditorPanel.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package de.tu_darmstadt.tk.SmartHomeNetworkSim.view.popups;
  2. import java.awt.Dimension;
  3. import java.awt.Window;
  4. import javax.swing.JFrame;
  5. import de.tu_darmstadt.tk.SmartHomeNetworkSim.control.Controller;
  6. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Link;
  7. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Model;
  8. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.SmartDevice;
  9. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.simpleImplementation.SimpleLink;
  10. import javax.swing.JPanel;
  11. import javax.swing.JLabel;
  12. import javax.swing.JScrollPane;
  13. import javax.swing.ScrollPaneConstants;
  14. import javax.swing.SwingConstants;
  15. import javax.swing.JButton;
  16. @SuppressWarnings("serial")
  17. public class LinkEditorPanel extends JScrollPane{
  18. JPanel content;
  19. private Window frame;
  20. private SmartDevice device;
  21. private Controller controller;
  22. private String deviceNames;
  23. public LinkEditorPanel(SmartDevice deviceWithLinks, Controller control) {
  24. this.device = deviceWithLinks;
  25. this.controller = control;
  26. this.setPreferredSize(new Dimension(630, 400));
  27. content = new JPanel();
  28. setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
  29. setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
  30. content.setPreferredSize(new Dimension(630, 80+20*device.getLinks().size()));
  31. this.setViewportView(content);
  32. content.setLayout(null);
  33. JLabel lblDescription = new JLabel("Links of Device: "+device.getName());
  34. lblDescription.setToolTipText(device.getName());
  35. lblDescription.setBounds(10, 10, 268, 16);
  36. content.add(lblDescription);
  37. JLabel lblAddLink = new JLabel("Add Link:");
  38. lblAddLink.setHorizontalAlignment(SwingConstants.RIGHT);
  39. lblAddLink.setBounds(12, 42, 138, 16);
  40. content.add(lblAddLink);
  41. JButton btnAddLink= new JButton("Add Link");
  42. btnAddLink.setBounds(162, 38, 116, 25);
  43. content.add(btnAddLink);
  44. btnAddLink.addActionListener(a->System.out.println("WARNING: Not implemented yet"));
  45. //int i = 0;
  46. int currentHeight = 70;
  47. for(Link l:device.getLinks()){
  48. JLabel lblLinkA = new JLabel(l.getName()+":");
  49. lblLinkA.setToolTipText(l.getName());
  50. lblLinkA.setHorizontalAlignment(SwingConstants.RIGHT);
  51. lblLinkA.setBounds(10, currentHeight, 150, 18);
  52. content.add(lblLinkA);
  53. JButton btnEditLink = new JButton("Edit");
  54. btnEditLink.setBounds(160, currentHeight, 80, 18);
  55. content.add(btnEditLink);
  56. btnEditLink.addActionListener(a -> new LinkCreationDialog(l, controller, this));
  57. deviceNames = "Devices: ";
  58. if(l.getDevices().isEmpty())
  59. deviceNames += "empty ";
  60. else
  61. l.getDevices().forEach(s->deviceNames+=s.getName()+", ");
  62. JLabel lblLinkADevices = new JLabel(deviceNames.substring(0, deviceNames.length()-2));
  63. lblLinkADevices.setToolTipText(deviceNames);
  64. lblLinkADevices.setBounds(240, currentHeight, 350, 18);
  65. content.add(lblLinkADevices);
  66. //i++;
  67. currentHeight+=20;
  68. }
  69. }
  70. /**
  71. * Test the Panel
  72. */
  73. private static void testGUI() {
  74. JFrame frame = new JFrame("Links Panel");
  75. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  76. LinkEditorPanel panel;
  77. SmartDevice devicesOfLink = new SmartDevice("Test");
  78. for(int i = 0; i<30; i++)
  79. devicesOfLink.addLink(new SimpleLink("Link"+i));
  80. panel = new LinkEditorPanel(devicesOfLink, new Controller(new Model()));
  81. panel.setFrame(frame);
  82. frame.setContentPane(panel);
  83. frame.pack();
  84. frame.setVisible(true);
  85. }
  86. public static void main(String[] args) {
  87. javax.swing.SwingUtilities.invokeLater(new Runnable() {
  88. public void run() {
  89. testGUI();
  90. }
  91. });
  92. }
  93. /**
  94. * @return the frame
  95. */
  96. public Window getFrame() {
  97. return frame;
  98. }
  99. /**
  100. * @param frame the frame to set
  101. */
  102. public void setFrame(Window frame) {
  103. this.frame = frame;
  104. }
  105. }