KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > texteditor > HyperlinkDetectorRegistry


1 /*******************************************************************************
2  * Copyright (c) 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.ui.texteditor;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.List JavaDoc;
15
16 import org.eclipse.core.runtime.Assert;
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.core.runtime.IAdaptable;
19
20 import org.eclipse.jface.preference.IPreferenceStore;
21
22 import org.eclipse.jface.text.IRegion;
23 import org.eclipse.jface.text.ITextViewer;
24 import org.eclipse.jface.text.hyperlink.AbstractHyperlinkDetector;
25 import org.eclipse.jface.text.hyperlink.IHyperlink;
26 import org.eclipse.jface.text.hyperlink.IHyperlinkDetector;
27 import org.eclipse.jface.text.hyperlink.IHyperlinkDetectorExtension;
28 import org.eclipse.jface.text.hyperlink.IHyperlinkDetectorExtension2;
29
30
31
32 /**
33  * Hyperlink detector registry that manages the detectors
34  * contributed by the <code>org.eclipse.ui.workbench.texteditor.hyperlinkDetectors</code> extension point for
35  * targets contributed by the <code>org.eclipse.ui.workbench.texteditor.hyperlinkDetectorTargets</code> extension point.
36  *
37  * @since 3.3
38  */

39 public final class HyperlinkDetectorRegistry {
40     
41     
42     /**
43      * Delegate for contributed hyperlink detectors.
44      */

45     private class HyperlinkDetectorDelegate implements IHyperlinkDetector, IHyperlinkDetectorExtension, IHyperlinkDetectorExtension2 {
46         
47         private HyperlinkDetectorDescriptor fHyperlinkDescriptor;
48         private AbstractHyperlinkDetector fHyperlinkDetector;
49         private boolean fFailedDuringCreation= false;
50         private IAdaptable fContext;
51         private int fStateMask;
52         private boolean fIsEnabled;
53
54         
55         private HyperlinkDetectorDelegate(HyperlinkDetectorDescriptor descriptor) {
56             fHyperlinkDescriptor= descriptor;
57             if (fPreferenceStore != null) {
58                 fStateMask= fPreferenceStore.getInt(fHyperlinkDescriptor.getId() + HyperlinkDetectorDescriptor.STATE_MASK_POSTFIX);
59                 fIsEnabled= !fPreferenceStore.getBoolean(fHyperlinkDescriptor.getId());
60             }
61         }
62
63         /*
64          * @see org.eclipse.jface.text.hyperlink.IHyperlinkDetector#detectHyperlinks(org.eclipse.jface.text.ITextViewer, org.eclipse.jface.text.IRegion, boolean)
65          */

66         public IHyperlink[] detectHyperlinks(ITextViewer textViewer, IRegion region, boolean canShowMultipleHyperlinks) {
67             if (!isEnabled())
68                 return null;
69             
70             if (!fFailedDuringCreation && fHyperlinkDetector == null) {
71                 try {
72                     fHyperlinkDetector= fHyperlinkDescriptor.createHyperlinkDetector();
73                 } catch (CoreException ex) {
74                     fFailedDuringCreation= true;
75                 }
76                 if (fHyperlinkDetector != null && fContext != null)
77                     fHyperlinkDetector.setContext(fContext);
78             }
79             if (fHyperlinkDetector != null)
80                 return fHyperlinkDetector.detectHyperlinks(textViewer, region, canShowMultipleHyperlinks);
81             
82             return null;
83         }
84         
85         private boolean isEnabled() {
86             return fIsEnabled;
87         }
88
89         private void setContext(IAdaptable context) {
90             fContext= context;
91         }
92         
93         /*
94          * @see org.eclipse.jface.text.hyperlink.IHyperlinkDetectorExtension#dispose()
95          */

96         public void dispose() {
97             if (fHyperlinkDetector != null) {
98                 fHyperlinkDetector.dispose();
99                 fHyperlinkDetector= null;
100             }
101             fHyperlinkDescriptor= null;
102             fContext= null;
103         }
104         
105         /*
106          * @see org.eclipse.jface.text.hyperlink.IHyperlinkDetectorExtension#getStateMask()
107          * @since 3.3
108          */

109         public int getStateMask() {
110             return fStateMask;
111         }
112         
113     }
114
115     
116     private HyperlinkDetectorDescriptor[] fHyperlinkDetectorDescriptors;
117     private IPreferenceStore fPreferenceStore;
118     
119
120     /**
121      * Creates a new hyperlink detector registry.
122      */

123     public HyperlinkDetectorRegistry() {
124     }
125     
126     /**
127      * Creates a new hyperlink detector registry that controls
128      * hyperlink enablement via the given preference store.
129      * <p>
130      * The hyperlink detector id is used as preference key.
131      * The value is of type <code>Boolean</code> where
132      * <code>false</code> means that the hyperlink detector is active.
133      * </p>
134      *
135      * @param preferenceStore the preference store to be used
136      */

137     public HyperlinkDetectorRegistry(IPreferenceStore preferenceStore) {
138         fPreferenceStore= preferenceStore;
139     }
140     
141     /**
142      * Returns all hyperlink detectors contributed to the workbench.
143      *
144      * @return an array of hyperlink detector descriptors
145      */

146     public synchronized HyperlinkDetectorDescriptor[] getHyperlinkDetectorDescriptors() {
147         initHyperlinkDetectorDescriptors();
148         HyperlinkDetectorDescriptor[] result= new HyperlinkDetectorDescriptor[fHyperlinkDetectorDescriptors.length];
149         System.arraycopy(fHyperlinkDetectorDescriptors, 0, result, 0, fHyperlinkDetectorDescriptors.length);
150         return result;
151     }
152
153     /**
154      * Initializes the hyperlink detector descriptors.
155      */

156     private synchronized void initHyperlinkDetectorDescriptors() {
157         if (fHyperlinkDetectorDescriptors == null)
158             fHyperlinkDetectorDescriptors= HyperlinkDetectorDescriptor.getContributedHyperlinkDetectors();
159     }
160
161     /*
162      * @see org.eclipse.jface.text.hyperlink.IHyperlinkDetectorRegistry#createHyperlinkDetectors(java.lang.String[], org.eclipse.core.runtime.IAdaptable)
163      */

164     public IHyperlinkDetector[] createHyperlinkDetectors(String JavaDoc targetId, IAdaptable context) {
165         Assert.isLegal(targetId != null);
166         initHyperlinkDetectorDescriptors();
167         
168         List JavaDoc result= new ArrayList JavaDoc();
169         for (int i= 0; i < fHyperlinkDetectorDescriptors.length; i++) {
170             if (targetId.equals(fHyperlinkDetectorDescriptors[i].getTargetId())) {
171                 HyperlinkDetectorDelegate detector= new HyperlinkDetectorDelegate(fHyperlinkDetectorDescriptors[i]);
172                 result.add(detector);
173                 detector.setContext(context);
174             }
175         }
176         return (IHyperlinkDetector[])result.toArray(new IHyperlinkDetector[result.size()]);
177     }
178
179 }
180
Popular Tags