Skip to main content

This CGIT instance is deprecated, and repositories have been moved to Gitlab or Github. See the repository descriptions for specific locations.

summaryrefslogtreecommitdiffstats
blob: 7032c211dfad9f8aa5468f33603946256ed3ce55 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/*******************************************************************************
 * Copyright (c) 2009 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.jst.jsp.ui.internal.java.refactoring;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.lang.reflect.InvocationTargetException;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.Document;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jst.jsp.core.internal.java.search.JSPSearchSupport;
import org.eclipse.jst.jsp.ui.internal.JSPUIMessages;
import org.eclipse.jst.jsp.ui.internal.Logger;
import org.eclipse.ltk.core.refactoring.Change;
import org.eclipse.ltk.core.refactoring.DocumentChange;
import org.eclipse.ltk.core.refactoring.RefactoringStatus;
import org.eclipse.text.edits.MalformedTreeException;
import org.eclipse.text.edits.MultiTextEdit;
import org.eclipse.text.edits.TextEdit;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IEditorReference;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.actions.WorkspaceModifyOperation;
import org.eclipse.ui.texteditor.ITextEditor;
import org.eclipse.wst.sse.core.internal.document.DocumentReader;
import org.eclipse.wst.sse.core.internal.encoding.CodedStreamCreator;

/**
 * {@link DocumentChange} implementation for JSP Documents
 */
public class JSPRenameChange extends DocumentChange {

	/**
	 * The JSP file this {@link Change} will change
	 */
	protected IFile fJSPFile = null;
	
	/**
	 * The description of this change
	 */
	private String fDescription;
	
	/**
	 * Create a new {@link JSPRenameChange}
	 * 
	 * @param jspFile
	 * @param jspDoc
	 * @param edit
	 * @param description
	 */
	public JSPRenameChange(IFile jspFile, IDocument jspDoc, TextEdit edit, String description) {
		super(JSPUIMessages.BasicRefactorSearchRequestor_6, jspDoc);
		MultiTextEdit parentEdit = new MultiTextEdit();
		parentEdit.addChild(edit);
		super.setEdit(parentEdit);
		this.fJSPFile = jspFile;
		this.fDescription = description;
	}
	
	/**
	 * Create a new {@link JSPRenameChange} by shallow copying the given
	 * original {@link JSPRenameChange}.
	 * 
	 * @param originalChange the {@link JSPRenameChange} to shallow copy to create
	 * a new {@link JSPRenameChange}
	 */
	public JSPRenameChange(JSPRenameChange originalChange) {
		super(JSPUIMessages.BasicRefactorSearchRequestor_6, originalChange.getJSPDoc());
		super.setEdit(originalChange.getEdit());
		this.fJSPFile = originalChange.fJSPFile;
		this.fDescription = originalChange.fDescription;
	}
	
	/**
	 * <p>Currently will always be {@link RefactoringStatus#OK}</p>
	 * 
	 * @see org.eclipse.ltk.core.refactoring.DocumentChange#isValid(org.eclipse.core.runtime.IProgressMonitor)
	 */
	public RefactoringStatus isValid(IProgressMonitor pm)throws CoreException {
		return new RefactoringStatus();
	}
	
	/**
	 * @see org.eclipse.ltk.core.refactoring.TextChange#getPreviewDocument(org.eclipse.core.runtime.IProgressMonitor)
	 */
	public IDocument getPreviewDocument(IProgressMonitor pm) throws CoreException {
		IDocument copyDoc = new Document(this.getJSPDoc().get());
		try {
			super.getEdit().apply(copyDoc);
		}
		catch (MalformedTreeException e) {
			// ignore
		}
		catch (BadLocationException e) {
			// ignore
		}
		return copyDoc;
	}
	
	/**
	 * Performs this change and returns a {@link JSPRenameUndoChange} to undo the change.
	 * 
	 * @return a {@link JSPRenameUndoChange} to undo this performed {@link Change}
	 * @see org.eclipse.ltk.core.refactoring.TextChange#perform(org.eclipse.core.runtime.IProgressMonitor)
	 */
	public Change perform(IProgressMonitor pm) throws CoreException {
		Change undoChange = null;
		try {
			//apply edit
			undoChange = super.perform(pm);
			undoChange = new JSPRenameUndoChange(this, undoChange);
			
			//save the model
			saveJSPFile(this.fJSPFile, this.getJSPDoc());
			
		} catch (MalformedTreeException e) {
			Logger.logException(e);
		}
		return undoChange;
	}

	/**
	 * @see org.eclipse.ltk.core.refactoring.TextEditBasedChange#getName()
	 */
	public String getName() {
		return this.fDescription;
	}
	
	/**
	 * <p>The modified element is the JSP {@link IFile} that this {@link Change}
	 * changes.</p>
	 * 
	 * @see org.eclipse.ltk.core.refactoring.DocumentChange#getModifiedElement()
	 */
	public Object getModifiedElement() {
		return this.fJSPFile;
	}
	
	/**
	 * <p>Convenience method to get the JSP {@link IDocument} that this {@link Change}
	 * edits.</p>
	 * 
	 * @return the JSP {@link IDocument} that this {@link Change} edits
	 */
	protected IDocument getJSPDoc() {
		IDocument doc = null;
		try {
			doc = this.acquireDocument(null);
		} catch(CoreException e) {
			//ignore, DocumentChange.acquireDocument will never throw it
		}
		
		return doc;
	}
	
	/**
	 * <p>Saves a JSP file.  If the file is not open in an editor then modifies the file directly, else
	 * if the file is open an editor then run the save method on the open editor.</p>
	 * 
	 * @param jspFile the {@link IFile} to save
	 * @param jspDoc the {@link IDocument} with the new content for the given {@link IFile}
	 */
	protected static void saveJSPFile(IFile jspFile, IDocument jspDoc) {
		//if not open then save model
		final ITextEditor editor = findOpenEditor(jspDoc);
		try {
			/* if no open editor then save the document to the file
			 * else save the open editor
			 */
			if(editor == null) {
				SaveJspFileOp op  = new SaveJspFileOp(jspFile, jspDoc);
				op.run(JSPSearchSupport.getInstance().getProgressMonitor());
			} else {
				//editor save must be done on UI thread
				IRunnableWithProgress runnable= new IRunnableWithProgress() {
					public void run(IProgressMonitor pm) throws InterruptedException {
						editor.doSave(pm);
					}
				};
				PlatformUI.getWorkbench().getProgressService().runInUI(editor.getSite().getWorkbenchWindow(), runnable, null);
			}
		} catch (InvocationTargetException e) {
			Logger.logException(e);
		} catch (InterruptedException e) {
			Logger.logException(e);
		}
	}
	
	/**
	 * <p>Checks if a document is open in an editor and returns it if it is</p>
	 * 
	 * @param jspDoc check to see if this {@link IDocument} is currently open in an editor
	 * @return the open {@link ITextEditor} associated with the given {@link IDocument} or
	 * <code>null</code> if none can be found.
	 */
	private static ITextEditor findOpenEditor(IDocument jspDoc) {
		IWorkbenchWindow[] windows = PlatformUI.getWorkbench().getWorkbenchWindows();
		IWorkbenchWindow w = null;
		for (int i = 0; i < windows.length; i++) {

			w = windows[i];
			IWorkbenchPage page = w.getActivePage();
			if (page != null) {

				IEditorReference[] references = page.getEditorReferences();
				IEditorPart editor = null;
				Object o = null;
				IDocument doc = null;
				for (int j = 0; j < references.length; j++) {

					editor = references[j].getEditor(false);
					// https://w3.opensource.ibm.com/bugzilla/show_bug.cgi?id=3764
					// use adapter to get ITextEditor (for things like
					// page designer)
					o = editor.getAdapter(ITextEditor.class);
					if (o != null && o instanceof ITextEditor) {

						doc = ((ITextEditor) o).getDocumentProvider().getDocument(editor.getEditorInput());
						if (doc != null && doc.equals(jspDoc)) {
							return (ITextEditor) o;
						}
					}
				}
			}
		}
		return null;
	}
	
	/**
	 * Workspace operation to perform save on model for updated documents.
	 * Should only be done on models not open in an editor.
	 */
	private static class SaveJspFileOp extends WorkspaceModifyOperation {
		
		private IDocument fJSPDoc = null;
		private IFile fJSPFile = null;
		
		public SaveJspFileOp(IFile jspFile, IDocument jspDoc) {
			this.fJSPDoc = jspDoc;
			this.fJSPFile = jspFile;
		}
		
		protected void execute(IProgressMonitor monitor) throws CoreException, InvocationTargetException, InterruptedException {
			
			// https://w3.opensource.ibm.com/bugzilla/show_bug.cgi?id=3765
			// save file w/ no intermediate model creation
			
			CodedStreamCreator codedStreamCreator = new CodedStreamCreator();
			Reader reader = new DocumentReader(this.fJSPDoc);
			codedStreamCreator.set(this.fJSPFile, reader);
			
			ByteArrayOutputStream codedByteStream = null;
			InputStream codedStream = null;
			try {
				codedByteStream = codedStreamCreator.getCodedByteArrayOutputStream();
				codedStream = new ByteArrayInputStream(codedByteStream.toByteArray());
				if (this.fJSPFile.exists())
					this.fJSPFile.setContents(codedStream, true, true, null);
				else
					this.fJSPFile.create(codedStream, false, null);
				
			} catch (CoreException e) {
				Logger.logException(e);
			} catch (IOException e) {
				Logger.logException(e);
			}
			finally {
				try {
					if(codedByteStream != null)
						codedByteStream.close();
					if(codedStream != null)
						codedStream.close();
				}
				catch (IOException e){
					// unlikely
				}
			}
		}
	}
}

Back to the top