Advertisement
Guest User

Untitled

a guest
Mar 10th, 2020
837
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.90 KB | None | 0 0
  1. package org.jabref.gui.fieldeditors;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Arrays;
  5. import java.util.List;
  6.  
  7. import javafx.geometry.Point2D;
  8. import javafx.geometry.Rectangle2D;
  9. import javafx.scene.Scene;
  10. import javafx.scene.control.ContextMenu;
  11. import javafx.scene.control.IndexRange;
  12. import javafx.scene.control.MenuItem;
  13. import javafx.scene.control.PasswordField;
  14. import javafx.scene.control.SeparatorMenuItem;
  15. import javafx.scene.control.TextArea;
  16. import javafx.scene.control.TextField;
  17. import javafx.scene.control.TextInputControl;
  18. import javafx.scene.control.skin.TextAreaSkin;
  19. import javafx.scene.control.skin.TextFieldSkin;
  20. import javafx.scene.input.Clipboard;
  21. import javafx.scene.input.ContextMenuEvent;
  22. import javafx.stage.Screen;
  23. import javafx.stage.Window;
  24.  
  25. import org.jabref.logic.l10n.Localization;
  26. import org.jabref.logic.util.OS;
  27.  
  28. import com.sun.javafx.scene.control.Properties; //comment
  29. //comment
  30.  
  31. /**
  32.  * This class contains some code taken from {@link com.sun.javafx.scene.control.behavior.TextInputControlBehavior},
  33.  * witch is not accessible and thus we have no other choice.
  34.  * TODO: remove this ugly workaround as soon as control behavior is made public
  35.  * reported at https://github.com/javafxports/openjdk-jfx/issues/583
  36.  */
  37. public class TextInputControlBehavior {
  38.  
  39.     private static final boolean SHOW_HANDLES = Properties.IS_TOUCH_SUPPORTED && !OS.OS_X;
  40.  
  41.     /**
  42.      * Returns the default context menu items (except undo/redo)
  43.      */
  44.     public static List<MenuItem> getDefaultContextMenuItems(TextInputControl textInputControl) {
  45.         boolean editable = textInputControl.isEditable();
  46.         boolean hasText = (textInputControl.getLength() > 0);
  47.         boolean hasSelection = (textInputControl.getSelection().getLength() > 0);
  48.         boolean allSelected = (textInputControl.getSelection().getLength() == textInputControl.getLength());
  49.         boolean maskText = (textInputControl instanceof PasswordField); // (maskText("A") != "A");
  50.         ArrayList<MenuItem> items = new ArrayList<>();
  51.  
  52.         MenuItem cutMI = new MenuItem(Localization.lang("Cut"));
  53.         cutMI.setOnAction(e -> textInputControl.cut());
  54.         MenuItem copyMI = new MenuItem(Localization.lang("Copy"));
  55.         copyMI.setOnAction(e -> textInputControl.copy());
  56.         MenuItem pasteMI = new MenuItem(Localization.lang("Paste"));
  57.         pasteMI.setOnAction(e -> textInputControl.paste());
  58.         MenuItem deleteMI = new MenuItem(Localization.lang("Delete"));
  59.         deleteMI.setOnAction(e -> {
  60.             IndexRange selection = textInputControl.getSelection();
  61.             textInputControl.deleteText(selection);
  62.         });
  63.         MenuItem selectAllMI = new MenuItem(Localization.lang("Select all"));
  64.         selectAllMI.setOnAction(e -> textInputControl.selectAll());
  65.         MenuItem separatorMI = new SeparatorMenuItem();
  66.  
  67.         if (SHOW_HANDLES) {
  68.             if (!maskText && hasSelection) {
  69.                 if (editable) {
  70.                     items.add(cutMI);
  71.                 }
  72.                 items.add(copyMI);
  73.             }
  74.             if (editable && Clipboard.getSystemClipboard().hasString()) {
  75.                 items.add(pasteMI);
  76.             }
  77.             if (hasText && !allSelected) {
  78.                 items.add(selectAllMI);
  79.             }
  80.             selectAllMI.getProperties().put("refreshMenu", Boolean.TRUE);
  81.         } else {
  82.             if (editable) {
  83.                 items.addAll(Arrays.asList(cutMI, copyMI, pasteMI, deleteMI, separatorMI, selectAllMI));
  84.             } else {
  85.                 items.addAll(Arrays.asList(copyMI, separatorMI, selectAllMI));
  86.             }
  87.             cutMI.setDisable(maskText || !hasSelection);
  88.             copyMI.setDisable(maskText || !hasSelection);
  89.             pasteMI.setDisable(!Clipboard.getSystemClipboard().hasString());
  90.             deleteMI.setDisable(!hasSelection);
  91.         }
  92.  
  93.         return items;
  94.     }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement