Project: engagement_test License: BSD Dependencies:
Used by:
None |
engagement_test/src/edu/wpi/hri/ros/testing/demo/DemoFrame.javaGo to the documentation of this file.00001 /* 00002 * Software License Agreement (BSD License) 00003 * 00004 * Copyright (c) 2010, Worcester Polytechnic Institute All rights reserved. 00005 * 00006 * Redistribution and use in source and binary forms, with or without 00007 * modification, are permitted provided that the following conditions are met: 00008 * 00009 * * Redistributions of source code must retain the above copyright notice, this 00010 * list of conditions and the following disclaimer. * Redistributions in binary 00011 * form must reproduce the above copyright notice, this list of conditions and 00012 * the following disclaimer in the documentation and/or other materials provided 00013 * with the distribution. * Neither the name of Worcester Polytechnic Institute. 00014 * nor the names of its contributors may be used to endorse or promote products 00015 * derived from this software without specific prior written permission. 00016 * 00017 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00018 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00019 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00020 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 00021 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00022 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00023 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00024 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00025 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00026 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00027 * POSSIBILITY OF SUCH DAMAGE. 00028 */ 00029 package edu.wpi.hri.ros.testing.demo; 00030 00031 import java.awt.BorderLayout; 00032 import java.awt.GridLayout; 00033 import java.awt.event.ActionEvent; 00034 import java.awt.event.ActionListener; 00035 import java.awt.event.WindowEvent; 00036 import java.awt.event.WindowListener; 00037 import java.io.File; 00038 import java.io.FilenameFilter; 00039 import java.io.IOException; 00040 import java.util.Scanner; 00041 00042 import javax.swing.DefaultListModel; 00043 import javax.swing.JButton; 00044 import javax.swing.JFrame; 00045 import javax.swing.JList; 00046 import javax.swing.JPanel; 00047 import javax.swing.JScrollPane; 00048 import javax.swing.JSplitPane; 00049 import javax.swing.JTextPane; 00050 import javax.swing.ListSelectionModel; 00051 import javax.swing.event.ListSelectionEvent; 00052 import javax.swing.event.ListSelectionListener; 00053 00061 public class DemoFrame extends JFrame { 00062 00063 private static final long serialVersionUID = 1L; 00064 // general panels 00065 private JSplitPane splitPane; 00066 private JSplitPane rgSplit; 00067 00068 // realizer items 00069 private JPanel realizerPanel; 00070 private JScrollPane realizerScroll; 00071 private JList realizerList; 00072 private JButton realizerButton; 00073 private final File realizerFolder; 00074 00075 // realizer items 00076 private JPanel generationPanel; 00077 private JScrollPane generationScroll; 00078 private JList generationList; 00079 private JButton generationButton; 00080 private final File generationFolder; 00081 00082 // other 00083 private JTextPane filePane; 00084 private final Demo demo; 00085 00091 DemoFrame(Demo demo) { 00092 initComponents(); 00093 00094 this.demo = demo; 00095 this.realizerFolder = new File(demo.realizerSamples); 00096 DefaultListModel listModel = (DefaultListModel) (realizerList 00097 .getModel()); 00098 // add each xml file in the sample folder to the list 00099 for (String file : this.realizerFolder.list(new XMLFileFilter())) 00100 listModel.addElement(file); 00101 00102 this.generationFolder = new File(demo.generationSamples); 00103 listModel = (DefaultListModel) (generationList.getModel()); 00104 // add each xml file in the sample folder to the list 00105 for (String file : this.generationFolder.list(new XMLFileFilter())) 00106 listModel.addElement(file); 00107 } 00108 00109 private void initComponents() { 00110 setLayout(new GridLayout(1, 1)); 00111 add(getSplitPane()); 00112 addWindowListener(new DemoWindowListener()); 00113 setSize(702, 466); 00114 } 00115 00116 private JSplitPane getSplitPane() { 00117 if (splitPane == null) { 00118 splitPane = new JSplitPane(); 00119 splitPane.setDividerLocation(150); 00120 splitPane.setLeftComponent(getRgSplit()); 00121 splitPane.setRightComponent(getFilePane()); 00122 } 00123 return splitPane; 00124 } 00125 00126 private JSplitPane getRgSplit() { 00127 if (rgSplit == null) { 00128 rgSplit = new JSplitPane(JSplitPane.VERTICAL_SPLIT); 00129 rgSplit.setTopComponent(getRealizerPanel()); 00130 rgSplit.setBottomComponent(getGenerationPanel()); 00131 } 00132 return rgSplit; 00133 } 00134 00135 private JButton getRealizerButton() { 00136 if (realizerButton == null) { 00137 realizerButton = new JButton(); 00138 realizerButton.setText("Realize"); 00139 realizerButton.setRolloverEnabled(false); 00140 realizerButton.addActionListener(new ActionListener() { 00141 00142 public void actionPerformed(ActionEvent event) { 00143 realizerActionActionPerformed(event); 00144 } 00145 }); 00146 } 00147 return realizerButton; 00148 } 00149 00150 private JPanel getRealizerPanel() { 00151 if (realizerPanel == null) { 00152 realizerPanel = new JPanel(); 00153 realizerPanel.setLayout(new BorderLayout()); 00154 realizerPanel.add(getRealizerScroll(), BorderLayout.CENTER); 00155 realizerPanel.add(getRealizerButton(), BorderLayout.SOUTH); 00156 } 00157 return realizerPanel; 00158 } 00159 00160 private JScrollPane getRealizerScroll() { 00161 if (realizerScroll == null) { 00162 realizerScroll = new JScrollPane(); 00163 realizerScroll.setViewportView(getRealizerList()); 00164 } 00165 return realizerScroll; 00166 } 00167 00168 private JList getRealizerList() { 00169 if (realizerList == null) { 00170 realizerList = new JList(); 00171 realizerList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 00172 DefaultListModel listModel = new DefaultListModel(); 00173 realizerList.setModel(listModel); 00174 realizerList.addListSelectionListener(new ListSelectionListener() { 00175 00176 public void valueChanged(ListSelectionEvent event) { 00177 realizerListSelectionValueChanged(event); 00178 } 00179 }); 00180 } 00181 return realizerList; 00182 } 00183 00184 private JButton getGenerationButton() { 00185 if (generationButton == null) { 00186 generationButton = new JButton(); 00187 generationButton.setText("Generation"); 00188 generationButton.setRolloverEnabled(false); 00189 generationButton.addActionListener(new ActionListener() { 00190 00191 public void actionPerformed(ActionEvent event) { 00192 generationActionActionPerformed(event); 00193 } 00194 }); 00195 } 00196 return generationButton; 00197 } 00198 00199 private JPanel getGenerationPanel() { 00200 if (generationPanel == null) { 00201 generationPanel = new JPanel(); 00202 generationPanel.setLayout(new BorderLayout()); 00203 generationPanel.add(getGenerationScroll(), BorderLayout.CENTER); 00204 generationPanel.add(getGenerationButton(), BorderLayout.SOUTH); 00205 } 00206 return generationPanel; 00207 } 00208 00209 private JScrollPane getGenerationScroll() { 00210 if (generationScroll == null) { 00211 generationScroll = new JScrollPane(); 00212 generationScroll.setViewportView(getGenerationList()); 00213 } 00214 return generationScroll; 00215 } 00216 00217 private JList getGenerationList() { 00218 if (generationList == null) { 00219 generationList = new JList(); 00220 generationList 00221 .setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 00222 DefaultListModel listModel = new DefaultListModel(); 00223 generationList.setModel(listModel); 00224 generationList 00225 .addListSelectionListener(new ListSelectionListener() { 00226 00227 public void valueChanged(ListSelectionEvent event) { 00228 generationListSelectionValueChanged(event); 00229 } 00230 }); 00231 } 00232 return generationList; 00233 } 00234 00235 private JTextPane getFilePane() { 00236 if (filePane == null) { 00237 filePane = new JTextPane(); 00238 } 00239 return filePane; 00240 } 00241 00242 private void realizerListSelectionValueChanged(ListSelectionEvent event) { 00243 try { 00244 String value = realizerList.getSelectedValue().toString(); 00245 if (value == null || value.isEmpty()) 00246 return; 00247 00248 File file = new File(this.realizerFolder, value); 00249 String contents = ""; 00250 Scanner scan = new Scanner(file); 00251 while (scan.hasNextLine()) 00252 contents += scan.nextLine() + "\n"; 00253 scan.close(); 00254 filePane.setText(contents); 00255 00256 generationList.setSelectedIndex(-1); 00257 } catch (IOException e) { 00258 } 00259 } 00260 00261 private void realizerActionActionPerformed(ActionEvent event) { 00262 demo.sendRealizer(filePane.getText()); 00263 } 00264 00265 private void generationListSelectionValueChanged(ListSelectionEvent event) { 00266 try { 00267 String value = realizerList.getSelectedValue().toString(); 00268 if (value == null || value.isEmpty()) 00269 return; 00270 00271 File file = new File(this.realizerFolder, value); 00272 String contents = ""; 00273 Scanner scan = new Scanner(file); 00274 while (scan.hasNextLine()) 00275 contents += scan.nextLine() + "\n"; 00276 scan.close(); 00277 filePane.setText(contents); 00278 00279 realizerList.setSelectedIndex(-1); 00280 } catch (IOException e) { 00281 } 00282 } 00283 00284 private void generationActionActionPerformed(ActionEvent event) { 00285 demo.sendGeneration(filePane.getText()); 00286 } 00287 00288 private class XMLFileFilter implements FilenameFilter { 00289 00290 @Override 00291 public boolean accept(File arg0, String str) { 00292 return str.endsWith(".xml"); 00293 } 00294 } 00295 00296 private class DemoWindowListener implements WindowListener { 00297 00298 @Override 00299 public void windowActivated(WindowEvent e) { 00300 } 00301 00302 @Override 00303 public void windowClosed(WindowEvent e) { 00304 } 00305 00306 @Override 00307 public void windowClosing(WindowEvent e) { 00308 demo.shutdown(); 00309 setVisible(false); 00310 dispose(); 00311 } 00312 00313 @Override 00314 public void windowDeactivated(WindowEvent e) { 00315 } 00316 00317 @Override 00318 public void windowDeiconified(WindowEvent e) { 00319 } 00320 00321 @Override 00322 public void windowIconified(WindowEvent e) { 00323 } 00324 00325 @Override 00326 public void windowOpened(WindowEvent e) { 00327 } 00328 } 00329 } |