Package gri.tasks.gui

Source Code of gri.tasks.gui.GroupedTaskWidget

/*
* File: GroupedTaskWidget.java
* Author: Daniel Rogers
* Created on Mar 14, 2008
*
*/
package gri.tasks.gui;

import gri.gui.widgets.options.OptionListWidget;
import gri.gui.widgets.options.Option;
import gri.tasks.gui.util.BasicTaskGrouper;
import gri.tasks.gui.util.TaskGrouper;

import java.awt.*;
import javax.swing.*;

import gri.tasks.managers.TaskEntry;

import dan.swing.PrettyListCellRenderer;
import dan.swing.JPrettyLabel;
import dan.swing.painters.GradientPainter;

/**
* Widget to display TaskEntry's in a JList.  The task name is displayed
* for each entry, and the entries are divided into groups based on
* either their group name or package name.  Each group has a header
* that will be colored differently from the entries.
*
* @author dan.rogers
*/
public class GroupedTaskWidget extends OptionListWidget {

  TaskGrouper taskGrouper;

  // ------------------------------------------------- Constructors

  public GroupedTaskWidget() {
    super();

    taskGrouper = new BasicTaskGrouper();
    super.lstValues.setCellRenderer(new CustomListCellRenderer());
  }

  // ----------------------------------------------- Implementation


  public void addOption(Option option) {
    Object value = option.getValue();
    if (value instanceof TaskEntry) {
      TaskEntry entry = (TaskEntry)value;
      String groupName = taskGrouper.getTaskGroup(entry.getTaskDescription());

      GroupElement groupElem = new GroupElement(groupName);
      int index = super.lstValuesModel.indexOf(groupElem);
      if (index < 0) {
        lstValuesModel.addElement(groupElem);
        lstValuesModel.addElement(new OptionListItem(option));
      }
      else {
        lstValuesModel.insertElementAt(new OptionListItem(option), index+1);
      }

    }
    else
      throw new RuntimeException("This widget only allows TaskEntry values to be given as options");

  }

  // ------------------------------------------------------------- Inner Classes

  /**
   * Element in the list to present a group name
   */
  class GroupElement {
    String name;

    public GroupElement(String name) {
      this.name = name;
    }

    public String toString()            {return name;}

    public boolean equals(Object o) {
      if (o == null)
        return this == null;

      if (o instanceof GroupElement)
        return ((GroupElement)o).name.equals(this.name);
      return false;
    }
    public int hashCode() {
      return name.hashCode();
    }
  }

  class CustomListCellRenderer extends PrettyListCellRenderer {

    GradientPainter groupPainter;
    GradientPainter selectedPainter;

    public CustomListCellRenderer() {
      groupPainter = new GradientPainter();

      selectedPainter = new GradientPainter();
      selectedPainter.setDirection(GradientPainter.TOP_RIGHT_DIAGONAL);
    }

    public Component getListCellRendererComponent(JList list,
        Object value,
        int index,
        boolean isSelected,
        boolean cellHasFocus) {

      JPrettyLabel label = (JPrettyLabel)super.getListCellRendererComponent(
          list, value, index, isSelected, cellHasFocus);

      //pad five pixels on left and right:
      label.setBorder(BorderFactory.createEmptyBorder(0,5,0,5));

      label.setOpaque(false);
      if (value instanceof GroupElement) {
        label.setBorder(BorderFactory.createLineBorder(Color.GRAY));
        label.setBackgroundPainter(groupPainter);
        label.setFont(label.getFont().deriveFont(Font.BOLD));
      }

      if (isSelected) {
        label.setBackgroundPainter(selectedPainter);
        label.setForeground(Color.WHITE);
        label.setFont(label.getFont().deriveFont(Font.ITALIC));
      }

      return label;
    }
  }

}
TOP

Related Classes of gri.tasks.gui.GroupedTaskWidget

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.