if (input instanceof IParent) { parent= (IParent)input; } else { IJavaElement parentElement= input.getParent(); if (parentElement instanceof IParent) { parent= (IParent)parentElement; if (input instanceof ISourceReference) { ISourceReference sr= (ISourceReference) input; try { ISourceRange range= sr.getSourceRange(); insertPosition= range.getOffset() + range.getLength(); } catch(JavaModelException ex) { MessageDialog.openError(shell, fTitle, "Can't find range to replace");
if (input instanceof IParent) { parent= (IParent)input; } else { IJavaElement parentElement= input.getParent(); if (parentElement instanceof IParent) { parent= (IParent)parentElement; if (input instanceof ISourceReference) { ISourceReference sr= (ISourceReference) input; try { ISourceRange range= sr.getSourceRange(); insertPosition= range.getOffset() + range.getLength(); } catch(JavaModelException ex) { }
	public final void run() {				Shell shell= JavaPlugin.getActiveWorkbenchShell();				ISelection selection= fSelectionProvider.getSelection();				ICompilationUnit cu= null;		IParent parent= null;		int insertPosition= -1;		// where to insert the element				if (selection.isEmpty()) {			if (fEditor != null) {				IEditorInput editorInput= fEditor.getEditorInput();				IWorkingCopyManager manager= JavaPlugin.getDefault().getWorkingCopyManager();				cu= manager.getWorkingCopy(editorInput);				parent= cu;			}					} else {			IMember input= getEditionElement(selection);			if (input == null) {				// shouldn't happen because Action should not be enabled in the first place				MessageDialog.openError(shell, fTitle, "No editions for selection");				return;			}			cu= input.getCompilationUnit();						if (input instanceof IParent) {				parent= (IParent)input;			} else {				IJavaElement parentElement= input.getParent();				if (parentElement instanceof IParent) {					parent= (IParent)parentElement;					if (input instanceof ISourceReference) {						ISourceReference sr= (ISourceReference) input;						try {							ISourceRange range= sr.getSourceRange();							insertPosition= range.getOffset() + range.getLength();						} catch(JavaModelException ex) {							MessageDialog.openError(shell, fTitle, "Can't find range to replace");						}					}				}			}		}				// extract CU from selection		if (cu.isWorkingCopy())			cu= (ICompilationUnit) cu.getOriginalElement();		// find underlying file		IFile file= null;		try {			file= (IFile) cu.getUnderlyingResource();		} catch (JavaModelException ex) {		}		if (file == null) {			MessageDialog.openError(shell, fTitle, "Can't find underlying file");			return;		}				// get available editions		IFileState[] states= null;		try {			states= file.getHistory(null);		} catch (CoreException ex) {		}			if (states == null || states.length <= 0) {			MessageDialog.openInformation(shell, fTitle, "No editions available");			return;		}				ITypedElement target= new ResourceNode(file);		ITypedElement[] editions= new ITypedElement[states.length];		for (int i= 0; i < states.length; i++)			editions[i]= new HistoryItem(target, states[i]);						DocumentManager docManager= null;		try {			docManager= new DocumentManager(cu);		} catch(JavaModelException ex) {			MessageDialog.openError(shell, fTitle, "JavaModelException");			return;		}				try {			docManager.connect();					EditionSelectionDialog d= new EditionSelectionDialog(shell, fBundle);						IDocument document= docManager.getDocument();			String type= file.getFileExtension();						ITypedElement ti= d.selectEdition(target, editions, parent);			if (ti instanceof IStreamContentAccessor) {				IStreamContentAccessor sca= (IStreamContentAccessor) ti;									//				Position range= null;//				ITypedElement target2= d.getTarget();//				if (target2 instanceof IDocumentRange)//					range= ((IDocumentRange)target2).getRange();						String text= JavaCompareUtilities.readString(sca.getContents());					if (text != null && insertPosition >= 0) {					document.replace(insertPosition, 0, text);					//	docManager.save(null);	// should not be necesssary				}			}		} catch(BadLocationException ex) {			MessageDialog.openError(shell, fTitle, "BadLocationException");		} catch(CoreException ex) {			MessageDialog.openError(shell, fTitle, "CoreException");		} finally {			docManager.disconnect();		}	}