1 /* 2 * Copyright (c) 2005, 2014, 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.awt.windows; 27 28 29 import java.awt.AppEvent; 30 import java.awt.Desktop.Action; 31 import java.awt.EventQueue; 32 import java.awt.desktop.SystemEventListener; 33 import java.awt.desktop.SystemSleepListener; 34 import java.awt.desktop.UserSessionListener; 35 import java.awt.peer.DesktopPeer; 36 import java.io.File; 37 import java.io.IOException; 38 import java.net.URI; 39 import javax.swing.event.EventListenerList; 40 import sun.awt.OSInfo; 41 42 43 /** 44 * Concrete implementation of the interface <code>DesktopPeer</code> for 45 * the Windows platform. 46 * 47 * @see DesktopPeer 48 */ 49 final class WDesktopPeer implements DesktopPeer { 50 /* Contants for the operation verbs */ 51 private static String ACTION_OPEN_VERB = "open"; 52 private static String ACTION_EDIT_VERB = "edit"; 53 private static String ACTION_PRINT_VERB = "print"; 54 55 private static boolean isEventUserSessionSupported = false; 56 57 private static native void init(); 58 59 WDesktopPeer() { 60 init(); 61 isEventUserSessionSupported = OSInfo.getWindowsVersion() 62 .compareTo(OSInfo.WINDOWS_VISTA) >= 0; 63 } 64 65 @Override 66 public boolean isSupported(Action action) { 67 switch(action) { 68 case OPEN: 69 case EDIT: 70 case PRINT: 71 case MAIL: 72 case BROWSE: 73 case MOVE_TO_TRASH: 74 case APP_SUDDEN_TERMINATION: 75 case APP_EVENT_SYSTEM_SLEEP: 76 return true; 77 case APP_EVENT_USER_SESSION: 78 return isEventUserSessionSupported; 79 default: 80 return false; 81 } 82 } 83 84 @Override 85 public void open(File file) throws IOException { 86 this.ShellExecute(file, ACTION_OPEN_VERB); 87 } 88 89 @Override 90 public void edit(File file) throws IOException { 91 this.ShellExecute(file, ACTION_EDIT_VERB); 92 } 93 94 @Override 95 public void print(File file) throws IOException { 96 this.ShellExecute(file, ACTION_PRINT_VERB); 97 } 98 99 @Override 100 public void mail(URI uri) throws IOException { 101 this.ShellExecute(uri, ACTION_OPEN_VERB); 102 } 103 104 @Override 105 public void browse(URI uri) throws IOException { 106 this.ShellExecute(uri, ACTION_OPEN_VERB); 107 } 108 109 private void ShellExecute(File file, String verb) throws IOException { 110 String errMsg = ShellExecute(file.getAbsolutePath(), verb); 111 if (errMsg != null) { 112 throw new IOException("Failed to " + verb + " " + file + ". Error message: " + errMsg); 113 } 114 } 115 116 private void ShellExecute(URI uri, String verb) throws IOException { 117 String errmsg = ShellExecute(uri.toString(), verb); 118 119 if (errmsg != null) { 120 throw new IOException("Failed to " + verb + " " + uri 121 + ". Error message: " + errmsg); 122 } 123 } 124 125 private static native String ShellExecute(String fileOrUri, String verb); 126 127 private static final EventListenerList listenerList = new EventListenerList(); 128 129 @Override 130 public void disableSuddenTermination() { 131 setSuddenTerminationEnabled(false); 132 } 133 134 @Override 135 public void enableSuddenTermination() { 136 setSuddenTerminationEnabled(true); 137 } 138 139 private static native void setSuddenTerminationEnabled(boolean enable); 140 141 @Override 142 public void addAppEventListener(final SystemEventListener listener) { 143 if (listener instanceof UserSessionListener) { 144 listenerList.add(UserSessionListener.class, (UserSessionListener) listener); 145 } 146 if (listener instanceof SystemSleepListener) { 147 listenerList.add(SystemSleepListener.class, (SystemSleepListener) listener); 148 } 149 } 150 151 @Override 152 public void removeAppEventListener(final SystemEventListener listener) { 153 if (listener instanceof UserSessionListener) { 154 listenerList.remove(UserSessionListener.class, (UserSessionListener) listener); 155 } 156 if (listener instanceof SystemSleepListener) { 157 listenerList.remove(SystemSleepListener.class, (SystemSleepListener) listener); 158 } 159 } 160 161 private static void userSessionCallback(boolean activated) { 162 UserSessionListener[] listeners = listenerList.getListeners(UserSessionListener.class); 163 for (UserSessionListener use : listeners) { 164 EventQueue.invokeLater(() -> { 165 if (activated) { 166 use.userSessionActivated(new AppEvent.UserSessionEvent()); 167 } else { 168 use.userSessionDeactivated(new AppEvent.UserSessionEvent()); 169 } 170 }); 171 } 172 } 173 174 private static void systemSleepCallback(boolean resumed) { 175 SystemSleepListener[] listeners = listenerList.getListeners(SystemSleepListener.class); 176 for (SystemSleepListener ssl : listeners) { 177 EventQueue.invokeLater(() -> { 178 if (resumed) { 179 ssl.systemAwoke(new AppEvent.SystemSleepEvent()); 180 } else { 181 ssl.systemAboutToSleep(new AppEvent.SystemSleepEvent()); 182 } 183 }); 184 } 185 } 186 187 @Override 188 public boolean moveToTrash(File file) { 189 return moveToTrash(file.getAbsolutePath()); 190 } 191 private static native boolean moveToTrash(String file); 192 193 }