PageRenderTime 42ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/eclipse_SDK-3.7.1/plugins/org.eclipse.jdt.ui.source_3.7.1.r371_v20110824-0800/org/eclipse/jdt/internal/ui/propertiesfileeditor/PropertiesFileSourceViewerConfiguration.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 398 lines | 214 code | 48 blank | 136 comment | 18 complexity | d96b2c9569880e61f2c27ae0e849a35e MD5 | raw file
  1. /*******************************************************************************
  2. * Copyright (c) 2000, 2011 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. * Brock Janiczak <brockj@tpg.com.au> - [nls tooling] Properties file editor should have "toggle comment" action - https://bugs.eclipse.org/bugs/show_bug.cgi?id=192045
  11. *******************************************************************************/
  12. package org.eclipse.jdt.internal.ui.propertiesfileeditor;
  13. import java.util.ArrayList;
  14. import java.util.List;
  15. import java.util.Map;
  16. import org.eclipse.swt.widgets.Shell;
  17. import org.eclipse.core.runtime.CoreException;
  18. import org.eclipse.core.runtime.Platform;
  19. import org.eclipse.core.runtime.content.IContentType;
  20. import org.eclipse.jface.preference.IPreferenceStore;
  21. import org.eclipse.jface.util.PropertyChangeEvent;
  22. import org.eclipse.jface.text.DefaultInformationControl;
  23. import org.eclipse.jface.text.IAutoEditStrategy;
  24. import org.eclipse.jface.text.IDocument;
  25. import org.eclipse.jface.text.IInformationControl;
  26. import org.eclipse.jface.text.IInformationControlCreator;
  27. import org.eclipse.jface.text.ITextDoubleClickStrategy;
  28. import org.eclipse.jface.text.ITextHover;
  29. import org.eclipse.jface.text.presentation.IPresentationReconciler;
  30. import org.eclipse.jface.text.presentation.PresentationReconciler;
  31. import org.eclipse.jface.text.quickassist.IQuickAssistAssistant;
  32. import org.eclipse.jface.text.reconciler.IReconciler;
  33. import org.eclipse.jface.text.reconciler.IReconcilingStrategy;
  34. import org.eclipse.jface.text.reconciler.MonoReconciler;
  35. import org.eclipse.jface.text.rules.DefaultDamagerRepairer;
  36. import org.eclipse.jface.text.rules.RuleBasedScanner;
  37. import org.eclipse.jface.text.source.Annotation;
  38. import org.eclipse.jface.text.source.IAnnotationHover;
  39. import org.eclipse.jface.text.source.ISourceViewer;
  40. import org.eclipse.ui.IFileEditorInput;
  41. import org.eclipse.ui.texteditor.ITextEditor;
  42. import org.eclipse.ui.texteditor.spelling.SpellingReconcileStrategy;
  43. import org.eclipse.ui.texteditor.spelling.SpellingService;
  44. import org.eclipse.ui.editors.text.EditorsUI;
  45. import org.eclipse.ui.editors.text.TextSourceViewerConfiguration;
  46. import org.eclipse.jdt.ui.PreferenceConstants;
  47. import org.eclipse.jdt.ui.text.IColorManager;
  48. import org.eclipse.jdt.internal.ui.JavaPlugin;
  49. import org.eclipse.jdt.internal.ui.text.AbstractJavaScanner;
  50. import org.eclipse.jdt.internal.ui.text.HTMLAnnotationHover;
  51. import org.eclipse.jdt.internal.ui.text.JavaPresentationReconciler;
  52. import org.eclipse.jdt.internal.ui.text.SingleTokenJavaScanner;
  53. import org.eclipse.jdt.internal.ui.text.java.PartitionDoubleClickSelector;
  54. /**
  55. * Configuration for a source viewer which shows a properties file.
  56. * <p>
  57. * This class may be instantiated; it is not intended to be subclassed.
  58. * </p>
  59. *
  60. * @since 3.1
  61. */
  62. public class PropertiesFileSourceViewerConfiguration extends TextSourceViewerConfiguration {
  63. /** Properties file content type */
  64. private static final IContentType PROPERTIES_CONTENT_TYPE= Platform.getContentTypeManager().getContentType("org.eclipse.jdt.core.javaProperties"); //$NON-NLS-1$
  65. /**
  66. * The text editor.
  67. */
  68. private ITextEditor fTextEditor;
  69. /**
  70. * The document partitioning.
  71. */
  72. private String fDocumentPartitioning;
  73. /**
  74. * The property key scanner.
  75. */
  76. private AbstractJavaScanner fPropertyKeyScanner;
  77. /**
  78. * The comment scanner.
  79. */
  80. private AbstractJavaScanner fCommentScanner;
  81. /**
  82. * The property value scanner.
  83. */
  84. private AbstractJavaScanner fPropertyValueScanner;
  85. /**
  86. * The color manager.
  87. */
  88. private IColorManager fColorManager;
  89. /**
  90. * Creates a new properties file source viewer configuration for viewers in the given editor
  91. * using the given preference store, the color manager and the specified document partitioning.
  92. *
  93. * @param colorManager the color manager
  94. * @param preferenceStore the preference store, can be read-only
  95. * @param editor the editor in which the configured viewer(s) will reside
  96. * @param partitioning the document partitioning for this configuration
  97. */
  98. public PropertiesFileSourceViewerConfiguration(IColorManager colorManager, IPreferenceStore preferenceStore, ITextEditor editor, String partitioning) {
  99. super(preferenceStore);
  100. fColorManager= colorManager;
  101. fTextEditor= editor;
  102. fDocumentPartitioning= partitioning;
  103. initializeScanners();
  104. }
  105. /**
  106. * Returns the property key scanner for this configuration.
  107. *
  108. * @return the property key scanner
  109. */
  110. protected RuleBasedScanner getPropertyKeyScanner() {
  111. return fPropertyKeyScanner;
  112. }
  113. /**
  114. * Returns the comment scanner for this configuration.
  115. *
  116. * @return the comment scanner
  117. */
  118. protected RuleBasedScanner getCommentScanner() {
  119. return fCommentScanner;
  120. }
  121. /**
  122. * Returns the property value scanner for this configuration.
  123. *
  124. * @return the property value scanner
  125. */
  126. protected RuleBasedScanner getPropertyValueScanner() {
  127. return fPropertyValueScanner;
  128. }
  129. /**
  130. * Returns the color manager for this configuration.
  131. *
  132. * @return the color manager
  133. */
  134. protected IColorManager getColorManager() {
  135. return fColorManager;
  136. }
  137. /**
  138. * Returns the editor in which the configured viewer(s) will reside.
  139. *
  140. * @return the enclosing editor
  141. */
  142. protected ITextEditor getEditor() {
  143. return fTextEditor;
  144. }
  145. /**
  146. * Initializes the scanners.
  147. */
  148. private void initializeScanners() {
  149. fPropertyKeyScanner= new SingleTokenJavaScanner(getColorManager(), fPreferenceStore, PreferenceConstants.PROPERTIES_FILE_COLORING_KEY);
  150. fPropertyValueScanner= new PropertyValueScanner(getColorManager(), fPreferenceStore);
  151. fCommentScanner= new SingleTokenJavaScanner(getColorManager(), fPreferenceStore, PreferenceConstants.PROPERTIES_FILE_COLORING_COMMENT);
  152. }
  153. /*
  154. * @see SourceViewerConfiguration#getPresentationReconciler(ISourceViewer)
  155. */
  156. @Override
  157. public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer) {
  158. PresentationReconciler reconciler= new JavaPresentationReconciler();
  159. reconciler.setDocumentPartitioning(getConfiguredDocumentPartitioning(sourceViewer));
  160. DefaultDamagerRepairer dr= new DefaultDamagerRepairer(getPropertyKeyScanner());
  161. reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE);
  162. reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE);
  163. dr= new DefaultDamagerRepairer(getCommentScanner());
  164. reconciler.setDamager(dr, IPropertiesFilePartitions.COMMENT);
  165. reconciler.setRepairer(dr, IPropertiesFilePartitions.COMMENT);
  166. dr= new DefaultDamagerRepairer(getPropertyValueScanner());
  167. reconciler.setDamager(dr, IPropertiesFilePartitions.PROPERTY_VALUE);
  168. reconciler.setRepairer(dr, IPropertiesFilePartitions.PROPERTY_VALUE);
  169. return reconciler;
  170. }
  171. /*
  172. * @see SourceViewerConfiguration#getDoubleClickStrategy(ISourceViewer, String)
  173. */
  174. @Override
  175. public ITextDoubleClickStrategy getDoubleClickStrategy(ISourceViewer sourceViewer, String contentType) {
  176. if (IDocument.DEFAULT_CONTENT_TYPE.equals(contentType))
  177. return new PartitionDoubleClickSelector(getConfiguredDocumentPartitioning(sourceViewer), 0, 0, 0);
  178. if (IPropertiesFilePartitions.COMMENT.equals(contentType))
  179. return new PartitionDoubleClickSelector(getConfiguredDocumentPartitioning(sourceViewer), 0, 0);
  180. if (IPropertiesFilePartitions.PROPERTY_VALUE.equals(contentType))
  181. return new PartitionDoubleClickSelector(getConfiguredDocumentPartitioning(sourceViewer), 1, -1);
  182. return super.getDoubleClickStrategy(sourceViewer, contentType);
  183. }
  184. /*
  185. * @see SourceViewerConfiguration#getConfiguredContentTypes(ISourceViewer)
  186. */
  187. @Override
  188. public String[] getConfiguredContentTypes(ISourceViewer sourceViewer) {
  189. int length= IPropertiesFilePartitions.PARTITIONS.length;
  190. String[] contentTypes= new String[length + 1];
  191. contentTypes[0]= IDocument.DEFAULT_CONTENT_TYPE;
  192. for (int i= 0; i < length; i++)
  193. contentTypes[i+1]= IPropertiesFilePartitions.PARTITIONS[i];
  194. return contentTypes;
  195. }
  196. /*
  197. * @see org.eclipse.jface.text.source.SourceViewerConfiguration#getConfiguredDocumentPartitioning(org.eclipse.jface.text.source.ISourceViewer)
  198. */
  199. @Override
  200. public String getConfiguredDocumentPartitioning(ISourceViewer sourceViewer) {
  201. if (fDocumentPartitioning != null)
  202. return fDocumentPartitioning;
  203. return super.getConfiguredDocumentPartitioning(sourceViewer);
  204. }
  205. /**
  206. * Determines whether the preference change encoded by the given event
  207. * changes the behavior of one of its contained components.
  208. *
  209. * @param event the event to be investigated
  210. * @return <code>true</code> if event causes a behavioral change
  211. */
  212. public boolean affectsTextPresentation(PropertyChangeEvent event) {
  213. return fPropertyKeyScanner.affectsBehavior(event)
  214. || fCommentScanner.affectsBehavior(event)
  215. || fPropertyValueScanner.affectsBehavior(event);
  216. }
  217. /**
  218. * Adapts the behavior of the contained components to the change
  219. * encoded in the given event.
  220. *
  221. * @param event the event to which to adapt
  222. * @see PropertiesFileSourceViewerConfiguration#PropertiesFileSourceViewerConfiguration(IColorManager, IPreferenceStore, ITextEditor, String)
  223. */
  224. public void handlePropertyChangeEvent(PropertyChangeEvent event) {
  225. if (fPropertyKeyScanner.affectsBehavior(event))
  226. fPropertyKeyScanner.adaptToPreferenceChange(event);
  227. if (fCommentScanner.affectsBehavior(event))
  228. fCommentScanner.adaptToPreferenceChange(event);
  229. if (fPropertyValueScanner.affectsBehavior(event))
  230. fPropertyValueScanner.adaptToPreferenceChange(event);
  231. }
  232. /*
  233. * @see org.eclipse.jface.text.source.SourceViewerConfiguration#getHyperlinkDetectorTargets(org.eclipse.jface.text.source.ISourceViewer)
  234. * @since 3.3
  235. */
  236. @Override
  237. protected Map<String, ITextEditor> getHyperlinkDetectorTargets(ISourceViewer sourceViewer) {
  238. Map<String, ITextEditor> targets= super.getHyperlinkDetectorTargets(sourceViewer);
  239. targets.put("org.eclipse.jdt.ui.PropertiesFileEditor", fTextEditor); //$NON-NLS-1$
  240. return targets;
  241. }
  242. /*
  243. * @see SourceViewerConfiguration#getAnnotationHover(ISourceViewer)
  244. */
  245. @Override
  246. public IAnnotationHover getAnnotationHover(ISourceViewer sourceViewer) {
  247. return new HTMLAnnotationHover(false) {
  248. @Override
  249. protected boolean isIncluded(Annotation annotation) {
  250. return isShowInVerticalRuler(annotation);
  251. }
  252. };
  253. }
  254. /*
  255. * @see SourceViewerConfiguration#getOverviewRulerAnnotationHover(ISourceViewer)
  256. */
  257. @Override
  258. public IAnnotationHover getOverviewRulerAnnotationHover(ISourceViewer sourceViewer) {
  259. return new HTMLAnnotationHover(true) {
  260. @Override
  261. protected boolean isIncluded(Annotation annotation) {
  262. return isShowInOverviewRuler(annotation);
  263. }
  264. };
  265. }
  266. /*
  267. * @see SourceViewerConfiguration#getInformationControlCreator(ISourceViewer)
  268. */
  269. @Override
  270. public IInformationControlCreator getInformationControlCreator(ISourceViewer sourceViewer) {
  271. return new IInformationControlCreator() {
  272. public IInformationControl createInformationControl(Shell parent) {
  273. return new DefaultInformationControl(parent, JavaPlugin.getAdditionalInfoAffordanceString());
  274. }
  275. };
  276. }
  277. /*
  278. * @see org.eclipse.ui.editors.text.TextSourceViewerConfiguration#getReconciler(org.eclipse.jface.text.source.ISourceViewer)
  279. */
  280. @Override
  281. public IReconciler getReconciler(ISourceViewer sourceViewer) {
  282. if (!EditorsUI.getPreferenceStore().getBoolean(SpellingService.PREFERENCE_SPELLING_ENABLED))
  283. return null;
  284. IReconcilingStrategy strategy= new SpellingReconcileStrategy(sourceViewer, EditorsUI.getSpellingService()) {
  285. @Override
  286. protected IContentType getContentType() {
  287. return PROPERTIES_CONTENT_TYPE;
  288. }
  289. };
  290. MonoReconciler reconciler= new MonoReconciler(strategy, false);
  291. reconciler.setDelay(500);
  292. return reconciler;
  293. }
  294. /*
  295. * @see org.eclipse.jface.text.source.SourceViewerConfiguration#getDefaultPrefixes(org.eclipse.jface.text.source.ISourceViewer, java.lang.String)
  296. * @since 3.4
  297. */
  298. @Override
  299. public String[] getDefaultPrefixes(ISourceViewer sourceViewer, String contentType) {
  300. return new String[] {"#", ""}; //$NON-NLS-1$ //$NON-NLS-2$
  301. }
  302. /*
  303. * (non-Javadoc)
  304. * @see org.eclipse.jface.text.source.SourceViewerConfiguration#getAutoEditStrategies(org.eclipse.jface.text.source.ISourceViewer, java.lang.String)
  305. * @since 3.7
  306. */
  307. @Override
  308. public IAutoEditStrategy[] getAutoEditStrategies(ISourceViewer sourceViewer, String contentType) {
  309. IAutoEditStrategy[] autoEditStrategies= super.getAutoEditStrategies(sourceViewer, contentType);
  310. if (fTextEditor == null)
  311. return autoEditStrategies;
  312. try {
  313. if (!PropertiesFileDocumentProvider.isJavaPropertiesFile(fTextEditor.getEditorInput())) {
  314. return autoEditStrategies;
  315. }
  316. List<IAutoEditStrategy> stratergies= new ArrayList<IAutoEditStrategy>();
  317. for (int i= 0; i < autoEditStrategies.length; i++) {
  318. stratergies.add(autoEditStrategies[i]);
  319. }
  320. stratergies.add(new PropertiesFileAutoEditStrategy(((IFileEditorInput)fTextEditor.getEditorInput()).getFile(), sourceViewer));
  321. return stratergies.toArray(new IAutoEditStrategy[stratergies.size()]);
  322. } catch (CoreException e) {
  323. JavaPlugin.log(e);
  324. return autoEditStrategies;
  325. }
  326. }
  327. /*
  328. * (non-Javadoc)
  329. * @see org.eclipse.jface.text.source.SourceViewerConfiguration#getTextHover(org.eclipse.jface.text.source.ISourceViewer, java.lang.String, int)
  330. * @since 3.7
  331. */
  332. @Override
  333. public ITextHover getTextHover(ISourceViewer sourceViewer, String contentType, int stateMask) {
  334. return new PropertiesFileHover(super.getTextHover(sourceViewer, contentType));
  335. }
  336. /*
  337. * (non-Javadoc)
  338. * @see org.eclipse.ui.editors.text.TextSourceViewerConfiguration#getQuickAssistAssistant(org.eclipse.jface.text.source.ISourceViewer)
  339. * @since 3.7
  340. */
  341. @Override
  342. public IQuickAssistAssistant getQuickAssistAssistant(ISourceViewer sourceViewer) {
  343. if (getEditor() != null) {
  344. PropertiesCorrectionAssistant assistant= new PropertiesCorrectionAssistant();
  345. assistant.setRestoreCompletionProposalSize(JavaPlugin.getDefault().getDialogSettingsSection("quick_assist_proposal_size")); //$NON-NLS-1$
  346. return assistant;
  347. }
  348. return null;
  349. }
  350. }