OOP-Projects-Java

Log | Files | Refs | README

GUI.java (3553B)


      1 package assignment2;
      2 
      3 import java.awt.BorderLayout;
      4 import java.awt.Dimension;
      5 import java.awt.event.*;
      6 import java.util.HashMap;
      7 import java.util.Map;
      8 
      9 import javax.swing.*;
     10 
     11 import se.his.it401g.todo.HomeTask;
     12 import se.his.it401g.todo.StudyTask;
     13 import se.his.it401g.todo.Task;
     14 
     15 public class GUI implements ActionListener {
     16 	// TaskHandler creates a new scrollPane and taskList and puts the taskList on the scrollPane.
     17 	// TaskHandler also tracks the amount of tasks created, and the amount of tasks marked completed.
     18 	private TaskHandler taskHandler;
     19 	private JFrame frame = new JFrame();
     20 	private JPanel menu;
     21 	private Dimension menuBar = new Dimension(50, 50);
     22 	private Dimension window = new Dimension(600, 500);
     23 	private Map<String, JButton> buttons = new HashMap<String, JButton>();
     24 	
     25 	/*
     26 	 * Initialize the GUI using Swing
     27 	 */
     28 	public GUI() {		
     29 		//set frame variables and make frame visible
     30 		frame.setSize(window);
     31 		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     32 		
     33 		//add buttons for creating new tasks (inside panel)
     34 		menu = new JPanel();
     35 		menu.setSize(menuBar);
     36 		
     37 		createMenuButton("study", "New StudyTask");
     38 		createMenuButton("home", "New HomeTask");
     39 		createMenuButton("work", "New WorkTask");
     40 		createMenuButton("sort", "Sort");
     41 		
     42 		frame.add(menu, BorderLayout.NORTH);
     43 		
     44 		taskHandler = new TaskHandler();
     45 		frame.add(taskHandler.getScrollPane());
     46 		frame.add(taskHandler.getTaskProgress(), BorderLayout.SOUTH);
     47 		
     48 		frame.setVisible(true);
     49 	}
     50 	
     51 	/*
     52 	 * Create button, add ActionListener to button and add button to GUI.
     53 	 * 
     54 	 * @param key		Used for distinguishing buttons from each other, unique "name".
     55 	 * @param btnText	Button text.
     56 	 */
     57 	private void createMenuButton(String key, String btnText) {
     58 		buttons.put(key, new JButton(btnText)); //Create new button and add to Map using key value.
     59 		buttons.get(key).addActionListener(this); //add ActionListener to button.
     60 		menu.add(buttons.get(key)); // add button to GUI.
     61 	}
     62 	
     63 	
     64 	/*
     65 	 * Used to create generic dialogue boxes
     66 	 * 
     67 	 * @param 	String[] options List of options available to the user.
     68 	 * @param 	String label 	 Title shown to user during selection process.
     69 	 * @return	String 			 value of selected option.
     70 	 */
     71 	public String selectionBox(String label, String[] options) {
     72 		Object selected = JOptionPane.showInputDialog(null, "Choose sorting type", "Selection", JOptionPane.DEFAULT_OPTION, null, options, "0");
     73 		if ( selected != null ) {
     74 		    return selected.toString(); //return selected option.
     75 		}
     76 		//If no selection is made (user cancelled) return empty string.
     77 		return "";
     78 	}
     79 	
     80 	
     81 	/*
     82 	 * Handles actions performed by the user e.g. button clicks.
     83 	 * 
     84 	 * @param ActionEvent e tracks all occurring events.
     85 	 */
     86 	@Override
     87 	public void actionPerformed(ActionEvent e) {
     88 		
     89 		if (e.getSource().equals(buttons.get("sort"))) {
     90 			if (taskHandler.getTaskList().getComponentCount() != 0) {
     91 				//Open dialogue box where user can choose sorting algorithm
     92 				String[] sortingOptions = {"Alphabetical", "Type", "Completed"};
     93 				String selectedOption = selectionBox("Sort By:", sortingOptions);
     94 				taskHandler.sort(selectedOption);
     95 			}
     96 		}
     97 		else {
     98 			Task t;	
     99 			if (e.getSource().equals(buttons.get("home"))) {
    100 				t = new HomeTask();
    101 			} 
    102 			else if (e.getSource().equals(buttons.get("study"))) {
    103 				t = new StudyTask();
    104 			}
    105 			else {
    106 				String[] colors = {"BLACK", "RED", "GREEN", "BLUE"};
    107 				String selectedColor = selectionBox("Choose color", colors);
    108 				t = new WorkTask(selectedColor);
    109 			}
    110 			taskHandler.taskCreated(t);	
    111 		}	
    112 	}
    113 }