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