WingEditor API Users' Guide


  1. Introduction to WingEditor API

    WingEditor's API provides an interface for Java programs to access WingEditor's editor environment directly. WingEditor's API provides the following capabilities to Java programs,

    
    
    
    
    
  2. How to Use WingEditor API

    2.1. Launch Editor Window

    To lauch an editor window, a simple program (save it as testeditor.java) as follows will do,

    
    import wingsoft.editor.Main;
    
    public class testeditor {
        public static void main(String argv[]) {
        Main editor= new Main("testeditor.java");
            editor.layoutMain();
            editor.show();
        }
    }
    
    

    2.2. Change Global Information

    WingEditor API provides methods to access global information. Say, if we want to set green as background color, and select Courier/PLAIN/14 as the default font, the program can be modified to do that (feel free to change the color to what you like).

    
    // save this as testeditor2.java
    import wingsoft.editor.Main;
    import wingsoft.editor.Global;
    import java.awt.*;
    
    public class testeditor2 {
        public static void main(String argv[]) {
            Main editor= new Main("testeditor.java");
            Global.setBackgroundColor(Color.green);
            Global.setTextFont(new Font("Courier",Font.PLAIN,14));
            editor.show();
        }
    }
    
    

    2.3. Create User-Defined Button-Bar

    UserButton interface provides ways for users to customerize their editing environment. The following program create a new user button bar to function as switches to 3 files (save it as testeditor3.java). In this example, the user-defined button bar is appended to the existing button bar.

    
    // save this as testeditor3.java
    import wingsoft.editor.Main;
    import wingsoft.editor.Global;
    import wingsoft.editor.UserButton;
    
    import java.awt.*;
    
    public class testeditor3 {
        public static void main(String argv[]) {
            Main myeditor= new Main("testeditor.java");
            Global.setBackgroundColor(Color.green);
            Global.setTextFont(new Font("Courier",Font.PLAIN,14));
    
            // create user-defined buttonBar
            myButton[] buttons= new myButton[3];
            buttons[0]= new myButton("testeditor");
            buttons[1]= new myButton("testeditor2");
            buttons[2]= new myButton("testeditor3");
                
            myeditor.setUserButtonBar(buttons,false); 
            // append to existing buttonBar 
            myeditor.layoutMain();
            myeditor.show();
        }
    }
    
    // this class implements UserButton interface
    class myButton extends Button implements UserButton {
        private String title;
        public myButton(String title) {
            super(title);
            this.title= title;
        }
    
        Main get_parent() {
            Container c= getParent();
            while ((c instanceof Main)==false) {
                c= c.getParent();
            }
            return (Main)c;
        }        
    
        public void userAction(Event event) {
            
            get_parent().loadFile(title+".java");
        }
    }
            
    

    2.4. Use Browser

    Browser class provides an interface for users to browsing source or error message. The contents of the file have to be in the format of

        
            file_name:line_number
    
    

    As an example, save the following contents into a file named testbrowser.txt,

    
    Compiling error at testeditor.java:5
    
    Compiling error at testeditor2.java:10
    
    Find something at testeditor3.java:12
    
    

    Modify the example to include a browser window (save as testeditor4.java). In this example, the default buttonBar is replaced by the user-defined button bar.

    
    import wingsoft.editor.Main;
    import wingsoft.editor.Global;
    import wingsoft.editor.UserButton;
    import wingsoft.editor.Browser;
    
    import java.awt.*;
    
    public class testeditor4 {
        public static void main(String argv[]) {
            Main myeditor= new Main("testeditor.java");
            Global.setBackgroundColor(Color.green);
            Global.setTextFont(new Font("Courier",Font.PLAIN,14));
    
            // create user-defined buttonBar
            myButton[] buttons= new myButton[3];
            buttons[0]= new myButton("testeditor");
            buttons[1]= new myButton("testeditor2");
            buttons[2]= new myButton("testeditor3");
                
            myeditor.setUserButtonBar(buttons,true);
            // replace the exising buttonBar
            myeditor.layoutMain();
            myeditor.show();
    
            // launch a browser window
            Browser browser= new Browser("testbrowser.txt",
                "Test Browser Capability",false);
            browser.show();
        }
    }
    
    // this class implements UserButton interface
    class myButton extends Button implements UserButton {
        private String title;
        public myButton(String title) {
            super(title);
            this.title= title;
        }
    
        Main get_parent() {
            Container c= getParent();
            while ((c instanceof Main)==false) {
                c= c.getParent();
            }
            return (Main)c;
        }        
    
        public void userAction(Event event) {
            get_parent().loadFile(title+".java");
        }
    }
            
    

    Just click on a non-empty line, you will see that an editor window is launched, and specific line is colored.

    2.5. Add a Language

    WingEditor API allows user to add a language for coloring. Modify the example in 2.4, and add a language named Question. WingEditor will consider the file with extension ".ask" as the language, and the language has 6 keywords ("What", "Which", "Where", "How", "Who", "Why"), and the keywords are case-sensitive.

    
    // save this file as testeditor5.java
    import wingsoft.editor.Main;
    import wingsoft.editor.Global;
    import wingsoft.editor.UserButton;
    import wingsoft.editor.Browser;
    
    import java.awt.*;
    
    public class testeditor5 {
        public static void main(String argv[]) {
            String[] keywords= {
                "What",
                "Which",
                "Where",
                "How",
                "Who",
                "Why"
            };
            
            String[] extensions= {
                ".ask"
            };
            
            Global.addLanguage("Question",extensions,keywords,true);
            Main myeditor= new Main("testeditor.java");
            Global.setBackgroundColor(Color.green);
            Global.setTextFont(new Font("Courier",Font.PLAIN,14));
    
            // create user-defined buttonBar
            myButton[] buttons= new myButton[3];
            buttons[0]= new myButton("testeditor");
            buttons[1]= new myButton("testeditor2");
            buttons[2]= new myButton("testeditor3");
                
            myeditor.setUserButtonBar(buttons,true);
            // replace the exising buttonBar
    
            myeditor.layoutMain();
            myeditor.show();
    
            // launch a browser window
            Browser browser= new Browser("testbrowser.txt",
                "Test Browser Capability",false);
            browser.show();
        }
    }
    
    // this class implements UserButton interface
    class myButton extends Button implements UserButton {
        private String title;
        public myButton(String title) {
            super(title);
            this.title= title;
        }
    
        Main get_parent() {
            Container c= getParent();
            while ((c instanceof Main)==false) {
                c= c.getParent();
            }
            return (Main)c;
        }        
    
        public void userAction(Event event) {
            
            get_parent().loadFile(title+".java");
        }
    }
            
    

    2.5. Subclass Editor Window

    So far, we have only talked about how to use Editor Window directly. WingEditor also allows users to subclass the WingEditor's editor window, modify menu bar, and replace button bar.

    The following is an example showing how to write a subclass of editor window. In the example, class myMain is a subclass to editor window. In order to use File Menu on editor window, the method getEditor has to be overwritten. The subclass also disable the Goto and Project Menu.

    
    // save this file as testeditor6.java
    import wingsoft.editor.Main;
    import wingsoft.editor.Global;
    import wingsoft.editor.UserButton;
    import wingsoft.editor.Browser;
    import wingsoft.editor.EditorConst;
    
    import java.awt.*;
    
    public class testeditor6 {
        public static void main(String argv[]) {
            String[] keywords= {
                "What",
                "Which",
                "Where",
                "How",
                "Who",
                "Why"
            };
            
            String[] extensions= {
                ".ask"
            };
            
            Global.addLanguage("Question",extensions,keywords,true);
            
            myMain myeditor= new myMain("testeditor.java");
    
            Global.setTextFont(new Font("Courier",Font.PLAIN,14));
            myeditor.show();
        }
    }
    
    // this is an example showing how to subclass the editor window.
    //
    // this is a subclass to WingEditor's editor window.
    // in the subclass, new Menu named ActionMenu is appended,
    // a MenuItem showAction is appended to the View Menu.
    // Goto and Project Menu are turned off.
    //
    class myMain extends Main {
    
        MenuItem sayItem, singItem, showActionItem;
        static int offMenus[]= {GOTO_MENU,PROJECT_MENU};
    
        public myMain(String filename) {
            super(filename,offMenus);
            Menu newMenu= new Menu("ActionMenu");
    
            newMenu.add(sayItem= new MenuItem("Say"));
            newMenu.add(singItem= new MenuItem("Cry"));
    
            appendMenu(newMenu);
    
            showActionItem= new MenuItem("Show Action");
            appendMenuItem(VIEW_MENU,showActionItem);
            
            myButton[] buttons= new myButton[3];
    
            buttons[0]= new myButton("testeditor");
            buttons[1]= new myButton("testeditor2");
            buttons[2]= new myButton("testeditor3");
                
            setUserButtonBar(buttons,true);
            layoutMain();
            // replace 
            
            the exising buttonBar
    
            resize(500,600);
        }
    
        protected Main getEditor(String filename,boolean must) {
            myMain m= new myMain(filename);
            return (Main)m;
        }
        
        public boolean handleEvent(Event evt) {
            if (evt.target==sayItem)
                postFooter("Say something");
            else if (evt.target==singItem)
                postFooter("Cry for something");
            else if (evt.target==showActionItem)
                postFooter("Actions: say and sing");
            else
                return super.handleEvent(evt);
            return true;
        }
    }
    
    // this class implements UserButton interface
    class myButton extends Button implements UserButton {
        private String title;
        public myButton(String title) {
            super(title);
            this.title= title;
        }
    
        Main get_parent() {
            Container c= getParent();
            while ((c instanceof Main)==false) {
                c= c.getParent();
            }
            return (Main)c;
        }        
    
        public void userAction(Event event) {
            
            get_parent().loadFile(title+".java");
        }
    }
    

    To subclass the editor window, users should follow the following steps,