Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add hotkey for expand/collapse project tree (#3083)
* Add hotkey for expand/collapse project tree * Add unit tests for the actions * Move project explorer tree expander to dedicate class * Expose external JS API * Typo fix * Add new line
- Loading branch information
Vladyslav Zhukovskyi
committed
Nov 17, 2016
1 parent
196e0c6
commit 5fb8ed5
Showing
9 changed files
with
458 additions
and
3 deletions.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
ide/che-core-ide-api/src/main/java/org/eclipse/che/ide/api/data/tree/TreeExpander.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2012-2016 Codenvy, S.A. | ||
* 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: | ||
* Codenvy, S.A. - initial API and implementation | ||
*******************************************************************************/ | ||
package org.eclipse.che.ide.api.data.tree; | ||
|
||
import com.google.common.annotations.Beta; | ||
|
||
/** | ||
* Component which performs basic tree operation such as expand and collapse. | ||
* | ||
* @author Vlad Zhukovskyi | ||
* @since 5.0.0 | ||
*/ | ||
@Beta | ||
public interface TreeExpander { | ||
|
||
/** | ||
* Perform tree expand in case if {@link #isExpandEnabled()} returns {@code true}. | ||
*/ | ||
void expandTree(); | ||
|
||
/** | ||
* Returns {@code true} in case if tree expand is possible. | ||
* | ||
* @return {@code true} in case if tree expand is possible, otherwise {@code false} | ||
*/ | ||
boolean isExpandEnabled(); | ||
|
||
/** | ||
* Perform tree collapse in case if {@link #isCollapseEnabled()} returns {@code true}. | ||
*/ | ||
void collapseTree(); | ||
|
||
/** | ||
* Returns {@code true} in case if tree collapse is possible. | ||
* | ||
* @return {@code true} in case if tree collapse is possible, otherwise {@code false} | ||
*/ | ||
boolean isCollapseEnabled(); | ||
} |
53 changes: 53 additions & 0 deletions
53
...che-core-ide-app/src/main/java/org/eclipse/che/ide/actions/common/CollapseTreeAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2012-2016 Codenvy, S.A. | ||
* 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: | ||
* Codenvy, S.A. - initial API and implementation | ||
*******************************************************************************/ | ||
package org.eclipse.che.ide.actions.common; | ||
|
||
import org.eclipse.che.ide.api.action.Action; | ||
import org.eclipse.che.ide.api.action.ActionEvent; | ||
import org.eclipse.che.ide.api.data.tree.TreeExpander; | ||
|
||
import static com.google.common.base.Preconditions.checkNotNull; | ||
|
||
/** | ||
* Base tree collapse action which consumes instance of {@link TreeExpander}. | ||
* | ||
* @author Vlad Zhukovskyi | ||
* @see TreeExpander | ||
* @since 5.0.0 | ||
*/ | ||
public abstract class CollapseTreeAction extends Action { | ||
|
||
public abstract TreeExpander getTreeExpander(); | ||
|
||
public CollapseTreeAction() { | ||
super("Collapse All"); | ||
} | ||
|
||
@Override | ||
public void actionPerformed(ActionEvent e) { | ||
final TreeExpander treeExpander = getTreeExpander(); | ||
|
||
checkNotNull(treeExpander); | ||
|
||
if (!treeExpander.isCollapseEnabled()) { | ||
return; | ||
} | ||
|
||
treeExpander.collapseTree(); | ||
} | ||
|
||
@Override | ||
public void update(ActionEvent e) { | ||
final TreeExpander treeExpander = getTreeExpander(); | ||
|
||
e.getPresentation().setEnabledAndVisible(treeExpander.isCollapseEnabled()); | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
ide/che-core-ide-app/src/main/java/org/eclipse/che/ide/actions/common/ExpandTreeAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2012-2016 Codenvy, S.A. | ||
* 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: | ||
* Codenvy, S.A. - initial API and implementation | ||
*******************************************************************************/ | ||
package org.eclipse.che.ide.actions.common; | ||
|
||
import org.eclipse.che.ide.api.action.Action; | ||
import org.eclipse.che.ide.api.action.ActionEvent; | ||
import org.eclipse.che.ide.api.data.tree.TreeExpander; | ||
|
||
import static com.google.common.base.Preconditions.checkNotNull; | ||
|
||
/** | ||
* Base tree expand action which consumes instance of {@link TreeExpander}. | ||
* | ||
* @author Vlad Zhukovskyi | ||
* @see TreeExpander | ||
* @since 5.0.0 | ||
*/ | ||
public abstract class ExpandTreeAction extends Action { | ||
|
||
public abstract TreeExpander getTreeExpander(); | ||
|
||
public ExpandTreeAction() { | ||
super("Expand All"); | ||
} | ||
|
||
@Override | ||
public void actionPerformed(ActionEvent e) { | ||
final TreeExpander treeExpander = getTreeExpander(); | ||
|
||
checkNotNull(treeExpander); | ||
|
||
if (!treeExpander.isExpandEnabled()) { | ||
return; | ||
} | ||
|
||
treeExpander.expandTree(); | ||
} | ||
|
||
@Override | ||
public void update(ActionEvent e) { | ||
final TreeExpander treeExpander = getTreeExpander(); | ||
|
||
e.getPresentation().setEnabledAndVisible(treeExpander.isExpandEnabled()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
.../src/main/java/org/eclipse/che/ide/part/explorer/project/ProjectExplorerTreeExpander.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package org.eclipse.che.ide.part.explorer.project; | ||
|
||
import com.google.common.base.Predicate; | ||
|
||
import org.eclipse.che.api.promises.client.Operation; | ||
import org.eclipse.che.api.promises.client.OperationException; | ||
import org.eclipse.che.ide.api.app.AppContext; | ||
import org.eclipse.che.ide.api.data.tree.Node; | ||
import org.eclipse.che.ide.api.data.tree.TreeExpander; | ||
import org.eclipse.che.ide.api.resources.Resource; | ||
import org.eclipse.che.ide.ui.smartTree.Tree; | ||
|
||
import static com.google.common.base.Preconditions.checkNotNull; | ||
import static com.google.common.collect.Iterables.any; | ||
|
||
/** | ||
* Project explorer tree expander. Takes care about resources loading state, in case if tree has been never expanded before, it requests | ||
* infinite resource tree from the server and then expands project tree on the UI. On the second time it just expands project tree without | ||
* additional resource requests. | ||
* | ||
* @author Vlad Zhukovskyi | ||
* @since 5.0.0 | ||
* @see TreeExpander | ||
*/ | ||
final class ProjectExplorerTreeExpander implements TreeExpander { | ||
|
||
private Tree tree; | ||
private AppContext appContext; | ||
|
||
public ProjectExplorerTreeExpander(Tree tree, AppContext appContext) { | ||
this.tree = tree; | ||
this.appContext = appContext; | ||
} | ||
|
||
private final boolean[] everExpanded = new boolean[]{false}; | ||
|
||
@Override | ||
public void expandTree() { | ||
if (everExpanded[0]) { | ||
tree.expandAll(); | ||
|
||
return; | ||
} | ||
|
||
appContext.getWorkspaceRoot().getTree(-1).then(new Operation<Resource[]>() { | ||
@Override | ||
public void apply(Resource[] ignored) throws OperationException { | ||
everExpanded[0] = true; | ||
|
||
tree.expandAll(); | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
public boolean isExpandEnabled() { | ||
return tree.getNodeStorage().getAllItemsCount() != 0; | ||
} | ||
|
||
@Override | ||
public void collapseTree() { | ||
tree.collapseAll(); | ||
} | ||
|
||
@Override | ||
public boolean isCollapseEnabled() { | ||
return any(tree.getRootNodes(), isExpanded()); | ||
} | ||
|
||
private Predicate<Node> isExpanded() { | ||
return new Predicate<Node>() { | ||
@Override | ||
public boolean apply(@javax.annotation.Nullable Node node) { | ||
checkNotNull(node); | ||
|
||
return tree.isExpanded(node); | ||
} | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.