1 20 21 package org.apache.directory.ldapstudio.browser.common.actions; 22 23 24 import org.apache.directory.ldapstudio.browser.common.dialogs.RenameEntryDialog; 25 import org.apache.directory.ldapstudio.browser.core.BrowserCorePlugin; 26 import org.apache.directory.ldapstudio.browser.core.internal.model.RootDSE; 27 import org.apache.directory.ldapstudio.browser.core.jobs.RenameEntryJob; 28 import org.apache.directory.ldapstudio.browser.core.model.IBookmark; 29 import org.apache.directory.ldapstudio.browser.core.model.IConnection; 30 import org.apache.directory.ldapstudio.browser.core.model.IEntry; 31 import org.apache.directory.ldapstudio.browser.core.model.ISearch; 32 import org.apache.directory.ldapstudio.browser.core.model.RDN; 33 import org.eclipse.jface.dialogs.Dialog; 34 import org.eclipse.jface.dialogs.IInputValidator; 35 import org.eclipse.jface.dialogs.InputDialog; 36 import org.eclipse.jface.resource.ImageDescriptor; 37 import org.eclipse.ui.texteditor.IWorkbenchActionDefinitionIds; 38 39 40 46 public class RenameAction extends BrowserAction 47 { 48 52 public RenameAction() 53 { 54 super(); 55 } 56 57 58 61 public String getText() 62 { 63 64 IConnection[] connections = getConnections(); 65 IEntry[] entries = getEntries(); 66 ISearch[] searches = getSearches(); 67 IBookmark[] bookmarks = getBookmarks(); 68 69 if ( connections.length == 1 && entries.length == 0 && searches.length == 0 && bookmarks.length == 0 ) 70 { 71 return "Rename Connection..."; 72 } 73 else if ( entries.length == 1 && connections.length == 0 && searches.length == 0 && bookmarks.length == 0 ) 74 { 75 return "Rename Entry..."; 76 } 77 else if ( searches.length == 1 && connections.length == 0 && entries.length == 0 && bookmarks.length == 0 ) 78 { 79 return "Rename Search..."; 80 } 81 else if ( bookmarks.length == 1 && connections.length == 0 && entries.length == 0 && searches.length == 0 ) 82 { 83 return "Rename Bookmark..."; 84 } 85 else 86 { 87 return "Rename"; 88 } 89 } 90 91 92 95 public ImageDescriptor getImageDescriptor() 96 { 97 return null; 98 } 99 100 101 104 public String getCommandId() 105 { 106 return IWorkbenchActionDefinitionIds.RENAME; 107 } 108 109 110 113 public void run() 114 { 115 IConnection[] connections = getConnections(); 116 IEntry[] entries = getEntries(); 117 ISearch[] searches = getSearches(); 118 IBookmark[] bookmarks = getBookmarks(); 119 120 if ( connections.length == 1 && entries.length == 0 && searches.length == 0 && bookmarks.length == 0 ) 121 { 122 renameConnection( connections[0] ); 123 } 124 else if ( entries.length == 1 && connections.length == 0 && searches.length == 0 && bookmarks.length == 0 ) 125 { 126 renameEntry( entries[0] ); 127 } 128 else if ( searches.length == 1 && connections.length == 0 && entries.length == 0 && bookmarks.length == 0 ) 129 { 130 renameSearch( searches[0] ); 131 } 132 else if ( bookmarks.length == 1 && connections.length == 0 && entries.length == 0 && searches.length == 0 ) 133 { 134 renameBookmark( bookmarks[0] ); 135 } 136 } 137 138 139 142 public boolean isEnabled() 143 { 144 try 145 { 146 IConnection[] connections = getConnections(); 147 IEntry[] entries = getEntries(); 148 ISearch[] searches = getSearches(); 149 IBookmark[] bookmarks = getBookmarks(); 150 151 return connections.length + entries.length + searches.length + bookmarks.length == 1; 152 153 } 154 catch ( Exception e ) 155 { 156 return false; 157 } 158 } 159 160 161 167 protected IConnection[] getConnections() 168 { 169 if ( getSelectedConnections().length == 1 ) 170 { 171 return getSelectedConnections(); 172 } 173 else 174 { 175 return new IConnection[0]; 176 } 177 } 178 179 180 186 protected void renameConnection( final IConnection connection ) 187 { 188 IInputValidator validator = new IInputValidator() 189 { 190 public String isValid( String newName ) 191 { 192 if ( connection.getName().equals( newName ) ) 193 return null; 194 else if ( BrowserCorePlugin.getDefault().getConnectionManager().getConnection( newName ) != null ) 195 return "A connection with this name already exists."; 196 else 197 return null; 198 } 199 }; 200 201 InputDialog dialog = new InputDialog( getShell(), "Rename Connection", "New name:", connection.getName(), 202 validator ); 203 204 dialog.open(); 205 String newName = dialog.getValue(); 206 if ( newName != null ) 207 { 208 connection.setName( newName ); 209 } 210 } 211 212 213 219 protected IEntry[] getEntries() 220 { 221 222 IEntry entry = null; 223 224 if ( getSelectedEntries().length == 1 ) 225 { 226 entry = getSelectedEntries()[0]; 227 } 228 else if ( getSelectedSearchResults().length == 1 ) 229 { 230 entry = getSelectedSearchResults()[0].getEntry(); 231 } 232 else if ( getSelectedValues().length == 1 && getSelectedValues()[0].isRdnPart() ) 233 { 234 entry = getSelectedValues()[0].getAttribute().getEntry(); 235 } 236 237 if ( entry != null && !( entry instanceof RootDSE ) ) 238 { 239 return new IEntry[] 240 { entry }; 241 } 242 else 243 { 244 return new IEntry[0]; 245 } 246 } 247 248 249 255 protected void renameEntry( final IEntry entry ) 256 { 257 RenameEntryDialog renameDialog = new RenameEntryDialog( getShell(), entry ); 258 if ( renameDialog.open() == Dialog.OK ) 259 { 260 RDN newRdn = renameDialog.getRdn(); 261 boolean deleteOldRdn = renameDialog.isDeleteOldRdn(); 262 if ( newRdn != null && !newRdn.equals( entry.getRdn() ) ) 263 { 264 new RenameEntryJob( entry, newRdn, deleteOldRdn ).execute(); 265 } 266 } 267 } 268 269 270 276 protected ISearch[] getSearches() 277 { 278 if ( getSelectedSearches().length == 1 ) 279 { 280 return getSelectedSearches(); 281 } 282 else 283 { 284 return new ISearch[0]; 285 } 286 } 287 288 289 295 protected void renameSearch( final ISearch search ) 296 { 297 IInputValidator validator = new IInputValidator() 298 { 299 public String isValid( String newName ) 300 { 301 if ( search.getName().equals( newName ) ) 302 return null; 303 else if ( search.getConnection().getSearchManager().getSearch( newName ) != null ) 304 return "A connection with this name already exists."; 305 else 306 return null; 307 } 308 }; 309 310 InputDialog dialog = new InputDialog( getShell(), "Rename Search", "New name:", search.getName(), validator ); 311 312 dialog.open(); 313 String newName = dialog.getValue(); 314 if ( newName != null ) 315 { 316 search.setName( newName ); 317 } 318 } 319 320 321 327 protected IBookmark[] getBookmarks() 328 { 329 if ( getSelectedBookmarks().length == 1 ) 330 { 331 return getSelectedBookmarks(); 332 } 333 else 334 { 335 return new IBookmark[0]; 336 } 337 } 338 339 340 346 protected void renameBookmark( final IBookmark bookmark ) 347 { 348 IInputValidator validator = new IInputValidator() 349 { 350 public String isValid( String newName ) 351 { 352 if ( bookmark.getName().equals( newName ) ) 353 return null; 354 else if ( bookmark.getConnection().getBookmarkManager().getBookmark( newName ) != null ) 355 return "A bookmark with this name already exists."; 356 else 357 return null; 358 } 359 }; 360 361 InputDialog dialog = new InputDialog( getShell(), "Rename Bookmark", "New name:", bookmark.getName(), validator ); 362 363 dialog.open(); 364 String newName = dialog.getValue(); 365 if ( newName != null ) 366 { 367 bookmark.setName( newName ); 368 } 369 } 370 } 371 | Popular Tags |