Package net.coljac.pirates.gui

Source Code of net.coljac.pirates.gui.CrewPanel$MouseHandler

package net.coljac.pirates.gui;

import net.coljac.pirates.Card;
import net.coljac.pirates.Crew;
import net.coljac.pirates.Fleet;
import net.coljac.pirates.Ship;
import net.coljac.pirates.gui.helper.*;

import javax.swing.*;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.List;

/**
* By Colin Jacobs, colin@q9software.com
* Date: Mar 15, 2006
*/
public class CrewPanel extends CardPanel {

  public List<Crew> cards, allCards;

  private static String[] cols = new String[]{"O", "W", "Card", "Set", "Name", "Points", "Faction", "Rules", "Link", "Rarity"};

  private static Icon[] icons = new Icon[]{Icons.SYMBOL_HAVE_16, Icons.SYMBOL_WANT_16, null, null, null, null, null, null, null, Icons.SYMBOL_RARITY_16};

  private JPopupMenu menu;

  private static int[] widths = new int[]{16, 16, 30, 30, 150, 20, 50, 400, 150, 10};

  private JTable table;
  private CrewTableModel model;
  private FleetsPanel fleetsPanel;
  private CrewSorter sorter = new CrewSorter();
  private CardFilterPanel filterPanel = new CardFilterPanel(this);
  private JLabel statusBar = new JLabel("");
  private JMenuItem addToFleet;
  private JMenu addToShip;

  public CrewPanel() {
    this.setLayout(new BorderLayout());
    allCards = ManagerMain.instance.db.getCrew();
    cards = new ArrayList<Crew>();
    cards.addAll(allCards);

    statusBar.setText(cards.size() + " crew.");

    this.add(filterPanel, BorderLayout.NORTH);

    model = new CrewTableModel();
    table = new JTable(model);
    TableColumn column = null;
    for (int i = 0; i < cols.length; i++) {
      column = table.getColumnModel().getColumn(i);
      column.setPreferredWidth(widths[i]);

      column.setHeaderRenderer(new IconHeaderRenderer());
      if (icons[i] != null) {
        column.setHeaderValue(icons[i]);
      } else {
        column.setHeaderValue(cols[i]);
      }
      if (i == 6) {
        column.setCellRenderer(new FactionCellRenderer());
      } else if (i == 9) {
        column.setCellRenderer(new RarityRenderer());
      } else if (i == 7) {
        column.setCellRenderer(new ToolTipCellRenderer());
      }

    }

    menu = new JPopupMenu();
    addToFleet = new JMenuItem("Add to fleet");
    if (fleetsPanel == null) {
      addToFleet.setEnabled(false);
    }
    addToFleet.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        Crew c = cards.get(model.getLastClickedRow());
        Fleet f = fleetsPanel.getCurrentFleet();
        if (f != null) {
          f.addCrew(c);
          fleetsPanel.fleetChanged();
          statusBar.setText(c.getName() + " added to fleet.");
        }
      }
    });
    menu.add(addToFleet);
    addToShip = new JMenu("Add to ship");
    menu.add(addToShip);
    addToShip.add(new JMenuItem("No ships"));
    addToShip.setEnabled(false);

    table.addMouseListener(new IncrementMouseListener(model, this));
    // Delete key
    table.addKeyListener(new KeyAdapter() {
      public void keyReleased(KeyEvent e) {
        if(e.getKeyCode() == KeyEvent.VK_DELETE) {
          int first = cards.size();
          int last = 0;
          int[] selected = table.getSelectedRows();
          List<Card> toDelete = new ArrayList<Card>();
          for(int i=0; i<selected.length; i++) {
            toDelete.add(cards.get(selected[i]));
            if(i < first) first = i;
            if(i > last) last = i;
          }
          cards.removeAll(toDelete);
          allCards.removeAll(toDelete);
          ManagerMain.instance.allPanel.cards.removeAll(toDelete);
          ManagerMain.instance.allPanel.allCards.removeAll(toDelete);
          ManagerMain.instance.db.getCrew().removeAll(toDelete);

          new Thread() {
            public void run() {
              ManagerMain.instance.db.save();
            }
          }.start();
          model.fireTableRowsDeleted(first, last);
        }
      }
    });


    table.getTableHeader().addMouseListener(new MouseHandler());

    JScrollPane pane = new JScrollPane();
    pane.setViewportView(table);
    this.add(pane, BorderLayout.CENTER);
    this.add(statusBar, BorderLayout.SOUTH);
  }

  public Card getCardAtRow(int row) {
    return cards.get(row);
  }

  public void showMenu(Component comp, int x, int y) {
    addToShip.removeAll();
    addToFleet.setEnabled(fleetsPanel.getCurrentFleet() != null);
    addToShip.setEnabled(fleetsPanel.getCurrentFleet() != null);
    if (fleetsPanel.getCurrentFleet() != null) {
      List<Ship> ships = fleetsPanel.getCurrentFleet().getShips();

      for (Ship ship : ships) {
        final Ship craft = ship;
        final Crew man = cards.get(model.getLastClickedRow());
        JMenuItem item = new JMenuItem(ship.getName());
        item.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            fleetsPanel.getCurrentFleet().addCrewToShip(craft, man);
            fleetsPanel.fleetChanged();
            statusBar.setText(man.getName() + " added to fleet.");
          }
        });
        addToShip.add(item);
      }
    }
    menu.show(comp, x, y);
  }

  public FleetsPanel getFleetsPanel() {
    return fleetsPanel;
  }

  public void setFleetsPanel(FleetsPanel fleetsPanel) {
    this.fleetsPanel = fleetsPanel;
  }

  public void reFilter() {
    cards = (List<Crew>) filterPanel.getFilter().filter(allCards);
    sorter.sort(cards);
    model.fireTableDataChanged();
    statusBar.setText(cards.size() + " crew.");
  }

  // ----------------- TABLE MODEL ------------------
  // Wraps the list

  class CrewTableModel extends HandyTableModel {
    public int getRowCount() {
      return cards.size();
    }

    public int getColumnCount() {
      return cols.length;
    }

    public Object getValueAt(int rowIndex, int columnIndex) {
      Crew crew = cards.get(rowIndex);

      switch (columnIndex) {
        case 0:
          return crew.getOwned();
        case 1:
          return crew.getWanted();
        case 2:
          return crew.getNumber();
        case 3:
          return Card.getSetAbbreviation(crew.getExpansion());
        case 4:
          return crew.getName();
        case 5:
          return crew.getPoints();
        case 6:
          return crew.getFaction();
        case 7:
          return crew.getRules();
        case 8:
          return crew.getLink();
        case 9:
          return crew.getRarity();
        default:
          return null;
      }
    }

    public String getColumnName(int columnIndex) {
      return cols[columnIndex];
    }

    public boolean isCellEditable(int row, int col) {
//      if (col < 2) return true;
      return false;
    }

    public void setValueAt(Object value, int row, int col) {
    }
  }

  private class MouseHandler extends MouseAdapter {
    public void mouseClicked(MouseEvent e) {
      JTableHeader h = (JTableHeader) e.getSource();
      TableColumnModel columnModel = h.getColumnModel();
      int viewColumn = columnModel.getColumnIndexAtX(e.getX());
      int column = columnModel.getColumn(viewColumn).getModelIndex();
      if (column != -1) {
        sorter.setSort(column + 1);
        sorter.sort(cards);
        model.fireTableDataChanged();
      }
    }
  }


}
TOP

Related Classes of net.coljac.pirates.gui.CrewPanel$MouseHandler

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.