PageRenderTime 42ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/src/com/machinepublishers/glass/ui/monocle/MonocleCursor.java

https://gitlab.com/hemp85/jBrowserDriver
Java | 132 lines | 69 code | 11 blank | 52 comment | 4 complexity | 731075f9a0aea6d19947c02f5c04dd57 MD5 | raw file
  1. /*
  2. * Copyright (c) 2012, 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. package com.machinepublishers.glass.ui.monocle;
  26. import com.sun.glass.ui.Application;
  27. import com.sun.glass.ui.Cursor;
  28. import com.sun.glass.ui.Pixels;
  29. import com.machinepublishers.glass.ui.monocle.MonocleApplication;
  30. import com.machinepublishers.glass.ui.monocle.MonocleCursor;
  31. import com.machinepublishers.glass.ui.monocle.NativeCursor;
  32. import com.machinepublishers.glass.ui.monocle.NativePlatformFactory;
  33. import java.io.IOException;
  34. import java.io.InputStream;
  35. final class MonocleCursor extends Cursor {
  36. private byte[] image;
  37. private int hotspotX;
  38. private int hotspotY;
  39. MonocleCursor(int type) {
  40. super(type);
  41. image = getImage(type);
  42. hotspotX = 0;
  43. hotspotY = 0;
  44. }
  45. MonocleCursor(int x, int y, Pixels pixels) {
  46. super(x, y, pixels);
  47. }
  48. void applyCursor() {
  49. int type = getType();
  50. if (type == CURSOR_NONE) {
  51. // CURSOR_NONE is mapped to setVisible(false) and will be registered
  52. // in MonocleApplication as a preference to not show the cursor.
  53. ((MonocleApplication) Application.GetApplication())
  54. .staticCursor_setVisible(false);
  55. } else {
  56. NativeCursor cursor = NativePlatformFactory.getNativePlatform().getCursor();
  57. cursor.setImage(image);
  58. ((MonocleApplication) Application.GetApplication())
  59. .staticCursor_setVisible(true);
  60. }
  61. }
  62. @Override
  63. protected long _createCursor(int x, int y, Pixels pixels) {
  64. hotspotX = x;
  65. hotspotY = y;
  66. image = pixels.asByteBuffer().array();
  67. return 1l;
  68. }
  69. private static String cursorResourceName(int cursorType) {
  70. switch (cursorType) {
  71. case CURSOR_CLOSED_HAND: return "ClosedHand";
  72. case CURSOR_CROSSHAIR: return "Crosshair";
  73. case CURSOR_DISAPPEAR: return "Disappear";
  74. case CURSOR_MOVE: return "Move";
  75. case CURSOR_OPEN_HAND: return "OpenHand";
  76. case CURSOR_POINTING_HAND: return "PointingHand";
  77. case CURSOR_RESIZE_DOWN: return "ResizeDown";
  78. case CURSOR_RESIZE_LEFT: return "ResizeLeft";
  79. case CURSOR_RESIZE_LEFTRIGHT: return "ResizeLeftRight";
  80. case CURSOR_RESIZE_NORTHEAST: return "ResizeNorthEast";
  81. case CURSOR_RESIZE_NORTHWEST: return "ResizeNorthWest";
  82. case CURSOR_RESIZE_RIGHT: return "ResizeRight";
  83. case CURSOR_RESIZE_SOUTHEAST: return "ResizeSouthEast";
  84. case CURSOR_RESIZE_SOUTHWEST: return "ResizeSouthWest";
  85. case CURSOR_RESIZE_UP: return "ResizeUp";
  86. case CURSOR_RESIZE_UPDOWN: return "ResizeUpDown";
  87. case CURSOR_TEXT: return "Text";
  88. case CURSOR_WAIT: return "Wait";
  89. default: return "Default";
  90. }
  91. }
  92. private static byte[] getImage(int cursorType) {
  93. // InputStream in = null;
  94. // try {
  95. // in = MonocleCursor.class.getResourceAsStream(
  96. // "Cursor"
  97. // + cursorResourceName(cursorType)
  98. // + "Translucent.raw");
  99. // byte[] b = new byte[1024];
  100. // int bytesRead = 0;
  101. // while (bytesRead < 1024) {
  102. // int read = in.read(b, bytesRead, 1024 - bytesRead);
  103. // if (read >= 0) {
  104. // bytesRead += read;
  105. // } else {
  106. // throw new IOException("Incomplete cursor resource");
  107. // }
  108. // }
  109. // return b;
  110. // } catch (IOException e) {
  111. // e.printStackTrace();
  112. return null;
  113. // } finally {
  114. // if (in != null) {
  115. // try {
  116. // in.close();
  117. // } catch (IOException e) { }
  118. // }
  119. // }
  120. }
  121. }