KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > ldifeditor > editor > text > LdifDoubleClickStrategy


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.text;
22
23
24 import org.apache.directory.ldapstudio.browser.core.model.ldif.LdifEOFPart;
25 import org.apache.directory.ldapstudio.browser.core.model.ldif.LdifFile;
26 import org.apache.directory.ldapstudio.browser.core.model.ldif.LdifInvalidPart;
27 import org.apache.directory.ldapstudio.browser.core.model.ldif.LdifPart;
28 import org.apache.directory.ldapstudio.browser.core.model.ldif.container.LdifContainer;
29 import org.apache.directory.ldapstudio.browser.core.model.ldif.lines.LdifLineBase;
30 import org.apache.directory.ldapstudio.browser.core.model.ldif.lines.LdifSepLine;
31 import org.apache.directory.ldapstudio.browser.core.model.ldif.lines.LdifValueLineBase;
32 import org.apache.directory.ldapstudio.browser.core.model.ldif.parser.LdifParser;
33 import org.apache.directory.ldapstudio.ldifeditor.LdifEditorConstants;
34 import org.apache.directory.ldapstudio.ldifeditor.LdifEditorActivator;
35
36 import org.eclipse.jface.text.BadLocationException;
37 import org.eclipse.jface.text.DefaultTextDoubleClickStrategy;
38 import org.eclipse.jface.text.IDocument;
39 import org.eclipse.jface.text.ITextDoubleClickStrategy;
40 import org.eclipse.jface.text.ITextViewer;
41 import org.eclipse.jface.text.ITypedRegion;
42
43
44 public class LdifDoubleClickStrategy implements ITextDoubleClickStrategy
45 {
46
47     private static final int OFFSET = 0;
48
49     private static final int LENGTH = 1;
50
51     /**
52      * Default double click strategy
53      */

54     private DefaultTextDoubleClickStrategy delegateDoubleClickStrategy;
55
56
57     public LdifDoubleClickStrategy()
58     {
59         this.delegateDoubleClickStrategy = new DefaultTextDoubleClickStrategy();
60     }
61
62
63     public void doubleClicked( ITextViewer viewer )
64     {
65
66         if ( !LdifEditorActivator.getDefault().getPreferenceStore().getBoolean(
67             LdifEditorConstants.PREFERENCE_LDIFEDITOR_DOUBLECLICK_USELDIFDOUBLECLICK ) )
68         {
69             delegateDoubleClickStrategy.doubleClicked( viewer );
70         }
71         else
72         {
73
74             int cursorPos = viewer.getSelectedRange().x;
75             if ( cursorPos < 0 )
76             {
77                 return;
78             }
79
80             try
81             {
82                 LdifParser parser = new LdifParser();
83                 IDocument document = viewer.getDocument();
84                 ITypedRegion partition = document.getPartition( cursorPos );
85
86                 // now use position relative to partition
87
int offset = partition.getOffset();
88                 int relativePos = cursorPos - offset;
89
90                 // parse partition
91
String JavaDoc s = document.get( partition.getOffset(), partition.getLength() );
92                 LdifFile model = parser.parse( s );
93                 LdifContainer container = LdifFile.getContainer( model, relativePos );
94                 if ( container != null )
95                 {
96                     LdifPart part = LdifFile.getContainerContent( container, relativePos );
97
98                     if ( part != null && !( part instanceof LdifSepLine ) && !( part instanceof LdifInvalidPart )
99                         && !( part instanceof LdifEOFPart ) )
100                     {
101
102                         // calculate selected range
103
int[] range = null;
104                         if ( part instanceof LdifValueLineBase )
105                         {
106                             LdifValueLineBase line = ( LdifValueLineBase ) part;
107                             range = getRange( relativePos, part.getOffset(), new String JavaDoc[]
108                                 { line.getRawLineStart(), line.getRawValueType(), line.getRawValue() } );
109                         }
110                         else if ( part instanceof LdifLineBase )
111                         {
112                             LdifLineBase line = ( LdifLineBase ) part;
113                             range = new int[]
114                                 { part.getOffset(), part.getLength() - line.getRawNewLine().length() };
115                         }
116
117                         // set range on viewer, add global offset
118
int start = range != null ? range[OFFSET] : part.getOffset();
119                         start += offset;
120                         int length = range != null ? range[LENGTH] : part.getLength();
121                         viewer.setSelectedRange( start, length );
122                     }
123                     else
124                     {
125                         // use default double click strategy
126
delegateDoubleClickStrategy.doubleClicked( viewer );
127                     }
128                 }
129
130             }
131             catch ( BadLocationException e )
132             {
133                 e.printStackTrace();
134             }
135         }
136     }
137
138
139     private int[] getRange( int pos, int offset, String JavaDoc[] parts )
140     {
141
142         for ( int i = 0; i < parts.length; i++ )
143         {
144             if ( parts[i] != null )
145             {
146                 if ( pos < offset + parts[i].length() )
147                 {
148                     return new int[]
149                         { offset, parts[i].length() };
150                 }
151                 offset += parts[i].length();
152             }
153         }
154         return null;
155     }
156
157 }
158
Popular Tags