📄 bookmarks.java
字号:
package com.ca.directory.jxplorer;
import java.awt.*;
import java.util.*;
import java.util.logging.Logger;
import javax.swing.*;
import javax.swing.border.*;
import com.ca.commons.naming.*;
import com.ca.commons.cbutil.*;
import com.ca.directory.jxplorer.search.SearchModel;
/**
* This class and it's inner classes handle adding, editing and deleting of bookmarks.
* All the common operations are in this class whereas all the GUI setup and specific
* operations are in the inner classes: AddDialog, EditDialog and DeleteDialog.
* It uses a property file called 'bookmarks.txt' to store the bookmarks in the
* following format:
* <p>
* dn.name=something e.g: dn.DEMOCORP=o=DEMOCORP,c=AU<br>
* desc.name=something e.g: desc.DEMOCORP=A sample directory.
* <p>
* The name part (e.g: DEMOCORP) is used as the title of the bookmark in the combo box.
* @author Trudi.
*/
public class BookMarks
{
/**
* The parent frame that the dialogs should centre on.
*/
JXplorer jxplorer = null;
/**
* The bookmark property file used for reading, writing and deleting
* bookmarks from and to.
*/
Properties propertyList = null;
/**
* The name of the property file.
*/
final String FILE_NAME = "bookmarks.txt";
/*
* The path to the property file.
*/
String bookmarkPath;
/**
* The dialog used for deleting a bookmark.
*/
DeleteDialog deleteDialog = null;
/**
* The dialog used for editing a bookmark.
*/
EditDialog editDialog = null;
/**
* The dialog used for adding a bookmark.
*/
AddDialog addDialog = null;
private static Logger log = Logger.getLogger(BookMarks.class.getName());
/**
* Sets up the property list.
* @param jx jxplorer (parent component).
*/
public BookMarks(JXplorer jx)
{
jxplorer = jx;
bookmarkPath = CBUtility.getPropertyConfigPath(FILE_NAME);
propertyList = CBUtility.readPropertyFile(bookmarkPath);
}
/**
* Checks if the Name contains only spaces. A Name is considered invalid if this is the
* case therefore this method will return false. Otherwise true is returned. This method
* can be expanded to do other checking in the future.
* @param name the name of the bookmark that we want to validate.
* @return true if the Name doesn't contains only spaces, false otherwise.
*/
protected boolean isValidName(String name)
{
try
{
if(name.trim().length()<=0)
return false;
}
catch(Exception e)
{
log.warning("Name '" + name + "' not specified in Bookmarks");
return false;
}
return true;
}
/**
* Checks if the DN equals 'cn=no entries' or contains only spaces.
* A DN is considered invalid if this is the
* case therefore this method will return false.
* Otherwise true is returned. This method
* can be expanded to do other checking in the future.
* @param dn the DN that we want to validate.
* @return true if the DN doesn't equal 'cn=no entries'
* or doesn't contains only spaces, false otherwise.
*/
protected boolean isValidDN(String dn)
{
if(dn.equalsIgnoreCase("cn=no entries"))
return false;
else if(dn.trim().length()<=0)
return false;
else
return true;
}
/**
* Takes a DN and returns it's current lowest RDN.
* @param dn the DN which we will get the RDN from.
* @return the RDN of the DN.
*/
protected String getCurrentRDN(String dn)
{
DN bloop = new DN(dn);
return bloop.getLowestRDN().toString();
}
/**
* Returns true or false depending on if a bookmark exists
* in the property file 'bookmarks.txt'.
* @param name the name of the book mark which is part of
* the key (i.e. dn.name).
* @return true if the bookmark exists, false if not.
*/
protected boolean checkIfBookmarkExists(String name)
{
propertyList = CBUtility.readPropertyFile(bookmarkPath);
return propertyList.containsKey("dn."+name);
}
/**
* Creates a new DeleteDialog object if deleteDialog is
* null.
* @return the DeleteDialog object.
*/
public DeleteDialog getDeleteDialog()
{
if(deleteDialog == null)
return new DeleteDialog();
return deleteDialog;
}
/**
* Creates a new EditDialog object if editDialog is
* null.
* @return the EditDialog object.
*/
public EditDialog getEditDialog()
{
if(editDialog == null)
return new EditDialog();
return editDialog;
}
/**
* Creates a new AddDialog object if editDialog is
* null.<br>
* <b>NOTE:</b> see 'name' and 'edit'.
* @param name if the user wants to edit an existing bookmark,
* 'edit' should be true and 'name' should be the name of that existing
* bookmark. If the user just wants to add a new bookmark, then 'name'
* should be the DN of that bookmark and 'edit' should be false.
* @param edit true if the user wants to edit an existing
* bookmark, false if the user wants to add a new bookmark.
* @return the AddDialog object.
*/
public AddDialog getAddDialog(String name, boolean edit)
{
if(addDialog == null)
addDialog = new AddDialog(name, edit);
if(edit == true)
addDialog.setHelpLink(HelpIDs.BOOKMARK_EDIT);
else
addDialog.setHelpLink(HelpIDs.BOOKMARK_ADD);
addDialog.Help.setToolTipText(CBIntText.get("Click here for Help."));
return addDialog;
}
/**
* Deletes a bookmark from the property file.
* Updates the bookmark menu also.
* @param name the name of the bookmark to delete.
*/
public void deleteBookmark(String name)
{
propertyList.remove("dn."+name);
propertyList.remove("desc."+name);
CBUtility.writePropertyFile(bookmarkPath, propertyList, null);
// Updates the Bookmark menu items...
jxplorer.getMainMenu().updateBookmarkMenu();
}
/**
* Gets the names of all the saved bookmarks
* from the property file.
* @return a list of saved bookmark names sorted alphabetically.
*/
public Object[] getSavedBookmarkNames()
{
Enumeration keys = propertyList.keys();
ArrayList list = new ArrayList();
while (keys.hasMoreElements())
{
String key = keys.nextElement().toString();
if(key.toLowerCase().startsWith("dn"))
{
String name = key.substring(key.indexOf(".")+1);
list.add(name);
}
}
Object listOb[] = list.toArray();
// Sort the list alphabetically...
Arrays.sort(listOb, new SearchModel.StringComparator());
return listOb;
}
/**
* Makes a new CBJComboBox with the values supplied.
* @param values a list of values to go into the combo box.
* @return a new CBJComboBox object populated with the values supplied.
*/
public CBJComboBox makeComboBox(Object[] values)
{
CBJComboBox combo = new CBJComboBox(values);
combo.setRenderer(new CBBasicComboBoxRenderer(values));
combo.setPreferredSize(new Dimension(140, 20));
return combo;
}
/**
* Makes a new dialog that allows the user to add or edit a bookmark.
* @author Trudi.
*/
public class AddDialog extends CBDialog
{
/**
* The text field for the name of the bookmark.
*/
JTextField nameField = new JTextField();
/**
* The text field for the DN of the bookmark.
*/
JTextField dnField = new JTextField();
/**
* The text field for the description of the bookmark.
*/
JTextField descField = new JTextField();
/**
* The name of the bookmark that is being edited. This is used
* as a store in the case of a user renaming the edited bookmark.
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -