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</code> 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 }
|
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_EVENT_SYSTEM_SLEEP:
75 return true;
76 case APP_EVENT_USER_SESSION:
77 return isEventUserSessionSupported;
78 default:
79 return false;
80 }
81 }
82
83 @Override
84 public void open(File file) throws IOException {
85 this.ShellExecute(file, ACTION_OPEN_VERB);
86 }
87
88 @Override
89 public void edit(File file) throws IOException {
90 this.ShellExecute(file, ACTION_EDIT_VERB);
91 }
92
93 @Override
94 public void print(File file) throws IOException {
95 this.ShellExecute(file, ACTION_PRINT_VERB);
96 }
97
98 @Override
99 public void mail(URI uri) throws IOException {
100 this.ShellExecute(uri, ACTION_OPEN_VERB);
106 }
107
108 private void ShellExecute(File file, String verb) throws IOException {
109 String errMsg = ShellExecute(file.getAbsolutePath(), verb);
110 if (errMsg != null) {
111 throw new IOException("Failed to " + verb + " " + file + ". Error message: " + errMsg);
112 }
113 }
114
115 private void ShellExecute(URI uri, String verb) throws IOException {
116 String errmsg = ShellExecute(uri.toString(), verb);
117
118 if (errmsg != null) {
119 throw new IOException("Failed to " + verb + " " + uri
120 + ". Error message: " + errmsg);
121 }
122 }
123
124 private static native String ShellExecute(String fileOrUri, String verb);
125
126 private static final EventListenerList listenerList = new EventListenerList();
127
128 @Override
129 public void addAppEventListener(final SystemEventListener listener) {
130 if (listener instanceof UserSessionListener) {
131 listenerList.add(UserSessionListener.class, (UserSessionListener) listener);
132 }
133 if (listener instanceof SystemSleepListener) {
134 listenerList.add(SystemSleepListener.class, (SystemSleepListener) listener);
135 }
136 }
137
138 @Override
139 public void removeAppEventListener(final SystemEventListener listener) {
140 if (listener instanceof UserSessionListener) {
141 listenerList.remove(UserSessionListener.class, (UserSessionListener) listener);
142 }
143 if (listener instanceof SystemSleepListener) {
144 listenerList.remove(SystemSleepListener.class, (SystemSleepListener) listener);
145 }
146 }
147
148 private static void userSessionCallback(boolean activated) {
149 UserSessionListener[] listeners = listenerList.getListeners(UserSessionListener.class);
150 for (UserSessionListener use : listeners) {
151 EventQueue.invokeLater(() -> {
152 if (activated) {
153 use.userSessionActivated(new AppEvent.UserSessionEvent());
154 } else {
155 use.userSessionDeactivated(new AppEvent.UserSessionEvent());
156 }
157 });
158 }
159 }
160
161 private static void systemSleepCallback(boolean resumed) {
162 SystemSleepListener[] listeners = listenerList.getListeners(SystemSleepListener.class);
163 for (SystemSleepListener ssl : listeners) {
164 EventQueue.invokeLater(() -> {
165 if (resumed) {
166 ssl.systemAwoke(new AppEvent.SystemSleepEvent());
167 } else {
168 ssl.systemAboutToSleep(new AppEvent.SystemSleepEvent());
169 }
170 });
171 }
172 }
173
174 @Override
175 public boolean moveToTrash(File file) {
176 return moveToTrash(file.getAbsolutePath());
177 }
178 private static native boolean moveToTrash(String file);
179
180 }
|