View Javadoc

1   package net.sourceforge.xmlfit.fit.stepper.gui;
2   
3   import java.awt.BorderLayout;
4   import java.awt.Color;
5   import java.awt.Rectangle;
6   import java.awt.event.ActionEvent;
7   import java.awt.event.ActionListener;
8   import java.util.ArrayList;
9   import java.util.HashMap;
10  import java.util.List;
11  
12  import javax.swing.JButton;
13  import javax.swing.JDialog;
14  import javax.swing.JFrame;
15  import javax.swing.JPanel;
16  import javax.swing.JScrollPane;
17  import javax.swing.JTable;
18  import javax.swing.ListSelectionModel;
19  import javax.swing.text.StyleConstants.ColorConstants;
20  
21  import fit.Parse;
22  
23  public class DebuggerFrame extends JFrame
24  {
25  
26    private static final String TITEL_DEBUGGER_FRAME = "FIT Action Fixture Debugger";
27  
28    private int maxCellCount = 0;
29  
30    private JButton nextStepButton = new JButton("step");
31  
32    private JButton runButtonp = new JButton("run");
33  
34    private JPanel buttonPanel = new JPanel();
35  
36    private HashMap<Parse, Integer> parseToCount = new HashMap<Parse, Integer>();
37  
38    private JTable tableView;
39  
40    private boolean isStepMode = false;
41  
42    private static Boolean lock = false;
43  
44    public DebuggerFrame()
45    {
46      super(TITEL_DEBUGGER_FRAME);
47      initButtons();
48    }
49  
50    public void initTabeles(Parse tables)
51    {
52      int count = 1;
53      Parse table = tables;
54      ArrayList<List<String>> rowList = new ArrayList<List<String>>();
55      while (table != null)
56      {
57        Parse rows = table.parts;
58        count = doRows(count, rowList, rows);
59        count++;
60        rowList.add(new ArrayList<String>());
61        table = table.more;
62      }
63      initTableView(rowList);
64      pack();
65      setVisible(true);
66      warten();
67    }
68  
69    private int doRows(int count, ArrayList<List<String>> rowList, Parse rows)
70    {
71      while (rows != null)
72      {
73        parseToCount.put(rows.parts, count++);
74        ArrayList<String> cellList = doCells(rows.parts);
75        rowList.add(cellList);
76        rows = rows.more;
77      }
78      return count;
79    }
80  
81    private ArrayList<String> doCells(Parse cells)
82    {
83      ArrayList<String> cellList = new ArrayList<String>();
84      int tmpCellCount = 0;
85      while (cells != null)
86      {
87        cellList.add(cells.text());
88        cells = cells.more;
89        tmpCellCount++;
90      }
91      setMaxCellCount(tmpCellCount);
92      return cellList;
93    }
94  
95    private void setMaxCellCount(int count)
96    {
97      if (count > maxCellCount)
98      {
99        maxCellCount = count;
100     }
101   }
102 
103   private void initTableView(ArrayList<List<String>> rowList)
104   {
105     tableView = new JTable(new DebuggerViewModel(rowList, maxCellCount));
106     tableView.getColumnModel().getColumn(0).setWidth(20);
107     tableView.getColumnModel().getColumn(0).setPreferredWidth(20);
108     tableView.getColumnModel().getColumn(0).setMaxWidth(20);
109     tableView.getColumnModel().getColumn(0).setMinWidth(20);
110     getContentPane().add(new JScrollPane(tableView), BorderLayout.CENTER);
111   }
112 
113   private void initButtons()
114   {
115     getContentPane().add(buttonPanel, BorderLayout.SOUTH);
116 
117     buttonPanel.add(nextStepButton);
118     buttonPanel.add(runButtonp);
119 
120     nextStepButton.addActionListener(new ActionListener()
121     {
122       public void actionPerformed(ActionEvent e)
123       {
124         isStepMode = true;
125         fortsetzen();
126       }
127     });
128 
129     runButtonp.addActionListener(new ActionListener()
130     {
131       public void actionPerformed(ActionEvent e)
132       {
133         fortsetzen();
134       }
135     });
136   }
137 
138   public void doRow(Parse parse)
139   {
140     if (isDebuggerEnable(parse))
141     {
142       isStepMode = false;
143       Integer row = parseToCount.get(parse);
144       setActualRowSelected(row);
145       scrollToRow(row);
146       setVisible(true);
147       warten();
148     }
149   }
150 
151   private void warten()
152   {
153     try
154     {
155       synchronized (lock)
156       {
157         tableView.setForeground(Color.BLACK);
158         lock.wait();
159         tableView.setForeground(Color.LIGHT_GRAY);
160       }
161     }
162     catch (InterruptedException e1)
163     {
164     }
165   }
166 
167   private void fortsetzen()
168   {
169     synchronized (lock)
170     {
171       lock.notifyAll();
172     }
173   }
174 
175   private void scrollToRow(Integer row)
176   {
177     Rectangle rect = tableView.getCellRect(row - 1, 0, true);
178     tableView.scrollRectToVisible(rect);
179   }
180 
181   private void setActualRowSelected(Integer row)
182   {
183     ListSelectionModel selectionModel = tableView.getSelectionModel();
184     selectionModel.setSelectionInterval(row - 1, row - 1);
185   }
186 
187   private boolean isDebuggerEnable(Parse parse)
188   {
189     return isStepMode || isBreakPoint(parse);
190   }
191 
192   private boolean isBreakPoint(Parse parse)
193   {
194     Integer count = parseToCount.get(parse);
195     Object object = tableView.getModel().getValueAt(count - 1, 0);
196     if (object instanceof Boolean)
197     {
198       Boolean result = (Boolean) object;
199       return result;
200     }
201     return false;
202   }
203 }