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

📄 attributetablemodel.java

📁 JAVA开源LDAP浏览器jxplorer的源码!
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
            if (((DXAttribute)att).hasOptions())
                newAV.setOptions(((DXAttribute)att).getOptions());
            newAV.setBinary(!((DXAttribute)att).isString());
        }
    }


    /**
     * Determines whether a particular attribute ID is part of an RDN Attribute Value Assertion (AVA),
     * and if so, returnes the attribute value (for visual tagging in the display later on).
     *
     * @param ID the attribute ID to be tested, e.g. "cn"
     * @return the value (if any) corresponding to this i.d. - eg. "smith" if the rdn was "cn=smith" and "cn" was passed in.
     */
    private String getAnyNamingValue(String ID)
    {
        for (int i=0; i<numberNamingValues; i++)
            if (ID.equalsIgnoreCase(namingTypes[i]))
                return namingRawValues[i];

        return null;  // no value found.
    }



    public void addAttribute(String ID, AttributeValue val, int type)
    {
        attributeTypes.add(new AttributeType(ID, (type==AttributeType.MANDATORY)));
        attributeValues.add(val);
        noRows++;
    }

    public void addAttribute(String ID, AttributeValue val, int type, int indexPos)
    {
        attributeTypes.add(indexPos, new AttributeType(ID, (type==AttributeType.MANDATORY)));
        attributeValues.add(indexPos, val);
        noRows++;
    }

    public void deleteAttribute(String ID, int indexPos)
    {
        if (attributeTypes.elementAt(indexPos).toString().equals(ID))
        {
            ((AttributeValue)attributeValues.elementAt(indexPos)).update(new String(""));
        }
        else
            System.err.println("Internal error: attempt to delete attribute with invalid ID in AttributeTableModel"+
                               "\n att name = " + attributeTypes.elementAt(indexPos).toString() + " ID = " + ID);
    }

    public void fireChange()
    {
        dataChanged = true;

        fireTableChanged(new TableModelEvent(this));
    }

    public DXEntry getOldEntry()
    {
        return oldEntry;
    }

    /**
     *  <p>Returns the escaped RDN (possibly multi-valued).</p>
     *
     *  <p>Implementation Note: The returned RDN is created based on the internal naming
     *  type/value arrays, which must be synched with the user modified
     *  attribute list</p>
     */

    public RDN getRDN()
    {
        String rdn = "";
        for (int i=0; i<attributeValues.size(); i++)
        {
            AttributeValue entryValue = (AttributeValue)attributeValues.get(i);
            if (entryValue.isNaming())
            {
                if (rdn.length()>0)
                    rdn += "+";
                rdn += attributeTypes.get(i).toString() + "=" + NameUtility.escape(entryValue.getStringValue());
            }
        }

        // in some ultra-wierd cases, the naming attribute can be made invisible due to access controls.  While
        // you might think anyone who does this gets what they deserve, we try to recover by using the 'old entry'
        // DN.

        if ("".equals(rdn))
            rdn = oldEntry.getRDN().toString();


        return new RDN(rdn);
/*
        StringBuffer rdn = new StringBuffer();
        for (int i=0; i<numberNamingValues; i++)  // usually only 1...
        {
            if (i > 0)
                rdn.append('+');

            rdn.append(namingTypes[i]);
            rdn.append('=');
            rdn.append(NameUtility.escape(namingRawValues[i]));
        }
        return new RDN(rdn.toString());
*/
    }

    /**
     *    Returns a new entry
     */

    public DXEntry getNewEntry()
    {
        /*
         *    Sort out the name of the entry, based on the (possibly)
         *    edited attribute value fields.
         */

        DN newDN = new DN(oldEntry.getDN());


        RDN newRDN = getRDN();



        newDN.setRDN(newRDN, newDN.size()-1);

        /*
         *    Create an empty DXEntry object, initialised with the new name.
         */

        DXEntry newEntry = new DXEntry(newDN);

        /*
         *    Work through all the known attributes, adding the atts to the
         *    new entry.
         */

        AttributeValue test;
        String id;

        for (int i=0; i<attributeTypes.size(); i++)
        {
            test = (AttributeValue)attributeValues.elementAt(i);
            if (!test.isEmpty())
            {
                id = attributeTypes.elementAt(i).toString();
                BasicAttribute exists = (BasicAttribute)newEntry.get(id);
                if (exists == null)  // no values of this att. already registered
                    newEntry.put(new BasicAttribute(id, test.value()));
                else
                    exists.add(test.value());
            }
        }

        if (oldEntry.getStatus() == DXEntry.NEW)    // old entry isn't in directory yet.
        {
            newEntry.setStatus(DXEntry.NEW);
        }

        return newEntry;
    }

    /**
     *    Reads all the values for a given attribute
     *    from the table.
     */
    public Attribute getAttribute(String ID)
    {
        BasicAttribute returnAtt = new BasicAttribute(ID);
        for (int i=0; i<attributeTypes.size(); i++)
            if (ID.equals(attributeTypes.elementAt(i).toString()))
            {
                Object o = ((AttributeValue)attributeValues.elementAt(i)).value();

                // don't allow zero length string attributes...
                if (o!= null && o instanceof String)
                {
                    if (((String)o).length() == 0)
                        o = null;
                }

                if (o != null) // only add if there is a real value...
                    returnAtt.add(o);

            }

        return returnAtt;
    }

    /**
     *    Brute force search to find an attributeType given only the name.
     *    rarely used - main use is when popupTool tries to create a new
     *    attribute value entry, knowing only the type name.
     */

    public boolean isMandatory(String attributeTypeName)
    {
        for ( int i=0; i<attributeTypes.size(); i++)
        {
            if (((AttributeType)attributeTypes.elementAt(i)).toString().equals(attributeTypeName))
            {
                return ((AttributeType)attributeTypes.elementAt(i)).isMandatory();
            }
        }
        System.err.println("unable to find type name " + attributeTypeName);
        return false;  // couldn't find it.
    }

    /**
     *    Checks that all mandatory attributes have at least one value entered.
     */

    // surprisingly messy ftn.
    // Check through type list...
    //    find new type
    //    check if mandatory
    //    if mandatory, check values
    //       -> if no values found, return false
    //    continue until no types left.

    public boolean checkMandatoryAttributesSet()
    {
        AttributeType type, testType;
        AttributeValue value;
        String ID = "";
        boolean inDoubt = false;
        int i=0;

        while (i<noRows)                                            // for all rows
        {
            type = (AttributeType)attributeTypes.elementAt(i);

            if (type.isMandatory())                                // find mandatory types
            {
                ID = type.toString();
                inDoubt = true;
                testType = type;

                while (ID.equals(testType.toString()) && i<noRows) // cycle through all the values for that
                {                                                  // mandatory attribute...
                    if (inDoubt)                                   // ... until we find a valid value
                    {
                        value = (AttributeValue)attributeValues.elementAt(i);
                        if (value.isEmpty()==false)                // !!! found a valid value
                            inDoubt = false;
                    }
                    i++;
                    if (i<noRows) testType = (AttributeType)attributeTypes.elementAt(i);
                }

                if (inDoubt)         // Iff still in doubt, means no valid value was found
                    return false;    // *** RETURN FALSE ***   - mandatory value not filled in!
            }
            else
            {
                i++;
            }
        }

        return true;                 // *** RETURN TRUE *** - no unfilled out mandatory value found.
    }

    /**
     *    This removes a component from the array of naming atts, and
     *    sets the currentValue object to be a naming value.
     */

    public void removeNamingComponent(AttributeType currentType, AttributeValue currentValue)
    {

        try
        {
            String type = currentType.getValue();
            String value = currentValue.getStringValue();

            if ("".equals(type) || "".equals(value)) // which it really, really shouldn't...
                return;
            if (numberNamingValues == 1) // it would be a bad idea to remove the last naming value
                return;

            dataChanged = true;

            for (int i=0; i<numberNamingValues; i++)
            {
                if (type.equals(namingTypes[i]) && value.equals(namingRawValues[i]))
                {
                    int removeRow = i;
                    namingTypes = removeRowFromArray(namingTypes, removeRow);
                    namingRawValues = removeRowFromArray(namingRawValues, removeRow);
                    numberNamingValues--;
                    break;
                }
            }

            for (int i=0; i<attributeValues.size(); i++)
            {
                AttributeValue attval = (AttributeValue)attributeValues.get(i);
                if (attval.getID().equals(type))
                    if (attval.getStringValue().equals(value))
                    {
                        attval.setNamingStatus(false);
                    }
            }
        }
        catch (Exception e) // nope, we won't be doing that.
        {
            e.printStackTrace();
            return;
        }

    }


    protected static String[] removeRowFromArray(String[] array, int removeRow)
    {
        int originalLength = array.length;
        if (removeRow < 0 || removeRow >= originalLength)
            return array;

        String[] temp = new String[array.length-1];

        if (removeRow > 0)
            System.arraycopy(array, 0, temp, 0, removeRow);

        if (removeRow < originalLength-1)
            System.arraycopy(array, removeRow+1, temp, removeRow, (originalLength-removeRow-1));

        return temp;
    }


    public void dumpNamingArrays()
    {
        System.out.println("dump naming array");
        for (int i=0; i<numberNamingValues; i++)
        {
            System.out.println(i + " type        " + namingTypes[i]);
            System.out.println(i + " value       " + namingRawValues[i]);
        }
    }


    public void addNamingComponent(AttributeType currentType, AttributeValue currentValue)
    {

        String type = currentType.getValue();
        String value = currentValue.getStringValue();

        if ("".equals(type) || "".equals(value)) // which it really, really shouldn't...
            return;

        dataChanged = true;

        String[] tempTypes = new String[numberNamingValues+1];
        String[] tempRawValues = new String[numberNamingValues+1];

        System.arraycopy(namingTypes, 0, tempTypes, 0, numberNamingValues);
        System.arraycopy(namingRawValues, 0, tempRawValues, 0, numberNamingValues);

        tempTypes[numberNamingValues] = type;
        tempRawValues[numberNamingValues] = value;

        numberNamingValues++;

        namingTypes = tempTypes;
        namingRawValues = tempRawValues;

        currentValue.setNamingStatus(true);
    }



    /**
     *    This returns whether the table data has been modified
     *    since the original display of the entry.
     */

    public boolean changedByUser()
    {
        return dataChanged;
    }
}

⌨️ 快捷键说明

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