|
Lines 1-5
Link Here
|
| 1 |
/******************************************************************************* |
1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2000, 2008 IBM Corporation and others. |
2 |
* Copyright (c) 2000, 2009 IBM Corporation and others. |
| 3 |
* All rights reserved. This program and the accompanying materials |
3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
5 |
* which accompanies this distribution, and is available at |
|
Lines 11-16
Link Here
|
| 11 |
package org.eclipse.jdt.internal.ui.javaeditor; |
11 |
package org.eclipse.jdt.internal.ui.javaeditor; |
| 12 |
|
12 |
|
| 13 |
import org.eclipse.core.runtime.Assert; |
13 |
import org.eclipse.core.runtime.Assert; |
|
|
14 |
import org.eclipse.core.runtime.IPath; |
| 15 |
|
| 16 |
import org.eclipse.core.filebuffers.FileBuffers; |
| 17 |
import org.eclipse.core.filebuffers.ITextFileBuffer; |
| 18 |
import org.eclipse.core.filebuffers.LocationKind; |
| 14 |
|
19 |
|
| 15 |
import org.eclipse.core.resources.IStorage; |
20 |
import org.eclipse.core.resources.IStorage; |
| 16 |
|
21 |
|
|
Lines 27-35
Link Here
|
| 27 |
|
32 |
|
| 28 |
import org.eclipse.ui.IEditorInput; |
33 |
import org.eclipse.ui.IEditorInput; |
| 29 |
import org.eclipse.ui.IEditorPart; |
34 |
import org.eclipse.ui.IEditorPart; |
|
|
35 |
import org.eclipse.ui.IFileEditorInput; |
| 30 |
import org.eclipse.ui.PartInitException; |
36 |
import org.eclipse.ui.PartInitException; |
| 31 |
import org.eclipse.ui.texteditor.IEditorStatusLine; |
37 |
import org.eclipse.ui.texteditor.IEditorStatusLine; |
| 32 |
import org.eclipse.ui.texteditor.ITextEditor; |
|
|
| 33 |
|
38 |
|
| 34 |
import org.eclipse.jdt.core.JavaModelException; |
39 |
import org.eclipse.jdt.core.JavaModelException; |
| 35 |
import org.eclipse.jdt.core.dom.ITypeBinding; |
40 |
import org.eclipse.jdt.core.dom.ITypeBinding; |
|
Lines 41-47
Link Here
|
| 41 |
import org.eclipse.jdt.internal.corext.util.Messages; |
46 |
import org.eclipse.jdt.internal.corext.util.Messages; |
| 42 |
|
47 |
|
| 43 |
import org.eclipse.jdt.internal.ui.propertiesfileeditor.IPropertiesFilePartitions; |
48 |
import org.eclipse.jdt.internal.ui.propertiesfileeditor.IPropertiesFilePartitions; |
| 44 |
import org.eclipse.jdt.internal.ui.propertiesfileeditor.PropertyKeyHyperlinkDetector; |
|
|
| 45 |
import org.eclipse.jdt.internal.ui.viewsupport.BasicElementLabels; |
49 |
import org.eclipse.jdt.internal.ui.viewsupport.BasicElementLabels; |
| 46 |
|
50 |
|
| 47 |
|
51 |
|
|
Lines 60-69
Link Here
|
| 60 |
|
64 |
|
| 61 |
/** |
65 |
/** |
| 62 |
* Creates a new NLS key hyperlink. |
66 |
* Creates a new NLS key hyperlink. |
| 63 |
* |
67 |
* |
| 64 |
* @param region |
68 |
* @param region the region of the link |
| 65 |
* @param keyName |
69 |
* @param keyName the name of the key |
| 66 |
* @param ref |
70 |
* @param ref the accessor class reference |
| 67 |
* @param editor the editor which contains the hyperlink |
71 |
* @param editor the editor which contains the hyperlink |
| 68 |
*/ |
72 |
*/ |
| 69 |
public NLSKeyHyperlink(IRegion region, String keyName, AccessorClassReference ref, IEditorPart editor) { |
73 |
public NLSKeyHyperlink(IRegion region, String keyName, AccessorClassReference ref, IEditorPart editor) { |
|
Lines 96-103
Link Here
|
| 96 |
} catch (JavaModelException e) { |
100 |
} catch (JavaModelException e) { |
| 97 |
// Don't open the file |
101 |
// Don't open the file |
| 98 |
} |
102 |
} |
|
|
103 |
openKeyInPropertiesFile(fKeyName, propertiesFile, fEditor); |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Calculates the region of the NLS key in the properties file and reveals it in editor. |
| 108 |
* |
| 109 |
* @param keyName the NLS key |
| 110 |
* @param propertiesFile the properties file, or <code>null</code> |
| 111 |
* @param activeEditor the active editor part |
| 112 |
*/ |
| 113 |
public static void openKeyInPropertiesFile(String keyName, IStorage propertiesFile, IEditorPart activeEditor) { |
| 99 |
if (propertiesFile == null) { |
114 |
if (propertiesFile == null) { |
| 100 |
showErrorInStatusLine(fEditor, JavaEditorMessages.Editor_OpenPropertiesFile_error_fileNotFound_dialogMessage); |
115 |
showErrorInStatusLine(activeEditor, JavaEditorMessages.Editor_OpenPropertiesFile_error_fileNotFound_dialogMessage); |
| 101 |
return; |
116 |
return; |
| 102 |
} |
117 |
} |
| 103 |
|
118 |
|
|
Lines 105-172
Link Here
|
| 105 |
try { |
120 |
try { |
| 106 |
editor= EditorUtility.openInEditor(propertiesFile, true); |
121 |
editor= EditorUtility.openInEditor(propertiesFile, true); |
| 107 |
} catch (PartInitException e) { |
122 |
} catch (PartInitException e) { |
| 108 |
handleOpenPropertiesFileFailed(propertiesFile); |
123 |
handleOpenPropertiesFileFailed(propertiesFile, activeEditor); |
| 109 |
return; |
124 |
return; |
| 110 |
} |
125 |
} |
| 111 |
|
126 |
|
| 112 |
// Reveal the key in the properties file |
127 |
// Reveal the key in the editor |
| 113 |
if (editor instanceof ITextEditor) { |
128 |
IEditorInput input = editor.getEditorInput(); |
| 114 |
IRegion region= null; |
129 |
if (input instanceof IFileEditorInput) { |
| 115 |
boolean found= false; |
130 |
IPath path= ((IFileEditorInput)input).getFile().getFullPath(); |
| 116 |
|
131 |
ITextFileBuffer buffer= FileBuffers.getTextFileBufferManager().getTextFileBuffer( |
| 117 |
// Find key in document |
132 |
path, LocationKind.IFILE); |
| 118 |
IEditorInput editorInput= editor.getEditorInput(); |
133 |
if (buffer != null) { |
| 119 |
IDocument document= ((ITextEditor)editor).getDocumentProvider().getDocument(editorInput); |
134 |
// Find key in document |
| 120 |
if (document != null) { |
135 |
IDocument document= buffer.getDocument(); |
| 121 |
FindReplaceDocumentAdapter finder= new FindReplaceDocumentAdapter(document); |
136 |
boolean found= false; |
| 122 |
PropertyKeyHyperlinkDetector detector= new PropertyKeyHyperlinkDetector(); |
137 |
IRegion region= null; |
| 123 |
detector.setContext(editor); |
138 |
if (document != null) { |
| 124 |
String key= PropertyFileDocumentModel.unwindEscapeChars(fKeyName); |
139 |
FindReplaceDocumentAdapter finder= new FindReplaceDocumentAdapter(document); |
| 125 |
int offset= document.getLength() - 1; |
140 |
String key= PropertyFileDocumentModel.unwindEscapeChars(keyName); |
| 126 |
try { |
141 |
int offset= document.getLength() - 1; |
| 127 |
while (!found && offset >= 0) { |
142 |
try { |
| 128 |
region= finder.find(offset, key, false, true, false, false); |
143 |
while (!found && offset >= 0) { |
| 129 |
if (region == null) |
144 |
region= finder.find(offset, key, false, true, false, false); |
| 130 |
offset= -1; |
145 |
if (region == null) |
| 131 |
else { |
146 |
offset= -1; |
| 132 |
// test whether it's the key |
147 |
else { |
| 133 |
IHyperlink[] hyperlinks= detector.detectHyperlinks(null, region, false); |
148 |
// test whether it's the key |
| 134 |
if (hyperlinks != null) { |
149 |
if (document instanceof IDocumentExtension3) { |
| 135 |
for (int i= 0; i < hyperlinks.length; i++) { |
150 |
// test using properties file partitioning |
| 136 |
IRegion hyperlinkRegion= hyperlinks[i].getHyperlinkRegion(); |
151 |
ITypedRegion partition= null; |
| 137 |
found= key.equals(document.get(hyperlinkRegion.getOffset(), hyperlinkRegion.getLength())); |
152 |
partition= ((IDocumentExtension3)document).getPartition(IPropertiesFilePartitions.PROPERTIES_FILE_PARTITIONING, region.getOffset(), false); |
|
|
153 |
found= IDocument.DEFAULT_CONTENT_TYPE.equals(partition.getType()) |
| 154 |
&& key.equals(document.get(partition.getOffset(), partition.getLength()).trim()); |
| 138 |
} |
155 |
} |
| 139 |
} else if (document instanceof IDocumentExtension3) { |
156 |
// Prevent endless loop (panic code, shouldn't be needed) |
| 140 |
// Fall back: test using properties file partitioning |
157 |
if (offset == region.getOffset()) |
| 141 |
ITypedRegion partition= null; |
158 |
offset= -1; |
| 142 |
partition= ((IDocumentExtension3)document).getPartition(IPropertiesFilePartitions.PROPERTIES_FILE_PARTITIONING, region.getOffset(), false); |
159 |
else |
| 143 |
found= IDocument.DEFAULT_CONTENT_TYPE.equals(partition.getType()) |
160 |
offset= region.getOffset(); |
| 144 |
&& key.equals(document.get(partition.getOffset(), partition.getLength()).trim()); |
|
|
| 145 |
} |
161 |
} |
| 146 |
// Prevent endless loop (panic code, shouldn't be needed) |
|
|
| 147 |
if (offset == region.getOffset()) |
| 148 |
offset= -1; |
| 149 |
else |
| 150 |
offset= region.getOffset(); |
| 151 |
} |
162 |
} |
|
|
163 |
} catch (BadLocationException ex) { |
| 164 |
found= false; |
| 165 |
} catch (BadPartitioningException e1) { |
| 166 |
found= false; |
| 152 |
} |
167 |
} |
| 153 |
} catch (BadLocationException ex) { |
|
|
| 154 |
found= false; |
| 155 |
} catch (BadPartitioningException e1) { |
| 156 |
found= false; |
| 157 |
} |
168 |
} |
| 158 |
} |
169 |
if (found) |
| 159 |
if (found) |
170 |
EditorUtility.revealInEditor(editor, region); |
| 160 |
EditorUtility.revealInEditor(editor, region); |
171 |
else { |
| 161 |
else { |
172 |
EditorUtility.revealInEditor(editor, 0, 0); |
| 162 |
EditorUtility.revealInEditor(editor, 0, 0); |
173 |
showErrorInStatusLine(editor, Messages.format(JavaEditorMessages.Editor_OpenPropertiesFile_error_keyNotFound, keyName)); |
| 163 |
showErrorInStatusLine(editor, Messages.format(JavaEditorMessages.Editor_OpenPropertiesFile_error_keyNotFound, fKeyName)); |
174 |
} |
| 164 |
} |
175 |
} |
| 165 |
} |
176 |
} |
| 166 |
} |
177 |
} |
| 167 |
|
178 |
|
| 168 |
private void showErrorInStatusLine(IEditorPart editor, final String message) { |
179 |
/** |
| 169 |
final Display display= fEditor.getSite().getShell().getDisplay(); |
180 |
* Shows the given message as error on the status line. |
|
|
181 |
* |
| 182 |
* @param editor the editor part |
| 183 |
* @param message message to be displayed |
| 184 |
*/ |
| 185 |
private static void showErrorInStatusLine(IEditorPart editor, final String message) { |
| 186 |
final Display display= editor.getSite().getShell().getDisplay(); |
| 170 |
display.beep(); |
187 |
display.beep(); |
| 171 |
final IEditorStatusLine statusLine= (IEditorStatusLine)editor.getAdapter(IEditorStatusLine.class); |
188 |
final IEditorStatusLine statusLine= (IEditorStatusLine)editor.getAdapter(IEditorStatusLine.class); |
| 172 |
if (statusLine != null) { |
189 |
if (statusLine != null) { |
|
Lines 181-188
Link Here
|
| 181 |
} |
198 |
} |
| 182 |
} |
199 |
} |
| 183 |
|
200 |
|
| 184 |
private void handleOpenPropertiesFileFailed(IStorage propertiesFile) { |
201 |
/** |
| 185 |
showErrorInStatusLine(fEditor, Messages.format(JavaEditorMessages.Editor_OpenPropertiesFile_error_openEditor_dialogMessage, BasicElementLabels.getPathLabel(propertiesFile.getFullPath(), true))); |
202 |
* Shows error message in status line if opening the properties file in editor fails. |
|
|
203 |
* |
| 204 |
* @param propertiesFile the propertiesFile |
| 205 |
* @param editor the editor part |
| 206 |
*/ |
| 207 |
private static void handleOpenPropertiesFileFailed(IStorage propertiesFile, IEditorPart editor) { |
| 208 |
showErrorInStatusLine(editor, Messages.format(JavaEditorMessages.Editor_OpenPropertiesFile_error_openEditor_dialogMessage, BasicElementLabels.getPathLabel(propertiesFile.getFullPath(), true))); |
| 186 |
} |
209 |
} |
| 187 |
|
210 |
|
| 188 |
/* |
211 |
/* |