org.jwebgenerator.views.WorkSpaceWindow.java Source code

Java tutorial

Introduction

Here is the source code for org.jwebgenerator.views.WorkSpaceWindow.java

Source

/*
 * Copyright (C) 2014 Nabil Andriantomanga <nabil.arrowbase at gmail.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
 */
package org.jwebgenerator.views;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import org.apache.commons.lang3.StringUtils;
import org.jwebgenerator.business.WebPage;
import org.jwebgenerator.canvas.WorkSpaceCanvas;
import org.jwebgenerator.constants.JWebGeneratorFolder;
import org.jwebgenerator.events.ResourceDragEventManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 
 * Modified or written by nabil.andriantomanga <br />
 * Creation date : 9 mars 2016 <br />
 * Modification date : 9 mars 2016 <br />
 */
public final class WorkSpaceWindow extends JwebGeneratorWindow {

    private static final Logger LOG = LoggerFactory.getLogger(WorkSpaceWindow.class);

    private static final int TOP_PANEL_HEIGHT = 70;

    private WorkSpaceCanvas canvas;

    private JPanel topPanel;

    private JButton addNewWebPageButton;

    public WorkSpaceWindow(String title) throws HeadlessException {
        LOG.info("Creating the workspace panel ...");
        if (StringUtils.isEmpty(title)) {
            setTitle(title);
        }

        final Dimension dimension = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setSize(dimension);

        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        ImageIcon jwebGeneratorIcon = new ImageIcon(JWebGeneratorFolder.ICONS_FOLDER + File.separator + "icon.png");
        setIconImage(jwebGeneratorIcon.getImage());

        getContentPane().add(buildWorkSpaceContent());
        handleUserEvents();
    }

    private void handleUserEvents() {

        new ResourceDragEventManager(canvas);
    }

    /**
     * @return
     */
    private Component buildWorkSpaceContent() {

        final JPanel container = new JPanel(new BorderLayout());

        topPanel = new JPanel();
        topPanel.setPreferredSize(new Dimension(getWidth(), TOP_PANEL_HEIGHT));
        container.add(topPanel, BorderLayout.NORTH);

        topPanel.add(buildTopPanel());

        canvas = new WorkSpaceCanvas();
        canvas.setPreferredSize(new Dimension(2000, 2000));
        JScrollPane workspaceScroller = new JScrollPane(canvas);
        workspaceScroller.setPreferredSize(new Dimension(getWidth(), getHeight() - TOP_PANEL_HEIGHT - 50));
        container.add(workspaceScroller, BorderLayout.SOUTH);
        setJMenuBar(createMenuBar());

        return container;
    }

    /**
     * @return
     */
    private Component buildTopPanel() {

        topPanel = new JPanel();

        addNewWebPageButton = new JButton("Nouvelle page");
        topPanel.add(addNewWebPageButton);
        addNewWebPageButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                WebPage webPage = new WebPage();

                canvas.getWebsite().addResource(webPage);
                canvas.invalidate();
                canvas.repaint();
            }
        });
        return topPanel;
    }

    /**
     * @return
     */
    private JMenuBar createMenuBar() {

        final JMenuBar menuBar = new JMenuBar();
        final JMenu fileMenu = new JMenu("Fichier");

        final JMenuItem exitMenu = new JMenuItem("Quitter");
        fileMenu.add(exitMenu);
        exitMenu.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                System.exit(0);
            }
        });

        menuBar.add(fileMenu);
        return menuBar;
    }
}