david_williams | b1f7505 | 2005-02-18 00:25:37 +0000 | [diff] [blame] | 1 | package org.eclipse.wst.css.ui.internal.projection; |
| 2 | |
| 3 | import org.eclipse.core.runtime.Platform; |
| 4 | import org.eclipse.jface.text.IDocument; |
amywu | 91c2566 | 2006-06-14 05:08:57 +0000 | [diff] [blame] | 5 | import org.eclipse.jface.text.ITextInputListener; |
david_williams | b1f7505 | 2005-02-18 00:25:37 +0000 | [diff] [blame] | 6 | import org.eclipse.jface.text.source.projection.IProjectionListener; |
| 7 | import org.eclipse.jface.text.source.projection.ProjectionViewer; |
david_williams | 63219a2 | 2005-04-10 01:59:51 +0000 | [diff] [blame] | 8 | import org.eclipse.wst.css.core.internal.provisional.document.ICSSDocument; |
| 9 | import org.eclipse.wst.css.core.internal.provisional.document.ICSSModel; |
david_williams | b5d0563 | 2006-02-27 09:24:00 +0000 | [diff] [blame] | 10 | import org.eclipse.wst.sse.core.StructuredModelManager; |
david_williams | c911388 | 2005-04-09 03:19:38 +0000 | [diff] [blame] | 11 | import org.eclipse.wst.sse.core.internal.model.FactoryRegistry; |
amywu | 91c2566 | 2006-06-14 05:08:57 +0000 | [diff] [blame] | 12 | import org.eclipse.wst.sse.core.internal.provisional.INodeAdapter; |
david_williams | 4ad020f | 2005-04-18 08:00:30 +0000 | [diff] [blame] | 13 | import org.eclipse.wst.sse.core.internal.provisional.INodeNotifier; |
| 14 | import org.eclipse.wst.sse.core.internal.provisional.IStructuredModel; |
david_williams | b1f7505 | 2005-02-18 00:25:37 +0000 | [diff] [blame] | 15 | import org.eclipse.wst.sse.ui.internal.projection.IStructuredTextFoldingProvider; |
| 16 | |
| 17 | /** |
| 18 | * Updates the projection model of a structured model for CSS. |
| 19 | */ |
amywu | 91c2566 | 2006-06-14 05:08:57 +0000 | [diff] [blame] | 20 | public class StructuredTextFoldingProviderCSS implements IStructuredTextFoldingProvider, IProjectionListener, ITextInputListener { |
david_williams | b1f7505 | 2005-02-18 00:25:37 +0000 | [diff] [blame] | 21 | private final static boolean debugProjectionPerf = "true".equalsIgnoreCase(Platform.getDebugOption("org.eclipse.wst.css.ui/projectionperf")); //$NON-NLS-1$ //$NON-NLS-2$\ |
| 22 | |
| 23 | private IDocument fDocument; |
| 24 | private ProjectionViewer fViewer; |
amywu | 91c2566 | 2006-06-14 05:08:57 +0000 | [diff] [blame] | 25 | private boolean fProjectionNeedsToBeEnabled = false; |
david_williams | b1f7505 | 2005-02-18 00:25:37 +0000 | [diff] [blame] | 26 | |
| 27 | /** |
| 28 | * Just add adapter to top stylesheet node. This adapter will track |
| 29 | * children addition/deletion. |
| 30 | */ |
| 31 | private void addAllAdapters() { |
| 32 | long start = System.currentTimeMillis(); |
| 33 | |
amywu | 91c2566 | 2006-06-14 05:08:57 +0000 | [diff] [blame] | 34 | if (fDocument != null) { |
| 35 | IStructuredModel sModel = null; |
| 36 | try { |
| 37 | sModel = StructuredModelManager.getModelManager().getExistingModelForRead(fDocument); |
| 38 | if (sModel instanceof ICSSModel) { |
| 39 | ICSSModel cssModel = (ICSSModel) sModel; |
| 40 | ICSSDocument cssDoc = cssModel.getDocument(); |
| 41 | if (cssDoc instanceof INodeNotifier) { |
| 42 | INodeNotifier notifier = (INodeNotifier) cssDoc; |
| 43 | ProjectionModelNodeAdapterCSS adapter = (ProjectionModelNodeAdapterCSS) notifier.getExistingAdapter(ProjectionModelNodeAdapterCSS.class); |
| 44 | if (adapter != null) { |
| 45 | adapter.updateAdapter(cssDoc, fViewer); |
| 46 | } |
| 47 | else { |
| 48 | // just call getadapter so the adapter is created |
| 49 | // and |
| 50 | // automatically initialized |
| 51 | notifier.getAdapterFor(ProjectionModelNodeAdapterCSS.class); |
| 52 | } |
david_williams | b1f7505 | 2005-02-18 00:25:37 +0000 | [diff] [blame] | 53 | } |
| 54 | } |
| 55 | } |
amywu | 91c2566 | 2006-06-14 05:08:57 +0000 | [diff] [blame] | 56 | finally { |
| 57 | if (sModel != null) { |
| 58 | sModel.releaseFromRead(); |
| 59 | } |
david_williams | b1f7505 | 2005-02-18 00:25:37 +0000 | [diff] [blame] | 60 | } |
| 61 | } |
| 62 | |
amywu | 91c2566 | 2006-06-14 05:08:57 +0000 | [diff] [blame] | 63 | if (debugProjectionPerf) { |
| 64 | long end = System.currentTimeMillis(); |
david_williams | 5bd06bc | 2005-04-08 19:03:00 +0000 | [diff] [blame] | 65 | System.out.println("StructuredTextFoldingProviderCSS.addAllAdapters: " + (end - start)); //$NON-NLS-1$ |
amywu | 91c2566 | 2006-06-14 05:08:57 +0000 | [diff] [blame] | 66 | } |
david_williams | b1f7505 | 2005-02-18 00:25:37 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Get the ProjectionModelNodeAdapterFactoryCSS to use with this provider. |
| 71 | * |
| 72 | * @return ProjectionModelNodeAdapterFactoryCSS |
| 73 | */ |
| 74 | private ProjectionModelNodeAdapterFactoryCSS getAdapterFactory(boolean createIfNeeded) { |
| 75 | ProjectionModelNodeAdapterFactoryCSS factory = null; |
amywu | 91c2566 | 2006-06-14 05:08:57 +0000 | [diff] [blame] | 76 | if (fDocument != null) { |
| 77 | IStructuredModel sModel = null; |
| 78 | try { |
| 79 | sModel = StructuredModelManager.getModelManager().getExistingModelForRead(fDocument); |
| 80 | if (sModel != null) { |
| 81 | FactoryRegistry factoryRegistry = sModel.getFactoryRegistry(); |
david_williams | b1f7505 | 2005-02-18 00:25:37 +0000 | [diff] [blame] | 82 | |
amywu | 91c2566 | 2006-06-14 05:08:57 +0000 | [diff] [blame] | 83 | // getting the projectionmodelnodeadapter for the first |
| 84 | // time |
| 85 | // so do some initializing |
| 86 | if (!factoryRegistry.contains(ProjectionModelNodeAdapterCSS.class) && createIfNeeded) { |
| 87 | ProjectionModelNodeAdapterFactoryCSS newFactory = new ProjectionModelNodeAdapterFactoryCSS(); |
david_williams | b1f7505 | 2005-02-18 00:25:37 +0000 | [diff] [blame] | 88 | |
amywu | 91c2566 | 2006-06-14 05:08:57 +0000 | [diff] [blame] | 89 | // add factory to factory registry |
| 90 | factoryRegistry.addFactory(newFactory); |
| 91 | } |
| 92 | |
| 93 | // try and get the factory |
| 94 | factory = (ProjectionModelNodeAdapterFactoryCSS) factoryRegistry.getFactoryFor(ProjectionModelNodeAdapterCSS.class); |
david_williams | b1f7505 | 2005-02-18 00:25:37 +0000 | [diff] [blame] | 95 | } |
david_williams | b1f7505 | 2005-02-18 00:25:37 +0000 | [diff] [blame] | 96 | } |
amywu | 91c2566 | 2006-06-14 05:08:57 +0000 | [diff] [blame] | 97 | finally { |
| 98 | if (sModel != null) |
| 99 | sModel.releaseFromRead(); |
| 100 | } |
david_williams | b1f7505 | 2005-02-18 00:25:37 +0000 | [diff] [blame] | 101 | } |
| 102 | return factory; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Initialize this provider with the correct document. Assumes projection |
| 107 | * is enabled. (otherwise, only install would have been called) |
| 108 | */ |
| 109 | public void initialize() { |
| 110 | if (!isInstalled()) |
| 111 | return; |
| 112 | |
amywu | 91c2566 | 2006-06-14 05:08:57 +0000 | [diff] [blame] | 113 | // clear out old info |
| 114 | projectionDisabled(); |
david_williams | b1f7505 | 2005-02-18 00:25:37 +0000 | [diff] [blame] | 115 | |
david_williams | b1f7505 | 2005-02-18 00:25:37 +0000 | [diff] [blame] | 116 | fDocument = fViewer.getDocument(); |
| 117 | |
amywu | 91c2566 | 2006-06-14 05:08:57 +0000 | [diff] [blame] | 118 | // set projection viewer on new document's adapter factory |
| 119 | if (fViewer.getProjectionAnnotationModel() != null) { |
| 120 | ProjectionModelNodeAdapterFactoryCSS factory = getAdapterFactory(true); |
david_williams | b1f7505 | 2005-02-18 00:25:37 +0000 | [diff] [blame] | 121 | if (factory != null) { |
amywu | 91c2566 | 2006-06-14 05:08:57 +0000 | [diff] [blame] | 122 | factory.addProjectionViewer(fViewer); |
david_williams | b1f7505 | 2005-02-18 00:25:37 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | addAllAdapters(); |
| 126 | } |
amywu | 91c2566 | 2006-06-14 05:08:57 +0000 | [diff] [blame] | 127 | fProjectionNeedsToBeEnabled = false; |
david_williams | b1f7505 | 2005-02-18 00:25:37 +0000 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Associate a ProjectionViewer with this IStructuredTextFoldingProvider |
| 132 | * |
amywu | 91c2566 | 2006-06-14 05:08:57 +0000 | [diff] [blame] | 133 | * @param viewer - |
| 134 | * assumes not null |
david_williams | b1f7505 | 2005-02-18 00:25:37 +0000 | [diff] [blame] | 135 | */ |
| 136 | public void install(ProjectionViewer viewer) { |
| 137 | // uninstall before trying to install new viewer |
| 138 | if (isInstalled()) { |
| 139 | uninstall(); |
| 140 | } |
| 141 | fViewer = viewer; |
| 142 | fViewer.addProjectionListener(this); |
amywu | 91c2566 | 2006-06-14 05:08:57 +0000 | [diff] [blame] | 143 | fViewer.addTextInputListener(this); |
david_williams | b1f7505 | 2005-02-18 00:25:37 +0000 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | private boolean isInstalled() { |
| 147 | return fViewer != null; |
| 148 | } |
| 149 | |
| 150 | public void projectionDisabled() { |
| 151 | ProjectionModelNodeAdapterFactoryCSS factory = getAdapterFactory(false); |
| 152 | if (factory != null) { |
amywu | 91c2566 | 2006-06-14 05:08:57 +0000 | [diff] [blame] | 153 | factory.removeProjectionViewer(fViewer); |
david_williams | b1f7505 | 2005-02-18 00:25:37 +0000 | [diff] [blame] | 154 | } |
| 155 | |
amywu | 91c2566 | 2006-06-14 05:08:57 +0000 | [diff] [blame] | 156 | // clear out all annotations |
| 157 | if (fViewer.getProjectionAnnotationModel() != null) |
| 158 | fViewer.getProjectionAnnotationModel().removeAllAnnotations(); |
| 159 | |
| 160 | removeAllAdapters(); |
| 161 | |
david_williams | b1f7505 | 2005-02-18 00:25:37 +0000 | [diff] [blame] | 162 | fDocument = null; |
amywu | 91c2566 | 2006-06-14 05:08:57 +0000 | [diff] [blame] | 163 | fProjectionNeedsToBeEnabled = false; |
david_williams | b1f7505 | 2005-02-18 00:25:37 +0000 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | public void projectionEnabled() { |
| 167 | initialize(); |
| 168 | } |
| 169 | |
| 170 | /** |
amywu | 91c2566 | 2006-06-14 05:08:57 +0000 | [diff] [blame] | 171 | * Removes adapter from top stylesheet node |
| 172 | */ |
| 173 | private void removeAllAdapters() { |
| 174 | long start = System.currentTimeMillis(); |
| 175 | |
| 176 | if (fDocument != null) { |
| 177 | IStructuredModel sModel = null; |
| 178 | try { |
| 179 | sModel = StructuredModelManager.getModelManager().getExistingModelForRead(fDocument); |
| 180 | if (sModel instanceof ICSSModel) { |
| 181 | ICSSModel cssModel = (ICSSModel) sModel; |
| 182 | ICSSDocument cssDoc = cssModel.getDocument(); |
| 183 | if (cssDoc instanceof INodeNotifier) { |
| 184 | INodeNotifier notifier = (INodeNotifier) cssDoc; |
| 185 | INodeAdapter adapter = notifier.getExistingAdapter(ProjectionModelNodeAdapterCSS.class); |
| 186 | if (adapter != null) { |
| 187 | notifier.removeAdapter(adapter); |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | finally { |
| 193 | if (sModel != null) { |
| 194 | sModel.releaseFromRead(); |
| 195 | } |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | if (debugProjectionPerf) { |
| 200 | long end = System.currentTimeMillis(); |
| 201 | System.out.println("StructuredTextFoldingProviderCSS.addAllAdapters: " + (end - start)); //$NON-NLS-1$ |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | public void inputDocumentAboutToBeChanged(IDocument oldInput, IDocument newInput) { |
| 206 | // if folding is enabled and new document is going to be a totally |
| 207 | // different document, disable projection |
| 208 | if (fDocument != null && fDocument != newInput) { |
| 209 | // disable projection and disconnect everything |
| 210 | projectionDisabled(); |
| 211 | fProjectionNeedsToBeEnabled = true; |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | public void inputDocumentChanged(IDocument oldInput, IDocument newInput) { |
| 216 | // if projection was previously enabled before input document changed |
| 217 | // and new document is different than old document |
| 218 | if (fProjectionNeedsToBeEnabled && fDocument == null && newInput != null) { |
| 219 | projectionEnabled(); |
| 220 | fProjectionNeedsToBeEnabled = false; |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | /** |
david_williams | b1f7505 | 2005-02-18 00:25:37 +0000 | [diff] [blame] | 225 | * Disconnect this IStructuredTextFoldingProvider from projection viewer |
| 226 | */ |
| 227 | public void uninstall() { |
| 228 | if (isInstalled()) { |
| 229 | projectionDisabled(); |
| 230 | |
| 231 | fViewer.removeProjectionListener(this); |
amywu | 91c2566 | 2006-06-14 05:08:57 +0000 | [diff] [blame] | 232 | fViewer.removeTextInputListener(this); |
david_williams | b1f7505 | 2005-02-18 00:25:37 +0000 | [diff] [blame] | 233 | fViewer = null; |
| 234 | } |
| 235 | } |
| 236 | } |