⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jcheckboxlist.java

📁 用Java开发的、实现类似Visio功能的应用程序源码
💻 JAVA
字号:
/**
 *    $Id:JCheckBoxList.java $
 *
 *    Copyright 2004 ~ 2005  JingFei International Cooperation LTD. All rights reserved. *
 */
package com.jfimagine.jfdraw.gui.dialog;


import javax.swing.JList;
import javax.swing.JCheckBox;           
import javax.swing.ListCellRenderer;
import javax.swing.ListSelectionModel;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import javax.swing.UIManager;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

import java.awt.Component;   
import java.awt.BorderLayout;
import java.awt.event.KeyEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;

 
import com.jfimagine.jfgraph.shape.union.CheckItem;
import com.jfimagine.jfdraw.gui.resource.CADResource;
import com.jfimagine.jfdraw.gui.GUIConst;
import com.jfimagine.jfdraw.gui.GUIUtils;

public class JCheckBoxList extends JList
{
    protected static Border noFocusBorder = new EmptyBorder(1, 1, 1, 1);
	
    /** A checkitem list */	    
    private List m_checkItemList	=new ArrayList();


    /** Get the item count */
    public int getItemCount(){
	return  m_checkItemList.size();   	
    }	

    /** Get the index of a first checked item*/
    public int getFirstCheckedIndex(){
    	Iterator it	=m_checkItemList.iterator();
    	int cnt=0;
    	while (it!=null && it.hasNext()){
    		cnt++;
    		CheckItem checkItem	=(CheckItem)it.next();
    		if (checkItem.getChecked()){
    			return cnt-1;
    		}
	}
	return -1;
    }	


    /** Get the checkitem list */
    public List getCheckItemList(){
	return  m_checkItemList;   	
    }	

    /**
     *  Set new check items for this checkbox list.
     *  @param items  a check item list
     */	
    public void setCheckItemList(List items){ 
    	//copy list items
  	m_checkItemList	=new ArrayList(items);
  	showCheckItemList();
   }
    

    /**
     *  Show check item list.
     */	
    private void showCheckItemList(){ 

	int size	=m_checkItemList.size();  
        Object[] cbArray = new Object[size];
        for (int i=0; i<size; i++){
        	try{
        		CheckItem checkItem	=(CheckItem)m_checkItemList.get(i);
        		JCheckBox cb		=new JCheckBox(checkItem.getLabel());
        		cb.setSelected(checkItem.getChecked());
        		cbArray[i] 	=cb;
        	}catch(Exception e){
        	}
        }
        
        //set this checkbox list's items.
        setListData(cbArray);
        
        if (m_checkItemList.size()>0)
		setSelectedIndex(0);        
        repaint();
    }
    	
    /** A removed checkitem list */	    
    private List m_removedCheckItemList	=new ArrayList();
    /** Get the removed checkitem list */
    public List getRemovedCheckItemList(){
	return  m_removedCheckItemList;   	
    }	

    /**
     *  Get a new name of list item.
     */	
    public String getNewItemCaption(){
    	int itemCnt	=m_checkItemList.size() + m_removedCheckItemList.size();
    	while (true){
    		itemCnt++;      
    		String item	=com.jfimagine.jfdraw.gui.resource.CADResource.getString("label.layer.title")+itemCnt;
    		if (!itemExists(item))
    			return item;
    	}
    }

    /**
     *  Test if this item caption exist in the list.
     *  @param item An item to be tested.
     */	
    public boolean itemExists(String item){
    	if (item==null || item.equals(""))
    		return false;
	
	CheckItem checkItem	=getItem(item);    	
	return (checkItem!=null && checkItem.getState()!=CheckItem.STATE_REMOVED);
    }

    /**
     *  Get a new item id
     */	
    private int newItemId(){
    	int id=0;
    	CheckItem checkItem;
    	Iterator it;
    	
    	it	=m_checkItemList.iterator();
    	while (it!=null && it.hasNext()){
    		checkItem	=(CheckItem)it.next();
    		id		=(int)Math.max(id,checkItem.getId());
	}
	
    	it	=m_removedCheckItemList.iterator();
    	while (it!=null && it.hasNext()){
    		checkItem	=(CheckItem)it.next();
    		id		=(int)Math.max(id,checkItem.getId());
	}
	
	return ++id;    	
    }

    /**
     *  Add a new list item.
     *  @param item A new item
     *  @param checked If this item is checked.
     */	
    public boolean add(String item, boolean checked){
    	if (itemExists(item))
    		return false;
    		
    	CheckItem	checkItem=new CheckItem();
	checkItem.setId(newItemId());		    	
	checkItem.setLabel(item);
	checkItem.setChecked(checked);   
	checkItem.setState(CheckItem.STATE_NEW);
	
	m_checkItemList.add(0,checkItem);
	showCheckItemList();
	
	return true;
    }


    /**
     *  Get an item by label
     */	
    private CheckItem getItem(String l){
    	l	=l.toLowerCase();
    	CheckItem checkItem;
    	String newLabel;
    	Iterator it;
    	
    	it	=m_checkItemList.iterator();
    	while (it!=null && it.hasNext()){
    		checkItem	=(CheckItem)it.next();
    		newLabel	=checkItem.getLabel().toLowerCase();
		if (newLabel.equals(l))
			return checkItem;    		
	}
	
    	it	=m_removedCheckItemList.iterator();
    	while (it!=null && it.hasNext()){
    		checkItem	=(CheckItem)it.next();
    		newLabel	=checkItem.getLabel().toLowerCase();
		if (newLabel.equals(l))
			return checkItem;
	}
	
	return null;
    }



    /**
     *  Remove selected item.
     */	
    public void removeSelected(){
    	int index	=getSelectedIndex();
    	if (index<0)
    		return;

	readItems();
    	
    	try{
    		CheckItem	checkItem=(CheckItem)m_checkItemList.get(index);
    		m_checkItemList.remove(index);
    		checkItem.setState(CheckItem.STATE_REMOVED);
    		m_removedCheckItemList.add(checkItem);
    		showCheckItemList();
    		
    		if (index>0)
    			setSelectedIndex(index-1);
    		
    		else if (m_checkItemList.size()>0)
    			setSelectedIndex(0);

	}catch(Exception e){
	}
    }


    /**
     *  Modify selected item.
     */	
    public void modifySelected(){
    	int index	=getSelectedIndex();
    	if (index<0)
    		return;

	readItems();
    	
    	try{
                JFrame	f	=GUIUtils.getJFrame(this);
    		CheckItem	checkItem=(CheckItem)m_checkItemList.get(index);
    		if (InputBoxDialog.inputText(f,checkItem.getLabel())){
    			checkItem.setLabel(InputBoxDialog.getString());
    			if (checkItem.getState()!=CheckItem.STATE_NEW)
    				checkItem.setState(CheckItem.STATE_UPDATE);
    			showCheckItemList();  
    		}
		
		setSelectedIndex(index);
		
	}catch(Exception e){
	}
    }


    /**
     *  add new item for this checkbox list
     */	
    public void addNewItem(String itemHead){
    	try{
		readItems();

                JFrame	f	=GUIUtils.getJFrame(this);
                if (itemHead==null) itemHead="";
                String item	=itemHead+newItemId();
    		if (InputBoxDialog.inputText(f,item)){
    			item	=InputBoxDialog.getString();
			if (itemExists(item)){               
				JOptionPane.showMessageDialog(null, CADResource.getString("dialog.layerSetup.itemExsists"), CADResource.getString("sys.warn"), JOptionPane.ERROR_MESSAGE); 
			}else{	
    				add(item,true);
    				showCheckItemList();
			}

			if (m_checkItemList.size()>0)
			   setSelectedIndex(0);
    		}

	}catch(Exception e){
	}
    }



    /**
     *  Move up selected item.
     */	
    public void moveUp(){
    	int index	=getSelectedIndex();
    	if (index<=0) //it's unnecessary to move up the first item in list.
    		return;

	readItems();
    	
    	try{
    		CheckItem	checkItem=(CheckItem)m_checkItemList.get(index);
    		CheckItem	checkItem1=(CheckItem)m_checkItemList.get(index-1);
    		
    		m_checkItemList.set(index,checkItem1);
    		m_checkItemList.set(index-1,checkItem);
			    			
    		showCheckItemList();

		if (index>0)
		   setSelectedIndex(index-1); 
		else
		   setSelectedIndex(index); 
		
	}catch(Exception e){
	}
    }


    /**
     *  Move down selected item.
     */	
    public void moveDown(){
    	int index	=getSelectedIndex();
    	if (index<0 || index==m_checkItemList.size()-1) //it's unnecessary to move down the last item in list.
    		return;

	readItems();
	
    	try{
    		CheckItem	checkItem=(CheckItem)m_checkItemList.get(index);
    		CheckItem	checkItem1=(CheckItem)m_checkItemList.get(index+1);
    		
    		m_checkItemList.set(index,checkItem1);
    		m_checkItemList.set(index+1,checkItem);
			    			
    		showCheckItemList(); 

		if (index<m_checkItemList.size()-1)
		   setSelectedIndex(index+1); 
		else
		   setSelectedIndex(index); 
    		
	}catch(Exception e){
	}
    }


    //Read checked state from items	
    public void readItems(){
    	// Get number of items in the list
    	int size = getModel().getSize(); // 4
    
    	// Get all item objects
    	for (int i=0; i<size; i++) {
    		
    		//get checkbox from GUI
        	Object item = getModel().getElementAt(i);
        	JCheckBox box	=(JCheckBox)item;
        	
        	//set check item by checkbox
        	try{
        		CheckItem  checkItem	=(CheckItem)m_checkItemList.get(i);
        		checkItem.setChecked(box.isSelected());
        	}catch(Exception e){
        	}
    	}
    	
    }	    

    /**
     *  Test if any item has been checked.
     */	
    public boolean hasItemChecked(){
    	CheckItem checkItem;
    	Iterator it;
    	
    	it	=m_checkItemList.iterator();
    	while (it!=null && it.hasNext()){
    		checkItem	=(CheckItem)it.next();
    		if (checkItem.getChecked())
    			return true;
	}
	
	return false;
    }
    
    
    /**
     *  Constructor of JCheckBoxList.
     */	
    public JCheckBoxList()
    {
        setCellRenderer(new CheckBoxCellRenderer());

        addMouseListener(new MouseAdapter()
        {
            public void mousePressed(MouseEvent e)
            {
                int index = locationToIndex(e.getPoint());
                if (index != -1)
                {
                    JCheckBox checkbox = (JCheckBox) getModel().getElementAt(index);
                    checkbox.setSelected(!checkbox.isSelected());
                    repaint();
                }
            }
        });

        addKeyListener(new KeyAdapter()
        {
            public void keyPressed(KeyEvent e)
            {
                if (e.getKeyCode() == KeyEvent.VK_SPACE)
                {
                    int index = getSelectedIndex();
                    if (index != -1)
                    {
                        JCheckBox checkbox = (JCheckBox) getModel().getElementAt(index);
                        checkbox.setSelected(!checkbox.isSelected());
                        repaint();
                    }
                }
            }
        });

        setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    }


    /**
     *  A new cell renderer to drawing a checkbox cell of list.
     */	
    protected class CheckBoxCellRenderer implements ListCellRenderer
    {
        public Component getListCellRendererComponent(JList list, Object value, int index,
                                                      boolean isSelected, boolean cellHasFocus)
        {
            JCheckBox checkbox = (JCheckBox) value;
            checkbox.setBackground(isSelected ? getSelectionBackground() : getBackground());
            checkbox.setForeground(isSelected ? getSelectionForeground() : getForeground());

            checkbox.setEnabled(isEnabled());
            checkbox.setFont(getFont());
            checkbox.setFocusPainted(false);

            checkbox.setBorderPainted(true);
            checkbox.setBorder(isSelected ? UIManager.getBorder("List.focusCellHighlightBorder") : noFocusBorder);

            return checkbox;
        }
    }


    
    /*
    public static void main(String args[])
    {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(new BorderLayout());

        JCheckBoxList cbList = new JCheckBoxList();

        Object[] cbArray = new Object[3];
        cbArray[0] = new JCheckBox("one");
        cbArray[1] = new JCheckBox("two");
        cbArray[2] = new JCheckBox("three");

        cbList.setListData(cbArray);

        frame.getContentPane().add(cbList);
        frame.pack();
        frame.setVisible(true);
    } 
    */
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -