Search in sources :

Example 6 with InputValidator

use of org.eclipse.che.ide.api.dialogs.InputValidator in project che by eclipse.

the class RenameItemAction method actionPerformed.

/** {@inheritDoc} */
@Override
public void actionPerformed(ActionEvent e) {
    final Resource resource = appContext.getResource();
    checkState(resource != null, "Null resource occurred");
    final String resourceName = resource.getName();
    final int selectionLength = resourceName.indexOf('.') >= 0 ? resourceName.lastIndexOf('.') : resourceName.length();
    final InputValidator validator;
    final String dialogTitle;
    if (resource.getResourceType() == FILE) {
        validator = new FileNameValidator(resourceName);
        dialogTitle = localization.renameFileDialogTitle(resourceName);
    } else if (resource.getResourceType() == FOLDER) {
        validator = new FolderNameValidator(resourceName);
        dialogTitle = localization.renameFolderDialogTitle(resourceName);
    } else if (resource.getResourceType() == PROJECT) {
        validator = new ProjectNameValidator(resourceName);
        dialogTitle = localization.renameProjectDialogTitle(resourceName);
    } else {
        throw new IllegalStateException("Not a resource");
    }
    final InputCallback inputCallback = new InputCallback() {

        @Override
        public void accepted(final String value) {
            //we shouldn't perform renaming file with the same name
            if (!value.trim().equals(resourceName)) {
                closeRelatedEditors(resource);
                final Path destination = resource.getLocation().parent().append(value);
                resource.move(destination).catchError(new Operation<PromiseError>() {

                    @Override
                    public void apply(PromiseError arg) throws OperationException {
                        notificationManager.notify("", arg.getMessage(), FAIL, EMERGE_MODE);
                    }
                });
            }
        }
    };
    InputDialog inputDialog = dialogFactory.createInputDialog(dialogTitle, localization.renameDialogNewNameLabel(), resource.getName(), 0, selectionLength, inputCallback, null);
    inputDialog.withValidator(validator);
    inputDialog.show();
}
Also used : Path(org.eclipse.che.ide.resource.Path) InputDialog(org.eclipse.che.ide.api.dialogs.InputDialog) InputCallback(org.eclipse.che.ide.api.dialogs.InputCallback) Resource(org.eclipse.che.ide.api.resources.Resource) InputValidator(org.eclipse.che.ide.api.dialogs.InputValidator) PromiseError(org.eclipse.che.api.promises.client.PromiseError) OperationException(org.eclipse.che.api.promises.client.OperationException)

Example 7 with InputValidator

use of org.eclipse.che.ide.api.dialogs.InputValidator in project che by eclipse.

the class InputDialogPresenterTest method onEnterClickedWhenInputValueIsIncorrect.

@Test
public void onEnterClickedWhenInputValueIsIncorrect() throws Exception {
    reset(view);
    when(view.getValue()).thenReturn(INCORRECT_INPUT_VALUE);
    InputValidator inputValidator = mock(InputValidator.class);
    InputValidator.Violation violation = mock(InputValidator.Violation.class);
    when(inputValidator.validate(INCORRECT_INPUT_VALUE)).thenReturn(violation);
    when(violation.getMessage()).thenReturn(ERROR_MESSAGE);
    presenter.withValidator(inputValidator);
    presenter.onEnterClicked();
    verify(view).showErrorHint(anyString());
    verify(view, never()).hideErrorHint();
    verify(view, never()).setValue(anyString());
    verify(inputCallback, never()).accepted(anyString());
}
Also used : InputValidator(org.eclipse.che.ide.api.dialogs.InputValidator) Test(org.junit.Test) BaseTest(org.eclipse.che.ide.ui.dialogs.BaseTest)

Example 8 with InputValidator

use of org.eclipse.che.ide.api.dialogs.InputValidator in project che by eclipse.

the class InputDialogPresenterTest method shouldNotShowErrorHintWhenViolationHasCorrectValue.

@Test
public void shouldNotShowErrorHintWhenViolationHasCorrectValue() throws Exception {
    reset(view);
    when(view.getValue()).thenReturn(INCORRECT_INPUT_VALUE);
    InputValidator inputValidator = mock(InputValidator.class);
    InputValidator.Violation violation = mock(InputValidator.Violation.class);
    when(inputValidator.validate(INCORRECT_INPUT_VALUE)).thenReturn(violation);
    when(violation.getMessage()).thenReturn(null);
    when(violation.getCorrectedValue()).thenReturn(CORRECT_INPUT_VALUE);
    presenter.withValidator(inputValidator);
    presenter.inputValueChanged();
    verify(view, never()).showErrorHint(anyString());
    verify(view).hideErrorHint();
    verify(view).setValue(eq(CORRECT_INPUT_VALUE));
}
Also used : InputValidator(org.eclipse.che.ide.api.dialogs.InputValidator) Test(org.junit.Test) BaseTest(org.eclipse.che.ide.ui.dialogs.BaseTest)

Aggregations

InputValidator (org.eclipse.che.ide.api.dialogs.InputValidator)8 Test (org.junit.Test)7 BaseTest (org.eclipse.che.ide.ui.dialogs.BaseTest)4 Violation (org.eclipse.che.ide.api.dialogs.InputValidator.Violation)3 Matchers.anyString (org.mockito.Matchers.anyString)3 OperationException (org.eclipse.che.api.promises.client.OperationException)1 PromiseError (org.eclipse.che.api.promises.client.PromiseError)1 InputCallback (org.eclipse.che.ide.api.dialogs.InputCallback)1 InputDialog (org.eclipse.che.ide.api.dialogs.InputDialog)1 Resource (org.eclipse.che.ide.api.resources.Resource)1 Path (org.eclipse.che.ide.resource.Path)1