1 21 22 package org.opensubsystems.patterns.thickclient.application.swt; 23 24 import java.util.Date ; 25 import java.util.HashSet ; 26 import java.util.Iterator ; 27 import java.util.Set ; 28 29 import org.eclipse.swt.SWT; 30 import org.eclipse.swt.events.SelectionAdapter; 31 import org.eclipse.swt.events.SelectionEvent; 32 import org.eclipse.swt.events.SelectionListener; 33 import org.eclipse.swt.graphics.Point; 34 import org.eclipse.swt.layout.GridData; 35 import org.eclipse.swt.layout.GridLayout; 36 import org.eclipse.swt.widgets.Button; 37 import org.eclipse.swt.widgets.Canvas; 38 import org.eclipse.swt.widgets.Composite; 39 import org.eclipse.swt.widgets.Label; 40 import org.eclipse.swt.widgets.Text; 41 import org.opensubsystems.core.application.swt.ResourceManager; 42 import org.opensubsystems.core.util.GlobalConstants; 43 import org.opensubsystems.patterns.thickclient.application.EventProcessor; 44 import org.opensubsystems.patterns.thickclient.application.ModificationListener; 45 import org.opensubsystems.patterns.thickclient.application.TextProcessor; 46 47 55 public class CharacterPad extends Canvas 56 implements ModificationListener, 57 TextProcessor, 58 EventProcessor 59 { 60 62 65 protected static final int BASE_DIALOG_HEIGHT = 200; 66 67 70 public static final int CHP_NONE = 0; 71 72 75 public static final int CHP_UPPERCASE = 1; 76 77 80 public static final int CHP_HIDECAPSLOCK = 2; 81 82 85 public static final int CHP_SWITCHTOLOWERCASE = 4; 86 87 90 public static final CharacterPadLayout KEY_LAYOUT_123 91 = new CharacterPadLayout( 92 "1234567890", 93 10); 94 95 96 99 public static final CharacterPadLayout KEY_LAYOUT_123_QWERTY 100 = new CharacterPadLayout( 101 "1234567890" + 102 "QWERTYUIOP" + 103 "ASDFGHJKL " + 104 " ZXCVBNM ", 105 10); 106 107 110 public static final CharacterPadLayout KEY_LAYOUT_QWERTY 111 = new CharacterPadLayout( 112 "QWERTYUIOP" + 113 "ASDFGHJKL " + 114 " ZXCVBNM ", 115 10); 116 117 120 public static final CharacterPadLayout KEY_LAYOUT_123_QWERTY_SPECIAL 121 = new CharacterPadLayout( 122 "1234567890-" + 123 "QWERTYUIOP " + 124 "ASDFGHJKL;\"" + 125 " ZXCVBNM,./", 126 11); 127 128 131 public static final String [] ENTER_BUTTON = {"Enter"}; 132 133 136 public static final int CAPSLOCK_WIDTH = 2; 137 138 140 146 public static class CharacterPadLayout 147 { 148 150 153 protected String m_strKeyLayout; 154 155 158 protected int m_iWidth; 159 160 162 166 public CharacterPadLayout( 167 String strKeyLayout, 168 int width 169 ) 170 { 171 super(); 172 m_strKeyLayout = strKeyLayout; 173 m_iWidth = width; 174 } 175 176 178 181 public int getWidth() 182 { 183 return m_iWidth; 184 } 185 186 189 public String getKeyLayout() 190 { 191 return m_strKeyLayout; 192 } 193 } 194 195 197 200 protected Point m_preferredSize; 201 202 206 protected Set m_setListeners = new HashSet (); 207 208 211 protected Text m_text; 212 213 217 protected Date m_lastModified; 218 219 222 protected boolean m_bHideCapsLock = false; 223 224 227 protected boolean m_bToLowercase = false; 228 229 232 protected String m_initialText = null; 233 234 237 protected int m_specialFlags = CHP_NONE; 238 239 242 protected Button m_capsLock; 243 244 246 260 public CharacterPad( 261 Composite parent, 262 int style, 263 String [] specialButtons, 264 CharacterPadLayout keyboardLayout, 265 int specialFlags, 266 boolean bBigButtons 267 ) 268 { 269 super(parent, style); 270 271 m_specialFlags = specialFlags; 272 273 if ((m_specialFlags & CHP_HIDECAPSLOCK) == CHP_HIDECAPSLOCK) 274 { 275 m_bHideCapsLock = true; 276 } 277 278 if ((m_specialFlags & CHP_SWITCHTOLOWERCASE) == CHP_SWITCHTOLOWERCASE) 279 { 280 m_bToLowercase = true; 281 } 282 283 int iDialogLineHeight; 284 285 if (bBigButtons) 286 { 287 iDialogLineHeight = (int)(ResourceManager.BIG_BUTTON_FONT_HEIGHT * 1.8); 288 } 289 else 290 { 291 iDialogLineHeight = (int)(ResourceManager.MIDDLE_BUTTON_FONT_HEIGHT * 2.3); 292 } 293 294 int dialogHeigth = BASE_DIALOG_HEIGHT; 295 if ((specialButtons != null) && (specialButtons.length > 0)) 296 { 297 dialogHeigth += iDialogLineHeight; 298 } 299 if ((keyboardLayout != null) && (keyboardLayout.getKeyLayout().length() > 0)) 300 { 301 dialogHeigth += iDialogLineHeight 303 * (((keyboardLayout.getKeyLayout().length() 304 / keyboardLayout.getWidth()) + 1)); 305 } 306 m_preferredSize = new Point(keyboardLayout.getWidth() * iDialogLineHeight * 2, 307 dialogHeigth); 308 309 GridLayout layout = new GridLayout(); 310 layout.numColumns = keyboardLayout.getWidth(); 311 layout.marginHeight = 0; 312 layout.marginWidth = 0; 313 layout.makeColumnsEqualWidth = true; 314 315 setLayout(layout); 316 317 GridData textData; 318 319 m_text = new Text(this, SWT.BORDER); 320 ResourceManager.getInstance().setBigTextFont(m_text); 321 322 textData = new GridData(); 323 textData.horizontalSpan = keyboardLayout.getWidth(); 324 textData.horizontalAlignment = GridData.FILL; 325 textData.verticalAlignment = GridData.FILL; 326 textData.grabExcessHorizontalSpace = true; 328 m_text.setLayoutData(textData); 329 330 Label emptySpace; 331 GridData emptySpaceData; 332 String specialChars = keyboardLayout.getKeyLayout(); 333 334 if ((specialChars != null) && (specialChars.length() > 0)) 335 { 336 String actualChar; 337 338 for (int index = 0; index < specialChars.length(); index++) 339 { 340 actualChar = specialChars.substring(index, index + 1); 341 342 if (actualChar.equals("&")) 343 { 344 createButton("&&", new TextModificationSelectionAdapter(this, 345 this, "&", m_text), 346 bBigButtons); 347 } 348 else if (actualChar.equals(" ")) { 350 emptySpace = new Label(this, SWT.NONE); 351 emptySpace.setText(" "); 352 emptySpaceData = new GridData(); 353 emptySpaceData.horizontalSpan = 1; 354 emptySpaceData.horizontalAlignment = GridData.FILL; 355 emptySpaceData.verticalAlignment = GridData.FILL; 356 emptySpaceData.grabExcessVerticalSpace = true; 357 emptySpace.setLayoutData(emptySpaceData); 358 } 359 else 360 { 361 createButton(specialChars.substring(index, index + 1), 362 new TextModificationSelectionAdapter(this, this, 363 actualChar, m_text), 364 bBigButtons); 365 } 366 } 367 368 emptySpace = new Label(this, SWT.NONE); 369 emptySpace.setText(" "); 370 emptySpaceData = new GridData(); 371 emptySpaceData.horizontalSpan = keyboardLayout.getWidth() 372 - (specialChars.length() % keyboardLayout.getWidth()); 373 emptySpaceData.horizontalAlignment = GridData.FILL; 374 emptySpaceData.verticalAlignment = GridData.FILL; 375 emptySpaceData.grabExcessVerticalSpace = true; 376 emptySpace.setLayoutData(emptySpaceData); 377 378 } 380 381 int iAllocatedSpace; 383 int iSpaceWidth; 384 boolean spaceBarSeparators; 385 386 iAllocatedSpace = 1 + 1; if (!m_bHideCapsLock) 389 { 390 iAllocatedSpace += CAPSLOCK_WIDTH; 391 } 392 if ((keyboardLayout.getWidth() - iAllocatedSpace) >= 5) 393 { 394 spaceBarSeparators = true; 395 iSpaceWidth = keyboardLayout.getWidth() - iAllocatedSpace - 2; 396 } 397 else 398 { 399 spaceBarSeparators = false; 400 iSpaceWidth = keyboardLayout.getWidth() - iAllocatedSpace; 401 } 402 403 GridData capsLockData; 404 405 if (!m_bHideCapsLock) 406 { 407 m_capsLock = new Button(this, SWT.BORDER | SWT.TOGGLE); 408 m_capsLock.setText("Caps lock"); 409 m_capsLock.setAlignment(SWT.CENTER); 410 if ((m_specialFlags & CHP_UPPERCASE) == CHP_UPPERCASE) 411 { 412 m_capsLock.setSelection(true); 413 m_bHideCapsLock = true; 414 } 415 416 if (bBigButtons) 417 { 418 ResourceManager.getInstance().setBigButtonFont(m_capsLock); 419 } 420 else 421 { 422 ResourceManager.getInstance().setMiddleButtonFont(m_capsLock); 423 } 424 capsLockData = new GridData(); 425 capsLockData.horizontalSpan = CAPSLOCK_WIDTH; 426 capsLockData.horizontalAlignment = GridData.FILL; 427 capsLockData.verticalAlignment = GridData.FILL; 428 capsLockData.grabExcessVerticalSpace = true; 429 m_capsLock.setLayoutData(capsLockData); 430 m_capsLock.addSelectionListener(new SelectionAdapter() 431 { 432 public void widgetSelected(SelectionEvent event) 433 { 434 if (m_bHideCapsLock) 435 { 436 m_bHideCapsLock = false; 437 } 438 else 439 { 440 m_bHideCapsLock = true; 441 } 442 m_text.setFocus(); 443 } 444 445 public void widgetDefaultSelected(SelectionEvent event) 446 { 447 widgetSelected(event); 449 } 450 }); 451 } 452 453 Button back; 454 GridData backData; 455 456 back = new Button(this, SWT.BORDER); 457 back.setText("C"); 458 if (bBigButtons) 459 { 460 ResourceManager.getInstance().setBigButtonFont(back); 461 } 462 else 463 { 464 ResourceManager.getInstance().setMiddleButtonFont(back); 465 } 466 backData = new GridData(); 467 backData.horizontalSpan = 1; 468 backData.horizontalAlignment = GridData.FILL; 469 backData.verticalAlignment = GridData.FILL; 470 backData.grabExcessVerticalSpace = true; 471 back.setLayoutData(backData); 472 back.addSelectionListener( 473 new TextModificationSelectionAdapter(this, this, 474 TextModificationSelectionAdapter.FUNCTION_CLEAR, m_text)); 475 476 if (spaceBarSeparators) 477 { 478 emptySpace = new Label(this, SWT.NONE); 479 emptySpace.setText(" "); 480 emptySpaceData = new GridData(); 481 emptySpaceData.horizontalSpan = 1; 482 emptySpaceData.horizontalAlignment = GridData.FILL; 483 emptySpaceData.verticalAlignment = GridData.FILL; 484 emptySpaceData.grabExcessVerticalSpace = true; 485 emptySpace.setLayoutData(emptySpaceData); 486 } 487 488 Button space; 489 GridData spaceData; 490 491 space = new Button(this, SWT.BORDER); 492 space.setText("Spacebar"); 493 if (bBigButtons) 494 { 495 ResourceManager.getInstance().setBigButtonFont(space); 496 } 497 else 498 { 499 ResourceManager.getInstance().setMiddleButtonFont(space); 500 } 501 spaceData = new GridData(); 502 spaceData.horizontalSpan = iSpaceWidth; 503 spaceData.horizontalAlignment = GridData.FILL; 504 spaceData.verticalAlignment = GridData.FILL; 505 spaceData.grabExcessVerticalSpace = true; 506 space.setLayoutData(spaceData); 507 space.addSelectionListener(new TextModificationSelectionAdapter(this, 508 this, " ", m_text)); 509 510 if (spaceBarSeparators) 511 { 512 emptySpace = new Label(this, SWT.NONE); 513 emptySpace.setText(" "); 514 emptySpaceData = new GridData(); 515 emptySpaceData.horizontalSpan = 1; 516 emptySpaceData.horizontalAlignment = GridData.FILL; 517 emptySpaceData.verticalAlignment = GridData.FILL; 518 emptySpaceData.grabExcessVerticalSpace = true; 519 emptySpace.setLayoutData(emptySpaceData); 520 } 521 522 Button clear; 523 GridData clearData; 524 525 clear = new Button(this, SWT.BORDER); 526 clear.setText("CA"); 527 if (bBigButtons) 528 { 529 ResourceManager.getInstance().setBigButtonFont(clear); 530 } 531 else 532 { 533 ResourceManager.getInstance().setMiddleButtonFont(clear); 534 } 535 clearData = new GridData(); 536 clearData.horizontalSpan = 1; 537 clearData.horizontalAlignment = GridData.FILL; 538 clearData.verticalAlignment = GridData.FILL; 539 clearData.grabExcessVerticalSpace = true; 540 clear.setLayoutData(clearData); 541 clear.addSelectionListener(new TextModificationSelectionAdapter(this, this, 542 TextModificationSelectionAdapter.FUNCTION_CLEAR_ALL, 543 m_text)); 544 545 Button specialButton; 546 GridData specialButtonData; 547 548 if (specialButtons != null && specialButtons.length > 0) 549 { 550 int iEmptySpace = keyboardLayout.getWidth() % specialButtons.length; 551 552 for (int index = 0; index < specialButtons.length; index++) 553 { 554 specialButton = new Button(this, SWT.BORDER); 555 specialButton.setText(specialButtons[index]); 556 specialButton.setAlignment(SWT.CENTER); 557 if (bBigButtons) 558 { 559 ResourceManager.getInstance().setBigButtonFont(specialButton); 560 } 561 else 562 { 563 ResourceManager.getInstance().setMiddleButtonFont(specialButton); 564 } 565 specialButtonData = new GridData(); 566 specialButtonData.horizontalSpan = keyboardLayout.getWidth() 567 / specialButtons.length; 568 specialButtonData.horizontalAlignment = GridData.FILL; 569 specialButtonData.verticalAlignment = GridData.FILL; 570 specialButtonData.grabExcessVerticalSpace = true; 571 specialButton.setLayoutData(specialButtonData); 572 specialButton.addSelectionListener( 573 new EventNotifierSelectionAdapter(this, specialButtons[index], 574 new Integer (index))); 575 576 if (iEmptySpace > 0) { 578 iEmptySpace--; 579 emptySpace = new Label(this, SWT.NONE); 580 emptySpace.setText(" "); 581 emptySpaceData = new GridData(); 582 emptySpaceData.horizontalSpan = 1; 583 emptySpaceData.horizontalAlignment = GridData.FILL; 584 emptySpaceData.verticalAlignment = GridData.FILL; 585 emptySpaceData.grabExcessVerticalSpace = true; 586 emptySpace.setLayoutData(emptySpaceData); 587 } 588 } 589 } 590 591 if (m_initialText != null) 592 { 593 m_text.setText(m_initialText); 594 } 595 m_text.setFocus(); 596 } 597 598 600 605 public Point getPreferredSize() 606 { 607 return m_preferredSize; 608 } 609 610 613 public void modified( 614 ) 615 { 616 m_lastModified = new Date (); 618 } 619 620 623 public String processText( 624 String text 625 ) 626 { 627 if (m_bHideCapsLock) 628 { 629 text = text.toUpperCase(); 630 } 631 else 632 { 633 text = text.toLowerCase(); 634 } 635 if (m_bToLowercase) 636 { 637 m_bHideCapsLock = false; 639 m_bToLowercase = false; 640 m_capsLock.setSelection(false); 641 } 642 return text; 643 } 644 645 651 public boolean setTextLimit( 652 int iLimit 653 ) 654 { 655 boolean bReturn = false; 656 657 if (m_text != null) 658 { 659 m_text.setTextLimit(iLimit); 660 bReturn = true; 661 } 662 663 return bReturn; 664 } 665 666 669 public void processEvent( 670 String strEventName, 671 Integer intEventId 672 ) 673 { 674 notifyCharacterPadListeners(intEventId.intValue(), strEventName); 675 modified(); 676 } 677 678 683 public void addCharacterPadListener( 684 CharacterPadListener listener 685 ) 686 { 687 m_setListeners.add(listener); 688 } 689 690 695 public void removeCharacterPadListener( 696 CharacterPadListener listener 697 ) 698 { 699 m_setListeners.remove(listener); 700 } 701 702 707 public String getText() 708 { 709 if (m_text != null) 710 { 711 return m_text.getText(); 712 } 713 return null; 714 } 715 716 721 public void setInitialText( 722 String initialText 723 ) 724 { 725 m_initialText = initialText; 726 if (initialText != null) 727 { 728 if (GlobalConstants.ERROR_CHECKING) 729 { 730 assert m_text != null : "text control not created"; 731 } 732 733 m_text.setText(initialText); 734 m_text.setSelection(initialText.length(), initialText.length()); 735 m_text.setFocus(); 736 } 737 } 738 739 741 749 protected void createButton( 750 String strText, 751 SelectionListener listener, 752 boolean bBigButtons 753 ) 754 { 755 Button letter; 756 GridData letterData; 757 758 letter = new Button(this, SWT.BORDER); 759 letter.setText(strText); 760 if (bBigButtons) 761 { 762 ResourceManager.getInstance().setBigButtonFont(letter); 763 } 764 else 765 { 766 ResourceManager.getInstance().setMiddleButtonFont(letter); 767 } 768 letterData = new GridData(); 769 letterData.horizontalAlignment = GridData.FILL; 770 letterData.verticalAlignment = GridData.FILL; 771 letterData.grabExcessVerticalSpace = true; 773 letter.setLayoutData(letterData); 774 letter.addSelectionListener(listener); 775 } 776 777 778 785 protected boolean notifyCharacterPadListeners( 786 int intValue, 787 String strEvent 788 ) 789 { 790 boolean bRetval = false; 791 if ((strEvent == null) || (strEvent.length() == 0)) 792 { 793 strEvent = ""; 794 } 795 for (Iterator items = m_setListeners.iterator(); items.hasNext();) 796 { 797 bRetval = ((CharacterPadListener)items.next()).takeAction( 798 this, intValue, strEvent, getText()) || bRetval; 799 } 800 return bRetval; 801 } 802 } 803 | Popular Tags |