1 22 package org.objectweb.petals.demo.mortgage.gui.gui; 23 24 import java.awt.BorderLayout ; 25 import java.awt.Dimension ; 26 import java.awt.Font ; 27 import java.awt.Rectangle ; 28 import java.awt.event.ActionEvent ; 29 import java.awt.event.ActionListener ; 30 import java.io.DataInputStream ; 31 import java.io.File ; 32 import java.io.FileInputStream ; 33 import java.io.InputStream ; 34 35 import javax.swing.ImageIcon ; 36 import javax.swing.JButton ; 37 import javax.swing.JDesktopPane ; 38 import javax.swing.JFrame ; 39 import javax.swing.JLabel ; 40 import javax.swing.JOptionPane ; 41 import javax.swing.JTextArea ; 42 import javax.swing.JTextField ; 43 import javax.swing.JTextPane ; 44 45 import org.objectweb.petals.demo.mortgage.gui.MortgageClient; 46 47 52 public class Console extends javax.swing.JFrame { 53 private static final long serialVersionUID = -8117896325113377153L; 54 55 private JTextArea address; 56 57 private JTextPane addressTxt; 58 59 private JTextField carPayment; 60 61 private JTextPane carPaymentTxt; 62 63 private JTextField creditCards; 64 65 private JTextPane creditCardsTxt; 66 67 private JTextPane custTxt; 68 69 private JTextPane expensesTxt; 70 71 private JTextField firstName; 72 73 private JTextPane firstNameTxt; 74 75 private JTextField insurance; 76 77 private JTextPane insuranceTxt; 78 79 private JDesktopPane jDesktopPane; 80 81 private JTextField lastName; 82 83 private JTextPane lastNameTxt; 84 85 private JTextPane monthlyIncomeTxt; 86 87 private MortgageClient mortgageClient; 88 89 private JTextPane otherExpensesTxt; 90 91 private JTextField otherPayments; 92 93 private JTextPane otherPaymentsTxt; 94 95 private JTextField propertyTaxes; 96 97 private JButton reset; 98 99 private JTextArea result; 100 101 private JTextPane resultTxt; 102 103 private JTextField salary; 104 105 private JTextPane salaryTxt; 106 107 private JButton submit; 108 109 private JTextPane taxesTxt; 110 111 public Console() { 112 super(); 113 initialize(); 114 } 115 116 public Console(MortgageClient sampleClient) { 117 this(); 118 this.mortgageClient = sampleClient; 119 } 120 121 public static void main(String [] args) { 122 Console inst = new Console(); 123 inst.setVisible(true); 124 inst.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 125 } 126 127 public void setResponse(String msg) { 128 result.setText(msg); 129 } 130 131 135 protected void resetForm() { 136 salary.setText("0"); 137 lastName.setText(""); 138 firstName.setText(""); 139 address.setText(""); 140 carPayment.setText("0"); 141 insurance.setText("0"); 142 creditCards.setText("0"); 143 propertyTaxes.setText("0"); 144 otherPayments.setText("0"); 145 } 146 147 151 protected void submit() { 152 String firstName = this.firstName.getText(); 154 String lastName = this.lastName.getText(); 155 String address = this.address.getText(); 156 String salary = this.salary.getText(); 157 String taxes = this.propertyTaxes.getText(); 158 String insurance = this.insurance.getText(); 159 String auto = this.carPayment.getText(); 160 String creditCards = this.creditCards.getText(); 161 String otherPayments = this.otherPayments.getText(); 162 163 String error = ""; 164 try { 165 Float.parseFloat(salary); 166 if (Float.parseFloat(salary) <= 0) { 167 error += "The salary must be positive! "; 168 } 169 } catch (NumberFormatException e) { 170 error += "The salary must be a number! "; 171 } 172 try { 173 Float.parseFloat(taxes); 174 } catch (NumberFormatException e) { 175 error += "The property taxes must be a number! "; 176 } 177 try { 178 Float.parseFloat(insurance); 179 } catch (NumberFormatException e) { 180 error += "The insurance must be a number! "; 181 } 182 try { 183 Float.parseFloat(auto); 184 } catch (NumberFormatException e) { 185 error += "The car payments must be a number! "; 186 } 187 try { 188 Float.parseFloat(creditCards); 189 } catch (NumberFormatException e) { 190 error += "The credit cards payments must be a number! "; 191 } 192 try { 193 Float.parseFloat(otherPayments); 194 } catch (NumberFormatException e) { 195 error += "The other payments must be a number! "; 196 } 197 198 if (!"".equals(error)) { 199 JOptionPane.showMessageDialog(this, error, "Error", 200 JOptionPane.ERROR_MESSAGE); 201 } else { 202 if (mortgageClient != null) { 203 mortgageClient.submit(firstName, lastName, address, salary, 204 taxes, insurance, auto, creditCards, otherPayments); 205 } else { 206 JOptionPane.showMessageDialog(this, 207 "The component is not deployed in a JBI Container", 208 "Error", JOptionPane.ERROR_MESSAGE); 209 } 210 } 211 } 212 213 218 private JTextArea getAddress() { 219 if (address == null) { 220 address = new JTextArea (2, 20); 221 address.setWrapStyleWord(true); 222 address.setLineWrap(true); 223 address.setBounds(new Rectangle (150, 72, 150, 36)); 224 address.setWrapStyleWord(true); 225 address.setBorder(new JTextField ().getBorder()); 226 } 227 return address; 228 } 229 230 235 private JTextPane getAddressTxt() { 236 if (addressTxt == null) { 237 addressTxt = new JTextPane (); 238 addressTxt.setBounds(new Rectangle (10, 70, 83, 22)); 239 addressTxt.setEditable(false); 240 addressTxt.setText("Address:"); 241 addressTxt.setOpaque(false); 242 } 243 return addressTxt; 244 } 245 246 251 private JTextField getCarPayment() { 252 if (carPayment == null) { 253 carPayment = new JTextField (); 254 carPayment.setText("0"); 255 carPayment.setBounds(new Rectangle (150, 262, 100, 18)); 256 } 257 return carPayment; 258 } 259 260 265 private JTextPane getCarPaymentTxt() { 266 if (carPaymentTxt == null) { 267 carPaymentTxt = new JTextPane (); 268 carPaymentTxt.setBounds(new Rectangle (8, 260, 94, 22)); 269 carPaymentTxt.setEditable(false); 270 carPaymentTxt.setText("Car payment:"); 271 carPaymentTxt.setOpaque(false); 272 } 273 return carPaymentTxt; 274 } 275 276 281 private JTextField getCreditCards() { 282 if (creditCards == null) { 283 creditCards = new JTextField (); 284 creditCards.setText("0"); 285 creditCards.setBounds(new Rectangle (150, 282, 100, 18)); 286 } 287 return creditCards; 288 } 289 290 295 private JTextPane getCreditCardsTxt() { 296 if (creditCardsTxt == null) { 297 creditCardsTxt = new JTextPane (); 298 creditCardsTxt.setBounds(new Rectangle (8, 280, 90, 22)); 299 creditCardsTxt.setEditable(false); 300 creditCardsTxt.setText("Credit cards:"); 301 creditCardsTxt.setOpaque(false); 302 } 303 return creditCardsTxt; 304 } 305 306 311 private JTextPane getCustTxt() { 312 if (custTxt == null) { 313 custTxt = new JTextPane (); 314 custTxt.setBounds(new Rectangle (8, 2, 185, 24)); 315 custTxt.setFont(new Font ("Dialog", Font.BOLD, 14)); 316 custTxt.setEditable(false); 317 custTxt.setFont(new Font ("Dialog", Font.BOLD, 14)); 318 custTxt.setText("Customer Information:"); 319 custTxt.setOpaque(false); 320 } 321 return custTxt; 322 } 323 324 329 private JTextPane getExpensesTxt() { 330 if (expensesTxt == null) { 331 expensesTxt = new JTextPane (); 332 expensesTxt.setBounds(new Rectangle (8, 160, 203, 24)); 333 expensesTxt.setText("Monthly housing expense:"); 334 expensesTxt.setFont(new Font ("Dialog", Font.BOLD, 14)); 335 expensesTxt.setOpaque(false); 336 } 337 return expensesTxt; 338 } 339 340 345 private JTextField getFirstName() { 346 if (firstName == null) { 347 firstName = new JTextField (); 348 firstName.setBounds(new Rectangle (150, 52, 150, 18)); 349 } 350 return firstName; 351 } 352 353 358 private JTextPane getFirstNameTxt() { 359 if (firstNameTxt == null) { 360 firstNameTxt = new JTextPane (); 361 firstNameTxt.setBounds(new Rectangle (10, 50, 95, 22)); 362 firstNameTxt.setEditable(false); 363 firstNameTxt.setText("First name:"); 364 firstNameTxt.setOpaque(false); 365 } 366 return firstNameTxt; 367 } 368 369 374 private JTextField getInsurance() { 375 if (insurance == null) { 376 insurance = new JTextField (); 377 insurance.setText("0"); 378 insurance.setBounds(new Rectangle (150, 212, 100, 18)); 379 } 380 return insurance; 381 } 382 383 388 private JTextPane getInsuranceTxt() { 389 if (insuranceTxt == null) { 390 insuranceTxt = new JTextPane (); 391 insuranceTxt.setBounds(new Rectangle (8, 211, 118, 22)); 392 insuranceTxt.setEditable(false); 393 insuranceTxt.setText("Hazard insurance:"); 394 insuranceTxt.setOpaque(false); 395 } 396 return insuranceTxt; 397 } 398 399 404 private final JDesktopPane getJDesktopPane() { 405 if (jDesktopPane == null) { 406 jDesktopPane = new JDesktopPane (); 407 jDesktopPane.add(getCustTxt(), null); 408 jDesktopPane.add(getLastNameTxt(), null); 409 jDesktopPane.add(getFirstNameTxt(), null); 410 jDesktopPane.add(getFirstName(), null); 411 jDesktopPane.add(getLastName(), null); 412 jDesktopPane.add(getAddressTxt(), null); 413 jDesktopPane.add(getAddress(), null); 414 jDesktopPane.add(getMonthlyIncomeTxt(), null); 415 jDesktopPane.add(getSalaryTxt(), null); 416 jDesktopPane.add(getSalary(), null); 417 jDesktopPane.add(getTaxesTxt(), null); 418 jDesktopPane.add(getPropertyTaxes(), null); 419 jDesktopPane.add(getExpensesTxt(), null); 420 jDesktopPane.add(getInsuranceTxt(), null); 421 jDesktopPane.add(getInsurance(), null); 422 jDesktopPane.add(getOtherExpensesTxt(), null); 423 jDesktopPane.add(getCarPaymentTxt(), null); 424 jDesktopPane.add(getCarPayment(), null); 425 jDesktopPane.add(getCreditCardsTxt(), null); 426 jDesktopPane.add(getCreditCards(), null); 427 jDesktopPane.add(getOtherPaymentsTxt(), null); 428 jDesktopPane.add(getOtherPayments(), null); 429 jDesktopPane.add(getSubmit(), null); 430 jDesktopPane.add(getReset(), null); 431 jDesktopPane.add(getResult(), null); 432 jDesktopPane.add(getResultTxt(), null); 433 434 byte[] buffer = null; 435 InputStream in = Console.class.getClassLoader() 436 .getResourceAsStream("Petals pwd by EBM.jpg"); 437 try { 438 if (in == null) { 439 in = new FileInputStream ("src" + File.separator + "img" 440 + File.separator + "Petals pwd by EBM.jpg"); 441 } 442 DataInputStream dis = new DataInputStream (in); 443 buffer = new byte[dis.available()]; 444 dis.readFully(buffer); 445 } catch (Exception e) { 446 JOptionPane.showMessageDialog(this, e.getMessage(), "Error", 447 JOptionPane.ERROR_MESSAGE); 448 } 449 ImageIcon img1 = new ImageIcon (buffer); 450 JLabel background1 = new JLabel (img1); 451 background1.setBounds(0, 0, img1.getIconWidth(), img1 452 .getIconHeight()); 453 jDesktopPane.add(background1, Integer.MIN_VALUE); 454 } 455 return jDesktopPane; 456 } 457 458 463 private JTextField getLastName() { 464 if (lastName == null) { 465 lastName = new JTextField (); 466 lastName.setBounds(new Rectangle (150, 32, 150, 18)); 467 } 468 return lastName; 469 } 470 471 476 private JTextPane getLastNameTxt() { 477 if (lastNameTxt == null) { 478 lastNameTxt = new JTextPane (); 479 lastNameTxt.setBounds(new Rectangle (10, 30, 84, 22)); 480 lastNameTxt.setEditable(false); 481 lastNameTxt.setText("Last name:"); 482 lastNameTxt.setOpaque(false); 483 } 484 return lastNameTxt; 485 } 486 487 492 private JTextPane getMonthlyIncomeTxt() { 493 if (monthlyIncomeTxt == null) { 494 monthlyIncomeTxt = new JTextPane (); 495 monthlyIncomeTxt.setBounds(new Rectangle (10, 110, 172, 22)); 496 monthlyIncomeTxt.setFont(new Font ("Dialog", Font.BOLD, 14)); 497 monthlyIncomeTxt.setText("Monthly Income:"); 498 monthlyIncomeTxt.setEditable(false); 499 monthlyIncomeTxt.setOpaque(false); 500 } 501 return monthlyIncomeTxt; 502 } 503 504 509 private JTextPane getOtherExpensesTxt() { 510 if (otherExpensesTxt == null) { 511 otherExpensesTxt = new JTextPane (); 512 otherExpensesTxt.setBounds(new Rectangle (8, 237, 194, 24)); 513 otherExpensesTxt.setText("Other monthly expenses:"); 514 otherExpensesTxt.setFont(new Font ("Dialog", Font.BOLD, 14)); 515 otherExpensesTxt.setEditable(false); 516 otherExpensesTxt.setOpaque(false); 517 } 518 return otherExpensesTxt; 519 } 520 521 526 private JTextField getOtherPayments() { 527 if (otherPayments == null) { 528 otherPayments = new JTextField (); 529 otherPayments.setText("0"); 530 otherPayments.setBounds(new Rectangle (150, 302, 100, 18)); 531 } 532 return otherPayments; 533 } 534 535 540 private JTextPane getOtherPaymentsTxt() { 541 if (otherPaymentsTxt == null) { 542 otherPaymentsTxt = new JTextPane (); 543 otherPaymentsTxt.setBounds(new Rectangle (8, 300, 110, 22)); 544 otherPaymentsTxt.setEditable(false); 545 otherPaymentsTxt.setText("Other payments:"); 546 otherPaymentsTxt.setOpaque(false); 547 } 548 return otherPaymentsTxt; 549 } 550 551 556 private JTextField getPropertyTaxes() { 557 if (propertyTaxes == null) { 558 propertyTaxes = new JTextField (); 559 propertyTaxes.setText("0"); 560 propertyTaxes.setBounds(new Rectangle (150, 192, 100, 18)); 561 } 562 return propertyTaxes; 563 } 564 565 570 private JButton getReset() { 571 if (reset == null) { 572 reset = new JButton (); 573 reset.setBounds(new Rectangle (210, 340, 120, 28)); 574 reset.setText("Reset"); 575 reset.addActionListener(new ActionListener () { 576 public void actionPerformed(ActionEvent evt) { 577 resetForm(); 578 } 579 }); 580 } 581 return reset; 582 } 583 584 589 private JTextArea getResult() { 590 if (result == null) { 591 result = new JTextArea (2, 20); 592 result.setWrapStyleWord(true); 593 result.setLineWrap(true); 594 result.setBounds(new Rectangle (10, 396, 290, 34)); 595 result.setEditable(false); 596 result.setOpaque(false); 597 result.setBorder(new JTextField ().getBorder()); 598 } 599 return result; 600 } 601 602 607 private JTextPane getResultTxt() { 608 if (resultTxt == null) { 609 resultTxt = new JTextPane (); 610 resultTxt.setBounds(new Rectangle (10, 370, 86, 24)); 611 resultTxt.setFont(new Font ("Dialog", Font.BOLD, 14)); 612 resultTxt.setText("Result:"); 613 resultTxt.setEditable(false); 614 resultTxt.setOpaque(false); 615 } 616 return resultTxt; 617 } 618 619 624 private JTextField getSalary() { 625 if (salary == null) { 626 salary = new JTextField (); 627 salary.setText("0"); 628 salary.setBounds(new Rectangle (150, 140, 100, 18)); 629 } 630 return salary; 631 } 632 633 638 private JTextPane getSalaryTxt() { 639 if (salaryTxt == null) { 640 salaryTxt = new JTextPane (); 641 salaryTxt.setBounds(new Rectangle (10, 140, 90, 22)); 642 salaryTxt.setEditable(false); 643 salaryTxt.setText("Salary:"); 644 salaryTxt.setOpaque(false); 645 } 646 return salaryTxt; 647 } 648 649 654 private JButton getSubmit() { 655 if (submit == null) { 656 submit = new JButton (); 657 submit.setBounds(new Rectangle (30, 340, 120, 26)); 658 submit.setText("Submit"); 659 submit.addActionListener(new ActionListener () { 660 public void actionPerformed(ActionEvent evt) { 661 submit(); 662 } 663 }); 664 } 665 return submit; 666 } 667 668 673 private JTextPane getTaxesTxt() { 674 if (taxesTxt == null) { 675 taxesTxt = new JTextPane (); 676 taxesTxt.setBounds(new Rectangle (8, 190, 94, 22)); 677 taxesTxt.setText("Property taxes:"); 678 taxesTxt.setOpaque(false); 679 taxesTxt.setEditable(false); 680 } 681 return taxesTxt; 682 } 683 684 private void initialize() { 685 this.setSize(new Dimension (410, 460)); 686 this.setTitle("Petals Mortgage Demonstration"); 687 this.getContentPane().add(getJDesktopPane(), BorderLayout.CENTER); 689 this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 690 this.setResizable(false); 691 } 692 693 } | Popular Tags |