1 20 21 package org.apache.directory.ldapstudio.browser.common.actions; 22 23 24 import java.util.Arrays ; 25 import java.util.LinkedHashSet ; 26 27 import org.apache.directory.ldapstudio.browser.common.actions.proxy.BrowserActionProxy; 28 import org.apache.directory.ldapstudio.browser.common.dnd.ConnectionTransfer; 29 import org.apache.directory.ldapstudio.browser.common.dnd.EntryTransfer; 30 import org.apache.directory.ldapstudio.browser.common.dnd.ValuesTransfer; 31 import org.apache.directory.ldapstudio.browser.core.BrowserCoreConstants; 32 import org.apache.directory.ldapstudio.browser.core.model.IAttribute; 33 import org.apache.directory.ldapstudio.browser.core.model.IConnection; 34 import org.apache.directory.ldapstudio.browser.core.model.IEntry; 35 import org.apache.directory.ldapstudio.browser.core.model.IValue; 36 import org.apache.directory.ldapstudio.browser.core.utils.LdifUtils; 37 38 import org.eclipse.jface.resource.ImageDescriptor; 39 import org.eclipse.swt.dnd.Clipboard; 40 import org.eclipse.swt.dnd.TextTransfer; 41 import org.eclipse.swt.dnd.Transfer; 42 import org.eclipse.swt.widgets.Display; 43 import org.eclipse.ui.ISharedImages; 44 import org.eclipse.ui.PlatformUI; 45 import org.eclipse.ui.texteditor.IWorkbenchActionDefinitionIds; 46 47 48 54 public class CopyAction extends BrowserAction 55 { 56 protected BrowserActionProxy pasteActionProxy; 57 58 59 65 public CopyAction( BrowserActionProxy pasteActionProxy ) 66 { 67 super(); 68 this.pasteActionProxy = pasteActionProxy; 69 } 70 71 72 75 public String getText() 76 { 77 78 IConnection[] connections = getConnections(); 80 if ( connections != null ) 81 { 82 return connections.length > 1 ? "Copy Connections" : "Copy Connection"; 83 } 84 85 IEntry[] entries = getEntries(); 87 if ( entries != null ) 88 { 89 return entries.length > 1 ? "Copy Entries / DNs" : "Copy Entry / DN"; 90 } 91 92 IValue[] values = getValues(); 94 if ( values != null ) 95 { 96 return values.length > 1 ? "Copy Values" : "Copy Value"; 97 } 98 99 return "Copy"; 100 } 101 102 103 106 public ImageDescriptor getImageDescriptor() 107 { 108 return PlatformUI.getWorkbench().getSharedImages().getImageDescriptor( ISharedImages.IMG_TOOL_COPY ); 109 } 110 111 112 115 public String getCommandId() 116 { 117 return IWorkbenchActionDefinitionIds.COPY; 118 } 119 120 121 124 public void run() 125 { 126 IConnection[] connections = getConnections(); 127 IEntry[] entries = getEntries(); 128 IValue[] values = getValues(); 129 130 if ( connections != null ) 132 { 133 copyToClipboard( new Object [] 134 { connections }, new Transfer[] 135 { ConnectionTransfer.getInstance() } ); 136 } 137 138 else if ( entries != null ) 140 { 141 StringBuffer text = new StringBuffer (); 142 for ( int i = 0; i < entries.length; i++ ) 143 { 144 text.append( entries[i].getDn().toString() ); 145 if ( i + 1 < entries.length ) 146 { 147 text.append( BrowserCoreConstants.LINE_SEPARATOR ); 148 } 149 } 150 copyToClipboard( new Object [] 151 { entries, text.toString() }, new Transfer[] 152 { EntryTransfer.getInstance(), TextTransfer.getInstance() } ); 153 } 154 155 else if ( values != null ) 157 { 158 159 StringBuffer text = new StringBuffer (); 161 162 for ( int i = 0; i < values.length; i++ ) 163 { 164 165 167 if ( values[i].isString() ) 168 { 169 text.append( values[i].getStringValue() ); 170 } 171 else if ( values[i].isBinary() ) 172 { 173 text.append( LdifUtils.base64encode( values[i].getBinaryValue() ) ); 174 } 175 if ( i + 1 < values.length ) 176 { 177 text.append( BrowserCoreConstants.LINE_SEPARATOR ); 178 } 179 } 180 181 copyToClipboard( new Object [] 182 { values, text.toString() }, new Transfer[] 183 { ValuesTransfer.getInstance(), TextTransfer.getInstance() } ); 184 } 185 186 if ( this.pasteActionProxy != null ) 188 { 189 this.pasteActionProxy.updateAction(); 190 } 191 } 192 193 194 203 public static void copyToClipboard( Object [] data, Transfer[] dataTypes ) 204 { 205 Clipboard clipboard = null; 206 try 207 { 208 clipboard = new Clipboard( Display.getCurrent() ); 209 clipboard.setContents( data, dataTypes ); 210 } 211 finally 212 { 213 if ( clipboard != null ) 214 clipboard.dispose(); 215 } 216 } 217 218 219 222 public boolean isEnabled() 223 { 224 225 if ( getConnections() != null ) 227 { 228 return true; 229 } 230 231 else if ( getEntries() != null ) 233 { 234 return true; 235 } 236 237 else if ( getValues() != null ) 239 { 240 return true; 241 } 242 243 else 244 { 245 return false; 246 } 247 } 248 249 250 256 private IConnection[] getConnections() 257 { 258 259 if ( getSelectedEntries().length + getSelectedSearchResults().length + getSelectedBookmarks().length 260 + getSelectedSearches().length + getSelectedAttributeHierarchies().length + getSelectedAttributes().length 261 + getSelectedValues().length == 0 262 && getSelectedConnections().length > 0 ) 263 { 264 return getSelectedConnections(); 265 } 266 else 267 { 268 return null; 269 } 270 } 271 272 273 279 private IEntry[] getEntries() 280 { 281 if ( getSelectedConnections().length + getSelectedSearches().length + getSelectedAttributeHierarchies().length 282 + getSelectedAttributes().length + getSelectedValues().length == 0 283 && getSelectedEntries().length + getSelectedSearchResults().length + getSelectedBookmarks().length > 0 ) 284 { 285 286 LinkedHashSet entriesSet = new LinkedHashSet (); 287 for ( int i = 0; i < getSelectedEntries().length; i++ ) 288 { 289 entriesSet.add( getSelectedEntries()[i] ); 290 } 291 for ( int i = 0; i < this.getSelectedSearchResults().length; i++ ) 292 { 293 entriesSet.add( this.getSelectedSearchResults()[i].getEntry() ); 294 } 295 for ( int i = 0; i < this.getSelectedBookmarks().length; i++ ) 296 { 297 entriesSet.add( this.getSelectedBookmarks()[i].getEntry() ); 298 } 299 return ( IEntry[] ) entriesSet.toArray( new IEntry[entriesSet.size()] ); 300 } 301 else 302 { 303 return null; 304 } 305 } 306 307 308 314 private IValue[] getValues() 315 { 316 if ( getSelectedConnections().length + getSelectedBookmarks().length + getSelectedEntries().length 317 + getSelectedSearches().length == 0 318 && getSelectedAttributeHierarchies().length + getSelectedAttributes().length + getSelectedValues().length > 0 ) 319 { 320 321 LinkedHashSet valuesSet = new LinkedHashSet (); 322 for ( int i = 0; i < this.getSelectedAttributeHierarchies().length; i++ ) 323 { 324 IAttribute[] attributes = getSelectedAttributeHierarchies()[i].getAttributes(); 325 for ( int k = 0; k < attributes.length; k++ ) 326 { 327 valuesSet.addAll( Arrays.asList( attributes[k].getValues() ) ); 328 } 329 } 330 for ( int i = 0; i < this.getSelectedAttributes().length; i++ ) 331 { 332 valuesSet.addAll( Arrays.asList( this.getSelectedAttributes()[i].getValues() ) ); 333 } 334 for ( int i = 0; i < this.getSelectedValues().length; i++ ) 335 { 336 valuesSet.add( this.getSelectedValues()[i] ); 337 } 338 return ( IValue[] ) valuesSet.toArray( new IValue[valuesSet.size()] ); 339 } 340 else 341 { 342 return null; 343 } 344 } 345 } 346 | Popular Tags |