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

📄 attributevaluecelleditor.java

📁 JAVA开源LDAP浏览器jxplorer的源码!
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package com.ca.directory.jxplorer.viewer.tableviewer;

import com.ca.commons.cbutil.CBIntText;
import com.ca.commons.cbutil.CBJComboBox;
import com.ca.commons.cbutil.CBUtility;
import com.ca.commons.jndi.SchemaOps;
import com.ca.commons.naming.DN;
import com.ca.commons.security.cert.CertViewer;
import com.ca.directory.jxplorer.DataSource;
import com.ca.directory.jxplorer.editor.*;

import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.lang.reflect.Constructor;
import java.security.cert.X509Certificate;
import java.util.Arrays;
import java.util.EventObject;
import java.util.logging.Logger;
import java.util.logging.Level;

/**
 *     The cell editor that brings up the dialog.
 *     We inherit from AbstractCellEditor,
 *     even though it means we have to create a dummy
 *     check box.  Attribute Value Editor uses schema
 *     info to validate the user's input before submission...
 */
public class AttributeValueCellEditor extends AbstractCellEditor
{
    Frame owner;

    JTextField textField = new JTextField();
    JLabel label = new JLabel("");

    CBJComboBox combobox = new CBJComboBox();

    JComponent editorComponent = textField;

    abstractbinaryeditor abstractEditor = null;    // this is the display editor for binary data - e.g. the audio player, or photo viewer

    Object value;
    boolean binaryEditFlag = false;            // handle binary editing separately
    boolean specialStringEditor = false;       // handle special string stuff like postal address
    protected ClassLoader myLoader = null;     // optional extended class loader
    public DataSource datasource = null;       //TE: The syntax of the attribute.
    public DN currentDN = null;                //TE: The dn of the entry being modified.

    int lastSelectedRow = 0;                    //TE: The last selected row - which is used to set the height back to normal (16).

    public static final String BINARY_SYNTAX =              "1.3.6.1.4.1.1466.115.121.1.5";
    public static final String BOOLEAN_SYNTAX =             "1.3.6.1.4.1.1466.115.121.1.7";
    public static final String CERTIFICATE_SYNTAX =         "1.3.6.1.4.1.1466.115.121.1.8";
    public static final String GENERALIZED_TIME_SYNTAX =    "1.3.6.1.4.1.1466.115.121.1.24";
    public static final String POSTAL_ADDRESS_SYNTAX =      "1.3.6.1.4.1.1466.115.121.1.41";

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

   /**
    *    A basic constructor, which does little except add a
    *    mouse listener to the default text field, setting the
    *    click count for 'cancel editing' to two.
    */
    public AttributeValueCellEditor(Frame parent)
    {
        owner = parent;

	//	textField.setFont(new Font("Tahoma", Font.PLAIN,11)); //TE: makes the textField same as the label - bug 3013.

		editorComponent.addMouseListener(new MouseAdapter()
        {
            public void mousePressed(MouseEvent e)
            {
//TE: I've commented this out because a double click makes the value in the cell disappear?? Bug - 3007.
//                if (e.getClickCount() == 2)
//                    cancelCellEditing();
            }
        });
    }

    //
    //  Implementing the CellEditor Interface
    //

    // implements javax.swing.table.TableCellEditor

    /**
     * Returns an awt.Component that acts as a cell editor.
     * Checks, in the following order, for a Certificate, binary syntax,
     * postalAddress, GeneralizedTime, if there are options.  If none of these
     * syntax' match then the default string editor is set.
     * This method also increases the size of the row selected so that the value
     * is easier to read.
     * @param table
     * @param value
     * @param isSelected
     * @param row
     * @param column
     * @return
     */
    public Component getTableCellEditorComponent(JTable table,
                         Object value, boolean isSelected,
                         int row, int column)
    {
        binaryEditFlag = false;
        specialStringEditor = false;

        table.setRowHeight(lastSelectedRow, 16);
        table.setRowHeight(row, 24);

        lastSelectedRow = row;

        if (value instanceof AttributeValue)
        {
            AttributeValue att = (AttributeValue) value;

            if (hasSyntax(att, CERTIFICATE_SYNTAX))			    //TE: a syntax check for Certificate.
                setCertificateEditor(att);
            else if (att.isBinary())						    //TE: binary check.
                setBinaryEditor(att);
            else if (hasSyntax(att, POSTAL_ADDRESS_SYNTAX))	    //TE: postalAddress syntax check.
                setPostalAddressEditor(att);
            else if (hasSyntax(att, GENERALIZED_TIME_SYNTAX))   //TE: generalizedTime syntax check.
                setGeneralizedTimeEditor(att);
            else if (hasSyntax(att, BOOLEAN_SYNTAX))	        //TE: boolean syntax check.
                setBooleanEditor(att);
            else if (att.hasOptions())      				    // there are suggested possible values
                setOptions(att);
            else
                setString(att);

            setCellEditorValue(att);
        }
        return editorComponent;
    }

   /**
    *	Checks if the attribute value's syntax matches the given syntax.
    *	@param att the attribute value for example, 'Fred' from 'cn=Fred'.
    *	@param syntax the syntax to check against for example, '1.3.6.1.4.1.1466.115.121.1.8'.
	*	@return true if the syntaxes match false otherwise.
	*/
	public boolean hasSyntax(AttributeValue att, String syntax)
	{
		String attSyntax = getAttributeSyntax(att);
	   	return (attSyntax != null && attSyntax.indexOf(syntax) > -1);
	}

   /**
    *   Sets the certificate editor in the table cell whose attribute is
    *   a certificate ["1.3.6.1.4.1.1466.115.121.1.8"].
    *   @param att the attribute value to be set in the editor.
    */
    private void setCertificateEditor(AttributeValue att)
    {
        CertViewer.CertAndFileName returnVal = CertViewer.editCertificate(owner, att.getValue());
        X509Certificate cert = returnVal.cert;
        if (cert != null)
        {
            try
            {
                byte[] newData = cert.getEncoded();
                if (Arrays.equals(newData, att.getValue()) == false)
                    att.setValue(newData);
            }
            catch(Exception e)
            {
                CBUtility.error(CBIntText.get("Error: unable to modify certificate."), e);
            }
        }

        binaryEditFlag = true;

        if (att.isEmpty())
        {
            label.setText(" ");
        }
        else
        {
            label.setText(CBIntText.get("(non string data)"));
        }

        editorComponent = label;
    }

   /**
    *   Sets the string in the table cell whose attribute is
    *   a string (does a check for the length of a string also and if
    *   the string is longer than 100 it sets the large string editor).
    *   @param att the attribute value to be set in the editor.
    */
    private void setString(AttributeValue att)
    {
        String textValue = att.toString();
        if (textValue.length() > 100) // arbitrary long display limit...
        {
            setLargeStringEditor(att);
        }
        else
        {
            textValue = textValue.trim();  // XXX trim off extra space that may be there for swing printing hack...

            textField.setText(textValue);

            editorComponent = textField;
        }
    }

   /**
    *   Sets the large string editor in the table cell whose attribute value is
    *   a string longer than 100 chars.
    *   @param att the attribute value to be set in the editor.
    */
    private void setLargeStringEditor(AttributeValue att)
    {
        largestringeditor lse = new largestringeditor(owner, att);
        specialStringEditor = true;
        CBUtility.center(lse, owner);
        lse.setVisible(true);
        label.setText(att.getStringValue().substring(0,100));
        editorComponent = label;
    }

   /**
    *   Sets a combo box in the table cell whose attribute could have
    *   suggested possible values.
    *   @param att the attribute value to be set in the editor.
    */
    private void setOptions(AttributeValue att)
    {
        combobox.removeAllItems();
        String[] ops = att.getOptions();
        for (int i=0; i<ops.length; i++)
            combobox.addItem(ops[i]);
        editorComponent = combobox;
    }

   /**
    *   Sets the binary editor in the table cell whose attribute is
    *   a binary.
    *   @param att the attribute value to be set in the editor.
    */
    private void setBinaryEditor(AttributeValue att)
    {
        startBinaryEditor(att); 					// runs modal dialog binary editor
        binaryEditFlag = true;

        if (att.isEmpty())
        {
            label.setText(" ");
        }
        else
        {
            label.setText(CBIntText.get("(non string data)"));
        }

        editorComponent = label;
    }

   /**
    *   Sets the generalized time editor in the table cell whose attribute is
    *   a generalizedTime ["1.3.6.1.4.1.1466.115.121.1.24"].
    *   @param att the attribute value to be set in the editor.
    */
    private void setGeneralizedTimeEditor(AttributeValue att)
    {
        generalizedtimeeditor timeEditor = null;

        if (att==null)
        {
            timeEditor = new generalizedtimeeditor(owner,"", true);
        }
        else
        {
            timeEditor = new generalizedtimeeditor(owner, att.toString(), true);
        }

        specialStringEditor = true;
        CBUtility.center(timeEditor, owner);    	//TE: centres the attribute editor.
        timeEditor.setStringValue(att);
        timeEditor.setVisible(true);

        if (att.isEmpty())
        {
            label.setText(" ");
        }
        else
        {
            label.setText(att.getStringValue());    //TE: sets the table label to reflect the changes.
        }

        editorComponent = label;
    }

   /**
    *   Sets the postal address editor in the table cell whose attribute is
    *   a postalAddress ["1.3.6.1.4.1.1466.115.121.1.41"].
    *   @param att the attribute value to be set in the editor.
    */
    private void setPostalAddressEditor(AttributeValue att)
    {
        postaladdresseditor postalEditor = new postaladdresseditor(owner, att);
        specialStringEditor = true;
        CBUtility.center(postalEditor, owner);    	//TE: centres the attribute editor.
        postalEditor.setStringValue(att);
        postalEditor.setVisible(true);

⌨️ 快捷键说明

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