OOP-Projects-Java

Log | Files | Refs | README

TaskHandler.java (4119B)


      1 package assignment2;
      2 
      3 import java.util.ArrayList;
      4 import java.util.Collections;
      5 import java.util.Comparator;
      6 import java.util.List;
      7 
      8 import javax.swing.BoxLayout;
      9 import javax.swing.JLabel;
     10 import javax.swing.JPanel;
     11 import javax.swing.JScrollPane;
     12 import javax.swing.SwingUtilities;
     13 
     14 import se.his.it401g.todo.Task;
     15 import se.his.it401g.todo.TaskListener;
     16 
     17 public class TaskHandler implements TaskListener {
     18 	private JPanel taskList;
     19 	private JScrollPane scrollPane;
     20 	private JLabel taskProgress;
     21 	private int completedTaskCount;
     22 	
     23 	/**
     24 	 * Constructor for taskHandler class.
     25 	 * 
     26 	 * Creates tasks and adds them to the Scrollpane
     27 	 * Also handles the tasks states (completed, uncompleted)
     28 	 * And worktasks color.
     29 	 */
     30 	public TaskHandler() {
     31 		taskList = new JPanel();
     32 		taskList.setLayout(new BoxLayout(taskList, BoxLayout.PAGE_AXIS));
     33 		
     34 		scrollPane = new JScrollPane();
     35 		scrollPane.getVerticalScrollBar().setUnitIncrement(16);
     36 		scrollPane.getViewport().add(taskList);
     37 		
     38 		completedTaskCount = 0;
     39 		taskProgress = new JLabel();
     40 		taskProgress.setHorizontalAlignment(JLabel.CENTER);
     41 		updateTaskProgress();
     42 	}
     43 	
     44 	/**
     45 	 * @return scrollPane of type JScrollPane
     46 	 */
     47 	public JScrollPane getScrollPane() {
     48 		return scrollPane;
     49 	}
     50 	
     51 	/**
     52 	 * @return taskProgress of type JLabel
     53 	 */
     54 	public JLabel getTaskProgress() {
     55 		return taskProgress;
     56 	}
     57 	
     58 	/**
     59 	 * @return taskList of type JPanel
     60 	 */
     61 	public JPanel getTaskList() {
     62 		return taskList;
     63 	}
     64 	
     65 	public void sort(String sortingOption) {
     66 		List<Task> sortedList = new ArrayList<Task>();
     67 		for (int i = 0; i < taskList.getComponentCount(); i++) {
     68 			sortedList.add((Task)taskList.getComponent(i));
     69 		}
     70 		
     71 		switch (sortingOption)
     72 		{
     73 		    case "Alphabetical":
     74 		    	Collections.sort(sortedList, new comparators.SortByName());
     75 		    	break;
     76 		    case "Type":
     77 		    	Collections.sort(sortedList, new comparators.SortByType());
     78 		    	break;
     79 		    case "Completed":
     80 		    	Collections.sort(sortedList, new comparators.SortByState());
     81 		    	break;
     82 		    default:
     83 		    	
     84 		}
     85 		taskList.removeAll();
     86 		
     87 		for (int i = 0; i < sortedList.size(); i++) {
     88 			taskList.add(sortedList.get(i).getGuiComponent());
     89 			
     90 		}
     91 		
     92 		//update the frame to show currently visible tasks.
     93 		SwingUtilities.updateComponentTreeUI(taskList);
     94 	}
     95 	
     96 	/**
     97 	 * Runs when a task is marked as complete
     98 	 * updates task progress label
     99 	 * and updates the swing component
    100 	 */
    101 	@Override
    102 	public void taskCompleted(Task t) {
    103 		// TODO Auto-generated method stub
    104 		completedTaskCount++;
    105 		updateTaskProgress();
    106 		
    107 		//update the frame to show currently visible tasks.
    108 		SwingUtilities.updateComponentTreeUI(taskList);
    109 	}
    110 
    111 	/**
    112 	 * Runs when a task is unmarked as completed
    113 	 * updates task progress label
    114 	 * and updates the swing component
    115 	 */
    116 	@Override
    117 	public void taskUncompleted(Task t) {
    118 		// TODO Auto-generated method stub
    119 		completedTaskCount--;
    120 		updateTaskProgress();
    121 		
    122 		//update the frame to show currently visible tasks.
    123 		SwingUtilities.updateComponentTreeUI(taskList);
    124 	}
    125 
    126 	/**
    127 	 * Runs when a task is created
    128 	 * updates task progress label
    129 	 * and updates the swing component
    130 	 */
    131 	@Override
    132 	public void taskCreated(Task t) {
    133 		// TODO Auto-generated method stub
    134 		t.setTaskListener(this);
    135 		taskList.add(t.getGuiComponent());
    136 		
    137 		updateTaskProgress();
    138 		
    139 		//update the frame to show currently visible tasks.
    140 		SwingUtilities.updateComponentTreeUI(taskList);
    141 	}
    142 	
    143 	/**
    144 	 * Runs when a task is removed
    145 	 * updates task progress label
    146 	 * and updates the swing component
    147 	 */
    148 	@Override
    149 	public void taskRemoved(Task t) {
    150 		taskList.remove(t.getGuiComponent());
    151 		if (t.isComplete()) {
    152 			completedTaskCount--;
    153 		}
    154 		
    155 		updateTaskProgress();
    156 		
    157 		//update the frame to show currently visible tasks.
    158 		SwingUtilities.updateComponentTreeUI(taskList);
    159 	}
    160 	
    161 	/*
    162 	 * Updates the task progress to show the current amount
    163 	 * of tasks.
    164 	 */
    165 	private void updateTaskProgress() {
    166 		taskProgress.setText(completedTaskCount + " out of " + taskList.getComponentCount() + " tasks completed.");
    167 	}
    168 
    169 	@Override
    170 	public void taskChanged(Task t) {
    171 		// TODO Auto-generated method stub
    172 		
    173 	}
    174 }