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.samples.ui.texteditor.myplugin.actions.Utils;
009    import graphlab.ui.UI;
010    import graphlab.ui.UIUtils;
011    import org.xml.sax.SAXException;
012    
013    import javax.swing.*;
014    import javax.swing.event.CaretEvent;
015    import javax.swing.event.CaretListener;
016    import javax.swing.text.BadLocationException;
017    import java.io.IOException;
018    
019    /**
020     * The main class of GraphLab notepad sample.
021     * <p/>
022     * see http://graphlab.sharif.ir/trac/wiki/XMLBasedUI
023     * @author Azin Azadi aazadi@gmail.com
024     */
025    public class UISample {
026        public static void main(String[] args) throws IOException, SAXException, IllegalAccessException, UnsupportedLookAndFeelException, InstantiationException, ClassNotFoundException {
027            //set the look and feel
028            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
029    
030            //load xml file
031            BlackBoard blackBoard = new BlackBoard();
032            UI u = new UI(blackBoard, true);
033            u.loadXML("myui.xml", UISample.class);
034    
035            //gets the component which is created when loading status bar.
036            //according to myui.xml it's id is "statusbar"
037            final JLabel lbl = (JLabel) UIUtils.getComponent(blackBoard, "statusbar");
038            final JTextArea editor = Utils.getMainEditor(blackBoard);
039    
040            //sets the lbl to show current row and colomn of caret in text editor
041            editor.addCaretListener(new CaretListener() {
042                public void caretUpdate(CaretEvent e) {
043                    try {
044                        int caretPosition = editor.getCaretPosition();
045                        int line = editor.getLineOfOffset(caretPosition);
046                        int col = caretPosition - editor.getLineStartOffset(line);
047                        lbl.setText(line + ":" + col);
048                    } catch (BadLocationException e1) {
049                        e1.printStackTrace();
050                    }
051                }
052            });
053            u.getGFrame().setVisible(true);
054        }
055    }