// Helper method to create the internals of the HTML Form (FormPanel) // Submit form may be for new post, or for editing existing post private FormPanel makeSubmitPostForm(final PostData post) { final FormPanel submitFormPanel = new FormPanel(); VerticalPanel postFormPanel = new VerticalPanel(); postFormPanel.addStyleName("submitPostVertPanel"); submitFormPanel.add(postFormPanel); // Name input HorizontalPanel nameRow = new HorizontalPanel(); Label nameLabel = new Label("Name (First Last"); final TextBox nameTextbox = new TextBox(); nameRow.add(nameLabel); nameRow.add(nameTextbox); postFormPanel.add(nameRow); // Title input HorizontalPanel titleRow = new HorizontalPanel(); Label titleLabel = new Label("Title (e.g. car, bike, etc)"); final TextBox titleTextbox = new TextBox(); titleRow.add(titleLabel); titleRow.add(titleTextbox); postFormPanel.add(titleRow); // Description input HorizontalPanel descrRow = new HorizontalPanel(); Label descrLabel = new Label("Item Short description"); final TextArea descrText = new TextArea(); descrText.setCharacterWidth(40); descrText.setVisibleLines(10); descrRow.add(descrLabel); descrRow.add(descrText); postFormPanel.add(descrRow); // Price input HorizontalPanel priceRow = new HorizontalPanel(); Label priceLabel = new Label("Price ($)"); final TextBox priceTextbox = new TextBox(); priceTextbox.setVisibleLength(6); priceRow.add(priceLabel); priceRow.add(priceTextbox); postFormPanel.add(priceRow); // Address input HorizontalPanel addressRow = new HorizontalPanel(); Label addressLabel = new Label("Address"); final TextArea addressText = new TextArea(); addressText.setCharacterWidth(40); addressText.setVisibleLines(5); addressRow.add(addressLabel); addressRow.add(addressText); postFormPanel.add(addressRow); if (post != null) { nameTextbox.setText(post.getSellerName()); titleTextbox.setText(post.getTitle()); descrText.setText(post.getDescription()); priceTextbox.setText("" + post.getPrice()); addressText.setText(post.getAddress()); } // New widget for file upload HorizontalPanel fileRow = new HorizontalPanel(); final FileUpload upload = new FileUpload(); if (post != null) { Anchor link = new Anchor("Stored File", post.getURL()); link.setTarget("_blank"); fileRow.add(link); upload.setTitle("Change Uploaded File"); } else upload.setTitle("Upload a File for Post"); fileRow.add(upload); postFormPanel.add(fileRow); // Submit button Button submitButton; if (post != null) { submitButton = new Button("Submit Changes"); submitButton.setText("Submit Changes"); } else { submitButton = new Button("Submit Ad"); submitButton.setText("Submit Ad"); } submitButton.setStyleName("sideBarButton"); postFormPanel.add(submitButton); // The submitFormPanel, when submitted, will trigger an HTTP call to the // servlet. The following parameters must be set submitFormPanel.setEncoding(FormPanel.ENCODING_MULTIPART); submitFormPanel.setMethod(FormPanel.METHOD_POST); // Set Names for the text boxes so that they can be retrieved from the // HTTP call as parameters nameTextbox.setName("name"); titleTextbox.setName("title"); descrText.setName("description"); priceTextbox.setName("price"); addressText.setName("address"); upload.setName("upload"); // Upon clicking "Submit" control goes to Controller submitButton.addClickHandler( new ClickHandler() { public void onClick(ClickEvent event) { if (post == null) control.handlePostFromSubmitForm(submitFormPanel); else control.handlePostEditFromSubmitForm(post, submitFormPanel); } }); // This event handler is "fired" just before the Form causes a doPost // message to server submitFormPanel.addSubmitHandler( new FormPanel.SubmitHandler() { public void onSubmit(SubmitEvent event) { // This event is fired just before the form is submitted. // We can take this opportunity to perform validation. if (nameTextbox.getText().length() == 0) { Window.alert("The author is required."); event.cancel(); } if (titleTextbox.getText().length() == 0) { Window.alert("The title is required."); event.cancel(); } Double price = Double.parseDouble(priceTextbox.getText()); if (priceTextbox.getText().length() == 0 || Double.isNaN(price)) { Window.alert("The price is required. It must be a valid number"); event.cancel(); } if (addressText.getText().length() == 0) { Window.alert("The address is required."); event.cancel(); } if (upload.getFilename().length() == 0 && post == null) { Window.alert("The upload file is required."); event.cancel(); } } }); // The doPost message itself is sent from the Form and not intercepted // by GWT. // This event handler is "fired" just after the Form causes a doPost // message to server submitFormPanel.addSubmitCompleteHandler( new FormPanel.SubmitCompleteHandler() { public void onSubmitComplete(SubmitCompleteEvent event) { if (post == null) { submitFormPanel.reset(); nameTextbox.setFocus(true); } else control.viewAdDataFromServer(); } }); return submitFormPanel; }