Skip to content

Commit 8322222

Browse files
authored
Add files via upload
1 parent bd93e5b commit 8322222

File tree

1 file changed

+158
-158
lines changed

1 file changed

+158
-158
lines changed
Lines changed: 158 additions & 158 deletions
Original file line numberDiff line numberDiff line change
@@ -1,158 +1,158 @@
1-
package aima.gui.swing.demo;
2-
3-
import java.awt.BorderLayout;
4-
import java.awt.Component;
5-
import java.awt.event.ActionEvent;
6-
import java.awt.event.ActionListener;
7-
import java.io.PrintStream;
8-
import java.lang.reflect.Method;
9-
10-
import javax.swing.JComponent;
11-
import javax.swing.JFrame;
12-
import javax.swing.JMenu;
13-
import javax.swing.JMenuBar;
14-
import javax.swing.JMenuItem;
15-
16-
import aima.gui.swing.framework.MessageLoggerPanel;
17-
18-
/**
19-
* Provides a simple frame for starting agent applications and console program
20-
* demos.
21-
*
22-
* @author Ruediger Lunde
23-
*/
24-
public class AimaDemoFrame extends JFrame {
25-
26-
private static final long serialVersionUID = 1L;
27-
protected JMenuBar menubar = new JMenuBar();
28-
JMenu appsMenu = new JMenu("Apps");
29-
JMenu progsMenu = new JMenu("Progs");
30-
MessageLoggerPanel textPanel = new MessageLoggerPanel();
31-
JComponent currPanel;
32-
PrintStream outStream;
33-
34-
/** Standard constructor. */
35-
public AimaDemoFrame() {
36-
setTitle("Artificial Intelligence a Modern Approach 3rd ed. Java Demos (AIMA3e-Java)");
37-
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
38-
setJMenuBar(menubar);
39-
menubar.add(appsMenu);
40-
menubar.add(progsMenu);
41-
outStream = System.out;
42-
}
43-
44-
/**
45-
* Adds a new agent application to the menu. The class is expected to be
46-
* part of a package and to provide a <code>constructApplicationFrame</code>
47-
* method.
48-
*/
49-
public void addApp(Class<?> appClass) {
50-
JMenuItem item = addDemoToMenu(appsMenu, appClass);
51-
item.addActionListener(new AppStarter(appClass));
52-
}
53-
54-
/**
55-
* Adds a new console application demo to the menu. The class is expected to be
56-
* part of a package and to provide a static main method.
57-
*/
58-
public void addProg(Class<?> demoClass) {
59-
JMenuItem item = addDemoToMenu(progsMenu, demoClass);
60-
item.addActionListener(new ProgStarter(demoClass));
61-
}
62-
63-
/**
64-
* Adds a new demo (agent application or console application) to the specified
65-
* menu.
66-
*/
67-
private JMenuItem addDemoToMenu(JMenu menu, Class<?> demoClass) {
68-
JMenuItem item = new JMenuItem(demoClass.getSimpleName());
69-
JMenu subMenu = null;
70-
String packageName = demoClass.getPackage().getName();
71-
Component[] menuComps = menu.getMenuComponents();
72-
int i;
73-
for (i = 0; i < menuComps.length; i++) {
74-
JMenu comp = (JMenu) menuComps[i];
75-
if (comp.getText().equals(packageName))
76-
subMenu = comp;
77-
else if (comp.getText().compareTo(packageName) > 0)
78-
break;
79-
}
80-
if (subMenu == null) {
81-
subMenu = new JMenu(packageName);
82-
menu.add(subMenu, i);
83-
}
84-
subMenu.add(item);
85-
return item;
86-
}
87-
88-
89-
// ///////////////////////////////////////////////////////////////
90-
// inner classes
91-
92-
/**
93-
* Implements an action listener which starts an agent application.
94-
*
95-
* @author Ruediger Lunde
96-
*/
97-
protected class AppStarter implements ActionListener {
98-
Class<?> appClass;
99-
100-
AppStarter(Class<?> ac) {
101-
appClass = ac;
102-
}
103-
104-
@Override
105-
public void actionPerformed(ActionEvent ev) {
106-
try {
107-
if (currPanel != null)
108-
getContentPane().remove(currPanel);
109-
System.setOut(outStream);
110-
Object instance = appClass.newInstance();
111-
Method m = appClass.getMethod("constructApplicationFrame",
112-
new Class[0]);
113-
JFrame af = (JFrame) m.invoke(instance, (Object[]) null);
114-
currPanel = (JComponent) af.getContentPane().getComponent(0);
115-
af.getContentPane().remove(currPanel);
116-
getContentPane().add(currPanel, BorderLayout.CENTER);
117-
validate();
118-
} catch (Exception e) {
119-
e.printStackTrace();
120-
}
121-
}
122-
}
123-
124-
/**
125-
* Implements an action listener which starts a console application demo.
126-
*
127-
* @author Ruediger Lunde
128-
*/
129-
protected class ProgStarter implements ActionListener {
130-
Class<?> demoClass;
131-
132-
ProgStarter(Class<?> dc) {
133-
demoClass = dc;
134-
}
135-
136-
@Override
137-
public void actionPerformed(ActionEvent arg0) {
138-
try {
139-
if (currPanel == textPanel)
140-
textPanel.clear();
141-
else {
142-
if (currPanel != null)
143-
getContentPane().remove(currPanel);
144-
getContentPane().add(textPanel, BorderLayout.CENTER);
145-
// redirect the standard output into the text area
146-
System.setOut(textPanel.getPrintStream());
147-
// System.setErr(panel.getPrintStream());
148-
validate();
149-
currPanel = textPanel;
150-
}
151-
Method m = demoClass.getMethod("main", String[].class);
152-
m.invoke(null, (Object) new String[] {});
153-
} catch (Exception e) {
154-
e.printStackTrace();
155-
}
156-
}
157-
}
158-
}
1+
package aima.gui.swing.demo;
2+
3+
import java.awt.BorderLayout;
4+
import java.awt.Component;
5+
import java.awt.event.ActionEvent;
6+
import java.awt.event.ActionListener;
7+
import java.io.PrintStream;
8+
import java.lang.reflect.Method;
9+
10+
import javax.swing.JComponent;
11+
import javax.swing.JFrame;
12+
import javax.swing.JMenu;
13+
import javax.swing.JMenuBar;
14+
import javax.swing.JMenuItem;
15+
16+
import aima.gui.swing.framework.MessageLoggerPanel;
17+
18+
/**
19+
* Provides a simple frame for starting agent applications and console program
20+
* demos.
21+
*
22+
* @author Ruediger Lunde
23+
*/
24+
public class AimaDemoFrame extends JFrame {
25+
26+
private static final long serialVersionUID = 1L;
27+
protected JMenuBar menubar = new JMenuBar();
28+
JMenu appsMenu = new JMenu("Apps");
29+
JMenu progsMenu = new JMenu("Progs");
30+
MessageLoggerPanel textPanel = new MessageLoggerPanel();
31+
JComponent currPanel;
32+
PrintStream outStream;
33+
34+
/** Standard constructor. */
35+
public AimaDemoFrame() {
36+
setTitle("Artificial Intelligence a Modern Approach 3rd ed. Java Demos (AIMA3e-Java)");
37+
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
38+
setJMenuBar(menubar);
39+
menubar.add(appsMenu);
40+
menubar.add(progsMenu);
41+
outStream = System.out;
42+
}
43+
44+
/**
45+
* Adds a new agent application to the menu. The class is expected to be
46+
* part of a package and to provide a <code>constructApplicationFrame</code>
47+
* method.
48+
*/
49+
public void addApp(Class<?> appClass) {
50+
JMenuItem item = addDemoToMenu(appsMenu, appClass);
51+
item.addActionListener(new AppStarter(appClass));
52+
}
53+
54+
/**
55+
* Adds a new console application demo to the menu. The class is expected to be
56+
* part of a package and to provide a static main method.
57+
*/
58+
public void addProg(Class<?> demoClass) {
59+
JMenuItem item = addDemoToMenu(progsMenu, demoClass);
60+
item.addActionListener(new ProgStarter(demoClass));
61+
}
62+
63+
/**
64+
* Adds a new demo (agent application or console application) to the specified
65+
* menu.
66+
*/
67+
private JMenuItem addDemoToMenu(JMenu menu, Class<?> demoClass) {
68+
JMenuItem item = new JMenuItem(demoClass.getSimpleName());
69+
JMenu subMenu = null;
70+
String packageName = demoClass.getPackage().getName();
71+
Component[] menuComps = menu.getMenuComponents();
72+
int i;
73+
for (i = 0; i < menuComps.length; i++) {
74+
JMenu comp = (JMenu) menuComps[i];
75+
if (comp.getText().equals(packageName))
76+
subMenu = comp;
77+
else if (comp.getText().compareTo(packageName) > 0)
78+
break;
79+
}
80+
if (subMenu == null) {
81+
subMenu = new JMenu(packageName);
82+
menu.add(subMenu, i);
83+
}
84+
subMenu.add(item);
85+
return item;
86+
}
87+
88+
89+
// ///////////////////////////////////////////////////////////////
90+
// inner classes
91+
92+
/**
93+
* Implements an action listener which starts an agent application.
94+
*
95+
* @author Ruediger Lunde
96+
*/
97+
protected class AppStarter implements ActionListener {
98+
Class<?> appClass;
99+
100+
AppStarter(Class<?> ac) {
101+
appClass = ac;
102+
}
103+
104+
@Override
105+
public void actionPerformed(ActionEvent ev) {
106+
try {
107+
if (currPanel != null)
108+
getContentPane().remove(currPanel);
109+
System.setOut(outStream);
110+
Object instance = appClass.newInstance();
111+
Method m = appClass.getMethod("constructApplicationFrame",
112+
new Class[0]);
113+
JFrame af = (JFrame) m.invoke(instance, (Object[]) null);
114+
currPanel = (JComponent) af.getContentPane().getComponent(0);
115+
af.getContentPane().remove(currPanel);
116+
getContentPane().add(currPanel, BorderLayout.CENTER);
117+
validate();
118+
} catch (Exception e) {
119+
e.printStackTrace();
120+
}
121+
}
122+
}
123+
124+
/**
125+
* Implements an action listener which starts a console application demo.
126+
*
127+
* @author Ruediger Lunde
128+
*/
129+
protected class ProgStarter implements ActionListener {
130+
Class<?> demoClass;
131+
132+
ProgStarter(Class<?> dc) {
133+
demoClass = dc;
134+
}
135+
136+
@Override
137+
public void actionPerformed(ActionEvent arg0) {
138+
try {
139+
if (currPanel == textPanel)
140+
textPanel.clear();
141+
else {
142+
if (currPanel != null)
143+
getContentPane().remove(currPanel);
144+
getContentPane().add(textPanel, BorderLayout.CENTER);
145+
// redirect the standard output into the text area
146+
System.setOut(textPanel.getPrintStream());
147+
// System.setErr(panel.getPrintStream());
148+
validate();
149+
currPanel = textPanel;
150+
}
151+
Method m = demoClass.getMethod("main", String[].class);
152+
m.invoke(null, (Object) new String[] {});
153+
} catch (Exception e) {
154+
e.printStackTrace();
155+
}
156+
}
157+
}
158+
}

0 commit comments

Comments
 (0)