org.bidib.wizard.mvc.features.view.FeaturesView Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of bidibwizard-client Show documentation
Show all versions of bidibwizard-client Show documentation
jBiDiB BiDiB Wizard Client Application POM
package org.bidib.wizard.mvc.features.view;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Collection;
import java.util.HashSet;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.WindowConstants;
import javax.swing.border.EmptyBorder;
import org.bidib.jbidibc.messages.Feature;
import org.bidib.wizard.api.locale.Resources;
import org.bidib.wizard.client.common.view.WindowUtils;
import org.bidib.wizard.common.model.settings.WizardSettingsInterface;
import org.bidib.wizard.common.utils.WindowPositionUtils;
import org.bidib.wizard.mvc.features.model.FeaturesModel;
import org.bidib.wizard.mvc.features.view.listener.FeaturesViewListener;
import org.bidib.wizard.mvc.features.view.panel.FeaturesPanel;
import org.bidib.wizard.mvc.features.view.panel.listener.FeaturesWriteListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.jgoodies.forms.builder.ButtonBarBuilder;
import com.jgoodies.forms.factories.Paddings;
public class FeaturesView extends JDialog {
private static final Logger LOGGER = LoggerFactory.getLogger(FeaturesView.class);
private static final long serialVersionUID = 1L;
private final Collection listeners = new HashSet();
private final JButton saveButton = new JButton(Resources.getString(getClass(), "save"));
private final JButton cancelButton = new JButton(Resources.getString(getClass(), "cancel"));
private final FeaturesPanel featuresPanel;
private final WizardSettingsInterface wizardSettings;
public FeaturesView(JFrame parent, final FeaturesModel model, final WizardSettingsInterface wizardSettings) {
super(parent);
this.wizardSettings = wizardSettings;
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
cancel();
}
});
setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
setResizable(true);
setTitle(Resources.getString(getClass(), "title", model.getNode()));
JPanel mainPanel = new JPanel(new BorderLayout());
mainPanel.setBorder(new EmptyBorder(Paddings.DIALOG.getBorderInsets()));
String encodedWindowSize = this.wizardSettings.getWindowSize("features");
final Rectangle screenBounds = WindowUtils.getScreenWorkingArea(parent);
Dimension size = new Dimension(600, 500);
Point location = new Point(screenBounds.x, screenBounds.y);
if (encodedWindowSize != null) {
try {
size = WindowPositionUtils.decodeWindowSize(encodedWindowSize);
}
catch (Exception ex) {
LOGGER.warn("Decode window size failed.", ex);
}
try {
location = WindowPositionUtils.decodeWindowPos(encodedWindowSize);
}
catch (Exception ex) {
LOGGER.warn("Decode window position failed.", ex);
}
}
if (size.width > (screenBounds.width - 50)) {
size.width = screenBounds.width - 50;
LOGGER.info("Reduced width: {}", size.width);
}
if (size.height > (screenBounds.height - 50)) {
size.height = screenBounds.height - 50;
LOGGER.info("Reduced height: {}", size.height);
}
if (location.x < 0 || location.x > (screenBounds.x + screenBounds.width)) {
location.x = screenBounds.x;
LOGGER.info("Reduced location.x: {}", location.x);
}
if (location.y < 0 || location.y > (screenBounds.y + screenBounds.height)) {
location.y = screenBounds.y + 10;
LOGGER.info("Reduced location.y: {}", location.y);
}
mainPanel.setPreferredSize(size);
featuresPanel = new FeaturesPanel(model);
JScrollPane scroll = new JScrollPane();
scroll.setViewportView(featuresPanel.getTable());
mainPanel.add(scroll, BorderLayout.CENTER);
featuresPanel.addFeaturesWriteListener(new FeaturesWriteListener() {
@Override
public void write(Collection features) {
fireWrite(features);
}
});
// prepare the buttons
saveButton.addActionListener(evt -> save());
cancelButton.addActionListener(evt -> cancel());
JPanel buttons =
new ButtonBarBuilder().addGlue().addButton(saveButton, cancelButton).border(Paddings.DLU2).build();
mainPanel.add(buttons, BorderLayout.SOUTH);
getContentPane().add(mainPanel);
pack();
setLocation(location);
int buttonWidth = Math.max(saveButton.getWidth(), cancelButton.getWidth());
saveButton.setPreferredSize(new Dimension(buttonWidth, saveButton.getSize().height));
cancelButton.setPreferredSize(new Dimension(buttonWidth, cancelButton.getSize().height));
}
public void addFeaturesViewListener(FeaturesViewListener listener) {
listeners.add(listener);
}
private void save() {
LOGGER.debug("Save the changed features.");
// write the features to the node
featuresPanel.writeFeatures();
// close the dialog
setVisible(false);
fireClose();
}
private void cancel() {
setVisible(false);
fireClose();
}
private void fireClose() {
String encodedWindowSize = WindowPositionUtils.encodedWindowPosition(getLocation(), getSize(), 0);
if (encodedWindowSize != null) {
this.wizardSettings.setWindowSize("features", encodedWindowSize);
}
for (FeaturesViewListener listener : listeners) {
listener.close();
}
}
private void fireWrite(Collection features) {
for (FeaturesViewListener listener : listeners) {
listener.write(features);
}
}
}