|
| 1 | +/* |
| 2 | + * Copyright 2017 Crown Copyright |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package stroom.dashboard.client.input; |
| 18 | + |
| 19 | +import stroom.dashboard.client.main.AbstractComponentPresenter; |
| 20 | +import stroom.dashboard.client.main.ComponentChangeEvent; |
| 21 | +import stroom.dashboard.client.main.ComponentRegistry.ComponentType; |
| 22 | +import stroom.dashboard.client.main.ComponentRegistry.ComponentUse; |
| 23 | +import stroom.dashboard.client.input.TextInputPresenter.TextInputView; |
| 24 | +import stroom.dashboard.client.main.HasParams; |
| 25 | +import stroom.dashboard.shared.ComponentConfig; |
| 26 | +import stroom.dashboard.shared.ComponentSettings; |
| 27 | +import stroom.dashboard.shared.TextInputComponentSettings; |
| 28 | +import stroom.query.api.v2.Param; |
| 29 | + |
| 30 | +import com.google.inject.Inject; |
| 31 | +import com.google.inject.Provider; |
| 32 | +import com.google.web.bindery.event.shared.EventBus; |
| 33 | +import com.gwtplatform.mvp.client.HasUiHandlers; |
| 34 | +import com.gwtplatform.mvp.client.View; |
| 35 | + |
| 36 | +import java.util.ArrayList; |
| 37 | +import java.util.List; |
| 38 | + |
| 39 | +public class TextInputPresenter |
| 40 | + extends AbstractComponentPresenter<TextInputView> |
| 41 | + implements TextInputUiHandlers, HasParams { |
| 42 | + |
| 43 | + public static final ComponentType TYPE = |
| 44 | + new ComponentType(1, |
| 45 | + "text-input", |
| 46 | + "Text Input", |
| 47 | + ComponentUse.INPUT); |
| 48 | + |
| 49 | + @Inject |
| 50 | + public TextInputPresenter(final EventBus eventBus, |
| 51 | + final TextInputView view, |
| 52 | + final Provider<TextInputSettingsPresenter> settingsPresenterProvider) { |
| 53 | + super(eventBus, view, settingsPresenterProvider); |
| 54 | + view.setUiHandlers(this); |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public void onValueChanged(final String value) { |
| 59 | + setSettings(getTextInputSettings().copy().value(value).build()); |
| 60 | + ComponentChangeEvent.fire(this, this); |
| 61 | + setDirty(true); |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public List<Param> getParams() { |
| 66 | + final List<Param> list = new ArrayList<>(); |
| 67 | + final String key = getTextInputSettings().getKey(); |
| 68 | + final String value = getView().getValue(); |
| 69 | + if (key != null && key.trim().length() > 0 && value != null && value.trim().length() > 0) { |
| 70 | + final Param param = new Param(key.trim(), value.trim()); |
| 71 | + list.add(param); |
| 72 | + } |
| 73 | + return list; |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public void read(final ComponentConfig componentConfig) { |
| 78 | + super.read(componentConfig); |
| 79 | + |
| 80 | + ComponentSettings settings = componentConfig.getSettings(); |
| 81 | + if (!(settings instanceof TextInputComponentSettings)) { |
| 82 | + setSettings(createSettings()); |
| 83 | + } |
| 84 | + |
| 85 | + update(getTextInputSettings()); |
| 86 | + } |
| 87 | + |
| 88 | + private void update(final TextInputComponentSettings settings) { |
| 89 | + getView().setValue(settings.getValue()); |
| 90 | + } |
| 91 | + |
| 92 | + private TextInputComponentSettings getTextInputSettings() { |
| 93 | + return (TextInputComponentSettings) getSettings(); |
| 94 | + } |
| 95 | + |
| 96 | + private TextInputComponentSettings createSettings() { |
| 97 | + return TextInputComponentSettings.builder().build(); |
| 98 | + } |
| 99 | + |
| 100 | + |
| 101 | + @Override |
| 102 | + public void link() { |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public void changeSettings() { |
| 107 | + super.changeSettings(); |
| 108 | + update(getTextInputSettings()); |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + public ComponentType getType() { |
| 113 | + return TYPE; |
| 114 | + } |
| 115 | + |
| 116 | + public interface TextInputView extends View, HasUiHandlers<TextInputUiHandlers> { |
| 117 | + |
| 118 | + void setValue(String value); |
| 119 | + |
| 120 | + String getValue(); |
| 121 | + } |
| 122 | +} |
0 commit comments