1 package org.lsst.ccs.command.demo.main;
2
3 import java.awt.event.ActionEvent;
4 import java.awt.event.ActionListener;
5 import java.io.IOException;
6 import java.util.logging.Level;
7 import java.util.logging.Logger;
8 import javax.swing.JButton;
9 import javax.swing.JFrame;
10 import javax.swing.JPanel;
11 import org.apache.commons.cli.BasicParser;
12 import org.apache.commons.cli.CommandLine;
13 import org.apache.commons.cli.CommandLineParser;
14 import org.apache.commons.cli.HelpFormatter;
15 import org.apache.commons.cli.Options;
16 import org.lsst.ccs.command.CommandSetBuilder;
17 import org.lsst.ccs.command.demo.DemoCommands;
18 import org.lsst.ccs.command.demo.remote.jgroups.JGroupsCommandClient;
19 import org.lsst.ccs.command.demo.remote.jgroups.JGroupsCommandServer;
20 import org.lsst.ccs.demo.shell.SwingShell;
21 import org.lsst.ccs.shell.JLineShell;
22
23
24
25
26
27
28 public class Main {
29
30 public static void main(String[] args) throws Exception {
31
32 Options mainOptions = new Options();
33 mainOptions.addOption("h", "help", false, "Print the help message");
34
35 mainOptions.addOption("m", "mode", true, "The MODE in which the shell operates.\n"
36 + "\tshell -- run the interactive shell locally"
37 + "\tserver -- run a distributed command server"
38 + "\tclient -- run a distributed command client"
39 + "\tgui -- an interactive gui, the default");
40 mainOptions.getOption("mode").setArgName("MODE");
41
42 CommandLineParser parser = new BasicParser();
43 CommandLine line = parser.parse(mainOptions, args, true);
44
45 boolean printHelp = false;
46
47 if (line.hasOption("help")) {
48 printHelp = true;
49 } else {
50 String mode = line.getOptionValue("mode");
51
52 Main main = new Main();
53 if (mode != null) {
54 switch (mode) {
55 case "shell":
56 Main.runShell();
57 break;
58 case "client":
59 Main.runClient();
60 break;
61 case "server":
62 Main.runServer();
63 break;
64 case "gui":
65 main.runGui();
66 break;
67 default:
68 printHelp = true;
69 }
70 } else {
71 main.runGui();
72 }
73 }
74
75 if (printHelp) {
76 HelpFormatter formatter = new HelpFormatter();
77 formatter.printHelp(100, "Command Demo", "", mainOptions, "", true);
78 }
79 }
80
81 private void runGui() throws IOException {
82 Main.GUIPanel gui = new Main.GUIPanel();
83 JFrame frame = new JFrame("CCS Command Demo");
84 frame.setContentPane(gui);
85 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
86 frame.setLocationByPlatform(true);
87 frame.pack();
88 frame.setVisible(true);
89 }
90
91 private static void runShell() throws IOException, Exception {
92 CommandSetBuilder builder = new CommandSetBuilder();
93 JLineShell shell = new JLineShell(builder.buildCommandSet(new DemoCommands()));
94 shell.run();
95 }
96
97 private static void runClient() throws Exception {
98 try (JGroupsCommandClient client = new JGroupsCommandClient()) {
99 JLineShell shell = new JLineShell(client);
100 shell.run();
101 }
102 }
103
104 private static void runServer() throws Exception {
105 CommandSetBuilder builder = new CommandSetBuilder();
106 JGroupsCommandServer server = new JGroupsCommandServer(builder.buildCommandSet(new DemoCommands()));
107 }
108
109 private static class GUIPanel extends JPanel implements ActionListener {
110
111 public GUIPanel() {
112 addButton(new JButton("Open Local Shell"), "shell");
113 addButton(new JButton("Open Distributed command client"), "client");
114 addButton(new JButton("Run Distributed command server"), "server");
115 }
116
117 @Override
118 public void actionPerformed(ActionEvent e) {
119 try {
120 String command = e.getActionCommand();
121 switch (command) {
122 case "shell":
123 runShell();
124 break;
125 case "client":
126 runClient();
127 break;
128 case "server":
129 runServer();
130 break;
131 }
132 } catch (Exception ex) {
133 Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
134 }
135
136 }
137
138 private void runShell() throws IOException {
139 CommandSetBuilder builder = new CommandSetBuilder();
140 final SwingShell swingShell = new SwingShell(builder.buildCommandSet(new DemoCommands()), "Swing Shell");
141 Thread t = new Thread() {
142 @Override
143 public void run() {
144 try {
145 swingShell.run();
146 } catch (IOException ex) {
147 Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
148 }
149 }
150 };
151 t.start();
152 }
153
154 private void runClient() throws Exception {
155 JGroupsCommandClient client = new JGroupsCommandClient();
156 final SwingShell swingShell = new SwingShell(client, "Remote Swing Shell");
157 Thread t = new Thread() {
158 @Override
159 public void run() {
160 try {
161 swingShell.run();
162 } catch (IOException ex) {
163 Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
164 }
165 }
166 };
167 t.start();
168 }
169
170 private void runServer() {
171
172 }
173
174 private void addButton(JButton jButton, String command) {
175 add(jButton);
176 jButton.setActionCommand(command);
177 jButton.addActionListener(this);
178 }
179 }
180 }
181