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

Do not set focus on project tree when 'Link with editor' feature is applied #6579

Merged
merged 1 commit into from Oct 10, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -178,7 +178,7 @@ public void onActivePartChanged(ActivePartChangedEvent event) {
preferencesManager.getValue(LinkWithEditorAction.LINK_WITH_EDITOR);
if (parseBoolean(isLinkedWithEditor)) {
final VirtualFile file = activeEditor.getEditorInput().getFile();
eventBus.fireEvent(new RevealResourceEvent(file.getLocation()));
eventBus.fireEvent(new RevealResourceEvent(file.getLocation(), true, false));
}
}

Expand Down
Expand Up @@ -59,6 +59,7 @@ public class TreeResourceRevealer {
private Tree tree;

private Node[] toSelect = null;
private boolean isFocusRequired = true;

private DelayedTask selectTask =
new DelayedTask() {
Expand All @@ -69,10 +70,11 @@ public void onExecute() {

if (copy.length == 1) {
tree.getSelectionModel().select(copy[0], false);
tree.scrollIntoView(copy[0]);
tree.scrollIntoView(copy[0], isFocusRequired);
}

toSelect = null;
isFocusRequired = true;
}
}
};
Expand All @@ -93,7 +95,10 @@ public void onRevealResource(final RevealResourceEvent event) {
new Function<Void, Promise<Void>>() {
@Override
public Promise<Void> apply(Void ignored) throws FunctionException {
return reveal(event.getLocation())
return reveal(
event.getLocation(),
event.isSelectionRequired(),
event.isFocusRequired())
.catchError(
new Function<PromiseError, Void>() {
@Override
Expand Down Expand Up @@ -125,7 +130,19 @@ public Promise<Node> reveal(final Path path) {
* @return promise object with found node or promise error if node wasn't found
*/
public Promise<Node> reveal(final Path path, final boolean select) {
return reveal(path, select, select);
}

/**
* Search node in the project explorer tree by storable path.
*
* @param path path to node
* @param select select node after reveal
* @param isFocusRequired whether tree should take focus after reveal
* @return promise object with found node or promise error if node wasn't found
*/
public Promise<Node> reveal(
final Path path, final boolean select, final boolean isFocusRequired) {
return queue.thenPromise(
new Function<Void, Promise<Node>>() {
@Override
Expand All @@ -134,14 +151,18 @@ public Promise<Node> apply(Void ignored) throws FunctionException {
new RequestCall<Node>() {
@Override
public void makeCall(AsyncCallback<Node> callback) {
reveal(path, select, callback);
reveal(path, select, isFocusRequired, callback);
}
});
}
});
}

protected void reveal(final Path path, final boolean select, final AsyncCallback<Node> callback) {
protected void reveal(
final Path path,
final boolean select,
final boolean isFocusRequired,
final AsyncCallback<Node> callback) {
if (path == null) {
callback.onFailure(new IllegalArgumentException("Invalid search path"));
}
Expand Down Expand Up @@ -169,7 +190,7 @@ public boolean execute() {
return false;
}

expandToPath(root, path, select)
expandToPath(root, path, select, isFocusRequired)
.then(
new Operation<ResourceNode>() {
@Override
Expand All @@ -192,12 +213,15 @@ public void apply(PromiseError arg) throws OperationException {
}

private Promise<ResourceNode> expandToPath(
final ResourceNode root, final Path path, final boolean select) {
final ResourceNode root,
final Path path,
final boolean select,
final boolean isFocusRequired) {
return createFromAsyncRequest(
new RequestCall<ResourceNode>() {
@Override
public void makeCall(final AsyncCallback<ResourceNode> callback) {
expand(root, path, select, callback);
expand(root, path, select, isFocusRequired, callback);
}
});
}
Expand All @@ -206,10 +230,13 @@ protected void expand(
final ResourceNode parent,
final Path segment,
final boolean select,
final boolean isFocusRequired,
final AsyncCallback<ResourceNode> callback) {

if (parent.getData().getLocation().equals(segment)) {
if (select) {
this.isFocusRequired = isFocusRequired;

if (toSelect == null) {
toSelect = new Node[] {parent};
} else {
Expand Down Expand Up @@ -247,7 +274,7 @@ public void onPostLoad(PostLoadEvent event) {
for (Node child : children) {
if (child instanceof ResourceNode
&& ((ResourceNode) child).getData().getLocation().isPrefixOf(segment)) {
expand((ResourceNode) child, segment, select, callback);
expand((ResourceNode) child, segment, select, isFocusRequired, callback);
return;
}
}
Expand Down
Expand Up @@ -58,6 +58,8 @@ public static Type<RevealResourceHandler> getType() {
}

private Path location;
private boolean isSelectionRequired = true;
private boolean isFocusRequired = true;

public RevealResourceEvent(Resource resource) {
this.location = checkNotNull(resource, "Resource should not be a null").getLocation();
Expand All @@ -67,6 +69,20 @@ public RevealResourceEvent(Path location) {
this.location = checkNotNull(location, "Path should not be a null");
}

/**
* Creates new event to reveal given resource.
*
* @param location the resource path which should be revealed
* @param isSelectionRequired whether corresponding node should be selected after reveal
* @param isFocusRequired whether corresponding node should be focused after reveal
*/
public RevealResourceEvent(
Path location, boolean isSelectionRequired, final boolean isFocusRequired) {
this.location = checkNotNull(location, "Path should not be a null");
this.isSelectionRequired = isSelectionRequired;
this.isFocusRequired = isFocusRequired;
}

/**
* Returns the resource path which should be revealed.
*
Expand All @@ -77,6 +93,26 @@ public Path getLocation() {
return location;
}

/**
* Returns whether corresponding node should be selected after reveal
*
* @return {@code true} if corresponding node should be selected after reveal or {@code false}
* otherwise
*/
public boolean isSelectionRequired() {
return isSelectionRequired;
}

/**
* Returns whether corresponding node should be focused after reveal
*
* @return {@code true} if corresponding node should be focused after reveal or {@code false}
* otherwise
*/
public boolean isFocusRequired() {
return isFocusRequired;
}

/** {@inheritDoc} */
@Override
public Type<RevealResourceHandler> getAssociatedType() {
Expand Down
Expand Up @@ -631,11 +631,24 @@ && getNodeDescriptor(node).isLoaded()
}

/**
* Scroll focus element into specific node.
* Scroll focus element into specific node and set focus on the tree. Use {@link
* #scrollIntoView(Node, boolean)} when setting focus is not required.
*
* @param node node to scroll
*/
public void scrollIntoView(Node node) {
scrollIntoView(node, true);
}

/**
* Scroll focus element into specific node. Set focus on the tree when {@code isFocusRequired} is
* {@code true}. Does not perform any operations with focus when {@code isFocusRequired} is {@code
* false}
*
* @param node node to scroll
* @param isFocusRequired whether tree should take focus after scroll
*/
public void scrollIntoView(Node node, boolean isFocusRequired) {
checkNotNull(node, NULL_NODE_MSG);
NodeDescriptor descriptor = getNodeDescriptor(node);
if (descriptor == null) {
Expand All @@ -648,7 +661,10 @@ public void scrollIntoView(Node node) {
container.scrollIntoView();
focusEl.getStyle().setLeft((nodeStorage.getDepth(node) - 1) * 16, Style.Unit.PX);
focusEl.getStyle().setTop(container.getOffsetTop(), Style.Unit.PX);
setFocus(true);

if (isFocusRequired) {
setFocus(true);
}
}

@Override
Expand Down