1 11 package org.eclipse.ui.texteditor; 12 13 import java.util.ArrayList ; 14 import java.util.List ; 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 39 public final class HyperlinkDetectorRegistry { 40 41 42 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 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 96 public void dispose() { 97 if (fHyperlinkDetector != null) { 98 fHyperlinkDetector.dispose(); 99 fHyperlinkDetector= null; 100 } 101 fHyperlinkDescriptor= null; 102 fContext= null; 103 } 104 105 109 public int getStateMask() { 110 return fStateMask; 111 } 112 113 } 114 115 116 private HyperlinkDetectorDescriptor[] fHyperlinkDetectorDescriptors; 117 private IPreferenceStore fPreferenceStore; 118 119 120 123 public HyperlinkDetectorRegistry() { 124 } 125 126 137 public HyperlinkDetectorRegistry(IPreferenceStore preferenceStore) { 138 fPreferenceStore= preferenceStore; 139 } 140 141 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 156 private synchronized void initHyperlinkDetectorDescriptors() { 157 if (fHyperlinkDetectorDescriptors == null) 158 fHyperlinkDetectorDescriptors= HyperlinkDetectorDescriptor.getContributedHyperlinkDetectors(); 159 } 160 161 164 public IHyperlinkDetector[] createHyperlinkDetectors(String targetId, IAdaptable context) { 165 Assert.isLegal(targetId != null); 166 initHyperlinkDetectorDescriptors(); 167 168 List result= new ArrayList (); 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 |