KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ca > directory > jxplorer > BookMarks


1 package com.ca.directory.jxplorer;
2
3 import java.awt.*;
4 import java.util.*;
5 import java.util.logging.Logger JavaDoc;
6 import javax.swing.*;
7 import javax.swing.border.*;
8
9 import com.ca.commons.naming.*;
10 import com.ca.commons.cbutil.*;
11 import com.ca.directory.jxplorer.search.SearchModel;
12
13 /**
14  * This class and it's inner classes handle adding, editing and deleting of bookmarks.
15  * All the common operations are in this class whereas all the GUI setup and specific
16  * operations are in the inner classes: AddDialog, EditDialog and DeleteDialog.
17  * It uses a property file called 'bookmarks.txt' to store the bookmarks in the
18  * following format:
19  * <p>
20  * dn.name=something e.g: dn.DEMOCORP=o=DEMOCORP,c=AU<br>
21  * desc.name=something e.g: desc.DEMOCORP=A sample directory.
22  * <p>
23  * The name part (e.g: DEMOCORP) is used as the title of the bookmark in the combo box.
24  * @author Trudi.
25  */

26 public class BookMarks
27 {
28     /**
29      * The parent frame that the dialogs should centre on.
30      */

31     JXplorer jxplorer = null;
32
33     /**
34      * The bookmark property file used for reading, writing and deleting
35      * bookmarks from and to.
36      */

37     Properties propertyList = null;
38
39     /**
40      * The name of the property file.
41      */

42     final String JavaDoc FILE_NAME = "bookmarks.txt";
43
44     /*
45      * The path to the property file.
46      */

47
48     String JavaDoc bookmarkPath;
49
50     /**
51      * The dialog used for deleting a bookmark.
52      */

53     DeleteDialog deleteDialog = null;
54
55     /**
56      * The dialog used for editing a bookmark.
57      */

58     EditDialog editDialog = null;
59
60     /**
61      * The dialog used for adding a bookmark.
62      */

63     AddDialog addDialog = null;
64
65     private static Logger JavaDoc log = Logger.getLogger(BookMarks.class.getName());
66
67    /**
68     * Sets up the property list.
69     * @param jx jxplorer (parent component).
70     */

71     public BookMarks(JXplorer jx)
72     {
73         jxplorer = jx;
74         bookmarkPath = CBUtility.getPropertyConfigPath(FILE_NAME);
75         propertyList = CBUtility.readPropertyFile(bookmarkPath);
76     }
77
78     /**
79      * Checks if the Name contains only spaces. A Name is considered invalid if this is the
80      * case therefore this method will return false. Otherwise true is returned. This method
81      * can be expanded to do other checking in the future.
82      * @param name the name of the bookmark that we want to validate.
83      * @return true if the Name doesn't contains only spaces, false otherwise.
84      */

85     protected boolean isValidName(String JavaDoc name)
86     {
87         try
88         {
89             if(name.trim().length()<=0)
90                 return false;
91         }
92         catch(Exception JavaDoc e)
93         {
94             log.warning("Name '" + name + "' not specified in Bookmarks");
95             return false;
96         }
97
98         return true;
99     }
100
101     /**
102      * Checks if the DN equals 'cn=no entries' or contains only spaces.
103      * A DN is considered invalid if this is the
104      * case therefore this method will return false.
105      * Otherwise true is returned. This method
106      * can be expanded to do other checking in the future.
107      * @param dn the DN that we want to validate.
108      * @return true if the DN doesn't equal 'cn=no entries'
109      * or doesn't contains only spaces, false otherwise.
110      */

111     protected boolean isValidDN(String JavaDoc dn)
112     {
113         if(dn.equalsIgnoreCase("cn=no entries"))
114             return false;
115         else if(dn.trim().length()<=0)
116             return false;
117         else
118             return true;
119     }
120
121     /**
122      * Takes a DN and returns it's current lowest RDN.
123      * @param dn the DN which we will get the RDN from.
124      * @return the RDN of the DN.
125      */

126     protected String JavaDoc getCurrentRDN(String JavaDoc dn)
127     {
128         DN bloop = new DN(dn);
129         return bloop.getLowestRDN().toString();
130     }
131
132     /**
133      * Returns true or false depending on if a bookmark exists
134      * in the property file 'bookmarks.txt'.
135      * @param name the name of the book mark which is part of
136      * the key (i.e. dn.name).
137      * @return true if the bookmark exists, false if not.
138      */

139     protected boolean checkIfBookmarkExists(String JavaDoc name)
140     {
141         propertyList = CBUtility.readPropertyFile(bookmarkPath);
142         return propertyList.containsKey("dn."+name);
143     }
144
145     /**
146      * Creates a new DeleteDialog object if deleteDialog is
147      * null.
148      * @return the DeleteDialog object.
149      */

150     public DeleteDialog getDeleteDialog()
151     {
152         if(deleteDialog == null)
153             return new DeleteDialog();
154         return deleteDialog;
155     }
156
157     /**
158      * Creates a new EditDialog object if editDialog is
159      * null.
160      * @return the EditDialog object.
161      */

162     public EditDialog getEditDialog()
163     {
164         if(editDialog == null)
165             return new EditDialog();
166         return editDialog;
167     }
168
169     /**
170      * Creates a new AddDialog object if editDialog is
171      * null.<br>
172      * <b>NOTE:</b> see 'name' and 'edit'.
173      * @param name if the user wants to edit an existing bookmark,
174      * 'edit' should be true and 'name' should be the name of that existing
175      * bookmark. If the user just wants to add a new bookmark, then 'name'
176      * should be the DN of that bookmark and 'edit' should be false.
177      * @param edit true if the user wants to edit an existing
178      * bookmark, false if the user wants to add a new bookmark.
179      * @return the AddDialog object.
180      */

181     public AddDialog getAddDialog(String JavaDoc name, boolean edit)
182     {
183         if(addDialog == null)
184             addDialog = new AddDialog(name, edit);
185
186         if(edit == true)
187             addDialog.setHelpLink(HelpIDs.BOOKMARK_EDIT);
188         else
189             addDialog.setHelpLink(HelpIDs.BOOKMARK_ADD);
190
191         addDialog.Help.setToolTipText(CBIntText.get("Click here for Help."));
192
193         return addDialog;
194     }
195
196     /**
197      * Deletes a bookmark from the property file.
198      * Updates the bookmark menu also.
199      * @param name the name of the bookmark to delete.
200      */

201     public void deleteBookmark(String JavaDoc name)
202     {
203         propertyList.remove("dn."+name);
204         propertyList.remove("desc."+name);
205
206         CBUtility.writePropertyFile(bookmarkPath, propertyList, null);
207
208         // Updates the Bookmark menu items...
209
jxplorer.getMainMenu().updateBookmarkMenu();
210     }
211
212     /**
213      * Gets the names of all the saved bookmarks
214      * from the property file.
215      * @return a list of saved bookmark names sorted alphabetically.
216      */

217     public Object JavaDoc[] getSavedBookmarkNames()
218     {
219         Enumeration keys = propertyList.keys();
220         ArrayList list = new ArrayList();
221         while (keys.hasMoreElements())
222         {
223             String JavaDoc key = keys.nextElement().toString();
224
225             if(key.toLowerCase().startsWith("dn"))
226             {
227                 String JavaDoc name = key.substring(key.indexOf(".")+1);
228                 list.add(name);
229             }
230         }
231
232         Object JavaDoc listOb[] = list.toArray();
233
234         // Sort the list alphabetically...
235
Arrays.sort(listOb, new SearchModel.StringComparator());
236
237         return listOb;
238     }
239
240     /**
241      * Makes a new CBJComboBox with the values supplied.
242      * @param values a list of values to go into the combo box.
243      * @return a new CBJComboBox object populated with the values supplied.
244      */

245     public CBJComboBox makeComboBox(Object JavaDoc[] values)
246     {
247         CBJComboBox combo = new CBJComboBox(values);
248         combo.setRenderer(new CBBasicComboBoxRenderer(values));
249         combo.setPreferredSize(new Dimension(140, 20));
250         return combo;
251     }
252
253     /**
254      * Makes a new dialog that allows the user to add or edit a bookmark.
255      * @author Trudi.
256      */

257     public class AddDialog extends CBDialog
258     {
259         /**
260          * The text field for the name of the bookmark.
261          */

262         JTextField nameField = new JTextField();
263
264         /**
265          * The text field for the DN of the bookmark.
266          */

267         JTextField dnField = new JTextField();
268
269         /**
270          * The text field for the description of the bookmark.
271          */

272         JTextField descField = new JTextField();
273
274         /**
275          * The name of the bookmark that is being edited. This is used
276          * as a store in the case of a user renaming the edited bookmark.
277          */

278         String JavaDoc editName = null;
279
280         /**
281          * A flag that represents the bookmark is being edited rather than
282          * added.
283          */

284         boolean edit = false;
285
286         /**
287          * Makes a new dialog that allows the user to add or edit a bookmark.
288          * <b>NOTE:</b> see 'name' and 'edit'.
289          * @param name if the user wants to edit an existing bookmark,
290          * 'edit' should be true and 'name' should be the name of that existing
291          * bookmark. If the user just wants to add a new bookmark, then 'name'
292          * should be the DN of that bookmark and 'edit' should be false.
293          * @param edit true if the user wants to edit an existing
294          * bookmark, false if the user wants to add a new bookmark.
295          */

296         public AddDialog(String JavaDoc name, boolean edit)
297         {
298             super(jxplorer, CBIntText.get("Add Bookmark"), null);
299
300             this.edit = edit;
301
302             if(edit)
303             {
304                 // Should be the name of a previously saved bookmark...
305
displayBookmarkDetails(name);
306                 editName = name;
307                 setTitle(CBIntText.get("Edit Bookmark"));
308             }
309             else
310             {
311                 // Should be the DN of the entry that the bookmark will represent...
312
displayNewBookmarkDetails(name);
313             }
314
315             CBPanel namePanel = new CBPanel();
316             namePanel.add(new JLabel(CBIntText.get("Bookmark Name: ")));
317             namePanel.makeWide();
318             namePanel.add(nameField);
319
320             namePanel.makeLight();
321
322             OK.setToolTipText(CBIntText.get("Click here to exit when finished."));
323             OK.setText(CBIntText.get("Save"));
324             Cancel.setToolTipText(CBIntText.get("Click here to exit."));
325             // NOTE: the Help button tooltip is set elsewhere (b/c it will be null here).
326

327             CBPanel detailsPanel = new CBPanel();
328             detailsPanel.add(new JLabel(CBIntText.get("DN: ")));
329             detailsPanel.makeWide();
330             detailsPanel.addln(dnField);
331
332             detailsPanel.makeLight();
333
334             detailsPanel.add(new JLabel(CBIntText.get("Description: " )));
335             detailsPanel.makeWide();
336
337             detailsPanel.addln(descField);
338             detailsPanel.setBorder(new TitledBorder(CBIntText.get(" Bookmark Properties ")));
339
340             display.makeWide();
341             display.addln(namePanel);
342             display.addln(detailsPanel);
343
344             setSize(480, 200);
345             CBUtility.center(this, jxplorer);
346         }
347
348         public JButton getHelpButton()
349         {
350             return Help;
351         }
352
353        /**
354         * Reads the details of the bookmark from the property file then
355         * displays the details such as it's DN and it's description
356         * in their appropriate text fields.
357         * @param name the name of the bookmark.
358         */

359         protected void displayBookmarkDetails(String JavaDoc name)
360         {
361             try
362             {
363                 nameField.setText(name);
364                 dnField.setText(propertyList.getProperty("dn."+name));
365                 descField.setText(propertyList.getProperty("desc."+name));
366             }
367             catch(Exception JavaDoc e)
368             {
369                 CBUtility.error("Error loading '" + name + "' bookmark. The bookmark cannot be found.", e);
370             }
371         }
372
373         /**
374          * Set the text fields in this dialog.<br><br>
375          * The name text field is set with the RDN of the DN supplied.<br>
376          * The dn text field is set with the DN supplied.<br>
377          * The description text field is set to blank.<br>
378          * @param dn the dn of the bookmark.
379          */

380         public void displayNewBookmarkDetails(String JavaDoc dn)
381         {
382             nameField.setText(getCurrentRDN(dn));
383             dnField.setText(dn);
384             descField.setText("");
385         }
386
387         /**
388          * Saves the bookmark to the property file after doing some
389          * basic checks.<br>
390          * The checks are if the name is valid, if the bookmark exists and
391          * if the dn is valid.<br>
392          * If the user is editing a bookmark and they change the name
393          * this method asks them if they want to delete the old bookmark.<br>
394          * The bookmark menu is updated also.
395          */

396         public void doOK()
397         {
398             String JavaDoc name = nameField.getText();
399             String JavaDoc desc = descField.getText();
400             String JavaDoc dn = dnField.getText();
401
402             try
403             {
404                 // Check if it only contains spaces...
405
if (!isValidName(name))
406                 {
407                     CBUtility.error(CBIntText.get("The bookmark you are trying to save " +
408                             "contains an invalid Name. Please check the Name then try again."));
409                     return;
410                 }
411
412                 if(checkIfBookmarkExists(name))
413                 {
414                     int response = JOptionPane.showConfirmDialog(this,
415                             CBIntText.get("Do you want to replace it?"),
416                             CBIntText.get("Bookmark Exists"), JOptionPane.OK_CANCEL_OPTION);
417
418                     if (response != JOptionPane.OK_OPTION)
419                         return;
420                 }
421
422                 // Check if the bookmark being added equals 'cn=no entries' or if it
423
// only contains spaces...
424
if (!isValidDN(dn))
425                 {
426                     CBUtility.error(CBIntText.get("The bookmark you are trying to save " +
427                             "contains an invalid DN. Please check the DN then try again."));
428                     return;
429                 }
430
431                 if(edit)
432                 {
433                     if(!name.equals(editName))
434                     {
435                         int response = JOptionPane.showConfirmDialog(this,
436                                 CBIntText.get("You have renamed ''{0}'' to ''{1}''. Do you want to delete ''{0}''?",
437                                         new String JavaDoc[] {editName,name}),
438                                 CBIntText.get("Delete Bookmark?"), JOptionPane.YES_NO_OPTION);
439
440                         if (response == JOptionPane.YES_OPTION)
441                             deleteBookmark(editName);
442                     }
443                 }
444
445                 propertyList.setProperty("dn." +name, dn);
446                 propertyList.setProperty("desc."+name, desc);
447
448                 CBUtility.writePropertyFile(bookmarkPath, propertyList, null);
449             }
450             catch(Exception JavaDoc e)
451             {
452                 CBUtility.error("Cannot add an empty bookmark.");
453                 return;
454             }
455
456             // Updates the Bookmark menu items...
457
jxplorer.getMainMenu().updateBookmarkMenu();
458
459             JOptionPane.showMessageDialog(jxplorer,
460                     CBIntText.get("The bookmark ''{0}'' was successfully saved.",
461                             new String JavaDoc[] {name}), CBIntText.get("Save Succeeded"),
462                     JOptionPane.INFORMATION_MESSAGE );
463
464             super.doOK();
465         }
466
467         /**
468          * When the user hits 'cancel', the window shuts and the bookmark menu is updated.
469          */

470         public void doCancel()
471         {
472             super.doCancel();
473             // Updates the Bookmark menu items...
474
jxplorer.getMainMenu().updateBookmarkMenu();
475         }
476     }
477
478     /**
479      * Opens a dialog that allows a user to select a bookmark
480      * to edit. Once a bookmark is selected this dialog closes
481      * and the AddDialog dialog opens.
482      * @author Trudi.
483      */

484     public class EditDialog
485     {
486         /**
487          * Opens a dialog that allows a user to select a bookmark
488          * to edit. Once a bookmark is selected this dialog closes
489          * and the AddDialog dialog opens.
490          */

491         public EditDialog()
492         {
493             Object JavaDoc bookmarks[] = getSavedBookmarkNames();
494             CBJComboBox combo = makeComboBox(bookmarks);
495             combo.setToolTipText(CBIntText.get("Select the bookmark name that you want to edit."));
496
497             int response = JOptionPane.showConfirmDialog(jxplorer, combo,
498                     CBIntText.get("Edit Bookmark"), JOptionPane.OK_CANCEL_OPTION);
499
500             if (response != JOptionPane.OK_OPTION)
501                 return;
502
503             if (combo.getSelectedItem() != null)
504             {
505                 String JavaDoc bookmark = combo.getSelectedItem().toString();
506                 AddDialog ad = getAddDialog(bookmark, true);
507                 ad.setVisible(true);
508             }
509         }
510     }
511
512     /**
513      * Opens a dialog that allows a user to select a bookmark
514      * to delete. Deletes that bookmark.
515      * @author Trudi.
516      */

517     public class DeleteDialog
518     {
519         /**
520          * Opens a dialog that allows a user to select a bookmark
521          * to delete. Deletes that bookmark.
522          */

523         public DeleteDialog()
524         {
525             Object JavaDoc bookmarks[] = getSavedBookmarkNames();
526             CBJComboBox combo = makeComboBox(bookmarks);
527             combo.setToolTipText(CBIntText.get("Select the bookmark name that you want to delete."));
528
529             int response = JOptionPane.showConfirmDialog(jxplorer, combo,
530                     CBIntText.get("Delete Bookmark"), JOptionPane.OK_CANCEL_OPTION);
531
532             if (response != JOptionPane.OK_OPTION)
533                 return;
534
535             if (combo.getSelectedItem()!=null)
536             {
537                 String JavaDoc toDelete = combo.getSelectedItem().toString();
538                 int res = JOptionPane.showConfirmDialog(jxplorer,
539                         CBIntText.get("Are you sure you want to delete the bookmark called ''{0}''?", new String JavaDoc[] {toDelete}), CBIntText.get("Confirm Delete"), JOptionPane.OK_CANCEL_OPTION);
540
541                 if (res != JOptionPane.OK_OPTION)
542                     return;
543
544                 deleteBookmark(toDelete);
545
546                 JOptionPane.showMessageDialog(jxplorer,
547                         CBIntText.get("The bookmark ''{0}'' was successfully deleted.",
548                                 new String JavaDoc[] {toDelete}), CBIntText.get("Delete Succeeded"),
549                         JOptionPane.INFORMATION_MESSAGE );
550             }
551         }
552     }
553 }
554
Popular Tags