00001
00002
00003
00004 package org.classroomgaming.cgp;
00005
00006 import java.awt.BorderLayout;
00007 import javax.swing.JComponent;
00008 import org.jdesktop.application.Action;
00009 import org.jdesktop.application.ResourceMap;
00010 import org.jdesktop.application.SingleFrameApplication;
00011 import org.jdesktop.application.FrameView;
00012 import org.jdesktop.application.TaskMonitor;
00013 import java.awt.event.ActionEvent;
00014 import java.awt.event.ActionListener;
00015 import java.awt.event.KeyListener;
00016 import javax.swing.Timer;
00017 import javax.swing.Icon;
00018 import javax.swing.JDialog;
00019 import javax.swing.JFrame;
00020 import javax.swing.UIManager;
00021 import javax.swing.plaf.ComponentUI;
00022
00026 public class CGPView extends FrameView {
00027
00028 protected GameViewer game;
00029
00030 public CGPView(SingleFrameApplication app) {
00031 super(app);
00032 UIManager.put(GameViewerUI.UI_CLASS_ID, "GameUI");
00033
00034 initComponents();
00035
00036 game = new GameViewer();
00037 jLayeredPane1.setSize(640, 480);
00038 game.setSize(jLayeredPane1.getWidth(), jLayeredPane1.getHeight());
00039
00040 jLayeredPane1.add(game, BorderLayout.CENTER);
00041
00042
00043
00045
00046
00047
00048
00049
00050 ResourceMap resourceMap = getResourceMap();
00051 int messageTimeout = resourceMap.getInteger("StatusBar.messageTimeout");
00052 messageTimer = new Timer(messageTimeout, new ActionListener() {
00053
00054 public void actionPerformed(ActionEvent e) {
00055 statusMessageLabel.setText("");
00056 }
00057 });
00058 messageTimer.setRepeats(false);
00059 int busyAnimationRate = resourceMap.getInteger("StatusBar.busyAnimationRate");
00060 for (int i = 0; i < busyIcons.length; i++) {
00061 busyIcons[i] = resourceMap.getIcon("StatusBar.busyIcons[" + i + "]");
00062 }
00063 busyIconTimer = new Timer(busyAnimationRate, new ActionListener() {
00064
00065 public void actionPerformed(ActionEvent e) {
00066 busyIconIndex = (busyIconIndex + 1) % busyIcons.length;
00067 statusAnimationLabel.setIcon(busyIcons[busyIconIndex]);
00068 }
00069 });
00070 idleIcon = resourceMap.getIcon("StatusBar.idleIcon");
00071 statusAnimationLabel.setIcon(idleIcon);
00072 progressBar.setVisible(false);
00073
00074
00075 TaskMonitor taskMonitor = new TaskMonitor(getApplication().getContext());
00076 taskMonitor.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
00077
00078 public void propertyChange(java.beans.PropertyChangeEvent evt) {
00079 String propertyName = evt.getPropertyName();
00080 if ("started".equals(propertyName)) {
00081 if (!busyIconTimer.isRunning()) {
00082 statusAnimationLabel.setIcon(busyIcons[0]);
00083 busyIconIndex = 0;
00084 busyIconTimer.start();
00085 }
00086 progressBar.setVisible(true);
00087 progressBar.setIndeterminate(true);
00088 } else if ("done".equals(propertyName)) {
00089 busyIconTimer.stop();
00090 statusAnimationLabel.setIcon(idleIcon);
00091 progressBar.setVisible(false);
00092 progressBar.setValue(0);
00093 } else if ("message".equals(propertyName)) {
00094 String text = (String) (evt.getNewValue());
00095 statusMessageLabel.setText((text == null) ? "" : text);
00096 messageTimer.restart();
00097 } else if ("progress".equals(propertyName)) {
00098 int value = (Integer) (evt.getNewValue());
00099 progressBar.setVisible(true);
00100 progressBar.setIndeterminate(false);
00101 progressBar.setValue(value);
00102 }
00103 }
00104 });
00105
00106 game.start();
00107 try {
00108 MapModule m = (MapModule) game.getModel().getModule("map");
00109 jLayeredPane1.addKeyListener((KeyListener) m);
00110
00111 } catch (ClassCastException e) {
00112 }
00113
00114
00115 }
00116
00117 @Action
00118 public void showAboutBox() {
00119 if (aboutBox == null) {
00120 JFrame mainFrame = CGPApp.getApplication().getMainFrame();
00121 aboutBox = new CGPAboutBox(mainFrame);
00122 aboutBox.setLocationRelativeTo(mainFrame);
00123 }
00124 CGPApp.getApplication().show(aboutBox);
00125 }
00126
00132 @SuppressWarnings("unchecked")
00133
00134 private void initComponents() {
00135
00136 menuBar = new javax.swing.JMenuBar();
00137 javax.swing.JMenu fileMenu = new javax.swing.JMenu();
00138 javax.swing.JMenuItem exitMenuItem = new javax.swing.JMenuItem();
00139 javax.swing.JMenu helpMenu = new javax.swing.JMenu();
00140 javax.swing.JMenuItem aboutMenuItem = new javax.swing.JMenuItem();
00141 statusPanel = new javax.swing.JPanel();
00142 javax.swing.JSeparator statusPanelSeparator = new javax.swing.JSeparator();
00143 statusMessageLabel = new javax.swing.JLabel();
00144 statusAnimationLabel = new javax.swing.JLabel();
00145 progressBar = new javax.swing.JProgressBar();
00146 jLayeredPane1 = new javax.swing.JLayeredPane() {
00147 public ComponentUI getUI() {
00148 return game.getUI();
00149 }
00150 };
00151
00152 menuBar.setName("menuBar");
00153
00154 org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(org.classroomgaming.cgp.CGPApp.class).getContext().getResourceMap(CGPView.class);
00155 fileMenu.setText(resourceMap.getString("fileMenu.text"));
00156 fileMenu.setName("fileMenu");
00157
00158 javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(org.classroomgaming.cgp.CGPApp.class).getContext().getActionMap(CGPView.class, this);
00159 exitMenuItem.setAction(actionMap.get("quit"));
00160 exitMenuItem.setName("exitMenuItem");
00161 fileMenu.add(exitMenuItem);
00162
00163 menuBar.add(fileMenu);
00164
00165 helpMenu.setText(resourceMap.getString("helpMenu.text"));
00166 helpMenu.setName("helpMenu");
00167
00168 aboutMenuItem.setAction(actionMap.get("showAboutBox"));
00169 aboutMenuItem.setName("aboutMenuItem");
00170 helpMenu.add(aboutMenuItem);
00171
00172 menuBar.add(helpMenu);
00173
00174 statusPanel.setName("statusPanel");
00175
00176 statusPanelSeparator.setName("statusPanelSeparator");
00177
00178 statusMessageLabel.setName("statusMessageLabel");
00179
00180 statusAnimationLabel.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
00181 statusAnimationLabel.setName("statusAnimationLabel");
00182
00183 progressBar.setName("progressBar");
00184
00185 org.jdesktop.layout.GroupLayout statusPanelLayout = new org.jdesktop.layout.GroupLayout(statusPanel);
00186 statusPanel.setLayout(statusPanelLayout);
00187 statusPanelLayout.setHorizontalGroup(
00188 statusPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
00189 .add(statusPanelSeparator, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
00190 .add(statusPanelLayout.createSequentialGroup()
00191 .addContainerGap()
00192 .add(statusMessageLabel)
00193 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 360, Short.MAX_VALUE)
00194 .add(statusAnimationLabel)
00195 .addContainerGap())
00196 .add(org.jdesktop.layout.GroupLayout.TRAILING, statusPanelLayout.createSequentialGroup()
00197 .addContainerGap(246, Short.MAX_VALUE)
00198 .add(progressBar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
00199 .add(8, 8, 8))
00200 );
00201 statusPanelLayout.setVerticalGroup(
00202 statusPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
00203 .add(statusPanelLayout.createSequentialGroup()
00204 .add(statusPanelSeparator, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
00205 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
00206 .add(statusPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
00207 .add(statusMessageLabel)
00208 .add(statusAnimationLabel))
00209 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
00210 .add(progressBar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
00211 );
00212
00213 jLayeredPane1.setName("jLayeredPane1");
00214 jLayeredPane1.setPreferredSize(new java.awt.Dimension(640, 480));
00215 jLayeredPane1.addFocusListener(new java.awt.event.FocusAdapter() {
00216 public void focusGained(java.awt.event.FocusEvent evt) {
00217 onFocusGained(evt);
00218 }
00219 public void focusLost(java.awt.event.FocusEvent evt) {
00220 onFocusLost(evt);
00221 }
00222 });
00223
00224 setComponent(jLayeredPane1);
00225 setMenuBar(menuBar);
00226 setStatusBar(statusPanel);
00227 }
00228
00229 private void onFocusGained(java.awt.event.FocusEvent evt) {
00230
00231 game.requestFocus();
00232 game.start();
00233 }
00234
00235 private void onFocusLost(java.awt.event.FocusEvent evt) {
00236
00237 game.stop();
00238 }
00239
00240
00241 protected javax.swing.JLayeredPane jLayeredPane1;
00242 private javax.swing.JMenuBar menuBar;
00243 private javax.swing.JProgressBar progressBar;
00244 private javax.swing.JLabel statusAnimationLabel;
00245 private javax.swing.JLabel statusMessageLabel;
00246 private javax.swing.JPanel statusPanel;
00247
00248 private final Timer messageTimer;
00249 private final Timer busyIconTimer;
00250 private final Icon idleIcon;
00251 private final Icon[] busyIcons = new Icon[15];
00252 private int busyIconIndex = 0;
00253 private JDialog aboutBox;
00254 }