Skip to content

Commit

Permalink
CHE-642. Correct handling buttons on dialogs when Enter is pushed
Browse files Browse the repository at this point in the history
Signed-off-by: Roman Nikitenko <rnikitenko@codenvy.com>
  • Loading branch information
RomanNikitenko committed Mar 11, 2016
1 parent bff2f2d commit de4e8fe
Show file tree
Hide file tree
Showing 23 changed files with 557 additions and 53 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*******************************************************************************
* 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.ui;

import com.google.gwt.event.dom.client.BlurEvent;
import com.google.gwt.event.dom.client.BlurHandler;
import com.google.gwt.event.dom.client.FocusEvent;
import com.google.gwt.event.dom.client.FocusHandler;
import com.google.gwt.user.client.ui.FocusWidget;
import com.google.inject.Singleton;

import java.util.HashMap;
import java.util.Map;

import static com.google.common.base.MoreObjects.firstNonNull;

/**
* The class contains business logic which allows to track the focus for widgets.
*
* @author Roman Nikitenko
*/
@Singleton
public class WidgetFocusTracker {

private Map<FocusWidget, Boolean> focusStates;

/**
* Add widget to track the focus
*
* @param widget
* the widget to track
*/
public void subscribe(final FocusWidget widget) {
if (focusStates == null) {
focusStates = new HashMap<>();
}

focusStates.put(widget, false);
widget.addFocusHandler(new FocusHandler() {
@Override
public void onFocus(FocusEvent event) {
focusStates.put(widget, true);
}
});

widget.addBlurHandler(new BlurHandler() {
@Override
public void onBlur(BlurEvent event) {
focusStates.put(widget, false);
}
});
}

/**
* Unsubscribe widget from tracking the focus
*
* @param widget
* the widget to unsubscribe from tracking the focus
*/
public void unSubscribe(FocusWidget widget) {
if (widget == null) {
return;
}
focusStates.remove(widget);
}

/**
* Returns {@code true} if widget is in the focus and {@code false} - otherwise.
* Note: this method returns {@code false} if widget hasn't subscribed to track the focus.
*/
public boolean isWidgetFocused(FocusWidget widget) {
return widget != null && firstNonNull(focusStates.get(widget), false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,24 @@ public void setPathDirectory(String path) {
public void setFocus() {
view.setFocus();
}

@Override
public void onEnterClicked() {
if (view.isAcceptButtonInFocus()) {
String searchText = view.getSearchText();
if (!searchText.isEmpty()) {
search(searchText);
}
return;
}

if (view.isCancelButtonInFocus()) {
view.close();
return;
}

if (view.isSelectPathButtonInFocus()) {
view.showSelectPathDialog();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ interface ActionDelegate {

/** Set focus to root window. */
void setFocus();

/** Called when Enter clicked */
void onEnterClicked();
}

/** Shows error message on the view */
Expand All @@ -42,6 +45,17 @@ interface ActionDelegate {
/** Set focus to root window. */
void setFocus();

/** Returns {@code true} if accept button is in the focus and {@code false} - otherwise. */
boolean isAcceptButtonInFocus();

/** Returns {@code true} if Cancel button is in the focus and {@code false} - otherwise. */
boolean isCancelButtonInFocus();

/** Returns {@code true} if 'select path' button is in the focus and {@code false} - otherwise. */
boolean isSelectPathButtonInFocus();

void showSelectPathDialog();

/** Close dialog. */
void close();

Expand All @@ -51,6 +65,9 @@ interface ActionDelegate {
/** Seth path of directory to search */
void setPathDirectory(String path);

/** Returns text for searching */
String getSearchText();

/** Returns file mask for searching */
String getFileMask();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.eclipse.che.ide.CoreLocalizationConstant;
import org.eclipse.che.ide.api.app.AppContext;
import org.eclipse.che.ide.search.selectpath.SelectPathPresenter;
import org.eclipse.che.ide.ui.WidgetFocusTracker;
import org.eclipse.che.ide.ui.window.Window;

/**
Expand Down Expand Up @@ -61,24 +62,32 @@ interface FullTextSearchViewImplUiBinder extends UiBinder<Widget, FullTextSearch
@UiField
Button selectPathButton;

Button cancelButton;
Button acceptButton;

private ActionDelegate delegate;
private Button accept;

private final SelectPathPresenter selectPathPresenter;
private final WidgetFocusTracker widgetFocusTracker;

@Inject
public FullTextSearchViewImpl(CoreLocalizationConstant locale,
final SelectPathPresenter selectPathPresenter,
FullTextSearchViewImplUiBinder uiBinder,
AppContext appContext) {
AppContext appContext,
WidgetFocusTracker widgetFocusTracker) {
this.locale = locale;
this.appContext = appContext;
this.selectPathPresenter = selectPathPresenter;
this.widgetFocusTracker = widgetFocusTracker;

setTitle(locale.textSearchTitle());

Widget widget = uiBinder.createAndBindUi(this);
setWidget(widget);

createButtons();
addHandlers(selectPathPresenter);
addHandlers();
}

@Override
Expand All @@ -89,11 +98,12 @@ public void setDelegate(ActionDelegate delegate) {
@Override
public void close() {
hide();
unTrackFocusForWidgets();
}

@Override
public void showDialog() {
accept.setEnabled(false);
acceptButton.setEnabled(false);
isUseFileMask.setValue(false);
filesMask.setEnabled(false);
isUseDirectory.setValue(false);
Expand All @@ -112,13 +122,19 @@ public void run() {
}.schedule(100);

super.show();
trackFocusForWidgets();
}

@Override
public void setPathDirectory(String path) {
directory.setText(path);
}

@Override
public String getSearchText() {
return text.getText();
}

@Override
public String getFileMask() {
return isUseFileMask.getValue() ? filesMask.getText() : "";
Expand All @@ -136,14 +152,12 @@ public void showErrorMessage(String message) {

@Override
protected void onClose() {
//Do nothing
unTrackFocusForWidgets();
}

@Override
protected void onEnterClicked() {
if (!text.getText().isEmpty()) {
delegate.search(text.getText());
}
delegate.onEnterClicked();
}

@Override
Expand All @@ -153,29 +167,61 @@ public void clearInput() {

@Override
public void setFocus() {
accept.setFocus(true);
acceptButton.setFocus(true);
}

@Override
public boolean isAcceptButtonInFocus() {
return widgetFocusTracker.isWidgetFocused(acceptButton);
}

@Override
public boolean isCancelButtonInFocus() {
return widgetFocusTracker.isWidgetFocused(cancelButton);
}

@Override
public boolean isSelectPathButtonInFocus() {
return widgetFocusTracker.isWidgetFocused(selectPathButton);
}

private void trackFocusForWidgets() {
widgetFocusTracker.subscribe(acceptButton);
widgetFocusTracker.subscribe(cancelButton);
widgetFocusTracker.subscribe(selectPathButton);
}

private void unTrackFocusForWidgets() {
widgetFocusTracker.unSubscribe(acceptButton);
widgetFocusTracker.unSubscribe(cancelButton);
widgetFocusTracker.unSubscribe(selectPathButton);
}

@Override
public void showSelectPathDialog() {
selectPathPresenter.show(delegate);
}

private void createButtons() {
Button cancel = createButton(locale.cancel(), "search-cancel-button", new ClickHandler() {
cancelButton = createButton(locale.cancel(), "search-cancel-button", new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
hide();
close();
}
});

accept = createPrimaryButton(locale.search(), "search-button", new ClickHandler() {
acceptButton = createPrimaryButton(locale.search(), "search-button", new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
delegate.search(text.getText());
}
});

addButtonToFooter(accept);
addButtonToFooter(cancel);
addButtonToFooter(acceptButton);
addButtonToFooter(cancelButton);
}

private void addHandlers(final SelectPathPresenter selectPathPresenter) {
private void addHandlers() {
isUseFileMask.addValueChangeHandler(new ValueChangeHandler<Boolean>() {
@Override
public void onValueChange(ValueChangeEvent<Boolean> event) {
Expand All @@ -194,16 +240,15 @@ public void onValueChange(ValueChangeEvent<Boolean> event) {
text.addKeyUpHandler(new KeyUpHandler() {
@Override
public void onKeyUp(KeyUpEvent event) {
accept.setEnabled(!text.getValue().isEmpty());
acceptButton.setEnabled(!text.getValue().isEmpty());
}
});

selectPathButton.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
selectPathPresenter.show(delegate);
showSelectPathDialog();
}
});
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@
import java.util.ArrayList;
import java.util.List;

import static org.mockito.Matchers.anyObject;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
Expand Down Expand Up @@ -143,4 +145,37 @@ public void searchHasDoneWithSomeError() throws Exception {

verify(view).showErrorMessage(SEARCHED_TEXT);
}
}

@Test
public void onEnterClickedWhenAcceptButtonInFocus() throws Exception {
when(view.getSearchText()).thenReturn(SEARCHED_TEXT);
when(view.isAcceptButtonInFocus()).thenReturn(true);

fullTextSearchPresenter.onEnterClicked();

verify(view).getSearchText();
verify(projectServiceClient).search(anyString(), (QueryExpression)anyObject());
}

@Test
public void onEnterClickedWhenCancelButtonInFocus() throws Exception {
when(view.isCancelButtonInFocus()).thenReturn(true);

fullTextSearchPresenter.onEnterClicked();

verify(view).close();
verify(view, never()).getSearchText();
verify(projectServiceClient, never()).search(anyString(), (QueryExpression)anyObject());
}

@Test
public void onEnterClickedWhenSelectPathButtonInFocus() throws Exception {
when(view.isSelectPathButtonInFocus()).thenReturn(true);

fullTextSearchPresenter.onEnterClicked();

verify(view).showSelectPathDialog();
verify(view, never()).getSearchText();
verify(projectServiceClient, never()).search(anyString(), (QueryExpression)anyObject());
}
}
Loading

0 comments on commit de4e8fe

Please sign in to comment.