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

📄 smartpopuptabletool.java

📁 JAVA开源LDAP浏览器jxplorer的源码!
💻 JAVA
字号:
package com.ca.directory.jxplorer.viewer.tableviewer;

import com.ca.commons.cbutil.*;
import com.ca.commons.naming.DN;
import com.ca.commons.naming.RDN;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.logging.Logger;
import java.util.logging.Level;

    /**
     *    This is the small popup menu that appears when a manager right-clicks
     *    (or system-dependant-whatever-s) on the attribute editing table, allowing them
     *    to cut/copy/paste/delete/rename tree elements
     */
     
public class SmartPopupTableTool extends JPopupMenu 
    implements ActionListener
{

    JMenuItem delete, newValue, makeNaming, removeNaming;  // displayable menu options for user input

    JTable table;                      // the table displaying the data - NOT CURRENTLY USED

    AttributeTableModel model;         // the data model - used to insert values into

    String attributeName = null;       // the currently selected attribute class name 
    
    int currentRow;                    // the currently selected row.

    AttributeValue currentValue;       // type of currently selected table row
    
    AttributeType currentType;         // value of currently selected table row
        
    DN currentDN = null;               // used by the cache system?
    //RDN currentRDN = null;             // used for naming attribute magic.

    AttributeValueCellEditor cellEditor = null;     //TE: to stop cell editing.

        private static Logger log = Logger.getLogger(SmartPopupTableTool.class.getName());

    /**
     *    Constructor initialises the drop down menu and menu items, 
     *    and registers 'this'
     *    component as being the listener for all the menu items.
     */

    public SmartPopupTableTool(JTable t, AttributeTableModel m)
    {
        table = t;
        model = m;
        add(newValue = new JMenuItem(CBIntText.get("Add Another Value")));
        add(delete = new JMenuItem(CBIntText.get("Delete Value")));
        add(makeNaming = new JMenuItem(CBIntText.get("Make Naming Value")));
        add(removeNaming = new JMenuItem(CBIntText.get("Remove Naming Value")));
        
        removeNaming.setVisible(false);
        
        newValue.addActionListener(this);
        delete.addActionListener(this);
        makeNaming.addActionListener(this);
        removeNaming.addActionListener(this);
        
        setVisible(false);
    }

    /**
     *    Set the name of the attribute being operated with.
     *    That is, for new Value creation.
     */
     
    public void registerCurrentRow(AttributeType type, AttributeValue value, int row, RDN currentRDN)
    {
        currentType = type;
        currentValue = value;
        currentRow = row;
        
        if (currentType.toString().equalsIgnoreCase("objectclass"))
        {
            newValue.setEnabled(false);        
            delete.setEnabled(false);
        }
        else
        {
            newValue.setEnabled(true);        
            delete.setEnabled(true);
        }
        
        if (value.isNaming())
        {
            
            if (currentRDN != null)  // which it never should
            {
                if (currentRDN.size() > 1)
                    removeNaming.setVisible(true);
                else
                    removeNaming.setVisible(false);
            }
            makeNaming.setVisible(false);
        }
        else
        {
            if (currentRDN != null)
            {
                if (currentRDN.toString().indexOf(type.toString() + "=") > -1) // i.e. if we already have a naming att of this type...
                    makeNaming.setVisible(false);   // don't let the user add another one.
                else if (currentType.isMandatory())     
                    makeNaming.setVisible(true);
                else               
                    makeNaming.setVisible(false);
            }
            removeNaming.setVisible(false);
        }
        
    }

    /**
     *  This handles the menu item actions.  They rely on   
     *  the attributeName String being set prior to this method being called
     *  (usually by setAttributeName() above).  Most of the action
     *  handling is simply tossing arguments to JTable,
     *
     *  @param ev the active event, i.e. the menu item selected
     */
     
    public void actionPerformed(ActionEvent ev)
    { 
        setVisible(false);
    
        Object eventSource = ev.getSource();
        if (eventSource == newValue)
        {
            cellEditor.stopCellEditing();   //TE: bug fix 3107
            newValue();
        }    
        else if (eventSource == delete)
        {
            delete();
        }
        else if (eventSource == removeNaming)
        {
            removeRDNComponent();
        }
        else if (eventSource == makeNaming)
        {
            addRDNComponent();
        }
        else  // should never happen...
            log.log(Level.WARNING, "Unknown event in popup menu:\n", ev);
        
        repaint();            
    }    


    public void newValue()
    {
        int type = currentType.isMandatory()?AttributeType.MANDATORY:AttributeType.NORMAL;
        String attName = currentType.getValue();
        AttributeValue newVal; 
        if (currentValue.isBinary())
        {
            newVal = new AttributeValue(attName, null);
            newVal.setBinary(true);
        }    
        else            
            newVal = new AttributeValue(attName, "");

        model.addAttribute(attName, newVal, type, currentRow+1);
        model.fireChange();
    }

    public void delete()
    {
        model.deleteAttribute(currentType.getValue(), currentRow);
        if (currentValue.isBinary())
            currentValue.setValue(null);
        model.fireChange();
        
        if((currentType.getValue()).equalsIgnoreCase("jpegPhoto"))    //TE: deletes the temporary files associated with the current entry.
            CBCache.cleanCache(currentDN.toString());
    }
    

    public void removeRDNComponent()
    {
        if (model.getRDNSize() == 1)
            CBUtility.error(CBIntText.get("Cannot remove the last naming component!"));
        else
            model.removeNamingComponent(currentType, currentValue);
    }
    
    public void addRDNComponent()
    {
        if (currentValue.isBinary())
            CBUtility.error(CBIntText.get("Binary naming components are not supported."));    
        else if (currentValue.isEmpty())
            CBUtility.error(CBIntText.get("A Naming Component must have an actual value."));
        else
            model.addNamingComponent(currentType, currentValue);            
    }

    
    public void setDN(DN dn)
    {
        currentDN = dn;
        //currentRDN = dn.getLowestRDN();
    }


    /**
     * registers the cell editor.  TE: for bug fix 3107.
     * @param myEditor the cell editor.
     */

    public void registerCellEditor(AttributeValueCellEditor myEditor)
    {
        cellEditor = myEditor;
    }
}

⌨️ 快捷键说明

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