Changeset 556


Ignore:
Timestamp:
11/28/13 14:43:02 (11 years ago)
Author:
ngw8
Message:

Added support to the JfxBrowser URL field for Expeditee-like mouse actions

File:
1 edited

Legend:

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

    r555 r556  
    88import java.awt.event.ComponentEvent;
    99import java.awt.event.ComponentListener;
     10import java.awt.event.FocusEvent;
     11import java.awt.event.FocusListener;
     12import java.awt.event.KeyEvent;
     13import java.awt.event.KeyListener;
     14import java.awt.event.MouseEvent;
     15import java.awt.event.MouseListener;
     16import java.awt.event.MouseMotionListener;
    1017import java.io.File;
    1118import java.lang.reflect.Constructor;
     
    2128import javax.swing.JTextField;
    2229import javax.swing.JToolBar;
    23 
     30import javax.swing.text.BadLocationException;
     31
     32import org.expeditee.gui.DisplayIO;
     33import org.expeditee.gui.FrameMouseActions;
    2434import org.expeditee.gui.FreeItems;
    2535import org.expeditee.gui.MessageBay;
    2636import org.expeditee.gui.UserSettings;
     37import org.expeditee.items.Item;
    2738import org.expeditee.items.Text;
    2839
     
    147158                JfxBrowser owner;
    148159
     160                /**
     161                 * ID of the mouse button that is currently pressed.
     162                 * <p>
     163                 * Uses {@link MouseEvent} constants
     164                 */
     165                private int buttonDownId;
     166
    149167                private JTextField urlField;
    150168
     
    192210                                        public void actionPerformed(ActionEvent e) {
    193211                                                owner.navigate(e.getActionCommand());
     212                                        }
     213                                });
     214
     215                                urlField.addKeyListener(new KeyListener() {
     216
     217                                        @Override
     218                                        public void keyTyped(KeyEvent arg0) {
     219                                                // Hiding the cursor when typing, to be more Expeditee-like
     220                                                DisplayIO.setCursor(org.expeditee.items.Item.HIDDEN_CURSOR);
     221                                        }
     222
     223                                        @Override
     224                                        public void keyReleased(KeyEvent arg0) {
     225                                        }
     226
     227                                        @Override
     228                                        public void keyPressed(KeyEvent arg0) {
     229                                        }
     230                                });
     231
     232                                urlField.addMouseMotionListener(new MouseMotionListener() {
     233
     234                                        @Override
     235                                        public void mouseMoved(MouseEvent arg0) {
     236                                                // Checking if the user has been typing - if so, move the cursor to the caret position
     237                                                if (DisplayIO.getCursor() == Item.HIDDEN_CURSOR) {
     238                                                        DisplayIO.setCursor(org.expeditee.items.Item.TEXT_CURSOR);
     239                                                        try {
     240                                                                DisplayIO.setCursorPosition(
     241                                                                                // Expeditee gives coords relative to the window, Swing gives it relative to the
     242                                                                                // screen
     243                                                                                urlField.getLocationOnScreen().x + urlField.modelToView(urlField.getCaretPosition() + 1).x - org.expeditee.gui.Browser._theBrowser.getOrigin().x,
     244                                                                                urlField.getLocationOnScreen().y + urlField.modelToView(urlField.getCaretPosition()).y - org.expeditee.gui.Browser._theBrowser.getOrigin().y);
     245
     246                                                        } catch (BadLocationException e) {
     247                                                        }
     248                                                } else {
     249                                                        // Otherwise, move the caret to the cursor location
     250                                                        urlField.setCaretPosition(urlField.viewToModel(arg0.getPoint()));
     251                                                }
     252                                        }
     253
     254                                        @Override
     255                                        public void mouseDragged(MouseEvent arg0) {
     256                                                // Selecting text when the mouse is dragged with the middle or right button pressed. If-else
     257                                                // determines which direction the user is selecting in
     258                                                if (buttonDownId == MouseEvent.BUTTON2 || buttonDownId == MouseEvent.BUTTON3) {
     259                                                        if (urlField.viewToModel(arg0.getPoint()) < urlField.getSelectionEnd()) {
     260                                                                urlField.select(urlField.viewToModel(arg0.getPoint()), urlField.getSelectionEnd());
     261                                                        } else {
     262                                                                urlField.select(urlField.getSelectionStart(), urlField.viewToModel(arg0.getPoint()));
     263                                                        }
     264                                                }
     265                                        }
     266                                });
     267                               
     268                                urlField.addFocusListener(new FocusListener() {
     269                                       
     270                                        @Override
     271                                        public void focusGained(FocusEvent arg0) {
     272                                                DisplayIO.setCursor(org.expeditee.items.Item.TEXT_CURSOR);
     273                                        }
     274
     275                                        @Override
     276                                        public void focusLost(FocusEvent arg0) {
     277                                                // Restoring the standard cursor, since it is changed to a text cursor when focus is gained
     278                                                DisplayIO.setCursor(org.expeditee.items.Item.DEFAULT_CURSOR);
     279                                        }
     280                                });
     281
     282                                urlField.addMouseListener(new MouseListener() {
     283
     284                                        @Override
     285                                        public void mouseReleased(MouseEvent arg0) {
     286                                                buttonDownId = MouseEvent.NOBUTTON;
     287
     288                                                Text item;
     289
     290                                                // If nothing is selected, then select all the text so that it will be copied/moved
     291                                                if (urlField.getSelectedText() == null) {
     292                                                        urlField.selectAll();
     293                                                }
     294
     295                                                if (arg0.getButton() == MouseEvent.BUTTON3) {
     296                                                        // Right mouse button released, so copy the selection (i.e. don't remove the original)
     297                                                        item = DisplayIO.getCurrentFrame().createNewText(urlField.getSelectedText());
     298                                                        FrameMouseActions.pickup(item);
     299                                                } else if (arg0.getButton() == MouseEvent.BUTTON2) {
     300                                                        // Middle mouse button released, so copy the selection then remove it from the URL field
     301                                                        item = DisplayIO.getCurrentFrame().createNewText(urlField.getSelectedText());
     302                                                        urlField.setText(urlField.getText().substring(0, urlField.getSelectionStart()) + urlField.getText().substring(urlField.getSelectionEnd(), urlField.getText().length()));
     303
     304                                                        FrameMouseActions.pickup(item);
     305                                                }
     306                                        }
     307
     308                                        @Override
     309                                        public void mousePressed(MouseEvent arg0) {
     310                                                buttonDownId = arg0.getButton();
     311                                        }
     312
     313                                        @Override
     314                                        public void mouseExited(MouseEvent arg0) {
     315                                        }
     316
     317                                        @Override
     318                                        public void mouseEntered(MouseEvent arg0) {
     319                                        }
     320
     321                                        @Override
     322                                        public void mouseClicked(MouseEvent arg0) {
    194323                                        }
    195324                                });
Note: See TracChangeset for help on using the changeset viewer.