📄 returnattributesdialog.java
字号:
package com.ca.directory.jxplorer.search;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.util.logging.Logger;
import java.util.logging.Level;
import javax.swing.*;
import javax.naming.NamingException;
import java.io.*;
import com.ca.commons.cbutil.*;
import com.ca.directory.jxplorer.JXplorer;
import com.ca.directory.jxplorer.HelpIDs;
import com.ca.directory.jxplorer.broker.JNDIBroker;
/**
* This class acts as a item selector. It sets up a dialog that lets you select items
* from one list and add them to another list. It also allows you to remove selected
* items from the list.
*/
public class ReturnAttributesDialog extends CBDialog
{
private static Logger log = Logger.getLogger(ReturnAttributesDialog.class.getName());
private JList availableList, selectedList;
private CBButton btnAdd, btnRemove, btnSave, btnLoad, btnDelete;
private JTextField nameField;
private JCheckBox includeDNCheckBox;
private ArrayList arrayList = new ArrayList();
private Properties properties;
private String localDir="";
private JXplorer jx;
/**
* Flag for save prompt.
*/
private boolean hasSaved = false;
/**
* The property file which stores the return attributes list.
*/
public static final String FILENAME = "return_attributes.txt";
/**
* Flag that indicates that the DN should be included in the results window.
*/
public static final String INCLUDE_DN = "[DN]";
/**
* Used to indicate no return attributes.
*/
public static final String DEFAULT_RETURN_ATTRS = "None";
/**
* Static method that should be used rather than creating an object directly if
* you wish to get the user input after the user has finished making selections and
* has closed the window.
* <p>
* This method creates a CBListSelector object and calls its 'show' method. When the
* user is finished with the dialog this method returns the user's selections.
* @param jx the parent frame (JXplorer).
* @return a list of user selections.
*/
public static ArrayList getSelectedValues(JXplorer jx)
{
ReturnAttributesDialog listSelector = new ReturnAttributesDialog(jx);
listSelector.setVisible(true);
return listSelector.getSelectedValues();
}
/**
* Returns the names of all the saved return-attributes lists from the property file
* 'return_attributes.txt'.
* @param name the name of the return attribute list.
* @return an array containing all the names of the lists.
*/
public static String[] getReturnAttributes(String name)
{
ArrayList list = new ArrayList(0);
Properties prop = getProperties();
if(prop == null)
return new String[] {"objectClass"}; // as a default, get the object class rather than nothing ("1.1")
String value = prop.getProperty(name);
if(value == null)
return new String[] {"objectClass"}; // as a default, get the object class rather than nothing ("1.1")
getReturnAttributes(value, list);
return (String[]) list.toArray(new String[list.size()]);
}
/**
* Returns the names of all the saved return-attributes lists from the property file
* 'return_attributes.txt'.
* @param value the list of return attributes.
* @param list a list of return attributes that are obtained from the value.
*/
public static void getReturnAttributes(String value, ArrayList list)
{
if ((value.indexOf(";")>-1)==true)
{
list.add(value.substring(0, value.indexOf(";")));
getReturnAttributes(value.substring(value.indexOf(";")+1), list);
}
}
/**
* Returns the names of all the saved return-attributes lists from the property file
* 'return_attributes.txt'.
* @return an array containing all the names of the lists.
*/
public static Object[] getSavedListNames()
{
Enumeration en = null;
ArrayList list = new ArrayList(0);
try
{
en = (getProperties()).propertyNames();
}
catch(Exception e)
{
list.add(DEFAULT_RETURN_ATTRS);
return list.toArray();
}
list.add(DEFAULT_RETURN_ATTRS);
while (en.hasMoreElements())
{
list.add(en.nextElement().toString());
}
return list.toArray();
}
/**
* Sets up the property file called 'return_attributes.txt' in the user dir.
*/
public static Properties getProperties()
{
Properties myProperties = new Properties();
String temp = System.getProperty("user.dir") + File.separator;
if (temp==null) { log.warning("Unable to read user home directory."); return null;}
myProperties = CBUtility.readPropertyFile(temp + FILENAME);
if (myProperties.size()==0) { log.info("Initialising config file: " + temp + FILENAME); return null;}
return myProperties;
}
/**
* Sets up a dialog with two lists. The list on the left displays all available values
* that the user can select from. When the user selects a value it is displayed in the
* list on the right either by double clicking on the value or clicking the '>' button.
* The user can also remove a value from the selection list either by double clicking on
* it or clicking the '<' button.
* @param jx the parent frame (JXplorer).
*/
public ReturnAttributesDialog(JXplorer jx)
{
super(jx, CBIntText.get("Return Attributes"), HelpIDs.SEARCH_RETURN_ATTRIBUTES);
this.jx = jx;
setUpPropertyFile();
CBPanel topPanel = new CBPanel();
CBPanel leftPanel = new CBPanel();
CBPanel middlePanel = new CBPanel();
CBPanel rightPanel = new CBPanel();
CBPanel bottomPanel = new CBPanel();
// Top panel...
topPanel.makeLight();
topPanel.add(new JLabel(CBIntText.get("Name: ")));
topPanel.makeWide();
topPanel.add(nameField = new JTextField(CBIntText.get("Untitled")));
topPanel.makeLight();
topPanel.add(btnSave = new CBButton(CBIntText.get("Save"),
CBIntText.get("Save your selected return attributes for use in the Search dialog.")));
topPanel.add(btnLoad = new CBButton(CBIntText.get("Load"),
CBIntText.get("Load an already saved list of return attributes.")));
topPanel.add(btnDelete = new CBButton(CBIntText.get("Delete"),
CBIntText.get("Delete a already saved list of return attributes.")));
btnSave.setPreferredSize(new Dimension(62, 20));
btnSave.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
save();
hasSaved = true;
}});
btnLoad.setPreferredSize(new Dimension(62, 20));
btnLoad.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
load();
hasSaved = false;
}});
btnDelete.setPreferredSize(new Dimension(70, 20));
btnDelete.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
delete();
hasSaved = true;
}});
// Left panel...
leftPanel.addln(new JLabel(CBIntText.get("Available Attributes:")));
leftPanel.makeHeavy();
availableList = new JList(getAttributes());
// Only one item can be selected at anyone time...
availableList.setSelectionMode(0);
// Tries to ensure that the selected item is visible...
availableList.setSelectionModel(new CBSingleSelectionModel(availableList));
leftPanel.addln(new JScrollPane(availableList));
// Middle panel...
btnAdd = new CBButton(CBIntText.get(">"),
CBIntText.get("Add an attribute from the attribute list on the left to the selection list on the right."));
btnAdd.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
add();
hasSaved = false;
}});
btnRemove = new CBButton(CBIntText.get("<"),
CBIntText.get("Remove an attribute from the selection list on the right."));
btnRemove.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
remove();
hasSaved = false;
}});
middlePanel.makeHeavy();
middlePanel.addln(new JLabel(" "));
middlePanel.makeLight();
middlePanel.addln(btnAdd);
middlePanel.addln(btnRemove);
middlePanel.makeHeavy();
middlePanel.addln(new JLabel(" "));
// Right panel...
rightPanel.addln(new JLabel(CBIntText.get("Selected Attributes:")));
rightPanel.makeHeavy();
selectedList = new JList();
// Only one item can be selected at anyone time...
selectedList.setSelectionMode(0);
// Tries to ensure that the selected item is visible...
selectedList.setSelectionModel(new CBSingleSelectionModel(selectedList));
rightPanel.addln(new JScrollPane(selectedList));
// Bottom panel...
includeDNCheckBox = new JCheckBox(CBIntText.get("Include DN in search results."));
includeDNCheckBox.setToolTipText(
CBIntText.get("Click the checkbox if you want the DN of each result displayed in the results window."));
bottomPanel.makeLight();
bottomPanel.add(includeDNCheckBox);
bottomPanel.makeHeavy();
bottomPanel.addln(new JLabel(" "));
// Main panel...
display.addln(new JLabel(" "));
display.makeWide();
display.addln(topPanel);
display.makeHeavy();
display.add(leftPanel);
display.makeLight();
display.add(middlePanel);
display.makeHeavy();
display.add(rightPanel);
display.newLine();
display.makeLight();
display.addln(bottomPanel);
setSize(400, 350);
CBUtility.center(this, owner);
registerMouseListeners();
}
/**
* Gets a list of attributes that are available in the schema which can be used for
* searching. These are used in the availableList part of the list.
* @return a string array of the available attributes to JX (null - if no schema publishing i.e. LDAP V2).
*/
protected String[] getAttributes()
{
try
{
JNDIBroker searchBroker = jx.getSearchBroker();
ArrayList en = searchBroker.getSchemaOps().listEntryNames("schema=AttributeDefinition,cn=schema");
// Check for no schema publishing i.e. LDAP V2...
if(en==null)
return null;
String[] temp = (String[]) en.toArray(new String[] {});
Arrays.sort(temp, new CBUtility.IgnoreCaseStringComparator());
return temp;
}
catch (NamingException e)
{
log.log(Level.WARNING, "Error accessing attribute defs in ReturnAttributesDialog ", e);
return null;
}
}
/**
* Sets up the property file called 'return_attributes.txt' in the user dir.
*/
protected void setUpPropertyFile()
{
properties = new Properties();
String temp = System.getProperty("user.dir") + File.separator;
if (temp==null) { log.warning("Unable to read user home directory."); return;}
localDir = temp;
properties = CBUtility.readPropertyFile(temp + FILENAME);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -