KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > text > TranslationHyperlink


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 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.pde.internal.ui.editor.text;
12
13 import org.eclipse.core.resources.IFile;
14 import org.eclipse.core.resources.IProject;
15 import org.eclipse.jface.text.IDocument;
16 import org.eclipse.jface.text.IRegion;
17 import org.eclipse.pde.core.IModel;
18 import org.eclipse.pde.core.plugin.IPluginModelBase;
19 import org.eclipse.pde.internal.core.PDEManager;
20 import org.eclipse.pde.internal.core.ibundle.IBundleModel;
21 import org.eclipse.pde.internal.ui.PDEPlugin;
22 import org.eclipse.ui.IEditorPart;
23 import org.eclipse.ui.PartInitException;
24 import org.eclipse.ui.editors.text.TextEditor;
25 import org.eclipse.ui.ide.IDE;
26
27 public class TranslationHyperlink extends AbstractHyperlink {
28
29     private IModel fBase;
30     
31     private boolean fOpened;
32     
33     public TranslationHyperlink(IRegion region, String JavaDoc element, IModel base) {
34         super(region, element);
35         fBase = base;
36     }
37
38     private String JavaDoc getLocaliation() {
39         String JavaDoc localiz = null;
40         if (fBase instanceof IPluginModelBase)
41             localiz = PDEManager.getBundleLocalization((IPluginModelBase)fBase);
42         else if (fBase instanceof IBundleModel)
43             localiz = ((IBundleModel)fBase).getBundle().getLocalization();
44         return localiz;
45     }
46     
47     public boolean getOpened() {
48         return fOpened;
49     }
50     
51     public void open() {
52         fOpened = openHyperLink();
53     }
54     
55     public boolean openHyperLink() {
56         String JavaDoc localiz = getLocaliation();
57         if (localiz == null) {
58             return false;
59         } else if (fBase.getUnderlyingResource() == null) {
60             return false;
61         } else if (fElement.length() == 0 || fElement.charAt(0) != '%') {
62             return false;
63         }
64         
65         IProject proj = fBase.getUnderlyingResource().getProject();
66         IFile file = proj.getFile(localiz + ".properties"); //$NON-NLS-1$
67
if (!file.exists())
68             return false;
69         
70         try {
71             IEditorPart editor = IDE.openEditor(PDEPlugin.getActivePage(), file);
72             if (!(editor instanceof TextEditor))
73                 return false;
74             TextEditor tEditor = (TextEditor)editor;
75             IDocument doc = tEditor.getDocumentProvider().getDocument(tEditor.getEditorInput());
76             if (doc == null)
77                 return false;
78             
79             String JavaDoc key = fElement.substring(1);
80             int keyLen = key.length();
81             String JavaDoc contents = doc.get();
82             int length = contents.length();
83             int start = 0;
84             int index;
85             while ((index = contents.indexOf(key, start)) >= 0) {
86                 if (index > 0) {
87                     // check for newline before
88
char c = contents.charAt(index - 1);
89                     if (c != '\n' && c != '\r') {
90                         start += keyLen;
91                         continue;
92                     }
93                 }
94                 if (index + keyLen < length) {
95                     // check for whitespace / assign symbol after
96
char c = contents.charAt(index + keyLen);
97                     if (!Character.isWhitespace(c) && c != '=' && c != ':') {
98                         start += keyLen;
99                         continue;
100                     }
101                 }
102                 tEditor.selectAndReveal(index, keyLen);
103                 break;
104             }
105         } catch (PartInitException e) {
106             return false;
107         }
108         return true;
109     }
110 }
111
Popular Tags