001: /*******************************************************************************
002: * Copyright (c) 2005, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.ui.editor.text;
011:
012: import org.eclipse.core.resources.IFile;
013: import org.eclipse.core.resources.IProject;
014: import org.eclipse.jface.text.IDocument;
015: import org.eclipse.jface.text.IRegion;
016: import org.eclipse.pde.core.IModel;
017: import org.eclipse.pde.core.plugin.IPluginModelBase;
018: import org.eclipse.pde.internal.core.PDEManager;
019: import org.eclipse.pde.internal.core.ibundle.IBundleModel;
020: import org.eclipse.pde.internal.ui.PDEPlugin;
021: import org.eclipse.ui.IEditorPart;
022: import org.eclipse.ui.PartInitException;
023: import org.eclipse.ui.editors.text.TextEditor;
024: import org.eclipse.ui.ide.IDE;
025:
026: public class TranslationHyperlink extends AbstractHyperlink {
027:
028: private IModel fBase;
029:
030: private boolean fOpened;
031:
032: public TranslationHyperlink(IRegion region, String element,
033: IModel base) {
034: super (region, element);
035: fBase = base;
036: }
037:
038: private String getLocaliation() {
039: String localiz = null;
040: if (fBase instanceof IPluginModelBase)
041: localiz = PDEManager
042: .getBundleLocalization((IPluginModelBase) fBase);
043: else if (fBase instanceof IBundleModel)
044: localiz = ((IBundleModel) fBase).getBundle()
045: .getLocalization();
046: return localiz;
047: }
048:
049: public boolean getOpened() {
050: return fOpened;
051: }
052:
053: public void open() {
054: fOpened = openHyperLink();
055: }
056:
057: public boolean openHyperLink() {
058: String localiz = getLocaliation();
059: if (localiz == null) {
060: return false;
061: } else if (fBase.getUnderlyingResource() == null) {
062: return false;
063: } else if (fElement.length() == 0 || fElement.charAt(0) != '%') {
064: return false;
065: }
066:
067: IProject proj = fBase.getUnderlyingResource().getProject();
068: IFile file = proj.getFile(localiz + ".properties"); //$NON-NLS-1$
069: if (!file.exists())
070: return false;
071:
072: try {
073: IEditorPart editor = IDE.openEditor(PDEPlugin
074: .getActivePage(), file);
075: if (!(editor instanceof TextEditor))
076: return false;
077: TextEditor tEditor = (TextEditor) editor;
078: IDocument doc = tEditor.getDocumentProvider().getDocument(
079: tEditor.getEditorInput());
080: if (doc == null)
081: return false;
082:
083: String key = fElement.substring(1);
084: int keyLen = key.length();
085: String contents = doc.get();
086: int length = contents.length();
087: int start = 0;
088: int index;
089: while ((index = contents.indexOf(key, start)) >= 0) {
090: if (index > 0) {
091: // check for newline before
092: char c = contents.charAt(index - 1);
093: if (c != '\n' && c != '\r') {
094: start += keyLen;
095: continue;
096: }
097: }
098: if (index + keyLen < length) {
099: // check for whitespace / assign symbol after
100: char c = contents.charAt(index + keyLen);
101: if (!Character.isWhitespace(c) && c != '='
102: && c != ':') {
103: start += keyLen;
104: continue;
105: }
106: }
107: tEditor.selectAndReveal(index, keyLen);
108: break;
109: }
110: } catch (PartInitException e) {
111: return false;
112: }
113: return true;
114: }
115: }
|