Java tutorial
/* * #%L * GeoWE Project * %% * Copyright (C) 2015 - 2016 GeoWE.org * %% * This file is part of GeoWE.org. * * GeoWE is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * GeoWE is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with GeoWE. If not, see <http://www.gnu.org/licenses/>. * #L% */ package org.geowe.client.local.main.tool.search; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import javax.annotation.PostConstruct; import javax.enterprise.context.ApplicationScoped; import javax.inject.Inject; import org.geowe.client.local.ImageProvider; import org.geowe.client.local.main.tool.info.FeatureTool; import org.geowe.client.local.main.tool.search.searcher.ContainSearcher; import org.geowe.client.local.main.tool.search.searcher.EndSearcher; import org.geowe.client.local.main.tool.search.searcher.EqualSearcher; import org.geowe.client.local.main.tool.search.searcher.GreaterThanSearcher; import org.geowe.client.local.main.tool.search.searcher.Searcher; import org.geowe.client.local.main.tool.search.searcher.SmallerThanSearcher; import org.geowe.client.local.main.tool.search.searcher.StartSearcher; import org.geowe.client.local.messages.UIMessages; import org.geowe.client.local.model.vector.FeatureAttributeDef; import org.geowe.client.local.model.vector.VectorLayer; import org.geowe.client.local.ui.KeyShortcutHandler; import org.geowe.client.local.ui.MessageDialogBuilder; import org.geowe.client.local.ui.PagingFeatureGrid; import org.gwtopenmaps.openlayers.client.feature.VectorFeature; import org.slf4j.Logger; import com.google.gwt.event.dom.client.KeyCodes; import com.google.gwt.event.logical.shared.SelectionEvent; import com.google.gwt.event.logical.shared.SelectionHandler; import com.google.gwt.user.client.ui.HorizontalPanel; import com.google.gwt.user.client.ui.Label; import com.google.gwt.user.client.ui.VerticalPanel; import com.google.gwt.user.client.ui.Widget; import com.sencha.gxt.cell.core.client.form.ComboBoxCell.TriggerAction; import com.sencha.gxt.core.client.resources.ThemeStyles; import com.sencha.gxt.data.shared.LabelProvider; import com.sencha.gxt.data.shared.ListStore; import com.sencha.gxt.data.shared.ModelKeyProvider; import com.sencha.gxt.widget.core.client.Dialog; import com.sencha.gxt.widget.core.client.button.TextButton; import com.sencha.gxt.widget.core.client.container.HorizontalLayoutContainer; import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer; import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer.VerticalLayoutData; import com.sencha.gxt.widget.core.client.form.CheckBox; import com.sencha.gxt.widget.core.client.form.ComboBox; import com.sencha.gxt.widget.core.client.form.TextField; import com.sencha.gxt.widget.core.client.info.Info; import com.sencha.gxt.widget.core.client.menu.Menu; import com.sencha.gxt.widget.core.client.menu.MenuItem; import com.sencha.gxt.widget.core.client.selection.SelectionChangedEvent; import com.sencha.gxt.widget.core.client.selection.SelectionChangedEvent.SelectionChangedHandler; import com.sencha.gxt.widget.core.client.toolbar.PagingToolBar; /** * Dilogo para la bsqueda de elementos, por atributo, de la capa seleccionada * * @author geowe * */ @ApplicationScoped public class AttributeSearchDialog extends Dialog { public static final int FEATURES_PER_PAGE = 50; private static final String FIELD_WIDTH = "225px"; @Inject private Logger logger; @Inject private LayerSearchToolBar layerSearchToolBar; @Inject private MessageDialogBuilder messageDialogBuilder; private TextField layerNameField; private TextField valueAttributeField; private TextButton searchButton; private VectorLayer selectedLayer; private PagingFeatureGrid featureGrid; private ComboBox<FeatureAttributeDef> attributeCombo; private CheckBox isCaseSensitive; public AttributeSearchDialog() { super(); this.getHeader().setIcon(ImageProvider.INSTANCE.layer16()); this.setHeadingText(UIMessages.INSTANCE.asdTitle()); this.setPredefinedButtons(PredefinedButton.CLOSE); this.setPixelSize(560, 520); this.setModal(false); this.setResizable(false); this.setHideOnButtonClick(true); } @PostConstruct private void initialize() { add(createPanel()); addKeyShortcuts(); } private Widget createPanel() { VerticalPanel vPanel = new VerticalPanel(); vPanel.setPixelSize(490, 420); vPanel.setSpacing(5); vPanel.add(createTopPanel()); vPanel.add(createBottomPanel()); return vPanel; } private HorizontalLayoutContainer createTopPanel() { HorizontalLayoutContainer hPanel = new HorizontalLayoutContainer(); VerticalPanel infoPanel = new VerticalPanel(); infoPanel.setSpacing(5); layerNameField = new TextField(); layerNameField.setEnabled(false); layerNameField.setWidth(FIELD_WIDTH); infoPanel.add(new Label(UIMessages.INSTANCE.asdLayerNameLabel())); infoPanel.add(layerNameField); infoPanel.add(createAttrPanel()); infoPanel.add(createSearchButtonPanel()); hPanel.add(infoPanel); return hPanel; } private HorizontalPanel createAttrPanel() { HorizontalPanel hPanel = new HorizontalPanel(); hPanel.setSpacing(10); hPanel.addStyleName(ThemeStyles.get().style().borderBottom()); hPanel.addStyleName(ThemeStyles.get().style().borderTop()); hPanel.add(getAttrCombo()); hPanel.add(getAttrValuePanel()); return hPanel; } private VerticalPanel getAttrCombo() { VerticalPanel vPanel = new VerticalPanel(); vPanel.setSpacing(5); vPanel.add(new Label(UIMessages.INSTANCE.asdAttributeLabel())); initializeAttributeLabelCombo(FIELD_WIDTH); vPanel.add(attributeCombo); return vPanel; } private VerticalPanel getAttrValuePanel() { VerticalPanel vPanel = new VerticalPanel(); vPanel.setSpacing(5); valueAttributeField = new TextField(); valueAttributeField.setEnabled(true); valueAttributeField.setWidth(FIELD_WIDTH); vPanel.add(new Label(UIMessages.INSTANCE.asdAttributeValueLabel())); vPanel.add(valueAttributeField); return vPanel; } private HorizontalLayoutContainer createBottomPanel() { HorizontalLayoutContainer hPanel = new HorizontalLayoutContainer(); hPanel.setSize("510px", "220px"); PagingToolBar toolBar = new PagingToolBar(FEATURES_PER_PAGE); toolBar.setBorders(false); featureGrid = new PagingFeatureGrid(toolBar); featureGrid.getSelectionModel().addSelectionChangedHandler(new SelectionChangedHandler<VectorFeature>() { @Override public void onSelectionChanged(SelectionChangedEvent<VectorFeature> event) { setSelectedElements(); } }); VerticalLayoutContainer gridContainer = new VerticalLayoutContainer(); gridContainer.setWidth(500); gridContainer.setHeight(220); gridContainer.add(featureGrid, new VerticalLayoutData(1, 1)); gridContainer.add(toolBar, new VerticalLayoutData(1, -1)); hPanel.add(gridContainer); hPanel.add(layerSearchToolBar); return hPanel; } private HorizontalPanel createSearchButtonPanel() { HorizontalPanel hPanel = new HorizontalPanel(); hPanel.setSpacing(10); searchButton = getSearchMenuButton(); hPanel.add(searchButton); isCaseSensitive = new CheckBox(); isCaseSensitive.setBoxLabel(UIMessages.INSTANCE.caseSensitive()); isCaseSensitive.setValue(true); hPanel.add(isCaseSensitive); return hPanel; } private TextButton getSearchMenuButton() { TextButton searchButton = new TextButton(UIMessages.INSTANCE.asdSearchButtonText()); searchButton.setMenu(getSearchMenu()); return searchButton; } private Menu getSearchMenu() { Menu menu = new Menu(); menu.add(new MenuItem(UIMessages.INSTANCE.exactEqual(), new SelectionHandler<MenuItem>() { @Override public void onSelection(SelectionEvent<MenuItem> event) { search(new EqualSearcher()); } })); menu.add(new MenuItem(UIMessages.INSTANCE.contains(), new SelectionHandler<MenuItem>() { @Override public void onSelection(SelectionEvent<MenuItem> event) { search(new ContainSearcher()); } })); menu.add(new MenuItem(UIMessages.INSTANCE.startsWith(), new SelectionHandler<MenuItem>() { @Override public void onSelection(SelectionEvent<MenuItem> event) { search(new StartSearcher()); } })); menu.add(new MenuItem(UIMessages.INSTANCE.endsWith(), new SelectionHandler<MenuItem>() { @Override public void onSelection(SelectionEvent<MenuItem> event) { search(new EndSearcher()); } })); menu.add(new MenuItem(UIMessages.INSTANCE.greater(), new SelectionHandler<MenuItem>() { @Override public void onSelection(SelectionEvent<MenuItem> event) { search(new GreaterThanSearcher()); } })); menu.add(new MenuItem(UIMessages.INSTANCE.smaller(), new SelectionHandler<MenuItem>() { @Override public void onSelection(SelectionEvent<MenuItem> event) { search(new SmallerThanSearcher()); } })); return menu; } private void initializeAttributeLabelCombo(String width) { ListStore<FeatureAttributeDef> attributeLabelStore = new ListStore<FeatureAttributeDef>( new ModelKeyProvider<FeatureAttributeDef>() { @Override public String getKey(FeatureAttributeDef item) { return (item == null) ? null : item.getName(); } }); attributeCombo = new ComboBox<FeatureAttributeDef>(attributeLabelStore, new LabelProvider<FeatureAttributeDef>() { @Override public String getLabel(FeatureAttributeDef item) { return (item == null) ? null : item.getName(); } }); attributeCombo.setWidth(width); attributeCombo.setTypeAhead(true); attributeCombo.setEmptyText(UIMessages.INSTANCE.asdAttributeComboEmptyText()); attributeCombo.setTriggerAction(TriggerAction.ALL); attributeCombo.setForceSelection(true); attributeCombo.setEditable(false); attributeCombo.enableEvents(); } private void search(Searcher searcher) { String attributeName = attributeCombo.getValue().getName(); String attributeValue = valueAttributeField.getValue(); List<VectorFeature> filteredFeatures = null; try { List<VectorFeature> features = Arrays.asList(selectedLayer.getFeatures()); filteredFeatures = searcher.search(features, attributeValue, attributeName, isCaseSensitive.getValue()); } catch (Exception e) { messageDialogBuilder.createError(UIMessages.INSTANCE.warning(), e.getMessage()); logger.error(e.getMessage()); } featureGrid.update(filteredFeatures); } public void setSelectedLayer(VectorLayer layer) { selectedLayer = layer; layerNameField.setText(selectedLayer.getName()); // Initilize the featureGrid for the new layer featureGrid.rebuild(getFeatures(layer)); featureGrid.clear(); updateLayerAttributes(); valueAttributeField.clear(); } private VectorFeature[] getFeatures(VectorLayer layer) { return (layer.getFeatures() == null) ? new ArrayList<VectorFeature>().toArray(new VectorFeature[0]) : layer.getFeatures(); } private void updateLayerAttributes() { try { attributeCombo.getStore().clear(); attributeCombo.setEmptyText(UIMessages.INSTANCE.asdAttributeComboEmptyText()); if (selectedLayer != null && selectedLayer.getAttributes() != null) { attributeCombo.getStore().addAll(selectedLayer.getAttributes()); } } catch (Exception e) { logger.error("Error reloading label atribute combo: " + e.getMessage(), e); } } private void setSelectedElements() { List<VectorFeature> selectedElements = featureGrid.getSelectionModel().getSelectedItems(); if (selectedElements == null || selectedElements.isEmpty()) { Info.display(UIMessages.INSTANCE.warning(), UIMessages.INSTANCE.selectAtLeast(1)); } else { for (FeatureTool tool : layerSearchToolBar.getTools()) { setSelectedElement(selectedElements, tool); } } } private void setSelectedElement(List<VectorFeature> selectedElements, FeatureTool tool) { tool.setSelectedLayer(selectedLayer); if (selectedElements.size() > 1) { tool.setSelectedFeatures(selectedElements); } else { tool.setSelectedFeature(selectedElements.get(0)); } } private void addKeyShortcuts() { KeyShortcutHandler keyShortcut = new KeyShortcutHandler(searchButton, KeyCodes.KEY_ENTER); layerNameField.addKeyDownHandler(keyShortcut); valueAttributeField.addKeyDownHandler(keyShortcut); attributeCombo.addKeyDownHandler(keyShortcut); } }