PageRenderTime 78ms CodeModel.GetById 40ms RepoModel.GetById 0ms app.codeStats 0ms

/src/com/team1160/scouting/frontend/panels/MatchScoutingPanel.java

https://github.com/FRC-Team-1160/1160-Scouting-App
Java | 370 lines | 280 code | 74 blank | 16 comment | 26 complexity | 03f239eff1ccc3939b52dde25e2ed178 MD5 | raw file
  1. /*
  2. * To change this template, choose Tools | Templates
  3. * and open the template in the editor.
  4. */
  5. package com.team1160.scouting.frontend.panels;
  6. import com.team1160.scouting.frontend.elements.JumpMenuItem;
  7. import com.team1160.scouting.frontend.elements.MultiLineInputElement;
  8. import com.team1160.scouting.frontend.elements.NumberDropDownElement;
  9. import com.team1160.scouting.frontend.elements.ScoutingElement;
  10. import com.team1160.scouting.frontend.elements.SingleLineInputElement;
  11. import com.team1160.scouting.frontend.elements.WeightingSliderElement;
  12. import com.team1160.scouting.frontend.resourcePackets.CardLayoutPacket;
  13. import com.team1160.scouting.h2.CommentTable;
  14. import com.team1160.scouting.h2.DictTable;
  15. import com.team1160.scouting.h2.MatchScoutingTable;
  16. import com.team1160.scouting.h2.WeightingTable;
  17. import com.team1160.scouting.xml.XMLParser;
  18. import java.awt.BorderLayout;
  19. import java.awt.Component;
  20. import java.awt.GridLayout;
  21. import java.awt.event.ActionEvent;
  22. import java.awt.event.ActionListener;
  23. import java.awt.event.KeyEvent;
  24. import java.io.IOException;
  25. import java.sql.SQLException;
  26. import java.util.ArrayList;
  27. import java.util.logging.Level;
  28. import java.util.logging.Logger;
  29. import javax.swing.BorderFactory;
  30. import javax.swing.JButton;
  31. import javax.swing.JMenu;
  32. import javax.swing.JMenuBar;
  33. import javax.swing.JOptionPane;
  34. import javax.swing.JPanel;
  35. import javax.swing.JScrollPane;
  36. import javax.swing.border.Border;
  37. import javax.xml.parsers.ParserConfigurationException;
  38. import org.w3c.dom.Element;
  39. import org.w3c.dom.NodeList;
  40. import org.xml.sax.SAXException;
  41. /**
  42. *
  43. * @author sakekasi
  44. */
  45. public class MatchScoutingPanel extends JPanel{
  46. /**
  47. *
  48. */
  49. private static final long serialVersionUID = -5439413188323739057L;
  50. protected JPanel bottomPanel;
  51. protected ArrayList<ScoutingElement> elements;
  52. protected JPanel elementPanel;
  53. protected JPanel elementInputPanel;
  54. protected JPanel elementButtonPanel;
  55. protected Border elementBorder;
  56. protected ArrayList<WeightingSliderElement> weightingElements;
  57. protected JPanel weightingPanel;
  58. protected JPanel weightingInputPanel;
  59. protected JPanel weightingButtonPanel;
  60. protected Border weightingBorder;
  61. protected JMenuBar menubar;
  62. protected JMenu go;
  63. protected CardLayoutPacket layout;
  64. protected MatchScoutingTable scoutingTable;
  65. protected WeightingTable weightingTable;
  66. protected DictTable dictTable;
  67. protected CommentTable commentTable;
  68. public MatchScoutingPanel(CardLayoutPacket layout, MatchScoutingTable scouting,
  69. WeightingTable weighting, DictTable dict,
  70. CommentTable comment) throws ParserConfigurationException, SAXException, IOException, Exception {
  71. this.layout = layout;
  72. this.elements = new ArrayList<ScoutingElement>();
  73. this.weightingElements = new ArrayList<WeightingSliderElement>();
  74. this.setLayout(new BorderLayout());
  75. XML xml = this.new XML();
  76. this.scoutingTable = scouting;
  77. this.weightingTable = weighting;
  78. this.dictTable = dict;
  79. this.commentTable = comment;
  80. this.menubar = new JMenuBar();
  81. this.menubar.setLayout(new BorderLayout());
  82. this.go = new JMenu("Go");
  83. this.go.setMnemonic(KeyEvent.VK_G);
  84. this.go.add(new JumpMenuItem(layout,"Graph","graph"));
  85. this.go.add(new JumpMenuItem(layout,"Comments","comment"));
  86. // this.go.add(new JumpMenuItem(layout,"Pit Scouting","pit"));
  87. this.menubar.add(this.go, BorderLayout.WEST);
  88. // this.toolbar.add(this.graph, BorderLayout.EAST);
  89. this.bottomPanel = new JPanel();
  90. this.bottomPanel.setLayout(new GridLayout(1,2,0,0));
  91. xml.parse();
  92. this.elementPanel = new JPanel();
  93. this.elementInputPanel = new JPanel();
  94. this.elementBorder = BorderFactory.createTitledBorder("Values");
  95. this.elementInputPanel.setLayout(new GridLayout(this.elements.size(),1,0,0));
  96. this.elementPanel.setBorder(this.elementBorder);
  97. this.elementPanel.setLayout(new BorderLayout());
  98. this.weightingPanel = new JPanel();
  99. this.weightingInputPanel = new JPanel();
  100. this.weightingBorder = BorderFactory.createTitledBorder("Weighting");
  101. this.weightingPanel.setBorder(this.weightingBorder);
  102. this.weightingInputPanel.setLayout(new GridLayout(this.weightingElements.size(),1,0,0));
  103. this.weightingPanel.setLayout(new BorderLayout());
  104. for(ScoutingElement se: this.elements){
  105. this.elementInputPanel.add(se);
  106. }
  107. for(WeightingSliderElement w: this.weightingElements){
  108. this.weightingInputPanel.add(w);
  109. }
  110. JButton elementSubmit=new JButton("submit"),elementClear = new JButton("reset");
  111. elementSubmit.addActionListener(this.new ElementSubmit());
  112. elementClear.addActionListener(this.new ElementClear());
  113. JButton weightingSubmit = new JButton("submit"), weightingClear = new JButton("reset");
  114. weightingSubmit.addActionListener(this.new WeightingSubmit());
  115. weightingClear.addActionListener(this.new WeightingClear());
  116. this.elementButtonPanel = new JPanel();
  117. // this.elementButtonPanel.setBackground(Color.red);
  118. this.weightingButtonPanel = new JPanel();
  119. // this.weightingButtonPanel.setLayout(new GridLayout(1,2));
  120. // this.weightingButtonPanel.setBackground(Color.red);
  121. this.elementButtonPanel.add(elementSubmit);
  122. this.elementButtonPanel.add(elementClear);
  123. this.weightingButtonPanel.add(weightingSubmit);
  124. this.weightingButtonPanel.add(weightingClear);
  125. this.elementPanel.add(new JScrollPane(this.elementInputPanel), BorderLayout.CENTER);
  126. this.elementPanel.add(this.elementButtonPanel, BorderLayout.SOUTH);
  127. this.weightingPanel.add(new JScrollPane(this.weightingInputPanel), BorderLayout.CENTER);
  128. this.weightingPanel.add(this.weightingButtonPanel, BorderLayout.SOUTH);
  129. this.bottomPanel.add(this.elementPanel);
  130. this.bottomPanel.add(this.weightingPanel);
  131. this.add(this.menubar, BorderLayout.NORTH);
  132. this.add(this.bottomPanel, BorderLayout.CENTER);
  133. }
  134. class XML{
  135. XMLParser xml;
  136. protected XML() throws ParserConfigurationException, SAXException, IOException{
  137. xml = new XMLParser("config.xml");
  138. }
  139. protected void parse() throws Exception{
  140. Element e = xml.getMatch();
  141. elements.add(new SingleLineInputElement("Team No:", SingleLineInputElement.INTEGER));
  142. SingleLineInputElement match = new SingleLineInputElement("Match No:", SingleLineInputElement.INTEGER);
  143. match.setInComments(true);
  144. elements.add(match);
  145. NodeList element = e.getElementsByTagName("field");
  146. MatchScoutingPanel.this.dictTable.reset();
  147. for(int i=0;i<element.getLength();i++){
  148. Element el = (Element) element.item(i);
  149. String type=el.getAttribute("type");
  150. String name=el.getAttribute("name");
  151. @SuppressWarnings("unused")
  152. String inWeight = el.getAttribute("inWeight");
  153. if(type.equalsIgnoreCase("sIntInput")){
  154. SingleLineInputElement se;
  155. if( (el.hasAttribute("isNegative")) &&
  156. el.getAttribute("isNegative").equals("true")){
  157. se = new SingleLineInputElement(name,
  158. SingleLineInputElement.INTEGER,
  159. true);
  160. } else if( ((el.hasAttribute("isTime"))) &&
  161. el.getAttribute("isTime").equals("true")){
  162. int maxTime = Integer.parseInt(el.getAttribute("maxTime"));
  163. se = new SingleLineInputElement(name,
  164. SingleLineInputElement.INTEGER,
  165. false,
  166. true,
  167. maxTime);
  168. }else {
  169. se = new SingleLineInputElement(name,
  170. SingleLineInputElement.INTEGER);
  171. }
  172. if((!(el.hasAttribute("inWeight"))) ||
  173. (el.getAttribute("inWeight")).equalsIgnoreCase("true")){
  174. weightingElements.add(new WeightingSliderElement(name, se.hashCode()));
  175. weightingElements.get(weightingElements.size()-1).setValue(
  176. MatchScoutingPanel.this.weightingTable.getValue(
  177. weightingElements.get(weightingElements.size()-1).hashCode()));
  178. MatchScoutingPanel.this.dictTable.insert(se.hashCode(), se.getText());
  179. }
  180. se.setAlignmentX(Component.LEFT_ALIGNMENT);
  181. elements.add(se);
  182. } else if(type.equalsIgnoreCase("numDropDown")){
  183. NumberDropDownElement ne=new NumberDropDownElement(name,
  184. Integer.parseInt(el.getAttribute("bottom")),
  185. Integer.parseInt(el.getAttribute("top")));
  186. if((!(el.hasAttribute("inWeight"))) ||
  187. (el.getAttribute("inWeight")).equalsIgnoreCase("true")){
  188. weightingElements.add(new WeightingSliderElement(name, ne.hashCode()));
  189. weightingElements.get(weightingElements.size()-1).setValue(
  190. MatchScoutingPanel.this.weightingTable.getValue(
  191. weightingElements.get(weightingElements.size()-1).hashCode()));
  192. MatchScoutingPanel.this.dictTable.insert(ne.hashCode(), ne.getText());
  193. }
  194. ne.setAlignmentX(Component.LEFT_ALIGNMENT);
  195. elements.add(ne);
  196. } else {
  197. throw new Exception("wrong type");
  198. }
  199. }
  200. MultiLineInputElement comments = new MultiLineInputElement("Comments:");
  201. comments.setInComments(true);
  202. elements.add(comments);
  203. }
  204. }
  205. class ElementSubmit implements ActionListener{
  206. public void actionPerformed(ActionEvent ae) {
  207. ArrayList<String> values = new ArrayList<String>();
  208. boolean error=false;
  209. for(ScoutingElement e: MatchScoutingPanel.this.elements){
  210. if(e.getClass()== SingleLineInputElement.class){
  211. SingleLineInputElement slie = (SingleLineInputElement) e;
  212. if(!slie.isNegative()){
  213. error = slie.getInput().startsWith("-");
  214. }
  215. if(slie.isTime()){
  216. try{
  217. int i = Integer.parseInt(slie.getInput());
  218. error = (i>slie.getMaxTime())||(i<=0);
  219. } catch(NumberFormatException nfe){
  220. error = true;
  221. }
  222. }
  223. }
  224. values.add(e.getInput());
  225. }
  226. for(String s:values){
  227. if(!s.contains("\n")){
  228. try{
  229. Integer.parseInt(s);
  230. }catch(NumberFormatException nfe){
  231. error = true;
  232. }
  233. }
  234. }
  235. if(error){
  236. JOptionPane.showMessageDialog(MatchScoutingPanel.this,
  237. "Wrong Input",
  238. "Input Error",
  239. JOptionPane.ERROR_MESSAGE);
  240. } else {
  241. for(int i=2; i<(MatchScoutingPanel.this.elements.size()-1);i++){
  242. try {
  243. MatchScoutingPanel.this.scoutingTable.insert(Integer.parseInt(values.get(0)), MatchScoutingPanel.this.elements.get(i).hashCode(), Integer.parseInt(values.get(i)));
  244. } catch (SQLException ex) {
  245. Logger.getLogger(MatchScoutingPanel.class.getName()).log(Level.SEVERE, null, ex);
  246. }
  247. }
  248. try {
  249. if(values.get(values.size()-1).matches(".*\\w.*"))
  250. MatchScoutingPanel.this.commentTable.insert(Integer.parseInt(values.get(0)),
  251. Integer.parseInt(values.get(1)),
  252. values.get(values.size()-1));
  253. } catch (SQLException ex) {
  254. Logger.getLogger(MatchScoutingPanel.class.getName()).log(Level.SEVERE, null, ex);
  255. }
  256. for(ScoutingElement s: MatchScoutingPanel.this.elements){
  257. s.clear();
  258. }
  259. }
  260. }
  261. }
  262. class ElementClear implements ActionListener{
  263. public void actionPerformed(ActionEvent ae) {
  264. for(ScoutingElement s: MatchScoutingPanel.this.elements){
  265. s.clear();
  266. }
  267. }
  268. }
  269. class WeightingSubmit implements ActionListener{
  270. public void actionPerformed(ActionEvent ae) {
  271. try {
  272. MatchScoutingPanel.this.weightingTable.reset();
  273. } catch (SQLException ex) {
  274. Logger.getLogger(MatchScoutingPanel.class.getName()).log(Level.SEVERE, null, ex);
  275. }
  276. for(WeightingSliderElement e:MatchScoutingPanel.this.weightingElements){
  277. try {
  278. MatchScoutingPanel.this.weightingTable.insert(e.hashCode(), Integer.parseInt(e.getInput()));
  279. } catch (SQLException ex) {
  280. Logger.getLogger(MatchScoutingPanel.class.getName()).log(Level.SEVERE, null, ex);
  281. }
  282. }
  283. }
  284. }
  285. class WeightingClear implements ActionListener{
  286. public void actionPerformed(ActionEvent ae) {
  287. for(WeightingSliderElement e:MatchScoutingPanel.this.weightingElements){
  288. try {
  289. e.setValue(MatchScoutingPanel.this.weightingTable.getValue(e.hashCode()));
  290. } catch (SQLException ex) {
  291. Logger.getLogger(MatchScoutingPanel.class.getName()).log(Level.SEVERE, null, ex);
  292. }
  293. }
  294. }
  295. }
  296. }