1 19 20 package org.apache.excalibur.instrument.client; 21 22 import java.awt.BorderLayout ; 23 import java.awt.Dimension ; 24 import java.awt.FlowLayout ; 25 import java.awt.Point ; 26 import java.awt.event.ActionEvent ; 27 28 import javax.swing.AbstractAction ; 29 import javax.swing.Action ; 30 import javax.swing.Box ; 31 import javax.swing.JButton ; 32 import javax.swing.JDialog ; 33 import javax.swing.JFrame ; 34 import javax.swing.JLabel ; 35 import javax.swing.JPanel ; 36 import javax.swing.SwingConstants ; 37 import javax.swing.border.CompoundBorder ; 38 import javax.swing.border.EmptyBorder ; 39 import javax.swing.border.EtchedBorder ; 40 41 47 public abstract class AbstractOptionDialog 48 extends JDialog 49 { 50 public static final int BUTTON_OK = 1; 51 public static final int BUTTON_CANCEL = 2; 52 53 protected int m_action = BUTTON_CANCEL; 54 55 58 65 protected AbstractOptionDialog( JFrame frame, String title, int buttons ) 66 { 67 super( frame, title, true ); 68 69 JPanel contentPane = (JPanel )getContentPane(); 70 contentPane.setLayout( new BorderLayout () ); 71 contentPane.setBorder( new EmptyBorder ( 5, 5, 5, 5 ) ); 72 73 JPanel backPane = new JPanel (); 74 backPane.setLayout( new BorderLayout () ); 75 backPane.setBorder( 76 new CompoundBorder ( 77 new EmptyBorder ( 0, 0, 5, 0 ), 78 new CompoundBorder ( 79 new EtchedBorder ( EtchedBorder.LOWERED ), 80 new EmptyBorder ( 5, 5, 5, 5 ) 81 ) 82 ) 83 ); 84 contentPane.add( backPane, BorderLayout.CENTER ); 85 86 backPane.add( new JLabel ( getMessage(), SwingConstants.LEFT ), BorderLayout.NORTH ); 88 89 JPanel mainPanel = getMainPanel(); 91 mainPanel.setBorder( new EmptyBorder ( 5, 0, 0, 0 ) ); 92 backPane.add( mainPanel, BorderLayout.CENTER ); 93 94 95 JPanel buttonPanel = new JPanel (); 97 buttonPanel.setLayout( new FlowLayout ( FlowLayout.CENTER ) ); 98 Box buttonBox = Box.createHorizontalBox(); 99 if ( ( buttons & BUTTON_OK ) != 0 ) 100 { 101 Action action = new AbstractAction ( "OK" ) 102 { 103 public void actionPerformed( ActionEvent event ) 104 { 105 if ( validateFields() ) 106 { 107 m_action = BUTTON_OK; 108 AbstractOptionDialog.this.hide(); 109 } 110 } 111 }; 112 JButton button = new JButton ( action ); 113 buttonBox.add( button ); 114 buttonBox.add( Box.createHorizontalStrut( 5 ) ); 115 } 116 if ( ( buttons & BUTTON_CANCEL ) != 0 ) 117 { 118 Action action = new AbstractAction ( "Cancel" ) 119 { 120 public void actionPerformed( ActionEvent event ) 121 { 122 m_action = BUTTON_CANCEL; 123 AbstractOptionDialog.this.hide(); 124 } 125 }; 126 JButton button = new JButton ( action ); 127 buttonBox.add( button ); 128 buttonBox.add( Box.createHorizontalStrut( 5 ) ); 129 } 130 buttonPanel.add( buttonBox ); 131 contentPane.add( buttonPanel, BorderLayout.SOUTH ); 132 133 pack(); 134 135 Point frameLocation = frame.getLocation(); 137 Dimension frameSize = frame.getSize(); 138 Dimension size = getSize(); 139 140 setLocation( 141 (int)( frameLocation.getX() + (frameSize.getWidth() - size.getWidth() ) / 2 ), 142 (int)( frameLocation.getY() + (frameSize.getHeight() - size.getHeight() ) / 2 ) ); 143 144 setResizable( false ); 146 } 147 148 151 156 protected abstract String getMessage(); 157 158 163 protected abstract JPanel getMainPanel(); 164 165 170 protected boolean validateFields() 171 { 172 return true; 173 } 174 175 178 public int getAction() 179 { 180 return m_action; 181 } 182 } 183 184 | Popular Tags |