1 package com.ca.directory.jxplorer; 2 3 import java.awt.*; 4 import java.util.*; 5 import java.util.logging.Logger ; 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 26 public class BookMarks 27 { 28 31 JXplorer jxplorer = null; 32 33 37 Properties propertyList = null; 38 39 42 final String FILE_NAME = "bookmarks.txt"; 43 44 47 48 String bookmarkPath; 49 50 53 DeleteDialog deleteDialog = null; 54 55 58 EditDialog editDialog = null; 59 60 63 AddDialog addDialog = null; 64 65 private static Logger log = Logger.getLogger(BookMarks.class.getName()); 66 67 71 public BookMarks(JXplorer jx) 72 { 73 jxplorer = jx; 74 bookmarkPath = CBUtility.getPropertyConfigPath(FILE_NAME); 75 propertyList = CBUtility.readPropertyFile(bookmarkPath); 76 } 77 78 85 protected boolean isValidName(String name) 86 { 87 try 88 { 89 if(name.trim().length()<=0) 90 return false; 91 } 92 catch(Exception e) 93 { 94 log.warning("Name '" + name + "' not specified in Bookmarks"); 95 return false; 96 } 97 98 return true; 99 } 100 101 111 protected boolean isValidDN(String 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 126 protected String getCurrentRDN(String dn) 127 { 128 DN bloop = new DN(dn); 129 return bloop.getLowestRDN().toString(); 130 } 131 132 139 protected boolean checkIfBookmarkExists(String name) 140 { 141 propertyList = CBUtility.readPropertyFile(bookmarkPath); 142 return propertyList.containsKey("dn."+name); 143 } 144 145 150 public DeleteDialog getDeleteDialog() 151 { 152 if(deleteDialog == null) 153 return new DeleteDialog(); 154 return deleteDialog; 155 } 156 157 162 public EditDialog getEditDialog() 163 { 164 if(editDialog == null) 165 return new EditDialog(); 166 return editDialog; 167 } 168 169 181 public AddDialog getAddDialog(String 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 201 public void deleteBookmark(String name) 202 { 203 propertyList.remove("dn."+name); 204 propertyList.remove("desc."+name); 205 206 CBUtility.writePropertyFile(bookmarkPath, propertyList, null); 207 208 jxplorer.getMainMenu().updateBookmarkMenu(); 210 } 211 212 217 public Object [] getSavedBookmarkNames() 218 { 219 Enumeration keys = propertyList.keys(); 220 ArrayList list = new ArrayList(); 221 while (keys.hasMoreElements()) 222 { 223 String key = keys.nextElement().toString(); 224 225 if(key.toLowerCase().startsWith("dn")) 226 { 227 String name = key.substring(key.indexOf(".")+1); 228 list.add(name); 229 } 230 } 231 232 Object listOb[] = list.toArray(); 233 234 Arrays.sort(listOb, new SearchModel.StringComparator()); 236 237 return listOb; 238 } 239 240 245 public CBJComboBox makeComboBox(Object [] 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 257 public class AddDialog extends CBDialog 258 { 259 262 JTextField nameField = new JTextField(); 263 264 267 JTextField dnField = new JTextField(); 268 269 272 JTextField descField = new JTextField(); 273 274 278 String editName = null; 279 280 284 boolean edit = false; 285 286 296 public AddDialog(String name, boolean edit) 297 { 298 super(jxplorer, CBIntText.get("Add Bookmark"), null); 299 300 this.edit = edit; 301 302 if(edit) 303 { 304 displayBookmarkDetails(name); 306 editName = name; 307 setTitle(CBIntText.get("Edit Bookmark")); 308 } 309 else 310 { 311 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 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 359 protected void displayBookmarkDetails(String 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 e) 368 { 369 CBUtility.error("Error loading '" + name + "' bookmark. The bookmark cannot be found.", e); 370 } 371 } 372 373 380 public void displayNewBookmarkDetails(String dn) 381 { 382 nameField.setText(getCurrentRDN(dn)); 383 dnField.setText(dn); 384 descField.setText(""); 385 } 386 387 396 public void doOK() 397 { 398 String name = nameField.getText(); 399 String desc = descField.getText(); 400 String dn = dnField.getText(); 401 402 try 403 { 404 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 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 [] {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 e) 451 { 452 CBUtility.error("Cannot add an empty bookmark."); 453 return; 454 } 455 456 jxplorer.getMainMenu().updateBookmarkMenu(); 458 459 JOptionPane.showMessageDialog(jxplorer, 460 CBIntText.get("The bookmark ''{0}'' was successfully saved.", 461 new String [] {name}), CBIntText.get("Save Succeeded"), 462 JOptionPane.INFORMATION_MESSAGE ); 463 464 super.doOK(); 465 } 466 467 470 public void doCancel() 471 { 472 super.doCancel(); 473 jxplorer.getMainMenu().updateBookmarkMenu(); 475 } 476 } 477 478 484 public class EditDialog 485 { 486 491 public EditDialog() 492 { 493 Object 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 bookmark = combo.getSelectedItem().toString(); 506 AddDialog ad = getAddDialog(bookmark, true); 507 ad.setVisible(true); 508 } 509 } 510 } 511 512 517 public class DeleteDialog 518 { 519 523 public DeleteDialog() 524 { 525 Object 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 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 [] {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 [] {toDelete}), CBIntText.get("Delete Succeeded"), 549 JOptionPane.INFORMATION_MESSAGE ); 550 } 551 } 552 } 553 } 554 | Popular Tags |