1 20 21 package org.apache.directory.ldapstudio.ldifeditor.editor.actions; 22 23 24 import org.apache.directory.ldapstudio.browser.common.BrowserCommonConstants; 25 import org.apache.directory.ldapstudio.browser.common.wizards.AttributeWizard; 26 import org.apache.directory.ldapstudio.browser.core.internal.model.DummyConnection; 27 import org.apache.directory.ldapstudio.browser.core.internal.model.DummyEntry; 28 import org.apache.directory.ldapstudio.browser.core.model.DN; 29 import org.apache.directory.ldapstudio.browser.core.model.IConnection; 30 import org.apache.directory.ldapstudio.browser.core.model.IEntry; 31 import org.apache.directory.ldapstudio.browser.core.model.ModelModificationException; 32 import org.apache.directory.ldapstudio.browser.core.model.NameException; 33 import org.apache.directory.ldapstudio.browser.core.model.ldif.LdifPart; 34 import org.apache.directory.ldapstudio.browser.core.model.ldif.container.LdifChangeAddRecord; 35 import org.apache.directory.ldapstudio.browser.core.model.ldif.container.LdifChangeModifyRecord; 36 import org.apache.directory.ldapstudio.browser.core.model.ldif.container.LdifContainer; 37 import org.apache.directory.ldapstudio.browser.core.model.ldif.container.LdifContentRecord; 38 import org.apache.directory.ldapstudio.browser.core.model.ldif.container.LdifModSpec; 39 import org.apache.directory.ldapstudio.browser.core.model.ldif.lines.LdifAttrValLine; 40 import org.apache.directory.ldapstudio.browser.core.model.ldif.lines.LdifModSpecSepLine; 41 import org.apache.directory.ldapstudio.browser.core.model.ldif.lines.LdifModSpecTypeLine; 42 import org.apache.directory.ldapstudio.browser.core.model.ldif.lines.LdifValueLineBase; 43 import org.apache.directory.ldapstudio.browser.core.model.schema.Schema; 44 import org.apache.directory.ldapstudio.browser.core.utils.ModelConverter; 45 import org.apache.directory.ldapstudio.ldifeditor.editor.LdifEditor; 46 import org.apache.directory.ldapstudio.valueeditors.ValueEditorManager; 47 import org.eclipse.jface.dialogs.Dialog; 48 import org.eclipse.jface.text.BadLocationException; 49 import org.eclipse.jface.text.IDocument; 50 import org.eclipse.jface.wizard.WizardDialog; 51 import org.eclipse.swt.widgets.Display; 52 53 54 public class EditLdifAttributeAction extends AbstractLdifAction 55 { 56 57 ValueEditorManager manager; 58 59 60 public EditLdifAttributeAction( LdifEditor editor ) 61 { 62 super( "Edit Attribute Description", editor ); 63 super.setActionDefinitionId( BrowserCommonConstants.ACTION_ID_EDIT_ATTRIBUTE_DESCRIPTION ); 64 65 manager = new ValueEditorManager( editor.getSite().getShell() ); 66 } 67 68 69 public void update() 70 { 71 LdifContainer[] containers = getSelectedLdifContainers(); 72 LdifModSpec modSpec = getSelectedLdifModSpec(); 73 LdifPart[] parts = getSelectedLdifParts(); 74 75 super 76 .setEnabled( parts.length == 1 77 && ( parts[0] instanceof LdifAttrValLine || modSpec != null ) 78 && containers.length == 1 79 && ( containers[0] instanceof LdifContentRecord || containers[0] instanceof LdifChangeAddRecord || containers[0] instanceof LdifChangeModifyRecord ) ); 80 } 81 82 83 protected void doRun() 84 { 85 86 LdifContainer[] containers = getSelectedLdifContainers(); 87 LdifModSpec modSpec = getSelectedLdifModSpec(); 88 LdifPart[] parts = getSelectedLdifParts(); 89 if ( parts.length == 1 90 && ( parts[0] instanceof LdifAttrValLine || parts[0] instanceof LdifModSpecTypeLine ) 91 && containers.length == 1 92 && ( containers[0] instanceof LdifContentRecord || containers[0] instanceof LdifChangeAddRecord || containers[0] instanceof LdifChangeModifyRecord ) ) 93 { 94 try 95 { 96 LdifValueLineBase line = ( LdifValueLineBase ) parts[0]; 97 String attributeDescription = null; 98 String oldValue = null; 99 if ( modSpec != null && line instanceof LdifModSpecTypeLine ) 100 { 101 LdifModSpecTypeLine oldLine = ( LdifModSpecTypeLine ) line; 102 attributeDescription = oldLine.getUnfoldedAttributeDescription(); 103 oldValue = null; 104 } 105 else 106 { 107 LdifAttrValLine oldLine = ( LdifAttrValLine ) line; 108 attributeDescription = oldLine.getUnfoldedAttributeDescription(); 109 oldValue = oldLine.getValueAsString(); 110 } 111 112 Schema schema = editor.getConnection() != null ? editor.getConnection().getSchema() 113 : Schema.DEFAULT_SCHEMA; 114 IConnection dummyConnection = new DummyConnection( schema ); 115 116 IEntry dummyEntry = null; 117 if ( containers[0] instanceof LdifContentRecord ) 118 { 119 dummyEntry = ModelConverter.ldifContentRecordToEntry( ( LdifContentRecord ) containers[0], 120 dummyConnection ); 121 } 122 else if ( containers[0] instanceof LdifChangeAddRecord ) 123 { 124 dummyEntry = ModelConverter.ldifChangeAddRecordToEntry( ( LdifChangeAddRecord ) containers[0], 125 dummyConnection ); 126 } 127 else if ( containers[0] instanceof LdifChangeModifyRecord ) 128 { 129 dummyEntry = new DummyEntry( new DN(), dummyConnection ); 130 } 131 132 AttributeWizard wizard = new AttributeWizard( "Edit Attribute Description", true, false, 133 attributeDescription, dummyEntry ); 134 WizardDialog dialog = new WizardDialog( Display.getDefault().getActiveShell(), wizard ); 135 dialog.setBlockOnOpen( true ); 136 dialog.create(); 137 if ( dialog.open() == Dialog.OK ) 138 { 139 String newAttributeDescription = wizard.getAttributeDescription(); 140 141 if ( newAttributeDescription != null ) 142 { 143 IDocument document = editor.getDocumentProvider().getDocument( editor.getEditorInput() ); 144 145 if ( modSpec != null ) 146 { 147 LdifModSpecTypeLine oldTypeLine = modSpec.getModSpecType(); 148 LdifModSpecTypeLine newTypeLine = null; 149 if ( oldTypeLine.isAdd() ) 150 { 151 newTypeLine = LdifModSpecTypeLine.createAdd( newAttributeDescription ); 152 } 153 else if ( oldTypeLine.isDelete() ) 154 { 155 newTypeLine = LdifModSpecTypeLine.createDelete( newAttributeDescription ); 156 } 157 else if ( oldTypeLine.isReplace() ) 158 { 159 newTypeLine = LdifModSpecTypeLine.createReplace( newAttributeDescription ); 160 } 161 162 LdifAttrValLine[] oldAttrValLines = modSpec.getAttrVals(); 163 LdifAttrValLine[] newAttrValLines = new LdifAttrValLine[oldAttrValLines.length]; 164 for ( int i = 0; i < oldAttrValLines.length; i++ ) 165 { 166 LdifAttrValLine oldAttrValLine = oldAttrValLines[i]; 167 newAttrValLines[i] = LdifAttrValLine.create( newAttributeDescription, oldAttrValLine 168 .getValueAsString() ); 169 170 } 171 172 LdifModSpecSepLine newSepLine = LdifModSpecSepLine.create(); 173 174 String text = newTypeLine.toFormattedString(); 175 for ( int j = 0; j < newAttrValLines.length; j++ ) 176 { 177 text += newAttrValLines[j].toFormattedString(); 178 } 179 text += newSepLine.toFormattedString(); 180 try 181 { 182 document.replace( modSpec.getOffset(), modSpec.getLength(), text ); 183 } 184 catch ( BadLocationException e ) 185 { 186 e.printStackTrace(); 187 } 188 189 } 190 else 191 { LdifAttrValLine newLine = LdifAttrValLine.create( newAttributeDescription, oldValue ); 194 try 195 { 196 document.replace( line.getOffset(), line.getLength(), newLine.toFormattedString() ); 197 } 198 catch ( BadLocationException e ) 199 { 200 e.printStackTrace(); 201 } 202 } 203 } 204 205 } 207 } 208 catch ( NameException e ) 209 { 210 } 211 catch ( ModelModificationException e ) 212 { 213 } 214 } 215 } 216 217 } 218 | Popular Tags |