![JAR search and dependency download from the Maven repository](/logo.png)
net.vectorpublish.desktop.vp.UserInterfaceImpl Maven / Gradle / Ivy
/*
* Copyright (c) 2016, Peter Rader. All rights reserved.
* ___ ___ __ ______ __ __ __ __
* | | |.-----..----.| |_ .-----..----.| __ \.--.--.| |--.| ||__|.-----.| |--.
* | | || -__|| __|| _|| _ || _|| __/| | || _ || || ||__ --|| |
* \_____/ |_____||____||____||_____||__| |___| |_____||_____||__||__||_____||__|__|
*
* http://www.gnu.org/licenses/gpl-3.0.html
*/
package net.vectorpublish.desktop.vp;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.WindowEvent;
import java.lang.reflect.InvocationTargetException;
import java.util.Set;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.inject.Inject;
import javax.inject.Named;
import javax.swing.JComponent;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import org.springframework.beans.factory.SmartInitializingSingleton;
import net.vectorpublish.desktop.vp.api.conf.Config;
import net.vectorpublish.desktop.vp.api.layer.Layer;
import net.vectorpublish.desktop.vp.api.ui.MenuBar;
import net.vectorpublish.desktop.vp.api.ui.StatusBar;
import net.vectorpublish.desktop.vp.api.ui.ToolBar;
import net.vectorpublish.desktop.vp.api.ui.UserInterface;
import net.vectorpublish.desktop.vp.api.vpd.VectorPublishNode;
import net.vectorpublish.desktop.vp.ui.Namespace;
/**
* The main window.
*/
@Named
public class UserInterfaceImpl extends UserInterface implements SmartInitializingSingleton, NodeExpandedChangeListener {
/**
* The Namespace configuration-values are stored fore.
*/
private static final Namespace NAMESPACE = Namespace.getNamespace("net.vectorpublish.start", "ui");
/**
* The northern {@link Container} containing all components you expect in
* the upper area except the {@link MenuBar}.
*/
private final Container northContainer = new Container();
/**
* The right side of the {@link #northContainer northern} shall contain
* multiple components. Theese components are in this container.
*/
private final Container northContainerRightSide = new Container();
/**
* The configuration you need to load the configuration-values for.
*
*
* The namespace for the configurationvalues are read is {@link #NAMESPACE}.
*/
@Inject
private final Config c = null;
/**
* The toolbar to use.
*
*
* This {@link ToolBar} usually appear right under the menubar.
*/
@Inject
private final ToolBar toolbar = null;
/**
* The {@link MenuBar} to add to the window.
*/
@Inject
private final MenuBar menuBar = null;
/**
* The constructor specifing the title.
*/
public UserInterfaceImpl() {
super("Vectorpublish");
addWindowListener(new ContextCloseOnWindowCloseListener());
}
@Override
public void addCenterOfNorth(JComponent center) {
northContainer.add(center, BorderLayout.CENTER);
}
/**
* Add all required Components to the northern container.
*/
@PostConstruct
private void addComponentsToNorthContainer() {
northContainer.setLayout(new BorderLayout());
northContainer.add(toolbar, BorderLayout.NORTH);
}
/**
* Add the Containers to the Window.
*/
@PostConstruct
private void addContainers() {
getContentPane().setLayout(new BorderLayout());
getContentPane().add(northContainer, BorderLayout.NORTH);
northContainer.add(northContainerRightSide, BorderLayout.EAST);
GridLayout northernRightLayout = new GridLayout();
northernRightLayout.setRows(1);
northContainerRightSide.setLayout(northernRightLayout);
}
@Override
public void addLeftOfNorth(JComponent left) {
northContainer.add(left, BorderLayout.WEST);
}
/**
* Add the {@link MenuBar} to the Frame.
*/
@PostConstruct
private void addMenu() {
super.setJMenuBar(menuBar);
}
@Override
public void addRightOfNorh(JComponent right) {
northContainerRightSide.add(right);
}
@Override
public void addStatus(StatusBar status) {
getContentPane().add(status, BorderLayout.SOUTH);
}
/**
* Make visible after all {@link PostConstruct} methods has been sucessfully
* invoked.
*/
@Override
public void afterSingletonsInstantiated() {
try {
SwingUtilities.invokeAndWait(new Runnable() {
@Override
public void run() {
setVisible(true);
requestFocus();
}
});
} catch (InvocationTargetException | InterruptedException e) {
throw new RuntimeException(e);
}
}
@Override
public void setCenter(JComponent center) {
getContentPane().add(center, BorderLayout.CENTER);
}
@Override
public void setLayerTree(Layer layer) {
JScrollPane comp = new JScrollPane(layer);
getContentPane().add(comp, BorderLayout.WEST);
doLayout();
repaint();
}
/**
* Read the bounds from configuration and show the component.
*
* @throws Exception
*/
@PostConstruct
public void start() throws Exception {
Rectangle bounds = c.loadBounds(NAMESPACE, "window");
if (bounds == null) {
bounds = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
}
setBounds(bounds);
}
/**
* Stops the execution, called on termination via Spring.
*/
@PreDestroy
public void stop() {
c.storeBounds(NAMESPACE, "window", getBounds());
}
@Override
public void triggerClose() {
this.processWindowEvent(new WindowEvent(this, WindowEvent.WINDOW_CLOSING));
}
@Override
public void expanded(Set node) {
doLayout();
}
}