1/*
2 * Copyright (c) 2011, 2016, 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
26package sun.lwawt.macosx;
27
28import com.apple.eawt.Application;
29
30import javax.swing.*;
31import java.awt.Desktop.Action;
32import java.awt.desktop.*;
33import java.awt.peer.DesktopPeer;
34import java.io.File;
35import java.io.FileNotFoundException;
36import java.io.IOException;
37import java.net.URI;
38
39
40/**
41 * Concrete implementation of the interface {@code DesktopPeer} for MacOS X
42 *
43 * @see DesktopPeer
44 */
45final public class CDesktopPeer implements DesktopPeer {
46
47    @Override
48    public boolean isSupported(Action action) {
49        return true;
50    }
51
52    @Override
53    public void open(File file) throws IOException {
54        this.lsOpenFile(file, false);
55    }
56
57    @Override
58    public void edit(File file) throws IOException {
59        this.lsOpenFile(file, false);
60    }
61
62    @Override
63    public void print(File file) throws IOException {
64        this.lsOpenFile(file, true);
65    }
66
67    @Override
68    public void mail(URI uri) throws IOException {
69        this.lsOpen(uri);
70    }
71
72    @Override
73    public void browse(URI uri) throws IOException {
74        this.lsOpen(uri);
75    }
76
77    @Override
78    public void addAppEventListener(SystemEventListener listener) {
79        Application.getApplication().addAppEventListener(listener);
80    }
81
82    @Override
83    public void removeAppEventListener(SystemEventListener listener) {
84        Application.getApplication().removeAppEventListener(listener);
85    }
86
87    @Override
88    public void setAboutHandler(AboutHandler aboutHandler) {
89        Application.getApplication().setAboutHandler(aboutHandler);
90    }
91
92    @Override
93    public void setPreferencesHandler(PreferencesHandler preferencesHandler) {
94        Application.getApplication().setPreferencesHandler(preferencesHandler);
95    }
96
97    @Override
98    public void setOpenFileHandler(OpenFilesHandler openFileHandler) {
99        Application.getApplication().setOpenFileHandler(openFileHandler);
100    }
101
102    @Override
103    public void setPrintFileHandler(PrintFilesHandler printFileHandler) {
104        Application.getApplication().setPrintFileHandler(printFileHandler);
105    }
106
107    @Override
108    public void setOpenURIHandler(OpenURIHandler openURIHandler) {
109        Application.getApplication().setOpenURIHandler(openURIHandler);
110    }
111
112    @Override
113    public void setQuitHandler(QuitHandler quitHandler) {
114        Application.getApplication().setQuitHandler(quitHandler);
115    }
116
117    @Override
118    public void setQuitStrategy(QuitStrategy strategy) {
119        Application.getApplication().setQuitStrategy(strategy);
120    }
121
122    @Override
123    public void enableSuddenTermination() {
124        Application.getApplication().enableSuddenTermination();
125    }
126
127    @Override
128    public void disableSuddenTermination() {
129        Application.getApplication().disableSuddenTermination();
130    }
131
132    @Override
133    public void requestForeground(boolean allWindows) {
134        Application.getApplication().requestForeground(allWindows);
135    }
136
137    @Override
138    public void openHelpViewer() {
139        Application.getApplication().openHelpViewer();
140    }
141
142    @Override
143    public void setDefaultMenuBar(JMenuBar menuBar) {
144        Application.getApplication().setDefaultMenuBar(menuBar);
145    }
146
147    @Override
148    public boolean browseFileDirectory(File file) {
149        try {
150            return com.apple.eio.FileManager.revealInFinder(file);
151        } catch (FileNotFoundException ex) {
152            return false; //handled in java.awt.Desktop
153        }
154    }
155
156    @Override
157    public boolean moveToTrash(File file) {
158        try {
159            return com.apple.eio.FileManager.moveToTrash(file);
160        } catch (FileNotFoundException ex) {
161            return false; //handled in java.awt.Desktop
162        }
163    }
164
165    private void lsOpen(URI uri) throws IOException {
166        int status = _lsOpenURI(uri.toString());
167
168        if (status != 0 /* noErr */) {
169            throw new IOException("Failed to mail or browse " + uri + ". Error code: " + status);
170        }
171    }
172
173    private void lsOpenFile(File file, boolean print) throws IOException {
174        int status = _lsOpenFile(file.getCanonicalPath(), print);
175
176        if (status != 0 /* noErr */) {
177            throw new IOException("Failed to open, edit or print " + file + ". Error code: " + status);
178        }
179    }
180
181    private static native int _lsOpenURI(String uri);
182
183    private static native int _lsOpenFile(String path, boolean print);
184
185}
186