package de.duehl.swing.ui.start.tabbars;

/*
 * Copyright 2017 Christian Dühl. All rights reserved.
 *
 * This program is free software. You can redistribute it and/or
 * modify it under the same terms as perl:
 *
 * general:  http://dev.perl.org/licenses/
 * GPL:      http://dev.perl.org/licenses/gpl1.html
 * artistic: http://dev.perl.org/licenses/artistic.html
 */

import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridLayout;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;

import de.duehl.basics.io.Charset;
import de.duehl.swing.ui.highlightingeditor.HighlightingEditor;
import de.duehl.swing.ui.highlightingeditor.syntax.SyntaxHighlighting;
import de.duehl.swing.ui.highlightingeditor.syntax.highlighting.HighlightingFactory;
import de.duehl.swing.ui.tabs.Tabulator;
import de.duehl.swing.ui.tabs.bars.editors.TabBarWithEditors;
import de.duehl.swing.ui.tabs.elements.TabElementsWithEditor;

/**
 * Diese Klasse testet die TabBar-Elemente.
 *
 * @version 1.01     2017-11-23
 * @author Christian Dühl
 */

public class TabBarWithJavaEditorTest {

    /** Die zu testenden Reiter. */
    private TabBarWithEditors tabBar;

    /** Konstruktor. */
    public TabBarWithJavaEditorTest() {
        tabBar = new TabBarWithEditors();
        SwingUtilities.invokeLater(() -> createAndShowGUI());
    }

    private void createAndShowGUI() {
        JFrame frame = new JFrame("TabBarWithEditorTabs Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new GridLayout(0,  1));

        frame.add(createTabBar());

        frame.setPreferredSize(new Dimension(800, 600));
        frame.pack();
        frame.setVisible(true);
    }

    private Component createTabBar() {
        addTab("c:/temp/swing_test/HighlightingEditor.java");
        addTab("c:/temp/swing_test/ButtonBar.java");
        addTab("c:/temp/swing_test/EditorForButtonBar.java");

        tabBar.focusSelectedEditor();

        return tabBar.getComponent();
    }

    private void addTab(String filename) {
        HighlightingEditor editor = new HighlightingEditor();
        editor.useMonospaceFont();
        editor.setFontSize(20);
        editor.useSyntaxHighlighting(generateSyntaxHighlighting());
        editor.openFile(filename, Charset.UTF_8);

        TabElementsWithEditor tabElements = tabBar.addTab(editor);
        Tabulator tabulator = tabElements.getTabulator();
        editor.addChangeStateDisplayer(tabulator);
    }

    private SyntaxHighlighting generateSyntaxHighlighting() {
        HighlightingFactory factory = new HighlightingFactory();
        String name = HighlightingFactory.JAVA;
        SyntaxHighlighting highlighting = factory.create(name);
        return highlighting;
    }

    /** Startpunkt des Programms, Parameter werden ignoriert. */
    public static void main(String[] args) {
        new TabBarWithJavaEditorTest();
    }

}

