YauhenMardan

Untitled

May 27th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.83 KB | None | 0 0
  1. package com.company;
  2.  
  3. import javax.swing.*;
  4. import javax.swing.filechooser.FileNameExtensionFilter;
  5. import javax.swing.table.DefaultTableModel;
  6. import javax.xml.parsers.DocumentBuilder;
  7. import javax.xml.parsers.DocumentBuilderFactory;
  8. import java.awt.*;
  9. import java.awt.event.ActionEvent;
  10. import java.awt.event.ActionListener;
  11. import java.io.File;
  12. import java.io.FileNotFoundException;
  13. import java.io.FileWriter;
  14. import java.io.IOException;
  15. import java.util.*;
  16. import org.w3c.dom.*;
  17. import org.xml.sax.SAXException;
  18.  
  19. import javax.xml.parsers.*;
  20.  
  21. public class WindowApp extends JFrame {
  22. private final static int LIST_X = 300;
  23. private final static int LIST_Y = 500;
  24. public static final Object[] tableHeader = new String[]{"ID","Surname","Course","Group"};
  25.  
  26. JMenu menu;
  27. JMenu menuStudents;
  28. JMenuBar menuBar;
  29. JMenuItem menuItem1;
  30. JMenuItem menuItem2;
  31. JMenuItem menuItem3;
  32. JMenuItem menuItem4;
  33. JMenuItem menuItem5;
  34.  
  35. JScrollPane scrollPane;
  36.  
  37. JPanel mainPanel;
  38. JPanel northPanel;
  39. JButton addDataButton;
  40. JButton chooseFileButton;
  41. JButton sortButton;
  42.  
  43. JTable table;
  44.  
  45. public ArrayList<Student> getModelStudents() {
  46. return studentArrayList;
  47. }
  48.  
  49. ArrayList<Student> studentArrayList;
  50. ArrayList<Student> studentTempArrayList;
  51. ArrayList<Student> studentSortedArrayList;
  52.  
  53. WindowApp() {
  54. //panel
  55. mainPanel = new JPanel();
  56. mainPanel.setLayout(new BorderLayout());
  57. //buttons
  58. addDataButton = new JButton("Add new students...");
  59. chooseFileButton = new JButton("Choose text file...");
  60. sortButton = new JButton("Sort");
  61. addDataButton.setPreferredSize(new Dimension(LIST_X, 20));
  62. sortButton.setPreferredSize(new Dimension(LIST_X, 20));
  63. //
  64. northPanel = new JPanel();
  65. northPanel.add(addDataButton, BorderLayout.WEST);
  66. //mainPanel.add(chooseFileButton, BorderLayout.SOUTH);
  67. northPanel.add(sortButton, BorderLayout.EAST);
  68. mainPanel.add(northPanel, BorderLayout.NORTH);
  69. //model
  70. studentArrayList = new ArrayList<>();
  71. //table
  72. table = new JTable();
  73. table.setPreferredSize(new Dimension(2*LIST_X, LIST_Y));
  74. scrollPane = new JScrollPane(table);
  75. scrollPane.setPreferredSize(new Dimension(2*LIST_X, LIST_Y));
  76. table.setFillsViewportHeight(true);
  77. mainPanel.add(scrollPane, BorderLayout.CENTER);
  78. //add header
  79. DefaultTableModel model = new DefaultTableModel(tableHeader,0);
  80. table.setModel(model);
  81. //menu bar
  82. menuBar = new JMenuBar();
  83. menu = new JMenu("File");
  84. menuStudents = new JMenu(("Students"));
  85. menuItem1 = new JMenuItem("Add student...");
  86. menuItem2 = new JMenuItem("Open text file...");
  87. menuItem3 = new JMenuItem("Sort");
  88. menuItem4 = new JMenuItem("Save as XML file...");
  89. menuItem5 = new JMenuItem("Open XML file...");
  90. menuStudents.add(menuItem3);
  91. menuStudents.add(menuItem1);
  92. menu.add(menuItem2);
  93. menu.add(menuItem5);
  94. menu.addSeparator();
  95. menu.add(menuItem4);
  96. menuBar.add(menu);
  97. menuBar.add(menuStudents);
  98. setJMenuBar(menuBar);
  99.  
  100. //window
  101. setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  102. setResizable(false);
  103. setContentPane(mainPanel);
  104. pack();
  105. //listeners
  106. addDataButton.addActionListener(new AddDataListener());
  107. chooseFileButton.addActionListener(new ChooseFileListener());
  108. menuItem1.addActionListener(new AddDataListener());
  109. menuItem2.addActionListener(new ChooseFileListener());
  110. menuItem4.addActionListener(new ActionListener() {
  111. @Override
  112. public void actionPerformed(ActionEvent e) {
  113. JFileChooser fileChooser = new JFileChooser();
  114. if (fileChooser.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {
  115. File file = fileChooser.getSelectedFile();
  116. try {
  117. writeToXMLFile(file);
  118. }
  119. catch (IOException exception){
  120.  
  121. }
  122. }
  123. }
  124. });
  125. menuItem5.addActionListener(new ActionListener() {
  126. @Override
  127. public void actionPerformed(ActionEvent e) {
  128. //parse xml file (DOM)
  129. JFileChooser fileOpen = new JFileChooser();
  130. int ret = fileOpen.showOpenDialog(null);
  131. if (ret == JFileChooser.APPROVE_OPTION) {
  132. try {
  133. studentArrayList=new ArrayList<>();
  134. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  135. DocumentBuilder builder = factory.newDocumentBuilder();
  136. File file=fileOpen.getSelectedFile();
  137. Document document = builder.parse(file);
  138.  
  139. NodeList studentElements = document
  140. .getDocumentElement()
  141. .getElementsByTagName("student");
  142.  
  143. for (int i = 0; i < studentElements.getLength(); i++) {
  144. Node student = studentElements.item(i);
  145. if (student.getNodeType() == Node.ELEMENT_NODE) {
  146. Element element = (Element) student;
  147. int recBookNum = Integer.parseInt(
  148. element
  149. .getAttribute("id")
  150. .toString());
  151. String surname =
  152. element
  153. .getElementsByTagName("surname")
  154. .item(0)
  155. .getTextContent();
  156. int course = Integer.parseInt(
  157. element
  158. .getElementsByTagName("course")
  159. .item(0)
  160. .getTextContent());
  161. int group = Integer.parseInt(
  162. element
  163. .getElementsByTagName("group")
  164. .item(0)
  165. .getTextContent());
  166. studentArrayList.add(new Student(recBookNum,surname,course,group));
  167.  
  168. }
  169. }
  170. updateList();
  171. }
  172. catch (ParserConfigurationException exception){
  173. JOptionPane.showMessageDialog(WindowApp.this,
  174. "Parser error.",
  175. "Inane warning",
  176. JOptionPane.WARNING_MESSAGE);
  177. }
  178. catch (SAXException exception){
  179. JOptionPane.showMessageDialog(WindowApp.this,
  180. "SAX error.",
  181. "Inane warning",
  182. JOptionPane.WARNING_MESSAGE);
  183. }
  184. catch (IOException exception){
  185. JOptionPane.showMessageDialog(WindowApp.this,
  186. "Cannot open the file.",
  187. "Inane warning",
  188. JOptionPane.WARNING_MESSAGE);
  189. }
  190. }
  191. }
  192. });
  193. }
  194.  
  195. public void updateList() {
  196. DefaultTableModel model = new DefaultTableModel(tableHeader,0);
  197. Iterator<Student> iterator = studentArrayList.iterator();
  198. Student student;
  199. while (iterator.hasNext()) {
  200. student=iterator.next();
  201. model.addRow(new Object[]{student.getRecBookNum(),student.getSurname(),student.getCourse(),student.getGroup()});
  202. }
  203. table.setModel(model);
  204. }
  205.  
  206.  
  207. public void writeToXMLFile(File file) throws IOException {
  208. file.createNewFile();
  209. FileWriter writer=new FileWriter(file);
  210. writer.write(this.toXMLString());
  211. writer.flush();
  212. writer.close();
  213. }
  214.  
  215. public String toXMLString(){
  216. StringBuffer resultXML=new StringBuffer();
  217. resultXML.append("<students>\n");
  218. Iterator<Student> iterator = studentArrayList.iterator();
  219. while (iterator.hasNext()){
  220. resultXML.append(iterator.next().toXMLString());
  221. }
  222. resultXML.append("</students>\n");
  223. return resultXML.toString();
  224. }
  225.  
  226. class ChooseFileListener implements ActionListener {
  227. public void actionPerformed(ActionEvent e) {
  228. JFileChooser fileOpen = new JFileChooser();
  229. FileNameExtensionFilter filter = new FileNameExtensionFilter(
  230. "TXT files", "txt");
  231. fileOpen.setFileFilter(filter);
  232. int ret = fileOpen.showOpenDialog(null);
  233. if (ret == JFileChooser.APPROVE_OPTION) {
  234. try {
  235. studentArrayList=new ArrayList<>();
  236. int recBookNum;
  237. String surname;
  238. int course;
  239. int group;
  240. Scanner scanner = new Scanner(fileOpen.getSelectedFile());
  241. while (scanner.hasNext()) {
  242. recBookNum = scanner.nextInt();
  243. surname = scanner.next();
  244. course = scanner.nextInt();
  245. group = scanner.nextInt();
  246. studentArrayList.add(new Student(recBookNum, surname, course, group));
  247. updateList();
  248. }
  249. } catch (FileNotFoundException exception) {
  250. JOptionPane.showMessageDialog(WindowApp.this,
  251. "Cannot open the file.",
  252. "Inane warning",
  253. JOptionPane.WARNING_MESSAGE);
  254. }
  255. }
  256. }
  257. }
  258. class AddDataListener implements ActionListener{
  259. @Override
  260. public void actionPerformed(ActionEvent e) {
  261. AddDataDialog dialog = new AddDataDialog(WindowApp.this);
  262. dialog.setVisible(true);
  263. }
  264. }
  265. }
  266.  
  267.  
  268.  
  269. // FileNameExtensionFilter filter = new FileNameExtensionFilter(
  270. // "TXT files", "txt");
  271. // fileOpen.setFileFilter(filter);
Advertisement
Add Comment
Please, Sign In to add comment