TodolistItemTable.java :  » Groupware » lucane » org » lucane » applications » todolist » gui » Java Open Source

Java Open Source » Groupware » lucane 
lucane » org » lucane » applications » todolist » gui » TodolistItemTable.java
/*
 * Lucane - a collaborative platform
 * Copyright (C) 2004  Jonathan Riboux <jonathan.riboux@wanadoo.Fr>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package org.lucane.applications.todolist.gui;

import java.awt.Color;
import java.awt.Component;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.text.DateFormat;
import java.util.Date;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import javax.swing.DefaultListSelectionModel;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableColumnModel;

import org.lucane.applications.todolist.TodolistItem;

public class TodolistItemTable extends JTable {
  public TodolistItemTable() {
    super();

    setDefaultRenderer(String.class, new ColoredCellRenderer());
    
    setModel(new TodolistItemTableModel());
    setSelectionModel(new DefaultListSelectionModel());
    
    for (int i=1; i<=6; i++) {
      getColumnModel().getColumn(i).setMinWidth(70);
      getColumnModel().getColumn(i).setMaxWidth(70);
      getColumnModel().getColumn(i).setResizable(false);
    }
    setRowHeight(getRowHeight()*2);
    getTableHeader().addMouseListener(new MouseAdapter() {
      public void mouseClicked(MouseEvent e) {
          TableColumnModel columnModel = getColumnModel();   
          int col = columnModel.getColumnIndexAtX(e.getX()); 
          if(e.getClickCount()==1 && col>=0) {
          ((TodolistItemTableModel)getModel()).sort(col);
          }
      }
    });
  }

  public static void setColumnsNames(String [] names) {
    TodolistItemTableModel.setColumnsNames(names);
  }
  
  public void addListSelectionListener(ListSelectionListener lsl) {
    getSelectionModel().addListSelectionListener(lsl);
  }
  
  public TodolistItem getSelectedTodolistItem() {
    int selectedIndex = getSelectionModel().getMinSelectionIndex();
    if (selectedIndex>=0)
      return (TodolistItem)((TodolistItemTableModel)getModel()).getValueAt(selectedIndex);
    return null;
  }

  public void add(TodolistItem tli) {
    ((TodolistItemTableModel)getModel()).add(tli);
  }
  
  public void remove(TodolistItem tli) {
    ((TodolistItemTableModel)getModel()).remove(tli);
  }
  
  public void clear() {
    ((TodolistItemTableModel)getModel()).clear();
  }
  
}

class TodolistItemTableModel extends AbstractTableModel {

  private static String[] columnsNames = {"Name", "Priority", "Complete", "Estimated start", "Estimated end", "real start", "Real end"};
  private static final int[] COLUMNS_SORT = {TodolistItemsSorter.NAME, TodolistItemsSorter.PRIORITY, TodolistItemsSorter.COMPLETED, TodolistItemsSorter.ESTIMATED_START, TodolistItemsSorter.ESTIMATED_END, TodolistItemsSorter.REAL_START, TodolistItemsSorter.REAL_END};
  private ArrayList items;
  private Comparator comparator;
  
  private int sortedCol = -1;
  private int sortDirection = TodolistItemsSorter.ASC;
  
  public TodolistItemTableModel() {
    items = new ArrayList();
    comparator = new TodolistItemsSorter(TodolistItemsSorter.NAME, TodolistItemsSorter.ASC);
  }

  public static void setColumnsNames(String [] names) {
    columnsNames = names;
  }

  public void setComparator(Comparator comparator) {
    this.comparator = comparator;
  }

  public void sort(int columnIndex) {
    if (columnIndex==sortedCol) {
      sortDirection = sortDirection == TodolistItemsSorter.ASC
          ? TodolistItemsSorter.DESC
          : TodolistItemsSorter.ASC;
    } else {
      sortedCol = columnIndex;
      sortDirection = TodolistItemsSorter.ASC;
    }
    
    comparator = new TodolistItemsSorter(COLUMNS_SORT[sortedCol], sortDirection);
    Collections.sort(items, comparator);
    fireTableDataChanged();
  }
  
  public void add(TodolistItem item) {
    items.add(item);
    Collections.sort(items, comparator);
    fireTableDataChanged();
  }
  
  public void remove(TodolistItem item) {
    items.remove(item);
    fireTableDataChanged();
  }
  
  public Class getColumnClass(int col) {
    return String.class;
  }

  public String getColumnName(int col) {
    return columnsNames[col];
  }

  public boolean isCellEditable(int row, int col) {
    return false;
  }

  public void setValueAt(Object value, int row, int col) {
    super.setValueAt(value, row, col);
  }
  
  public int getColumnCount() {
    return columnsNames.length;
  }

  public int getRowCount() {
    return items.size();
  }

  public Object getValueAt(int row, int col) {
    TodolistItem item = (TodolistItem)items.get(row);

    DateFormat df1 = DateFormat.getDateInstance(DateFormat.SHORT);
    DateFormat df2 = DateFormat.getTimeInstance(DateFormat.SHORT);

    switch (col) {
      case 0 :
        return item.getName();
      case 1 :
        return TodolistItem.getPriorityLabels()[item.getPriority()];
      case 2 :
        return TodolistItem.getCompleteLabels()[item.isCompleted()?1:0];
      case 3 :
        if (item.getEstimatedStartDate()==null) return "";
        return "<HTML>"+df1.format(item.getEstimatedStartDate())+"<BR>"+df2.format(item.getEstimatedStartDate())+"</HTML>";
      case 4 :
        if (item.getEstimatedEndDate()==null) return "";
        return "<HTML>"+df1.format(item.getEstimatedEndDate())+"<BR>"+df2.format(item.getEstimatedEndDate())+"</HTML>";
      case 5 :
        if (item.getStartDate()==null) return "";
        return "<HTML>"+df1.format(item.getStartDate())+"<BR>"+df2.format(item.getStartDate())+"</HTML>";
      case 6 :
        if (item.getEndDate()==null) return "";
        return "<HTML>"+df1.format(item.getEndDate())+"<BR>"+df2.format(item.getEndDate())+"</HTML>";
    }
    return "";
  }

  public Object getValueAt(int row) {
    try {
      return ((TodolistItem)items.get(row));
    } catch (IndexOutOfBoundsException e) {
      return null;
    }
  }
  
  public void clear() {
    items.clear();
    fireTableRowsDeleted(0, 0);
  }
}

class TodolistItemsSorter implements Comparator {

  public static final int ASC = 1;
  public static final int DESC = -1;

  public static final int NAME = 0;
  public static final int PRIORITY = 1;
  public static final int COMPLETED = 2;
  public static final int ESTIMATED_START = 3;
  public static final int ESTIMATED_END = 4;
  public static final int REAL_START = 5;
  public static final int REAL_END = 6;
  
  private int sortBy = NAME;
  private int direction = ASC;
  
  public TodolistItemsSorter(int sortBy, int direction) {
    this.sortBy = sortBy;
    this.direction = direction;
  }
  
  public int compare(Object o1, Object o2) {
    TodolistItem tli1 = (TodolistItem) o1;
    TodolistItem tli2 = (TodolistItem) o2;
    int res = 0;
    switch (sortBy) {
      case NAME :
        res = direction*(tli1.getName().compareTo(tli2.getName()));
        if (res == 0) res = direction*((tli1.isCompleted()?1:0)-(tli2.isCompleted()?1:0));
        if (res == 0) res = direction*(tli2.getPriority()-tli1.getPriority());
        if (res == 0) res = direction*compareDates(tli1.getStartDate(),tli2.getStartDate());
        if (res == 0) res = direction*compareDates(tli1.getEndDate(),tli2.getEndDate());
        if (res == 0) res = direction*compareDates(tli1.getEstimatedStartDate(),tli2.getEstimatedStartDate());
        if (res == 0) res = direction*compareDates(tli1.getEstimatedEndDate(),tli2.getEstimatedEndDate());
        break;
      case PRIORITY :
        res = direction*((tli1.isCompleted()?1:0)-(tli2.isCompleted()?1:0));
        if (res == 0) res = direction*(tli2.getPriority()-tli1.getPriority());
        if (res == 0) res = direction*(tli1.getName().compareTo(tli2.getName()));
        if (res == 0) res = direction*compareDates(tli1.getStartDate(),tli2.getStartDate());
        if (res == 0) res = direction*compareDates(tli1.getEndDate(),tli2.getEndDate());
        if (res == 0) res = direction*compareDates(tli1.getEstimatedStartDate(),tli2.getEstimatedStartDate());
        if (res == 0) res = direction*compareDates(tli1.getEstimatedEndDate(),tli2.getEstimatedEndDate());
        break;
      case COMPLETED :
        res = direction*((tli1.isCompleted()?1:0)-(tli2.isCompleted()?1:0));
        if (res == 0) res = direction*(tli1.getName().compareTo(tli2.getName()));
        if (res == 0) res = direction*(tli2.getPriority()-tli1.getPriority());
        if (res == 0) res = direction*compareDates(tli1.getStartDate(),tli2.getStartDate());
        if (res == 0) res = direction*compareDates(tli1.getEndDate(),tli2.getEndDate());
        if (res == 0) res = direction*compareDates(tli1.getEstimatedStartDate(),tli2.getEstimatedStartDate());
        if (res == 0) res = direction*compareDates(tli1.getEstimatedEndDate(),tli2.getEstimatedEndDate());
        break;
      case REAL_START :
        if (res == 0) res = direction*compareDates(tli1.getStartDate(),tli2.getStartDate());
        if (res == 0) res = direction*(tli1.getName().compareTo(tli2.getName()));
        if (res == 0) res = direction*((tli1.isCompleted()?1:0)-(tli2.isCompleted()?1:0));
        if (res == 0) res = direction*(tli2.getPriority()-tli1.getPriority());
        if (res == 0) res = direction*compareDates(tli1.getEndDate(),tli2.getEndDate());
        if (res == 0) res = direction*compareDates(tli1.getEstimatedStartDate(),tli2.getEstimatedStartDate());
        if (res == 0) res = direction*compareDates(tli1.getEstimatedEndDate(),tli2.getEstimatedEndDate());
        break;
      case REAL_END :
        if (res == 0) res = direction*compareDates(tli1.getEndDate(),tli2.getEndDate());
        if (res == 0) res = direction*(tli1.getName().compareTo(tli2.getName()));
        if (res == 0) res = direction*((tli1.isCompleted()?1:0)-(tli2.isCompleted()?1:0));
        if (res == 0) res = direction*(tli2.getPriority()-tli1.getPriority());
        if (res == 0) res = direction*compareDates(tli1.getStartDate(),tli2.getStartDate());
        if (res == 0) res = direction*compareDates(tli1.getEstimatedStartDate(),tli2.getEstimatedStartDate());
        if (res == 0) res = direction*compareDates(tli1.getEstimatedEndDate(),tli2.getEstimatedEndDate());
        break;
      case ESTIMATED_START :
        if (res == 0) res = direction*compareDates(tli1.getEstimatedStartDate(),tli2.getEstimatedStartDate());
        if (res == 0) res = direction*(tli1.getName().compareTo(tli2.getName()));
        if (res == 0) res = direction*((tli1.isCompleted()?1:0)-(tli2.isCompleted()?1:0));
        if (res == 0) res = direction*(tli2.getPriority()-tli1.getPriority());
        if (res == 0) res = direction*compareDates(tli1.getEstimatedEndDate(),tli2.getEstimatedEndDate());
        if (res == 0) res = direction*compareDates(tli1.getStartDate(),tli2.getStartDate());
        if (res == 0) res = direction*compareDates(tli1.getEndDate(),tli2.getEndDate());
        break;
      case ESTIMATED_END :
        if (res == 0) res = direction*compareDates(tli1.getEstimatedEndDate(),tli2.getEstimatedEndDate());
        if (res == 0) res = direction*(tli1.getName().compareTo(tli2.getName()));
        if (res == 0) res = direction*((tli1.isCompleted()?1:0)-(tli2.isCompleted()?1:0));
        if (res == 0) res = direction*(tli2.getPriority()-tli1.getPriority());
        if (res == 0) res = direction*compareDates(tli1.getEstimatedStartDate(),tli2.getEstimatedStartDate());
        if (res == 0) res = direction*compareDates(tli1.getStartDate(),tli2.getStartDate());
        if (res == 0) res = direction*compareDates(tli1.getEndDate(),tli2.getEndDate());
        break;
    }
    return res;
  }
  private static int compareDates (Date d1, Date d2) {
    if (d1 == null && d2 == null) return 0;
    if (d1 == null) return 1;
    if (d2 == null) return -1;
    return d1.compareTo(d2);
  }
}

class ColoredCellRenderer extends DefaultTableCellRenderer {
  
  public Component getTableCellRendererComponent(JTable table, Object value,
      boolean isSelected, boolean hasFocus, int row, int column) {
    
    Component cmp = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
    if (column>=3) {
      Component cmp2 = new JTextArea(value.toString());
    }
    if (!isSelected) {
      TodolistItem tli = (TodolistItem) ((TodolistItemTableModel)table.getModel()).getValueAt(row);
      if (!tli.isCompleted()) {
        switch (tli.getPriority()) {
          case TodolistItem.PRIORITY_LOW :
            cmp.setBackground(new Color(223, 255, 223));
            break;
          case TodolistItem.PRIORITY_MEDIUM :
            cmp.setBackground(new Color(255, 255, 223));
            break;
          case TodolistItem.PRIORITY_HIGH :
            cmp.setBackground(new Color(255, 223, 223));
            break;
          default :
            cmp.setBackground(Color.white);
            break;
        }
      } else {
        cmp.setBackground(new Color(223, 223, 223));
      }

      /*if (column==3 || column==4) {
        Date d1 = tli.getEstimatedStartDate();
        Date d2 = tli.getEstimatedEndDate();
        Date now = new Date();
        if (d1!=null && d2!=null && d1.compareTo(now)>=0 && d2.compareTo(now)<=0) {
          cmp.setFont(cmp.getFont().deriveFont(Font.BOLD));
          cmp.setBackground(Color.RED);
        }
      }
      if (column==5 || column==6) {
        Date d1 = tli.getStartDate();
        Date d2 = tli.getEndDate();
        Date now = new Date();
        if (d1!=null && d2!=null && d1.compareTo(now)>=0 && d2.compareTo(now)<=0)
          cmp.setFont(cmp.getFont().deriveFont(Font.BOLD));
      }*/
      
      return cmp;
    } else {
      return cmp;
    }
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.