1 7 package org.openharmonise.him.configuration; 8 9 import java.awt.BorderLayout ; 10 import java.awt.Color ; 11 import java.awt.Component ; 12 import java.awt.Container ; 13 import java.awt.Dialog ; 14 import java.awt.Dimension ; 15 import java.awt.Font ; 16 import java.awt.Frame ; 17 import java.awt.GraphicsConfiguration ; 18 import java.awt.HeadlessException ; 19 import java.awt.LayoutManager ; 20 import java.awt.event.ActionEvent ; 21 import java.awt.event.ActionListener ; 22 23 import javax.swing.JButton ; 24 import javax.swing.JDialog ; 25 import javax.swing.JFrame ; 26 import javax.swing.JLabel ; 27 import javax.swing.JPanel ; 28 import javax.swing.JTextField ; 29 import javax.swing.JComboBox ; 30 31 38 public class EmailDialog extends JDialog implements LayoutManager , ActionListener { 39 40 private JTextField m_emailTextField; 41 private JPanel m_panel; 42 private JButton m_okButton; 43 private JButton m_cancelButton; 44 private JComboBox m_comboBox; 45 private JLabel m_labelEmail; 46 private JLabel m_labelType; 47 public final String TYPE_ALL = "All"; 48 public final String TYPE_CONTENTS = "Content"; 49 public final String TYPE_METADATA = "Metadata"; 50 private String m_sEmailAddress = null; 51 55 public EmailDialog(Frame owner) throws HeadlessException { 56 super(owner, true); 57 setup(); 58 } 59 private void setup(){ 60 this.setTitle("Export content"); 61 this.setSize(550,100); 62 this.setResizable(false); 63 64 m_panel = new JPanel (); 65 m_panel.setSize(600,80); 66 getContentPane().add(m_panel); 67 m_panel.setBackground(new Color (224,224,224)); 68 getContentPane().setLayout(new BorderLayout ()); 69 m_panel.setLayout(this); 70 71 Font font = new Font ("Dialog", Font.PLAIN, 11); 72 73 m_emailTextField = new JTextField (30); 74 m_emailTextField.setFont(font); 75 76 m_panel.add(m_emailTextField); 77 78 int x = this.getGraphicsConfiguration().getBounds().width/2-this.getSize().width/2; 79 int y = this.getGraphicsConfiguration().getBounds().height/2-this.getSize().height/2; 80 81 this.setLocation(x, y); 82 83 m_okButton = new JButton ("OK"); 84 m_okButton.setActionCommand("OK"); 85 m_okButton.setFont(font); 86 m_okButton.addActionListener(this); 87 88 m_cancelButton = new JButton ("Cancel"); 89 m_cancelButton.setActionCommand("Cancel"); 90 m_cancelButton.setFont(font); 91 m_cancelButton.addActionListener(this); 92 93 m_panel.add(m_okButton); 95 m_panel.add(m_cancelButton); 96 97 m_comboBox = new JComboBox (); 98 m_comboBox.setFont(font); 99 m_comboBox.addItem(TYPE_ALL); 100 m_comboBox.addItem(TYPE_CONTENTS); 101 m_comboBox.addItem(TYPE_METADATA); 102 103 m_panel.add(m_comboBox); 104 105 m_labelEmail = new JLabel ("Email address"); 106 m_labelEmail.setFont(font); 107 108 m_panel.add(m_labelEmail); 109 110 m_labelType = new JLabel ("Export"); 111 m_labelType.setFont(font); 112 113 m_panel.add(m_labelType); 114 } 115 118 public void layoutContainer(Container parent) { 119 int x = 10; 120 int yLabel = 10; 121 int yField = 35; 122 123 m_labelEmail.setSize(200, 20); 124 m_labelEmail.setLocation(x, yLabel); 125 126 m_emailTextField.setSize(250,20); 127 m_emailTextField.setLocation(x, yField); 128 x += m_emailTextField.getWidth() + 20; 129 130 m_labelType.setSize(90,20); 131 m_labelType.setLocation(x, yLabel); 132 133 m_comboBox.setSize(90,20); 134 m_comboBox.setLocation(x, yField); 135 x += m_comboBox.getWidth() + 20; 136 137 m_okButton.setSize(50,20); 138 m_okButton.setLocation(x, yField); 139 x += m_okButton.getWidth() + 20; 140 141 m_cancelButton.setSize(70,20); 142 m_cancelButton.setLocation(x, yField); 143 } 144 public String getEmail() { 145 return m_sEmailAddress; 146 } 147 public String getType() { 148 return (String )m_comboBox.getSelectedItem(); 149 } 150 153 public void removeLayoutComponent(Component comp) { 154 156 } 157 158 161 public void addLayoutComponent(String name, Component comp) { 162 164 } 165 166 169 public Dimension minimumLayoutSize(Container parent) { 170 return null; 172 } 173 174 177 public Dimension preferredLayoutSize(Container parent) { 178 return null; 180 } 181 public static void main(String [] args){ 182 JFrame frame = new JFrame (); 183 frame.setTitle("Export Content"); 184 frame.setVisible(true); 185 EmailDialog email = new EmailDialog(frame); 186 email.show(); 187 188 System.exit(0); 189 } 190 193 public void actionPerformed(ActionEvent e) { 194 if(e.getActionCommand().equals("OK")){ 195 if(isValidEmail()){ 196 m_sEmailAddress = m_emailTextField.getText(); 197 this.getParent().setVisible(false); 198 this.hide(); 199 } else { 200 m_labelEmail.setForeground(Color.RED); 201 m_labelEmail.setText("Please enter a valid email address"); 202 } 203 } else { 204 this.getParent().setVisible(false); 205 this.hide(); 206 } 207 } 208 211 private boolean isValidEmail() { 212 String sEmail = m_emailTextField.getText(); 213 int indexAt = sEmail.indexOf("@"); 214 int indexPt = sEmail.lastIndexOf("."); 215 if(indexAt > 1 && indexPt > indexAt){ 216 return true; 217 } else { 218 return false; 219 } 220 } 221 } 222 | Popular Tags |