KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > propertiesfileeditor > PropertyKeyHyperlinkDetector


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.jdt.internal.ui.propertiesfileeditor;
13
14 import java.io.ByteArrayInputStream JavaDoc;
15 import java.io.IOException JavaDoc;
16 import java.text.StringCharacterIterator JavaDoc;
17 import java.util.Properties JavaDoc;
18
19 import org.eclipse.jface.text.BadLocationException;
20 import org.eclipse.jface.text.BadPartitioningException;
21 import org.eclipse.jface.text.IDocument;
22 import org.eclipse.jface.text.IDocumentExtension3;
23 import org.eclipse.jface.text.IRegion;
24 import org.eclipse.jface.text.ITextViewer;
25 import org.eclipse.jface.text.ITypedRegion;
26 import org.eclipse.jface.text.Region;
27 import org.eclipse.jface.text.hyperlink.AbstractHyperlinkDetector;
28 import org.eclipse.jface.text.hyperlink.IHyperlink;
29
30 import org.eclipse.ui.IEditorSite;
31 import org.eclipse.ui.IFileEditorInput;
32 import org.eclipse.ui.IStorageEditorInput;
33 import org.eclipse.ui.texteditor.ITextEditor;
34
35
36 /**
37  * Properties key hyperlink detector.
38  * <p>
39  * XXX: This does not work for properties files coming from a JAR due to
40  * missing J Core functionality. For details see:
41  * https://bugs.eclipse.org/bugs/show_bug.cgi?id=22376
42  * </p>
43  *
44  * @since 3.1
45  */

46 public class PropertyKeyHyperlinkDetector extends AbstractHyperlinkDetector {
47
48     /*
49      * @see org.eclipse.jface.text.hyperlink.IHyperlinkDetector#detectHyperlinks(org.eclipse.jface.text.ITextViewer, org.eclipse.jface.text.IRegion, boolean)
50      */

51     public IHyperlink[] detectHyperlinks(ITextViewer textViewer, IRegion region, boolean canShowMultipleHyperlinks) {
52         ITextEditor textEditor= (ITextEditor)getAdapter(ITextEditor.class);
53         if (region == null || textEditor == null || canShowMultipleHyperlinks)
54             return null;
55
56         IEditorSite site= textEditor.getEditorSite();
57         if (site == null)
58             return null;
59
60         int offset= region.getOffset();
61         if (!checkEnabled(textEditor, offset))
62             return null;
63
64         ITypedRegion partition= null;
65         try {
66             IStorageEditorInput storageEditorInput= (IStorageEditorInput)textEditor.getEditorInput();
67             IDocument document= textEditor.getDocumentProvider().getDocument(storageEditorInput);
68             if (document instanceof IDocumentExtension3)
69                 partition= ((IDocumentExtension3)document).getPartition(IPropertiesFilePartitions.PROPERTIES_FILE_PARTITIONING, offset, false);
70
71             // Check whether it is the correct partition
72
if (partition == null || !IDocument.DEFAULT_CONTENT_TYPE.equals(partition.getType())) {
73                 return null;
74             }
75
76             // Check whether the partition covers the selection
77
if (offset + region.getLength() > partition.getOffset() + partition.getLength()) {
78                 return null;
79             }
80
81             // Extract the key from the partition (which contains key and assignment
82
String JavaDoc key= document.get(partition.getOffset(), partition.getLength());
83
84             String JavaDoc realKey= key.trim();
85             int delta= key.indexOf(realKey);
86
87             String JavaDoc unicodeKey= getUnicodeString(realKey);
88             // Check whether the key is valid
89
Properties JavaDoc properties= new Properties JavaDoc();
90             properties.load(new ByteArrayInputStream JavaDoc(document.get().getBytes()));
91             if (properties.getProperty(unicodeKey) == null) {
92                 return null;
93             }
94
95             return new PropertyKeyHyperlink[] {new PropertyKeyHyperlink(new Region(partition.getOffset() + delta, realKey.length()), realKey, textEditor)};
96
97         } catch (BadLocationException ex) {
98             return null;
99         } catch (BadPartitioningException ex) {
100             return null;
101         } catch (IOException JavaDoc ex) {
102             return null;
103         }
104     }
105
106     private String JavaDoc getUnicodeString(String JavaDoc key) {
107         StringCharacterIterator JavaDoc iter= new StringCharacterIterator JavaDoc(key);
108         StringBuffer JavaDoc result= new StringBuffer JavaDoc();
109         while (iter.getIndex() < iter.getEndIndex()) {
110             char c= iter.current();
111             if (c == '\\') {
112                 iter.next();
113                 c= iter.current();
114                 if (c == 'u') {
115                     StringBuffer JavaDoc unicode= new StringBuffer JavaDoc();
116                     unicode.append(iter.next());
117                     unicode.append(iter.next());
118                     unicode.append(iter.next());
119                     unicode.append(iter.next());
120                     c= (char)Integer.parseInt(unicode.toString(), 16);
121                 }
122             }
123             result.append(c);
124             iter.next();
125         }
126         return result.toString();
127     }
128
129     private boolean checkEnabled(ITextEditor textEditor, int offset) {
130         if (offset < 0)
131             return false;
132
133          // XXX: Must be changed to IStorageEditorInput once support for JARs is available (see class Javadoc for details)
134
return textEditor.getEditorInput() instanceof IFileEditorInput;
135     }
136 }
137
Popular Tags