Package net.sourceforge.seriesdownloader.controller

Source Code of net.sourceforge.seriesdownloader.controller.GridTabController

package net.sourceforge.seriesdownloader.controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.List;

import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import net.sourceforge.seriesdownloader.controller.WatchListController.TableModel;
import net.sourceforge.seriesdownloader.model.Show;
import net.sourceforge.seriesdownloader.model.WatchList;
import net.sourceforge.seriesdownloader.model.WatchList.SortOrder;


public class GridTabController implements ActionListener, MouseListener {
  private JTable table;
  private int sortorder = -1;
  private WatchList.SortColumn column = null;
  private WatchList.SortOrder order;

  public static final void init(JTable table) {
    GridTabController listener = new GridTabController();
    listener.table = table;

    WatchListController.addActionListener(listener);
    table.getTableHeader().addMouseListener(listener);
    listener.refreshContents(WatchList.SortColumn.name, SortOrder.asc, true);
  }

  public void mouseClicked(MouseEvent e) {
    if (e.getButton() == MouseEvent.BUTTON1) {
      int index = table.getColumnModel().getColumnIndexAtX(e.getX());
      WatchList.SortColumn column = null;
      WatchList.SortOrder order;
      switch (index) {
        case 0:
          column = WatchList.SortColumn.name;
          break;
        case 1:
          column = WatchList.SortColumn.nextEpisodeName;
          break;
        case 2:
          column = WatchList.SortColumn.nextEpisodeDate;
          break;
        case 3:
          column = WatchList.SortColumn.episode;
          break;
      }
      if (index == sortorder) {
        order = SortOrder.desc;
        sortorder = -1;
      } else {
        order = SortOrder.asc;
        sortorder = index;
      }
      refreshContents(column, order, true);
    }
  }

  private void refreshContents(WatchList.SortColumn sortColumn, WatchList.SortOrder sortOrder, boolean clear) {
    if (sortColumn == null) {
      sortColumn = this.column;
      sortOrder = this.order;
    }
    this.column = sortColumn;
    this.order = sortOrder;

    List<Show> data = WatchListController.getShowsOrderedBy(sortColumn, sortOrder);

    if (clear) {
      DefaultTableModel model = (DefaultTableModel) table.getModel();
      while (model.getRowCount() > 0)
        model.removeRow(0);

      for (Show show : data)
        model.addRow(WatchListController.getRowData(show));
    } else {
      for (int i = 0; i < table.getRowCount(); i++) {
        Show show = (Show) table.getValueAt(i, 0);
        Object[] values = WatchListController.getRowData(show);
        for (int j = 1; j < values.length; j++)
          ((TableModel) table.getModel()).setValueAtClean(values[j], i, j);
      }
    }
  }

  public void actionPerformed(ActionEvent e) {
    if (e.getID() == WatchList.SHOW_INFO_UPDATED) {
      refreshContents(null, null, false);
      return;
    }

    refreshContents(null, null, true);
  }

  public void mouseEntered(MouseEvent arg0) {
  }

  public void mouseExited(MouseEvent arg0) {
  }

  public void mousePressed(MouseEvent e) {
  }

  public void mouseReleased(MouseEvent arg0) {
  }
}
TOP

Related Classes of net.sourceforge.seriesdownloader.controller.GridTabController

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.