1 20 21 package org.apache.directory.ldapstudio.browser.ui.actions; 22 23 24 import java.util.Arrays ; 25 import java.util.Iterator ; 26 import java.util.LinkedHashSet ; 27 import java.util.Set ; 28 29 import org.apache.directory.ldapstudio.browser.common.actions.BrowserAction; 30 import org.apache.directory.ldapstudio.browser.common.actions.CopyAction; 31 import org.apache.directory.ldapstudio.browser.core.BrowserCoreConstants; 32 import org.apache.directory.ldapstudio.browser.core.model.DN; 33 import org.apache.directory.ldapstudio.browser.core.model.IAttribute; 34 import org.apache.directory.ldapstudio.browser.core.model.IValue; 35 import org.apache.directory.ldapstudio.browser.core.utils.LdifUtils; 36 import org.apache.directory.ldapstudio.browser.core.utils.ModelConverter; 37 import org.apache.directory.ldapstudio.browser.ui.BrowserUIConstants; 38 import org.apache.directory.ldapstudio.browser.ui.BrowserUIPlugin; 39 40 import org.eclipse.jface.resource.ImageDescriptor; 41 import org.eclipse.swt.dnd.TextTransfer; 42 import org.eclipse.swt.dnd.Transfer; 43 44 45 51 public class CopyValueAction extends BrowserAction 52 { 53 56 public static final int MODE_UTF8 = 1; 57 58 61 public static final int MODE_BASE64 = 2; 62 63 66 public static final int MODE_HEX = 3; 67 68 71 public static final int MODE_LDIF = 4; 72 73 private int mode; 74 75 76 82 public CopyValueAction( int mode ) 83 { 84 this.mode = mode; 85 } 86 87 88 91 public String getText() 92 { 93 if ( mode == MODE_UTF8 ) 94 { 95 return getValueSet().size() > 1 ? "Copy Values (UTF-8)" : "Copy Value (UTF-8)"; 96 } 97 else if ( mode == MODE_BASE64 ) 98 { 99 return getValueSet().size() > 1 ? "Copy Values (BASE-64)" : "Copy Value (BASE-64)"; 100 } 101 else if ( mode == MODE_HEX ) 102 { 103 return getValueSet().size() > 1 ? "Copy Values (HEX)" : "Copy Value (HEX)"; 104 } 105 else if ( mode == MODE_LDIF ) 106 { 107 return getValueSet().size() > 1 ? "Copy Name-Value-Pairs as LDIF" : "Copy Name-Value-Pair as LDIF"; 108 } 109 else 110 { 111 return "Copy Value"; 112 } 113 } 114 115 116 119 public ImageDescriptor getImageDescriptor() 120 { 121 if ( mode == MODE_UTF8 ) 122 { 123 return BrowserUIPlugin.getDefault().getImageDescriptor( BrowserUIConstants.IMG_COPY_UTF8 ); 124 } 125 else if ( mode == MODE_BASE64 ) 126 { 127 return BrowserUIPlugin.getDefault().getImageDescriptor( BrowserUIConstants.IMG_COPY_BASE64 ); 128 } 129 else if ( mode == MODE_HEX ) 130 { 131 return BrowserUIPlugin.getDefault().getImageDescriptor( BrowserUIConstants.IMG_COPY_HEX ); 132 } 133 else if ( mode == MODE_LDIF ) 134 { 135 return BrowserUIPlugin.getDefault().getImageDescriptor( BrowserUIConstants.IMG_COPY_LDIF ); 136 } 137 else 138 { 139 return null; 140 } 141 } 142 143 144 147 public String getCommandId() 148 { 149 return null; 150 } 151 152 153 156 public boolean isEnabled() 157 { 158 return getValueSet().size() > 0 || getSelectedSearchResults().length > 0; 159 } 160 161 162 165 public void run() 166 { 167 168 StringBuffer text = new StringBuffer (); 169 Set valueSet = getValueSet(); 170 if ( !valueSet.isEmpty() ) 171 { 172 for ( Iterator iterator = valueSet.iterator(); iterator.hasNext(); ) 173 { 174 IValue value = ( IValue ) iterator.next(); 175 176 if ( mode == MODE_UTF8 ) 177 { 178 text.append( LdifUtils.utf8decode( value.getBinaryValue() ) ); 179 if ( iterator.hasNext() ) 180 text.append( BrowserCoreConstants.LINE_SEPARATOR ); 181 } 182 else if ( mode == MODE_BASE64 ) 183 { 184 text.append( LdifUtils.base64encode( value.getBinaryValue() ) ); 185 if ( iterator.hasNext() ) 186 text.append( BrowserCoreConstants.LINE_SEPARATOR ); 187 } 188 else if ( mode == MODE_HEX ) 189 { 190 text.append( LdifUtils.hexEncode( value.getBinaryValue() ) ); 191 if ( iterator.hasNext() ) 192 text.append( BrowserCoreConstants.LINE_SEPARATOR ); 193 } 194 else if ( mode == MODE_LDIF ) 195 { 196 text.append( ModelConverter.valueToLdifAttrValLine( value ).toFormattedString() ); 197 } 198 199 } 200 } 201 else if ( getSelectedSearchResults().length > 0 ) 202 { 203 DN dn = getSelectedSearchResults()[0].getDn(); 204 205 if ( mode == MODE_UTF8 ) 206 { 207 text.append( LdifUtils.utf8decode( dn.toString().getBytes() ) ); 208 } 209 else if ( mode == MODE_BASE64 ) 210 { 211 text.append( LdifUtils.base64encode( dn.toString().getBytes() ) ); 212 } 213 else if ( mode == MODE_HEX ) 214 { 215 text.append( LdifUtils.hexEncode( dn.toString().getBytes() ) ); 216 } 217 else if ( mode == MODE_LDIF ) 218 { 219 text.append( ModelConverter.dnToLdifDnLine( dn ).toFormattedString() ); 220 } 221 } 222 223 if ( text.length() > 0 ) 224 { 225 CopyAction.copyToClipboard( new Object [] 226 { text.toString() }, new Transfer[] 227 { TextTransfer.getInstance() } ); 228 } 229 } 230 231 232 238 protected Set getValueSet() 239 { 240 Set valueSet = new LinkedHashSet (); 241 for ( int i = 0; i < getSelectedAttributeHierarchies().length; i++ ) 242 { 243 for ( Iterator it = getSelectedAttributeHierarchies()[i].iterator(); it.hasNext(); ) 244 { 245 IAttribute att = ( IAttribute ) it.next(); 246 valueSet.addAll( Arrays.asList( att.getValues() ) ); 247 } 248 } 249 for ( int i = 0; i < getSelectedAttributes().length; i++ ) 250 { 251 valueSet.addAll( Arrays.asList( getSelectedAttributes()[i].getValues() ) ); 252 } 253 valueSet.addAll( Arrays.asList( getSelectedValues() ) ); 254 return valueSet; 255 } 256 } 257 | Popular Tags |