FlowTableWindow.java 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package holeg.ui;
  2. import holeg.HolegGateway;
  3. import holeg.HolegPowerFlowContext;
  4. import javax.swing.*;
  5. import java.awt.*;
  6. import java.io.ByteArrayOutputStream;
  7. import java.io.IOException;
  8. import java.io.PrintStream;
  9. import java.nio.charset.StandardCharsets;
  10. public class FlowTableWindow extends JDialog {
  11. private JTextArea textArea;
  12. public FlowTableWindow(Frame owner) {
  13. super(owner);
  14. setTitle("HOLEG: Power flow table");
  15. setSize(500, 600);
  16. textArea = (JTextArea) add(new JTextArea("Updating..."));
  17. }
  18. public void update() {
  19. try (ByteArrayOutputStream stream = new ByteArrayOutputStream()) {
  20. final String utf8 = StandardCharsets.UTF_8.name();
  21. try (PrintStream ps = new PrintStream(stream, true, utf8)) {
  22. for (HolegPowerFlowContext context : HolegGateway.getALlContext()) {
  23. synchronized (context.lock) {
  24. if (context.result != null && context.problem != null) {
  25. context.result.printTo(context.problem, ps, "M");
  26. }
  27. }
  28. }
  29. }
  30. String data = stream.toString(utf8);
  31. textArea.setText(data);
  32. } catch (IOException e) {
  33. e.printStackTrace();
  34. textArea.setText(e.toString());
  35. }
  36. doLayout();
  37. }
  38. }