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

📄 returnattributesdialog.java

📁 JAVA开源LDAP浏览器jxplorer的源码!
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        if (properties.size()==0) { log.info("Initialising config file: " + temp + FILENAME); return;}
	}

   /**
    *	Adds a double click mouse listener to both lists in this dialog.
	*	If the double click occurs in the list on the left, the 'add' method is
	*	called.  If the double click occurs in the list on the right, the
	*	remove method is called.
	*/
    protected void registerMouseListeners()
    {
		availableList.addMouseListener(new MouseAdapter()
		{
			public void mouseClicked(MouseEvent e)
			{
			   	if (e.getClickCount() == 2)
					add();
                    hasSaved = false;
			}
		});

		selectedList.addMouseListener(new MouseAdapter()
		{
		    public void mouseClicked(MouseEvent e)
		    {
		       	if (e.getClickCount() == 2)
					remove();
                    hasSaved = false;
		    }
		});
    }

   /**
    *	Gets the selected item from the list on the left and adds it to
	*	the list on the right.  It uses a global array list to keep track
	*	of the selections in the right hand list.
	*/
	public void add()
	{
		try
		{
		    if(!arrayList.contains(availableList.getSelectedValue()))
				arrayList.add(availableList.getSelectedValue());

			selectedList.setListData(arrayList.toArray());
		}
		catch(Exception e)
		{
			log.warning("No selection to add.");
		}
	}

   /**
    *	Removes the selected item from the list on the left.  It uses a
	*	global array list to keep track of the selections in the right hand list.
	*/
	public void remove()
	{
		try
		{
			arrayList.remove(selectedList.getSelectedIndex());
			selectedList.setListData(arrayList.toArray());
		}
		catch(Exception e)
		{
			log.warning("No selection to remove.");
		}
	}

   /**
    * 	Saves a list to the property file called 'return_attributes.txt'.  Saves the list
	*	by 'name=value', where 'value' is the list of attributes to return separated by a ';'.
	*	For example myList=attr1;attr2;attr3;
	*	<p>
	*	Before saving some quick checks are done...
	*	<br>attributes have been selected,
	*	<br>the name is valid (not null, not empty and not 'Untitled),
	*	<br>the name doesn't already exist.
	*/
	public void save()
	{
		if(properties == null)
			setUpPropertyFile();

		ArrayList list = getSelectedValues();

        // Any attributes selected?
		if (list==null)
		{
			JOptionPane.showMessageDialog(this,
                    CBIntText.get("Please select the return attributes that you want to save in your list"),
                    CBIntText.get("Nothing to Save"), JOptionPane.INFORMATION_MESSAGE );
			return;
		}

		String name = nameField.getText();

        // Valid name?
		if (name==null || name.trim().length() <= 0 || name.equalsIgnoreCase("Untitled"))
		{
			JOptionPane.showMessageDialog(this, CBIntText.get("Please enter a name for your list."),
                    CBIntText.get("Name Not Supplied"), JOptionPane.INFORMATION_MESSAGE );
			return;
		}

        // Name exists?
		if (exists(name))
		{
	        int response = JOptionPane.showConfirmDialog(this, CBIntText.get("Do you want to replace it?"),
                    CBIntText.get("List Exists"), JOptionPane.OK_CANCEL_OPTION);

	        if (response != JOptionPane.OK_OPTION)
	            return;
		}

		StringBuffer buffy = new StringBuffer(0);

        // Check if user wants to include the DN in the search results, if so add to list...
        if(includeDNCheckBox.isSelected())
            buffy.append(INCLUDE_DN + ";");

		for(int i=0; i<list.size();i++)
			buffy.append(list.get(i)+";");

		properties.setProperty(name, buffy.toString());
		CBUtility.writePropertyFile(localDir + FILENAME, properties, "");

		JOptionPane.showMessageDialog(this,
                CBIntText.get("Your return attributes list has been saved as ''{0}''.",new String[] {name}),
                CBIntText.get("Saved"), JOptionPane.INFORMATION_MESSAGE );

        // Set the search GUI to null so that it is forced to re-read it's config and pick up new lists...
		jx.getTree().setSearchGUI(null);
		jx.getSearchTree().setSearchGUI(null);
		jx.getSchemaTree().setSearchGUI(null);
	}

   /**
    *	Gets all the names of the saved list from the property file 'return_attributes.txt', then
	*	pops up a JOptionPane dialog that has a combo box displaying these names.  Gets the user selection
	*	then retreves the value from the property file.  Recall that in the property file the value is saved
	*	as a group of attribute names that are separated by a ';'.  This method calls the 'getListAttrs' method
	*	that recursively extracts the attribute names.  Once the names come back they are dropped into the
	*	selectedList (JList).
	*/
	public void load()
	{
		Enumeration en = properties.propertyNames();

		ArrayList list = new ArrayList(0);

		while (en.hasMoreElements())
		{
            list.add((String)en.nextElement());
		}

		if (list.size()==0)
		{
			JOptionPane.showMessageDialog(this,
                    CBIntText.get("There are no filters available to load."),
                    CBIntText.get("Nothing to Load"), JOptionPane.INFORMATION_MESSAGE );
			return;
		}

		Object listOb[] = list.toArray();

		CBJComboBox loadCombo = new CBJComboBox(listOb);
		loadCombo.setRenderer(new CBBasicComboBoxRenderer(listOb));
		loadCombo.setPreferredSize(new Dimension(140, 20));
        int response = JOptionPane.showConfirmDialog(this, loadCombo,
                CBIntText.get("Select List"), JOptionPane.OK_CANCEL_OPTION);

        if (response != JOptionPane.OK_OPTION)
            return;

        // Default the check box to NOT checked...
        includeDNCheckBox.setSelected(false);

		String name = (loadCombo.getSelectedItem()).toString();
		String loadList = getList(name);

		nameField.setText(name);

        // Get rid of the old attributes that are floating around...
		arrayList.clear();
		getListAttrs(loadList, arrayList);

		selectedList.setListData(arrayList.toArray());
	}

   /**
    *	Recursively extracts the attributes from the saved return attributes list.
	*	@param loadList the list of file names separated by a ';'.
	*	@param list the list to store the file names in.
	*/
	public void getListAttrs(String loadList, ArrayList list)
	{
		if (loadList.indexOf(";") > -1)
		{
            String temp = loadList.substring(0, loadList.indexOf(";"));

            // Check if the attribute is the inlude DN flag, if it is just check the check box.
            // Otherwise add the attribute to the list of selected attributes.
            if(temp.equalsIgnoreCase(INCLUDE_DN))
                includeDNCheckBox.setSelected(true);
            else
			    list.add(temp);

            // Move along to the next index and call this method again...
			getListAttrs(loadList.substring(loadList.indexOf(";")+1), list);
		}
	}

   /**
    *	Returns true if the property file (return_attributes.txt) contains the supplied
	*	key name.
	*	@param name the name of the list for example, 'myList'.
	*	@return true if the property file contains the list, false otherwise.
	*/
	protected boolean exists(String name)
	{
        // Check if the list name already exists, if so return true...
		if(properties.containsKey(name))
			return true;

		return false;
	}

   /**
    *	Returns the value from the property file of a given list name.
	*	@param name the key of the value that is being returned (e.g. 'myList').
	*	@return the value of the key i.e. the list.
	*/
	public String getList(String name)
	{
		return properties.getProperty(name);
	}

   /**
    * 	Checks if the name of the list that the user wants to delete is valid, calls the remove method
	*	then clears the text in the name field.
	*/
	public void delete()
	{
		String toDelete = nameField.getText();

		if (toDelete==null || toDelete.trim().length() <= 0 || toDelete.equalsIgnoreCase("Untitled"))
		{
			JOptionPane.showMessageDialog(this,
                    CBIntText.get("Please enter the name of the list that you want to delete."),
                    CBIntText.get("Nothing to Delete"), JOptionPane.INFORMATION_MESSAGE );
			return;
		}
		else
		{
	        int response = JOptionPane.showConfirmDialog(this,
                    CBIntText.get("Are you sure you want to delete the list ''{0}''?", new String[] {toDelete}),
                    CBIntText.get("Delete List?"), JOptionPane.OK_CANCEL_OPTION);

	        if (response != JOptionPane.OK_OPTION)
	            return;
		}

		removeList(toDelete);

		nameField.setText("");

        // Get rid of the old attributes that are floating around...
		arrayList.clear();
		selectedList.setListData(arrayList.toArray());

        // Default the check box to NOT checked...
        includeDNCheckBox.setSelected(false);

        // Set the search GUI to null so that it is forced to re-read it's config and pick up new lists...
		jx.getTree().setSearchGUI(null);
		jx.getSearchTree().setSearchGUI(null);
		jx.getSchemaTree().setSearchGUI(null);
	}

   /**
    *	Removes a list from the properties file return_attributes.txt.
	*	@param name the list name (key) to be removed (deleted).
	*/
	protected void removeList(String name)
	{
		if(!properties.containsKey(name))
			return;

		properties.remove(name);
		CBUtility.writePropertyFile(localDir + FILENAME, properties, "");
		removeFromSearch(name);
	}

   /**
    *	If a return attribute list is deleted check that the search file doesn't have
	*	a reference to it.  If it does delete it.
	*	@param name the name of the return attribute list that is being deleted.
	*/
	public void removeFromSearch(String name)
	{
		SearchModel sm = new SearchModel();
		sm.removeRetAttrs(name);
	}

   /**
    *	Returns a list of the values that the user has selected.
	*	@return a list of the values that the user has selected.
	*/
	public ArrayList getSelectedValues()
	{
		if(arrayList.isEmpty())
			return null;
		else
			return arrayList;
	}

   /**
    *   Overrides the doOK of CBDialog so that a prompt is displayed before the user exits.
    */
    public void doOK()
    {
        if(!hasSaved)
        {
            int response = JOptionPane.showConfirmDialog(this, CBIntText.get("Exit without saving the return attributes list?"),
                                                            CBIntText.get("Exit Without Saving"), JOptionPane.OK_CANCEL_OPTION);

            if (response == JOptionPane.OK_OPTION)
                super.doOK();
        }
        else
        {
            super.doOK();
        }
    }
}

⌨️ 快捷键说明

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