Package nixonftp.list

Source Code of nixonftp.list.NXList$ListSelectionHandler

/* nixonFTP
* FTP client version 0.1
* Copyright (C) 2010 NIXON Development Corporation.
* All rights reserved.
* http://members.shaw.ca/nixon.com
*/
package nixonftp.list;

import java.awt.Color;
import java.awt.Rectangle;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import nixonftp.ui.NXMenu;
import nixonftp.*;
import java.util.Date;
import javax.swing.AbstractListModel;
//import javax.swing.DropMode;
import javax.swing.BorderFactory;
import javax.swing.JList;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.ListModel;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import nixonftp.ui.NXContextMenu;

public class NXList extends JList implements NXListAdapter, FocusListener {
  private String dir = "";
  private final int index;
  private Object[] draggedValues;
  private NXContextMenu menu;
  private boolean local;
  private NXFilter[] filters;
  private boolean leftSide;
  private Color defaultBackground;

  public NXList(int index, boolean local) {
    NXDefaultListModel listModel = new NXDefaultListModel();
    setModel(listModel);
    setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
    this.index = index;
    this.local = local;
    this.menu = new NXContextMenu(local);
    setDragEnabled(true);
    addFocusListener(this);
    defaultBackground = Color.WHITE;
    //setDropMode(DropMode.ON);
  }

  public JPopupMenu getMenu() {
    return menu;
  }

  public NXFilter[] getFilters() {
    return filters;
  }

  public void setFilters(NXFilter[] filters) {
    this.filters = filters;
  }

  public NXObjectIndex[] getSelectedObjects() {
    Object[] obj = this.getSelectedValues();
    NXObjectIndex[] idx = new NXObjectIndex[obj.length];
    for (int x = 0; x < obj.length; x++) {
      idx[x] = (NXObjectIndex) obj[x];
    }
    return idx;
  }

  public NXFilter[] filter(NXFilter[] filters, NXObjectIndex oi) {
    NXFilter[] returnFilters = new NXFilter[filters.length];
    for (int x = 0; x < filters.length; x++) {
      boolean match = false;
      NXFilter filter = (NXFilter) filters[x];
      long value = filter.value * filter.getUnitValue();
      if (filter.type == NXFilter.DATE) {
        long secs = oi.date.getTime() / 1000;
        long before = ((new Date()).getTime() / 1000) - value;
        if (filter.range == NXFilter.WITHIN) {
          match = secs >= before;
        } else {
          match = secs < before;
        }
      } else if (filter.type == NXFilter.SIZE) {
        if (filter.range == NXFilter.LESS_THAN) {
          match = oi.size < value;
        } else if (filter.range == NXFilter.EQUAL_TO) {
          match = oi.size == value;
        } else if (filter.range == NXFilter.GREATER_THAN) {
          match = oi.size > value;
        }
      }
      if (match) {
        returnFilters[x] = filters[x];
      }
      //setVisible(show);
      //setVisible(filter.action == NXFilter.SHOW && match);
    }
    return returnFilters;
  }

  public int getSelectedItemCount() {
    return getSelectedIndices().length;
  }

  public NXObjectIndex getSelectedValue() {
    return (NXObjectIndex) super.getSelectedValue();
  }

  public String getDirectory() {
    return dir;
  }

  public ListModel getAdapterModel() {
    return getModel();
  }

  public void setDirectory(String s) {
    dir = s;
  }

  public boolean getType() {
    return false; //List, not table
  }

  public int getIndex() {
    return index;
  }

  public boolean getLocal() {
    return local;
  }

  public void addElement(Object o) {
    doElement(o, null);
  }
 
  public void doElement(Object o, String s) {
    NXObjectIndex add = (NXObjectIndex) o;

    NXObjectIndex obj;
    NXListModel model;

    try {
      model = (NXListModel) getModel();
    } catch (java.lang.ClassCastException ex) {
      model = (NXListModel) getModel();
    }
   
    for (int x = 0; x < model.getSize(); x++) {
      obj = (NXObjectIndex) model.getElementAt(x);
     
      if (o != null) { //add element, do not add duplicate
         if (obj.name.equals(add.name)) return;
      } else {
         if (obj.name.equals(s)) {
            model.removeElementAt(x);
            return;
         }
      }
    }
   
    if (o != null) { //for adding
       model.addElement(o);
    }
  }

  public void removeElement(String s) {
     doElement(null, s);
  }

  public boolean dropLeft() {
    return leftSide;
  }

  public void setDropLeft(boolean b) {
    leftSide = b;
  }

  public void focusGained(FocusEvent e) {
    setBackground(new Color(223,243,255));
  }

  public void focusLost(FocusEvent e) {
    setBackground(defaultBackground);
  }

  public Rectangle getRealBounds() {
    return ((JScrollPane)this.getParent().getParent()).getBounds();
  }

  class ListSelectionHandler implements ListSelectionListener {

    public void valueChanged(ListSelectionEvent e) {
      ListSelectionModel lsm = (ListSelectionModel) e.getSource();

      int firstIndex = e.getFirstIndex();
      int lastIndex = e.getLastIndex();
      boolean isAdjusting = e.getValueIsAdjusting();
      System.out.println("Event for indexes " + firstIndex + " - " + lastIndex + "; isAdjusting is " + isAdjusting + "; selected indexes:");

      if (lsm.isSelectionEmpty()) {
        System.out.println(" <none>");
      } else {
        // Find out which indexes are selected.
        int minIndex = lsm.getMinSelectionIndex();
        int maxIndex = lsm.getMaxSelectionIndex();
        for (int i = minIndex; i <= maxIndex; i++) {
          if (lsm.isSelectedIndex(i)) {
            System.out.println(" " + i);
          }
        }
      }
      System.out.println("\r\n");
    }
  }
}
TOP

Related Classes of nixonftp.list.NXList$ListSelectionHandler

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.