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

📄 nodeitemlist.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.
*/

/*
 * NodeItemList.java - 19/11/05
 */
package issrg.utils.xml;

import java.util.*;
import javax.swing.event.*;
import org.w3c.dom.*;

/**
 * Abstract Class that defines the NodeItemList component. This is used 
 * by further components to implement a list which stores org.w3c Document
 * nodes. 
 * <p> 
 * It extends the functionality of the AddDelList, and works in parallel
 * with nodes. Therefore, the listbox will contain a string label of a node 
 * representation, and operations done on a current list selection will 
 * reflect that node.
 *
 * @author Christian Azzopardi
 */
public abstract class NodeItemList extends AddDelList implements XMLChangeListener, NodeSelectionListener
{
    Object nlistSemaphore = new Object();
    Node parentNode;
    Node selectedNode;
    Node oldSelection;
    public NodeList nlist;
    public XMLEditor xmlED;
    
    /**
     *  Constructor
     *
     *  @param xmlED         Sets the class XMLEditor 
     */
    public NodeItemList(XMLEditor xmlED) 
    {
        super();
        this.xmlED = xmlED;
    }
    
    /**
     *  Comparator for not using the natural order (the default)
     */
    private class StringCaseSensitiveComparator implements Comparator
    {
        public int compare(Object o1, Object o2)
        {
            return o1.toString().compareToIgnoreCase(o2.toString());
        }
    }
    
    /**
     *  Sort the data of the NodeList passed as a parameter alphabetically.
     *  Sorting the NodeList on it own will not sort the list that is displayed
     *  on the screen gui list. Thus an array with the relative pointers is 
     *  passed too.
     *
     *  @param nodelist    The NodeList to Sort Alphabetically
     *  @param labels      The String Values of the Nodelist to Sort Alphabetically
     *
     *  @return            A data Map with the sorted data.
     */
    public Map sortArgs(NodeList nodelist, String[] labels)
    {
        StringCaseSensitiveComparator comp = new StringCaseSensitiveComparator();
        Map newArgs = new TreeMap(comp);
        
        if ((nodelist != null) || (labels != null) && (nodelist.getLength() == labels.length))
        {
            for (int i = 0; i < nodelist.getLength(); i++)
            {
                newArgs.put(labels[i], nodelist.item(i));
                //maps cannot take duplicate keyssssss
            }
        }
        else return null;
        
        return newArgs;
    }
    
    /**
     *  Uses the synchronized command to set the current NodeList 
     *  keeping note of the current thread.
     *
     *  @param nodelist         NodeList to Set
     */
    public void setNodeList(NodeList nodelist)
    {
        synchronized(nlistSemaphore)
        {
            nlist = nodelist;   
        }
    }
   
    /**
     *  Sets the current NodeList with the corresponding List of labels 
     *  
     *  @param nodelist         NodeList to set
     *  @param labels           Array of Strings corresponding to the NodeList
     */
    public void setNodeList(NodeList nodelist, String[] labels)
    {
        NodeVector nlist = new NodeVector();
        Map sortedArgs = sortArgs(nodelist, labels);
        
        if ((labels != null) && (nodelist != null))
        {
            labels = (String[])sortedArgs.keySet().toArray(new String[0]);
            for(int i=0; i < labels.length; i++)
            {
                nlist.add(sortedArgs.get(labels[i]));
            }
        }
        else
        {
            nlist = null;
            labels = null;
        }
        
        setNodeList(nlist);
        setListData(labels);
        setSelectedNode(oldSelection);
        itemSelected();
    }
   
    /**
     *  Function that returns the NodeList of all descendant Elements 
     *  with a given tag name
     *
     *  @param tagname  A String Containing the Tag Name to Retrieve
     *
     *  @return         The NodeList of all descendant Elements of the tag name
     */
    public NodeList getNodeList(String tagname)
    {
        return xmlED.DOM.getElementsByTagName(tagname);
    }
    
    /**
     *  Remembers the Parent Node passed as a parameter and updates the List View
     *
     *  @param n        The node to Set as Parent Node
     */
    public void setParentNode(Node n)
    {
        this.parentNode = n;
        refreshView();
    }
    
    /**
     *  Returns the parentNode
     *
     *  @return        The current Parent Node is returned
     */
    public Node getParentNode()
    {
        return parentNode;
    }
    
    /**
     *  An abstract method that is called when the list view is to be refreshed.
     */
    public abstract void refreshView();
    
    /**
     *  Sets the Selected Node in the current NodeList. If the passed node is 
     *  not found the selectedNode is unchanged. Otherwise selectedNode is set to n.
     *  The position of the node in the NodeList is compared to the selected index of 
     *  the listbox. If they are not the same,the appropraite row in the listbox is
     *  selected.
     *
     *  @param n        Sets the selectedNode to n
     */
    public void setSelectedNode(Node n)
    {        
        this.selectedNode = null;
        
        synchronized(nlistSemaphore){
        if (n!=null && nlist!=null)
        {
            for (int i = 0; i < nlist.getLength(); i++)
            {
                if(n == nlist.item(i))
                {
                    this.selectedNode = n;
                    if (i != listBox.getSelectedIndex())
                    {
                        listBox.setSelectedIndex(i);
                    }
                    break;
                }       
            }
        }
        
        if (selectedNode==null)
        {
            listBox.setSelectedIndex(-1);
        }
        
        if (selectedNode!=null) oldSelection = selectedNode; // remember non null selections - this is used when restoring selected nodes
        }
    }
    
    /**
     *  Returns the selectedNode
     *
     *  @return         The node currently set selected.
     */    
    public Node getSelectedNode()
    {
        return selectedNode;
    }
        
    /**
     *  The method checks that the nodeList is not null and sets the selectedNode 
     *  value to the node corresponding to the selected item in the list. If no 
     *  item is selected in the list, the selectedNode is set to null. 
     *  After this a NodeSelectionChanged event is fired.
     */
    public void itemSelected()
    {
        if (nlist!=null && getSelectedIndex() >= 0)
        {
           setSelectedNode(nlist.item(getSelectedIndex()));
        }
        else 
        {
           setSelectedNode(null);
        }
        NodeSelectionEvent nse = new NodeSelectionEvent(xmlED, this, getSelectedNode()); 
        FireNodeSelectionChanged(nse);
    }
    
    EventListenerList NodeChangeListeners = new EventListenerList();

    /**
     * Adds a NodeChangeListener to the component
     */
    public void addNodeChangeListener(NodeSelectionListener listener)
    {
       NodeChangeListeners.add(NodeSelectionListener.class, listener);
    }

    /**
     * Removes the NodeChangeListener from the component
     */
    public void removeNodeChangeListener(NodeSelectionListener listener)
    {
       NodeChangeListeners.remove(NodeSelectionListener.class, listener);
    }
    
    /**
     * When the NodeSelection is Changed, this method will loop through
     * the registered NodeSelectionListeners and will fire each listeners
     * own method, NodeSelectionChanged with the current event as a parameter.
     */
    public void FireNodeSelectionChanged(NodeSelectionEvent ev)
    {
       Object[] listeners = NodeChangeListeners.getListenerList(); 
       // loop through each listener and pass on the event if needed
       for (int i = 0; i < listeners.length ; i++)
       {
          if (listeners[i] == NodeSelectionListener.class)
          {
             // pass the event to the listeners event dispatch method
             ((NodeSelectionListener)listeners[i+1]).NodeSelectionChanged(ev);
          }            
       }
    }   
    
    public void NodeSelectionChanged(NodeSelectionEvent ev)
    {
        setParentNode(ev.getSelectedNode());
    }
    
    EventListenerList ItemAddedListeners = new EventListenerList();

    public void addItemAddedListener(ItemAddedListener listener)
    {
       ItemAddedListeners.add(ItemAddedListener.class, listener);
    }

    public void removeItemAddedListener(ItemAddedListener listener)
    {
       ItemAddedListeners.remove(ItemAddedListener.class, listener);
    }
    
    public void FireItemAddedEvent(ItemAddedEvent ev)
    {
       Object[] listeners = ItemAddedListeners.getListenerList(); 
       // loop through each listener and pass on the event if needed
       for (int i = 0; i < listeners.length ; i++)
       {
          if (listeners[i] == ItemAddedListener.class)
          {
             // pass the event to the listeners event dispatch method
             ((ItemAddedListener)listeners[i+1]).ItemAddedChanged(ev);
          }            
       }
    }   
    
    public void ItemAddedChanged(ItemAddedEvent ev)
    {  
    }
    
   /**
    * When an XMLChangeEvent is received, instruction is given to refresh the
    * component.
    */
    public void XMLChanged(XMLChangeEvent ev)
    {
        refreshView();
    }
}

⌨️ 快捷键说明

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