1 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 51 public class NLSKeyHyperlink implements IHyperlink { 52 53 private IRegion fRegion; 54 private AccessorClassReference fAccessorClassReference; 55 private IEditorPart fEditor; 56 private final String fKeyName; 57 58 59 67 public NLSKeyHyperlink(IRegion region, String 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 82 public IRegion getHyperlinkRegion() { 83 return fRegion; 84 } 85 86 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 } 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 if (editor instanceof ITextEditor) { 115 IRegion region= null; 116 boolean found= false; 117 118 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 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 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 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 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 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 () { 175 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 192 public String getTypeLabel() { 193 return null; 194 } 195 196 199 public String getHyperlinkText() { 200 return null; 201 } 202 } 203 | Popular Tags |