1 20 21 package org.apache.directory.ldapstudio.valueeditors; 22 23 24 import java.util.ArrayList ; 25 import java.util.Arrays ; 26 import java.util.Iterator ; 27 import java.util.List ; 28 29 import org.apache.directory.ldapstudio.browser.common.BrowserCommonActivator; 30 import org.apache.directory.ldapstudio.browser.common.BrowserCommonConstants; 31 import org.apache.directory.ldapstudio.browser.core.model.AttributeHierarchy; 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.model.ModelModificationException; 37 import org.apache.directory.ldapstudio.browser.core.utils.LdifUtils; 38 import org.eclipse.jface.resource.ImageDescriptor; 39 import org.eclipse.jface.viewers.CellEditor; 40 import org.eclipse.jface.viewers.TextCellEditor; 41 42 43 51 public abstract class AbstractInPlaceStringValueEditor extends TextCellEditor implements IValueEditor 52 { 53 54 58 private TextValueEditor delegate; 59 60 61 private String name; 62 63 64 private ImageDescriptor imageDescriptor; 65 66 67 70 protected AbstractInPlaceStringValueEditor() 71 { 72 super(); 73 this.delegate = new TextValueEditor(); 74 } 75 76 77 84 protected boolean showRawValues() 85 { 86 return BrowserCommonActivator.getDefault().getPreferenceStore() 87 .getBoolean( BrowserCommonConstants.PREFERENCE_SHOW_RAW_VALUES ); 88 } 89 90 91 97 public String getDisplayValue( AttributeHierarchy attributeHierarchy ) 98 { 99 if ( attributeHierarchy == null ) 100 { 101 return "NULL"; 102 } 103 104 List <IValue> valueList = new ArrayList <IValue>(); 105 for ( Iterator it = attributeHierarchy.iterator(); it.hasNext(); ) 106 { 107 IAttribute attribute = ( IAttribute ) it.next(); 108 valueList.addAll( Arrays.asList( attribute.getValues() ) ); 109 } 110 111 StringBuffer sb = new StringBuffer (); 112 for ( Iterator <IValue> it = valueList.iterator(); it.hasNext(); ) 113 { 114 IValue value = it.next(); 115 sb.append( getDisplayValue( value ) ); 116 if ( it.hasNext() ) 117 sb.append( ", " ); 118 } 119 return sb.toString(); 120 } 121 122 123 128 public String getDisplayValue( IValue value ) 129 { 130 Object obj = this.getRawValue( value ); 131 return obj == null ? "NULL" : obj.toString(); 132 } 133 134 135 142 public Object getRawValue( AttributeHierarchy attributeHierarchy ) 143 { 144 if ( attributeHierarchy == null ) 145 { 146 return null; 147 } 148 else if ( attributeHierarchy.size() == 1 && attributeHierarchy.getAttribute().getValueSize() == 0 ) 149 { 150 if ( attributeHierarchy.getAttribute().isString() ) 151 { 152 return IValue.EMPTY_STRING_VALUE; 153 } 154 else 155 { 156 return IValue.EMPTY_BINARY_VALUE; 157 } 158 } 159 else if ( attributeHierarchy.size() == 1 && attributeHierarchy.getAttribute().getValueSize() == 1 ) 160 { 161 return getRawValue( attributeHierarchy.getAttribute().getValues()[0] ); 162 } 163 else 164 { 165 return null; 166 } 167 } 168 169 170 176 public Object getRawValue( IValue value ) 177 { 178 if ( value == null ) 179 { 180 return null; 181 } 182 else if ( value.isString() ) 183 { 184 return value.getStringValue(); 185 } 186 else if ( value.isBinary() ) 187 { 188 return isEditable( value.getBinaryValue() ) ? value.getStringValue() : null; 189 } 190 else 191 { 192 return null; 193 } 194 } 195 196 197 200 private boolean isEditable( byte[] b ) 201 { 202 if ( b == null ) 203 { 204 return false; 205 } 206 207 for ( int i = 0; i < b.length; i++ ) 208 { 209 if ( !( b[i] == '\n' || b[i] == '\r' || ( b[i] >= '\u0020' && b[i] <= '\u007F' ) ) ) 210 { 211 return false; 212 } 213 } 214 215 return true; 216 } 217 218 219 226 public Object getRawValue( IConnection connection, Object value ) 227 { 228 if ( value == null ) 229 { 230 return null; 231 } 232 else if ( value instanceof String ) 233 { 234 return value; 235 } 236 else if ( value instanceof byte[] ) 237 { 238 String s = LdifUtils.utf8decode( ( byte[] ) value ); 239 for ( int i = 0; i < s.length(); i++ ) 240 { 241 if ( Character.isISOControl( s.charAt( i ) ) && s.charAt( i ) != '\n' && s.charAt( i ) != '\r' ) 242 { 243 return null; 244 } 245 } 246 return s; 247 } 248 else 249 { 250 return null; 251 } 252 } 253 254 255 261 public Object getStringOrBinaryValue( Object rawValue ) 262 { 263 if ( rawValue == null ) 264 { 265 return null; 266 } 267 else if ( rawValue instanceof String ) 268 { 269 return rawValue; 270 } 271 else 272 { 273 return null; 274 } 275 } 276 277 278 281 public CellEditor getCellEditor() 282 { 283 return this; 284 } 285 286 287 290 protected Object doGetValue() 291 { 292 return "".equals( text.getText() ) ? null : text.getText(); 293 } 294 295 296 299 protected void doSetValue( Object value ) 300 { 301 if ( value != null && value instanceof IValue.EmptyValue ) 302 { 303 value = ( ( IValue.EmptyValue ) value ).getStringValue(); 304 } 305 super.doSetValue( value ); 306 } 307 308 309 312 public void setValueEditorName( String name ) 313 { 314 this.name = name; 315 } 316 317 318 321 public String getValueEditorName() 322 { 323 return name; 324 } 325 326 327 330 public void setValueEditorImageDescriptor( ImageDescriptor imageDescriptor ) 331 { 332 this.imageDescriptor = imageDescriptor; 333 } 334 335 336 339 public ImageDescriptor getValueEditorImageDescriptor() 340 { 341 return imageDescriptor; 342 } 343 344 345 348 public final void createValue( IEntry entry, String attributeDescription, Object newRawValue ) 349 throws ModelModificationException 350 { 351 delegate.createValue( entry, attributeDescription, newRawValue ); 352 } 353 354 355 358 public final void deleteAttribute( AttributeHierarchy ah ) throws ModelModificationException 359 { 360 delegate.deleteAttribute( ah ); 361 } 362 363 364 367 public final void deleteValue( IValue oldValue ) throws ModelModificationException 368 { 369 delegate.deleteValue( oldValue ); 370 } 371 372 373 376 public final void modifyValue( IValue oldValue, Object newRawValue ) throws ModelModificationException 377 { 378 delegate.modifyValue( oldValue, newRawValue ); 379 } 380 381 } 382 | Popular Tags |