1 20 21 package org.apache.directory.ldapstudio.browser.common.actions; 22 23 24 import org.apache.directory.ldapstudio.browser.common.dialogs.ScopeDialog; 25 import org.apache.directory.ldapstudio.browser.common.dnd.ConnectionTransfer; 26 import org.apache.directory.ldapstudio.browser.common.dnd.EntryTransfer; 27 import org.apache.directory.ldapstudio.browser.common.dnd.ValuesTransfer; 28 import org.apache.directory.ldapstudio.browser.core.BrowserCorePlugin; 29 import org.apache.directory.ldapstudio.browser.core.jobs.CopyEntriesJob; 30 import org.apache.directory.ldapstudio.browser.core.jobs.CreateValuesJob; 31 import org.apache.directory.ldapstudio.browser.core.model.IConnection; 32 import org.apache.directory.ldapstudio.browser.core.model.IEntry; 33 import org.apache.directory.ldapstudio.browser.core.model.ISearch; 34 import org.apache.directory.ldapstudio.browser.core.model.IValue; 35 import org.apache.directory.ldapstudio.browser.core.model.ldif.container.LdifContentRecord; 36 37 import org.eclipse.jface.resource.ImageDescriptor; 38 import org.eclipse.swt.dnd.Clipboard; 39 import org.eclipse.swt.dnd.Transfer; 40 import org.eclipse.swt.widgets.Display; 41 import org.eclipse.ui.ISharedImages; 42 import org.eclipse.ui.PlatformUI; 43 import org.eclipse.ui.texteditor.IWorkbenchActionDefinitionIds; 44 45 46 52 public class PasteAction extends BrowserAction 53 { 54 57 public PasteAction() 58 { 59 super(); 60 } 61 62 63 66 public String getText() 67 { 68 IConnection[] connections = getConnectionsToPaste(); 70 if ( connections != null ) 71 { 72 return connections.length > 1 ? "Paste Connections" : "Paste Connection"; 73 } 74 75 IEntry[] entries = getEntriesToPaste(); 77 if ( entries != null ) 78 { 79 return entries.length > 1 ? "Paste Entries" : "Paste Entry"; 80 } 81 82 IValue[] values = getValuesToPaste(); 84 if ( values != null ) 85 { 86 return values.length > 1 ? "Paste Values" : "Paste Value"; 87 } 88 89 return "Paste"; 90 } 91 92 93 96 public ImageDescriptor getImageDescriptor() 97 { 98 return PlatformUI.getWorkbench().getSharedImages().getImageDescriptor( ISharedImages.IMG_TOOL_PASTE ); 99 } 100 101 102 105 public String getCommandId() 106 { 107 return IWorkbenchActionDefinitionIds.PASTE; 108 } 109 110 111 114 public boolean isEnabled() 115 { 116 117 if ( getConnectionsToPaste() != null ) 119 { 120 return true; 121 } 122 123 else if ( getEntriesToPaste() != null ) 125 { 126 return true; 127 } 128 129 else if ( getValuesToPaste() != null ) 131 { 132 return true; 133 } 134 135 return false; 136 } 137 138 139 142 public void run() 143 { 144 IConnection[] connections = getConnectionsToPaste(); 146 if ( connections != null ) 147 { 148 for ( int i = 0; i < connections.length; i++ ) 149 { 150 IConnection newConnection = ( IConnection ) connections[i].clone(); 151 BrowserCorePlugin.getDefault().getConnectionManager().addConnection( newConnection ); 152 } 153 return; 154 } 155 156 IEntry[] entries = getEntriesToPaste(); 158 if ( entries != null ) 159 { 160 this.pasteEntries( getSelectedEntries()[0], entries ); 161 return; 162 } 163 164 IValue[] values = getValuesToPaste(); 166 if ( values != null ) 167 { 168 this.pasteValues( values ); 169 return; 170 } 171 172 } 173 174 175 183 private void pasteEntries( final IEntry parent, final IEntry[] entriesToPaste ) 184 { 185 186 int scope = ISearch.SCOPE_OBJECT; 187 boolean askForScope = false; 188 for ( int i = 0; i < entriesToPaste.length; i++ ) 189 { 190 if ( entriesToPaste[i].hasChildren() ) 191 { 192 askForScope = true; 193 break; 194 } 195 } 196 if ( askForScope ) 197 { 198 ScopeDialog scopeDialog = new ScopeDialog( Display.getDefault().getActiveShell(), "Select Copy Depth", 199 entriesToPaste.length > 1 ); 200 scopeDialog.open(); 201 scope = scopeDialog.getScope(); 202 } 203 204 new CopyEntriesJob( parent, entriesToPaste, scope ).execute(); 205 } 206 207 208 214 private void pasteValues( IValue[] values ) 215 { 216 IEntry entry = null; 217 if ( getSelectedAttributes().length > 0 ) 218 { 219 entry = getSelectedAttributes()[0].getEntry(); 220 } 221 else if ( getSelectedValues().length > 0 ) 222 { 223 entry = getSelectedValues()[0].getAttribute().getEntry(); 224 } 225 else if ( getSelectedEntries().length == 1 ) 226 { 227 entry = getSelectedEntries()[0]; 228 } 229 else if ( getSelectedSearchResults().length == 1 ) 230 { 231 entry = getSelectedSearchResults()[0].getEntry(); 232 } 233 else if ( getSelectedBookmarks().length == 1 ) 234 { 235 entry = getSelectedBookmarks()[0].getEntry(); 236 } 237 238 if ( entry != null ) 239 { 240 String [] attributeNames = new String [values.length]; 241 Object [] rawValues = new Object [values.length]; 242 for ( int v = 0; v < values.length; v++ ) 243 { 244 attributeNames[v] = values[v].getAttribute().getDescription(); 245 rawValues[v] = values[v].getRawValue(); 246 } 247 new CreateValuesJob( entry, attributeNames, rawValues ).execute(); 248 } 249 } 250 251 252 258 private IConnection[] getConnectionsToPaste() 259 { 260 if ( getSelectedBookmarks().length + getSelectedEntries().length + getSelectedSearchResults().length 261 + getSelectedSearches().length + getSelectedAttributes().length + getSelectedValues().length == 0 262 && getSelectedConnections().length > 0 ) 263 { 264 265 Object content = this.getFromClipboard( ConnectionTransfer.getInstance() ); 266 if ( content != null && content instanceof IConnection[] ) 267 { 268 IConnection[] connections = ( IConnection[] ) content; 269 return connections; 270 } 271 } 272 273 return null; 274 } 275 276 277 282 private IEntry[] getEntriesToPaste() 283 { 284 if ( getSelectedBookmarks().length + getSelectedSearchResults().length + getSelectedSearches().length 285 + getSelectedConnections().length + getSelectedAttributes().length + getSelectedValues().length == 0 286 && getSelectedEntries().length == 1 ) 287 { 288 289 Object content = this.getFromClipboard( EntryTransfer.getInstance() ); 290 if ( content != null && content instanceof IEntry[] ) 291 { 292 IEntry[] entries = ( IEntry[] ) content; 293 return entries; 294 } 295 } 296 297 return null; 298 } 299 300 301 307 private IValue[] getValuesToPaste() 308 { 309 if ( ( getSelectedEntries().length + getSelectedSearchResults().length + getSelectedBookmarks().length 310 + getSelectedSearches().length + getSelectedConnections().length == 0 && ( getSelectedAttributes().length 311 + getSelectedValues().length > 0 ) ) 312 || ( getSelectedAttributes().length + getSelectedValues().length + getSelectedSearchResults().length 313 + getSelectedBookmarks().length + getSelectedSearches().length + getSelectedConnections().length == 0 && ( getSelectedEntries().length == 1 ) ) 314 || ( getSelectedAttributes().length + getSelectedValues().length + getSelectedEntries().length 315 + getSelectedSearchResults().length + getSelectedSearches().length + getSelectedConnections().length == 0 && ( getSelectedBookmarks().length == 1 ) ) 316 || ( getSelectedAttributes().length + getSelectedValues().length + getSelectedEntries().length 317 + getSelectedBookmarks().length + getSelectedSearches().length + getSelectedConnections().length == 0 && ( getSelectedSearchResults().length == 1 ) ) 318 319 ) 320 { 321 322 Object content = this.getFromClipboard( ValuesTransfer.getInstance() ); 323 if ( content != null && content instanceof IValue[] ) 324 { 325 IValue[] values = ( IValue[] ) content; 326 return values; 327 } 328 } 329 330 return null; 331 } 332 333 334 342 protected Object getFromClipboard( Transfer dataType ) 343 { 344 Clipboard clipboard = null; 345 try 346 { 347 clipboard = new Clipboard( Display.getCurrent() ); 348 return clipboard.getContents( dataType ); 349 } 350 finally 351 { 352 if ( clipboard != null ) 353 clipboard.dispose(); 354 } 355 } 356 } 357 | Popular Tags |