Changeset 807


Ignore:
Timestamp:
02/03/14 11:09:31 (10 years ago)
Author:
jts21
Message:

Hopefully fix incompatibility with Java 7 caused by using JavaFX features that did not exist in Java 7

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/expeditee/items/widgets/JfxBrowser.java

    r802 r807  
    1010 */
    1111import java.awt.Point;
     12import java.lang.reflect.Field;
    1213
    1314import javafx.application.Platform;
     
    2324import javafx.geometry.Point2D;
    2425import javafx.geometry.Rectangle2D;
     26import javafx.scene.Node;
    2527import javafx.scene.Scene;
    2628import javafx.scene.control.Button;
     
    3537import javafx.scene.web.WebView;
    3638
    37 import javax.swing.JComponent;
    38 
    3939import org.expeditee.gui.DisplayIO;
    4040import org.expeditee.gui.FrameMouseActions;
     
    7272       
    7373        private MouseButton _buttonDownId = MouseButton.NONE;
     74        private MouseEvent _backupEvent = null;
     75        private static Field MouseEvent_x, MouseEvent_y;
    7476
    7577        static {
    7678                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) {
    8091                TextFieldSkin skin = (TextFieldSkin) text.getSkin();
    8192               
    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;
    8597               
    8698                Rectangle2D cp = skin.getCharacterBounds(text.getCaretPosition());
     
    89101        }
    90102       
    91         private static int getCaretFromCoord(TextField text, int x, int y) {
     103        private int getCaretFromCoord(TextField text, MouseEvent e) {
    92104                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);
    117106                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;
    118125        }
    119126
     
    241248                                @Override
    242249                                public void handle(MouseEvent e) {
     250                                        JfxBrowser.this._backupEvent = e;
    243251                                        // make sure we have focus if the mouse is moving over us
    244252                                        if(!JfxBrowser.this._urlField.isFocused()) {
     
    251259                                        } else {
    252260                                                // 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));
    255263                                        }
    256264                                }
     
    370378                                @Override
    371379                                public void handle(javafx.scene.input.MouseEvent e) {
    372                                         switch (e.getButton()) {
    373                                         case SECONDARY:
     380                                        if(e.getButton() == MouseButton.SECONDARY) {
    374381                                                // Gets text currently selected in webview
    375382                                                String selection = (String) JfxBrowser.this._webEngine.executeScript("window.getSelection().toString()");
     
    383390                                                        FrameMouseActions.pickup(t);
    384391                                                }
    385 
    386                                                 break;
    387392                                        }
    388393                                }
     
    609614                                // Inserts text in text item into urlField at the position of the mouse.
    610615                        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));
    612617                        if(index < s.length()) {
    613618                                s = s.substring(0, index) + insert + s.substring(index);
Note: See TracChangeset for help on using the changeset viewer.