/*
 * MIT License
 *
 * Copyright (c) 2020 Jannis Weis
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 */
package ui.toolBar;/*
                    * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved.
                    *
                    * Redistribution and use in source and binary forms, with or without
                    * modification, are permitted provided that the following conditions
                    * are met:
                    *
                    * - Redistributions of source code must retain the above copyright
                    * notice, this list of conditions and the following disclaimer.
                    *
                    * - Redistributions in binary form must reproduce the above copyright
                    * notice, this list of conditions and the following disclaimer in the
                    * documentation and/or other materials provided with the distribution.
                    *
                    * - Neither the name of Oracle or the names of its
                    * contributors may be used to endorse or promote products derived
                    * from this software without specific prior written permission.
                    *
                    * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
                    * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
                    * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
                    * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
                    * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
                    * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
                    * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
                    * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
                    * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
                    * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
                    * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
                    */

/*
 * toolBar.ToolBarDemo.java requires the following additional files:
 * images/Back24.gif
 * images/Forward24.gif
 * images/Up24.gif
 */
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

import ui.ComponentDemo;

public class ToolBarDemo extends JPanel implements ActionListener, ComponentDemo {
    private static final String PREVIOUS = "previous";
    private static final String UP = "up";
    private static final String NEXT = "next";
    private JTextArea textArea;

    private ToolBarDemo() {
        super(new BorderLayout());
    }

    public static void main(final String[] args) {
        ComponentDemo.showDemo(new ToolBarDemo());
    }

    private void addButtons(final JToolBar toolBar) {
        JButton button;
        button = makeNavigationButton(PREVIOUS, "Back to previous something-or-other", "Previous");
        toolBar.add(button);
        toolBar.addSeparator();
        button = makeNavigationButton(UP, "Up to something-or-other", "Up");
        toolBar.add(button);
        toolBar.addSeparator();
        button = makeNavigationButton(NEXT, "Forward to something-or-other", "Next");
        toolBar.add(button);
    }

    private JButton makeNavigationButton(final String actionCommand,
                                         final String toolTipText,
                                         final String altText) {
        // Create and initialize the button.
        JButton button = new JButton();
        button.setActionCommand(actionCommand);
        button.setToolTipText(toolTipText);
        button.addActionListener(this);
        button.setText(altText);

        return button;
    }

    public void actionPerformed(final ActionEvent e) {
        String cmd = e.getActionCommand();
        String description = null;

        // Handle each button.
        if (PREVIOUS.equals(cmd)) { // first button clicked
            description = "taken you to the previous <something>.";
        } else if (UP.equals(cmd)) { // second button clicked
            description = "taken you up one level to <something>.";
        } else if (NEXT.equals(cmd)) { // third button clicked
            description = "taken you to the next <something>.";
        }

        displayResult("If this were a real app, it would have " + description);
    }

    private void displayResult(final String actionDescription) {
        String newline = "\n";
        textArea.append(actionDescription + newline);
        textArea.setCaretPosition(textArea.getDocument().getLength());
    }

    @Override
    public JComponent createComponent() {
        // Create the toolbar.
        JToolBar toolBar = new JToolBar("Demo ToolBar");
        addButtons(toolBar);

        // Create the text area used for output. Request
        // enough space for 5 rows and 30 columns.
        textArea = new JTextArea(5, 30);
        textArea.setEditable(false);
        JScrollPane scrollPane = new JScrollPane(textArea);

        // Lay out the main panel.
        setPreferredSize(new Dimension(450, 130));
        add(toolBar, BorderLayout.PAGE_START);
        add(scrollPane, BorderLayout.CENTER);
        return this;
    }

    @Override
    public String getTitle() {
        return "ToolBar Demo";
    }
}