Skip to content

Commit

Permalink
CHE-6813: Add search filter to Git branch dialogs (#7433)
Browse files Browse the repository at this point in the history
  • Loading branch information
vinokurig committed Nov 21, 2017
1 parent 4160bbd commit 366c105
Show file tree
Hide file tree
Showing 17 changed files with 423 additions and 162 deletions.
@@ -0,0 +1,107 @@
/**
* Copyright (c) 2012-2017 Red Hat, Inc. 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
*
* <p>Contributors: Red Hat, Inc. - initial API and implementation
*/
package org.eclipse.che.ide.ui.list;

import static com.google.gwt.event.dom.client.KeyCodes.KEY_BACKSPACE;
import static com.google.gwt.event.dom.client.KeyCodes.KEY_ESCAPE;

import com.google.gwt.user.client.ui.FocusPanel;
import java.util.Map;
import java.util.stream.Collectors;

/**
* A focus panel witch contain {@link SimpleList} widget. Supports filtering items according to
* typed from keyboard symbols.
*/
public class FilterableSimpleList<M> extends FocusPanel {
private Map<String, M> filterableItems;
private SimpleList<M> simpleList;
private StringBuilder filter;

public interface FilterChangedHandler {
/** Is called when search filter is changed */
void onFilterChanged(String filter);
}

private FilterableSimpleList(
SimpleList.View view,
SimpleList.Css css,
SimpleList.ListItemRenderer<M> itemRenderer,
SimpleList.ListEventDelegate<M> eventDelegate,
FilterChangedHandler filterChangedHandler) {
super();
simpleList = SimpleList.create(view, css, itemRenderer, eventDelegate);
this.getElement().setAttribute("style", "outline: 0");

addKeyDownHandler(
keyDownEvent -> {
int keyCode = keyDownEvent.getNativeEvent().getKeyCode();
if (keyCode == KEY_BACKSPACE) {
filter.deleteCharAt(filter.length() - 1);
filterChangedHandler.onFilterChanged(filter.toString());
} else if (keyCode == KEY_ESCAPE && !filter.toString().isEmpty()) {
clearFilter();
keyDownEvent.stopPropagation();
filterChangedHandler.onFilterChanged("");
} else {
return;
}
doFilter();
});

addKeyPressHandler(
keyPressEvent -> {
filter.append(String.valueOf(keyPressEvent.getCharCode()));
filterChangedHandler.onFilterChanged(filter.toString());
doFilter();
});

add(simpleList);
filter = new StringBuilder();
}

public static <M> FilterableSimpleList<M> create(
SimpleList.View view,
SimpleList.Css simpleListCss,
SimpleList.ListItemRenderer<M> itemRenderer,
SimpleList.ListEventDelegate<M> eventDelegate,
FilterChangedHandler filterChangedHandler) {
return new FilterableSimpleList<>(
view, simpleListCss, itemRenderer, eventDelegate, filterChangedHandler);
}

/**
* Render the list with given items.
*
* @param filterableItems map with filterable value as a key and the item as a value
*/
public void render(Map<String, M> filterableItems) {
this.filterableItems = filterableItems;
doFilter();
}

/** Reset the filter. */
public void clearFilter() {
filter.delete(0, filter.length() + 1);
}

/** Returns the selection model from parent {@link SimpleList} widget. */
public HasSelection<M> getSelectionModel() {
return simpleList.getSelectionModel();
}

private void doFilter() {
simpleList.render(
filterableItems
.keySet()
.stream()
.filter(name -> name.startsWith(filter.toString()))
.map(name -> filterableItems.get(name))
.collect(Collectors.toList()));
}
}
Expand Up @@ -334,7 +334,7 @@ private void maybeRemoveSelectionFromElement() {

private HTML widget;

private SimpleList(
SimpleList(
View view,
Element container,
Element itemHolder,
Expand Down
Expand Up @@ -360,12 +360,15 @@ public interface GitLocalizationConstant extends Messages {
@Key("view.branch.delete_ask")
String branchDeleteAsk(String name);

@Key("view.branch.filter.label")
String branchFilterLabel();
@Key("view.branch.local_remote_filter.label")
String branchLocalRemoteFilterLabel();

@Key("view.branch.title")
String branchTitle();

@Key("view.branch.search_filter.label")
String branchSearchFilterLabel();

// Commit
@Key("view.commit.commit_message")
String commitMessage(String revision, String time);
Expand Down
Expand Up @@ -21,7 +21,6 @@
import org.eclipse.che.api.git.shared.Branch;
import org.eclipse.che.api.git.shared.BranchListMode;
import org.eclipse.che.api.git.shared.CheckoutRequest;
import org.eclipse.che.ide.api.app.AppContext;
import org.eclipse.che.ide.api.notification.NotificationManager;
import org.eclipse.che.ide.api.resources.Project;
import org.eclipse.che.ide.dto.DtoFactory;
Expand Down Expand Up @@ -54,20 +53,18 @@ public class BranchPresenter implements BranchView.ActionDelegate {
private final DialogFactory dialogFactory;
private final GitServiceClient service;
private final GitLocalizationConstant constant;
private final AppContext appContext;
private final NotificationManager notificationManager;

private Branch selectedBranch;
private Project project;

/** Create presenter. */
@Inject
public BranchPresenter(
BranchPresenter(
BranchView view,
DtoFactory dtoFactory,
GitServiceClient service,
GitLocalizationConstant constant,
AppContext appContext,
NotificationManager notificationManager,
GitOutputConsoleFactory gitOutputConsoleFactory,
ProcessesPanelPresenter processesPanelPresenter,
Expand All @@ -80,7 +77,6 @@ public BranchPresenter(
this.view.setDelegate(this);
this.service = service;
this.constant = constant;
this.appContext = appContext;
this.notificationManager = notificationManager;
}

Expand All @@ -91,8 +87,10 @@ public void showBranches(Project project) {
}

@Override
public void onCloseClicked() {
public void onClose() {
view.close();
view.setTextToSearchFilterLabel("");
view.clearSearchFilter();
}

@Override
Expand Down Expand Up @@ -240,10 +238,15 @@ public void onBranchUnselected() {
}

@Override
public void onFilterValueChanged() {
public void onLocalRemoteFilterChanged() {
getBranches();
}

@Override
public void onSearchFilterChanged(String filter) {
view.setTextToSearchFilterLabel(filter);
}

@Override
public void onBranchSelected(@NotNull Branch branch) {
selectedBranch = branch;
Expand Down
Expand Up @@ -22,9 +22,9 @@
*/
public interface BranchView extends View<BranchView.ActionDelegate> {
/** Needs for delegate some function into Branch view. */
public interface ActionDelegate {
/** Performs any actions appropriate in response to the user having pressed the Close button. */
void onCloseClicked();
interface ActionDelegate {
/** Performs any actions appropriate in response to the user having pressed the Close action. */
void onClose();

/**
* Performs any actions appropriate in response to the user having pressed the Rename button.
Expand Down Expand Up @@ -56,8 +56,13 @@ public interface ActionDelegate {
/** Performs any action in response to the user do not have any selected branch. */
void onBranchUnselected();

/** Performs any action in response to the user having selected branch filter. */
void onFilterValueChanged();
/**
* Performs any action in response to the user having selected branch filter (local/remote/all).
*/
void onLocalRemoteFilterChanged();

/** Is called when search filter is updated */
void onSearchFilterChanged(String filter);
}

/**
Expand Down Expand Up @@ -96,4 +101,14 @@ public interface ActionDelegate {

/** Show dialog. */
void showDialogIfClosed();

/**
* Set new content to search filter label.
*
* @param text text to set
*/
void setTextToSearchFilterLabel(String text);

/** Clear search filter. */
void clearSearchFilter();
}

0 comments on commit 366c105

Please sign in to comment.