package com.zaranux.sample.hello.client;
import com.zaranux.client.api.AsyncCallback;
import com.zaranux.client.app.Program;
import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextArea;
import com.zaranux.client.java.io.File;
import com.zaranux.client.java.io.FileInputStream;
import com.zaranux.client.java.io.FileOutputStream;
import com.zaranux.client.java.io.InputStreamReader;
import com.zaranux.client.java.io.OutputStreamWriter;
public class Hello extends Program {
public void main(final String[] args)
{
Label l = new Label();
String argList = "Number of Arguments: " + args.length + " Arguments : " ;
for(int i = 0; i < args.length; i++)
{
argList += args[i] + "\n";
}
l.setText(argList);
final TextBox path = new TextBox();
final TextArea textArea = new TextArea();
Button readButton = new Button("Read", new ClickHandler() {
public void onClick(ClickEvent event) {
File file = new File(path.getText());
FileInputStream fis = new FileInputStream(file);
InputStreamReader isr = new InputStreamReader(fis);
char[] cbuf = new char[100 * 1024];
textArea.setText("");
readAll(textArea, cbuf, isr);
}
});
Button writeButton = new Button("Write", new ClickHandler() {
public void onClick(ClickEvent event) {
final FileOutputStream fos = new FileOutputStream(path.getText());
OutputStreamWriter osw = new OutputStreamWriter(fos);
osw.write(textArea.getText(), new AsyncCallback<Boolean>()
{
public void onSuccess(Boolean b)
{
fos.close(new AsyncCallback<Boolean>()
{
public void onSuccess(Boolean b)
{
if(b)
{
//TextEditor.this.setStatus(filePath + " saved.");
Log.debug(path.getText() + " saved.");
}else
{
//TextEditor.this.setStatus("Unable to save "+ filePath + ".");
Log.debug("Unable to save "+ path.getText() + " .");
}
}
public void onFailure(Throwable t)
{
//TextEditor.this.setStatus("Failed to save "+ filePath + ".");
Log.error("Failed to save "+ path.getText() + "." + " (" + t + ")");
}
});
}
public void onFailure(Throwable t)
{
//TextEditor.this.setStatus("Failed to save "+ filePath + ".");
Log.error("Failed to save "+ path.getText() + " ." + " (" + t + ")");
}
});
}
});
RootPanel.get().add(l);
RootPanel.get().add(path);
RootPanel.get().add(textArea);
RootPanel.get().add(readButton);
RootPanel.get().add(writeButton);
}
private void readAll(final TextArea textArea, final char[] cbuf, final InputStreamReader isr)
{
isr.read(cbuf, new AsyncCallback<Integer>()
{
public void onSuccess(Integer n)
{
if(n>=0)
{
String data = new String(cbuf,0,n);
textArea.setText(textArea.getText() + data);
readAll(textArea,cbuf,isr);
}else // end of file
{
//TextEditor.this.setStatus(filePath + " loaded.");
}
}
public void onFailure(Throwable t)
{
//TextEditor.this.setStatus("Failed to retrive "+ filePath + ".");
Log.error("Failed to retrieve the file ." + " (" + t + ")");
}
});
}
}