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

📄 buildfilterpanel.java

📁 JAVA开源LDAP浏览器jxplorer的源码!
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
				attributeCombo[rowCount].setRenderer(new CBBasicComboBoxRenderer(attrs));

                int pos = getPriorIndex(attributeCombo, rowCount);                  //TE: select the prior combox box selection if possible.
                if(pos>0)
                    attributeCombo[rowCount].setSelectedIndex(pos);
                else
                    attributeCombo[rowCount].setSelectedItem("sn");	 				//TE: display 'sn' as first choice.
			}
						
			attributeCombo[rowCount].setPreferredSize(new Dimension(140, 20));

			panel.makeLight();
				
			panel.add(functionCombo[rowCount] = new CBJComboBox(functionArray));	//TE: the function combo ('equals', 'beginning with' etc).

            int pos = getPriorIndex(functionCombo, rowCount);                       //TE: select the prior combox box selection if possible.
            if(pos>0)
                functionCombo[rowCount].setSelectedIndex(pos);
            else
                functionCombo[rowCount].setSelectedItem(functionArray[BEGIN]);

            functionCombo[rowCount].addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e){								
					for(int i=1;i<=rowCount;i++)								    //TE: if a user selects present or !present, disable text field.
					{
                        checkTextFieldEnabled(i);
					}
			}});
			functionCombo[rowCount].setPreferredSize(new Dimension(140, 20));
			functionCombo[rowCount].setRenderer(new CBBasicComboBoxRenderer(functionArray));

			panel.makeWide();
			panel.add(filterField[rowCount] = new JTextField());					//TE: the filter criteria field (bob, fred etc).
	      	filterField[rowCount].setRequestFocusEnabled(true);
						
			final int row = rowCount;

            // Add a mouse listener so we know when filter field is clicked and so we can check for editor...
            addMouseListener(row);

            checkTextFieldEnabled(rowCount);

			panel.makeLight();

			panel.revalidate();				
		}		
	}

    /**
     * Adds a mouse listener to the filterField box.  The mouse
     * listener checks and opens an editor if any exist for the
     * attribute type.
     * @param row the index of the current row which is used
     * to get the attribute name from the attribute box.  This
     * name is used to determine the syntax of the attribute
     * which in turn determines which (if any) editor is loaded.
     */
    private void addMouseListener(final int row)
    {
        filterField[rowCount].addMouseListener(new MouseListener()              //TE: fix for bug 6466.
        {
            /**
             * Invoked when the mouse button has been clicked (pressed and released) on a component.
             */
            public void mouseClicked(MouseEvent e)
            {
                // Opens special editors...i.e. generalized time editor.
                checkForSpecialEditor(attributeCombo[row].getSelectedItem().toString(), filterField[row]);
            }

            /**
             * Invoked when the mouse enters a component.
             */
            public void mouseEntered(MouseEvent e) {}

            /**
             * Invoked when the mouse exits a component.
             */
            public void mouseExited(MouseEvent e) {}

            /**
             * Invoked when a mouse button has been pressed on a component.
             */
            public void mousePressed(MouseEvent e) {}

            /**
             * Invoked when a mouse button has been released on a component.
             */
            public void mouseReleased(MouseEvent e) {}
        });
    }

    /**
     * Checks if the PRESENT or NOTPRESENT function is selected.
     * If so, disables the text field.  If not enables it.
     * @param row the filter row that we are checking.
     */
    private void checkTextFieldEnabled(int row)
    {
        int item = functionCombo[row].getSelectedIndex();
        if (item == PRESENT || item == NOTPRESENT)
        {
            filterField[row].setEnabled(false);
            filterField[row].setBackground(Color.lightGray);
        }
        else
        {
            filterField[row].setEnabled(true);
            filterField[row].setBackground(Color.white);
        }
    }

    /**
     * Returns the selected item of the combo box indicated to by the value of pos.
     * @param combo an array of combo boxes that we want to find the selected item of
     * a specified element.
     * @param pos indicates which element in the combo array to get the selected
     * index of.
     * @return either the index selected item of the selected combo box, or pos.
     */
    public int getPriorIndex(JComboBox[] combo, int pos)
    {
        pos = (pos <= 0) ? pos : pos -1;
        if(pos > 0)
            return combo[pos].getSelectedIndex();
        else
            return pos;
    }

   /**
    *	Takes an attribute name and checks its syntax against that of the GeneralizedTime
	*	syntax (~121.1.24).  If they are the same the GeneralizedTime editor is opened & the
	*	Search dialog is forced to wait until the user has finished entering date and time info.
	*	This can be expanded to check for other editors - currently it just checks for one.
	*	@param attrName the attribute name - usually from the combo box in the Search dialog (Build
	*				tab).
	*	@param field the text field where the text will be set after the time dialog is closed.
	*/
	protected void checkForSpecialEditor(String attrName, JTextField field)
	{
		if(schema.getAttributeSyntax(attrName).indexOf("121.1.24")>-1)
		{
			gte = new generalizedtimeeditor(jxplorer, field.getText(), false);
			CBUtility.center(gte, jxplorer);    	//TE: centres the attribute editor.
			gte.setVisible(true);
			try
			{
				while (gte.isVisible())
					wait();
					
				field.setText(gte.getTime());
				field.setEditable(false);	 
				field.transferFocus();							
			}
			catch(Exception e)
			{
				log.log(Level.WARNING, "Problem with getting search filter information from editor: ", e);
				field.setText("");				
			}
		}	
		else
		{
			field.requestFocus();
			field.setEditable(true);			
		}				
	}

   /**	
    *	Removes lines (rows) from the filter constructor.  Starting from the last added.
	*/
	protected void removeFilterRow()
	{
		if (rowCount > 1)
		{	
			if(rowCount==2)				
				panel.remove(andOrCombo);
			
			panel.remove(attributeCombo[rowCount]);	
			panel.remove(functionCombo[rowCount]);
			panel.remove(filterField[rowCount]); 	
			
			if (rowCount>2)
			{	
				labelCount--;
				panel.remove(andOrLabel[labelCount]);
			}
			
			panel.repaint();	   
						
			rowCount--;	
		}

		panel.revalidate();
	}	

   /**
    *	Gets a list of attributes that are available in the schema which can be used for 
	*	searching.  These are used in the attributeCombo part of the filter.  
	*	@return a string array of the available attributes to JX (null - if no schema publishing i.e. LDAP V2).
	*/
	protected String[] getAttributes()
	{
        if(schema == null)
        {
            CBUtility.error("Unable to access schema in BuildFilterPanel - no schema available.");
            return new String[] {};
        }

        try
        {
            ArrayList attributeNames = schema.listEntryNames("schema=AttributeDefinition,cn=schema");
            if(attributeNames==null)		//TE: check for no schema publishing i.e. LDAP V2.
                return null;

            String[] temp = (String[]) attributeNames.toArray(new String[] {});
            Arrays.sort(temp, new CBUtility.IgnoreCaseStringComparator());

            return temp;
        }
        catch (NamingException e)
        {
            CBUtility.error("Unable to access schema in BuildFilterPanel " + e.toString());
            return null;
        }
	}

   /**
    *	Checks if the filter is valid.  A filter is not valid if any attribute or function
	*	combo contains an empty string or a null.
	*	returns true if all combo contain a value, false otherwise.
	*/
	protected boolean isFilterValid()
	{
		try
		{
			for(int i=1; i<=rowCount; i++)
			{
				String attr = attributeCombo[i].getSelectedItem().toString();
				
				if(attr.trim().length()<=0)
					return false;				//TE: check if the attribute combo has a value.
					
				String func = functionCombo[i].getSelectedItem().toString();
						
				if(attr.trim().length()<=0)		//TE: check if the function combo has a value.
					return false;
					
				if (rowCount>=2)
				{		
					String andOr = andOrCombo.getSelectedItem().toString();
			
					if(andOr.trim().length()<=0) 		//TE: check if the and/or combo has a value.
						return false;
				}					
			}
		}
		catch(Exception e)						//TE: incase the row count has been messed up.
		{
			return false;
		}
		
		return true;
	}					
			
   /**
    *	Constructs the filter by adding filter parts to a string buffer for each row in the filter constructor.
	*	@return the filter.
	*/
	protected String getFilter()
	{	
		StringBuffer buffy = new StringBuffer();

		for(int i=1;i<=rowCount;i++)				
			buffy.append(getFunctionPart(i));			

		if (rowCount>=2)
		{
			switch (andOrCombo.getSelectedIndex())
			{
				case 1: { buffy.insert(0, "(|"); break; }       //TE: 'Or' option.
				default: { buffy.insert(0, "(&"); break; }		//TE: 'And' option.
			}
			
			buffy.append(")");
		}		
		
		if (notCheckBox.isSelected())
		{
			buffy.insert(0, "(!");
			buffy.append(")");	
		}
								
		return buffy.toString();
	}

   /**
    *	Constructs the body ('(cn=w*)') of the filter by reading the attribute type selected (i.e. 'cn'),
	*	the function (i.e. 'Beginning With') and reading the actual filter (i.e. 'w') for a given row 
	*	number.  
	*	@param i the row number that is being processed.
	*   @return returns the function string which is then appended to a global string 
	*   	buffer that is used in the search.
	*/
	protected String getFunctionPart(int i)
	{
		String attr = attributeCombo[i].getSelectedItem().toString();
		String func = functionCombo[i].getSelectedItem().toString();
		String text = filterField[i].getText();
	
		int index = functionCombo[i].getSelectedIndex();
		
		text = replace(text, "\\", "\\5c");		//TE: escapes any of the following characters: *, (, ), \.  WARNING: the '\\' replace should always go first.
		text = replace(text, "(", "\\28");
		text = replace(text, ")", "\\29");
		text = replace(text, "*", "\\2A");
		
		switch (index)
		{
			case BEGIN:			return "("   + attr + "="  + text + "*)";	//TE: (cn=f*)
			case NOTBEGIN:		return "(!(" + attr + "="  + text + "*))";	//TE: (!(cn=f*))
			case CONTAINING:	return "("   + attr + "=*" + text + "*)";	//TE: (cn=*f*)
			case NOTCONTAINING:	return "(!(" + attr + "=*" + text + "*))";	//TE: (!(cn=*f*))
			case EQUALS:		return "("   + attr + "="  + text + ")";	//TE: (cn=f)
			case NOTEQUALS:		return "(!(" + attr + "="  + text + "))";	//TE: (!(cn=f))
			case ENDING:		return "("   + attr + "=*" + text + ")";	//TE: (cn=*f)
			case NOTENDING:		return "(!(" + attr + "=*" + text + "))";	//TE: (!(cn=*f))
			case GREATER:		return "("   + attr + ">=" + text + ")";	//TE: (cn>=f)
			case NOTGREATER:	return "(!(" + attr + ">=" + text + "))";	//TE: (!(cn>=f))
			case LESS:			return "("   + attr + "<=" + text + ")";	//TE: (cn<=f)
			case NOTLESS:		return "(!(" + attr + "<=" + text + "))";	//TE: (!(cn<=f))			
			case PRESENT:		return "("   + attr + "=*)";				//TE: (cn=*)
			case NOTPRESENT:	return "(!(" + attr + "=*))";				//TE: (!(cn=*))
			case SIMILAR:		return "("   + attr + "~=" + text + ")";	//TE: (cn~=f)
			case NOTSIMILAR:	return "(!(" + attr + "~=" + text + "))";	//TE: (!(cn~=f))
			default: 			return "";
		}
	}

   /**
    *	Takes a string and replaces any occurances of a given string with another given string.
	*	@param string the string that contains the text to be replaced.
	*	@param oldString the text to be replaced.
	*	@param newString the text to take the place of the old text.
	*	@return the string with the new text in place.
	*/
	protected String replace(String string, String oldString, String newString)
	{
		int pos = -1;
		
		try
		{
			while ((pos = string.indexOf(oldString, pos+1))!=-1)
			{
				string = string.substring(0,pos)+newString+string.substring(pos+oldString.length());
			}
			return string;
		}
		catch(Exception e)
		{
			return string;	
		}
	}
}

⌨️ 快捷键说明

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