package Http;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import swing2swt.layout.BorderLayout;
public class HttpServer {
protected Shell shlHttpServer;
private static Text text;
private Text text_1;
private Text text_2;
private String selectedDir;
private int port;
private Thread thread;
private boolean runable= false;
/**
* Launch the application.
*
* @param args
*/
public static void main(String[] args) {
try {
HttpServer window = new HttpServer();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Open the window.
*/
public void open() {
Display display = Display.getDefault();
createContents();
shlHttpServer.open();
shlHttpServer.layout();
while (!shlHttpServer.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
public static void setText(String words){
text.append(words+"\n");
}
/**
* Create contents of the window.
*/
protected void createContents() {
shlHttpServer = new Shell();
shlHttpServer.setMinimumSize(new Point(120, 40));
shlHttpServer.setLayout(new BorderLayout(0, 0));
shlHttpServer.setSize(450, 300);
shlHttpServer.setText("Http Server");
{
Group group = new Group(shlHttpServer, SWT.NONE);
group.setLayout(new GridLayout(6, false));
group.setLayoutData(BorderLayout.NORTH);
{
Label label = new Label(group, SWT.NONE);
label.setText("端口");
}
{
text_2 = new Text(group, SWT.BORDER);
text_2.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false,
false, 1, 1));
}
{
Label label = new Label(group, SWT.NONE);
label.setText("根目录");
}
{
text_1 = new Text(group, SWT.BORDER);
text_1.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true,
false, 1, 1));
}
{
final Button button1 = new Button(group, SWT.CENTER);
button1.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
DirectoryDialog directoryDialog = new DirectoryDialog(
shlHttpServer);
directoryDialog.setFilterPath(selectedDir);
directoryDialog
.setMessage("选择网站根目录");
String dir = directoryDialog.open();
if (dir != null) {
text_1.setText(dir);
}
}
});
button1.setText("...");
}
{
final Button button2 = new Button(group, SWT.CENTER);
button2.setText("启动");
button2.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent e) {
try {
port = Integer.parseInt(text_2.getText());
selectedDir = text_1.getText();
} catch (Exception e1) {
port = 80;
}
if (runable) {
thread.interrupt();
button2.setText("启动");
runable = false;
} else {
try{
thread = new Thread(new Connect(port,selectedDir));
thread.start();
//shlHttpServer.syncExec(thread);
}catch (Exception e1) {
System.out.println(e1);
}
//httpRequestHandler.connect(port,selectedDir);
button2.setText("停止");
runable = true;
}
}
});
}
}
{
text = new Text(shlHttpServer, SWT.BORDER | SWT.V_SCROLL | SWT.MULTI);
text.setEditable(false);
text.setLayoutData(BorderLayout.CENTER);
}
}
}