📄 formview.java
字号:
break; } return super.getMaximumSpan(axis); } /** * Responsible for processeing the ActionEvent. * If the element associated with the FormView, * has a type of "submit", "reset", "text" or "password" * then the action is processed. In the case of a "submit" * the form is submitted. In the case of a "reset" * the form is reset to its original state. * In the case of "text" or "password", if the * element is the last one of type "text" or "password", * the form is submitted. Otherwise, focus is transferred * to the next component in the form. * * @param evt the ActionEvent. */ public void actionPerformed(ActionEvent evt) { Element element = getElement(); StringBuffer dataBuffer = new StringBuffer(); HTMLDocument doc = (HTMLDocument)getDocument(); AttributeSet attr = element.getAttributes(); String type = (String) attr.getAttribute(HTML.Attribute.TYPE); if (type.equals("submit")) { getFormData(dataBuffer); submitData(dataBuffer.toString()); } else if (type.equals("reset")) { resetForm(); } else if (type.equals("text") || type.equals("password")) { if (isLastTextOrPasswordField()) { getFormData(dataBuffer); submitData(dataBuffer.toString()); } else { getComponent().transferFocus(); } } } /** * This method is responsible for submitting the form data. * A thread is forked to undertake the submission. */ protected void submitData(String data) { //System.err.println("data ->"+data+"<-"); SubmitThread dataThread = new SubmitThread(getElement(), data); dataThread.start(); } /** * The SubmitThread is responsible for submitting the form. * It performs a POST or GET based on the value of method * attribute associated with HTML.Tag.FORM. In addition to * submitting, it is also responsible for display the * results of the form submission. */ class SubmitThread extends Thread { String data; HTMLDocument hdoc; HTMLDocument newDoc; AttributeSet formAttr; InputStream in; public SubmitThread(Element elem, String data) { this.data = data; hdoc = (HTMLDocument)elem.getDocument(); Element formE = getFormElement(); if (formE != null) { formAttr = formE.getAttributes(); } } /** * This method is responsible for extracting the * method and action attributes associated with the * <FORM> and using those to determine how (POST or GET) * and where (URL) to submit the form. If action is * not specified, the base url of the existing document is * used. Also, if method is not specified, the default is * GET. Once form submission is done, run uses the * SwingUtilities.invokeLater() method, to load the results * of the form submission into the current JEditorPane. */ public void run() { if (data.length() > 0) { String method = getMethod(); String action = getAction(); URL url; try { URL actionURL; /* if action is null use the base url and ensure that the file name excludes any parameters that may be attached */ URL baseURL = hdoc.getBase(); if (action == null) { String file = baseURL.getFile(); actionURL = new URL(baseURL.getProtocol(), baseURL.getHost(), baseURL.getPort(), file); } else { actionURL = new URL(baseURL, action); } URLConnection connection; if ("post".equals(method)) { url = actionURL; connection = url.openConnection(); postData(connection, data); } else { /* the default, GET */ url = new URL(actionURL+"?"+data); connection = url.openConnection(); } in = connection.getInputStream(); // safe assumption since we are in an html document JEditorPane c = (JEditorPane)getContainer(); HTMLEditorKit kit = (HTMLEditorKit)c.getEditorKit(); newDoc = (HTMLDocument)kit.createDefaultDocument(); newDoc.putProperty(Document.StreamDescriptionProperty, url); Runnable callLoadDocument = new Runnable() { public void run() { loadDocument(); } }; SwingUtilities.invokeLater(callLoadDocument); } catch (MalformedURLException m) { // REMIND how do we deal with exceptions ?? } catch (IOException e) { // REMIND how do we deal with exceptions ?? } } } /** * This method is responsible for loading the * document into the FormView's container, * which is a JEditorPane. */ public void loadDocument() { JEditorPane c = (JEditorPane)getContainer(); try { c.read(in, newDoc); } catch (IOException e) { // REMIND failed to load document } } /** * Get the value of the action attribute. */ public String getAction() { if (formAttr == null) { return null; } return (String)formAttr.getAttribute(HTML.Attribute.ACTION); } /** * Get the form's method parameter. */ String getMethod() { if (formAttr != null) { String method = (String)formAttr.getAttribute(HTML.Attribute.METHOD); if (method != null) { return method.toLowerCase(); } } return null; } /** * This method is responsible for writing out the form submission * data when the method is POST. * * @param connection to use. * @param data to write. */ public void postData(URLConnection connection, String data) { connection.setDoOutput(true); PrintWriter out = null; try { out = new PrintWriter(new OutputStreamWriter(connection.getOutputStream())); out.print(data); out.flush(); } catch (IOException e) { // REMIND: should do something reasonable! } finally { if (out != null) { out.close(); } } } } /** * MouseEventListener class to handle form submissions when * an input with type equal to image is clicked on. * A MouseListener is necessary since along with the image * data the coordinates associated with the mouse click * need to be submitted. */ protected class MouseEventListener extends MouseAdapter { public void mouseReleased(MouseEvent evt) { String imageData = getImageData(evt.getPoint()); imageSubmit(imageData); } } /** * This method is called to submit a form in response * to a click on an image -- an <INPUT> form * element of type "image". * * @param imageData the mouse click coordinates. */ protected void imageSubmit(String imageData) { StringBuffer dataBuffer = new StringBuffer(); Element elem = getElement(); HTMLDocument hdoc = (HTMLDocument)elem.getDocument(); getFormData(dataBuffer); if (dataBuffer.length() > 0) { dataBuffer.append('&'); } dataBuffer.append(imageData); submitData(dataBuffer.toString()); return; } /** * Extracts the value of the name attribute * associated with the input element of type * image. If name is defined it is encoded using * the URLEncoder.encode() method and the * image data is returned in the following format: * name + ".x" +"="+ x +"&"+ name +".y"+"="+ y * otherwise, * "x="+ x +"&y="+ y * * @param point associated with the mouse click. * @return the image data. */ private String getImageData(Point point) { String mouseCoords = point.x + ":" + point.y; int sep = mouseCoords.indexOf(':'); String x = mouseCoords.substring(0, sep); String y = mouseCoords.substring(++sep); String name = (String) getElement().getAttributes().getAttribute(HTML.Attribute.NAME); String data; if (name == null || name.equals("")) { data = "x="+ x +"&y="+ y; } else { name = URLEncoder.encode(name); data = name + ".x" +"="+ x +"&"+ name +".y"+"="+ y; } return data; } /** * The following methods provide functionality required to * iterate over a the elements of the form and in the case * of a form submission, extract the data from each model * that is associated with each form element, and in the * case of reset, reinitialize the each model to its * initial state. */ /** * Returns the Element representing the <code>FORM</code>. */ private Element getFormElement() { Element elem = getElement(); while (elem != null) { if (elem.getAttributes().getAttribute (StyleConstants.NameAttribute) == HTML.Tag.FORM) { return elem; } elem = elem.getParentElement(); } return null; } /** * Iterates over the * element hierarchy, extracting data from the * models associated with the relevant form elements. * "Relevant" means the form elements that are part * of the same form whose element triggered the submit * action. * * @param buffer the buffer that contains that data to submit * @param targetElement the element that triggered the * form submission */ void getFormData(StringBuffer buffer) { Element formE = getFormElement(); if (formE != null) { ElementIterator it = new ElementIterator(formE); Element next; while ((next = it.next()) != null) { if (isControl(next)) { String type = (String)next.getAttributes().getAttribute (HTML.Attribute.TYPE); if (type != null && type.equals("submit") &&
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -