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 
  26 package sun.lwawt.macosx;
  27 
  28 import com.apple.eawt.Application;
  29 import com.apple.eawt.FullScreenUtilities;
  30 import sun.awt.AWTAccessor;
  31 import sun.lwawt.LWWindowPeer;
  32 
  33 import javax.swing.*;
  34 import java.awt.*;
  35 import java.awt.Desktop.Action;
  36 import java.awt.desktop.*;
  37 import java.awt.peer.ComponentPeer;
  38 import java.awt.peer.DesktopPeer;
  39 import java.io.File;
  40 import java.io.FileNotFoundException;
  41 import java.io.IOException;
  42 import java.net.URI;
  43 
  44 
  45 /**
  46  * Concrete implementation of the interface <code>DesktopPeer</code> for MacOS X
  47  *
  48  * @see DesktopPeer
  49  */
  50 final public class CDesktopPeer implements DesktopPeer {
  51 
  52     @Override
  53     public boolean isSupported(Action action) {
  54         return true;
  55     }
  56 
  57     @Override
  58     public void open(File file) throws IOException {
  59         this.lsOpenFile(file, false);
  60     }
  61 
  62     @Override
  63     public void edit(File file) throws IOException {
  64         this.lsOpenFile(file, false);
  65     }
  66 
  67     @Override
  68     public void print(File file) throws IOException {
  69         this.lsOpenFile(file, true);
  70     }
  71 
  72     @Override
  73     public void mail(URI uri) throws IOException {
  74         this.lsOpen(uri);
  75     }
  76 
  77     @Override
  78     public void browse(URI uri) throws IOException {
  79         this.lsOpen(uri);
  80     }
  81 
  82     @Override
  83     public void addAppEventListener(SystemEventListener listener) {
  84         Application.getApplication().addAppEventListener(listener);
  85     }
  86 
  87     @Override
  88     public void removeAppEventListener(SystemEventListener listener) {
  89         Application.getApplication().removeAppEventListener(listener);
  90     }
  91 
  92     @Override
  93     public void setAboutHandler(AboutHandler aboutHandler) {
  94         Application.getApplication().setAboutHandler(aboutHandler);
  95     }
  96 
  97     @Override
  98     public void setPreferencesHandler(PreferencesHandler preferencesHandler) {
  99         Application.getApplication().setPreferencesHandler(preferencesHandler);
 100     }
 101 
 102     @Override
 103     public void setOpenFileHandler(OpenFilesHandler openFileHandler) {
 104         Application.getApplication().setOpenFileHandler(openFileHandler);
 105     }
 106 
 107     @Override
 108     public void setPrintFileHandler(PrintFilesHandler printFileHandler) {
 109         Application.getApplication().setPrintFileHandler(printFileHandler);
 110     }
 111 
 112     @Override
 113     public void setOpenURIHandler(OpenURIHandler openURIHandler) {
 114         Application.getApplication().setOpenURIHandler(openURIHandler);
 115     }
 116 
 117     @Override
 118     public void setQuitHandler(QuitHandler quitHandler) {
 119         Application.getApplication().setQuitHandler(quitHandler);
 120     }
 121 
 122     @Override
 123     public void setQuitStrategy(QuitStrategy strategy) {
 124         Application.getApplication().setQuitStrategy(strategy);
 125     }
 126 
 127     @Override
 128     public void enableSuddenTermination() {
 129         Application.getApplication().enableSuddenTermination();
 130     }
 131 
 132     @Override
 133     public void disableSuddenTermination() {
 134         Application.getApplication().disableSuddenTermination();
 135     }
 136 
 137     @Override
 138     public void requestForeground(boolean allWindows) {
 139         Application.getApplication().requestForeground(allWindows);
 140     }
 141 
 142     @Override
 143     public void openHelpViewer() {
 144         Application.getApplication().openHelpViewer();
 145     }
 146 
 147     @Override
 148     public void setDefaultMenuBar(JMenuBar menuBar) {
 149         Application.getApplication().setDefaultMenuBar(menuBar);
 150     }
 151 
 152     @Override
 153     public void addWindowFullScreenListener(final Window window,
 154             final FullScreenListener listener) {
 155         if (!(window instanceof JFrame) && !(window instanceof JDialog)) {
 156             throw new IllegalArgumentException("Can't mark a non-JFrame or a non-JDialog as full screen-able");
 157         }
 158         FullScreenUtilities.addFullScreenListenerTo(window, listener);
 159     }
 160 
 161     @Override
 162     public void removeWindowFullScreenListener(final Window window,
 163             final FullScreenListener listener) {
 164         if (!(window instanceof JFrame) && !(window instanceof JDialog)) {
 165             throw new IllegalArgumentException("Can't remove FullScreenListener from a non-JFrame or a non-JDialog");
 166         }
 167         FullScreenUtilities.removeFullScreenListenerFrom(window, listener);
 168     }
 169 
 170     @Override
 171     public void setWindowCanFullScreen(Window window, boolean canFullScreen) {
 172         if (!(window instanceof JFrame) && !(window instanceof JDialog)) {
 173             throw new IllegalArgumentException("Can't attach FullScreenListener to a non-JFrame or a non-JDialog");
 174         }
 175         FullScreenUtilities.setWindowCanFullScreen(window, canFullScreen);
 176     }
 177 
 178     @Override
 179     public void requestToggleFullScreen(Window window) {
 180         final ComponentPeer peer = AWTAccessor.getComponentAccessor().getPeer(window);
 181 
 182         if (!(peer instanceof LWWindowPeer)) {
 183             return;
 184         }
 185         Object platformWindow = ((LWWindowPeer) peer).getPlatformWindow();
 186         if (!(platformWindow instanceof CPlatformWindow)) {
 187             return;
 188         }
 189         ((CPlatformWindow) platformWindow).toggleFullScreen();
 190     }
 191 
 192     @Override
 193     public boolean browseFileDirectory(File file) {
 194         try {
 195             return com.apple.eio.FileManager.revealInFinder(file);
 196         } catch (FileNotFoundException ex) {
 197             return false; //handled in java.awt.Desktop
 198         }
 199     }
 200     
 201     @Override
 202     public boolean moveToTrash(File file) {
 203         try {
 204             return com.apple.eio.FileManager.moveToTrash(file);
 205         } catch (FileNotFoundException ex) {
 206             return false; //handled in java.awt.Desktop
 207         }
 208     }
 209 
 210     private void lsOpen(URI uri) throws IOException {
 211         int status = _lsOpenURI(uri.toString());
 212 
 213         if (status != 0 /* noErr */) {
 214             throw new IOException("Failed to mail or browse " + uri + ". Error code: " + status);
 215         }
 216     }
 217 
 218     private void lsOpenFile(File file, boolean print) throws IOException {
 219         int status = _lsOpenFile(file.getCanonicalPath(), print);
 220 
 221         if (status != 0 /* noErr */) {
 222             throw new IOException("Failed to open, edit or print " + file + ". Error code: " + status);
 223         }
 224     }
 225 
 226     private static native int _lsOpenURI(String uri);
 227 
 228     private static native int _lsOpenFile(String path, boolean print);
 229 
 230 }