KickJava   Java API By Example, From Geeks To Geeks.

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


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.LdifContainer;
25 import org.apache.directory.ldapstudio.browser.core.model.ldif.container.LdifRecord;
26 import org.apache.directory.ldapstudio.ldifeditor.editor.LdifEditor;
27
28 import org.eclipse.jface.text.IDocument;
29 import org.eclipse.jface.text.source.ISourceViewer;
30
31
32 public class FormatLdifRecordAction extends AbstractLdifAction
33 {
34
35     public FormatLdifRecordAction( LdifEditor editor )
36     {
37         super( "Format Record", editor );
38     }
39
40
41     protected void doRun()
42     {
43
44         LdifContainer[] containers = super.getSelectedLdifContainers();
45         if ( containers.length > 0 )
46         {
47             IDocument document = editor.getDocumentProvider().getDocument( editor.getEditorInput() );
48             String JavaDoc old = document.get();
49             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
50             sb.append( old.substring( 0, containers[0].getOffset() ) );
51
52             for ( int i = 0; i < containers.length; i++ )
53             {
54                 LdifContainer container = containers[i];
55                 sb.append( container.toFormattedString() );
56             }
57
58             sb.append( old.substring( containers[containers.length - 1].getOffset()
59                 + containers[containers.length - 1].getLength(), old.length() ) );
60
61             ISourceViewer sourceViewer = ( ISourceViewer ) editor.getAdapter( ISourceViewer.class );
62             int topIndex = sourceViewer.getTopIndex();
63             document.set( sb.toString() );
64             sourceViewer.setTopIndex( topIndex );
65         }
66     }
67
68
69     public void update()
70     {
71         LdifContainer[] ldifContainers = super.getSelectedLdifContainers();
72         for ( int i = 0; i < ldifContainers.length; i++ )
73         {
74             LdifContainer container = ldifContainers[i];
75             if ( !( container instanceof LdifRecord ) )
76             {
77                 super.setEnabled( false );
78                 return;
79             }
80         }
81
82         super.setEnabled( true );
83     }
84
85 }
86
Popular Tags