001: package jimm.datavision.gui.applet;
002:
003: import jimm.datavision.Report;
004: import jimm.datavision.ErrorHandler;
005: import jimm.datavision.gui.Designer;
006: import jimm.datavision.gui.AskStringDialog;
007: import jimm.util.I18N;
008: import jimm.util.XMLWriter;
009: import java.io.InputStreamReader;
010: import java.io.IOException;
011: import java.net.URL;
012: import java.net.URLConnection;
013: import org.xml.sax.InputSource;
014:
015: /**
016: * A designer suitable for use with applets. This designer is created
017: * by a {@link DVApplet} in its <code>init</code> method.
018: *
019: * @author Jim Menard, <a href="mailto:jimm@io.com">jimm@io.com</a>
020: */
021: class DesignApplet extends Designer {
022:
023: DesignApplet(DVApplet applet) {
024: super (null, null, applet, null);
025:
026: reportFilePath = getAppletViaCheapTrick().getSaveURL();
027: }
028:
029: /**
030: * Reads the named report file or, if it's <code>null</code>, creates
031: * a new, empty report. Returns <code>true</code> if we need to ask
032: * the user for connection info because this is a new report.
033: *
034: * @param fileName
035: * @param databasePassword string to give to report; OK if it's
036: * <code>null</code>
037: * @return <code>true</code> if we need to ask the user for connection info
038: */
039: protected boolean readReport(String fileName,
040: String databasePassword) {
041: report = new Report();
042: String url = getAppletViaCheapTrick().getReportURL();
043: try {
044: if (url != null && url.length() > 0)
045: report.read(new InputSource(url));
046: } catch (Exception e) {
047: ErrorHandler.error(e);
048: }
049:
050: reportFilePath = null;
051: return false; // No password needed
052: }
053:
054: /**
055: * A cheap trick: we need the applet but this method is called from the
056: * constructor indirectly so we can't assign the applet to an instance var
057: * of the correct type. However, we passed the applet in as our root pane
058: * container.
059: *
060: * @return the applet
061: */
062: protected DVApplet getAppletViaCheapTrick() {
063: return (DVApplet) rootPaneContainer;
064: }
065:
066: /**
067: * Saves the current report to a URL specified by the user.
068: */
069: protected void saveReportAs() {
070: String name = new AskStringDialog(getFrame(), I18N
071: .get("DesignApplet.new_url_title"), I18N
072: .get("DesignApplet.new_url_prompt"), reportFilePath)
073: .getString();
074:
075: if (name != null) {
076: reportFilePath = name;
077: writeReportFile(reportFilePath);
078: }
079: }
080:
081: /**
082: * Writes the current report to the specified file. Also tells the
083: * command history the report has been saved so it knows how to report
084: * if any changes have been made from this point on.
085: *
086: * @param fileName a file name
087: */
088: protected void writeReportFile(String fileName) {
089: URLConnection conn = null;
090:
091: try {
092: URL url = new URL(fileName);
093: conn = url.openConnection();
094: conn.setDoOutput(true);
095:
096: sendData(conn);
097: // I don't know why, but we have to read the response from the
098: // server, even if it's empty.
099: receiveResponse(conn);
100: } catch (Exception e) {
101: ErrorHandler.error(e);
102: }
103:
104: // Don't forget this.
105: commandHistory.setBaseline();
106: }
107:
108: protected void sendData(URLConnection conn) throws IOException {
109: XMLWriter out = null;
110: try {
111: out = new XMLWriter(conn.getOutputStream());
112: report.writeXML(out);
113: out.flush();
114: } catch (IOException e) {
115: throw e;
116: } finally {
117: if (out != null)
118: out.close();
119: }
120: }
121:
122: protected void receiveResponse(URLConnection conn)
123: throws IOException {
124: InputStreamReader in = null;
125: try {
126: in = new InputStreamReader(conn.getInputStream());
127: char[] buf = new char[1024];
128: while (in.read(buf) > 0)
129: ;
130: } catch (IOException e) {
131: throw e;
132: } finally {
133: if (in != null)
134: in.close();
135: }
136: }
137:
138: }
|