daily pastebin goal
91%
SHARE
TWEET

Untitled

a guest May 27th, 2018 133 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package edu.ycp.cs.dh.acegwt.client.ace;
  2.  
  3. import com.google.gwt.core.client.JavaScriptObject;
  4.  
  5. /**
  6.  * Rules describing how editor runs this command (from keyboard,
  7.  * from command line or by {@link AceEditor#execCommand(String)} API calls).
  8.  */
  9. public class AceCommandDescription {
  10.     private final String name;
  11.     private final ExecAction exec;
  12.     private KeyBinding bindKey = null;
  13.     private boolean readOnly = false;
  14.     private boolean passEvent = false;
  15.     private ScrollIntoView scrollIntoView = null;
  16.     private MultiSelectAction multiSelectAction = null;
  17.     private String aceCommandGroup = null;
  18.    
  19.     /**
  20.      * Define Ace command with command line name and execution action
  21.      * @param name command line name
  22.      * @param exec execution action
  23.      */
  24.     public AceCommandDescription(String name, ExecAction exec) {
  25.         this.name = name;
  26.         this.exec = exec;
  27.     }
  28.  
  29.     /**
  30.      * Give command line name.
  31.      * @return command line name
  32.      */
  33.     public String getName() {
  34.         return name;
  35.     }
  36.    
  37.     /**
  38.      * Give execution action of this command.
  39.      * @return execution action
  40.      */
  41.     public ExecAction getExec() {
  42.         return exec;
  43.     }
  44.  
  45.     /**
  46.      * Give key bindings.
  47.      * @return key bindings
  48.      */
  49.     public KeyBinding getBindKey() {
  50.         return bindKey;
  51.     }
  52.  
  53.     /**
  54.      * Describe does this command change editor document or not.
  55.      * @return whether or not editor document could be changed
  56.      */
  57.     public boolean isReadOnly() {
  58.         return readOnly;
  59.     }
  60.  
  61.     /**
  62.      * In case this parameter is true keyboard event will be passed to
  63.      * original listener.
  64.      * @return true in case event shouldn't be stopped after command execution
  65.      */
  66.     public boolean isPassEvent() {
  67.         return passEvent;
  68.     }
  69.  
  70.     /**
  71.      * Give scroll settings for this command. In case it's null command is
  72.      * executed without scroll adjustment. Otherwise see
  73.      * {@link ScrollIntoView} for details.
  74.      * @return scroll settings
  75.      */
  76.     public ScrollIntoView getScrollIntoView() {
  77.         return scrollIntoView;
  78.     }
  79.    
  80.     /**
  81.      * Give multi-select action for this command. In case it's null command is
  82.      * executed once without virtual changes in selection ranges. Otherwise
  83.      * see {@link MultiSelectAction} for details.
  84.      * @return multi-select action
  85.      */
  86.     public MultiSelectAction getMultiSelectAction() {
  87.         return multiSelectAction;
  88.     }
  89.  
  90.     /**
  91.      * Give Ace command group name of this command.
  92.      * @return Ace command group name
  93.      */
  94.     public String getAceCommandGroup() {
  95.         return aceCommandGroup;
  96.     }
  97.  
  98.     /**
  99.      * Chainable setter method for bindKey property.
  100.      * @param bindKey key binding (e.g. "shift-esc|ctrl-`" or "Command+Alt+C")
  101.      * @return reference to this command description
  102.      */
  103.     public AceCommandDescription withBindKey(KeyBinding bindKey) {
  104.         this.bindKey = bindKey;
  105.         return this;
  106.     }
  107.  
  108.     /**
  109.      * Chainable setter method for bindKey property for all platforms.
  110.      * @param bindKeyForAllPlatforms key binding (e.g. "shift-esc|ctrl-`" or "Command+Alt+C")
  111.      * @return reference to this command description
  112.      */
  113.     public AceCommandDescription withBindKey(String bindKeyForAllPlatforms) {
  114.         this.bindKey = new KeyBinding(bindKeyForAllPlatforms);
  115.         return this;
  116.     }
  117.  
  118.     /**
  119.      * Chainable setter method for bindKey property for Mac and other platforms separately.
  120.      * @param bindKeyForAllPlatformsExceptMac key binding for everything except Mac (e.g. "shift-esc|ctrl-`")
  121.      * @param bindKeyForMac key binding for Mac (e.g. "shift-esc|ctrl-`" or "Command+Alt+C")
  122.      * @return reference to this command description
  123.      */
  124.     public AceCommandDescription withBindKey(String bindKeyForAllPlatformsExceptMac,
  125.             String bindKeyForMac) {
  126.         this.bindKey = new KeyBinding(bindKeyForAllPlatformsExceptMac, bindKeyForMac);
  127.         return this;
  128.     }
  129.  
  130.     /**
  131.      * Chainable setter method for readOnly property.
  132.      * @param readOnly describes does this command change editor document or not
  133.      * @return reference to this command description
  134.      */
  135.     public AceCommandDescription withReadOnly(boolean readOnly) {
  136.         this.readOnly = readOnly;
  137.         return this;
  138.     }
  139.  
  140.     /**
  141.      * Chainable setter method for passEvent property.
  142.      * @param passEvent in case it's true keyboard event will not be stopped
  143.      *      and will be passed to original listener after command execution
  144.      * @return reference to this command description
  145.      */
  146.     public AceCommandDescription withPassEvent(boolean passEvent) {
  147.         this.passEvent = passEvent;
  148.         return this;
  149.     }
  150.  
  151.     /**
  152.      * Chainable setter method for scrollIntoView property.
  153.      * @param scrollIntoView scroll settings for this command (in case it's
  154.      *      null command is executed without scroll adjustment; otherwise
  155.      *      see {@link ScrollIntoView} for details)
  156.      * @return reference to this command description
  157.      */
  158.     public AceCommandDescription withScrollIntoView(ScrollIntoView scrollIntoView) {
  159.         this.scrollIntoView = scrollIntoView;
  160.         return this;
  161.     }
  162.    
  163.     /**
  164.      * Chainable setter method for multiSelectAction property.
  165.      * @param multiSelectAction multi-select action for this command (in
  166.      *      case it's null command is executed once without virtual changes
  167.      *      in selection ranges; otherwise see {@link MultiSelectAction} for
  168.      *      details)
  169.      * @return reference to this command description
  170.      */
  171.     public AceCommandDescription withMultiSelectAction(MultiSelectAction multiSelectAction) {
  172.         this.multiSelectAction = multiSelectAction;
  173.         return this;
  174.     }
  175.  
  176.     /**
  177.      * Chainable setter method for aceCommandGroup property.
  178.      * @param aceCommandGroup Ace command group name of this command
  179.      * @return reference to this command description
  180.      */
  181.     public AceCommandDescription withAceCommandGroup(String aceCommandGroup) {
  182.         this.aceCommandGroup = aceCommandGroup;
  183.         return this;
  184.     }
  185.  
  186.     @Override
  187.     public String toString() {
  188.         return "AceCommandDescription [name=" + name + ", exec=" + exec
  189.                 + ", bindKey=" + bindKey + ", readOnly=" + readOnly
  190.                 + ", passEvent=" + passEvent + ", scrollIntoView="
  191.                 + scrollIntoView + ", multiSelectAction=" + multiSelectAction
  192.                 + ", aceCommandGroup=" + aceCommandGroup + "]";
  193.     }
  194.  
  195.     /**
  196.      * Create Ace command from javascript object.
  197.      * @param obj javascript object describing Ace command
  198.      * @return Ace command description object
  199.      */
  200.     public static native AceCommandDescription fromJavaScript(JavaScriptObject obj) /*-{
  201.         var name = obj.name;
  202.         var exec = @edu.ycp.cs.dh.acegwt.client.ace.AceCommandDescription::createJavaScriptWrapper(Ljava/lang/Object;)(obj.exec);
  203.         var ret = @edu.ycp.cs.dh.acegwt.client.ace.AceCommandDescription::new(Ljava/lang/String;Ledu/ycp/cs/dh/acegwt/client/ace/AceCommandDescription$ExecAction;)(name, exec);
  204.         var bindKey = @edu.ycp.cs.dh.acegwt.client.ace.AceCommandDescription.KeyBinding::fromJavaScript(Ljava/lang/Object;)(obj.bindKey);
  205.         if (bindKey)
  206.             ret.@edu.ycp.cs.dh.acegwt.client.ace.AceCommandDescription::withBindKey(Ledu/ycp/cs/dh/acegwt/client/ace/AceCommandDescription$KeyBinding;)(bindKey);
  207.         var readOnly = obj.readOnly;
  208.         if (readOnly)
  209.             ret.@edu.ycp.cs.dh.acegwt.client.ace.AceCommandDescription::withReadOnly(Z)(readOnly);
  210.         var passEvent = obj.passEvent;
  211.         if (passEvent)
  212.             ret.@edu.ycp.cs.dh.acegwt.client.ace.AceCommandDescription::withPassEvent(Z)(passEvent);
  213.         var scrollIntoView = @edu.ycp.cs.dh.acegwt.client.ace.AceCommandDescription.ScrollIntoView::fromString(Ljava/lang/String;)(obj.scrollIntoView);
  214.         if (scrollIntoView)
  215.             ret.@edu.ycp.cs.dh.acegwt.client.ace.AceCommandDescription::withScrollIntoView(Ledu/ycp/cs/dh/acegwt/client/ace/AceCommandDescription$ScrollIntoView;)(scrollIntoView);
  216.         var objMultiSelectAction = obj.multiSelectAction;
  217.         var multiSelectAction = null;
  218.         if (typeof objMultiSelectAction === "string")
  219.             multiSelectAction = @edu.ycp.cs.dh.acegwt.client.ace.AceCommandDescription.MultiSelectAction::fromString(Ljava/lang/String;)(objMultiSelectAction);
  220.         if (multiSelectAction)
  221.             ret.@edu.ycp.cs.dh.acegwt.client.ace.AceCommandDescription::withMultiSelectAction(Ledu/ycp/cs/dh/acegwt/client/ace/AceCommandDescription$MultiSelectAction;)(multiSelectAction);
  222.         var aceCommandGroup = obj.aceCommandGroup;
  223.         if (aceCommandGroup)
  224.             ret.@edu.ycp.cs.dh.acegwt.client.ace.AceCommandDescription::withAceCommandGroup(Ljava/lang/String;)(aceCommandGroup);
  225.         return ret;
  226.     }-*/;
  227.  
  228.     private static AceCommandDescription.ExecAction createJavaScriptWrapper(final Object jsFunction) {
  229.         return new AceCommandDescription.ExecAction() {
  230.             @Override
  231.             public Object exec(AceEditor editor) {
  232.                 return invokeJavaScriptCommand(jsFunction, editor);
  233.             }
  234.            
  235.             @Override
  236.             public String toString() {
  237.                 return "ExecAction [javascript=" + jsFunction + "]";
  238.             }
  239.         };
  240.     }
  241.    
  242.     private static native Object invokeJavaScriptCommand(Object jsFunction, AceEditor javaWrapper) /*-{
  243.         var jsEditor = javaWrapper.@edu.ycp.cs.dh.acegwt.client.ace.AceEditor::editor;
  244.         return jsFunction(jsEditor);
  245.     }-*/;
  246.  
  247.     /**
  248.      * Create javascript object from Ace command description.
  249.      * @param editor Ace editor java wrapper
  250.      * @return Ace command javascript object
  251.      */
  252.     public native JavaScriptObject toJavaScript(AceEditor editor) /*-{
  253.         var ret = {};
  254.         ret['name'] = this.@edu.ycp.cs.dh.acegwt.client.ace.AceCommandDescription::name;
  255.         var javaExec = this.@edu.ycp.cs.dh.acegwt.client.ace.AceCommandDescription::exec;
  256.         ret['exec'] = function() {
  257.             javaExec.@edu.ycp.cs.dh.acegwt.client.ace.AceCommandDescription.ExecAction::exec(Ledu/ycp/cs/dh/acegwt/client/ace/AceEditor;)(editor);
  258.         };
  259.         var bindKey = this.@edu.ycp.cs.dh.acegwt.client.ace.AceCommandDescription::bindKey;
  260.         if (bindKey)
  261.             ret['bindKey'] = bindKey.@edu.ycp.cs.dh.acegwt.client.ace.AceCommandDescription.KeyBinding::toJavaScript()();
  262.         var readOnly = this.@edu.ycp.cs.dh.acegwt.client.ace.AceCommandDescription::readOnly;
  263.         if (readOnly)
  264.             ret['readOnly'] = readOnly;
  265.         var passEvent = this.@edu.ycp.cs.dh.acegwt.client.ace.AceCommandDescription::passEvent;
  266.         if (passEvent)
  267.             ret['passEvent'] = passEvent;
  268.         var scrollIntoView = this.@edu.ycp.cs.dh.acegwt.client.ace.AceCommandDescription::scrollIntoView;
  269.         if (scrollIntoView)
  270.             ret['scrollIntoView'] = scrollIntoView.@edu.ycp.cs.dh.acegwt.client.ace.AceCommandDescription.ScrollIntoView::name()();
  271.         var multiSelectAction = this.@edu.ycp.cs.dh.acegwt.client.ace.AceCommandDescription::multiSelectAction;
  272.         if (multiSelectAction)
  273.             ret['multiSelectAction'] = multiSelectAction.@edu.ycp.cs.dh.acegwt.client.ace.AceCommandDescription.MultiSelectAction::name()();
  274.         var aceCommandGroup = this.@edu.ycp.cs.dh.acegwt.client.ace.AceCommandDescription::aceCommandGroup;
  275.         if (aceCommandGroup)
  276.             ret['aceCommandGroup'] = aceCommandGroup;
  277.         return ret;
  278.     }-*/;
  279.    
  280.     /**
  281.      * Key binding description.
  282.      */
  283.     public static class KeyBinding {
  284.         private final String allPlatforms;
  285.         private final String mac;
  286.         private final String exceptMac;
  287.        
  288.         /**
  289.          * Constructor for key bindings for all platforms.
  290.          * @param allPlatforms key bindings (e.g. "shift-esc|ctrl-`" or "Command+Alt+C")
  291.          */
  292.         public KeyBinding(String allPlatforms) {
  293.             this.allPlatforms = allPlatforms;
  294.             this.mac = null;
  295.             this.exceptMac = null;
  296.         }
  297.        
  298.         /**
  299.          * Constructor for separate key bindings for Mac and other platforms.
  300.          * @param exceptMac key bindings for all other than Mac (e.g. "shift-esc|ctrl-`")
  301.          * @param mac key bindings for Mac (e.g. "Command+Alt+C")
  302.          */
  303.         public KeyBinding(String exceptMac, String mac) {
  304.             this.allPlatforms = null;
  305.             this.mac = mac;
  306.             this.exceptMac = exceptMac;
  307.         }
  308.        
  309.         public String getAllPlatforms() {
  310.             return allPlatforms;
  311.         }
  312.        
  313.         public String getMac() {
  314.             return mac;
  315.         }
  316.        
  317.         public String getExceptMac() {
  318.             return exceptMac;
  319.         }
  320.  
  321.         @Override
  322.         public String toString() {
  323.             if (allPlatforms != null)
  324.                 return "KeyBinding [allPlatforms=" + allPlatforms + "]";
  325.             return "KeyBinding [mac=" + mac + ", exceptMac=" + exceptMac + "]";
  326.         }
  327.        
  328.         private static native KeyBinding fromJavaScript(Object obj) /*-{
  329.             if (!obj)
  330.                 return null;
  331.             if (typeof obj === "string")
  332.                 return @edu.ycp.cs.dh.acegwt.client.ace.AceCommandDescription.KeyBinding::new(Ljava/lang/String;)(obj);
  333.             return @edu.ycp.cs.dh.acegwt.client.ace.AceCommandDescription.KeyBinding::new(Ljava/lang/String;Ljava/lang/String;)(obj.win, obj.mac);
  334.         }-*/;
  335.  
  336.         private native Object toJavaScript() /*-{
  337.             var ret = this.@edu.ycp.cs.dh.acegwt.client.ace.AceCommandDescription.KeyBinding::allPlatforms;
  338.             if (ret)
  339.                 return ret;
  340.             var win = this.@edu.ycp.cs.dh.acegwt.client.ace.AceCommandDescription.KeyBinding::exceptMac;
  341.             var mac = this.@edu.ycp.cs.dh.acegwt.client.ace.AceCommandDescription.KeyBinding::mac;
  342.             return {win: win, mac: mac};
  343.         }-*/;
  344.     }
  345.    
  346.     /**
  347.      * Ace command execution action.
  348.      */
  349.     public static interface ExecAction {
  350.         public Object exec(AceEditor editor);
  351.     }
  352.    
  353.     /**
  354.      * Ace command scrolling options.
  355.      */
  356.     public static enum ScrollIntoView {
  357.         /**
  358.          * Scroll to cursor with animation
  359.          */
  360.         animate,
  361.         /**
  362.          * Locate cursor in the center of the screen
  363.          */
  364.         center,
  365.         /**
  366.          * Scroll to cursor without animation
  367.          */
  368.         cursor,
  369.         /**
  370.          * Scroll to selected text
  371.          */
  372.         selectionPart;
  373.        
  374.         public static ScrollIntoView fromString(String value) {
  375.             for (ScrollIntoView ret : ScrollIntoView.values()) {
  376.                 if (ret.name().equals(value))
  377.                     return ret;
  378.             }
  379.             return null;
  380.         }
  381.     }
  382.    
  383.     /**
  384.      * Action defines a way of running commands based on editor text selection.
  385.      */
  386.     public static enum MultiSelectAction {
  387.         /**
  388.          * Execute command for each selection in multi-select mode
  389.          */
  390.         forEach,
  391.         /**
  392.          * Execute command for each selection grouped by lines in multi-select mode
  393.          */
  394.         forEachLine,
  395.         /**
  396.          * Execute command once treating multi-selection as a single range
  397.          */
  398.         single;
  399.  
  400.         public static MultiSelectAction fromString(String value) {
  401.             for (MultiSelectAction ret : MultiSelectAction.values()) {
  402.                 if (ret.name().equals(value))
  403.                     return ret;
  404.             }
  405.             return null;
  406.         }
  407.     }
  408. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top