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

📄 stringitemlist.java

📁 一个完整的XACML工程,学习XACML技术的好例子!
💻 JAVA
字号:
/*
* Copyright (c) 2006, University of Kent
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without 
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this 
* list of conditions and the following disclaimer.
* 
* Redistributions in binary form must reproduce the above copyright notice, 
* this list of conditions and the following disclaimer in the documentation 
* and/or other materials provided with the distribution. 
*
* 1. Neither the name of the University of Kent nor the names of its 
* contributors may be used to endorse or promote products derived from this 
* software without specific prior written permission. 
*
* 2. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS  
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
* PURPOSE ARE DISCLAIMED. 
*
* 3. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
* POSSIBILITY OF SUCH DAMAGE.
*
* 4. YOU AGREE THAT THE EXCLUSIONS IN PARAGRAPHS 2 AND 3 ABOVE ARE REASONABLE
* IN THE CIRCUMSTANCES.  IN PARTICULAR, YOU ACKNOWLEDGE (1) THAT THIS
* SOFTWARE HAS BEEN MADE AVAILABLE TO YOU FREE OF CHARGE, (2) THAT THIS
* SOFTWARE IS NOT "PRODUCT" QUALITY, BUT HAS BEEN PRODUCED BY A RESEARCH
* GROUP WHO DESIRE TO MAKE THIS SOFTWARE FREELY AVAILABLE TO PEOPLE WHO WISH
* TO USE IT, AND (3) THAT BECAUSE THIS SOFTWARE IS NOT OF "PRODUCT" QUALITY
* IT IS INEVITABLE THAT THERE WILL BE BUGS AND ERRORS, AND POSSIBLY MORE
* SERIOUS FAULTS, IN THIS SOFTWARE.
*
* 5. This license is governed, except to the extent that local laws
* necessarily apply, by the laws of England and Wales.
*/

/*
 * StringItemList.java 17/11/05
 */

package issrg.utils.xml;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ResourceBundle;

/**
 * The StringItemList is a class that extends AddDelList, and adds a text field 
 * for user input to the list, and label to describe what type of input is 
 * required.
 *
 * @author Christian Azzopardi
 */
public class StringItemList extends AddDelList implements KeyListener, ActionListener
{
    public JTextField tbField;
    protected JLabel tbLabel;
    public JPanel tb;
    
    ResourceBundle rbl = ResourceBundle.getBundle("issrg/editor2/PEComponent_i18n");
    String errorHeader = rbl.getString("ErrorHeader"); //contains Error!
    String strItemListError1 = rbl.getString("StrItemListError1");//contains Error! Item Already In List!
    
    public StringItemList() 
    {
        super();
    }
    
    /**
     * Creates a panel with the main components this object contains.
     *
     * @return  a JPanel with the GUI components that will be displayed when 
     *          this component is used.
     */
    public JPanel getContentPanel()
    {
        JPanel oldDisplay = super.getContentPanel();
                
        tb = new JPanel(new BorderLayout());
        tbLabel = new JLabel();
        tbField = new JTextField(15);
        tbField.addActionListener(this);
        tbField.addKeyListener(this);
        addButton.setEnabled(false);
        
        tb.add(tbLabel, BorderLayout.WEST);
        tb.add(tbField, BorderLayout.CENTER);
        
        constraints.insets = new Insets(3,0,0,0);
        addComponent(tb, oldDisplay, 4, 0, 3, 1, GridBagConstraints.CENTER, 1, 0, GridBagConstraints.BOTH);
        
        return oldDisplay;
    }
    
    /**
     * Invoked when the Add Button is clicked.
     * Adds the item in the Textfield to the List.
     */
    public void addItem()
    {
        if ((tbField.getText().equals("")) || tbField.getText().equals(null))
        {
            return;
        }
        
        if (isInList(tbField.getText()))
        {
            JOptionPane.showMessageDialog(this, strItemListError1, errorHeader, JOptionPane.ERROR_MESSAGE);
            return;
        }
        
        String [] oldData = getListData();        
        String newData[] = new String[oldData.length+1];
        
        System.arraycopy(oldData, 0, newData, 0, oldData.length);
                
        newData[oldData.length] = tbField.getText();
        setListData(newData);      
        tbField.setText("");
        addButton.setEnabled(false);
        
        listBox.ensureIndexIsVisible(getListData().length-1);                
    }
    
    public void replaceItem()
    {
        if ((tbField.getText().equals("")) || tbField.getText().equals(null))
        {
            return;
        }
        if (isInList(tbField.getText()))
        {
            JOptionPane.showMessageDialog(this, strItemListError1, errorHeader, JOptionPane.ERROR_MESSAGE);
            return;
        }
        addButton.setEnabled(false);
        if (getSelectedIndex() >= 0)    
        {
           String[] listData = getListData(); 
           listData[getSelectedIndex()] = tbField.getText();
           setListData(listData);
        }
        else addItem();
    }
    
    /**
     * Deletes the Selected Item in the List.
     */
    public void deleteItem()
    {
        if (getSelectedIndex() >= 0)    
        {
           String[] oldData = getListData(); 
           String newData[] = new String[oldData.length-1];
           
           int i = oldData.length;
           int j = getSelectedIndex();
           
           for (int k = 0; k < j; k++)
           {
               newData[k] = oldData[k];
           }
           for (int k = j+1; k < i; k++)
           {
               newData[k-1] = oldData[k];
           }    
           
           setListData(newData);
        }
    }
    
    public void itemSelected()
    {
        if (getSelectedIndex() >= 0)    
        {
           tbField.setText(getListData()[getSelectedIndex()]);
        }
        else tbField.setText("");
    }
    
   /**
    *  Sets the text the component will show.  
    *
    *  @param internalName         The internal name used in the program.     
    *  @param internationalName    The international name to set the text to.
    */    
    public void setCaption(String internalName, String internationalName)
    {        
        if (internalName.equals("TEXT_LABEL"))
        {
            tbLabel.setText(internationalName);
        }
        else super.setCaption(internalName, internationalName);
    }
    
    public JComponent removeTextBox()
    {
        this.remove(tb);
        return tb;
    }
    
    /**
     * Checks if the Item passed is in the List already.
     *
     * @param value   the String to check if in present in list.
     * @return        true - if the parameter is present in the list.
     */
    public boolean isInList(String value)
    {
        for (int i = 0; i < getListData().length ; i++)
        {
            String toCompare = ((String[])getListData())[i];
            if (toCompare.equals(value))
            {
                return true;
            }
        }                
        return false;
    }
    
    public void keyTyped(KeyEvent e)
    {
    }
    
    public void keyPressed(KeyEvent e)
    {
    }
    
    public void keyReleased(KeyEvent e)
    {      
        if (e.getSource() == tbField)
        {
            if (listBox.getSelectedValue() == null && 
                    !tbField.getText().intern().equals(""))
                    addButton.setEnabled(true);
            else if (!tbField.getText().intern().equals(""))
            {
                replaceButton.setEnabled(true);
                addButton.setEnabled(true);
            }
            else if (tbField.getText().intern().equals(""))
            {
                replaceButton.setEnabled(false);
                addButton.setEnabled(false);                
            }
        }
    }
    
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == tbField) 
        {
            if (tbField.getText().equals("")) 
            {
            }
            else
            {
                addItem();
                tbField.grabFocus();
            }
        }        
        else super.actionPerformed(e);
    }
}

⌨️ 快捷键说明

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