/**
* 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.
*/
/**
* Create and edit publication parameters.
*
* @author Jørgen Binningsbø <jb@underdusken.no>
*/
package org.pegadi.publicationcontrol;
//rmi -imports
import org.pegadi.disposal.DisposalControl;
import org.pegadi.model.LoginContext;
import org.pegadi.model.Publication;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Calendar;
import java.util.Date;
import java.util.ResourceBundle;
/**
* a class for displaying and changing the name of one given publication.
*/
public class PublicationControl extends JFrame{
private JButton loadButton;
private JButton newButton;
private JButton editButton;
private JButton disposalButton;
private JButton quitButton;
private JMenuBar jMenuBar1 = new JMenuBar();
private JMenu mainMenu = new JMenu();
private JMenuItem quitMenuItem = new JMenuItem();
private JPanel panel;
private JLabel heading;
private JPanel bottompanel;
private JTextField year;
private JScrollPane scroller;
private JList list;
private DefaultListModel listModel;
private ButtonGroup buttonGroup;
private JRadioButton yearButton;
/**
* the list of publications we are controlling
*/
private java.util.List<Publication> publications;
/** UI strings */
ResourceBundle messages;
private final Logger log = LoggerFactory.getLogger(getClass());
/**
* constructor of the frame.
* arranges all the buttons and GUI elements, and adds listeners to them.
*/
public PublicationControl() {
super();
messages = ResourceBundle.getBundle("org.pegadi.publicationcontrol.PublicationControlStrings");
setTitle(messages.getString("app_name"));
panel = new JPanel();
bottompanel = new JPanel();
jMenuBar1 = new JMenuBar();
mainMenu = new JMenu();
mainMenu.setText(messages.getString("menu_file"));
quitMenuItem = new JMenuItem();
quitMenuItem.setText(messages.getString("action_close"));
quitMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(87, java.awt.event.KeyEvent.CTRL_MASK, false));
quitMenuItem.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
dispose();
}
});
jMenuBar1.add(mainMenu);
mainMenu.add(quitMenuItem);
setJMenuBar(jMenuBar1);
loadButton = new JButton(messages.getString("button_refresh"));
newButton = new JButton(messages.getString("button_new"));
/**
* sets ut the radiobuttons for changing between active and
* yearly list of publications
*/
JRadioButton activeButton = new JRadioButton(messages.getString("button_active"));
activeButton.setActionCommand("Active");
activeButton.setSelected(true);
yearButton = new JRadioButton(messages.getString("button_year"));
yearButton.setActionCommand("year");
buttonGroup = new ButtonGroup();
buttonGroup.add(activeButton);
buttonGroup.add(yearButton);
RadioListener myListener = new RadioListener();
yearButton.addActionListener(myListener);
activeButton.addActionListener(myListener);
/**
* the heading of the applicatoin
*/
heading = new JLabel(messages.getString("label_publications"));
heading.setFont(new java.awt.Font ("Dialog", 0, 18));
listModel = new DefaultListModel();
list = new JList(listModel);
scroller = new JScrollPane(list);
list.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(MouseEvent e) {
list_mouseClicked(e);
disposalButton.setEnabled(true);
}
});
/**
* a panel grouping toghether the year-functions
*/
int y = Calendar.getInstance().get(Calendar.YEAR);
year = new JTextField(String.valueOf(y));
year.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
updateList();
}
});
year.addFocusListener(new FocusAdapter() {
public void focusGained(FocusEvent e) {
selectYearButton();
updateList();
}
});
JPanel yearPanel = new JPanel();
yearPanel.setLayout(new BoxLayout(yearPanel, BoxLayout.X_AXIS));
yearPanel.add(yearButton);
yearPanel.add(year);
yearPanel.add(loadButton);
Box activePanel = Box.createHorizontalBox();
activePanel.add(activeButton);
activePanel.add(Box.createHorizontalGlue());
/**
* the top-panel
*/
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
Box b = Box.createHorizontalBox();
b.add(heading);
b.add(Box.createHorizontalGlue());
panel.add(b);
panel.add(Box.createVerticalStrut(10));
panel.add(activePanel);
panel.add(yearPanel);
/**
* the quit button
*/
quitButton = new JButton(messages.getString("action_close"));
/**
* the edit button
*/
editButton = new JButton(messages.getString("button_edit"));
/**
* the disposal button
*/
disposalButton = new JButton(messages.getString("button_disposal"));
disposalButton.setEnabled(false);
/**
* the bottom panel
*/
bottompanel.add(newButton);
bottompanel.add(editButton);
bottompanel.add(disposalButton);
bottompanel.add(quitButton);
/**
* finally, adds everything to the content pane.
*/
getContentPane().setLayout(new BorderLayout());
getContentPane().add(panel, BorderLayout.NORTH);
getContentPane().add(scroller, BorderLayout.CENTER);
getContentPane().add(bottompanel, BorderLayout.SOUTH);
/**
* sets up listeners for all the components
*/
loadButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
updateList();
}
});
editButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
editPublication();
}
});
newButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
newPublication();
}
});
quitButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dispose();
}
});
disposalButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
disposal();
}
});
updateList();
}
/**
* Selects the year radion button
*/
private void selectYearButton() {
yearButton.setSelected(true);
}
/**
* Called when the 'edit' action is triggered.
*/
protected void editPublication() {
this.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
if ( ! (list.isSelectionEmpty() )) {
int index = list.getSelectedIndex();
PublicationDialog pubFrame = new PublicationDialog(this,
true,
LoginContext.server,
publications.get(index));
updateList();
} else {
log.debug("no selection");
}
this.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
}
/**
* Creates a new publication.
*/
protected void newPublication() {
Publication pub = new Publication();
pub.setDeadlineText(new Date());
pub.setPrintDate(new Date());
pub.setReleaseDate(new Date());
PublicationDialog pubFrame = new PublicationDialog(this, true, LoginContext.server, pub);
updateList();
}
protected void disposal() {
this.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
if ( ! (list.isSelectionEmpty() )) {
int index = list.getSelectedIndex();
DisposalControl control = new DisposalControl(publications.get(index), this);
updateList();
} else {
log.debug("no selection");
}
this.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
}
/**
* this method checks which radiobutton is selected, and
* then updates the list of publications accordingly
*/
public void updateList() {
if (buttonGroup.getSelection().getActionCommand().equals("Active")) {
try {
publications = LoginContext.server.getActivePublications(LoginContext.sessionKey);
} catch (Exception ee) {
log.error("error - couldn't fetch active publications", ee);
}
} else {
String stringyear = year.getText();
try {
publications = LoginContext.server.getPublicationsByYear(new Integer(stringyear), LoginContext.sessionKey);
} catch (Exception ee) {
log.error("error - couldn't fetch publications for this year", ee);
}
}
listModel.removeAllElements();
for (Publication p : publications) {
// Don't add the no_publication publication
if (p.getId() != 9999) listModel.addElement(p);
}
}
void list_mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2 && SwingUtilities.isLeftMouseButton(e)) {
editPublication();
}
}
/**
* this listener updates the list of publications when the user
* chooses between
* "active" and "yearly" listings.
*/
class RadioListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
updateList();
}
}
}