KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > javaeditor > NLSKeyHyperlink


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 package org.eclipse.jdt.internal.ui.javaeditor;
12
13 import org.eclipse.core.runtime.Assert;
14
15 import org.eclipse.core.resources.IStorage;
16
17 import org.eclipse.swt.widgets.Display;
18
19 import org.eclipse.jface.text.BadLocationException;
20 import org.eclipse.jface.text.BadPartitioningException;
21 import org.eclipse.jface.text.FindReplaceDocumentAdapter;
22 import org.eclipse.jface.text.IDocument;
23 import org.eclipse.jface.text.IDocumentExtension3;
24 import org.eclipse.jface.text.IRegion;
25 import org.eclipse.jface.text.ITypedRegion;
26 import org.eclipse.jface.text.hyperlink.IHyperlink;
27
28 import org.eclipse.ui.IEditorInput;
29 import org.eclipse.ui.IEditorPart;
30 import org.eclipse.ui.PartInitException;
31 import org.eclipse.ui.texteditor.IEditorStatusLine;
32 import org.eclipse.ui.texteditor.ITextEditor;
33
34 import org.eclipse.jdt.core.JavaModelException;
35 import org.eclipse.jdt.core.dom.ITypeBinding;
36
37 import org.eclipse.jdt.internal.corext.refactoring.nls.AccessorClassReference;
38 import org.eclipse.jdt.internal.corext.refactoring.nls.NLSHintHelper;
39 import org.eclipse.jdt.internal.corext.refactoring.nls.PropertyFileDocumentModel;
40 import org.eclipse.jdt.internal.corext.util.Messages;
41
42 import org.eclipse.jdt.internal.ui.propertiesfileeditor.IPropertiesFilePartitions;
43 import org.eclipse.jdt.internal.ui.propertiesfileeditor.PropertyKeyHyperlinkDetector;
44
45
46 /**
47  * NLS key hyperlink.
48  *
49  * @since 3.1
50  */

51 public class NLSKeyHyperlink implements IHyperlink {
52
53     private IRegion fRegion;
54     private AccessorClassReference fAccessorClassReference;
55     private IEditorPart fEditor;
56     private final String JavaDoc fKeyName;
57
58
59     /**
60      * Creates a new NLS key hyperlink.
61      *
62      * @param region
63      * @param keyName
64      * @param ref
65      * @param editor the editor which contains the hyperlink
66      */

67     public NLSKeyHyperlink(IRegion region, String JavaDoc keyName, AccessorClassReference ref, IEditorPart editor) {
68         Assert.isNotNull(region);
69         Assert.isNotNull(keyName);
70         Assert.isNotNull(ref);
71         Assert.isNotNull(editor);
72
73         fRegion= region;
74         fKeyName= keyName;
75         fAccessorClassReference= ref;
76         fEditor= editor;
77     }
78
79     /*
80      * @see org.eclipse.jdt.internal.ui.javaeditor.IHyperlink#getHyperlinkRegion()
81      */

82     public IRegion getHyperlinkRegion() {
83         return fRegion;
84     }
85
86     /*
87      * @see org.eclipse.jdt.internal.ui.javaeditor.IHyperlink#open()
88      */

89     public void open() {
90         IStorage propertiesFile= null;
91         try {
92             ITypeBinding typeBinding= fAccessorClassReference.getBinding();
93             propertiesFile= NLSHintHelper.getResourceBundle(typeBinding.getJavaElement().getJavaProject(), fAccessorClassReference);
94         } catch (JavaModelException e) {
95             // Don't open the file
96
}
97         if (propertiesFile == null) {
98             showErrorInStatusLine(fEditor, JavaEditorMessages.Editor_OpenPropertiesFile_error_fileNotFound_dialogMessage);
99             return;
100         }
101
102         IEditorPart editor;
103         try {
104             editor= EditorUtility.openInEditor(propertiesFile, true);
105         } catch (PartInitException e) {
106             handleOpenPropertiesFileFailed(propertiesFile);
107             return;
108         } catch (JavaModelException e) {
109             handleOpenPropertiesFileFailed(propertiesFile);
110             return;
111         }
112
113         // Reveal the key in the properties file
114
if (editor instanceof ITextEditor) {
115             IRegion region= null;
116             boolean found= false;
117
118             // Find key in document
119
IEditorInput editorInput= editor.getEditorInput();
120             IDocument document= ((ITextEditor)editor).getDocumentProvider().getDocument(editorInput);
121             if (document != null) {
122                 FindReplaceDocumentAdapter finder= new FindReplaceDocumentAdapter(document);
123                 PropertyKeyHyperlinkDetector detector= new PropertyKeyHyperlinkDetector();
124                 detector.setContext(editor);
125                 String JavaDoc key= PropertyFileDocumentModel.unwindEscapeChars(fKeyName);
126                 int offset= document.getLength() - 1;
127                 try {
128                     while (!found && offset >= 0) {
129                         region= finder.find(offset, key, false, true, false, false);
130                         if (region == null)
131                             offset= -1;
132                         else {
133                             // test whether it's the key
134
IHyperlink[] hyperlinks= detector.detectHyperlinks(null, region, false);
135                             if (hyperlinks != null) {
136                                 for (int i= 0; i < hyperlinks.length; i++) {
137                                     IRegion hyperlinkRegion= hyperlinks[i].getHyperlinkRegion();
138                                     found= key.equals(document.get(hyperlinkRegion.getOffset(), hyperlinkRegion.getLength()));
139                                 }
140                             } else if (document instanceof IDocumentExtension3) {
141                                 // Fall back: test using properties file partitioning
142
ITypedRegion partition= null;
143                                 partition= ((IDocumentExtension3)document).getPartition(IPropertiesFilePartitions.PROPERTIES_FILE_PARTITIONING, region.getOffset(), false);
144                                 found= IDocument.DEFAULT_CONTENT_TYPE.equals(partition.getType())
145                                         && key.equals(document.get(partition.getOffset(), partition.getLength()).trim());
146                             }
147                             // Prevent endless loop (panic code, shouldn't be needed)
148
if (offset == region.getOffset())
149                                 offset= -1;
150                             else
151                                 offset= region.getOffset();
152                         }
153                     }
154                 } catch (BadLocationException ex) {
155                     found= false;
156                 } catch (BadPartitioningException e1) {
157                     found= false;
158                 }
159             }
160             if (found)
161                 EditorUtility.revealInEditor(editor, region);
162             else {
163                 EditorUtility.revealInEditor(editor, 0, 0);
164                 showErrorInStatusLine(editor, Messages.format(JavaEditorMessages.Editor_OpenPropertiesFile_error_keyNotFound, fKeyName));
165             }
166         }
167     }
168
169     private void showErrorInStatusLine(IEditorPart editor, final String JavaDoc message) {
170         final Display display= fEditor.getSite().getShell().getDisplay();
171         display.beep();
172         final IEditorStatusLine statusLine= (IEditorStatusLine)editor.getAdapter(IEditorStatusLine.class);
173         if (statusLine != null) {
174             display.asyncExec(new Runnable JavaDoc() {
175                 /*
176                  * @see java.lang.Runnable#run()
177                  */

178                 public void run() {
179                     statusLine.setMessage(true, message, null);
180                 }
181             });
182         }
183     }
184
185     private void handleOpenPropertiesFileFailed(IStorage propertiesFile) {
186         showErrorInStatusLine(fEditor, Messages.format(JavaEditorMessages.Editor_OpenPropertiesFile_error_openEditor_dialogMessage, propertiesFile.getFullPath().toOSString()));
187     }
188
189     /*
190      * @see org.eclipse.jdt.internal.ui.javaeditor.IHyperlink#getTypeLabel()
191      */

192     public String JavaDoc getTypeLabel() {
193         return null;
194     }
195
196     /*
197      * @see org.eclipse.jdt.internal.ui.javaeditor.IHyperlink#getHyperlinkText()
198      */

199     public String JavaDoc getHyperlinkText() {
200         return null;
201     }
202 }
203
Popular Tags