xitiomet is sharing code with you
Bitbucket is a code hosting site. Unlimited public and private repositories. Free for small teams.
Don't show this againplacebohttp / src / org / openstatic / placebo / ControlPanel.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 | /*
Copyright (C) 2010 Brian Dunigan
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.openstatic.placebo;
import org.openstatic.placebo.CoreServer;
import org.openstatic.util.JTextAreaOutputStream;
import org.openstatic.placebo.PlaceboPlugin;
import org.openstatic.http.PlaceboSession;
import org.openstatic.http.HttpRequest;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.DefaultListModel;
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 javax.imageio.ImageIO;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import javax.swing.JTabbedPane;
import java.awt.Font;
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.util.Hashtable;
import java.util.Enumeration;
import java.util.Properties;
import java.io.PrintStream;
public class ControlPanel extends JFrame implements ActionListener
{
private JTabbedPane tabbed;
private JButton start_btn;
private JButton stop_btn;
private JButton add_plugin;
private JTextField port_field;
private JTextField root_field;
private JTextField url_path;
private JTextField url_class_name;
private JCheckBox debug_field;
private JCheckBox show_data_field;
private DefaultListModel apps;
private DefaultListModel sessions;
private JTextAreaOutputStream log_output;
private JTextArea log_box;
private CoreServer core;
private Properties global_settings;
private Hashtable<String, String> init_plugins;
private BufferedImage placebo_icon;
public void centerWindow()
{
Toolkit tk = Toolkit.getDefaultToolkit();
Dimension screenSize = tk.getScreenSize();
final int WIDTH = screenSize.width;
final int HEIGHT = screenSize.height;
this.setSize(520, 340);
this.setLocation(WIDTH / 2 - 260, HEIGHT / 2 - 170);
}
public boolean isCentered()
{
Toolkit tk = Toolkit.getDefaultToolkit();
Dimension screenSize = tk.getScreenSize();
final int WIDTH = screenSize.width;
final int HEIGHT = screenSize.height;
if (this.getX() == ((WIDTH / 2) - (this.getWidth() / 2)) && this.getY() == ((HEIGHT / 2) - (this.getHeight() / 2)))
{
return true;
} else {
return false;
}
}
public void logSizeWindow()
{
this.setVisible(false);
Toolkit tk = Toolkit.getDefaultToolkit();
Dimension screenSize = tk.getScreenSize();
final int WIDTH = screenSize.width;
final int HEIGHT = screenSize.height;
this.setSize(820, 340);
this.setLocation(WIDTH / 2 - 410, HEIGHT / 2 - 170);
this.setVisible(true);
}
public void setPort(int value)
{
this.port_field.setText(String.valueOf(value));
}
public void setDebug(boolean value)
{
this.debug_field.setSelected(value);
}
public void setShowData(boolean value)
{
this.show_data_field.setSelected(value);
}
public void setWebRoot(String value)
{
this.root_field.setText(value);
}
public void addPanel(String panel_caption, Component panel)
{
final JPanel plugin_pane = new JPanel(new BorderLayout());
plugin_pane.add(panel, BorderLayout.CENTER);
plugin_pane.setForeground(Color.WHITE);
plugin_pane.setBackground(Color.GRAY);
JButton close_button = new JButton("Close Tab");
close_button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e)
{
tabbed.remove(plugin_pane);
}
});
JPanel sub_layout = new JPanel(new BorderLayout())
{
public void paintComponent(Graphics g)
{
g.drawImage(placebo_icon, 2, 2, 24, 24, null);
}
};
sub_layout.add(close_button, BorderLayout.EAST);
plugin_pane.add(sub_layout, BorderLayout.PAGE_END);
tabbed.addTab(panel_caption, null, plugin_pane, "");
}
public void populatePluginList()
{
apps.clear();
Hashtable<String, PlaceboPlugin> plugins = this.core.getPlugins();
for (Enumeration<String> e = plugins.keys(); e.hasMoreElements(); )
{
String this_plugin = e.nextElement();
apps.addElement(this_plugin + " @" + plugins.get(this_plugin).getClass().getName());
}
}
public void populateSessions()
{
sessions.clear();
Hashtable<String, PlaceboSession> session_list = this.core.getSessions();
for (Enumeration<String> e = session_list.keys(); e.hasMoreElements(); )
{
String this_session = e.nextElement();
PlaceboSession session = session_list.get(this_session);
sessions.addElement(this_session + " @" + session.getClientHostname() + " (" + session.peekLastRequest().getPath() + ")");
}
}
public void setPlugins(Hashtable<String, String> plugin_list)
{
this.init_plugins = plugin_list;
}
public void setGlobalSettings(Properties prop)
{
this.global_settings = prop;
}
public ControlPanel()
{
super("Placebo Web Server");
try
{
this.placebo_icon = ImageIO.read(getClass().getResourceAsStream("/www/placebo.png"));
} catch (Exception image_e) {
System.err.println(image_e.getMessage());
image_e.printStackTrace(System.err);
}
this.addWindowListener(new java.awt.event.WindowAdapter()
{
public void windowClosing(WindowEvent winEvt)
{
if (ControlPanel.this.core != null)
{
ControlPanel.this.core.quit();
}
System.exit(0);
}
});
tabbed = new JTabbedPane();
JPanel main_pane = new JPanel();
main_pane.setLayout(new BorderLayout());
// Setup tab
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();
JLabel show_data_label = new JLabel("Show Data:", JLabel.TRAILING);
show_data_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);
pane.add(show_data_label);
pane.add(show_data_field);
JPanel page_start = new JPanel(new BorderLayout());
JLabel page_header = new JLabel(" PlaceboHTTP") {
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawImage(placebo_icon, 0, 0, null);
}
};
page_header.setFont(new Font("Monospaced", Font.BOLD, 48));
page_start.add(page_header, BorderLayout.PAGE_START);
page_start.add(pane, BorderLayout.PAGE_END);
main_pane.add(page_start, BorderLayout.PAGE_START);
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,BorderLayout.PAGE_END);
tabbed.addTab("Placebo Setup", null, main_pane, "");
// Log Tab
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, "");
// Applications Tab
JPanel plugin_pane = new JPanel();
plugin_pane.setLayout(new BorderLayout());
// Add Item area
JPanel plugin_pane_items = new JPanel(new GridLayout(0,2,6,6));
JLabel url_path_label = new JLabel("URL Path:", JLabel.TRAILING);
url_path = new JTextField(15);
JLabel url_class_name_label = new JLabel("Class Name:", JLabel.TRAILING);
url_class_name = new JTextField(15);
plugin_pane_items.add(url_path_label);
plugin_pane_items.add(url_path);
plugin_pane_items.add(url_class_name_label);
plugin_pane_items.add(url_class_name);
add_plugin = new JButton("Add Application");
add_plugin.setActionCommand("add_plugin");
add_plugin.addActionListener(this);
plugin_pane_items.add(new JLabel());
plugin_pane_items.add(add_plugin);
// Active apps section
JPanel plugin_pane_active_apps = new JPanel(new BorderLayout());
apps = new DefaultListModel();
JList apps_jlist = new JList(apps);
apps_jlist.setFont(font);
JScrollPane apps_scroll = new JScrollPane();
apps_scroll.getViewport().setView(apps_jlist);
apps_scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
plugin_pane_active_apps.add(new JLabel("Active Applications", JLabel.LEFT), BorderLayout.PAGE_START);
plugin_pane_active_apps.add(apps_scroll, BorderLayout.CENTER);
// add main nodes
plugin_pane.add(plugin_pane_items, BorderLayout.PAGE_START);
plugin_pane.add(plugin_pane_active_apps, BorderLayout.CENTER);
tabbed.addTab("Applications", null, plugin_pane, "");
// Sessions Tab
sessions = new DefaultListModel();
JList sessions_jlist = new JList(sessions);
//Font font2 = new Font("Monospaced", Font.BOLD, 12);
sessions_jlist.setFont(font);
//sessions_jlist.setForeground(Color.WHITE);
//sessions_jlist.setBackground(Color.BLACK);
JScrollPane sessions_scroll = new JScrollPane();
sessions_scroll.getViewport().setView(sessions_jlist);
sessions_scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
tabbed.addTab("Active Sessions", null, sessions_scroll, "");
// finish window
this.add(tabbed);
centerWindow();
this.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if ("add_plugin".equals(e.getActionCommand()))
{
core.addPlugin(url_path.getText(), url_class_name.getText());
}
if ("start".equals(e.getActionCommand()))
{
start_server();
}
if ("stop".equals(e.getActionCommand()))
{
stop_server();
}
}
public void start_server()
{
start_btn.setEnabled(false);
stop_btn.setEnabled(true);
core = new CoreServer(this.global_settings, this)
{
public void pluginChange()
{
populatePluginList();
}
public void onRequest(HttpRequest request)
{
populateSessions();
}
};
core.setPort(Integer.valueOf(port_field.getText()).intValue());
core.setWebRoot(root_field.getText());
core.setDebug(debug_field.isSelected());
core.setShowData(show_data_field.isSelected());
core.getPlaceboLogger().addDisplay(new PrintStream(log_output, true));
if (debug_field.isSelected())
{
tabbed.setSelectedIndex(1);
if (isCentered())
{
logSizeWindow();
}
}
core.start();
if (init_plugins != null)
{
core.addPlugins(init_plugins);
}
}
public void stop_server()
{
stop_btn.setEnabled(false);
core.quit();
start_btn.setEnabled(true);
}
}
|