1 /*** 2 * Redistribution and use in source and binary forms, with or without 3 * modification, are permitted provided that the following conditions are 4 * met : 5 * 6 * . Redistributions of source code must retain the above copyright 7 * notice, this list of conditions and the following disclaimer. 8 * 9 * . Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * . The name of the author may not be used to endorse or promote products 14 * derived from this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 20 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 24 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 25 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 * 28 * $Id: View.java,v 1.2 2005/09/10 14:06:08 mat007 Exp $ 29 */ 30 31 package battlehex.ui.main; 32 33 import java.awt.Color; 34 import java.awt.event.ActionEvent; 35 import java.awt.event.ActionListener; 36 import java.awt.event.WindowAdapter; 37 import java.awt.event.WindowEvent; 38 import javax.swing.BorderFactory; 39 import javax.swing.JComponent; 40 import javax.swing.JFrame; 41 import javax.swing.JMenu; 42 import javax.swing.JMenuBar; 43 import javax.swing.JMenuItem; 44 import javax.swing.WindowConstants; 45 import javax.swing.border.Border; 46 import jtge.engine.net.IClosable; 47 48 /*** 49 * Provides the application frame. 50 * 51 * @author Jean-Laurent 52 * @version $Id: View.java,v 1.2 2005/09/10 14:06:08 mat007 Exp $ 53 */ 54 public class View extends JFrame 55 { 56 private static final long serialVersionUID = 1678907335026689223L; 57 private static final String FRAME_NAME = "Battle Hex"; 58 private static final int WIDTH = 800; 59 private static final int HEIGHT = 600; 60 61 /*** 62 * Create a view. 63 * 64 * @param controller the controller 65 * @param content the content 66 * @param closable the close callback 67 */ 68 public View( final IController controller, final JComponent content, final IClosable closable ) 69 { 70 super( FRAME_NAME ); 71 configureClose( closable ); 72 setJMenuBar( createMenuBar( controller ) ); 73 setSize( WIDTH, HEIGHT ); 74 setLocationRelativeTo( null ); 75 getContentPane().add( content ); 76 } 77 78 private void configureClose( final IClosable closable ) 79 { 80 addWindowListener( new WindowAdapter() 81 { 82 public final void windowClosing( final WindowEvent e ) 83 { 84 if( closable != null ) 85 closable.close(); 86 hide(); 87 dispose(); 88 } 89 } ); 90 setDefaultCloseOperation( WindowConstants.DO_NOTHING_ON_CLOSE ); 91 } 92 93 private JMenuBar createMenuBar( final IController controller ) 94 { 95 final JMenuBar result = new JMenuBar(); 96 result.add( createFileMenu( controller ) ); 97 result.add( createBuyMenu( controller ) ); 98 final Border border = BorderFactory.createLineBorder( Color.BLACK ); 99 result.setBorder( border ); 100 return result; 101 } 102 103 private JMenu createBuyMenu( final IController controller ) 104 { 105 final JMenu result = new JMenu( "Buy" ); 106 final JMenuItem fileMenuItem = new JMenuItem( "Castle" ); 107 fileMenuItem.addActionListener( new ActionListener() 108 { 109 public final void actionPerformed( final ActionEvent event ) 110 { 111 controller.place(); 112 } 113 } ); 114 result.add( fileMenuItem ); 115 return result; 116 } 117 118 private JMenu createFileMenu( final IController controller ) 119 { 120 final JMenu result = new JMenu( "File" ); 121 result.add( createFileNewMenuItem( controller ) ); 122 result.addSeparator(); 123 result.add( createFileExitMenuItem() ); 124 return result; 125 } 126 127 private JComponent createFileNewMenuItem( final IController controller ) 128 { 129 final JMenuItem fileMenuItem = new JMenuItem( "New" ); 130 fileMenuItem.addActionListener( new ActionListener() 131 { 132 public final void actionPerformed( final ActionEvent event ) 133 { 134 controller.newGame(); 135 } 136 } ); 137 return fileMenuItem; 138 } 139 140 private JComponent createFileExitMenuItem() 141 { 142 final JMenuItem fileMenuItem = new JMenuItem( "Exit" ); 143 fileMenuItem.addActionListener( new ActionListener() 144 { 145 public final void actionPerformed( final ActionEvent event ) 146 { 147 dispose(); 148 } 149 } ); 150 return fileMenuItem; 151 } 152 }