net.vectorpublish.desktop.vp.FilesImpl 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.Component;
import java.io.File;
import java.util.LinkedHashSet;
import java.util.Set;
import javax.annotation.PostConstruct;
import javax.inject.Inject;
import javax.inject.Named;
import javax.swing.JComponent;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import net.vectorpublish.desktop.vp.api.io.Files;
import net.vectorpublish.desktop.vp.api.ui.DocumentSelectionChangeListener;
import net.vectorpublish.desktop.vp.api.ui.FileChangedListener;
import net.vectorpublish.desktop.vp.api.ui.UserInterface;
import net.vectorpublish.desktop.vp.api.vpd.DocumentNode;
import net.vectorpublish.desktop.vp.pd.official.DrawPanel;
@Named
public class FilesImpl extends Files implements ChangeListener, FileChangedListener {
@Inject
private final UserInterface ui = null;
private final Set docs = new LinkedHashSet<>();
@Inject
private final DocumentSelectionChangeListener[] listener = null;
public FilesImpl() {
super();
addChangeListener(this);
}
@Override
public void addDocument(DocumentNode doc) {
docs.add(doc);
final JComponent component = doc.getComponent();
// addTab will trigger stateChanged.
super.addTab(doc.getTitle(), component);
super.setSelectedIndex(super.getTabCount() - 1);
}
@Override
public void fileChanged(DocumentNode doc, File file) {
final int count = getTabCount();
for (int index = 0; index < count; index++) {
final Component tabComponentAt = getComponentAt(index);
if (tabComponentAt == doc.getComponent()) {
setTitleAt(index, doc.getTitle());
return;
}
}
}
private void notify(DocumentNode selected) {
for (final DocumentSelectionChangeListener fileSelectionChangeListener : listener) {
fileSelectionChangeListener.notifyDocumentSelected(selected);
}
}
@Override
public void repaintCurrent() {
getSelectedComponent().repaint();
}
@PostConstruct
private void setup() {
ui.setCenter(this);
}
@Override
public void stateChanged(ChangeEvent event) {
final DrawPanel selectedComponent = (DrawPanel) getSelectedComponent();
for (final DocumentNode doc : docs) {
if (doc.getComponent() == selectedComponent) {
notify(doc);
}
}
}
}