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.IValue; 34 import org.eclipse.jface.resource.ImageDescriptor; 35 import org.eclipse.jface.viewers.CellEditor; 36 import org.eclipse.swt.widgets.Composite; 37 import org.eclipse.swt.widgets.Control; 38 import org.eclipse.swt.widgets.Shell; 39 40 41 48 public abstract class AbstractDialogValueEditor extends CellEditor implements IValueEditor 49 { 50 51 52 private Object value; 53 54 55 private Shell shell; 56 57 58 private String name; 59 60 61 private ImageDescriptor imageDescriptor; 62 63 64 68 protected AbstractDialogValueEditor() 69 { 70 } 71 72 73 80 protected boolean showRawValues() 81 { 82 return BrowserCommonActivator.getDefault().getPreferenceStore() 83 .getBoolean( BrowserCommonConstants.PREFERENCE_SHOW_RAW_VALUES ); 84 } 85 86 87 92 public CellEditor getCellEditor() 93 { 94 return this; 95 } 96 97 98 104 protected final Control createControl( Composite parent ) 105 { 106 this.shell = parent.getShell(); 107 return null; 108 } 109 110 111 116 protected final void doSetFocus() 117 { 118 } 119 120 121 126 protected final Object doGetValue() 127 { 128 return this.value; 129 } 130 131 132 137 protected final void doSetValue( Object value ) 138 { 139 if ( value != null && value instanceof IValue.EmptyValue ) 140 { 141 IValue.EmptyValue emptyValue = ( IValue.EmptyValue ) value; 142 if ( emptyValue.isBinary() ) 143 value = emptyValue.getBinaryValue(); 144 else 145 value = emptyValue.getStringValue(); 146 } 147 this.value = value; 148 } 149 150 151 157 public final void activate() 158 { 159 boolean save = this.openDialog( shell ); 160 if ( !save || this.value == null ) 162 { 163 this.value = null; 164 fireCancelEditor(); 165 } 166 else 167 { 168 fireApplyEditorValue(); 169 deactivate(); 170 } 171 } 172 173 174 183 protected abstract boolean openDialog( Shell shell ); 184 185 186 192 protected abstract Object getEmptyRawValue( IAttribute attribute ); 193 194 195 201 public String getDisplayValue( AttributeHierarchy attributeHierarchy ) 202 { 203 if ( attributeHierarchy == null ) 204 { 205 return "NULL"; 206 } 207 208 List <IValue> valueList = new ArrayList <IValue>(); 209 for ( Iterator it = attributeHierarchy.iterator(); it.hasNext(); ) 210 { 211 IAttribute attribute = ( IAttribute ) it.next(); 212 valueList.addAll( Arrays.asList( attribute.getValues() ) ); 213 } 214 215 StringBuffer sb = new StringBuffer (); 216 for ( Iterator <IValue> it = valueList.iterator(); it.hasNext(); ) 217 { 218 IValue value = it.next(); 219 sb.append( getDisplayValue( value ) ); 220 if ( it.hasNext() ) 221 sb.append( ", " ); 222 } 223 return sb.toString(); 224 } 225 226 227 234 public Object getRawValue( AttributeHierarchy attributeHierarchy ) 235 { 236 if ( attributeHierarchy == null ) 237 { 238 return null; 239 } 240 else if ( attributeHierarchy.size() == 1 && attributeHierarchy.getAttribute().getValueSize() == 0 ) 241 { 242 return getEmptyRawValue( attributeHierarchy.getAttribute() ); 243 } 244 else if ( attributeHierarchy.size() == 1 && attributeHierarchy.getAttribute().getValueSize() == 1 ) 245 { 246 return getRawValue( attributeHierarchy.getAttribute().getValues()[0] ); 247 } 248 else 249 { 250 return null; 251 } 252 } 253 254 255 258 public void setValueEditorName( String name ) 259 { 260 this.name = name; 261 } 262 263 264 267 public String getValueEditorName() 268 { 269 return name; 270 } 271 272 273 276 public void setValueEditorImageDescriptor( ImageDescriptor imageDescriptor ) 277 { 278 this.imageDescriptor = imageDescriptor; 279 } 280 281 282 285 public ImageDescriptor getValueEditorImageDescriptor() 286 { 287 return imageDescriptor; 288 } 289 290 } 291 | Popular Tags |