Guest User

Untitled

a guest
Dec 9th, 2016
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 13.30 KB | None | 0 0
  1. package dml.userInterface;
  2.  
  3. import org.eclipse.swt.widgets.Display;
  4. import org.eclipse.swt.widgets.Shell;
  5.  
  6. import java.io.FileInputStream;
  7. import java.io.FileOutputStream;
  8. import java.io.IOException;
  9. import java.io.ObjectInputStream;
  10. import java.io.ObjectOutputStream;
  11. import java.util.ArrayList;
  12.  
  13. import org.eclipse.swt.SWT;
  14. import org.eclipse.swt.widgets.TabFolder;
  15. import org.eclipse.swt.widgets.TabItem;
  16. import org.eclipse.swt.widgets.Composite;
  17. import org.eclipse.swt.layout.FormLayout;
  18. import org.eclipse.swt.widgets.Table;
  19. import org.eclipse.swt.widgets.TableItem;
  20. import org.eclipse.swt.layout.FormData;
  21. import org.eclipse.swt.layout.FormAttachment;
  22. import org.eclipse.swt.events.MouseAdapter;
  23. import org.eclipse.swt.events.MouseEvent;
  24. import dml.logic.AnimeItem;
  25. import dml.logic.UserProfile;
  26. import org.eclipse.swt.layout.FillLayout;
  27. import org.eclipse.swt.events.ShellAdapter;
  28. import org.eclipse.swt.events.ShellEvent;
  29. import org.eclipse.swt.widgets.Button;
  30. import org.eclipse.swt.events.SelectionAdapter;
  31. import org.eclipse.swt.events.SelectionEvent;
  32. import org.eclipse.swt.widgets.Label;
  33. import org.eclipse.swt.widgets.Spinner;
  34. import org.eclipse.swt.events.ModifyListener;
  35. import org.eclipse.swt.events.ModifyEvent;
  36. import org.eclipse.swt.events.FocusAdapter;
  37. import org.eclipse.swt.events.FocusEvent;
  38. import org.eclipse.wb.swt.SWTResourceManager;
  39.  
  40. public class ListWindow {
  41.  
  42.     protected Shell listWindow;
  43.     private AnimeItemDetails animeItemDetails;
  44.     private UserProfile user;
  45.     private Table animeTable;
  46.     private ItemReview reviewWindow;
  47.     private AnimeSearchWindow animeSearchWindow;
  48.  
  49.     // The constructor takes the string given to it and reads from a file, if
  50.     // the boolean given to it is true, otherwise it creates a new file for the
  51.     // user
  52.  
  53.     public ListWindow(String userName, Boolean existing) {
  54.         if (existing == false) {
  55.             this.user = new UserProfile(userName);
  56.             saveUserProfile();
  57.         } else {
  58.             try {
  59.                 FileInputStream fis = new FileInputStream(userName + ".dat");
  60.                 ObjectInputStream ois = new ObjectInputStream(fis);
  61.                 this.user = (UserProfile) ois.readObject();
  62.                 ois.close();
  63.             } catch (IOException e) {
  64.                 e.printStackTrace();
  65.             } catch (ClassNotFoundException ex) {
  66.                 ex.printStackTrace();
  67.             }
  68.         }
  69.     }
  70.  
  71.     /**
  72.      * @wbp.parser.entryPoint
  73.      */
  74.     public void open() {
  75.         createContents();
  76.         listWindow.open();
  77.         listWindow.layout();
  78.  
  79.     }
  80.  
  81.     /**
  82.      * Create contents of the window.
  83.      */
  84.     protected void createContents() {
  85.         listWindow = new Shell(Display.getCurrent());
  86.         listWindow.addShellListener(new ShellAdapter() {
  87.             @Override
  88.             public void shellClosed(ShellEvent e) {
  89.                 saveUserProfile();
  90.                 Display.getCurrent().close();
  91.             }
  92.         });
  93.         listWindow.setSize(1280, 720);
  94.         listWindow.setText("User List of " + this.user.getuserName());
  95.         listWindow.setLayout(new FillLayout(SWT.HORIZONTAL));
  96.  
  97.         TabFolder tabFolder = new TabFolder(listWindow, SWT.NONE);
  98.  
  99.         TabItem animeTab = new TabItem(tabFolder, SWT.NONE);
  100.         animeTab.setText("Anime List");
  101.  
  102.         Composite animeComposite = new Composite(tabFolder, SWT.NONE);
  103.         animeTab.setControl(animeComposite);
  104.         animeComposite.setLayout(new FormLayout());
  105.  
  106.         createAnimeComposite(animeComposite);
  107.  
  108.         TabItem tvShowsTab = new TabItem(tabFolder, SWT.NONE);
  109.         tvShowsTab.setText("TV Shows List");
  110.  
  111.         Composite tvComposite = new Composite(tabFolder, SWT.NONE);
  112.         tvShowsTab.setControl(tvComposite);
  113.  
  114.         TabItem movieListTab = new TabItem(tabFolder, SWT.NONE);
  115.         movieListTab.setText("Movie List");
  116.  
  117.         Composite movieComposite = new Composite(tabFolder, SWT.NONE);
  118.         movieListTab.setControl(movieComposite);
  119.  
  120.         TabItem videoGamesTab = new TabItem(tabFolder, SWT.NONE);
  121.         videoGamesTab.setText("Video Game List");
  122.  
  123.         Composite videoGameComposite = new Composite(tabFolder, SWT.NONE);
  124.         videoGamesTab.setControl(videoGameComposite);
  125.  
  126.     }
  127.  
  128.     // Next procedure: retrieves the values that need to be used for the table
  129.     // and fills it in
  130.  
  131.     public void drawAnimeTable() {
  132.         animeTable.removeAll();
  133.         String[] animeTitles = { "Name", "Progress", "User score", "Critic Score", "Release date" };
  134.         ArrayList<String> tablez = this.user.returnAnimeList();
  135.         for (int i = 0; i < tablez.size(); i = i + 5) {
  136.             TableItem itemz = new TableItem(animeTable, SWT.NULL);
  137.             itemz.setText(0, tablez.get(i));
  138.             itemz.setText(1, tablez.get(i + 1));
  139.             itemz.setText(2, tablez.get(i + 2));
  140.             itemz.setText(3, tablez.get(i + 3));
  141.             itemz.setText(4, tablez.get(i + 4));
  142.         }
  143.         for (int i = 0; i < animeTitles.length; i++) {
  144.             animeTable.getColumn(i).pack();
  145.         }
  146.     }
  147.  
  148.     // Next procedure: Resets all of the buttons in the form
  149.  
  150.     public void resetButtons(Spinner userScore, Spinner progress, Button setScore, Button setProgress, Button review,
  151.             Button remove) {
  152.         userScore.setSelection(0);
  153.         userScore.setEnabled(false);
  154.         progress.setEnabled(false);
  155.         progress.setSelection(0);
  156.         setScore.setEnabled(false);
  157.         setProgress.setEnabled(false);
  158.         review.setEnabled(false);
  159.         remove.setEnabled(false);
  160.     }
  161.  
  162.     // Next procedure: saves the current contents of the users profile to a file
  163.  
  164.     public void saveUserProfile() {
  165.         try {
  166.             FileOutputStream fos = new FileOutputStream(user.getuserName() + ".dat");
  167.             ObjectOutputStream oos = new ObjectOutputStream(fos);
  168.             oos.writeObject(user);
  169.             oos.close();
  170.         } catch (IOException e1) {
  171.             e1.printStackTrace();
  172.         }
  173.     }
  174.  
  175.     // Next procedure: Creates the contents of the anime composite
  176.  
  177.     public void createAnimeComposite(Composite animeComposite) {
  178.         animeTable = new Table(animeComposite, SWT.BORDER | SWT.FULL_SELECTION);
  179.  
  180.         // Sets the columns for the table
  181.         String[] animeTitles = { "Name", "Progress", "User score", "Critic Score", "Release date" };
  182.         for (int i = 0; i < animeTitles.length; i++) {
  183.             org.eclipse.swt.widgets.TableColumn column = new org.eclipse.swt.widgets.TableColumn(animeTable, SWT.NULL);
  184.             column.setText(animeTitles[i]);
  185.         }
  186.  
  187.         FormData fd_animeTable = new FormData();
  188.         fd_animeTable.bottom = new FormAttachment(0, 643);
  189.         fd_animeTable.right = new FormAttachment(0, 989);
  190.         fd_animeTable.top = new FormAttachment(0, 10);
  191.         fd_animeTable.left = new FormAttachment(0, 10);
  192.         animeTable.setLayoutData(fd_animeTable);
  193.         animeTable.setHeaderVisible(true);
  194.         animeTable.setLinesVisible(true);
  195.  
  196.         // Next Procedure: Opens the review form
  197.  
  198.         Button btnReview = new Button(animeComposite, SWT.NONE);
  199.         btnReview.setEnabled(false);
  200.         btnReview.addSelectionListener(new SelectionAdapter() {
  201.             @Override
  202.             public void widgetSelected(SelectionEvent e) {
  203.                 if (animeTable.getItemCount() > 0 && animeTable.getSelectionIndex() != -1) {
  204.                     reviewWindow = new ItemReview(user.getAnime(animeTable.getSelectionIndex()), user);
  205.                     reviewWindow.open();
  206.                 }
  207.             }
  208.         });
  209.         FormData fd_btnReview = new FormData();
  210.         btnReview.setLayoutData(fd_btnReview);
  211.         btnReview.setText("Review");
  212.  
  213.         drawAnimeTable();
  214.  
  215.         // Next Procedure: When a user double clicks an item in in the table,
  216.         // opens a form showing further information about that item
  217.  
  218.         animeTable.addMouseListener(new MouseAdapter() {
  219.             @Override
  220.             public void mouseDoubleClick(MouseEvent e) {
  221.                 if (animeTable.getItemCount() > 0 && animeTable.getSelectionIndex() != -1) {
  222.                     animeItemDetails = new AnimeItemDetails(user.getAnime(animeTable.getSelectionIndex()));
  223.                     animeItemDetails.open();
  224.                 }
  225.             }
  226.         });
  227.  
  228.         Label lblChangeProgress = new Label(animeComposite, SWT.NONE);
  229.         FormData fd_lblChangeProgress = new FormData();
  230.         lblChangeProgress.setLayoutData(fd_lblChangeProgress);
  231.         lblChangeProgress.setText("Change Progress: ");
  232.  
  233.         Spinner spnEpisodeCounter = new Spinner(animeComposite, SWT.BORDER);
  234.         fd_lblChangeProgress.top = new FormAttachment(spnEpisodeCounter, 3, SWT.TOP);
  235.         fd_lblChangeProgress.right = new FormAttachment(100, -153);
  236.         spnEpisodeCounter.setEnabled(false);
  237.  
  238.         FormData fd_spnEpisodeCounter = new FormData();
  239.         fd_spnEpisodeCounter.top = new FormAttachment(0, 35);
  240.         spnEpisodeCounter.setLayoutData(fd_spnEpisodeCounter);
  241.  
  242.         Spinner spnUserScore = new Spinner(animeComposite, SWT.BORDER);
  243.         fd_spnEpisodeCounter.right = new FormAttachment(100, -100);
  244.         spnUserScore.setMaximum(10);
  245.         spnUserScore.setEnabled(false);
  246.         FormData fd_spnUserScore = new FormData();
  247.         fd_spnUserScore.top = new FormAttachment(0, 75);
  248.         spnUserScore.setLayoutData(fd_spnUserScore);
  249.  
  250.         Button btnSetScore = new Button(animeComposite, SWT.NONE);
  251.         fd_btnReview.top = new FormAttachment(btnSetScore, 6);
  252.         fd_btnReview.right = new FormAttachment(btnSetScore, -10, SWT.RIGHT);
  253.         fd_spnUserScore.right = new FormAttachment(btnSetScore, -6);
  254.         btnSetScore.setEnabled(false);
  255.  
  256.         Button btnSetProgress = new Button(animeComposite, SWT.NONE);
  257.         btnSetProgress.setEnabled(false);
  258.  
  259.         ListWindow pass = this;
  260.  
  261.         Button btnRemove = new Button(animeComposite, SWT.NONE);
  262.         btnRemove.setEnabled(false);
  263.  
  264.         // Opens the AnimeSearchWindow form so that a new item may be added to
  265.         // the table
  266.  
  267.         Button btnAddnewitem = new Button(animeComposite, SWT.NONE);
  268.         btnAddnewitem.addSelectionListener(new SelectionAdapter() {
  269.             @Override
  270.             public void widgetSelected(SelectionEvent e) {
  271.                 animeSearchWindow = new AnimeSearchWindow(user, pass, spnUserScore, spnEpisodeCounter, btnSetScore,
  272.                         btnSetProgress, btnReview, btnRemove);
  273.                 animeSearchWindow.open();
  274.             }
  275.         });
  276.  
  277.         FormData fd_btnAddnewitem = new FormData();
  278.         fd_btnAddnewitem.bottom = new FormAttachment(100, -10);
  279.         fd_btnAddnewitem.left = new FormAttachment(animeTable, 6);
  280.         btnAddnewitem.setLayoutData(fd_btnAddnewitem);
  281.         btnAddnewitem.setText("Add New Item");
  282.  
  283.         Label lblUserScore = new Label(animeComposite, SWT.NONE);
  284.         fd_spnUserScore.left = new FormAttachment(0, 1115);
  285.         FormData fd_lblUserScore = new FormData();
  286.         fd_lblUserScore.top = new FormAttachment(spnUserScore, 3, SWT.TOP);
  287.         fd_lblUserScore.left = new FormAttachment(lblChangeProgress, 0, SWT.LEFT);
  288.         lblUserScore.setLayoutData(fd_lblUserScore);
  289.         lblUserScore.setText("User Score:");
  290.  
  291.         // Next Procedure: Sets the score the user gives an item and redraws the
  292.         // table
  293.  
  294.         btnSetScore.addSelectionListener(new SelectionAdapter() {
  295.             @Override
  296.             public void widgetSelected(SelectionEvent e) {
  297.                 if (!spnUserScore.getText().isEmpty() && animeTable.getSelectionIndex() != -1) {
  298.                     int selectionIndex = animeTable.getSelectionIndex();
  299.                     user.getAnime(animeTable.getSelectionIndex()).setUserScore(spnUserScore.getSelection());
  300.                     drawAnimeTable();
  301.                     animeTable.setSelection(selectionIndex);
  302.                     saveUserProfile();
  303.                 }
  304.             }
  305.         });
  306.         FormData fd_btnSetScore = new FormData();
  307.         fd_btnSetScore.top = new FormAttachment(spnUserScore, -2, SWT.TOP);
  308.         fd_btnSetScore.left = new FormAttachment(0, 1162);
  309.         btnSetScore.setLayoutData(fd_btnSetScore);
  310.         btnSetScore.setText("Set Score");
  311.  
  312.         // Next Procedure: Sets the amount a user has progressed through an item
  313.         // and redraws the table
  314.  
  315.         btnSetProgress.addSelectionListener(new SelectionAdapter() {
  316.             @Override
  317.             public void widgetSelected(SelectionEvent e) {
  318.                 if (!spnEpisodeCounter.getText().isEmpty() && animeTable.getSelectionIndex() != -1) {
  319.                     int selectionIndex = animeTable.getSelectionIndex();
  320.                     user.getAnime(animeTable.getSelectionIndex()).setEpisodesWatched(spnEpisodeCounter.getText());
  321.                     drawAnimeTable();
  322.                     animeTable.setSelection(selectionIndex);
  323.                     saveUserProfile();
  324.                 }
  325.             }
  326.         });
  327.         FormData fd_btnSetProgress = new FormData();
  328.         fd_btnSetProgress.top = new FormAttachment(0, 33);
  329.         fd_btnSetProgress.left = new FormAttachment(spnEpisodeCounter, 6);
  330.         btnSetProgress.setLayoutData(fd_btnSetProgress);
  331.         btnSetProgress.setText("Set Progress");
  332.  
  333.         // Next procedure: When the user presses the remove button the, item is
  334.         // removed from the user's list, the table is redrawn and all of the
  335.         // buttons are disabled
  336.  
  337.         btnRemove.addSelectionListener(new SelectionAdapter() {
  338.             @Override
  339.             public void widgetSelected(SelectionEvent e) {
  340.                 if (animeTable.getItemCount() > 0 && animeTable.getSelectionIndex() != -1) {
  341.                     user.removeAnime(animeTable.getSelectionIndex());
  342.                     drawAnimeTable();
  343.                     resetButtons(spnUserScore, spnEpisodeCounter, btnSetScore, btnSetProgress, btnReview, btnRemove);
  344.                 }
  345.                 saveUserProfile();
  346.             }
  347.         });
  348.  
  349.         FormData fd_btnRemove = new FormData();
  350.         fd_btnRemove.top = new FormAttachment(btnReview, 6);
  351.         fd_btnRemove.left = new FormAttachment(btnReview, 0, SWT.LEFT);
  352.         btnRemove.setLayoutData(fd_btnRemove);
  353.         btnRemove.setText("Remove");
  354.  
  355.         // Next procedure: When an item is selected from the table, the values
  356.         // of the spinners are correct and enables all of the buttons
  357.  
  358.         animeTable.addSelectionListener(new SelectionAdapter() {
  359.             @Override
  360.             public void widgetSelected(SelectionEvent e) {
  361.                 if (animeTable.getSelectionIndex() != -1) {
  362.                     spnEpisodeCounter.setEnabled(true);
  363.                     spnEpisodeCounter.setMaximum(user.getAnime(animeTable.getSelectionIndex()).getNumberOfEpisodes());
  364.                     spnEpisodeCounter.setSelection(user.getAnime(animeTable.getSelectionIndex()).getEpisodesWatched());
  365.                     spnUserScore.setEnabled(true);
  366.                     spnUserScore.setSelection(user.getAnime(animeTable.getSelectionIndex()).getUserScore());
  367.                     btnSetProgress.setEnabled(true);
  368.                     btnSetScore.setEnabled(true);
  369.                     btnReview.setEnabled(true);
  370.                     btnRemove.setEnabled(true);
  371.                 }
  372.             }
  373.         });
  374.  
  375.         animeComposite.pack();
  376.  
  377.     }
  378. }
Add Comment
Please, Sign In to add comment