use of org.eclipse.che.ide.api.editor.EditorPartPresenter in project che by eclipse.
the class RefactoringUpdater method updateAfterRefactoring.
/**
* Iterates over each refactoring change and according to change type performs specific update operation.
* i.e. for {@code ChangeName#UPDATE} updates only opened editors, for {@code ChangeName#MOVE or ChangeName#RENAME_COMPILATION_UNIT}
* updates only new paths and opened editors, for {@code ChangeName#RENAME_PACKAGE} reloads package structure and restore expansion.
*
* @param changes
* applied changes
*/
public void updateAfterRefactoring(List<ChangeInfo> changes) {
if (changes == null || changes.isEmpty()) {
return;
}
ExternalResourceDelta[] deltas = new ExternalResourceDelta[0];
final List<String> pathChanged = new ArrayList<>();
for (ChangeInfo change : changes) {
final ExternalResourceDelta delta;
final Path newPath = Path.valueOf(change.getPath());
final Path oldPath = !isNullOrEmpty(change.getOldPath()) ? Path.valueOf(change.getOldPath()) : Path.EMPTY;
switch(change.getName()) {
case MOVE:
case RENAME_COMPILATION_UNIT:
delta = new ExternalResourceDelta(newPath, oldPath, ADDED | MOVED_FROM | MOVED_TO);
registerRemovedFile(change);
break;
case RENAME_PACKAGE:
delta = new ExternalResourceDelta(newPath, oldPath, ADDED | MOVED_FROM | MOVED_TO);
break;
case UPDATE:
if (!isFileRemoved(change.getPath(), changes)) {
pathChanged.add(change.getPath());
registerRemovedFile(change);
}
default:
continue;
}
final int index = deltas.length;
deltas = Arrays.copyOf(deltas, index + 1);
deltas[index] = delta;
}
//here we need to remove file for file that moved or renamed JDT lib sent it to
for (int i = 0; i < deltas.length; i++) {
if (pathChanged.contains(deltas[i].getToPath().toString())) {
pathChanged.remove(deltas[i].getToPath().toString());
}
if (pathChanged.contains(deltas[i].getFromPath().toString())) {
pathChanged.remove(deltas[i].getFromPath().toString());
}
}
if (deltas.length > 0) {
appContext.getWorkspaceRoot().synchronize(deltas).then(new Operation<ResourceDelta[]>() {
@Override
public void apply(final ResourceDelta[] appliedDeltas) throws OperationException {
for (ResourceDelta delta : appliedDeltas) {
eventBus.fireEvent(new RevealResourceEvent(delta.getToPath()));
}
for (EditorPartPresenter editorPartPresenter : editorAgent.getOpenedEditors()) {
final String path = editorPartPresenter.getEditorInput().getFile().getLocation().toString();
if (pathChanged.contains(path)) {
eventBus.fireEvent(new FileContentUpdateEvent(editorPartPresenter.getEditorInput().getFile().getLocation().toString()));
}
}
setActiveEditor();
}
});
} else {
Scheduler.get().scheduleDeferred(new Scheduler.ScheduledCommand() {
@Override
public void execute() {
for (EditorPartPresenter editorPartPresenter : editorAgent.getOpenedEditors()) {
eventBus.fireEvent(new FileContentUpdateEvent(editorPartPresenter.getEditorInput().getFile().getLocation().toString()));
}
setActiveEditor();
}
});
}
}
use of org.eclipse.che.ide.api.editor.EditorPartPresenter in project che by eclipse.
the class OpenDeclarationFinder method openDeclaration.
public void openDeclaration() {
EditorPartPresenter activeEditor = editorAgent.getActiveEditor();
if (activeEditor == null) {
return;
}
if (!(activeEditor instanceof TextEditor)) {
Log.error(getClass(), "Open Declaration support only TextEditor as editor");
return;
}
TextEditor editor = ((TextEditor) activeEditor);
int offset = editor.getCursorOffset();
final VirtualFile file = editor.getEditorInput().getFile();
if (file instanceof Resource) {
final Optional<Project> project = ((Resource) file).getRelatedProject();
final Optional<Resource> srcFolder = ((Resource) file).getParentWithMarker(SourceFolderMarker.ID);
if (!srcFolder.isPresent()) {
return;
}
final String fqn = JavaUtil.resolveFQN((Container) srcFolder.get(), (Resource) file);
navigationService.findDeclaration(project.get().getLocation(), fqn, offset).then(new Operation<OpenDeclarationDescriptor>() {
@Override
public void apply(OpenDeclarationDescriptor result) throws OperationException {
if (result != null) {
handleDescriptor(project.get().getLocation(), result);
}
}
});
} else if (file instanceof JarFileNode) {
navigationService.findDeclaration(((JarFileNode) file).getProjectLocation(), file.getLocation().toString().replace('/', '.'), offset).then(new Operation<OpenDeclarationDescriptor>() {
@Override
public void apply(OpenDeclarationDescriptor result) throws OperationException {
if (result != null) {
handleDescriptor(((JarFileNode) file).getProject(), result);
}
}
});
}
}
use of org.eclipse.che.ide.api.editor.EditorPartPresenter in project che by eclipse.
the class OpenDeclarationFinder method handleDescriptor.
private void handleDescriptor(final Path projectPath, final OpenDeclarationDescriptor descriptor) {
final EditorPartPresenter openedEditor = editorAgent.getOpenedEditor(Path.valueOf(descriptor.getPath()));
if (openedEditor != null) {
editorAgent.openEditor(openedEditor.getEditorInput().getFile(), new OpenEditorCallbackImpl() {
@Override
public void onEditorOpened(EditorPartPresenter editor) {
setCursorAndActivateEditor(editor, descriptor.getOffset());
}
@Override
public void onEditorActivated(EditorPartPresenter editor) {
setCursorAndActivateEditor(editor, descriptor.getOffset());
}
});
return;
}
if (descriptor.isBinary()) {
navigationService.getEntry(projectPath, descriptor.getLibId(), descriptor.getPath()).then(new Operation<JarEntry>() {
@Override
public void apply(final JarEntry entry) throws OperationException {
navigationService.getContent(projectPath, descriptor.getLibId(), Path.valueOf(entry.getPath())).then(new Operation<ClassContent>() {
@Override
public void apply(ClassContent content) throws OperationException {
final VirtualFile file = javaNodeFactory.newJarFileNode(entry, descriptor.getLibId(), projectPath, null);
editorAgent.openEditor(file, new OpenEditorCallbackImpl() {
@Override
public void onEditorOpened(final EditorPartPresenter editor) {
Scheduler.get().scheduleDeferred(new Scheduler.ScheduledCommand() {
@Override
public void execute() {
if (editor instanceof TextEditor) {
((TextEditor) editor).getDocument().setSelectedRange(LinearRange.createWithStart(descriptor.getOffset()).andLength(0), true);
editor.activate();
}
}
});
}
});
}
});
}
});
} else {
appContext.getWorkspaceRoot().getFile(descriptor.getPath()).then(new Operation<Optional<File>>() {
@Override
public void apply(Optional<File> file) throws OperationException {
if (file.isPresent()) {
editorAgent.openEditor(file.get(), new OpenEditorCallbackImpl() {
@Override
public void onEditorOpened(final EditorPartPresenter editor) {
Scheduler.get().scheduleDeferred(new Scheduler.ScheduledCommand() {
@Override
public void execute() {
if (editor instanceof TextEditor) {
((TextEditor) editor).getDocument().setSelectedRange(LinearRange.createWithStart(descriptor.getOffset()).andLength(0), true);
editor.activate();
}
}
});
}
});
}
}
});
}
}
use of org.eclipse.che.ide.api.editor.EditorPartPresenter in project che by eclipse.
the class RenameRefactoringAction method updateInPerspective.
@Override
public void updateInPerspective(ActionEvent event) {
event.getPresentation().setVisible(true);
if (editorInFocus) {
final EditorPartPresenter editorPart = editorAgent.getActiveEditor();
if (editorPart == null || !(editorPart instanceof TextEditor)) {
event.getPresentation().setEnabled(false);
return;
}
final VirtualFile file = editorPart.getEditorInput().getFile();
if (file instanceof File) {
final Optional<Project> project = ((File) file).getRelatedProject();
if (!project.isPresent()) {
event.getPresentation().setEnabled(false);
return;
}
event.getPresentation().setEnabled(JavaUtil.isJavaProject(project.get()) && isJavaFile(file));
} else {
event.getPresentation().setEnabled(isJavaFile(file));
}
} else {
final Resource[] resources = appContext.getResources();
if (resources == null || resources.length > 1) {
event.getPresentation().setEnabled(false);
return;
}
final Resource resource = resources[0];
final Optional<Project> project = resource.getRelatedProject();
if (!project.isPresent()) {
event.getPresentation().setEnabled(false);
return;
}
final Optional<Resource> srcFolder = resource.getParentWithMarker(SourceFolderMarker.ID);
if (resource.getResourceType() == FILE) {
event.getPresentation().setEnabled(JavaUtil.isJavaProject(project.get()) && srcFolder.isPresent() && isJavaFile((File) resource));
} else if (resource instanceof Container) {
event.getPresentation().setEnabled(JavaUtil.isJavaProject(project.get()) && srcFolder.isPresent());
}
}
}
use of org.eclipse.che.ide.api.editor.EditorPartPresenter in project che by eclipse.
the class RenameRefactoringAction method actionPerformed.
@Override
public void actionPerformed(ActionEvent event) {
if (editorInFocus) {
final EditorPartPresenter editorPart = editorAgent.getActiveEditor();
if (editorPart == null || !(editorPart instanceof TextEditor)) {
return;
}
javaRefactoringRename.refactor((TextEditor) editorPart);
} else {
final Resource[] resources = appContext.getResources();
if (resources == null || resources.length > 1) {
return;
}
final Resource resource = resources[0];
final Optional<Project> project = resource.getRelatedProject();
if (!JavaUtil.isJavaProject(project.get())) {
return;
}
final Optional<Resource> srcFolder = resource.getParentWithMarker(SourceFolderMarker.ID);
if (!srcFolder.isPresent() || resource.getLocation().equals(srcFolder.get().getLocation())) {
return;
}
RefactoredItemType renamedItemType = null;
if (resource.getResourceType() == FILE && isJavaFile((File) resource)) {
renamedItemType = COMPILATION_UNIT;
} else if (resource instanceof Container) {
renamedItemType = PACKAGE;
}
if (renamedItemType == null) {
return;
}
renamePresenter.show(RefactorInfo.of(renamedItemType, resources));
}
}
Aggregations