1 /*
   2  * Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package com.apple.jobjc;
  26 
  27 import com.apple.jobjc.Utils.Strings;
  28 import com.apple.jobjc.appkit.NSApplication;
  29 import com.apple.jobjc.appkit.NSButton;
  30 import com.apple.jobjc.appkit.NSDrawer;
  31 import com.apple.jobjc.appkit.NSMenu;
  32 import com.apple.jobjc.appkit.NSWindow;
  33 import com.apple.jobjc.foundation.NSAutoreleasePool;
  34 import com.apple.jobjc.foundation.NSObject;
  35 import com.apple.jobjc.foundation.NSObjectClass;
  36 import com.apple.jobjc.foundation.NSRect;
  37 import com.apple.jobjc.foundation.NSSize;
  38 
  39 class MyDelegate extends NSObject{
  40     static final JObjC objc = JObjC.getInstance();
  41     static final Strings str = Utils.get().strings();
  42 
  43     public MyDelegate(long ptr, JObjCRuntime r) { super(ptr, r); }
  44 
  45     private NSWindow myWindow;
  46     private NSDrawer myDrawer;
  47 
  48     public void printHello(ID sender){
  49         System.out.println("Hello!");
  50         myDrawer.toggle(this);
  51     }
  52 
  53     public void createMenu(){
  54         NSMenu menu = objc.AppKit().NSMenu().newID();
  55         menu.addItemWithTitle_action_keyEquivalent(
  56                 str.nsString("Quit"),
  57                 new SEL("terminate:"),
  58                 str.nsString("q"));
  59         NSApplication app = objc.AppKit().NSApp();
  60         app.setMainMenu(menu);
  61     }
  62 
  63     public void createWindow(){
  64         NSRect rect;
  65         int styleMask = objc.AppKit().NSTitledWindowMask() | objc.AppKit().NSMiniaturizableWindowMask();
  66         NSButton myButton;
  67         NSSize buttonSize;
  68         myButton = objc.AppKit().NSButton().newID();
  69         myButton.setTitle(str.nsString("Print Hello!"));
  70         myButton.sizeToFit();
  71         myButton.setTarget(this);
  72         myButton.setAction(new SEL("printHello:"));
  73         buttonSize = myButton.frame().size();
  74         rect = objc.Foundation().NSMakeRect(100, 100, 2*buttonSize.width(), 2*buttonSize.height());
  75         myWindow = objc.AppKit().NSWindow().alloc();
  76         myWindow = myWindow.initWithContentRect_styleMask_backing_defer(
  77                 rect, styleMask, objc.AppKit().NSBackingStoreBuffered(), false);
  78         myWindow.setTitle(str.nsString("This is a test window."));
  79         myWindow.setContentView(myButton);
  80 
  81         myDrawer = objc.AppKit().NSDrawer().alloc();
  82         myDrawer = myDrawer.initWithContentSize_preferredEdge(
  83                   objc.Foundation().NSMakeSize(100, 40), objc.Foundation().NSMinYEdge());
  84         myDrawer.setParentWindow(myWindow);
  85     }
  86 
  87     public void applicationWillFinishLaunching(ID not){
  88         createMenu();
  89         createWindow();
  90     }
  91 
  92     public void applicationDidFinishLaunching(ID not){
  93         myWindow.makeKeyAndOrderFront(null);
  94     }
  95 }
  96 
  97 class MyDelegateClass extends NSObjectClass{
  98     public MyDelegateClass(JObjCRuntime r){ super(r); }
  99 }
 100 
 101 public class GUIDemo{
 102     static final JObjC objc = JObjC.getInstance();
 103 
 104     public static void main(String[] args){
 105         JObjCRuntime.getInstance().registerUserClass(MyDelegate.class, MyDelegateClass.class);
 106 
 107         objc.AppKit().NSApplication().sharedApplication();
 108         NSApplication app = objc.AppKit().NSApp();
 109 
 110         NSAutoreleasePool pool = objc.Foundation().NSAutoreleasePool().alloc();
 111         pool = pool.init();
 112         app.setDelegate(new MyDelegateClass(JObjCRuntime.getInstance()).newID());
 113         app.run();
 114         pool.drain();
 115     }
 116 }