/Tests/Manual/CPAlertTest/AppController.j

http://github.com/cacaodev/cappuccino · Unknown · 152 lines · 120 code · 32 blank · 0 comment · 0 complexity · 2951f0f4b61d507b1723c993a0de5b5e MD5 · raw file

  1. /*
  2. * AppController.j
  3. * CPAlertTest
  4. *
  5. * Created by Alexander Ljungberg on August 19, 2010.
  6. * Copyright 2010, WireLoad, LLC All rights reserved.
  7. */
  8. @import <Foundation/CPObject.j>
  9. @implementation AppController : CPObject
  10. {
  11. CPWindow theWindow;
  12. CPTextField label;
  13. CPArray variations;
  14. CPArray messages;
  15. int messageIndex;
  16. BOOL useBlocks;
  17. }
  18. - (void)_init
  19. {
  20. messages = [
  21. [@"Are you sure you want to theorise before you have data?",
  22. @"Invariably, you end up twisting facts to suit theories, instead of theories to suit facts.",
  23. "Theorise", "Cancel"],
  24. [@"Snakes. Why did it have to be snakes?",
  25. nil,
  26. "Torch"],
  27. [@"Sometimes a message can be really long and just appear to go on and on. It could be a speech. It could be the television.",
  28. nil]
  29. ];
  30. messageIndex = 0;
  31. variations = [
  32. [nil, CPWarningAlertStyle],
  33. [nil, CPInformationalAlertStyle],
  34. [nil, CPCriticalAlertStyle],
  35. [CPHUDBackgroundWindowMask, CPWarningAlertStyle],
  36. [CPHUDBackgroundWindowMask, CPInformationalAlertStyle],
  37. [CPHUDBackgroundWindowMask, CPCriticalAlertStyle],
  38. [CPDocModalWindowMask, CPWarningAlertStyle],
  39. [CPDocModalWindowMask, CPInformationalAlertStyle],
  40. [CPDocModalWindowMask, CPCriticalAlertStyle]
  41. ];
  42. }
  43. - (void)applicationDidFinishLaunching:(CPNotification)aNotification
  44. {
  45. [self _init];
  46. theWindow = [[CPWindow alloc] initWithContentRect:CGRectMake(100, 100, 500, 500) styleMask:CPTitledWindowMask];
  47. [theWindow setTitle:@"CPAlert Test"];
  48. var contentView = [theWindow contentView];
  49. label = [[CPTextField alloc] initWithFrame:CGRectMake(15, 15, 400, 24)];
  50. [label setStringValue:"Respond to the alert dialog with the mouse or the keyboard."];
  51. [contentView addSubview:label];
  52. var button = [CPButton buttonWithTitle:@"Start again using didEnd blocks"];
  53. [button setTarget:self];
  54. [button setAction:@selector(testWithBlocks:)];
  55. [button setCenter:[contentView center]];
  56. [contentView addSubview:button];
  57. [theWindow orderFront:self];
  58. [self showNextAlertVariation];
  59. // Uncomment the following line to turn on the standard menu bar.
  60. //[CPMenu setMenuBarVisible:YES];
  61. }
  62. - (@action)testWithBlocks:(id)sender
  63. {
  64. useBlocks = YES;
  65. [self _init];
  66. [self showNextAlertVariation];
  67. }
  68. - (void)alertDidEnd:(CPAlert)anAlert returnCode:(CPInteger)returnCode
  69. {
  70. CPLog.info("%s alert = %s, code = %d", _cmd, [anAlert description], returnCode);
  71. if (returnCode === 0)
  72. [label setStringValue:"You chose the default action."];
  73. else
  74. [label setStringValue:"You cancelled the dialog."];
  75. [self showNextAlertVariation];
  76. }
  77. - (void)customDidEnd:(CPAlert)anAlert code:(id)code context:(id)context
  78. {
  79. CPLog.info("%s alert = %s, code = %d, context = %s", _cmd, [anAlert description], code, context);
  80. }
  81. - (void)showNextAlertVariation
  82. {
  83. if (![variations count])
  84. return;
  85. var variation = variations[0],
  86. message = messages[messageIndex],
  87. alert = [CPAlert new];
  88. messageIndex = (messageIndex + 1) % messages.length;
  89. [variations removeObjectAtIndex:0];
  90. var windowStyle = variation[0];
  91. [alert setDelegate:self];
  92. [alert setMessageText:message[0] || @""];
  93. [alert setInformativeText:message[1] || @""];
  94. if (message.length > 2)
  95. [alert addButtonWithTitle:message[2]];
  96. if (message.length > 3)
  97. [alert addButtonWithTitle:message[3]];
  98. [alert setTheme:(windowStyle === CPHUDBackgroundWindowMask) ? [CPTheme defaultHudTheme] : [CPTheme defaultTheme]];
  99. [alert setAlertStyle:variation[1]];
  100. if (windowStyle & CPDocModalWindowMask)
  101. {
  102. if (useBlocks)
  103. [alert beginSheetModalForWindow:theWindow didEndBlock:function(alert, returnCode)
  104. {
  105. CPLog.info("didEndBlock: alert = %s, code = %d", [alert description], returnCode);
  106. [self showNextAlertVariation];
  107. }];
  108. else
  109. [alert beginSheetModalForWindow:theWindow modalDelegate:self didEndSelector:@selector(customDidEnd:code:context:) contextInfo:@"here is some context"];
  110. }
  111. else if (useBlocks)
  112. [alert runModalWithDidEndBlock:function(alert, returnCode)
  113. {
  114. CPLog.info("didEndBlock: alert = %s, code = %d", [alert description], returnCode);
  115. [self showNextAlertVariation];
  116. }];
  117. else
  118. [alert runModal];
  119. }
  120. @end