1 20 21 package org.apache.directory.ldapstudio.ldifeditor.editor.actions; 22 23 24 import org.apache.directory.ldapstudio.browser.core.internal.model.DummyConnection; 25 import org.apache.directory.ldapstudio.browser.core.model.IConnection; 26 import org.apache.directory.ldapstudio.browser.core.model.ldif.LdifPart; 27 import org.apache.directory.ldapstudio.browser.core.model.ldif.lines.LdifAttrValLine; 28 import org.apache.directory.ldapstudio.browser.core.model.ldif.lines.LdifControlLine; 29 import org.apache.directory.ldapstudio.browser.core.model.ldif.lines.LdifDeloldrdnLine; 30 import org.apache.directory.ldapstudio.browser.core.model.ldif.lines.LdifDnLine; 31 import org.apache.directory.ldapstudio.browser.core.model.ldif.lines.LdifNewrdnLine; 32 import org.apache.directory.ldapstudio.browser.core.model.ldif.lines.LdifNewsuperiorLine; 33 import org.apache.directory.ldapstudio.browser.core.model.ldif.lines.LdifValueLineBase; 34 import org.apache.directory.ldapstudio.browser.core.model.schema.Schema; 35 import org.apache.directory.ldapstudio.ldifeditor.editor.LdifEditor; 36 import org.apache.directory.ldapstudio.valueeditors.AbstractDialogValueEditor; 37 import org.apache.directory.ldapstudio.valueeditors.IValueEditor; 38 import org.apache.directory.ldapstudio.valueeditors.ValueEditorManager; 39 import org.eclipse.jface.text.BadLocationException; 40 import org.eclipse.jface.text.IDocument; 41 42 43 public abstract class AbstractOpenValueEditorAction extends AbstractLdifAction 44 { 45 46 protected ValueEditorManager valueEditorManager; 47 48 protected IValueEditor valueEditor; 49 50 51 public AbstractOpenValueEditorAction( LdifEditor editor ) 52 { 53 super( "Edit Value", editor ); 54 valueEditorManager = editor.getValueEditorManager(); 55 } 56 57 58 public Object getValueEditor() 59 { 60 return valueEditor; 61 } 62 63 64 protected void doRun() 65 { 66 67 LdifPart[] parts = getSelectedLdifParts(); 68 if ( parts.length == 1 && ( parts[0] instanceof LdifValueLineBase ) ) 69 { 70 LdifValueLineBase line = ( LdifValueLineBase ) parts[0]; 71 72 String attributeDescription = getAttributeDescription(); 73 Object rawValue = getValueEditorRawValue(); 74 75 if ( valueEditor instanceof AbstractDialogValueEditor ) 76 { 77 AbstractDialogValueEditor cellEditor = ( AbstractDialogValueEditor ) valueEditor; 78 cellEditor.setValue( rawValue ); 79 cellEditor.activate(); 80 Object newValue = cellEditor.getValue(); 81 82 if ( newValue != null && newValue instanceof String || newValue instanceof byte[] ) 83 { 84 IDocument document = editor.getDocumentProvider().getDocument( editor.getEditorInput() ); 85 86 LdifValueLineBase newLine; 87 if ( line instanceof LdifControlLine ) 88 { 89 LdifControlLine oldControlLine = ( LdifControlLine ) line; 90 if ( newValue instanceof String ) 91 { 92 newLine = LdifControlLine.create( oldControlLine.getUnfoldedOid(), oldControlLine 93 .getUnfoldedCriticality(), ( String ) newValue ); 94 } 95 else 96 { 97 newLine = LdifControlLine.create( oldControlLine.getUnfoldedOid(), oldControlLine 98 .getUnfoldedCriticality(), ( byte[] ) newValue ); 99 } 100 } 101 else 102 { 103 if ( newValue instanceof String ) 104 { 105 newLine = LdifAttrValLine.create( attributeDescription, ( String ) newValue ); 106 } 107 else 108 { 109 newLine = LdifAttrValLine.create( attributeDescription, ( byte[] ) newValue ); 110 } 111 } 112 113 try 114 { 115 document.replace( line.getOffset(), line.getLength(), newLine.toFormattedString() ); 116 } 117 catch ( BadLocationException e ) 118 { 119 e.printStackTrace(); 120 } 121 122 } 123 } 124 } 125 } 126 127 128 protected IConnection getConnection() 129 { 130 return editor.getConnection() != null ? editor.getConnection() : new DummyConnection( Schema.DEFAULT_SCHEMA ); 131 } 132 133 134 protected Object getValueEditorRawValue() 135 { 136 Object rawValue = null; 137 Object value = getValue(); 138 if ( value != null ) 139 { 140 IConnection connection = getConnection(); 141 rawValue = valueEditor.getRawValue( connection, value ); 142 } 143 144 return rawValue; 145 } 146 147 148 protected Object getValue() 149 { 150 LdifPart[] parts = getSelectedLdifParts(); 151 Object oldValue = null; 152 if ( parts.length == 1 && ( parts[0] instanceof LdifValueLineBase ) ) 153 { 154 LdifValueLineBase line = ( LdifValueLineBase ) parts[0]; 155 oldValue = line.getValueAsObject(); 156 157 if ( line instanceof LdifControlLine ) 158 { 159 oldValue = ( ( LdifControlLine ) line ).getUnfoldedControlValue(); 160 } 161 162 } 163 return oldValue; 164 } 165 166 167 protected String getAttributeDescription() 168 { 169 String attributeDescription = null; 170 LdifPart[] parts = getSelectedLdifParts(); 171 if ( parts.length == 1 && ( parts[0] instanceof LdifValueLineBase ) ) 172 { 173 LdifValueLineBase line = ( LdifValueLineBase ) parts[0]; 174 175 if ( line instanceof LdifControlLine ) 176 { 177 attributeDescription = ""; 178 } 179 else 180 { 181 attributeDescription = line.getUnfoldedLineStart(); 182 } 183 } 184 return attributeDescription; 185 } 186 187 188 protected boolean isEditableLineSelected() 189 { 190 LdifPart[] parts = getSelectedLdifParts(); 191 boolean b = parts.length == 1 192 && ( parts[0] instanceof LdifAttrValLine || parts[0] instanceof LdifDnLine 193 || parts[0] instanceof LdifControlLine || parts[0] instanceof LdifNewrdnLine 194 || parts[0] instanceof LdifDeloldrdnLine || parts[0] instanceof LdifNewsuperiorLine ); 195 return b; 196 } 197 198 } 199 | Popular Tags |