xitiomet / placebohttp (http://openstatic.org/)

Embeded Http server for gcj/java applications. PlaceboHttp uses an automatic session management system, Treating each session as a HttpRequest queue.

Clone this repository (size: 135.2 KB): HTTPS / SSH
$ hg clone http://hg.openstatic.org/placebohttp
commit 20: fc9e1983c2a0
parent 19: 81f8f47a533a
branch: default
More gui tweaking
xitiomet
9 months ago
r20:fc9e1983c2a0 141 loc 4.3 KB embed / history / annotate / raw /
package org.openstatic.placebo;

import org.openstatic.placebo.CoreServer;
import org.openstatic.util.JTextAreaOutputStream;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JTabbedPane;
import java.awt.Font;
import java.awt.Color;

public class ControlPanel extends JFrame implements ActionListener
{
    private JButton start_btn;
    private JButton stop_btn;
    private JTextField port_field;
    private JTextField root_field;
    private JCheckBox debug_field;
    private JTextAreaOutputStream log_output;
    private JTextArea log_box;
    private CoreServer core;
    
    public void centerWindow()
    {
        Toolkit tk = Toolkit.getDefaultToolkit();
        Dimension screenSize = tk.getScreenSize();
        final int WIDTH = screenSize.width;
        final int HEIGHT = screenSize.height;
        this.setSize(420, 240);
        this.setLocation(WIDTH / 2 - 210, HEIGHT / 2 - 120);
    }
    
    public void setPort(int value)
    {
        this.port_field.setText(String.valueOf(value));
    }
    
    public void setDebug(boolean value)
    {
        this.debug_field.setSelected(value);
    }
    
    public void setWebRoot(String value)
    {
        this.root_field.setText(value);
    }
    
    public ControlPanel()
    {
        super("Placebo Web Server");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JTabbedPane tabbed = new JTabbedPane();
        
        JPanel main_pane = new JPanel();
        // Left Side thingy
        JPanel pane = new JPanel(new GridLayout(0,2,6,6));
        pane.setSize(50,100);
        JLabel port_label = new JLabel("Port:", JLabel.TRAILING);
        port_field = new JTextField(15);

        JLabel root_label = new JLabel("Web Root:", JLabel.TRAILING);
        root_field = new JTextField(15);
        
        JLabel debug_label = new JLabel("Debug:", JLabel.TRAILING);
        debug_field = new JCheckBox();

        start_btn = new JButton("Start Server");
        start_btn.setActionCommand("start");
        start_btn.addActionListener(this);
        
        stop_btn = new JButton("Stop Server");
        stop_btn.setActionCommand("stop");
        stop_btn.addActionListener(this);
        stop_btn.setEnabled(false);
        
        pane.add(port_label);
        pane.add(port_field);
        pane.add(root_label);
        pane.add(root_field);
        pane.add(debug_label);
        pane.add(debug_field);
        main_pane.add(pane);
        
        JPanel button_pane = new JPanel(new GridLayout(0,2,6,6));
        button_pane.add(start_btn);
        button_pane.add(stop_btn);
        main_pane.add(button_pane);

        tabbed.addTab("Setup Placebo", null, main_pane, "");
        
        // Right Side

        log_box = new JTextArea();
        Font font = new Font("Monospaced", Font.BOLD, 12);
        log_box.setFont(font);
        log_box.setForeground(Color.WHITE);
        log_box.setBackground(Color.BLACK);
        log_output = new JTextAreaOutputStream(log_box);
        JScrollPane scroller = new JScrollPane(log_box);
        scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        tabbed.addTab("Server Log", null, scroller, "");

        // finish window
        this.add(tabbed);
        centerWindow();
        this.setVisible(true);
    }
    
    public void actionPerformed(ActionEvent e)
    {
        if ("start".equals(e.getActionCommand()))
        {
            start_btn.setEnabled(false);
            stop_btn.setEnabled(true);
            core = new CoreServer();
            core.setPort(Integer.valueOf(port_field.getText()).intValue());
            core.setWebRoot(root_field.getText());
            core.setDebug(debug_field.isSelected());
            core.setDebugStream(log_output);
            core.start();
        }
        if ("stop".equals(e.getActionCommand()))
        {
            stop_btn.setEnabled(false);
            core.quit();
            start_btn.setEnabled(true);
        }
    }
}