Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CHE-4939: Fix bugs related to processing of read-only files #5003

Merged
merged 1 commit into from
May 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,16 @@ public void apply(Optional<File> file) throws OperationException {
}
}

@Override
protected void updateDirtyState(boolean dirty) {
if (isReadOnly()) {
dirtyState = false;
return;
}

super.updateDirtyState(dirty);
}

private void updateTabReference(File file, Path oldPath) {
final PartPresenter activePart = editorMultiPartStackPresenter.getActivePart();
final EditorPartStack activePartStack = editorMultiPartStackPresenter.getPartStackByPart(activePart);
Expand Down Expand Up @@ -585,9 +595,10 @@ public void onFailure(final Throwable caught) {
@Override
public void doSave(final AsyncCallback<EditorInput> callback) {
//If the workspace is stopped we shouldn't try to save a file
if (appContext.getDevMachine() == null) {
if (isReadOnly() || appContext.getDevMachine() == null) {
return;
}

this.documentStorage.saveDocument(getEditorInput(), this.document, false, new AsyncCallback<EditorInput>() {
@Override
public void onSuccess(EditorInput editorInput) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import com.google.common.annotations.Beta;
import com.google.inject.Inject;
import com.google.inject.assistedinject.Assisted;
import com.google.web.bindery.event.shared.EventBus;
import com.google.web.bindery.event.shared.HandlerRegistration;

import org.eclipse.che.api.promises.client.Function;
import org.eclipse.che.api.promises.client.FunctionException;
Expand All @@ -22,6 +24,12 @@
import org.eclipse.che.ide.api.data.tree.Node;
import org.eclipse.che.ide.api.data.tree.settings.NodeSettings;
import org.eclipse.che.ide.api.editor.EditorAgent;
import org.eclipse.che.ide.api.editor.EditorPartPresenter;
import org.eclipse.che.ide.api.event.FileEvent;
import org.eclipse.che.ide.api.event.FileEvent.FileEventHandler;
import org.eclipse.che.ide.api.resources.ResourceChangedEvent;
import org.eclipse.che.ide.api.resources.ResourceChangedEvent.ResourceChangedHandler;
import org.eclipse.che.ide.api.resources.ResourceDelta;
import org.eclipse.che.ide.api.resources.VirtualFile;
import org.eclipse.che.ide.api.theme.Style;
import org.eclipse.che.ide.ext.java.client.JavaResources;
Expand All @@ -38,22 +46,27 @@
import java.util.List;

import static java.util.Collections.singletonList;
import static org.eclipse.che.ide.api.resources.ResourceDelta.REMOVED;

/**
* It might be used for any jar content.
*
* @author Vlad Zhukovskiy
*/
@Beta
public class JarFileNode extends SyntheticNode<JarEntry> implements VirtualFile, HasAction {
public class JarFileNode extends SyntheticNode<JarEntry> implements VirtualFile, HasAction, FileEventHandler, ResourceChangedHandler {

private final int libId;
private final Path project;
private final JavaResources javaResources;
private final NodesResources nodesResources;
private final JavaNavigationService service;
private final EditorAgent editorAgent;
private boolean contentGenerated;
private final EventBus eventBus;

private HandlerRegistration fileEventHandlerRegistration;
private HandlerRegistration resourceChangeHandlerRegistration;
private boolean contentGenerated;

@Inject
public JarFileNode(@Assisted JarEntry jarEntry,
Expand All @@ -63,14 +76,16 @@ public JarFileNode(@Assisted JarEntry jarEntry,
JavaResources javaResources,
NodesResources nodesResources,
JavaNavigationService service,
EditorAgent editorAgent) {
EditorAgent editorAgent,
EventBus eventBus) {
super(jarEntry, nodeSettings);
this.libId = libId;
this.project = project;
this.javaResources = javaResources;
this.nodesResources = nodesResources;
this.service = service;
this.editorAgent = editorAgent;
this.eventBus = eventBus;

getAttributes().put(CUSTOM_BACKGROUND_FILL, singletonList(Style.theme.projectExplorerReadonlyItemBackground()));
}
Expand All @@ -85,6 +100,10 @@ protected Promise<List<Node>> getChildrenImpl() {
/** {@inheritDoc} */
@Override
public void actionPerformed() {
if (fileEventHandlerRegistration == null) {
fileEventHandlerRegistration = eventBus.addHandler(FileEvent.TYPE, this);
}

editorAgent.openEditor(this);
}

Expand Down Expand Up @@ -179,4 +198,58 @@ public boolean isContentGenerated() {
public Path getProject() {
return project;
}

@Override
public void onFileOperation(FileEvent event) {
Path filePath = event.getFile().getLocation();
Path currentPath = getLocation();

if (!filePath.equals(currentPath)) {
return;
}

switch (event.getOperationType()) {
case OPEN: {
if (resourceChangeHandlerRegistration == null) {
resourceChangeHandlerRegistration = eventBus.addHandler(ResourceChangedEvent.getType(), this);
}

break;
}

case CLOSE: {
if (resourceChangeHandlerRegistration != null) {
resourceChangeHandlerRegistration.removeHandler();
resourceChangeHandlerRegistration = null;
}

break;
}
}
}

@Override
public void onResourceChanged(ResourceChangedEvent event) {
ResourceDelta delta = event.getDelta();
Path resourceLocation = delta.getResource().getLocation();

if (REMOVED == delta.getKind() && project.equals(resourceLocation)) {
EditorPartPresenter editorPart = editorAgent.getOpenedEditor(getLocation());
editorAgent.closeEditor(editorPart);

removeHandlers();
}
}

private void removeHandlers() {
if (fileEventHandlerRegistration != null) {
fileEventHandlerRegistration.removeHandler();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Set fileEventHandlerRegistration to null.

fileEventHandlerRegistration = null;
}

if (resourceChangeHandlerRegistration != null) {
resourceChangeHandlerRegistration.removeHandler();
resourceChangeHandlerRegistration = null;
}
}
}