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 | |
| 14 | import java.io.ByteArrayInputStream; |
| 15 | import java.io.IOException; |
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; |
| 24 | import org.eclipse.jface.text.reconciler.DirtyRegion; |
| 25 | import org.eclipse.jface.text.reconciler.IReconcilingStrategy; |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 26 | import org.eclipse.jface.text.source.ISourceViewer; |
| 27 | import org.eclipse.wst.sse.ui.internal.IReleasable; |
nitind | 93023a1 | 2006-09-19 18:24:13 +0000 | [diff] [blame^] | 28 | import org.eclipse.wst.sse.ui.internal.Logger; |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 29 | import org.eclipse.wst.sse.ui.internal.reconcile.validator.ValidatorBuilder; |
| 30 | import org.eclipse.wst.sse.ui.internal.reconcile.validator.ValidatorMetaData; |
| 31 | import org.eclipse.wst.sse.ui.internal.reconcile.validator.ValidatorStrategy; |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 32 | import org.eclipse.wst.sse.ui.internal.spelling.SpellcheckStrategy; |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 33 | |
| 34 | /** |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 35 | * Adds to DirtyRegionProcessor Job: - IDocumentListener - ValidatorStrategy - |
| 36 | * Text viewer(dispose, input changed) listeners. - default, spelling, and |
| 37 | * validator strategies - DirtyRegion processing logic. |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 38 | */ |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 39 | public class DocumentRegionProcessor extends DirtyRegionProcessor { |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 40 | |
nitind | 93023a1 | 2006-09-19 18:24:13 +0000 | [diff] [blame^] | 41 | private static final boolean DEBUG_VALIDATORS = true;// Boolean.TRUE.toString().equalsIgnoreCase(Platform.getDebugOption("org.eclipse.wst.sse.ui/debug/reconcilerValidators")); |
| 42 | // //$NON-NLS-1$ |
| 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 | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 47 | private SpellcheckStrategy 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 | |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 58 | protected void beginProcessing() { |
| 59 | super.beginProcessing(); |
nitind | a5804e8 | 2006-03-14 00:54:11 +0000 | [diff] [blame] | 60 | ValidatorStrategy validatorStrategy = getValidatorStrategy(); |
| 61 | if (validatorStrategy != null) { |
| 62 | validatorStrategy.beginProcessing(); |
| 63 | } |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 64 | } |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 65 | |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 66 | protected void endProcessing() { |
| 67 | super.endProcessing(); |
nitind | a5804e8 | 2006-03-14 00:54:11 +0000 | [diff] [blame] | 68 | ValidatorStrategy validatorStrategy = getValidatorStrategy(); |
| 69 | if (validatorStrategy != null) { |
| 70 | validatorStrategy.endProcessing(); |
| 71 | } |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 72 | } |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 73 | |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 74 | protected String getContentType(IDocument doc) { |
| 75 | if (doc == null) |
| 76 | return null; |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 77 | |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 78 | String contentTypeId = null; |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 79 | |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 80 | // pa_TODO it would be nice to be able to get filename from |
| 81 | // IDocument... |
| 82 | // because it seems that getting content type from input stream |
| 83 | // isn't all that accurate (eg. w/ a javascript file) |
| 84 | |
| 85 | // IContentType ct = |
| 86 | // Platform.getContentTypeManager().findContentTypeFor("test.js"); |
| 87 | IContentType ct = null; |
| 88 | try { |
| 89 | IContentDescription desc = Platform.getContentTypeManager().getDescriptionFor(new ByteArrayInputStream(doc.get().getBytes()), null, IContentDescription.ALL); |
| 90 | if (desc != null) { |
| 91 | ct = desc.getContentType(); |
| 92 | if (ct != null) |
| 93 | contentTypeId = ct.getId(); |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 94 | } |
| 95 | } |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 96 | catch (IOException e) { |
| 97 | // just bail |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 98 | } |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 99 | return contentTypeId; |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 100 | } |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 101 | |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 102 | protected final StructuredTextReconcilingStrategy getSpellcheckStrategy() { |
| 103 | if (fSpellcheckStrategy == null) { |
| 104 | String contentTypeId = getContentType(getDocument()); |
| 105 | if (contentTypeId != null) { |
| 106 | if (getTextViewer() instanceof ISourceViewer) { |
| 107 | ISourceViewer viewer = (ISourceViewer) getTextViewer(); |
| 108 | fSpellcheckStrategy = new SpellcheckStrategy(viewer, contentTypeId); |
| 109 | fSpellcheckStrategy.setDocument(getDocument()); |
| 110 | fSpellcheckStrategy.setDocumentPartitioning(getDocumentPartitioning()); |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 111 | } |
| 112 | } |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 113 | } |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 114 | return fSpellcheckStrategy; |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 115 | } |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 116 | |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 117 | /** |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 118 | * @return Returns the ValidatorStrategy. |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 119 | */ |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 120 | protected ValidatorStrategy getValidatorStrategy() { |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 121 | if (fValidatorStrategy == null) { |
| 122 | ValidatorStrategy validatorStrategy = null; |
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 (getTextViewer() instanceof ISourceViewer) { |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 125 | ISourceViewer viewer = (ISourceViewer) getTextViewer(); |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 126 | String contentTypeId = null; |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 127 | |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 128 | IDocument doc = viewer.getDocument(); |
| 129 | contentTypeId = getContentType(doc); |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 130 | |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 131 | if (contentTypeId != null) { |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 132 | validatorStrategy = new ValidatorStrategy(viewer, contentTypeId); |
| 133 | ValidatorBuilder vBuilder = new ValidatorBuilder(); |
nitind | a5804e8 | 2006-03-14 00:54:11 +0000 | [diff] [blame] | 134 | ValidatorMetaData[] vmds = vBuilder.getValidatorMetaData(SSE_UI_ID); |
nitind | 93023a1 | 2006-09-19 18:24:13 +0000 | [diff] [blame^] | 135 | List enabledValidators = new ArrayList(1); |
| 136 | /* if any "must" handle this content type, just add them */ |
| 137 | boolean foundSpecificContentTypeValidators = false; |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 138 | for (int i = 0; i < vmds.length; i++) { |
nitind | 93023a1 | 2006-09-19 18:24:13 +0000 | [diff] [blame^] | 139 | if (vmds[i].mustHandleContentType(contentTypeId)) { |
| 140 | if (DEBUG_VALIDATORS) |
| 141 | Logger.log(Logger.INFO, contentTypeId + " using specific validator " + vmds[i].getValidatorId()); //$NON-NLS-1$ |
| 142 | foundSpecificContentTypeValidators = true; |
| 143 | enabledValidators.add(vmds[i]); |
| 144 | } |
| 145 | } |
| 146 | if (!foundSpecificContentTypeValidators) { |
| 147 | for (int i = 0; i < vmds.length; i++) { |
| 148 | if (vmds[i].canHandleContentType(contentTypeId)) { |
| 149 | if (DEBUG_VALIDATORS) |
| 150 | Logger.log(Logger.INFO, contentTypeId + " using inherited(?) validator " + vmds[i].getValidatorId()); //$NON-NLS-1$ |
| 151 | enabledValidators.add(vmds[i]); |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | for (int i = 0; i < enabledValidators.size(); i++) { |
| 156 | validatorStrategy.addValidatorMetaData((ValidatorMetaData) enabledValidators.get(i)); |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 157 | } |
| 158 | } |
| 159 | } |
| 160 | fValidatorStrategy = validatorStrategy; |
| 161 | } |
| 162 | return fValidatorStrategy; |
| 163 | } |
| 164 | |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 165 | /** |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 166 | * @param dirtyRegion |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 167 | */ |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 168 | protected void process(DirtyRegion dirtyRegion) { |
| 169 | if (!isInstalled()) |
| 170 | return; |
| 171 | |
| 172 | super.process(dirtyRegion); |
| 173 | |
| 174 | // Also call the validation and spell-check strategies |
| 175 | ITypedRegion[] partitions = computePartitioning(dirtyRegion); |
| 176 | |
| 177 | DirtyRegion dirty = null; |
| 178 | for (int i = 0; i < partitions.length; i++) { |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 179 | // [source]validator (extension) for this partition |
david_williams | 5ce6d6c | 2006-05-28 23:58:12 +0000 | [diff] [blame] | 180 | if (getValidatorStrategy() != null) { |
| 181 | dirty = createDirtyRegion(partitions[i], DirtyRegion.INSERT); |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 182 | getValidatorStrategy().reconcile(partitions[i], dirty); |
david_williams | 5ce6d6c | 2006-05-28 23:58:12 +0000 | [diff] [blame] | 183 | } |
pavery | 1f1588d | 2006-02-15 20:44:54 +0000 | [diff] [blame] | 184 | } |
nitind | 93023a1 | 2006-09-19 18:24:13 +0000 | [diff] [blame^] | 185 | |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 186 | // single spell-check for everything |
david_williams | 5ce6d6c | 2006-05-28 23:58:12 +0000 | [diff] [blame] | 187 | if (getSpellcheckStrategy() != null) { |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 188 | getSpellcheckStrategy().reconcile(dirtyRegion, dirtyRegion); |
david_williams | 5ce6d6c | 2006-05-28 23:58:12 +0000 | [diff] [blame] | 189 | } |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 190 | } |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 191 | |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 192 | public void setDocument(IDocument doc) { |
| 193 | super.setDocument(doc); |
| 194 | IReconcilingStrategy validatorStrategy = getValidatorStrategy(); |
| 195 | if (validatorStrategy != null) { |
| 196 | validatorStrategy.setDocument(doc); |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 197 | } |
nitind | 0f05955 | 2006-05-01 17:36:50 +0000 | [diff] [blame] | 198 | if (fSpellcheckStrategy != null) { |
| 199 | fSpellcheckStrategy.setDocument(doc); |
| 200 | } |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 201 | } |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 202 | |
| 203 | /** |
| 204 | * @see org.eclipse.wst.sse.ui.internal.reconcile.DirtyRegionProcessor#uninstall() |
| 205 | */ |
| 206 | public void uninstall() { |
| 207 | if (isInstalled()) { |
| 208 | |
| 209 | cancel(); |
| 210 | |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 211 | IReconcilingStrategy validatorStrategy = getValidatorStrategy(); |
| 212 | |
| 213 | if (validatorStrategy != null) { |
| 214 | if (validatorStrategy instanceof IReleasable) |
| 215 | ((IReleasable) validatorStrategy).release(); |
| 216 | } |
nitind | 0f05955 | 2006-05-01 17:36:50 +0000 | [diff] [blame] | 217 | if (fSpellcheckStrategy != null) { |
| 218 | fSpellcheckStrategy.setDocument(null); |
| 219 | } |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 220 | } |
| 221 | super.uninstall(); |
| 222 | } |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 223 | } |