protected void doRun()
{
LdifContainer[] containers = getSelectedLdifContainers();
LdifModSpec modSpec = getSelectedLdifModSpec();
LdifPart[] parts = getSelectedLdifParts();
if ( parts.length == 1
&& ( parts[0] instanceof LdifAttrValLine || parts[0] instanceof LdifModSpecTypeLine )
&& containers.length == 1
&& ( containers[0] instanceof LdifContentRecord || containers[0] instanceof LdifChangeAddRecord || containers[0] instanceof LdifChangeModifyRecord ) )
{
try
{
LdifValueLineBase line = ( LdifValueLineBase ) parts[0];
String attributeDescription = null;
String oldValue = null;
if ( modSpec != null && line instanceof LdifModSpecTypeLine )
{
LdifModSpecTypeLine oldLine = ( LdifModSpecTypeLine ) line;
attributeDescription = oldLine.getUnfoldedAttributeDescription();
oldValue = null;
}
else
{
LdifAttrValLine oldLine = ( LdifAttrValLine ) line;
attributeDescription = oldLine.getUnfoldedAttributeDescription();
oldValue = oldLine.getValueAsString();
}
Schema schema = editor.getConnection() != null ? editor.getConnection().getSchema()
: Schema.DEFAULT_SCHEMA;
IBrowserConnection dummyConnection = new DummyConnection( schema );
IEntry dummyEntry = null;
if ( containers[0] instanceof LdifContentRecord )
{
dummyEntry = ModelConverter.ldifContentRecordToEntry( ( LdifContentRecord ) containers[0],
dummyConnection );
}
else if ( containers[0] instanceof LdifChangeAddRecord )
{
dummyEntry = ModelConverter.ldifChangeAddRecordToEntry( ( LdifChangeAddRecord ) containers[0],
dummyConnection );
}
else if ( containers[0] instanceof LdifChangeModifyRecord )
{
dummyEntry = new DummyEntry( new Dn(), dummyConnection );
}
AttributeWizard wizard = new AttributeWizard( Messages
.getString( "EditLdifAttributeAction.EditAttributeDescription" ), true, false, //$NON-NLS-1$
attributeDescription, dummyEntry );
WizardDialog dialog = new WizardDialog( Display.getDefault().getActiveShell(), wizard );
dialog.setBlockOnOpen( true );
dialog.create();
if ( dialog.open() == Dialog.OK )
{
String newAttributeDescription = wizard.getAttributeDescription();
if ( newAttributeDescription != null )
{
IDocument document = editor.getDocumentProvider().getDocument( editor.getEditorInput() );
if ( modSpec != null )
{
LdifModSpecTypeLine oldTypeLine = modSpec.getModSpecType();
LdifModSpecTypeLine newTypeLine = null;
if ( oldTypeLine.isAdd() )
{
newTypeLine = LdifModSpecTypeLine.createAdd( newAttributeDescription );
}
else if ( oldTypeLine.isDelete() )
{
newTypeLine = LdifModSpecTypeLine.createDelete( newAttributeDescription );
}
else if ( oldTypeLine.isReplace() )
{
newTypeLine = LdifModSpecTypeLine.createReplace( newAttributeDescription );
}
LdifAttrValLine[] oldAttrValLines = modSpec.getAttrVals();
LdifAttrValLine[] newAttrValLines = new LdifAttrValLine[oldAttrValLines.length];
for ( int i = 0; i < oldAttrValLines.length; i++ )
{
LdifAttrValLine oldAttrValLine = oldAttrValLines[i];
newAttrValLines[i] = LdifAttrValLine.create( newAttributeDescription, oldAttrValLine
.getValueAsString() );
}
LdifModSpecSepLine newSepLine = LdifModSpecSepLine.create();
String text = newTypeLine.toFormattedString( Utils.getLdifFormatParameters() );
for ( int j = 0; j < newAttrValLines.length; j++ )
{
text += newAttrValLines[j].toFormattedString( Utils.getLdifFormatParameters() );
}
text += newSepLine.toFormattedString( Utils.getLdifFormatParameters() );
try
{
document.replace( modSpec.getOffset(), modSpec.getLength(), text );
}
catch ( BadLocationException e )
{
e.printStackTrace();
}