## page was renamed from Cappuccino = Cappuccino = Cappuccino is a framework which makes it easy to create advanced web apps. [[http://www.cappuccino-project.org/]] Objective-J is a powerful object-oriented language which compiles to run in the browser. API [[http://www.cappuccino-project.org/learn/documentation/]] == Get the code == Clone it using git: {{{#!highlight sh cd ~ mkdir gitCappuccino cd gitCappuccino git clone git://github.com/cappuccino/cappuccino.git cd ~/gitCappuccino/cappuccino }}} == Vim editor Objective-J plugin == Run the following commands after fetching code from github: {{{#!highlight sh mkdir -p ~/.vim/plugin cd ~/gitCappuccino/cappuccino/Tools/Editors/Vim] cp objj.vim ~/.vim/plugin }}} == Emacs editor Objective-J plugin == {{{#!highlight sh mkdir ~/cappuccinoEmacs cd ~/gitCappuccino/cappuccino/Tools/Editors/Emacs cp *.el ~/cappuccinoEmacs/ touch ~/.emacs nano ~/.emacs #(add-to-list 'load-path "~/cappuccinoEmacs") #(require 'objj-mode) }}} == Init String, stringWithFormat, CPString == {{{#!highlight objectivej var str1 = [CPString initWithString: @""]; var str2 = [CPString initWithString: @""]; var str3 = [CPString stringWithFormat:"StrX %s %s %s" , [obj field1] , [obj field2] , [obj field3] ]; var strLen [str3 length]; }}} == Mutable Array == {{{#!highlight objectivej @import var arrayX = [[CPMutableArray alloc] init]; }}} == Callbacks == {{{#!highlight objectivej @implementation TestObject : CPObject { } -(void)helloWorld{ CPLogConsole("Called hello world"); } //(...) @end //-------------------- var selectorHelloWorld = @selector(helloWorld) ; var signature = [self methodSignatureForSelector: aSelector]; var invocation = [CPInvocation invocationWithMethodSignature:signature]; [invocation setSelector: selectorHelloWorld]; [invocation invokeWithTarget: self]; //self -> instance of TestObject }}} {{{#!highlight objectivej @implementation TestObject : CPObject { } -(void)helloWorldMsg:(CPString) message p1:(int)intVal { CPLogConsole( message ); CPLogConsole( intVal ); } //(...) @end //-------------------- var selectorHelloWorldMsg = @selector(helloWorldMsg:p1:) ; var sig = [self methodSignatureForSelector: selectorHelloWorldMsg]; var invocation = [CPInvocation invocationWithMethodSignature:sig]; [invocation setSelector: selectorHelloWorldMsg]; [invocation setArgument:@"Message" atIndex:2 ]; //0-> self 1->_cmd [invocation setArgument: 1234 atIndex:3 ]; //0-> self 1->_cmd [invocation invokeWithTarget: self]; }}} == Dates == {{{#!highlight objectivej var datex = [CPDate dateWithTimeIntervalSince1970: dateInSeconds]; //set CPDate with seconds since 01-01-1970 UTC var strDate = [instanceCPDate description];// get string with date in format YYYY-MM-DD HH:MM:SS TZ±HHMM. }}} == Dictionary from JSON object == {{{#!highlight objectivej - (void)connection:(CPURLConnection) connection didReceiveData:(CPString)data { var dict = [CPDictionary dictionaryWithJSObject: [data objectFromJSON] ]; [textField setStringValue: [dict valueForKey:@"lkeyx"] ]; } - (void)connection:(CPURLConnection)connection didFailWithError:(CPString)error { } }}} When a number is defined on a JSON object, a CPNumber is returned in valueForKey. {{{#!highlight javascript // JSON for aData { "subDocument": { "dateValue":12345 } } }}} {{{#!highlight objectivej var otherDict = [CPDictionary dictionaryWithJSObject: [aData objectFromJSON] ]; var dictx = [CPDictionary dictionaryWithJSObject: [otherdict valueForKey:@"subDocument"] ]; var dateValue = [ [dictx valueForKey:@"dateValue"] doubleValue]; }}} == Load JSON array == JSON array {{{#!highlight javascript [ {"paramx":"aaaa","valuex":"aaaaaddd"} , {"paramx":"asassassa","valuex":"ggghfghf"} ] }}} Acme class {{{#!highlight objectivej @implementation Acme : CPObject { CPString paramx @accessors; CPString valuex @accessors; } @end }}} Receive data from web service {{{#!highlight objectivej -(void) connection:(CPURLConnection) connection didReceiveData:(CPString)aData { var jsonArray = [aData objectFromJSON]; _tableData = [[CPMutableArray alloc] init]; for(var idx=0;idx< jsonArray.length;idx++){ var objx = jsonArray[idx]; var dictx = [CPDictionary dictionaryWithJSObject: objx ]; var acme = [Acme alloc]; [acme setParamx: [dictx valueForKey:@"paramx"] ]; [acme setValuex: [dictx valueForKey:@"valuex"] ]; [_tableData addObject: acme]; } [_tableView reloadData]; } }}} == URL request == {{{#!highlight objectivej var url = [CPString stringWithFormat:"/JEE-war/rest/getInfo/%@", [textField stringValue] ] ; var request = [CPURLRequest requestWithURL: url ]; var connection = [CPURLConnection connectionWithRequest:request delegate:self]; //calls didFailWithError and didReceiveData }}} == Mime types Glassfish + JBoss == Adapted from [[https://github.com/cappuccino/cappuccino/wiki/Server-Side-Issues]] You need to have the mime type of file types it doesn’t understand set to something for Cappuccino to work. Specifically, you should set the following mime-type/extension pairs. This can be done in the '''web.xml''': {{{#!highlight xml j text/javascript plist text/xml sj text/javascript }}} == Download file == [[http://ique.github.io/2009/11/cptableview-uploading-and-downloading-in-cappuccino/]] {{{#!highlight objectivej DownloadIFrame = document.createElement("iframe"); DownloadIFrame.style.position = "absolute"; DownloadIFrame.style.top = "-100px"; DownloadIFrame.style.left = "-100px"; DownloadIFrame.style.height = "0px"; DownloadIFrame.style.width = "0px"; document.body.appendChild(DownloadIFrame); //.... DownloadIFrame.src = "http://localhost:3000/uploads/fileXpto"; }}} == Index of string == [[https://developer.apple.com/library/ios/documentation/cocoa/reference/foundation/Miscellaneous/Foundation_DataTypes/Reference/reference.html|Apple Foundation data types]] {{{#!highlight objectivej var range = [data rangeOfString:@"str xyz"]; CPLogConsole( JSON.stringify(range) ); // CPrange has fields location and length // If location >=0 the string has been found }}} == TableView == {{{#!highlight objectivej //allow multiple selections , CPTableView (NSTableView) [tableviewx setAllowsMultipleSelection:YES]; }}} == Timer == {{{#!highlight objectivej @import CPTimer _timer; // timer each 5 seconds _timer = [CPTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(timerHandler:) userInfo:nil repeats:YES]; -(void)timerHandler:(id)sender { [self updateData]; if( [self isVisible]==false ){ // terminates the timer [_timer invalidate]; } } }}} == Button == {{{#!highlight objectivej // create button CPButton cpbutton = [CPButton alloc]; [cpbutton initWithFrame:CGRectMakeZero()]; [cpbutton setFrameOrigin:CGPointMake(10,10)]; [cpbutton setFrameSize:CGSizeMake(100,25)]; [cpbutton setTitle: @"Buttonx"]; [cpbutton setTarget: self]; [cpbutton setAction: @selector(cpbuttonClicked:)]; [aView addSubview: cpbutton]; // clicked handler -(void)cpbuttonClicked:(id)sender { //(...) } //enable button [cpbutton setEnabled:YES]; //disable button [cpbutton setEnabled:NO]; }}} == Data transfer object == {{{#!highlight objectivej @implementation SampleDTO : CPObject { CPString date @accessors; CPString text @accessors; CPString origin @accessors; CPString destination @accessors; } @end }}} To get data in the fields: {{{#!highlight objectivej [obj date]; [obj text]; [obj origin]; [obj destination]; }}} To set data in the fields: {{{#!highlight objectivej [obj setDate @"datex"]; [obj setText @"textx"]; [obj setOrigin @"originx"]; [obj setDestination @"destinationx"]; }}} == Starter == * wget http://www.cappuccino-project.org/downloads/CappuccinoStarter-0.9.7-1.zip