Changeset 807
- Timestamp:
- 02/03/14 11:09:31 (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/expeditee/items/widgets/JfxBrowser.java
r802 r807 10 10 */ 11 11 import java.awt.Point; 12 import java.lang.reflect.Field; 12 13 13 14 import javafx.application.Platform; … … 23 24 import javafx.geometry.Point2D; 24 25 import javafx.geometry.Rectangle2D; 26 import javafx.scene.Node; 25 27 import javafx.scene.Scene; 26 28 import javafx.scene.control.Button; … … 35 37 import javafx.scene.web.WebView; 36 38 37 import javax.swing.JComponent;38 39 39 import org.expeditee.gui.DisplayIO; 40 40 import org.expeditee.gui.FrameMouseActions; … … 72 72 73 73 private MouseButton _buttonDownId = MouseButton.NONE; 74 private MouseEvent _backupEvent = null; 75 private static Field MouseEvent_x, MouseEvent_y; 74 76 75 77 static { 76 78 Platform.setImplicitExit(false); 77 } 78 79 private static Point getCoordFromCaret(TextField text) { 79 80 try { 81 MouseEvent_x = MouseEvent.class.getDeclaredField("x"); 82 MouseEvent_x.setAccessible(true); 83 MouseEvent_y = MouseEvent.class.getDeclaredField("y"); 84 MouseEvent_y.setAccessible(true); 85 } catch (Exception e) { 86 e.printStackTrace(); 87 } 88 } 89 90 private Point getCoordFromCaret(TextField text) { 80 91 TextFieldSkin skin = (TextFieldSkin) text.getSkin(); 81 92 82 Point2D onScreen = text.localToScreen(0, 0); 83 double x = onScreen.getX() - org.expeditee.gui.Browser._theBrowser.getOrigin().x; 84 double y = onScreen.getY() - org.expeditee.gui.Browser._theBrowser.getOrigin().y; 93 Point2D onScene = text.localToScene(0, 0); 94 95 double x = onScene.getX() + JfxBrowser.this.getX();// - org.expeditee.gui.Browser._theBrowser.getOrigin().x; 96 double y = onScene.getY() + JfxBrowser.this.getY();// - org.expeditee.gui.Browser._theBrowser.getOrigin().y; 85 97 86 98 Rectangle2D cp = skin.getCharacterBounds(text.getCaretPosition()); … … 89 101 } 90 102 91 private static int getCaretFromCoord(TextField text, int x, int y) {103 private int getCaretFromCoord(TextField text, MouseEvent e) { 92 104 TextFieldSkin skin = (TextFieldSkin) text.getSkin(); 93 94 MouseEvent fakeMouseEvent = new MouseEvent( 95 MouseEvent.MOUSE_MOVED, //EventType<? extends MouseEvent> eventType 96 x - text.getBoundsInParent().getMinX(), // x 97 y - text.getBoundsInParent().getMinY(), // y 98 FrameMouseActions.getX() + org.expeditee.gui.Browser._theBrowser.getOrigin().x, // screen x 99 FrameMouseActions.getY() + org.expeditee.gui.Browser._theBrowser.getOrigin().y, // screen y 100 MouseButton.NONE, // button 101 0, // click count 102 false, // shift down 103 false, // control down 104 false, // alt down 105 false, // meta down 106 false, // primary button down 107 false, // middle button down 108 false, // secondary button down 109 true, // synthesized 110 false, // popup trigger 111 false, // still since press 112 null // pick result 113 ); 114 115 HitInfo hit = skin.getIndex(fakeMouseEvent); 116 105 HitInfo hit = skin.getIndex(e); 117 106 return hit.getInsertionIndex(); 107 } 108 109 /** 110 * @param src The MouseEvent to clone 111 * @param node The node the position will be relative to 112 * @param x The position in Expeditee space 113 * @param y The position in Expeditee space 114 * @return A fake MouseEvent for a specific position relative to a Node 115 */ 116 private MouseEvent getMouseEventForPosition(MouseEvent src, Node node, int x, int y) { 117 MouseEvent dst = (MouseEvent) ((Event) src).copyFor(null, null); 118 try { 119 MouseEvent_x.set(dst, x - node.localToScene(0, 0).getX()); 120 MouseEvent_y.set(dst, y - node.localToScene(0, 0).getY()); 121 } catch (Exception e) { 122 e.printStackTrace(); 123 } 124 return dst; 118 125 } 119 126 … … 241 248 @Override 242 249 public void handle(MouseEvent e) { 250 JfxBrowser.this._backupEvent = e; 243 251 // make sure we have focus if the mouse is moving over us 244 252 if(!JfxBrowser.this._urlField.isFocused()) { … … 251 259 } else { 252 260 // Otherwise, move the caret to the cursor location 253 int x = FrameMouseActions.getX() - JfxBrowser.this.getX(), y = FrameMouseActions.getY() - JfxBrowser.this.getY();254 JfxBrowser.this._urlField.positionCaret(getCaretFromCoord(JfxBrowser.this._urlField, x, y));261 // int x = FrameMouseActions.getX() - JfxBrowser.this.getX(), y = FrameMouseActions.getY() - JfxBrowser.this.getY(); 262 JfxBrowser.this._urlField.positionCaret(getCaretFromCoord(JfxBrowser.this._urlField, e)); 255 263 } 256 264 } … … 370 378 @Override 371 379 public void handle(javafx.scene.input.MouseEvent e) { 372 switch (e.getButton()) { 373 case SECONDARY: 380 if(e.getButton() == MouseButton.SECONDARY) { 374 381 // Gets text currently selected in webview 375 382 String selection = (String) JfxBrowser.this._webEngine.executeScript("window.getSelection().toString()"); … … 383 390 FrameMouseActions.pickup(t); 384 391 } 385 386 break;387 392 } 388 393 } … … 609 614 // Inserts text in text item into urlField at the position of the mouse. 610 615 String s = JfxBrowser.this._urlField.getText(); 611 int index = getCaretFromCoord(JfxBrowser.this._urlField, x, y);616 int index = getCaretFromCoord(JfxBrowser.this._urlField, getMouseEventForPosition(JfxBrowser.this._backupEvent, JfxBrowser.this._urlField, x, y)); 612 617 if(index < s.length()) { 613 618 s = s.substring(0, index) + insert + s.substring(index);
Note:
See TracChangeset
for help on using the changeset viewer.