< prev index next >

src/java.desktop/windows/classes/sun/awt/windows/WDesktopPeer.java

Print this page


   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.Desktop.Action;







  30 import java.awt.peer.DesktopPeer;
  31 import java.io.File;
  32 import java.io.IOException;
  33 import java.net.URI;


  34 
  35 
  36 /**
  37  * Concrete implementation of the interface {@code DesktopPeer} for
  38  * the Windows platform.
  39  *
  40  * @see DesktopPeer
  41  */
  42 final class WDesktopPeer implements DesktopPeer {
  43     /* Contants for the operation verbs */
  44     private static String ACTION_OPEN_VERB = "open";
  45     private static String ACTION_EDIT_VERB = "edit";
  46     private static String ACTION_PRINT_VERB = "print";
  47 










  48     @Override
  49     public boolean isSupported(Action action) {
  50         // OPEN, EDIT, PRINT, MAIL, BROWSE all supported on windows.








  51         return true;





  52     }
  53 
  54     @Override
  55     public void open(File file) throws IOException {
  56         this.ShellExecute(file, ACTION_OPEN_VERB);
  57     }
  58 
  59     @Override
  60     public void edit(File file) throws IOException {
  61         this.ShellExecute(file, ACTION_EDIT_VERB);
  62     }
  63 
  64     @Override
  65     public void print(File file) throws IOException {
  66         this.ShellExecute(file, ACTION_PRINT_VERB);
  67     }
  68 
  69     @Override
  70     public void mail(URI uri) throws IOException {
  71         this.ShellExecute(uri, ACTION_OPEN_VERB);


  77     }
  78 
  79     private void ShellExecute(File file, String verb) throws IOException {
  80         String errMsg = ShellExecute(file.getAbsolutePath(), verb);
  81         if (errMsg != null) {
  82             throw new IOException("Failed to " + verb + " " + file + ". Error message: " + errMsg);
  83         }
  84     }
  85 
  86     private void ShellExecute(URI uri, String verb) throws IOException {
  87         String errmsg = ShellExecute(uri.toString(), verb);
  88 
  89         if (errmsg != null) {
  90             throw new IOException("Failed to " + verb + " " + uri
  91                     + ". Error message: " + errmsg);
  92         }
  93     }
  94 
  95     private static native String ShellExecute(String fileOrUri, String verb);
  96 


































































  97 }
   1 /*
   2  * Copyright (c) 2005, 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 
  26 package sun.awt.windows;
  27 
  28 
  29 import java.awt.Desktop.Action;
  30 import java.awt.EventQueue;
  31 import java.awt.desktop.SystemEventListener;
  32 import java.awt.desktop.SystemSleepEvent;
  33 import java.awt.desktop.SystemSleepListener;
  34 import java.awt.desktop.UserSessionEvent;
  35 import java.awt.desktop.UserSessionEvent.Reason;
  36 import java.awt.desktop.UserSessionListener;
  37 import java.awt.peer.DesktopPeer;
  38 import java.io.File;
  39 import java.io.IOException;
  40 import java.net.URI;
  41 import javax.swing.event.EventListenerList;
  42 import sun.awt.OSInfo;
  43 
  44 
  45 /**
  46  * Concrete implementation of the interface {@code DesktopPeer} for
  47  * the Windows platform.
  48  *
  49  * @see DesktopPeer
  50  */
  51 final class WDesktopPeer implements DesktopPeer {
  52     /* Contants for the operation verbs */
  53     private static String ACTION_OPEN_VERB = "open";
  54     private static String ACTION_EDIT_VERB = "edit";
  55     private static String ACTION_PRINT_VERB = "print";
  56     
  57     private static boolean isEventUserSessionSupported = false;
  58     
  59     private static native void init();
  60 
  61     WDesktopPeer() {
  62         init();
  63         isEventUserSessionSupported = OSInfo.getWindowsVersion()
  64                                         .compareTo(OSInfo.WINDOWS_VISTA) >= 0;
  65     }
  66    
  67     @Override
  68     public boolean isSupported(Action action) {
  69         switch(action) {
  70             case OPEN:
  71             case EDIT:
  72             case PRINT:
  73             case MAIL:
  74             case BROWSE:
  75             case MOVE_TO_TRASH:
  76             case APP_SUDDEN_TERMINATION:
  77             case APP_EVENT_SYSTEM_SLEEP:
  78                 return true;
  79             case APP_EVENT_USER_SESSION:
  80                 return isEventUserSessionSupported;
  81             default:
  82                 return false;
  83         }
  84     }
  85 
  86     @Override
  87     public void open(File file) throws IOException {
  88         this.ShellExecute(file, ACTION_OPEN_VERB);
  89     }
  90 
  91     @Override
  92     public void edit(File file) throws IOException {
  93         this.ShellExecute(file, ACTION_EDIT_VERB);
  94     }
  95 
  96     @Override
  97     public void print(File file) throws IOException {
  98         this.ShellExecute(file, ACTION_PRINT_VERB);
  99     }
 100 
 101     @Override
 102     public void mail(URI uri) throws IOException {
 103         this.ShellExecute(uri, ACTION_OPEN_VERB);


 109     }
 110 
 111     private void ShellExecute(File file, String verb) throws IOException {
 112         String errMsg = ShellExecute(file.getAbsolutePath(), verb);
 113         if (errMsg != null) {
 114             throw new IOException("Failed to " + verb + " " + file + ". Error message: " + errMsg);
 115         }
 116     }
 117 
 118     private void ShellExecute(URI uri, String verb) throws IOException {
 119         String errmsg = ShellExecute(uri.toString(), verb);
 120 
 121         if (errmsg != null) {
 122             throw new IOException("Failed to " + verb + " " + uri
 123                     + ". Error message: " + errmsg);
 124         }
 125     }
 126 
 127     private static native String ShellExecute(String fileOrUri, String verb);
 128     
 129     private static final EventListenerList listenerList = new EventListenerList();
 130 
 131     @Override
 132     public void disableSuddenTermination() {
 133         setSuddenTerminationEnabled(false);
 134     }
 135 
 136     @Override
 137     public void enableSuddenTermination() {
 138         setSuddenTerminationEnabled(true);
 139     }
 140     
 141     private static native void setSuddenTerminationEnabled(boolean enable);
 142     
 143     @Override
 144     public void addAppEventListener(final SystemEventListener listener) {
 145         if (listener instanceof UserSessionListener) {
 146             listenerList.add(UserSessionListener.class, (UserSessionListener) listener);
 147         } 
 148         if (listener instanceof SystemSleepListener) {
 149             listenerList.add(SystemSleepListener.class, (SystemSleepListener) listener);
 150         }
 151     }
 152     
 153     @Override
 154     public void removeAppEventListener(final SystemEventListener listener) {
 155         if (listener instanceof UserSessionListener) {
 156             listenerList.remove(UserSessionListener.class, (UserSessionListener) listener);
 157         } 
 158         if (listener instanceof SystemSleepListener) {
 159             listenerList.remove(SystemSleepListener.class, (SystemSleepListener) listener);
 160         }
 161     }
 162     
 163     private static void userSessionCallback(boolean activated, Reason reason) {
 164             UserSessionListener[] listeners = listenerList.getListeners(UserSessionListener.class);
 165             for (UserSessionListener use : listeners) {
 166                 EventQueue.invokeLater(() -> {
 167                     if (activated) {
 168                         use.userSessionActivated(new UserSessionEvent(reason));
 169                     } else {
 170                         use.userSessionDeactivated(new UserSessionEvent(reason));
 171                     }
 172                 });
 173             }
 174     }
 175     
 176     private static void systemSleepCallback(boolean resumed) {
 177         SystemSleepListener[] listeners = listenerList.getListeners(SystemSleepListener.class);
 178         for (SystemSleepListener ssl : listeners) {
 179             EventQueue.invokeLater(() -> {
 180                 if (resumed) {
 181                     ssl.systemAwoke(new SystemSleepEvent());
 182                 } else {
 183                     ssl.systemAboutToSleep(new SystemSleepEvent());
 184                 }
 185             });
 186         }
 187     }
 188     
 189     @Override
 190     public boolean moveToTrash(File file) {
 191         return moveToTrash(file.getAbsolutePath());
 192     }
 193     private static native boolean moveToTrash(String file);
 194     
 195 }
< prev index next >