/**
* Copyright 1999-2009 The Pegadi Team
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* PublicationDialog.java
* Created on September 10, 2000, 5:30 PM
* @author Jørgen Binningsbø <jb@underdusken.no>
*/
package org.pegadi.publicationcontrol;
// pegadi-imports
import de.wannawork.jcalendar.JCalendarComboBox;
import org.pegadi.model.LoginContext;
import org.pegadi.model.Publication;
import org.pegadi.server.Server;
import org.pegadi.util.GridBagConstraints2;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.ResourceBundle;
public class PublicationDialog extends javax.swing.JDialog {
private JLabel header;
private JPanel bottomPanel;
private JButton saveButton;
private JButton abortButton;
private GridBagLayout gbl = new GridBagLayout();
private JLabel nameLabel;
private JTextField nameField;
private JLabel descriptionLabel;
private JLabel releaseDateLabel;
private JCalendarComboBox releaseDateField;
private JLabel printDateLabel;
private JCalendarComboBox printDateField;
private JLabel deadlineTextLabel;
private JCalendarComboBox deadlineTextField;
private JTextArea descriptionField = new JTextArea();
private Publication publication;
private Server ps;
/** UI strings */
private ResourceBundle messages;
private final Logger log = LoggerFactory.getLogger(getClass());
/** Creates new form PublicationDialog */
public PublicationDialog(Frame parent, boolean modal, org.pegadi.server.Server pS, Publication pub) {
super (parent, "", modal);
ImageIcon icon = new ImageIcon(getClass().getResource("/images/pegadi.gif"));
setIconImage(icon.getImage());
messages = ResourceBundle.getBundle("org.pegadi.publicationcontrol.PublicationDialogStrings");
this.setTitle(messages.getString("dialog_title"));
publication = pub;
ps = pS;
jbInit ();
fillPublicationValues();
pack();
setLocationRelativeTo(parent);
setVisible(true);
}
/**
* This method fills the field with
* values from the publications, and
* also adds actionlisteners and such
*/
public void fillPublicationValues() {
nameField.setText(publication.getName());
descriptionField.setText(publication.getDescription());
Calendar release = new GregorianCalendar();
release.setTime(publication.getReleaseDate());
Calendar print = new GregorianCalendar();
print.setTime(publication.getPrintDate());
Calendar deadline = new GregorianCalendar();
deadline.setTime(publication.getDeadlineText());
releaseDateField.setCalendar(release);
printDateField.setCalendar(print);
deadlineTextField.setCalendar(deadline);
}
/** This method is called from within the constructor to
* initialize the form.
*/
private void jbInit () {
Container cp = getContentPane();
cp.setLayout(gbl);
header = new javax.swing.JLabel ();
bottomPanel = new javax.swing.JPanel ();
saveButton = new javax.swing.JButton ();
abortButton = new javax.swing.JButton ();
nameLabel = new javax.swing.JLabel ();
nameField = new javax.swing.JTextField ();
descriptionLabel = new javax.swing.JLabel ();
releaseDateLabel = new javax.swing.JLabel ();
releaseDateField = new JCalendarComboBox();
printDateLabel = new javax.swing.JLabel ();
printDateField = new JCalendarComboBox();
printDateField.setEnabled(false);
deadlineTextLabel = new javax.swing.JLabel ();
deadlineTextField = new JCalendarComboBox();
deadlineTextField.setEnabled(false);
releaseDateField.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
Calendar selectedDate = ((JCalendarComboBox) e.getSource()).getCalendar();
Calendar printDate = (Calendar) selectedDate.clone();
printDate.add(Calendar.DATE, -1);
printDateField.setCalendar(printDate);
Calendar deadlineDate = (Calendar) selectedDate.clone();
deadlineDate.add(Calendar.DATE, -6);
deadlineTextField.setCalendar(deadlineDate);
}
});
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
closeDialog();
}
});
header.setText (messages.getString("dialog_title"));
header.setHorizontalAlignment (javax.swing.SwingConstants.CENTER);
header.setFont (new java.awt.Font ("Dialog", 0, 18));
nameLabel.setText(messages.getString("label_name"));
descriptionLabel.setText(messages.getString("label_description"));
releaseDateLabel.setText(messages.getString("label_pub_date"));
printDateLabel.setText(messages.getString("label_print_date"));
deadlineTextLabel.setText(messages.getString("label_deadline"));
descriptionField.setLineWrap(true);
descriptionField.setRows(3);
saveButton.setText (messages.getString("button_save"));
saveButton.addActionListener (new java.awt.event.ActionListener () {
public void actionPerformed (java.awt.event.ActionEvent evt) {
saveButtonActionPerformed (evt);
}
});
abortButton.setText (messages.getString("button_cancel"));
abortButton.addActionListener (new java.awt.event.ActionListener () {
public void actionPerformed (java.awt.event.ActionEvent evt) {
closeDialog();
}
});
cp.add(header, new GridBagConstraints2(0, 0, 3, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(5,5,15,0), 0, 0));
cp.add(nameLabel, new GridBagConstraints2(0, 2, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0,5,0,0), 0, 0));
cp.add(nameField, new GridBagConstraints2(1, 2, 2, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(0,0,0,5), 0, 0));
cp.add(descriptionLabel, new GridBagConstraints2(0, 3, 3, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0,5,0,5), 0, 0));
cp.add(new JScrollPane(descriptionField), new GridBagConstraints2(0, 4, 3, 3, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0,5,5,5), 0, 0));
cp.add(deadlineTextLabel, new GridBagConstraints2(0, 7, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0,5,0,2), 0, 0));
cp.add(deadlineTextField, new GridBagConstraints2(1, 7, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0,0,0,0), 0, 0));
cp.add(printDateLabel, new GridBagConstraints2(0, 8, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0,5,0,2), 0, 0));
cp.add(printDateField, new GridBagConstraints2(1, 8, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0,0,0,0), 0, 0));
cp.add(releaseDateLabel, new GridBagConstraints2(0, 9, 1, 1, 0.0, 0.0,
GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(0,5,0,2), 0, 0));
cp.add(releaseDateField, new GridBagConstraints2(1, 9, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0,0,0,0), 0, 0));
bottomPanel.add(saveButton);
bottomPanel.add(abortButton);
cp.add(bottomPanel, new GridBagConstraints2(0, 11, 3, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(10,5,5,5), 0, 0));
addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ESCAPE)
closeDialog();
}
});
}
private boolean isValidPublicationName(String name){
return name.matches("\\d{2}-\\d{4}");
}
private void saveButtonActionPerformed (java.awt.event.ActionEvent evt) {
if (nameField.getText().equals("")) {
JOptionPane.showMessageDialog(this, messages.getString("no_name_error_message"),
messages.getString("no_name_error_title"), JOptionPane.ERROR_MESSAGE);
nameField.requestFocus();
return;
} else if (!nameField.getText().equals("Ingen_utgave_nett") && !isValidPublicationName(nameField.getText())){
JOptionPane.showMessageDialog(this,
messages.getString("illegal_name_error_message")+" "+nameField.getText(),
messages.getString("illegal_name_error_title"), JOptionPane.ERROR_MESSAGE);
nameField.requestFocus();
return;
}
if(!( (deadlineTextField.getCalendar().before(printDateField.getCalendar()) ||
deadlineTextField.getCalendar().get(Calendar.YEAR) == printDateField.getCalendar().get(Calendar.YEAR) &&
deadlineTextField.getCalendar().get(Calendar.DAY_OF_YEAR) == printDateField.getCalendar().get(Calendar.DAY_OF_YEAR))
&&
(printDateField.getCalendar().before(releaseDateField.getCalendar()) ||
printDateField.getCalendar().get(Calendar.YEAR) == releaseDateField.getCalendar().get(Calendar.YEAR) &&
printDateField.getCalendar().get(Calendar.DAY_OF_YEAR) == releaseDateField.getCalendar().get(Calendar.DAY_OF_YEAR))
)) {
JOptionPane.showMessageDialog(this, messages.getString("date_conflict_error_message"),
messages.getString("date_conflict_error_title"), JOptionPane.ERROR_MESSAGE);
return;
}
publication.setName(nameField.getText());
publication.setDescription(descriptionField.getText());
publication.setReleaseDate(releaseDateField.getCalendar().getTime());
publication.setPrintDate(printDateField.getCalendar().getTime());
publication.setDeadlineText(deadlineTextField.getCalendar().getTime());
try {
int id = ps.saveOrUpdatePublication(publication, LoginContext.sessionKey);
publication.setId(id);
} catch (Exception ee) {
log.error("Unable to save", ee);
}
setVisible(false);
dispose();
}
private void closeDialog() {
dispose();
}
}