KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > ldifeditor > editor > actions > EditLdifRecordAction


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  *
19  */

20
21 package org.apache.directory.ldapstudio.ldifeditor.editor.actions;
22
23
24 import org.apache.directory.ldapstudio.browser.core.model.ldif.container.LdifChangeAddRecord;
25 import org.apache.directory.ldapstudio.browser.core.model.ldif.container.LdifContainer;
26 import org.apache.directory.ldapstudio.browser.core.model.ldif.container.LdifContentRecord;
27 import org.apache.directory.ldapstudio.browser.core.model.ldif.container.LdifRecord;
28 import org.apache.directory.ldapstudio.ldifeditor.LdifEditorConstants;
29 import org.apache.directory.ldapstudio.ldifeditor.dialogs.LdifEntryEditorDialog;
30 import org.apache.directory.ldapstudio.ldifeditor.editor.LdifEditor;
31 import org.eclipse.jface.text.IDocument;
32
33
34 public class EditLdifRecordAction extends AbstractLdifAction
35 {
36
37     public EditLdifRecordAction( LdifEditor editor )
38     {
39         super( "Edit Record", editor );
40         super.setActionDefinitionId( LdifEditorConstants.ACTION_ID_EDIT_RECORD );
41     }
42
43
44     protected void doRun()
45     {
46
47         LdifContainer[] containers = getSelectedLdifContainers();
48         if ( containers.length == 1
49             && ( containers[0] instanceof LdifContentRecord || containers[0] instanceof LdifChangeAddRecord ) )
50         {
51
52             LdifContainer container = containers[0];
53
54             LdifEntryEditorDialog dialog = null;
55             if ( container instanceof LdifContentRecord )
56             {
57                 dialog = new LdifEntryEditorDialog( editor.getEditorSite().getShell(), editor.getConnection(),
58                     ( LdifContentRecord ) container );
59             }
60             else
61             {
62                 dialog = new LdifEntryEditorDialog( editor.getEditorSite().getShell(), editor.getConnection(),
63                     ( LdifChangeAddRecord ) container );
64             }
65
66             editor.deactivateGlobalActionHandlers();
67             if ( dialog.open() == LdifEntryEditorDialog.OK )
68             {
69                 LdifRecord record = dialog.getLdifRecord();
70
71                 IDocument document = editor.getDocumentProvider().getDocument( editor.getEditorInput() );
72                 String JavaDoc old = document.get();
73                 StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
74                 sb.append( old.substring( 0, container.getOffset() ) );
75                 sb.append( record.toFormattedString() );
76                 sb.append( old.substring( container.getOffset() + container.getLength(), old.length() ) );
77                 document.set( sb.toString() );
78             }
79             editor.activateGlobalActionHandlers();
80         }
81     }
82
83
84     public void update()
85     {
86         LdifContainer[] containers = getSelectedLdifContainers();
87         super.setEnabled( containers.length == 1
88             && ( containers[0] instanceof LdifContentRecord || containers[0] instanceof LdifChangeAddRecord ) );
89     }
90
91 }
92
Popular Tags