]> git.uio.no Git - ifi-stolz-refaktor.git/blob - case-study/jdt-before/ui/org/eclipse/jdt/internal/ui/propertiesfileeditor/PropertyKeyHyperlinkDetector.java
Case Study: adding data and statistics
[ifi-stolz-refaktor.git] / case-study / jdt-before / ui / org / eclipse / jdt / internal / ui / propertiesfileeditor / PropertyKeyHyperlinkDetector.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2011 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;
15 import java.io.IOException;
16 import java.text.StringCharacterIterator;
17 import java.util.Properties;
18
19 import org.eclipse.swt.widgets.Display;
20
21 import org.eclipse.jface.text.BadLocationException;
22 import org.eclipse.jface.text.BadPartitioningException;
23 import org.eclipse.jface.text.IDocument;
24 import org.eclipse.jface.text.IDocumentExtension3;
25 import org.eclipse.jface.text.IRegion;
26 import org.eclipse.jface.text.ITextViewer;
27 import org.eclipse.jface.text.ITypedRegion;
28 import org.eclipse.jface.text.Region;
29 import org.eclipse.jface.text.hyperlink.AbstractHyperlinkDetector;
30 import org.eclipse.jface.text.hyperlink.IHyperlink;
31
32 import org.eclipse.ui.IEditorSite;
33 import org.eclipse.ui.IFileEditorInput;
34 import org.eclipse.ui.IStorageEditorInput;
35
36 import org.eclipse.ui.texteditor.IEditorStatusLine;
37 import org.eclipse.ui.texteditor.ITextEditor;
38
39
40 /**
41  * Properties key hyperlink detector.
42  * <p>
43  * XXX: This does not work for properties files coming from a JAR due to missing J Core
44  * functionality. For details see http://bugs.eclipse.org/22376
45  * </p>
46  * 
47  * @since 3.1
48  */
49 public class PropertyKeyHyperlinkDetector extends AbstractHyperlinkDetector {
50
51         /*
52          * @see org.eclipse.jface.text.hyperlink.IHyperlinkDetector#detectHyperlinks(org.eclipse.jface.text.ITextViewer, org.eclipse.jface.text.IRegion, boolean)
53          */
54         public IHyperlink[] detectHyperlinks(ITextViewer textViewer, IRegion region, boolean canShowMultipleHyperlinks) {
55                 ITextEditor textEditor= (ITextEditor)getAdapter(ITextEditor.class);
56                 if (region == null || textEditor == null)
57                         return null;
58
59                 IEditorSite site= textEditor.getEditorSite();
60                 if (site == null)
61                         return null;
62
63                 int offset= region.getOffset();
64                 if (!checkEnabled(textEditor, offset))
65                         return null;
66
67                 ITypedRegion partition= null;
68                 try {
69                         IStorageEditorInput storageEditorInput= (IStorageEditorInput)textEditor.getEditorInput();
70                         IDocument document= textEditor.getDocumentProvider().getDocument(storageEditorInput);
71                         if (document instanceof IDocumentExtension3)
72                                 partition= ((IDocumentExtension3)document).getPartition(IPropertiesFilePartitions.PROPERTIES_FILE_PARTITIONING, offset, false);
73
74                         // Check whether it is the correct partition
75                         if (partition == null || !IDocument.DEFAULT_CONTENT_TYPE.equals(partition.getType())) {
76                                 return null;
77                         }
78
79                         // Check whether the partition covers the selection
80                         if (offset + region.getLength() > partition.getOffset() + partition.getLength()) {
81                                 return null;
82                         }
83
84                         // Extract the key from the partition (which contains key and assignment
85                         String key= document.get(partition.getOffset(), partition.getLength());
86
87                         String realKey= key.trim();
88                         int delta= key.indexOf(realKey);
89
90                         String unicodeKey= getUnicodeString(realKey);
91                         // Check whether the key is valid
92                         Properties properties= new Properties();
93                         properties.load(new ByteArrayInputStream(document.get().getBytes()));
94                         if (properties.getProperty(unicodeKey) == null) {
95                                 return null;
96                         }
97
98                         return new PropertyKeyHyperlink[] { new PropertyKeyHyperlink(new Region(partition.getOffset() + delta, realKey.length()), realKey, textEditor) };
99
100                 } catch (BadLocationException ex) {
101                         return null;
102                 } catch (BadPartitioningException ex) {
103                         return null;
104                 } catch (IOException ex) {
105                         return null;
106                 } catch (IllegalArgumentException ex) {
107                         showErrorInStatusLine(ex.getLocalizedMessage(), textEditor);
108                         return null;
109                 }
110         }
111
112         private String getUnicodeString(String key) {
113                 StringCharacterIterator iter= new StringCharacterIterator(key);
114                 StringBuffer result= new StringBuffer();
115                 while (iter.getIndex() < iter.getEndIndex()) {
116                         char c= iter.current();
117                         if (c == '\\') {
118                                 iter.next();
119                                 c= iter.current();
120                                 if (c == 'u') {
121                                         StringBuffer unicode= new StringBuffer();
122                                         unicode.append(iter.next());
123                                         unicode.append(iter.next());
124                                         unicode.append(iter.next());
125                                         unicode.append(iter.next());
126                                         c= (char)Integer.parseInt(unicode.toString(), 16);
127                                 }
128                         }
129                         result.append(c);
130                         iter.next();
131                 }
132                 return result.toString();
133         }
134
135         private boolean checkEnabled(ITextEditor textEditor, int offset) {
136                 if (offset < 0)
137                         return false;
138
139                  // XXX: Must be changed to IStorageEditorInput once support for JARs is available (see class Javadoc for details)
140                 return textEditor.getEditorInput() instanceof IFileEditorInput;
141         }
142
143         private void showErrorInStatusLine(final String message, ITextEditor textEditor) {
144                 Display display= textEditor.getEditorSite().getShell().getDisplay();
145                 display.beep();
146                 final IEditorStatusLine statusLine= (IEditorStatusLine)textEditor.getAdapter(IEditorStatusLine.class);
147                 if (statusLine != null) {
148                         display.asyncExec(new Runnable() {
149                                 /*
150                                  * @see java.lang.Runnable#run()
151                                  */
152                                 public void run() {
153                                         statusLine.setMessage(true, message, null);
154                                 }
155                         });
156                 }
157         }
158 }