Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package dml.userInterface;
- import org.eclipse.swt.widgets.Display;
- import org.eclipse.swt.widgets.Shell;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.ObjectInputStream;
- import java.io.ObjectOutputStream;
- import java.util.ArrayList;
- import org.eclipse.swt.SWT;
- import org.eclipse.swt.widgets.TabFolder;
- import org.eclipse.swt.widgets.TabItem;
- import org.eclipse.swt.widgets.Composite;
- import org.eclipse.swt.layout.FormLayout;
- import org.eclipse.swt.widgets.Table;
- import org.eclipse.swt.widgets.TableItem;
- import org.eclipse.swt.layout.FormData;
- import org.eclipse.swt.layout.FormAttachment;
- import org.eclipse.swt.events.MouseAdapter;
- import org.eclipse.swt.events.MouseEvent;
- import dml.logic.AnimeItem;
- import dml.logic.UserProfile;
- import org.eclipse.swt.layout.FillLayout;
- import org.eclipse.swt.events.ShellAdapter;
- import org.eclipse.swt.events.ShellEvent;
- import org.eclipse.swt.widgets.Button;
- import org.eclipse.swt.events.SelectionAdapter;
- import org.eclipse.swt.events.SelectionEvent;
- import org.eclipse.swt.widgets.Label;
- import org.eclipse.swt.widgets.Spinner;
- import org.eclipse.swt.events.ModifyListener;
- import org.eclipse.swt.events.ModifyEvent;
- import org.eclipse.swt.events.FocusAdapter;
- import org.eclipse.swt.events.FocusEvent;
- import org.eclipse.wb.swt.SWTResourceManager;
- public class ListWindow {
- protected Shell listWindow;
- private AnimeItemDetails animeItemDetails;
- private UserProfile user;
- private Table animeTable;
- private ItemReview reviewWindow;
- private AnimeSearchWindow animeSearchWindow;
- // The constructor takes the string given to it and reads from a file, if
- // the boolean given to it is true, otherwise it creates a new file for the
- // user
- public ListWindow(String userName, Boolean existing) {
- if (existing == false) {
- this.user = new UserProfile(userName);
- saveUserProfile();
- } else {
- try {
- FileInputStream fis = new FileInputStream(userName + ".dat");
- ObjectInputStream ois = new ObjectInputStream(fis);
- this.user = (UserProfile) ois.readObject();
- ois.close();
- } catch (IOException e) {
- e.printStackTrace();
- } catch (ClassNotFoundException ex) {
- ex.printStackTrace();
- }
- }
- }
- /**
- * @wbp.parser.entryPoint
- */
- public void open() {
- createContents();
- listWindow.open();
- listWindow.layout();
- }
- /**
- * Create contents of the window.
- */
- protected void createContents() {
- listWindow = new Shell(Display.getCurrent());
- listWindow.addShellListener(new ShellAdapter() {
- @Override
- public void shellClosed(ShellEvent e) {
- saveUserProfile();
- Display.getCurrent().close();
- }
- });
- listWindow.setSize(1280, 720);
- listWindow.setText("User List of " + this.user.getuserName());
- listWindow.setLayout(new FillLayout(SWT.HORIZONTAL));
- TabFolder tabFolder = new TabFolder(listWindow, SWT.NONE);
- TabItem animeTab = new TabItem(tabFolder, SWT.NONE);
- animeTab.setText("Anime List");
- Composite animeComposite = new Composite(tabFolder, SWT.NONE);
- animeTab.setControl(animeComposite);
- animeComposite.setLayout(new FormLayout());
- createAnimeComposite(animeComposite);
- TabItem tvShowsTab = new TabItem(tabFolder, SWT.NONE);
- tvShowsTab.setText("TV Shows List");
- Composite tvComposite = new Composite(tabFolder, SWT.NONE);
- tvShowsTab.setControl(tvComposite);
- TabItem movieListTab = new TabItem(tabFolder, SWT.NONE);
- movieListTab.setText("Movie List");
- Composite movieComposite = new Composite(tabFolder, SWT.NONE);
- movieListTab.setControl(movieComposite);
- TabItem videoGamesTab = new TabItem(tabFolder, SWT.NONE);
- videoGamesTab.setText("Video Game List");
- Composite videoGameComposite = new Composite(tabFolder, SWT.NONE);
- videoGamesTab.setControl(videoGameComposite);
- }
- // Next procedure: retrieves the values that need to be used for the table
- // and fills it in
- public void drawAnimeTable() {
- animeTable.removeAll();
- String[] animeTitles = { "Name", "Progress", "User score", "Critic Score", "Release date" };
- ArrayList<String> tablez = this.user.returnAnimeList();
- for (int i = 0; i < tablez.size(); i = i + 5) {
- TableItem itemz = new TableItem(animeTable, SWT.NULL);
- itemz.setText(0, tablez.get(i));
- itemz.setText(1, tablez.get(i + 1));
- itemz.setText(2, tablez.get(i + 2));
- itemz.setText(3, tablez.get(i + 3));
- itemz.setText(4, tablez.get(i + 4));
- }
- for (int i = 0; i < animeTitles.length; i++) {
- animeTable.getColumn(i).pack();
- }
- }
- // Next procedure: Resets all of the buttons in the form
- public void resetButtons(Spinner userScore, Spinner progress, Button setScore, Button setProgress, Button review,
- Button remove) {
- userScore.setSelection(0);
- userScore.setEnabled(false);
- progress.setEnabled(false);
- progress.setSelection(0);
- setScore.setEnabled(false);
- setProgress.setEnabled(false);
- review.setEnabled(false);
- remove.setEnabled(false);
- }
- // Next procedure: saves the current contents of the users profile to a file
- public void saveUserProfile() {
- try {
- FileOutputStream fos = new FileOutputStream(user.getuserName() + ".dat");
- ObjectOutputStream oos = new ObjectOutputStream(fos);
- oos.writeObject(user);
- oos.close();
- } catch (IOException e1) {
- e1.printStackTrace();
- }
- }
- // Next procedure: Creates the contents of the anime composite
- public void createAnimeComposite(Composite animeComposite) {
- animeTable = new Table(animeComposite, SWT.BORDER | SWT.FULL_SELECTION);
- // Sets the columns for the table
- String[] animeTitles = { "Name", "Progress", "User score", "Critic Score", "Release date" };
- for (int i = 0; i < animeTitles.length; i++) {
- org.eclipse.swt.widgets.TableColumn column = new org.eclipse.swt.widgets.TableColumn(animeTable, SWT.NULL);
- column.setText(animeTitles[i]);
- }
- FormData fd_animeTable = new FormData();
- fd_animeTable.bottom = new FormAttachment(0, 643);
- fd_animeTable.right = new FormAttachment(0, 989);
- fd_animeTable.top = new FormAttachment(0, 10);
- fd_animeTable.left = new FormAttachment(0, 10);
- animeTable.setLayoutData(fd_animeTable);
- animeTable.setHeaderVisible(true);
- animeTable.setLinesVisible(true);
- // Next Procedure: Opens the review form
- Button btnReview = new Button(animeComposite, SWT.NONE);
- btnReview.setEnabled(false);
- btnReview.addSelectionListener(new SelectionAdapter() {
- @Override
- public void widgetSelected(SelectionEvent e) {
- if (animeTable.getItemCount() > 0 && animeTable.getSelectionIndex() != -1) {
- reviewWindow = new ItemReview(user.getAnime(animeTable.getSelectionIndex()), user);
- reviewWindow.open();
- }
- }
- });
- FormData fd_btnReview = new FormData();
- btnReview.setLayoutData(fd_btnReview);
- btnReview.setText("Review");
- drawAnimeTable();
- // Next Procedure: When a user double clicks an item in in the table,
- // opens a form showing further information about that item
- animeTable.addMouseListener(new MouseAdapter() {
- @Override
- public void mouseDoubleClick(MouseEvent e) {
- if (animeTable.getItemCount() > 0 && animeTable.getSelectionIndex() != -1) {
- animeItemDetails = new AnimeItemDetails(user.getAnime(animeTable.getSelectionIndex()));
- animeItemDetails.open();
- }
- }
- });
- Label lblChangeProgress = new Label(animeComposite, SWT.NONE);
- FormData fd_lblChangeProgress = new FormData();
- lblChangeProgress.setLayoutData(fd_lblChangeProgress);
- lblChangeProgress.setText("Change Progress: ");
- Spinner spnEpisodeCounter = new Spinner(animeComposite, SWT.BORDER);
- fd_lblChangeProgress.top = new FormAttachment(spnEpisodeCounter, 3, SWT.TOP);
- fd_lblChangeProgress.right = new FormAttachment(100, -153);
- spnEpisodeCounter.setEnabled(false);
- FormData fd_spnEpisodeCounter = new FormData();
- fd_spnEpisodeCounter.top = new FormAttachment(0, 35);
- spnEpisodeCounter.setLayoutData(fd_spnEpisodeCounter);
- Spinner spnUserScore = new Spinner(animeComposite, SWT.BORDER);
- fd_spnEpisodeCounter.right = new FormAttachment(100, -100);
- spnUserScore.setMaximum(10);
- spnUserScore.setEnabled(false);
- FormData fd_spnUserScore = new FormData();
- fd_spnUserScore.top = new FormAttachment(0, 75);
- spnUserScore.setLayoutData(fd_spnUserScore);
- Button btnSetScore = new Button(animeComposite, SWT.NONE);
- fd_btnReview.top = new FormAttachment(btnSetScore, 6);
- fd_btnReview.right = new FormAttachment(btnSetScore, -10, SWT.RIGHT);
- fd_spnUserScore.right = new FormAttachment(btnSetScore, -6);
- btnSetScore.setEnabled(false);
- Button btnSetProgress = new Button(animeComposite, SWT.NONE);
- btnSetProgress.setEnabled(false);
- ListWindow pass = this;
- Button btnRemove = new Button(animeComposite, SWT.NONE);
- btnRemove.setEnabled(false);
- // Opens the AnimeSearchWindow form so that a new item may be added to
- // the table
- Button btnAddnewitem = new Button(animeComposite, SWT.NONE);
- btnAddnewitem.addSelectionListener(new SelectionAdapter() {
- @Override
- public void widgetSelected(SelectionEvent e) {
- animeSearchWindow = new AnimeSearchWindow(user, pass, spnUserScore, spnEpisodeCounter, btnSetScore,
- btnSetProgress, btnReview, btnRemove);
- animeSearchWindow.open();
- }
- });
- FormData fd_btnAddnewitem = new FormData();
- fd_btnAddnewitem.bottom = new FormAttachment(100, -10);
- fd_btnAddnewitem.left = new FormAttachment(animeTable, 6);
- btnAddnewitem.setLayoutData(fd_btnAddnewitem);
- btnAddnewitem.setText("Add New Item");
- Label lblUserScore = new Label(animeComposite, SWT.NONE);
- fd_spnUserScore.left = new FormAttachment(0, 1115);
- FormData fd_lblUserScore = new FormData();
- fd_lblUserScore.top = new FormAttachment(spnUserScore, 3, SWT.TOP);
- fd_lblUserScore.left = new FormAttachment(lblChangeProgress, 0, SWT.LEFT);
- lblUserScore.setLayoutData(fd_lblUserScore);
- lblUserScore.setText("User Score:");
- // Next Procedure: Sets the score the user gives an item and redraws the
- // table
- btnSetScore.addSelectionListener(new SelectionAdapter() {
- @Override
- public void widgetSelected(SelectionEvent e) {
- if (!spnUserScore.getText().isEmpty() && animeTable.getSelectionIndex() != -1) {
- int selectionIndex = animeTable.getSelectionIndex();
- user.getAnime(animeTable.getSelectionIndex()).setUserScore(spnUserScore.getSelection());
- drawAnimeTable();
- animeTable.setSelection(selectionIndex);
- saveUserProfile();
- }
- }
- });
- FormData fd_btnSetScore = new FormData();
- fd_btnSetScore.top = new FormAttachment(spnUserScore, -2, SWT.TOP);
- fd_btnSetScore.left = new FormAttachment(0, 1162);
- btnSetScore.setLayoutData(fd_btnSetScore);
- btnSetScore.setText("Set Score");
- // Next Procedure: Sets the amount a user has progressed through an item
- // and redraws the table
- btnSetProgress.addSelectionListener(new SelectionAdapter() {
- @Override
- public void widgetSelected(SelectionEvent e) {
- if (!spnEpisodeCounter.getText().isEmpty() && animeTable.getSelectionIndex() != -1) {
- int selectionIndex = animeTable.getSelectionIndex();
- user.getAnime(animeTable.getSelectionIndex()).setEpisodesWatched(spnEpisodeCounter.getText());
- drawAnimeTable();
- animeTable.setSelection(selectionIndex);
- saveUserProfile();
- }
- }
- });
- FormData fd_btnSetProgress = new FormData();
- fd_btnSetProgress.top = new FormAttachment(0, 33);
- fd_btnSetProgress.left = new FormAttachment(spnEpisodeCounter, 6);
- btnSetProgress.setLayoutData(fd_btnSetProgress);
- btnSetProgress.setText("Set Progress");
- // Next procedure: When the user presses the remove button the, item is
- // removed from the user's list, the table is redrawn and all of the
- // buttons are disabled
- btnRemove.addSelectionListener(new SelectionAdapter() {
- @Override
- public void widgetSelected(SelectionEvent e) {
- if (animeTable.getItemCount() > 0 && animeTable.getSelectionIndex() != -1) {
- user.removeAnime(animeTable.getSelectionIndex());
- drawAnimeTable();
- resetButtons(spnUserScore, spnEpisodeCounter, btnSetScore, btnSetProgress, btnReview, btnRemove);
- }
- saveUserProfile();
- }
- });
- FormData fd_btnRemove = new FormData();
- fd_btnRemove.top = new FormAttachment(btnReview, 6);
- fd_btnRemove.left = new FormAttachment(btnReview, 0, SWT.LEFT);
- btnRemove.setLayoutData(fd_btnRemove);
- btnRemove.setText("Remove");
- // Next procedure: When an item is selected from the table, the values
- // of the spinners are correct and enables all of the buttons
- animeTable.addSelectionListener(new SelectionAdapter() {
- @Override
- public void widgetSelected(SelectionEvent e) {
- if (animeTable.getSelectionIndex() != -1) {
- spnEpisodeCounter.setEnabled(true);
- spnEpisodeCounter.setMaximum(user.getAnime(animeTable.getSelectionIndex()).getNumberOfEpisodes());
- spnEpisodeCounter.setSelection(user.getAnime(animeTable.getSelectionIndex()).getEpisodesWatched());
- spnUserScore.setEnabled(true);
- spnUserScore.setSelection(user.getAnime(animeTable.getSelectionIndex()).getUserScore());
- btnSetProgress.setEnabled(true);
- btnSetScore.setEnabled(true);
- btnReview.setEnabled(true);
- btnRemove.setEnabled(true);
- }
- }
- });
- animeComposite.pack();
- }
- }
Add Comment
Please, Sign In to add comment