/* * Copyright 2007 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); you may not * use this file except in compliance with the License. You may obtain a copy of * the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations under * the License. */ package com.mlhc.imaging.client; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; import com.google.gwt.core.client.GWT; import com.google.gwt.user.client.Window; import com.google.gwt.user.client.rpc.AsyncCallback; import com.google.gwt.user.client.rpc.ServiceDefTarget; import com.google.gwt.user.client.ui.Button; import com.google.gwt.user.client.ui.ChangeListener; import com.google.gwt.user.client.ui.ClickListener; import com.google.gwt.user.client.ui.FormHandler; import com.google.gwt.user.client.ui.FormPanel; import com.google.gwt.user.client.ui.FormSubmitCompleteEvent; import com.google.gwt.user.client.ui.FormSubmitEvent; import com.google.gwt.user.client.ui.HorizontalPanel; import com.google.gwt.user.client.ui.Label; import com.google.gwt.user.client.ui.ListBox; import com.google.gwt.user.client.ui.RootPanel; import com.google.gwt.user.client.ui.TextBox; import com.google.gwt.user.client.ui.VerticalPanel; import com.google.gwt.user.client.ui.Widget; import com.google.gwt.widgetideas.table.client.FixedWidthFlexTable; import com.google.gwt.widgetideas.table.client.ListTableModel; import com.google.gwt.widgetideas.table.client.PagingGrid; import com.google.gwt.widgetideas.table.client.PagingScrollTable; import com.mlhc.imaging.client.component.ExpertStoredImage; import com.mlhc.imaging.client.component.SelectOption; /** * Demonstrates {@link com.google.gwt.user.client.ui.ListBox}. */ public class DisplayAndRoutingPanel extends Sink implements ChangeListener { public static SinkInfo init(final Sink.Images images) { return new SinkInfo("DisplayAndRouting", "") { public Sink createInstance() { return new DisplayAndRoutingPanel(images); } }; } private Map catsAndTypes; private List searchResults; private TextBox orderNumber; private ListBox categories = new ListBox(); private ListBox types = new ListBox(); private ListBox sortBy = new ListBox(); private Button searchButton = new Button(); private SearchFormServiceAsync searchFormService; private VerticalPanel mainPanel = new VerticalPanel(); private VerticalPanel formPanel = new VerticalPanel(); /** * Display & Routing panel contructor * * @param images */ public DisplayAndRoutingPanel(Sink.Images images) { // setup order number orderNumber = new TextBox(); orderNumber.setTitle("orderNumber"); orderNumber.setFocus(true); // setup the categories categories.setVisibleItemCount(1); categories.setMultipleSelect(true); categories.setVisibleItemCount(5); categories.setWidth("300"); categories.addChangeListener(this); categories.setTitle("category"); categories.addItem("Please wait..."); // setup types types.setVisibleItemCount(5); types.setMultipleSelect(true); types.setWidth("300"); types.setTitle("type"); types.addItem("Please wait..."); // setup sort by sortBy.addItem("Date", "D"); sortBy.addItem("Category", "C"); sortBy.addItem("Type", "T"); sortBy.setTitle("sort"); // setup search button searchButton.setText("Search"); // // set up category and type service // CategoryAndTypeServiceAsync catAndTypeService = (CategoryAndTypeServiceAsync) GWT .create(CategoryAndTypeService.class); ServiceDefTarget endpoint = (ServiceDefTarget) catAndTypeService; String categoryAndTypeURL = GWT.getModuleBaseURL() + "categoryAndTypeService"; endpoint.setServiceEntryPoint(categoryAndTypeURL); catAndTypeService.getMap("rellis", "PTC", new AsyncCallback() { public void onFailure(Throwable caught) { } public void onSuccess(Object result) { catsAndTypes = (Map) result; List cats = Arrays.asList(catsAndTypes.keySet().toArray()); Collections.sort(cats); categories.clear(); for (Iterator i = cats.iterator(); i.hasNext();) { SelectOption so = (SelectOption) i.next(); categories.addItem(so.getValue(), so.getKey()); } categories.setSelectedIndex(0); fillTypes(0); } }); types.addChangeListener(this); // // search form service // searchFormService = (SearchFormServiceAsync) GWT .create(SearchFormService.class); ServiceDefTarget catAndTypeEndpoint = (ServiceDefTarget) searchFormService; String searchFormServiceModuleRelativeURL = GWT.getModuleBaseURL() + "searchFormService"; catAndTypeEndpoint .setServiceEntryPoint(searchFormServiceModuleRelativeURL); // Create a FormPanel and point it at a service. final FormPanel form = new FormPanel(); form.setAction(searchFormServiceModuleRelativeURL); searchButton.addClickListener(new ClickListener() { public void onClick(Widget sender) { form.submit(); } }); // Add an event handler to the form. form.addFormHandler(new FormHandler() { public void onSubmit(FormSubmitEvent event) { String sendCats = ""; for (int i = 0; i < categories.getItemCount(); i++) { if (categories.isItemSelected(i)) { if (sendCats.equals("")) { sendCats = categories.getValue(i); } else { sendCats = sendCats + "," + categories.getValue(i); } } } String sendTypes = ""; for (int i = 0; i < types.getItemCount(); i++) { if (types.isItemSelected(i)) { if (!types.getItemText(i).equals("All")) { if (sendTypes.equals("")) { sendTypes = types.getValue(i); } else { sendTypes = sendTypes + "," + types.getValue(i); } } } } // This event is fired just before the form is submitted. We can // take // this opportunity to perform validation. if (orderNumber.getText().length() == 0) { Window.alert("The order number must not be empty"); event.setCancelled(true); } else { searchButton.setEnabled(false); searchButton.setText("Please wait..."); searchFormService.getSearchResults("rellis", "PTC", orderNumber.getText(), sendCats, sendTypes, sortBy .getValue(sortBy.getSelectedIndex()), new AsyncCallback() { public void onFailure(Throwable caught) { searchButton.setEnabled(true); searchButton.setText("Operation failed"); } public void onSuccess(Object result) { searchResults = (List) result; showResults(); } }); } } public void onSubmitComplete(FormSubmitCompleteEvent event) { // When the form submission is successfully completed, this // event is // fired. Assuming the service returned a response of type // text/html, // we can get the result text here (see the FormPanel // documentation for // further explanation). // Window.alert("submit complete!"); searchButton.setEnabled(true); searchButton.setText("Search"); GWT.log("submit complete!", null); } }); // mainPanel.setWidth("600"); mainPanel.setHorizontalAlignment(VerticalPanel.ALIGN_CENTER); formPanel.setSpacing(1); // formPanel.setHorizontalAlignment(VerticalPanel.ALIGN_LEFT); HorizontalPanel orderNumberPanel = new HorizontalPanel(); orderNumberPanel.setSpacing(8); Label orderNumberLabel = new Label("Order Number"); orderNumberLabel.setWidth("150"); orderNumberLabel.setHorizontalAlignment(orderNumberLabel.ALIGN_RIGHT); orderNumberPanel.add(orderNumberLabel); orderNumberPanel.add(orderNumber); HorizontalPanel categoryPanel = new HorizontalPanel(); categoryPanel.setSpacing(8); Label categoryLabel = new Label("Document Category"); categoryLabel.setWidth("150"); categoryLabel.setHorizontalAlignment(Label.ALIGN_RIGHT); categoryPanel.add(categoryLabel); categoryPanel.add(categories); HorizontalPanel typePanel = new HorizontalPanel(); typePanel.setVerticalAlignment(HorizontalPanel.ALIGN_MIDDLE); typePanel.setSpacing(8); Label typeLabel = new Label("Document Type"); typeLabel.setWidth("150"); typeLabel.setHorizontalAlignment(Label.ALIGN_RIGHT); typePanel.add(typeLabel); typePanel.add(types); HorizontalPanel sortByPanel = new HorizontalPanel(); sortByPanel.setSpacing(8); Label sortByLabel = new Label("Sort By"); sortByLabel.setWidth("150"); sortByLabel.setHorizontalAlignment(Label.ALIGN_RIGHT); sortByPanel.add(sortByLabel); sortByPanel.add(sortBy); HorizontalPanel bottomButtonPanel = new HorizontalPanel(); bottomButtonPanel.setSpacing(8); bottomButtonPanel.setWidth("100%"); bottomButtonPanel.setHorizontalAlignment(HorizontalPanel.ALIGN_CENTER); bottomButtonPanel.add(searchButton); // searchResultsPanel.setVisible(false); mainPanel.add(formPanel); formPanel.add(orderNumberPanel); formPanel.add(categoryPanel); formPanel.add(typePanel); formPanel.add(sortByPanel); formPanel.add(bottomButtonPanel); // formPanel.setSpacing(8); // formPanel.setWidth("400"); // formPanel.setVisible(true); // // // formPanel.add(orderNumber); // formPanel.add(categories); // formPanel.add(types); // formPanel.add(sortBy); // formPanel.add(searchButton); // form.add(panel); // mainPanel.add(labelPanel); // mainPanel2.add(formPanel); // form.setWidget(formPanel); RootPanel.get().add(mainPanel); // orderNumber.setFocus(true); initWidget(mainPanel); } public void showResults() { // formPanel.setVisible(false); mainPanel.remove(formPanel); VerticalPanel results = new VerticalPanel(); results.setWidth("100%"); results.setHorizontalAlignment(VerticalPanel.ALIGN_CENTER); // List zeList = new ArrayList(); // for (Iterator i = searchResults.iterator(); i.hasNext();) { // ExpertStoredImage image = (ExpertStoredImage) i.next(); // zeList.add(Arrays.asList(new String[] { image.getSelected().toString(), // image.getType(), // "test", // image.getDateTime(), // "blah", // image.getWebView().toString() })); //// GWT.log("adding entry to zeList: " + image.getId(), null); // } PagingGrid grid = new PagingGrid(new ESITableModel(searchResults)); // grid.resizeColumns(6); grid.setNumRows(searchResults.size()); FixedWidthFlexTable headerTable = new FixedWidthFlexTable(); PagingScrollTable table = new PagingScrollTable(grid, headerTable); // Set column widths // Setup the formatting table.setCellPadding(3); table.setCellSpacing(1); table.setResizePolicy(table.RESIZE_POLICY_FILL_WIDTH); table.setFooterTable(null); table.setScrollingEnabled(false); table.setSize("650", "350"); table.setPageSize(2); // headers headerTable.setHTML(0, 0, "Selected"); headerTable.setHTML(0, 1, "Image Description"); headerTable.setHTML(0, 2, "Remarks"); headerTable.setHTML(0, 3, "Date/Time"); headerTable.setHTML(0, 4, "View"); headerTable.setHTML(0, 5, "on Web"); // Set column widths table.setColumnWidth(0, 50); table.setColumnWidth(1, 140); table.setColumnWidth(2, 100); table.setColumnWidth(3, 90); table.setColumnWidth(4, 50); table.setColumnWidth(5, 50); results.add(table); mainPanel.add(results); } public void onChange(Widget sender) { if (sender == categories) { fillTypes(categories.getSelectedIndex()); } } public void onShow() { orderNumber.setFocus(true); } /** * Populate types using the map of associated categories/types * * @param idx */ private void fillTypes(int idx) { // Set the contents of the list box to reflect the category selection. types.clear(); boolean isAllSelected = false; for (int i = 0; i < categories.getItemCount(); i++) { if (categories.isItemSelected(i)) { Set keys = catsAndTypes.keySet(); List valuesToAdd = new ArrayList(); for (Iterator m = keys.iterator(); m.hasNext() && !isAllSelected;) { SelectOption currentCategory = (SelectOption) m.next(); if (currentCategory.getKey().equals(categories.getValue(i)) && catsAndTypes.get(currentCategory) != null) { valuesToAdd.addAll((List) catsAndTypes.get(currentCategory)); } if (categories.getItemText(i).equals("All")) { isAllSelected = true; } } types.addItem("All"); if (isAllSelected) { List allTypes = Arrays.asList(catsAndTypes.values() .toArray()); for (Iterator h = allTypes.iterator(); h.hasNext();) { List currentTypes = (List) h.next(); List typesToAdd; if (currentTypes != null) { typesToAdd = new ArrayList(); for (Iterator e = currentTypes.iterator(); e .hasNext();) { SelectOption so = (SelectOption) e.next(); typesToAdd.add(so); } Collections.sort(typesToAdd); for (Iterator g = typesToAdd.iterator(); g .hasNext();) { SelectOption so = (SelectOption) g.next(); types.addItem(so.getValue(), so.getKey()); } } } } else { Collections.sort(valuesToAdd); for (Iterator p = valuesToAdd.iterator(); p.hasNext();) { SelectOption so = (SelectOption) p.next(); types.addItem(so.getValue(), so.getKey()); } } types.setSelectedIndex(0); } } } }