1 20 21 package org.apache.directory.ldapstudio.browser.ui.editors.searchresult; 22 23 24 import java.util.Iterator ; 25 26 import org.apache.directory.ldapstudio.browser.core.internal.model.Attribute; 27 import org.apache.directory.ldapstudio.browser.core.model.AttributeHierarchy; 28 import org.apache.directory.ldapstudio.browser.core.model.IAttribute; 29 import org.apache.directory.ldapstudio.browser.core.model.ISearchResult; 30 import org.apache.directory.ldapstudio.browser.core.model.ModelModificationException; 31 import org.apache.directory.ldapstudio.browser.core.model.schema.SchemaUtils; 32 import org.apache.directory.ldapstudio.browser.ui.BrowserUIConstants; 33 import org.apache.directory.ldapstudio.valueeditors.ValueEditorManager; 34 import org.eclipse.jface.dialogs.MessageDialog; 35 import org.eclipse.jface.viewers.ICellModifier; 36 import org.eclipse.jface.viewers.TableViewer; 37 import org.eclipse.swt.widgets.Item; 38 39 40 public class SearchResultEditorCellModifier implements ICellModifier 41 { 42 43 private TableViewer viewer; 44 45 private ValueEditorManager valueEditorManager; 46 47 48 public SearchResultEditorCellModifier( TableViewer viewer, ValueEditorManager valueEditorManager ) 49 { 50 this.viewer = viewer; 51 this.valueEditorManager = valueEditorManager; 52 } 53 54 55 public void dispose() 56 { 57 this.viewer = null; 58 this.valueEditorManager = null; 59 } 60 61 62 public boolean canModify( Object element, String property ) 63 { 64 65 if ( element != null && element instanceof ISearchResult && property != null ) 66 { 67 ISearchResult result = ( ISearchResult ) element; 68 AttributeHierarchy ah = result.getAttributeWithSubtypes( property ); 69 70 if ( BrowserUIConstants.DN.equals( property ) ) 72 { 73 return false; 74 } 75 76 if ( ah == null ) 78 { 79 try 80 { 81 ah = new AttributeHierarchy( result.getEntry(), property, new IAttribute[] 82 { new Attribute( result.getEntry(), property ) } ); 83 } 84 catch ( ModelModificationException e ) 85 { 86 e.printStackTrace(); 87 return false; 88 } 89 } 90 91 boolean isOneModifyable = false; 93 for ( Iterator it = ah.iterator(); it.hasNext(); ) 94 { 95 IAttribute attribute = ( IAttribute ) it.next(); 96 if ( SchemaUtils.isModifyable( attribute.getAttributeTypeDescription() ) ) 97 { 98 isOneModifyable = true; 99 break; 100 } 101 } 102 if ( !isOneModifyable ) 103 { 104 return false; 105 } 106 107 boolean isOneValid = false; 109 for ( Iterator it = ah.iterator(); it.hasNext(); ) 110 { 111 IAttribute attribute = ( IAttribute ) it.next(); 112 if ( attribute.isObjectClassAttribute() || attribute.isMustAttribute() || attribute.isMayAttribute() ) 113 { 114 isOneValid = true; 115 break; 116 } 117 } 118 if ( !isOneValid ) 119 { 120 return false; 121 } 122 123 return this.valueEditorManager.getCurrentValueEditor( ah ).getRawValue( ah ) != null; 125 } 126 else 127 { 128 return false; 129 } 130 } 131 132 133 public Object getValue( Object element, String property ) 134 { 135 136 if ( element != null && element instanceof ISearchResult && property != null ) 137 { 138 ISearchResult result = ( ISearchResult ) element; 139 AttributeHierarchy ah = result.getAttributeWithSubtypes( property ); 140 141 if ( !this.canModify( element, property ) ) 142 { 143 return null; 144 } 145 146 if ( ah == null ) 147 { 148 try 149 { 150 ah = new AttributeHierarchy( result.getEntry(), property, new IAttribute[] 151 { new Attribute( result.getEntry(), property ) } ); 152 } 153 catch ( ModelModificationException e ) 154 { 155 e.printStackTrace(); 156 return null; 157 } 158 } 159 160 return this.valueEditorManager.getCurrentValueEditor( ah ).getRawValue( ah ); 161 } 162 else 163 { 164 return null; 165 } 166 } 167 168 169 public void modify( Object element, String property, Object newRawValue ) 170 { 171 172 if ( element != null && element instanceof Item ) 173 { 174 element = ( ( Item ) element ).getData(); 175 } 176 177 if ( element != null && element instanceof ISearchResult && property != null ) 178 { 179 ISearchResult result = ( ISearchResult ) element; 180 AttributeHierarchy ah = result.getAttributeWithSubtypes( property ); 181 182 try 183 { 184 if ( ah == null && newRawValue != null ) 186 { 187 this.valueEditorManager.getCurrentValueEditor( result.getEntry(), property ).createValue( 188 result.getEntry(), property, newRawValue ); 189 } 190 else if ( ah != null && newRawValue == null ) 191 { 192 this.valueEditorManager.getCurrentValueEditor( ah ).deleteAttribute( ah ); 193 } 194 else if ( ah != null && ah.size() == 1 && ah.getAttribute().getValueSize() == 1 && newRawValue != null ) 195 { 196 this.valueEditorManager.getCurrentValueEditor( ah ).modifyValue( ah.getAttribute().getValues()[0], 197 newRawValue ); 198 } 199 } 200 catch ( ModelModificationException mme ) 201 { 202 MessageDialog.openError( this.viewer.getTable().getShell(), "Error While Modifying Value", mme 203 .getMessage() ); 204 } 205 } 206 } 207 208 } 209 | Popular Tags |