nitind | 93023a1 | 2006-09-19 18:24:13 +0000 | [diff] [blame] | 1 | /******************************************************************************* |
| 2 | * Copyright (c) 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 | *******************************************************************************/ |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 12 | package org.eclipse.wst.sse.ui.internal.reconcile; |
| 13 | |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 14 | import java.io.IOException; |
nitind | c77f963 | 2008-03-19 21:26:39 +0000 | [diff] [blame] | 15 | import java.io.StringReader; |
nitind | 93023a1 | 2006-09-19 18:24:13 +0000 | [diff] [blame] | 16 | import java.util.ArrayList; |
| 17 | import java.util.List; |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 18 | |
| 19 | import org.eclipse.core.runtime.Platform; |
| 20 | import org.eclipse.core.runtime.content.IContentDescription; |
| 21 | import org.eclipse.core.runtime.content.IContentType; |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 22 | import org.eclipse.jface.text.IDocument; |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 23 | import org.eclipse.jface.text.ITypedRegion; |
nitind | f3f1087 | 2007-09-03 06:36:56 +0000 | [diff] [blame] | 24 | import org.eclipse.jface.text.Region; |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 25 | import org.eclipse.jface.text.reconciler.DirtyRegion; |
| 26 | import org.eclipse.jface.text.reconciler.IReconcilingStrategy; |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 27 | import org.eclipse.jface.text.source.ISourceViewer; |
| 28 | import org.eclipse.wst.sse.ui.internal.IReleasable; |
nitind | 93023a1 | 2006-09-19 18:24:13 +0000 | [diff] [blame] | 29 | import org.eclipse.wst.sse.ui.internal.Logger; |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 30 | import org.eclipse.wst.sse.ui.internal.reconcile.validator.ValidatorBuilder; |
| 31 | import org.eclipse.wst.sse.ui.internal.reconcile.validator.ValidatorMetaData; |
| 32 | import org.eclipse.wst.sse.ui.internal.reconcile.validator.ValidatorStrategy; |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 33 | import org.eclipse.wst.sse.ui.internal.spelling.SpellcheckStrategy; |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 34 | |
| 35 | /** |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 36 | * Adds to DirtyRegionProcessor Job: - IDocumentListener - ValidatorStrategy - |
| 37 | * Text viewer(dispose, input changed) listeners. - default, spelling, and |
| 38 | * validator strategies - DirtyRegion processing logic. |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 39 | */ |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 40 | public class DocumentRegionProcessor extends DirtyRegionProcessor { |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 41 | |
nitind | c77f963 | 2008-03-19 21:26:39 +0000 | [diff] [blame] | 42 | private static final boolean DEBUG_VALIDATORS = Boolean.TRUE.toString().equalsIgnoreCase(Platform.getDebugOption("org.eclipse.wst.sse.ui/debug/reconcilerValidators")); //$NON-NLS-1$ |
nitind | 93023a1 | 2006-09-19 18:24:13 +0000 | [diff] [blame] | 43 | |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 44 | /** |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 45 | * A strategy to use the defined default Spelling service. |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 46 | */ |
nitind | c77f963 | 2008-03-19 21:26:39 +0000 | [diff] [blame] | 47 | private IReconcilingStrategy fSpellcheckStrategy; |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 48 | |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 49 | /** |
| 50 | * The strategy that runs validators contributed via |
| 51 | * <code>org.eclipse.wst.sse.ui.extensions.sourcevalidation</code> |
| 52 | * extension point |
| 53 | */ |
| 54 | private ValidatorStrategy fValidatorStrategy; |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 55 | |
nitind | a5804e8 | 2006-03-14 00:54:11 +0000 | [diff] [blame] | 56 | private final String SSE_UI_ID = "org.eclipse.wst.sse.ui"; //$NON-NLS-1$ |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 57 | |
amywu | cfaee2d | 2006-11-21 00:28:32 +0000 | [diff] [blame] | 58 | |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 59 | protected void beginProcessing() { |
| 60 | super.beginProcessing(); |
nitind | a5804e8 | 2006-03-14 00:54:11 +0000 | [diff] [blame] | 61 | ValidatorStrategy validatorStrategy = getValidatorStrategy(); |
| 62 | if (validatorStrategy != null) { |
| 63 | validatorStrategy.beginProcessing(); |
| 64 | } |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 65 | } |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 66 | |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 67 | protected void endProcessing() { |
| 68 | super.endProcessing(); |
nitind | a5804e8 | 2006-03-14 00:54:11 +0000 | [diff] [blame] | 69 | ValidatorStrategy validatorStrategy = getValidatorStrategy(); |
| 70 | if (validatorStrategy != null) { |
| 71 | validatorStrategy.endProcessing(); |
| 72 | } |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 73 | } |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 74 | |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 75 | protected String getContentType(IDocument doc) { |
| 76 | if (doc == null) |
| 77 | return null; |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 78 | |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 79 | String contentTypeId = null; |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 80 | |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 81 | IContentType ct = null; |
| 82 | try { |
nitind | c77f963 | 2008-03-19 21:26:39 +0000 | [diff] [blame] | 83 | IContentDescription desc = Platform.getContentTypeManager().getDescriptionFor(new StringReader(doc.get()), null, IContentDescription.ALL); |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 84 | if (desc != null) { |
| 85 | ct = desc.getContentType(); |
| 86 | if (ct != null) |
| 87 | contentTypeId = ct.getId(); |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 88 | } |
| 89 | } |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 90 | catch (IOException e) { |
| 91 | // just bail |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 92 | } |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 93 | return contentTypeId; |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 94 | } |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 95 | |
nitind | c77f963 | 2008-03-19 21:26:39 +0000 | [diff] [blame] | 96 | protected IReconcilingStrategy getSpellcheckStrategy() { |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 97 | if (fSpellcheckStrategy == null) { |
| 98 | String contentTypeId = getContentType(getDocument()); |
| 99 | if (contentTypeId != null) { |
| 100 | if (getTextViewer() instanceof ISourceViewer) { |
| 101 | ISourceViewer viewer = (ISourceViewer) getTextViewer(); |
| 102 | fSpellcheckStrategy = new SpellcheckStrategy(viewer, contentTypeId); |
| 103 | fSpellcheckStrategy.setDocument(getDocument()); |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 104 | } |
| 105 | } |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 106 | } |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 107 | return fSpellcheckStrategy; |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 108 | } |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 109 | |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 110 | /** |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 111 | * @return Returns the ValidatorStrategy. |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 112 | */ |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 113 | protected ValidatorStrategy getValidatorStrategy() { |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 114 | if (fValidatorStrategy == null) { |
| 115 | ValidatorStrategy validatorStrategy = null; |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 116 | |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 117 | if (getTextViewer() instanceof ISourceViewer) { |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 118 | ISourceViewer viewer = (ISourceViewer) getTextViewer(); |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 119 | String contentTypeId = null; |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 120 | |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 121 | IDocument doc = viewer.getDocument(); |
| 122 | contentTypeId = getContentType(doc); |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 123 | |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 124 | if (contentTypeId != null) { |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 125 | validatorStrategy = new ValidatorStrategy(viewer, contentTypeId); |
| 126 | ValidatorBuilder vBuilder = new ValidatorBuilder(); |
nitind | a5804e8 | 2006-03-14 00:54:11 +0000 | [diff] [blame] | 127 | ValidatorMetaData[] vmds = vBuilder.getValidatorMetaData(SSE_UI_ID); |
nitind | 93023a1 | 2006-09-19 18:24:13 +0000 | [diff] [blame] | 128 | List enabledValidators = new ArrayList(1); |
| 129 | /* if any "must" handle this content type, just add them */ |
| 130 | boolean foundSpecificContentTypeValidators = false; |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 131 | for (int i = 0; i < vmds.length; i++) { |
nitind | 93023a1 | 2006-09-19 18:24:13 +0000 | [diff] [blame] | 132 | if (vmds[i].mustHandleContentType(contentTypeId)) { |
| 133 | if (DEBUG_VALIDATORS) |
| 134 | Logger.log(Logger.INFO, contentTypeId + " using specific validator " + vmds[i].getValidatorId()); //$NON-NLS-1$ |
| 135 | foundSpecificContentTypeValidators = true; |
| 136 | enabledValidators.add(vmds[i]); |
| 137 | } |
| 138 | } |
| 139 | if (!foundSpecificContentTypeValidators) { |
| 140 | for (int i = 0; i < vmds.length; i++) { |
| 141 | if (vmds[i].canHandleContentType(contentTypeId)) { |
| 142 | if (DEBUG_VALIDATORS) |
| 143 | Logger.log(Logger.INFO, contentTypeId + " using inherited(?) validator " + vmds[i].getValidatorId()); //$NON-NLS-1$ |
| 144 | enabledValidators.add(vmds[i]); |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | for (int i = 0; i < enabledValidators.size(); i++) { |
| 149 | validatorStrategy.addValidatorMetaData((ValidatorMetaData) enabledValidators.get(i)); |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 150 | } |
| 151 | } |
| 152 | } |
| 153 | fValidatorStrategy = validatorStrategy; |
| 154 | } |
| 155 | return fValidatorStrategy; |
| 156 | } |
| 157 | |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 158 | /** |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 159 | * @param dirtyRegion |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 160 | */ |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 161 | protected void process(DirtyRegion dirtyRegion) { |
amywu | cfaee2d | 2006-11-21 00:28:32 +0000 | [diff] [blame] | 162 | if (!isInstalled() || isInRewriteSession()) |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 163 | return; |
| 164 | |
| 165 | super.process(dirtyRegion); |
| 166 | |
| 167 | // Also call the validation and spell-check strategies |
| 168 | ITypedRegion[] partitions = computePartitioning(dirtyRegion); |
| 169 | |
| 170 | DirtyRegion dirty = null; |
| 171 | for (int i = 0; i < partitions.length; i++) { |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 172 | // [source]validator (extension) for this partition |
david_williams | 5ce6d6c | 2006-05-28 23:58:12 +0000 | [diff] [blame] | 173 | if (getValidatorStrategy() != null) { |
| 174 | dirty = createDirtyRegion(partitions[i], DirtyRegion.INSERT); |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 175 | getValidatorStrategy().reconcile(partitions[i], dirty); |
david_williams | 5ce6d6c | 2006-05-28 23:58:12 +0000 | [diff] [blame] | 176 | } |
pavery | 1f1588d | 2006-02-15 20:44:54 +0000 | [diff] [blame] | 177 | } |
nitind | 93023a1 | 2006-09-19 18:24:13 +0000 | [diff] [blame] | 178 | |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 179 | // single spell-check for everything |
david_williams | 5ce6d6c | 2006-05-28 23:58:12 +0000 | [diff] [blame] | 180 | if (getSpellcheckStrategy() != null) { |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 181 | getSpellcheckStrategy().reconcile(dirtyRegion, dirtyRegion); |
david_williams | 5ce6d6c | 2006-05-28 23:58:12 +0000 | [diff] [blame] | 182 | } |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 183 | } |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 184 | |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 185 | public void setDocument(IDocument doc) { |
| 186 | super.setDocument(doc); |
| 187 | IReconcilingStrategy validatorStrategy = getValidatorStrategy(); |
| 188 | if (validatorStrategy != null) { |
| 189 | validatorStrategy.setDocument(doc); |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 190 | } |
nitind | 0f05955 | 2006-05-01 17:36:50 +0000 | [diff] [blame] | 191 | if (fSpellcheckStrategy != null) { |
| 192 | fSpellcheckStrategy.setDocument(doc); |
| 193 | } |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 194 | } |
nitind | c77f963 | 2008-03-19 21:26:39 +0000 | [diff] [blame] | 195 | |
nitind | f3f1087 | 2007-09-03 06:36:56 +0000 | [diff] [blame] | 196 | protected void setEntireDocumentDirty(IDocument document) { |
| 197 | super.setEntireDocumentDirty(document); |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 198 | |
nitind | f3f1087 | 2007-09-03 06:36:56 +0000 | [diff] [blame] | 199 | // make the entire document dirty |
| 200 | // this also happens on a "save as" |
| 201 | if (document != null && isInstalled() && fLastPartitions != null && document.getLength() == 0) { |
| 202 | /** |
| 203 | * https://bugs.eclipse.org/bugs/show_bug.cgi?id=199053 |
| 204 | * |
| 205 | * Process the strategies for the last known-good partitions. |
| 206 | */ |
| 207 | for (int i = 0; i < fLastPartitions.length; i++) { |
nitind | 69bc47f | 2008-04-07 22:45:51 +0000 | [diff] [blame^] | 208 | ValidatorStrategy validatorStrategy = getValidatorStrategy(); |
| 209 | if (validatorStrategy != null) { |
| 210 | validatorStrategy.reconcile(fLastPartitions[i], createDirtyRegion(fLastPartitions[i], DirtyRegion.REMOVE)); |
| 211 | } |
nitind | f3f1087 | 2007-09-03 06:36:56 +0000 | [diff] [blame] | 212 | } |
| 213 | if (fSpellcheckStrategy != null) { |
| 214 | fSpellcheckStrategy.reconcile(new Region(0, document.getLength())); |
| 215 | } |
| 216 | } |
| 217 | } |
nitind | c77f963 | 2008-03-19 21:26:39 +0000 | [diff] [blame] | 218 | |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 219 | /** |
| 220 | * @see org.eclipse.wst.sse.ui.internal.reconcile.DirtyRegionProcessor#uninstall() |
| 221 | */ |
| 222 | public void uninstall() { |
| 223 | if (isInstalled()) { |
| 224 | |
| 225 | cancel(); |
| 226 | |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 227 | IReconcilingStrategy validatorStrategy = getValidatorStrategy(); |
| 228 | |
| 229 | if (validatorStrategy != null) { |
| 230 | if (validatorStrategy instanceof IReleasable) |
| 231 | ((IReleasable) validatorStrategy).release(); |
| 232 | } |
nitind | 0f05955 | 2006-05-01 17:36:50 +0000 | [diff] [blame] | 233 | if (fSpellcheckStrategy != null) { |
| 234 | fSpellcheckStrategy.setDocument(null); |
| 235 | } |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 236 | } |
| 237 | super.uninstall(); |
| 238 | } |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 239 | } |