/*
 * @author Adam Nelson
 * 
 */
package main_interface;

import com.Ostermiller.Syntax.HighlightedDocument;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
//import hamlet.graph.*;

/**
 * A <a href="http://ostermiller.org/syntax/editor.html">demonstration text editor</a>
 * that uses syntax highlighting.
 */
public class LanguageSelection extends JFrame {
    /** The document holding the text being edited. */
    private static HighlightedDocument _document = new HighlightedDocument();
    
    private static String javaSample =
            "/**\n" +
            " * Simple common test program.\n" +
            " */\n" +
            "public class HelloWorld {\n" +
            "    public static void main(String[] args) {\n" +
            "        // Display the greeting.\n" +
            "        System.out.println(\"Hello World!\");\n" +
            "    }\n" +
            "}\n";

    private static String stepSample = 
            "FUNCTION which_class( \n" +
            "          t: GENERIC \n" +
            "     ): LIST OF STRING; \n" +
            "\n" +
            "    LOCAL \n" +
            "      class_list : LIST OF STRING := []; \n" +
            "      elements   : BAG OF applied_classification_assignment; \n" +
            "    END_LOCAL; \n" +
            "    elements := USEDIN(t, \n" +
            "        'SHIP_STRUCTURES_SCHEMA.APPLIED_CLASSIFICATION_ASSIGNMENT.ITEMS'); \n" +
            "    REPEAT i := 1 TO HIINDEX(elements) BY 1; \n" +
            "      IF elements[i]\\classification_assignment.role.name = \n" +
            "          'class membership' THEN \n" +
            "        class_list := class_list + elements[i]\\classification_assignment. \n" +
            "            assigned_class\\group.name; \n" +
            "      END_IF; \n" +
            "    END_REPEAT; \n" +
            "    RETURN(class_list); \n" +
            "\n" +
            "  END_FUNCTION; -- which_class";

	/** The text pane displaying the document. */
	//private JTextPane _textPane = new JTextPane(getDocument());
         private static JTextPane _textPane;
	
    /**
     * Create a new Demo
     */
    public LanguageSelection() {
        // initial set up that sets the title
        
        /*
        super("Programmer's Editor Demonstration");
        setLocation(50, 50);

        // Create a scroll pane wrapped around the text pane
        JScrollPane scrollPane = new JScrollPane(textPane);
        scrollPane.setPreferredSize(new Dimension(620, 460));

        // Add the components to the frame.
        JPanel contentPane = new JPanel(new BorderLayout());
        contentPane.add(scrollPane, BorderLayout.CENTER);
        setContentPane(contentPane);
        
        // Set up the menu bar.
        JMenu styleMenu = createStyleMenu();
        JMenuBar mb = new JMenuBar();
        mb.add(styleMenu);
        setJMenuBar(mb);
         */

        // Make the window so that it can close the application
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
            public void windowActivated(WindowEvent e) {
                // focus magic
                getTextPane().requestFocus();
            }
        });

        // Put the initial text into the text pane and
        // set it's initial coloring style.
        initDocument();

        // put it all together and show it.
        pack();
    }

    
    public HighlightedDocument getDocument() {
        return _document;
    }
     

    public JTextPane getTextPane() {
        return _textPane;
    }

    public void setTextPane(JTextPane textPane) {
        this._textPane = textPane;
    }

	private class StyleMenuItem extends JRadioButtonMenuItem
			implements ActionListener {
		Object style;
		
		StyleMenuItem(String name, Object style) {
			super(name);
			this.style = style;
			addActionListener(this);
		}
		
		public void actionPerformed(ActionEvent e) 
                {
                     getDocument().setHighlightStyle(style);
                     
                    // System.out.println("Style is: " + style.toString());
                    //Graph.getInstance().setMode(style);
                    
                    if(style == HighlightedDocument.STEP_STYLE)
                    {
                        getTextPane().setText(stepSample);
                    }
                    else if(style == HighlightedDocument.JAVA_STYLE)
                    {
                        getTextPane().setText(javaSample);

                    }
                    else
                    {
                        System.out.println("Unsupported document type.  Using java by default");
                    }
                    
		}
	}

    /**
	 * Create the style menu.
     *
     * @return the style menu.
     */
    public JMenu createStyleMenu() {
        JRadioButtonMenuItem[] items = {
            	new StyleMenuItem("Java",            HighlightedDocument.JAVA_STYLE),
                new StyleMenuItem("STEP",            HighlightedDocument.STEP_STYLE),
            	new StyleMenuItem("C/C++",           HighlightedDocument.C_STYLE),
            	new StyleMenuItem("HTML",            HighlightedDocument.HTML_STYLE),
        };
        
        //Disable the languages we don't support searching for'
        for(int i = 2; i < items.length; i++)
        {
            items[i].setEnabled(false);
        }

        JMenu menu = new JMenu("Language");
    
        ButtonGroup group = new ButtonGroup();
      
        for(int i = 0; i < items.length; i++) {
        	group.add(items[i]);
        	menu.add(items[i]);
        }
        
        //Initialize the list to have java selected
        //items[0].setSelected(true);
        //getDocument().setHighlightStyle(HighlightedDocument.JAVA_STYLE);
        //Graph.getInstance().setMode(HighlightedDocument.JAVA_STYLE);
        return menu;
    }

    /**
     * Initialize the document with some default text and set
     * they initial type of syntax highlighting.
     */
    private void initDocument() {
        
        //getTextPane().setText(javaSample);
    }

    /**
     * Run the demo.
     *
     * @param args ignored
     */
    public static void main(String[] args) {
        // create the demo
        new LanguageSelection();
    }
}
