KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jimm > datavision > gui > applet > DesignApplet


1 package jimm.datavision.gui.applet;
2 import jimm.datavision.Report;
3 import jimm.datavision.ErrorHandler;
4 import jimm.datavision.gui.Designer;
5 import jimm.datavision.gui.AskStringDialog;
6 import jimm.util.I18N;
7 import jimm.util.XMLWriter;
8 import java.io.InputStreamReader JavaDoc;
9 import java.io.IOException JavaDoc;
10 import java.net.URL JavaDoc;
11 import java.net.URLConnection JavaDoc;
12 import org.xml.sax.InputSource JavaDoc;
13
14 /**
15  * A designer suitable for use with applets. This designer is created
16  * by a {@link DVApplet} in its <code>init</code> method.
17  *
18  * @author Jim Menard, <a HREF="mailto:jimm@io.com">jimm@io.com</a>
19  */

20 class DesignApplet extends Designer {
21
22 DesignApplet(DVApplet applet) {
23     super(null, null, applet, null);
24
25     reportFilePath = getAppletViaCheapTrick().getSaveURL();
26 }
27
28 /**
29  * Reads the named report file or, if it's <code>null</code>, creates
30  * a new, empty report. Returns <code>true</code> if we need to ask
31  * the user for connection info because this is a new report.
32  *
33  * @param fileName
34  * @param databasePassword string to give to report; OK if it's
35  * <code>null</code>
36  * @return <code>true</code> if we need to ask the user for connection info
37  */

38 protected boolean readReport(String JavaDoc fileName, String JavaDoc databasePassword) {
39     report = new Report();
40     String JavaDoc url = getAppletViaCheapTrick().getReportURL();
41     try {
42     if (url != null && url.length() > 0)
43         report.read(new InputSource JavaDoc(url));
44     }
45     catch (Exception JavaDoc e) {
46     ErrorHandler.error(e);
47     }
48
49     reportFilePath = null;
50     return false; // No password needed
51
}
52
53 /**
54  * A cheap trick: we need the applet but this method is called from the
55  * constructor indirectly so we can't assign the applet to an instance var
56  * of the correct type. However, we passed the applet in as our root pane
57  * container.
58  *
59  * @return the applet
60  */

61 protected DVApplet getAppletViaCheapTrick() {
62     return (DVApplet)rootPaneContainer;
63 }
64
65 /**
66  * Saves the current report to a URL specified by the user.
67  */

68 protected void saveReportAs() {
69     String JavaDoc name = new AskStringDialog(getFrame(),
70                       I18N.get("DesignApplet.new_url_title"),
71                       I18N.get("DesignApplet.new_url_prompt"),
72                       reportFilePath)
73     .getString();
74
75     if (name != null) {
76     reportFilePath = name;
77     writeReportFile(reportFilePath);
78     }
79 }
80
81 /**
82  * Writes the current report to the specified file. Also tells the
83  * command history the report has been saved so it knows how to report
84  * if any changes have been made from this point on.
85  *
86  * @param fileName a file name
87  */

88 protected void writeReportFile(String JavaDoc fileName) {
89     URLConnection JavaDoc conn = null;
90
91     try {
92     URL JavaDoc url = new URL JavaDoc(fileName);
93     conn = url.openConnection();
94     conn.setDoOutput(true);
95
96     sendData(conn);
97     // I don't know why, but we have to read the response from the
98
// server, even if it's empty.
99
receiveResponse(conn);
100     }
101     catch (Exception JavaDoc e) {
102     ErrorHandler.error(e);
103     }
104
105     // Don't forget this.
106
commandHistory.setBaseline();
107 }
108
109 protected void sendData(URLConnection JavaDoc conn) throws IOException JavaDoc {
110     XMLWriter out = null;
111     try {
112     out = new XMLWriter(conn.getOutputStream());
113     report.writeXML(out);
114     out.flush();
115     }
116     catch (IOException JavaDoc e) {
117     throw e;
118     }
119     finally {
120     if (out != null) out.close();
121     }
122 }
123
124 protected void receiveResponse(URLConnection JavaDoc conn) throws IOException JavaDoc {
125     InputStreamReader JavaDoc in = null;
126     try {
127     in = new InputStreamReader JavaDoc(conn.getInputStream());
128     char[] buf = new char[1024];
129     while (in.read(buf) > 0) ;
130     }
131     catch (IOException JavaDoc e) {
132     throw e;
133     }
134     finally {
135     if (in != null) in.close();
136     }
137 }
138
139 }
140
Popular Tags