001    // GraphLab Project: http://graphlab.sharif.edu
002    // Copyright (C) 2008 Mathematical Science Department of Sharif University of Technology
003    // Distributed under the terms of the GNU General Public License (GPL): http://www.gnu.org/licenses/
004    
005    package graphlab.samples.ui.texteditor.myplugin;
006    
007    import graphlab.platform.core.BlackBoard;
008    import graphlab.platform.plugin.PluginInterface;
009    import graphlab.samples.ui.texteditor.myplugin.actions.Utils;
010    import graphlab.ui.UI;
011    import graphlab.ui.UIUtils;
012    import org.xml.sax.SAXException;
013    
014    import javax.swing.*;
015    import javax.swing.event.CaretEvent;
016    import javax.swing.event.CaretListener;
017    import javax.swing.text.BadLocationException;
018    import java.io.IOException;
019    
020    public class Init implements PluginInterface {
021        public void init(BlackBoard blackboard) {
022            //set the look and feel
023            try {
024                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
025            } catch (Exception e) {
026                e.printStackTrace();
027            }
028    
029            //load xml file
030            BlackBoard blackBoard = new BlackBoard();
031            UI u = new UI(blackBoard, true);
032            try {
033                u.loadXML("/myui.xml", u.getClass());
034            } catch (IOException e) {
035                e.printStackTrace();
036            } catch (SAXException e) {
037                e.printStackTrace();
038            }
039    
040            //gets the component which is created when loading status bar.
041            //according to myui.xml it's id is "statusbar"
042            final JLabel lbl = (JLabel) UIUtils.getComponent(blackBoard, "statusbar");
043            final JTextArea editor = Utils.getMainEditor(blackBoard);
044    
045            //sets the lbl to show current row and colomn of caret in text editor
046            editor.addCaretListener(new CaretListener() {
047                public void caretUpdate(CaretEvent e) {
048                    try {
049                        int caretPosition = editor.getCaretPosition();
050                        int line = editor.getLineOfOffset(caretPosition);
051                        int col = caretPosition - editor.getLineStartOffset(line);
052                        lbl.setText(line + ":" + col);
053                    } catch (BadLocationException e1) {
054                        e1.printStackTrace();
055                    }
056                }
057            });
058    
059        }
060    }