Advertisement
Guest User

UI

a guest
Apr 27th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.26 KB | None | 0 0
  1. package gui;
  2.  
  3. import sfg.MasonSolver;
  4. import sfg.SFG;
  5.  
  6. import javax.swing.*;
  7. import javax.swing.table.DefaultTableModel;
  8.  
  9. import java.awt.*;
  10. import java.awt.event.ActionEvent;
  11. import java.awt.event.ActionListener;
  12. import java.util.ArrayList;
  13.  
  14. public class UI extends JFrame {
  15.  
  16.     private static final long serialVersionUID = 1L;
  17.  
  18.     protected JFrame frame;
  19.     private JPanel panel;
  20.  
  21.     private JLabel numLBL;
  22.     private JButton setNum;
  23.     private JTextField num;
  24.  
  25.     private JLabel nodeLBL;
  26.     private JButton addNode;
  27.     private JTextField node;
  28.  
  29.     private JLabel sourceLBL;
  30.     private JTextField source;
  31.     private JLabel sinkLBL;
  32.     private JTextField sink;
  33.  
  34.     private JLabel fromLBL;
  35.     private JButton addEdge;
  36.     private JTextField from;
  37.     private JLabel toLBL;
  38.     private JTextField to;
  39.     private JLabel gainLBL;
  40.     private JTextField gain;
  41.  
  42.     private JButton solve;
  43.     private JLabel deltaTotalLBL;
  44.     private JLabel deltaTotal;
  45.     private JLabel TF_LBL;
  46.     private JLabel TF;
  47.  
  48.     private JScrollPane pathScroll;
  49.     private JScrollPane nonTouchingScroll;
  50.     private JScrollPane deltaScroll;
  51.     private JTable pathTable;
  52.     private JTable nonTouchingTable;
  53.     private JTable deltaTable;
  54.  
  55.     private FrameDesigner frameDesigner;
  56.     private SizeSectionDesigner sizeSecDesigner;
  57.     private NodeSectionDesigner nodeSecDesigner;
  58.     private EdgeSectionDesigner edgeSecDesigner;
  59.     private ResultSectionDesigner resultSecDesigner;
  60.     private TableSectionDesigner tableSectionDesigner;
  61.     private GraphManager graphManager;
  62.  
  63.     private SFG sfg;
  64.     private MasonSolver solver;
  65.     private ArrayList<ArrayList<String>> forwardPaths;
  66.     private ArrayList<ArrayList<Double>> forwardGains;
  67.     private ArrayList<ArrayList<String>> loops;
  68.     private ArrayList<ArrayList<Integer>> nonTouchingLoops;
  69.     private ArrayList<ArrayList<Double>> loopGains;
  70.     private ArrayList<Double> deltas;
  71.  
  72.     public UI() {
  73.         initialize();
  74.     }
  75.  
  76.     private void initialize() {
  77.  
  78.         frame = new JFrame();
  79.         panel = new JPanel();
  80.         frameDesigner = new FrameDesigner(panel, frame);
  81.  
  82.         numLBL = new JLabel("Set SFG Size");
  83.         num = new JTextField();
  84.         setNum = new JButton("Set");
  85.         sizeSecDesigner = new SizeSectionDesigner(numLBL, num, setNum, frame);
  86.         readSize();
  87.  
  88.         nodeLBL = new JLabel("Add New Node");
  89.         node = new JTextField();
  90.         addNode = new JButton("Add");
  91.         sourceLBL = new JLabel("Source");
  92.         source = new JTextField();
  93.         sinkLBL = new JLabel("Sink");
  94.         sink = new JTextField();
  95.         nodeSecDesigner = new NodeSectionDesigner(nodeLBL, node, addNode, sourceLBL, source, sinkLBL, sink, frame);
  96.         readNode();
  97.  
  98.  
  99.         fromLBL = new JLabel("Add Edge From");
  100.         from = new JTextField();
  101.         toLBL = new JLabel("To");
  102.         to = new JTextField();
  103.         gainLBL = new JLabel("Gain");
  104.         gain = new JTextField();
  105.         addEdge = new JButton("Add");
  106.         edgeSecDesigner = new EdgeSectionDesigner(fromLBL, from, toLBL, to, gainLBL, gain, addEdge, frame);
  107.         readEdge();
  108.  
  109.         solve = new JButton("Solve");
  110.         solve.setFont(new Font("Tahoma", Font.BOLD, 13));
  111.         solve.setBounds(10, 505, 93, 25);
  112.         frame.getContentPane().add(solve);
  113.         solve();
  114.  
  115.         deltaTotalLBL = new JLabel("Delta Total = ");
  116.         deltaTotal = new JLabel();
  117.         TF_LBL = new JLabel("Overall TF = ");
  118.         TF = new JLabel();
  119.         resultSecDesigner = new ResultSectionDesigner(deltaTotalLBL, deltaTotal, TF_LBL, TF, frame);
  120.  
  121.         pathScroll = new JScrollPane();
  122.         pathTable = new JTable();
  123.         nonTouchingScroll = new JScrollPane();
  124.         nonTouchingTable = new JTable();      
  125.         deltaScroll = new JScrollPane();
  126.         deltaTable = new JTable();
  127.         tableSectionDesigner = new TableSectionDesigner(pathScroll, pathTable, nonTouchingScroll, nonTouchingTable, deltaScroll, deltaTable, frame);
  128.     }
  129.  
  130.     private void readSize() {
  131.         setNum.addActionListener(new ActionListener() {
  132.             public void actionPerformed(ActionEvent e) {
  133.                 sfg = new SFG(Integer.parseInt(num.getText()));
  134.                 num.setText("");
  135.             }
  136.         });
  137.     }
  138.  
  139.     private void readNode() {
  140.         addNode.addActionListener(new ActionListener() {
  141.             public void actionPerformed(ActionEvent e) {
  142.                 sfg.addNode(new sfg.Node(node.getText()));
  143.                 node.setText("");
  144.             }
  145.         });
  146.     }
  147.  
  148.     private void readEdge() {
  149.         addEdge.addActionListener(new ActionListener() {
  150.             public void actionPerformed(ActionEvent e) {
  151.                 String fromNode = from.getText();
  152.                 String toNode = to.getText();
  153.                 Double edgeGain = Double.parseDouble(gain.getText());
  154.                 sfg.getNode(fromNode).addEdge(new sfg.Edge(edgeGain, sfg.getNode(toNode)));
  155.                 from.setText("");
  156.                 to.setText("");
  157.                 gain.setText("");
  158.             }
  159.         });
  160.     }
  161.  
  162.     private void solve() {
  163.         solve.addActionListener(new ActionListener() {
  164.             public void actionPerformed(ActionEvent e) {
  165.                 sfg.Node sourceNode = sfg.getNode(source.getText());
  166.                 sfg.Node sinkNode = sfg.getNode(sink.getText());
  167.                 source.setText("");
  168.                 sink.setText("");
  169.                 graphManager = new GraphManager(sfg, frame); // draw graph
  170.                 forwardPaths = sfg.forwardPaths(sourceNode, sinkNode);
  171.                 forwardGains = sfg.forwardGains();
  172.                 loops = sfg.loops();
  173.                 loopGains = sfg.loopGains();
  174.                 solver = new MasonSolver(forwardPaths, forwardGains, loops, loopGains);
  175.                 // fill labels: deltaTotal, TF
  176.                 deltaTotal.setText(solver.calculateDeltaTotal().toString());
  177.                 TF.setText(solver.calculateTF().toString());
  178.                 nonTouchingLoops=solver.getNonTouchingLoops();
  179.                 deltas=solver.getDeltas();
  180.                 // fill tables
  181.                 fillTable1();
  182.                 fillTable2();
  183.                 fillTable3();
  184.  
  185.             }
  186.         });
  187.     }
  188.  
  189.     /*fills the table of forward paths and loops*/
  190.     private void fillTable1() {
  191.         int tableSize = Math.max(forwardPaths.size(), loops.size());
  192.         tableSize = Math.max(tableSize, 7);
  193.         Object list[][] = new Object[tableSize][tableSize];
  194.         for (int i = 0; i < tableSize; i++) {
  195.             String path = "";
  196.             if (i < forwardPaths.size()) {
  197.                 for (int j = 0; j < forwardPaths.get(i).size(); j++) {
  198.                     path += forwardPaths.get(i).get(j);
  199.                 }
  200.                 list[i][0] = path;
  201.             } else
  202.                 list[i][0] = null;
  203.         }
  204.         for (int i = 0; i < tableSize; i++) {
  205.             String path = "";
  206.             if (i < loops.size()) {
  207.                 for (int j = 0; j < loops.get(i).size(); j++) {
  208.                     path += loops.get(i).get(j);
  209.                 }
  210.                 list[i][1] = path;
  211.             } else
  212.                 list[i][1] = null;
  213.         }
  214.         pathTable.setModel(new DefaultTableModel(list, new String[]{
  215.                 "ForwardPaths", "Loops"}));
  216.     }
  217.  
  218.     /*fills the table of nontouching loops*/
  219.     private void fillTable2() {
  220.         int tableSize = Math.max(nonTouchingLoops.size(), 7);
  221.         Object list[][] = new Object[tableSize][tableSize];
  222.         for(int i=0;i<nonTouchingLoops.size();i++){
  223.             String loop="";
  224.             for(int j=0;j<nonTouchingLoops.get(i).size();j++){
  225.                loop+=loops.get(nonTouchingLoops.get(i).get(j));
  226.             }
  227.             list[i][0]=loop;
  228.         }
  229.         nonTouchingTable.setModel(new DefaultTableModel(list,new String[]{"Non-Touching Loops"}));
  230.     }
  231.  
  232.     /*fills the table of deltas*/
  233.     private void fillTable3() {
  234.         int tableSize = Math.max(deltas.size(), 7);
  235.         Object list[][] = new Object[tableSize][tableSize];
  236.         for(int i=0;i<deltas.size();i++)
  237.             list[i][0]=deltas.get(i);
  238.         deltaTable.setModel(new DefaultTableModel(list, new String[]{"Deltas"}));
  239.     }
  240. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement