PageRenderTime 9ms CodeModel.GetById 6ms app.highlight 0ms RepoModel.GetById 1ms app.codeStats 1ms

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