PageRenderTime 48ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/webswing-app-toolkit/src/main/java/org/webswing/toolkit/WebToolkit.java

https://bitbucket.org/meszarv/webswing
Java | 831 lines | 693 code | 124 blank | 14 comment | 53 complexity | 8a6b3d37efd3ecbdd0723a0b292da152 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. package org.webswing.toolkit;
  2. import java.applet.Applet;
  3. import java.awt.*;
  4. import java.awt.Dialog.ModalityType;
  5. import java.awt.datatransfer.Clipboard;
  6. import java.awt.dnd.DragGestureEvent;
  7. import java.awt.dnd.DragGestureListener;
  8. import java.awt.dnd.DragGestureRecognizer;
  9. import java.awt.dnd.DragSource;
  10. import java.awt.dnd.InvalidDnDOperationException;
  11. import java.awt.dnd.MouseDragGestureRecognizer;
  12. import java.awt.dnd.peer.DragSourceContextPeer;
  13. import java.awt.event.WindowEvent;
  14. import java.awt.font.TextAttribute;
  15. import java.awt.im.InputMethodHighlight;
  16. import java.awt.im.spi.InputMethodDescriptor;
  17. import java.awt.image.ColorModel;
  18. import java.awt.peer.ButtonPeer;
  19. import java.awt.peer.CheckboxMenuItemPeer;
  20. import java.awt.peer.CheckboxPeer;
  21. import java.awt.peer.ChoicePeer;
  22. import java.awt.peer.DesktopPeer;
  23. import java.awt.peer.DialogPeer;
  24. import java.awt.peer.FileDialogPeer;
  25. import java.awt.peer.FontPeer;
  26. import java.awt.peer.FramePeer;
  27. import java.awt.peer.KeyboardFocusManagerPeer;
  28. import java.awt.peer.LabelPeer;
  29. import java.awt.peer.ListPeer;
  30. import java.awt.peer.MenuBarPeer;
  31. import java.awt.peer.MenuItemPeer;
  32. import java.awt.peer.MenuPeer;
  33. import java.awt.peer.MouseInfoPeer;
  34. import java.awt.peer.PanelPeer;
  35. import java.awt.peer.PopupMenuPeer;
  36. import java.awt.peer.RobotPeer;
  37. import java.awt.peer.ScrollPanePeer;
  38. import java.awt.peer.ScrollbarPeer;
  39. import java.awt.peer.SystemTrayPeer;
  40. import java.awt.peer.TextAreaPeer;
  41. import java.awt.peer.TextFieldPeer;
  42. import java.awt.peer.TrayIconPeer;
  43. import java.awt.peer.WindowPeer;
  44. import java.io.File;
  45. import java.io.FileInputStream;
  46. import java.io.Serializable;
  47. import java.lang.reflect.Field;
  48. import java.lang.reflect.Method;
  49. import java.util.Hashtable;
  50. import java.util.Map;
  51. import java.util.Properties;
  52. import java.util.concurrent.Callable;
  53. import java.util.concurrent.ExecutorService;
  54. import java.util.concurrent.Executors;
  55. import java.util.concurrent.TimeUnit;
  56. import javax.swing.JPanel;
  57. import javax.swing.SwingUtilities;
  58. import javax.swing.UIManager;
  59. import org.webswing.Constants;
  60. import org.webswing.dispatch.EventDispatcher;
  61. import org.webswing.dispatch.PaintDispatcher;
  62. import org.webswing.dispatch.CwmPaintDispatcher;
  63. import org.webswing.dispatch.WebEventDispatcher;
  64. import org.webswing.dispatch.WebPaintDispatcher;
  65. import org.webswing.model.Msg;
  66. import org.webswing.toolkit.api.WebswingMessagingApi;
  67. import org.webswing.toolkit.api.WebswingApi;
  68. import org.webswing.toolkit.api.WebswingApiProvider;
  69. import org.webswing.toolkit.api.lifecycle.ShutdownReason;
  70. import org.webswing.toolkit.extra.WindowManager;
  71. import org.webswing.toolkit.util.DeamonThreadFactory;
  72. import org.webswing.toolkit.util.Logger;
  73. import org.webswing.toolkit.util.Util;
  74. import sun.awt.SunToolkit;
  75. import sun.awt.image.SurfaceManager;
  76. import sun.java2d.SurfaceData;
  77. import sun.print.PrintJob2D;
  78. @SuppressWarnings("restriction")
  79. public abstract class WebToolkit extends SunToolkit implements WebswingApiProvider {
  80. public static final Font defaultFont = new Font("Dialog", 0, 12);
  81. public static final String BACKGROUND_WINDOW_ID = "BG";
  82. private static Object TREELOCK = null;
  83. private EventDispatcher eventDispatcher;
  84. private PaintDispatcher paintDispatcher;
  85. private WebswingApiImpl api = new WebswingApiImpl();
  86. private WebswingMessagingApiImpl msgapi = new WebswingMessagingApiImpl();
  87. private WindowManager windowManager = WindowManager.getInstance();
  88. private ClassLoader swingClassLoader;
  89. public void init() {
  90. try {
  91. if (!System.getProperty("os.name", "").startsWith("Windows") && !System.getProperty("os.name", "").startsWith("Mac")) {
  92. Class<?> c = ClassLoader.getSystemClassLoader().loadClass("sun.awt.X11GraphicsEnvironment");
  93. Method initDisplayMethod = c.getDeclaredMethod("initDisplay", Boolean.TYPE);
  94. initDisplayMethod.setAccessible(true);
  95. initDisplayMethod.invoke(null, false);
  96. }
  97. } catch (Exception e) {
  98. Logger.error("Failed to init X11 display: ", e.getMessage());
  99. }
  100. if (System.getProperty("os.name", "").startsWith("Windows")) {
  101. String path = System.getenv("USERPROFILE") + "\\Desktop";
  102. File desktopFolder = new File(path);
  103. if (!desktopFolder.exists() && !desktopFolder.mkdir()) {
  104. Logger.error("Failed to create Desktop folder: " + path);
  105. }
  106. }
  107. installFonts();
  108. }
  109. private void installFonts() {
  110. try {
  111. GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
  112. Properties fontsProp = new Properties();
  113. String fontConfig = System.getProperty("sun.awt.fontconfig");
  114. if (fontConfig != null) {
  115. fontsProp.load(new FileInputStream(new File(fontConfig)));
  116. for (String name : fontsProp.stringPropertyNames()) {
  117. if (name.startsWith("filename.")) {
  118. String file = fontsProp.getProperty(name);
  119. Font font = Font.createFont(Font.TRUETYPE_FONT, new File(file));
  120. ge.registerFont(font);
  121. }
  122. }
  123. }
  124. } catch (Exception e) {
  125. Logger.error("Failed to install fonts", e);
  126. }
  127. }
  128. public void startDispatchers() {
  129. eventDispatcher = Util.instantiateClass(EventDispatcher.class, Constants.SWING_START_SYS_PROP_EVENT_DISPATCHER_CLASS, WebEventDispatcher.class.getName());
  130. if(eventDispatcher==null){
  131. Logger.fatal("EventDispatcher not initialized. Exiting.");
  132. System.exit(1);
  133. }
  134. Class<? extends PaintDispatcher> defaultPaintDispatcher = Util.isCompositingWM() ? CwmPaintDispatcher.class : WebPaintDispatcher.class;
  135. paintDispatcher = Util.instantiateClass(PaintDispatcher.class, Constants.SWING_START_SYS_PROP_PAINT_DISPATCHER_CLASS, defaultPaintDispatcher.getName());
  136. if(paintDispatcher==null){
  137. Logger.fatal("PaintDispatcher not initialized. Exiting.");
  138. System.exit(1);
  139. }
  140. Logger.info("Webswing Event Dispatcher: "+eventDispatcher.getClass().getName());
  141. Logger.info("Webswing Paint Dispatcher: "+paintDispatcher.getClass().getName());
  142. }
  143. public void initSize(final Integer width, final Integer height) {
  144. final Integer desktopWidth = Math.max(Constants.SWING_SCREEN_WIDTH_MIN, width);
  145. final Integer desktopHeight = Math.max(Constants.SWING_SCREEN_HEIGHT_MIN, height);
  146. if (!SwingUtilities.isEventDispatchThread()) {
  147. SwingUtilities.invokeLater(() -> initSize(desktopWidth, desktopHeight));
  148. } else {
  149. int oldWidht = screenWidth;
  150. int oldHeight = screenHeight;
  151. screenWidth = desktopWidth;
  152. screenHeight = desktopHeight;
  153. displayChanged();
  154. resetGC();
  155. Util.resetWindowsGC(screenWidth, screenHeight);
  156. getPaintDispatcher().notifyScreenSizeChanged(oldWidht, oldHeight,screenWidth, screenHeight);
  157. }
  158. }
  159. public WindowManager getWindowManager() {
  160. return windowManager;
  161. }
  162. public EventDispatcher getEventDispatcher() {
  163. return eventDispatcher;
  164. }
  165. public PaintDispatcher getPaintDispatcher() {
  166. return paintDispatcher;
  167. }
  168. // ////////////////////////////////////////////////////////////////////////////////////////////
  169. // /////////////////// Toolkit Implementation//////////////////////////////////////////////////
  170. private static WebMouseInfoPeer mPeer;
  171. public WebToolkit() {
  172. if (System.getProperty("java.version").startsWith("1.6") || System.getProperty("java.version").startsWith("1.7")) {
  173. try {
  174. Method m = SunToolkit.class.getDeclaredMethod("setDataTransfererClassName", String.class);
  175. m.setAccessible(true);
  176. m.invoke(null, "org.webswing.toolkit.WebDataTransfer");
  177. } catch (Exception e) {
  178. // do nothing
  179. }
  180. }
  181. }
  182. private static GraphicsConfiguration config;
  183. private Hashtable<String, FontPeer> cacheFontPeer;
  184. private WebClipboard clipboard;
  185. private Clipboard selectionClipboard;
  186. private boolean exiting = false;
  187. public static int screenWidth = Math.max(Constants.SWING_SCREEN_WIDTH_MIN,Integer.parseInt(System.getProperty(Constants.SWING_SCREEN_WIDTH, Constants.SWING_SCREEN_WIDTH_MIN + "")));
  188. public static int screenHeight = Math.max(Constants.SWING_SCREEN_HEIGHT_MIN,Integer.parseInt(System.getProperty(Constants.SWING_SCREEN_HEIGHT, Constants.SWING_SCREEN_HEIGHT_MIN + "")));
  189. public static final Object targetToPeer(Object paramObject) {
  190. return SunToolkit.targetToPeer(paramObject);
  191. }
  192. public static final void targetDisposedPeer(Object paramObject1, Object paramObject2) {
  193. SunToolkit.targetDisposedPeer(paramObject1, paramObject2);
  194. }
  195. @Override
  196. protected void initializeDesktopProperties() {
  197. if (Boolean.getBoolean(Constants.SWING_START_SYS_PROP_ISOLATED_FS)) {
  198. this.desktopProperties.put("Shell.shellFolderManager", "org.webswing.toolkit.extra.IsolatedFsShellFolderManager");
  199. } else {
  200. if (System.getProperty("os.name", "").startsWith("Windows")) {
  201. this.desktopProperties.put("Shell.shellFolderManager", "sun.awt.shell.Win32ShellFolderManager2");
  202. }
  203. }
  204. this.desktopProperties.put("DnD.gestureMotionThreshold", 2);
  205. this.desktopProperties.put("DnD.Autoscroll.initialDelay", 100);
  206. this.desktopProperties.put("DnD.Autoscroll.interval", 100);
  207. this.desktopProperties.put("DnD.Autoscroll.cursorHysteresis", 10);
  208. this.desktopProperties.put("awt.dynamicLayoutSupported", true);
  209. this.desktopProperties.put("awt.file.showAttribCol", false);
  210. this.desktopProperties.put("awt.file.showHiddenFiles", false);
  211. this.desktopProperties.put("awt.mouse.numButtons", 5);
  212. this.desktopProperties.put("awt.multiClickInterval", 500);
  213. this.desktopProperties.put("awt.wheelMousePresent", true);
  214. this.desktopProperties.put("win.3d.backgroundColor", new java.awt.Color(240, 240, 240));
  215. this.desktopProperties.put("win.3d.darkShadowColor", new java.awt.Color(105, 105, 105));
  216. this.desktopProperties.put("win.3d.highlightColor", new java.awt.Color(255, 255, 255));
  217. this.desktopProperties.put("win.3d.lightColor", new java.awt.Color(227, 227, 227));
  218. this.desktopProperties.put("win.3d.shadowColor", new java.awt.Color(160, 160, 160));
  219. this.desktopProperties.put("win.ansiFixed.font", Font.decode("Monospaced 0 13"));
  220. this.desktopProperties.put("win.ansiFixed.font.height", 13);
  221. this.desktopProperties.put("win.ansiVar.font", Font.decode("Dialog 0 11"));
  222. this.desktopProperties.put("win.ansiVar.font.height", 11);
  223. this.desktopProperties.put("win.button.textColor", new java.awt.Color(0, 0, 0));
  224. this.desktopProperties.put("win.caret.width", 1);
  225. this.desktopProperties.put("win.defaultGUI.font", Font.decode("Dialog 0 11"));
  226. this.desktopProperties.put("win.defaultGUI.font.height", 11);
  227. this.desktopProperties.put("win.desktop.backgroundColor", new java.awt.Color(0, 0, 0));
  228. this.desktopProperties.put("win.deviceDefault.font", Font.decode("Dialog 1 13"));
  229. this.desktopProperties.put("win.deviceDefault.font.height", 13);
  230. this.desktopProperties.put("win.drag.height", 4);
  231. this.desktopProperties.put("win.drag.width", 4);
  232. this.desktopProperties.put("win.frame.activeBorderColor", new java.awt.Color(180, 180, 180));
  233. this.desktopProperties.put("win.frame.activeCaptionColor", new java.awt.Color(153, 180, 209));
  234. this.desktopProperties.put("win.frame.activeCaptionGradientColor", new java.awt.Color(185, 209, 234));
  235. this.desktopProperties.put("win.frame.backgroundColor", new java.awt.Color(255, 255, 255));
  236. this.desktopProperties.put("win.frame.captionButtonHeight", 22);
  237. this.desktopProperties.put("win.frame.captionButtonWidth", 36);
  238. this.desktopProperties.put("win.frame.captionFont", Font.decode("Dialog 0 12"));
  239. this.desktopProperties.put("win.frame.captionFont.height", 12);
  240. this.desktopProperties.put("win.frame.captionGradientsOn", true);
  241. this.desktopProperties.put("win.frame.captionHeight", 22);
  242. this.desktopProperties.put("win.frame.captionTextColor", new java.awt.Color(0, 0, 0));
  243. this.desktopProperties.put("win.frame.color", new java.awt.Color(100, 100, 100));
  244. this.desktopProperties.put("win.frame.fullWindowDragsOn", true);
  245. this.desktopProperties.put("win.frame.inactiveBorderColor", new java.awt.Color(244, 247, 252));
  246. this.desktopProperties.put("win.frame.inactiveCaptionColor", new java.awt.Color(191, 205, 219));
  247. this.desktopProperties.put("win.frame.inactiveCaptionGradientColor", new java.awt.Color(215, 228, 242));
  248. this.desktopProperties.put("win.frame.inactiveCaptionTextColor", new java.awt.Color(0, 0, 0));
  249. this.desktopProperties.put("win.frame.sizingBorderWidth", 5);
  250. this.desktopProperties.put("win.frame.smallCaptionButtonHeight", 22);
  251. this.desktopProperties.put("win.frame.smallCaptionButtonWidth", 22);
  252. this.desktopProperties.put("win.frame.smallCaptionFont", Font.decode("Dialog 0 12"));
  253. this.desktopProperties.put("win.frame.smallCaptionFont.height", 12);
  254. this.desktopProperties.put("win.frame.smallCaptionHeight", 22);
  255. this.desktopProperties.put("win.frame.textColor", new java.awt.Color(0, 0, 0));
  256. this.desktopProperties.put("win.highContrast.on", false);
  257. this.desktopProperties.put("win.icon.font", Font.decode("Dialog 0 12"));
  258. this.desktopProperties.put("win.icon.font.height", 12);
  259. this.desktopProperties.put("win.icon.hspacing", 75);
  260. this.desktopProperties.put("win.icon.titleWrappingOn", true);
  261. this.desktopProperties.put("win.icon.vspacing", 75);
  262. this.desktopProperties.put("win.item.highlightColor", new java.awt.Color(51, 153, 255));
  263. this.desktopProperties.put("win.item.highlightTextColor", new java.awt.Color(255, 255, 255));
  264. this.desktopProperties.put("win.item.hotTrackedColor", new java.awt.Color(0, 102, 204));
  265. this.desktopProperties.put("win.item.hotTrackingOn", true);
  266. this.desktopProperties.put("win.mdi.backgroundColor", new java.awt.Color(171, 171, 171));
  267. this.desktopProperties.put("win.menu.backgroundColor", new java.awt.Color(240, 240, 240));
  268. this.desktopProperties.put("win.menu.buttonWidth", 19);
  269. this.desktopProperties.put("win.menu.font", Font.decode("Dialog 0 12"));
  270. this.desktopProperties.put("win.menu.font.height", 12);
  271. this.desktopProperties.put("win.menu.height", 19);
  272. this.desktopProperties.put("win.menu.keyboardCuesOn", false);
  273. this.desktopProperties.put("win.menu.textColor", new java.awt.Color(0, 0, 0));
  274. this.desktopProperties.put("win.menubar.backgroundColor", new java.awt.Color(240, 240, 240));
  275. this.desktopProperties.put("win.messagebox.font", Font.decode("Dialog 0 12"));
  276. this.desktopProperties.put("win.messagebox.font.height", 12);
  277. this.desktopProperties.put("win.oemFixed.font", Font.decode("Dialog 0 12"));
  278. this.desktopProperties.put("win.oemFixed.font.height", 12);
  279. this.desktopProperties.put("win.properties.version", 3);
  280. this.desktopProperties.put("win.scrollbar.backgroundColor", new java.awt.Color(200, 200, 200));
  281. this.desktopProperties.put("win.scrollbar.height", 17);
  282. this.desktopProperties.put("win.scrollbar.width", 17);
  283. this.desktopProperties.put("win.status.font", Font.decode("Dialog 0 12"));
  284. this.desktopProperties.put("win.status.font.height", 12);
  285. this.desktopProperties.put("win.system.font", Font.decode("Dialog 1 13"));
  286. this.desktopProperties.put("win.system.font.height", 13);
  287. this.desktopProperties.put("win.systemFixed.font", Font.decode("Dialog 0 12"));
  288. this.desktopProperties.put("win.systemFixed.font.height", 12);
  289. this.desktopProperties.put("win.text.fontSmoothingContrast", 1200);
  290. this.desktopProperties.put("win.text.fontSmoothingOn", true);
  291. this.desktopProperties.put("win.text.fontSmoothingOrientation", 1);
  292. this.desktopProperties.put("win.text.fontSmoothingType", 2);
  293. this.desktopProperties.put("win.text.grayedTextColor", new java.awt.Color(109, 109, 109));
  294. this.desktopProperties.put("win.tooltip.backgroundColor", new java.awt.Color(255, 255, 225));
  295. this.desktopProperties.put("win.tooltip.font", Font.decode("Dialog 0 12"));
  296. this.desktopProperties.put("win.tooltip.font.height", 12);
  297. this.desktopProperties.put("win.tooltip.textColor", new java.awt.Color(0, 0, 0));
  298. this.desktopProperties.put("win.xpstyle.colorName", "NormalColor");
  299. this.desktopProperties.put("win.xpstyle.dllName", "C:\\WINDOWS\\resources\\themes\\Aero\\Aero.msstyles");
  300. this.desktopProperties.put("win.xpstyle.sizeName", "NormalSize");
  301. this.desktopProperties.put("win.xpstyle.themeActive", true);
  302. if (System.getProperty("os.name", "").startsWith("Windows")) {
  303. try {
  304. Field xpStyleEnabledField = sun.awt.windows.ThemeReader.class.getDeclaredField("xpStyleEnabled");
  305. xpStyleEnabledField.setAccessible(true);
  306. xpStyleEnabledField.setBoolean(null, true);
  307. } catch (Exception e) {
  308. Logger.debug("Failed to set xpStyleEnabled to true", e);
  309. }
  310. }
  311. if(Util.isDD()){
  312. RenderingHints hints= new RenderingHints(RenderingHints.KEY_FRACTIONALMETRICS,RenderingHints.VALUE_FRACTIONALMETRICS_OFF);
  313. this.desktopProperties.put("awt.font.desktophints",hints);
  314. }
  315. System.setProperty("swing.disablevistaanimation","true");//See:com.sun.java.swing.plaf.windows.AnimationController.VISTA_ANIMATION_DISABLED
  316. int repaintInterval = 400;
  317. UIManager.put("ProgressBar.repaintInterval", repaintInterval);
  318. UIManager.put("ProgressBar.cycleTime", repaintInterval*60);
  319. }
  320. public boolean needUpdateWindow() {
  321. return true;
  322. }
  323. public boolean isTranslucencyCapable(GraphicsConfiguration paramGraphicsConfiguration) {
  324. return true;
  325. }
  326. public boolean isFrameStateSupported(int state) throws HeadlessException {
  327. return true;
  328. }
  329. public KeyboardFocusManagerPeer createKeyboardFocusManagerPeer(KeyboardFocusManager paramKeyboardFocusManager) throws HeadlessException {
  330. return new WebKeyboardFocusManagerPeer();
  331. }
  332. public FramePeer createFrame(Frame frame) throws HeadlessException {
  333. WebFramePeer localWFramePeer = createWebFramePeer(frame);
  334. targetCreatedPeer(frame, localWFramePeer);
  335. return localWFramePeer;
  336. }
  337. abstract WebFramePeer createWebFramePeer(Frame frame) throws HeadlessException;
  338. public DialogPeer createDialog(Dialog paramDialog) throws HeadlessException {
  339. WebDialogPeer localdialogPeer = createWebDialogPeer(paramDialog);
  340. targetCreatedPeer(paramDialog, localdialogPeer);
  341. return localdialogPeer;
  342. }
  343. abstract WebDialogPeer createWebDialogPeer(Dialog paramDialog);
  344. public boolean isModalityTypeSupported(ModalityType mt) {
  345. return true;
  346. }
  347. public WindowPeer createWindow(Window paramWindow) throws HeadlessException {
  348. WebWindowPeer localwindowPeer = createWebWindowPeer(paramWindow);
  349. targetCreatedPeer(paramWindow, localwindowPeer);
  350. return localwindowPeer;
  351. }
  352. abstract WebWindowPeer createWebWindowPeer(Window paramWindow);
  353. public PanelPeer createPanel(Panel panel) {
  354. if (panel instanceof Applet) {
  355. return super.createPanel(panel);
  356. }
  357. WebPanelPeer localpanelPeer = createWebPanelPeer(panel);
  358. targetCreatedPeer(panel, localpanelPeer);
  359. return localpanelPeer;
  360. }
  361. abstract WebPanelPeer createWebPanelPeer(Panel panel);
  362. public FileDialogPeer createFileDialog(FileDialog paramFileDialog) throws HeadlessException{
  363. WebFileDialogPeer localFileDialogPeer= createWebFileDialogPeer(paramFileDialog);
  364. return localFileDialogPeer;
  365. };
  366. abstract WebFileDialogPeer createWebFileDialogPeer(FileDialog paramFileDialog);
  367. @Override
  368. public synchronized MouseInfoPeer getMouseInfoPeer() {
  369. {
  370. if (mPeer == null) {
  371. mPeer = new WebMouseInfoPeer();
  372. }
  373. return mPeer;
  374. }
  375. }
  376. public FontPeer getFontPeer(String paramString, int paramInt) {
  377. FontPeer localObject = null;
  378. String str = paramString.toLowerCase();
  379. if (null != this.cacheFontPeer) {
  380. localObject = this.cacheFontPeer.get(str + paramInt);
  381. if (null != localObject) {
  382. return localObject;
  383. }
  384. }
  385. localObject = new WebFontPeer(paramString, paramInt);
  386. if (localObject != null) {
  387. if (null == this.cacheFontPeer) {
  388. this.cacheFontPeer = new Hashtable<String, FontPeer>(5, 0.9F);
  389. }
  390. if (null != this.cacheFontPeer) {
  391. this.cacheFontPeer.put(str + paramInt, localObject);
  392. }
  393. }
  394. return ((FontPeer) localObject);
  395. }
  396. public Clipboard getSystemClipboard() throws HeadlessException {
  397. return getWebswingClipboard();
  398. }
  399. public WebClipboard getWebswingClipboard() {
  400. synchronized (this) {
  401. if (this.clipboard == null) {
  402. this.clipboard = new WebClipboard("default", true);
  403. }
  404. }
  405. return this.clipboard;
  406. }
  407. public Clipboard getSystemSelection() throws HeadlessException {
  408. synchronized (this) {
  409. if (this.selectionClipboard == null) {
  410. this.selectionClipboard = new WebClipboard("selection", false);
  411. }
  412. }
  413. return this.selectionClipboard;
  414. }
  415. @Override
  416. protected Object lazilyLoadDesktopProperty(String name) {
  417. if ("awt.font.desktophints".equals(name)) {
  418. return SunToolkit.getDesktopFontHints();
  419. }
  420. return super.lazilyLoadDesktopProperty(name);
  421. }
  422. @Override
  423. protected RenderingHints getDesktopAAHints() {
  424. RenderingHints hints = new RenderingHints(null);
  425. hints.put(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
  426. hints.put(RenderingHints.KEY_TEXT_LCD_CONTRAST, 140);
  427. return hints;
  428. }
  429. public DragSourceContextPeer createDragSourceContextPeer(DragGestureEvent paramDragGestureEvent) throws InvalidDnDOperationException {
  430. return WebDragSourceContextPeer.createDragSourceContextPeer(paramDragGestureEvent);
  431. }
  432. @SuppressWarnings("unchecked")
  433. public <T extends DragGestureRecognizer> T createDragGestureRecognizer(Class<T> type, DragSource dragSource, Component component, int act, DragGestureListener listener) {
  434. if (MouseDragGestureRecognizer.class.equals(type)) {
  435. return (T) new WebMouseDragGestureRecognizer(dragSource, component, act, listener);
  436. }
  437. return null;
  438. }
  439. protected int getScreenWidth() {
  440. return screenWidth;
  441. }
  442. protected int getScreenHeight() {
  443. return screenHeight;
  444. }
  445. public int getScreenResolution() throws HeadlessException {
  446. return 72;
  447. }
  448. public ColorModel getColorModel() throws HeadlessException {
  449. if (config == null) {
  450. resetGC();
  451. }
  452. return config.getColorModel();
  453. }
  454. public void sync() {
  455. }
  456. public Map<TextAttribute, ?> mapInputMethodHighlight(InputMethodHighlight paramInputMethodHighlight) throws HeadlessException {
  457. return null;
  458. }
  459. public static void resetGC() {
  460. config = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
  461. }
  462. public abstract void displayChanged();
  463. // EventQueue.invokeLater(new Runnable() {
  464. //
  465. // public void run() {
  466. // ((WebGraphicsEnvironment) GraphicsEnvironment.getLocalGraphicsEnvironment()).displayChanged();
  467. // }
  468. // });
  469. // }
  470. public InputMethodDescriptor getInputMethodAdapterDescriptor() throws AWTException {
  471. return new WebInputMethodDescriptor();
  472. }
  473. protected boolean syncNativeQueue() {
  474. return true;
  475. }
  476. public void grab(Window paramWindow) {
  477. }
  478. public void ungrab(Window paramWindow) {
  479. }
  480. public ButtonPeer createButton(Button paramButton) throws HeadlessException {
  481. throw new UnsupportedOperationException();
  482. }
  483. public TextFieldPeer createTextField(TextField paramTextField) throws HeadlessException {
  484. throw new UnsupportedOperationException();
  485. }
  486. public ChoicePeer createChoice(Choice paramChoice) throws HeadlessException {
  487. throw new UnsupportedOperationException();
  488. }
  489. public LabelPeer createLabel(Label paramLabel) throws HeadlessException {
  490. throw new UnsupportedOperationException();
  491. }
  492. public ListPeer createList(List paramList) throws HeadlessException {
  493. throw new UnsupportedOperationException();
  494. }
  495. public CheckboxPeer createCheckbox(Checkbox paramCheckbox) throws HeadlessException {
  496. throw new UnsupportedOperationException();
  497. }
  498. public ScrollbarPeer createScrollbar(Scrollbar paramScrollbar) throws HeadlessException {
  499. throw new UnsupportedOperationException();
  500. }
  501. public ScrollPanePeer createScrollPane(ScrollPane paramScrollPane) throws HeadlessException {
  502. throw new UnsupportedOperationException();
  503. }
  504. public TextAreaPeer createTextArea(TextArea paramTextArea) throws HeadlessException {
  505. throw new UnsupportedOperationException();
  506. }
  507. public MenuBarPeer createMenuBar(MenuBar paramMenuBar) throws HeadlessException {
  508. throw new UnsupportedOperationException();
  509. }
  510. public MenuPeer createMenu(Menu paramMenu) throws HeadlessException {
  511. throw new UnsupportedOperationException();
  512. }
  513. public PopupMenuPeer createPopupMenu(PopupMenu paramPopupMenu) throws HeadlessException {
  514. throw new UnsupportedOperationException();
  515. }
  516. public MenuItemPeer createMenuItem(MenuItem paramMenuItem) throws HeadlessException {
  517. throw new UnsupportedOperationException();
  518. }
  519. public CheckboxMenuItemPeer createCheckboxMenuItem(CheckboxMenuItem paramCheckboxMenuItem) throws HeadlessException {
  520. throw new UnsupportedOperationException();
  521. }
  522. public TrayIconPeer createTrayIcon(TrayIcon paramTrayIcon) throws HeadlessException, AWTException {
  523. throw new UnsupportedOperationException();
  524. }
  525. public SystemTrayPeer createSystemTray(SystemTray paramSystemTray) {
  526. return new WebSystemTrayPeer();
  527. }
  528. public boolean isTraySupported() {
  529. return false;
  530. }
  531. public RobotPeer createRobot(Robot robot, GraphicsDevice device) throws AWTException {
  532. return new WebRobotPeer(robot, device);
  533. }
  534. public boolean isDesktopSupported() {
  535. return true;
  536. }
  537. public boolean isWindowOpacityControlSupported() {
  538. return true;
  539. }
  540. public boolean isWindowShapingSupported() {
  541. return false;
  542. }
  543. public boolean isWindowTranslucencySupported() {
  544. return true;
  545. }
  546. public DesktopPeer createDesktopPeer(Desktop paramDesktop) throws HeadlessException {
  547. return new WebDesktopPeer(paramDesktop);
  548. }
  549. public PrintJob getPrintJob(Frame frame, String jobtitle, JobAttributes jobAttributes, PageAttributes pageAttributes) {
  550. PrintJob2D localPrintJob2D = new PrintJob2D(frame, jobtitle, jobAttributes, pageAttributes);
  551. if (!localPrintJob2D.printDialog()) {
  552. localPrintJob2D = null;
  553. }
  554. return localPrintJob2D;
  555. }
  556. public PrintJob getPrintJob(Frame frame, String jobtitle, Properties paramProperties) {
  557. return getPrintJob(frame, jobtitle, null, null);
  558. }
  559. public void beep() {
  560. }
  561. @Override
  562. public Dimension getBestCursorSize(int preferredWidth, int preferredHeight) throws HeadlessException {
  563. return new Dimension(preferredWidth, preferredHeight);
  564. }
  565. @Override
  566. public boolean getLockingKeyState(int keyCode) throws UnsupportedOperationException {
  567. return false;
  568. }
  569. public GraphicsConfiguration getGraphicsConfig() {
  570. if (config == null) {
  571. resetGC();
  572. }
  573. return config;
  574. }
  575. public boolean areExtraMouseButtonsEnabled() throws HeadlessException {
  576. return true;
  577. }
  578. // update system colors to win7 default theme (light)
  579. protected void loadSystemColors(int[] systemColors) throws HeadlessException {
  580. if (systemColors != null && systemColors.length == 26) {
  581. systemColors[0] = 0xff000000;
  582. systemColors[1] = 0xff99b4d1;
  583. systemColors[2] = 0xff000000;
  584. systemColors[3] = 0xffb4b4b4;
  585. systemColors[4] = 0xffbfcddb;
  586. systemColors[5] = 0xff434e54;
  587. systemColors[6] = 0xfff4f7fc;
  588. systemColors[7] = 0xffffffff;
  589. systemColors[8] = 0xff646464;
  590. systemColors[9] = 0xff000000;
  591. systemColors[10] = 0xfff0f0f0;
  592. systemColors[11] = 0xff000000;
  593. systemColors[12] = 0xffffffff;
  594. systemColors[13] = 0xff000000;
  595. systemColors[14] = 0xff3399ff;
  596. systemColors[15] = 0xffffffff;
  597. systemColors[16] = 0xff6d6d6d;
  598. systemColors[17] = 0xfff0f0f0;
  599. systemColors[18] = 0xff000000;
  600. systemColors[19] = 0xffe3e3e3;
  601. systemColors[20] = 0xffffffff;
  602. systemColors[21] = 0xffa0a0a0;
  603. systemColors[22] = 0xff696969;
  604. systemColors[23] = 0xffc8c8c8;
  605. systemColors[24] = 0xffffffe1;
  606. systemColors[25] = 0xff000000;
  607. }
  608. }
  609. abstract public boolean webConpoenentPeerUpdateGraphicsData();
  610. abstract public SurfaceData webComponentPeerReplaceSurfaceData(SurfaceManager mgr);
  611. abstract public int shouldNativelyFocusHeavyweight(Component heavyweight, Component descendant, boolean temporary, boolean focusedWindowChangeAllowed, long time, FocusEventCause cause) ;
  612. @SuppressWarnings("deprecation")
  613. abstract public boolean deliverFocus(Component heavyweight, Component descendant, boolean temporary, boolean focusedWindowChangeAllowed, long time, FocusEventCause cause) ;
  614. public synchronized int executeOnBeforeShutdownListeners(final ShutdownReason reason){
  615. ExecutorService executor = Executors.newSingleThreadExecutor(DeamonThreadFactory.getInstance("Webswing pre-shutdown thread"));
  616. try {
  617. long wait = Long.getLong(Constants.SWING_START_SYS_PROP_SYNC_TIMEOUT, 3000);
  618. Integer delay = executor.submit(new Callable<Integer>() {
  619. @Override
  620. public Integer call() throws Exception {
  621. return api.fireBeforeShutdownListeners(reason);
  622. }
  623. }).get(wait, TimeUnit.MILLISECONDS);
  624. return delay;
  625. } catch (Exception e) {
  626. Logger.error("Failed to execute before-shutdown listeners",e);
  627. }finally {
  628. executor.shutdownNow();
  629. }
  630. return 0;
  631. }
  632. public synchronized void exitSwing(final int i) {
  633. if (!exiting) {
  634. exiting = true;
  635. Thread shutdownThread = new Thread(new Runnable() {
  636. @Override
  637. public void run() {
  638. //tell server to kill this application after defined time
  639. try {
  640. getPaintDispatcher().notifyApplicationExiting();
  641. api.fireShutdownListeners();
  642. } catch (Exception e) {
  643. e.printStackTrace();
  644. System.exit(1);
  645. }
  646. }
  647. });
  648. shutdownThread.setName("Webswing shutdown thread");
  649. shutdownThread.setDaemon(true);
  650. shutdownThread.start();
  651. }
  652. }
  653. public void defaultShutdownProcedure() {
  654. SwingUtilities.invokeLater(new Runnable() {
  655. @Override
  656. public void run() {
  657. //first send windows closing event to all windows
  658. for (Window w : Window.getWindows()) {
  659. w.dispatchEvent(new WindowEvent(w, WindowEvent.WINDOW_CLOSING));
  660. }
  661. }
  662. });
  663. SwingUtilities.invokeLater(new Runnable() {
  664. @Override
  665. public void run() {
  666. //make sure we close windows created by window close listeners executed above
  667. for (Window w : Window.getWindows()) {
  668. w.setVisible(false);
  669. w.dispose();
  670. }
  671. }
  672. });
  673. }
  674. public Object getTreeLock() {
  675. if (TREELOCK == null) {
  676. TREELOCK = new JPanel().getTreeLock();
  677. }
  678. return TREELOCK;
  679. }
  680. @Override
  681. public WebswingApi getApi() {
  682. return api;
  683. }
  684. @Override
  685. public WebswingMessagingApi getMessagingApi() {
  686. return msgapi;
  687. }
  688. public void processApiEvent(Msg event) {
  689. api.processEvent(event);
  690. }
  691. public boolean messageApiHasListenerForClass(String msgtype) {
  692. return msgapi.hasListenerForClass(msgtype);
  693. }
  694. public void messageApiProcessMessage(Serializable object) {
  695. msgapi.processMessage(object);
  696. }
  697. @Override
  698. public Cursor createCustomCursor(Image cursor, Point hotSpot, String name) throws IndexOutOfBoundsException, HeadlessException {
  699. return new WebCursor(cursor, hotSpot, name);
  700. }
  701. public void setSwingClassLoader(ClassLoader swingClassLoader) {
  702. this.swingClassLoader = swingClassLoader;
  703. }
  704. public ClassLoader getSwingClassLoader() {
  705. return swingClassLoader;
  706. }
  707. }