/**
* 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.
*/
package org.pegadi.sources;
import org.pegadi.model.LoginContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.event.EventListenerList;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.rmi.RemoteException;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.ResourceBundle;
public class SourcePanel extends JPanel {
GridBagLayout gridBag = new GridBagLayout();
Source source;
JLabel mainHeader;
TabbedPanel tabbedPanel;
ResourceBundle strings;
JButton saveButton;
JButton exitButton;
JButton deleteButton;
private final Logger log = LoggerFactory.getLogger(getClass());
public SourcePanel(Source source) {
this.source=source;
strings = ResourceBundle.getBundle("org.pegadi.sources.SourcePanelStrings");
tabbedPanel = new TabbedPanel();
mainHeader = new JLabel(strings.getString("mainHeader.edit"));
mainHeader.setFont(new Font("SansSerif", Font.PLAIN, 18));
setLayout(gridBag);
GridBagConstraints constraints = new GridBagConstraints();
// Toolbar
JToolBar tb = createToolBar();
constraints = new GridBagConstraints();
constraints.gridwidth=GridBagConstraints.REMAINDER;
constraints.gridheight=1;
constraints.fill=GridBagConstraints.NONE;
constraints.anchor=GridBagConstraints.NORTHWEST;
constraints.ipadx=10;
constraints.ipady=10;
gridBag.setConstraints(tb,constraints);
add(tb);
// Header
constraints = new GridBagConstraints();
constraints.gridwidth=GridBagConstraints.REMAINDER;
constraints.gridheight=1;
constraints.fill=GridBagConstraints.NONE;
constraints.anchor=GridBagConstraints.WEST;//NORTHWEST;
constraints.ipadx=10;
constraints.ipady=10;
constraints.insets = new Insets(5,10,5,5);
gridBag.setConstraints(mainHeader,constraints);
add(mainHeader);
// tabbedPanel
constraints = new GridBagConstraints();
constraints.gridwidth=GridBagConstraints.REMAINDER;
constraints.gridheight=1;
constraints.fill=GridBagConstraints.NONE;
constraints.anchor=GridBagConstraints.NORTHWEST;
constraints.ipadx=10;
constraints.ipady=10;
gridBag.setConstraints(tabbedPanel,constraints);
add(tabbedPanel);
updateGUI(source);
}
public void addExitListener(ActionListener a) {
listenerList.add(ActionListener.class, a);
}
public void fireActionListeners(ActionEvent e) {
Object[] listeners = listenerList.getListenerList();
for (Object listener : listeners) {
if (listener instanceof ActionListener) {
((ActionListener) listener).actionPerformed(e);
}
}
}
public JToolBar createToolBar() {
JToolBar tb = new JToolBar();
saveButton = new JButton();
saveButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
saveButton_actionPerformed(e);
}
});
saveButton.setIcon(new ImageIcon(getClass().getResource(strings.getString("saveButton.icon"))));
saveButton.setMargin(new Insets(0,0,0,0));
saveButton.setToolTipText(strings.getString("saveButton.tooltip"));
deleteButton = new JButton();
deleteButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
deleteButton_actionPerformed(e);
}
});
deleteButton.setIcon(new ImageIcon(getClass().getResource(strings.getString("removeButton.icon"))));
deleteButton.setMargin(new Insets(0,0,0,0));
deleteButton.setToolTipText(strings.getString("deleteButton.tooltip"));
exitButton = new JButton();
exitButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ActionEvent event = new ActionEvent(source,0,"Sourcebutton pressed");
fireActionListeners(event);
}
});
exitButton.setIcon(new ImageIcon(getClass().getResource(strings.getString("exitButton.icon"))));
exitButton.setMargin(new Insets(0,0,0,0));
exitButton.setToolTipText(strings.getString("exitButton.tooltip"));
tb.add(saveButton);
tb.add(deleteButton);
tb.addSeparator();
tb.add(exitButton);
return tb;
}
class SourceInfoPanel extends JPanel {
NamePanel namePanel;
AddressPanel addressPanel;
ContactInfoPanel contactInfoPanel;
GridBagLayout gridBag = new GridBagLayout();
public SourceInfoPanel() {
namePanel = new NamePanel();
addressPanel = new AddressPanel();
contactInfoPanel = new ContactInfoPanel();
setLayout(gridBag);
GridBagConstraints constraints = new GridBagConstraints();
constraints.gridwidth=GridBagConstraints.REMAINDER;
constraints.gridheight=1;
constraints.fill=GridBagConstraints.BOTH;
constraints.anchor=GridBagConstraints.NORTH;
constraints.ipadx=10;
constraints.ipady=10;
constraints.gridy=GridBagConstraints.RELATIVE;
gridBag.setConstraints(namePanel,constraints);
add(namePanel);
// contactInfoPanel
constraints = new GridBagConstraints();
constraints.gridwidth=GridBagConstraints.REMAINDER;
constraints.gridheight=1;
constraints.fill=GridBagConstraints.BOTH;
constraints.anchor=GridBagConstraints.NORTH;
constraints.ipadx=10;
constraints.ipady=10;
constraints.gridy=GridBagConstraints.RELATIVE;
gridBag.setConstraints(contactInfoPanel,constraints);
add(contactInfoPanel);
// addressPanel
constraints = new GridBagConstraints();
constraints.gridwidth=GridBagConstraints.REMAINDER;
constraints.gridheight=1;
constraints.fill=GridBagConstraints.BOTH;
constraints.anchor=GridBagConstraints.NORTH;
constraints.ipadx=10;
constraints.ipady=10;
constraints.gridy=GridBagConstraints.RELATIVE;
gridBag.setConstraints(addressPanel,constraints);
add(addressPanel);
}
public void updateSource(Source source) {
namePanel.updateSource(source);
addressPanel.updateSource(source);
contactInfoPanel.updateSource(source);
}
public void updateGUI(Source source) {
namePanel.updateGUI(source);
addressPanel.updateGUI(source);
contactInfoPanel.updateGUI(source);
}
}
class AddressPanel extends JPanel {
JTextArea streetAddress = new JTextArea(2,15);
JTextField postNumber = new JTextField(5);
JTextField postAddress = new JTextField(15);
JLabel streetAddressLabel = new JLabel(strings.getString("streetAddress") +": ");
JLabel postNumberLabel = new JLabel(strings.getString("postNumber") +": ");
JLabel postAddressLabel = new JLabel(strings.getString("postAddress") +": ");
GridBagLayout gridBag = new GridBagLayout();
public AddressPanel() {
setBorder(BorderFactory.createTitledBorder(strings.getString("addressBorder")));
setLayout(gridBag);
GridBagConstraints constraints = new GridBagConstraints();
streetAddress.setWrapStyleWord(true);
streetAddress.setLineWrap(true);
// streetAddressLabel
constraints.gridwidth=1;
constraints.gridheight=1;
constraints.fill=GridBagConstraints.NONE;
constraints.anchor=GridBagConstraints.NORTHEAST;
gridBag.setConstraints(streetAddressLabel,constraints);
add(streetAddressLabel);
// streetAddress
constraints = new GridBagConstraints();
JScrollPane jsp = new JScrollPane(streetAddress);
constraints.gridwidth=GridBagConstraints.REMAINDER;
constraints.gridheight=1;
constraints.fill=GridBagConstraints.NONE;
gridBag.setConstraints(jsp,constraints);
add(jsp);
// postNumberLabel
constraints = new GridBagConstraints();
constraints.gridwidth=1;
constraints.gridheight=1;
constraints.fill=GridBagConstraints.NONE;
constraints.anchor=GridBagConstraints.NORTHEAST;
gridBag.setConstraints(postNumberLabel,constraints);
add(postNumberLabel);
// postNumber
constraints = new GridBagConstraints();
constraints.gridwidth=GridBagConstraints.REMAINDER;
constraints.gridheight=1;
constraints.fill=GridBagConstraints.NONE;
constraints.anchor=GridBagConstraints.NORTHWEST;
gridBag.setConstraints(postNumber,constraints);
add(postNumber);
// postAddressLabel
constraints = new GridBagConstraints();
constraints.gridwidth=1;
constraints.gridheight=1;
constraints.fill=GridBagConstraints.NONE;
constraints.anchor=GridBagConstraints.NORTHEAST;
gridBag.setConstraints(postAddressLabel,constraints);
add(postAddressLabel);
// postAddress
constraints = new GridBagConstraints();
constraints.gridwidth=GridBagConstraints.REMAINDER;
constraints.gridheight=1;
constraints.fill=GridBagConstraints.BOTH;
gridBag.setConstraints(postAddress,constraints);
add(postAddress);
}
public void updateSource(Source source) {
source.setAddress(streetAddress.getText());
source.setPostNumber(postNumber.getText());
source.setPostAddress(postAddress.getText());
}
public void updateGUI(Source source) {
streetAddress.setText(source.getAddress());
postNumber.setText(source.getPostNumber());
postAddress.setText(source.getPostAddress());
}
}
class ContactInfoPanel extends JPanel {
JButton newButton = new JButton();
JButton removeButton = new JButton();
JButton editButton = new JButton();
String addString;
String changeString;
JLabel emailLabel = new JLabel(strings.getString("email") +": ");
JLabel urlLabel = new JLabel(strings.getString("url") +": ");
JTextField email = new JTextField(20);
JTextField url = new JTextField(20);
JLabel numbersLabel = new JLabel(strings.getString("number.label") +":");
PhoneNumberTableModel model;
PhoneNumber[] numbers = new PhoneNumber[0];
JTable table;
GridBagLayout gridBag = new GridBagLayout();
PhoneNumber inEdit;
public ContactInfoPanel() {
model = new PhoneNumberTableModel(numbers);
table = new JTable(model);
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
table.getColumnModel().getColumn(0).setMaxWidth(80);
table.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e){
contactInfoEdit_actionPerformed(e);
}
});
JComboBox comboBox = new JComboBox();
for (PhoneNumberType phoneNumberType : PhoneNumberType.values()) comboBox.addItem(phoneNumberType);
table.getColumnModel().getColumn(0).setCellEditor(new DefaultCellEditor(comboBox));
setBorder(BorderFactory.createTitledBorder(strings.getString("contactBorder")));
newButton.setIcon(new ImageIcon(getClass().getResource(strings.getString("newButton.icon"))));
newButton.setToolTipText(strings.getString("contactpanel.newbutton.tooltip"));
removeButton.setIcon(new ImageIcon(getClass().getResource(strings.getString("removeButton.icon"))));
removeButton.setToolTipText(strings.getString("contactpanel.removebutton.tooltip"));
editButton.setIcon(new ImageIcon(getClass().getResource(strings.getString("editButton.icon"))));
editButton.setToolTipText(strings.getString("contactpanel.editbutton.tooltip"));
newButton.setMargin(new Insets(0,0,0,0));
removeButton.setMargin(new Insets(0,0,0,0));
editButton.setMargin(new Insets(0,0,0,0));
newButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
newButton_actionPerformed(ae);
}
});
removeButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
removeButton_actionPerformed(ae);
}
});
editButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
editButton_actionPerformed(ae);
}
});
setLayout(gridBag);
GridBagConstraints constraints = new GridBagConstraints();
// Phone numbers label
constraints.gridwidth=2;
constraints.gridheight=1;
constraints.fill=GridBagConstraints.NONE;
constraints.anchor=GridBagConstraints.SOUTHWEST;
gridBag.setConstraints(numbersLabel,constraints);
add(numbersLabel);
// new/remove buttons
constraints = new GridBagConstraints();
constraints.gridwidth=GridBagConstraints.REMAINDER;
constraints.gridheight=1;
constraints.fill=GridBagConstraints.NONE;
constraints.anchor=GridBagConstraints.NORTHEAST;
JPanel p = new JPanel();
p.setBorder(new EmptyBorder(0,0,0,0));
p.setLayout(new FlowLayout(FlowLayout.LEFT));
p.add(newButton);
p.add(removeButton);
p.add(editButton);
gridBag.setConstraints(p,constraints);
add(p);
// phonenumbertable
constraints = new GridBagConstraints();
constraints.gridwidth=GridBagConstraints.REMAINDER;
constraints.gridheight=1;
constraints.fill=GridBagConstraints.HORIZONTAL;
constraints.anchor=GridBagConstraints.NORTHWEST;
table.setPreferredScrollableViewportSize(new Dimension(300,70));
JScrollPane jsp = new JScrollPane(table);
gridBag.setConstraints(jsp,constraints);
add(jsp);
// EmailLabel
constraints = new GridBagConstraints();
constraints.gridwidth=1;
constraints.gridheight=1;
constraints.fill=GridBagConstraints.NONE;
constraints.anchor=GridBagConstraints.NORTHWEST;
gridBag.setConstraints(emailLabel,constraints);
add(emailLabel);
// email
constraints = new GridBagConstraints();
constraints.gridwidth=GridBagConstraints.REMAINDER;
constraints.gridheight=1;
constraints.fill=GridBagConstraints.HORIZONTAL;
constraints.anchor=GridBagConstraints.NORTHWEST;
gridBag.setConstraints(email,constraints);
add(email);
// URLLabel
constraints = new GridBagConstraints();
constraints.gridwidth=1;
constraints.gridheight=1;
constraints.fill=GridBagConstraints.NONE;
constraints.anchor=GridBagConstraints.NORTHWEST;
gridBag.setConstraints(urlLabel,constraints);
add(urlLabel);
// url
constraints = new GridBagConstraints();
constraints.gridwidth=GridBagConstraints.REMAINDER;
constraints.gridheight=1;
constraints.fill=GridBagConstraints.HORIZONTAL;
constraints.anchor=GridBagConstraints.NORTHWEST;
gridBag.setConstraints(url,constraints);
add(url);
}
void contactInfoEdit_actionPerformed(MouseEvent e) {
if(e.getClickCount() != 2)
return;
Object source = e.getSource();
int rowIndex = ((JTable)source).rowAtPoint(e.getPoint());
JTable table = (JTable) source;
Component o = this;
while(!(o instanceof JFrame))
o = o.getParent();
ContactInfoDialog d = new ContactInfoDialog((JFrame) o, strings);
d.setPhoneNumberType((PhoneNumberType) table.getValueAt(rowIndex,0));
d.setNumber((String) table.getValueAt(rowIndex,1));
d.setDescription((String) table.getValueAt(rowIndex,2));
d.pack();
d.setVisible(true);
if(!d.accepted)
return;
table.setValueAt(d.getPhoneNumberType(),rowIndex,0);
table.setValueAt(d.getNumber(),rowIndex,1);
table.setValueAt(d.getDescription(),rowIndex,2);
}
void editButton_actionPerformed(ActionEvent e) {
int rowIndex = table.getSelectedRow();
if(rowIndex == -1)
return;
Component o = this;
while(!(o instanceof JFrame))
o = o.getParent();
ContactInfoDialog d = new ContactInfoDialog((JFrame) o, strings);
d.setPhoneNumberType((PhoneNumberType) table.getValueAt(rowIndex,0));
d.setNumber((String) table.getValueAt(rowIndex,1));
d.setDescription((String) table.getValueAt(rowIndex,2));
d.pack();
d.setVisible(true);
if(!d.accepted)
return;
table.setValueAt(d.getPhoneNumberType(),rowIndex,0);
table.setValueAt(d.getNumber(),rowIndex,1);
table.setValueAt(d.getDescription(),rowIndex,2);
}
void newButton_actionPerformed(ActionEvent e) {
Component o = this;
while(!(o instanceof JFrame))
o = o.getParent();
ContactInfoDialog d = new ContactInfoDialog((JFrame) o, strings);
d.pack();
d.setVisible(true);
if(!d.accepted)
return;
model.addPhoneNumber(new PhoneNumber(d.getPhoneNumberType(), d.getNumber(), d.getDescription()));
model.fireTableDataChanged();
}
void removeButton_actionPerformed(ActionEvent e) {
int selected = table.getSelectedRow();
if(selected >= 0)
model.removePhoneNumber(selected);
}
public void updateSource(Source source) {
source.setPhoneNumbers(model.getPhoneNumbers());
source.setEmail(email.getText());
source.setURL(url.getText());
}
public void updateGUI(Source source) {
model.setPhoneNumbers(source.getPhoneNumbers());
email.setText(source.getEmail());
url.setText(source.getURL());
}
private class PhoneNumberTableModel extends javax.swing.table.AbstractTableModel {
List<PhoneNumber> numbers;
String[] columns;
public PhoneNumberTableModel(PhoneNumber[] numbers) {
this.numbers = new ArrayList<PhoneNumber>();
columns = new String[3];
columns[0] = strings.getString("number.typeColumn");
columns[1] = strings.getString("number.numberColumn");
columns[2] = strings.getString("number.descriptionColumn");
this.numbers.addAll(Arrays.asList(numbers));
}
public int getRowCount() {
return numbers.size();
}
public int getColumnCount() {
return 3;
}
public Object getValueAt(int row,int col) {
PhoneNumber n = numbers.get(row);
switch(col) {
case 0:
return n.getType();
case 1:
return n.getNumber();
case 2:
return n.getDescription();
}
return null;
}
public boolean isCellEditable(int row, int col) {
return false;
}
public void setValueAt(Object value, int row, int col) {
switch(col) {
case 0:
getPhoneNumber(row).setType((PhoneNumberType)value);
break;
case 1:
getPhoneNumber(row).setNumber(value.toString());
break;
case 2:
getPhoneNumber(row).setDescription(value.toString());
break;
}
fireTableCellUpdated(row, col);
}
public String getColumnName(int col) {
return columns[col];
}
public void setPhoneNumbers(List<PhoneNumber> numbers) {
this.numbers = numbers;
fireTableDataChanged();
}
public PhoneNumber getPhoneNumber(int row) {
return numbers.get(row);
}
public List<PhoneNumber> getPhoneNumbers() {
return numbers;
}
public void addPhoneNumber(PhoneNumber number) {
numbers.add(number);
fireTableDataChanged();
}
public void removePhoneNumber(int number) {
numbers.remove(number);
fireTableDataChanged();
}
}
}
private class NamePanel extends JPanel {
JTextField name = new JTextField(23);
JTextField position = new JTextField(23);
JTextField organization = new JTextField(10);
JLabel nameLabel = new JLabel(strings.getString("name") +": ");
JLabel positionLabel = new JLabel(strings.getString("position") +": ");
JLabel organizationLabel = new JLabel(strings.getString("organization") +": ");
int orgID;
GridBagLayout gridBag = new GridBagLayout();
JButton orgButton = new JButton("..");
JButton newOrgButton = new JButton("+");
JButton noOrgButton = new JButton("-");
JDialog orgSearchDialog;
JDialog newSourceDialog;
public NamePanel() {
setBorder(BorderFactory.createTitledBorder(strings.getString("nameBorder")));
orgButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
orgButton_actionPerformed();
}
});
newOrgButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
newOrgButton_actionPerformed();
}
});
noOrgButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
noOrgButton_actionPerformed();
}
});
orgButton.setMargin(new Insets(2,6,2,6));
newOrgButton.setMargin(new Insets(2,6,2,6));
noOrgButton.setMargin(new Insets(2,6,2,6));
Font fnt = new Font("SansSerif", Font.PLAIN, 10);
orgButton.setFont(fnt);
newOrgButton.setFont(fnt);
noOrgButton.setFont(fnt);
organization.setColumns(15);
organization.setEditable(false);
setLayout(gridBag);
addLabel(nameLabel);
addTextField(name);
addLabel(positionLabel);
addTextField(position);
addLabel(organizationLabel);
GridBagConstraints constraints;
// Organization text field
constraints = new GridBagConstraints();
constraints.gridwidth=1;
constraints.gridheight=1;
constraints.fill=GridBagConstraints.NONE;
constraints.anchor=GridBagConstraints.NORTHWEST;
organization.setHorizontalAlignment(SwingConstants.LEFT);
gridBag.setConstraints(organization,constraints);
add(organization);
// Organization select button
constraints = new GridBagConstraints();
constraints.gridwidth=1;
constraints.gridheight=1;
constraints.fill=GridBagConstraints.NONE;
constraints.anchor=GridBagConstraints.NORTHWEST;
gridBag.setConstraints(orgButton,constraints);
orgButton.setToolTipText(strings.getString("namepanel.orgbutton.tooltip"));
add(orgButton);
// New organization button
constraints = new GridBagConstraints();
constraints.gridwidth=1;
constraints.gridheight=1;
constraints.fill=GridBagConstraints.NONE;
constraints.anchor=GridBagConstraints.NORTHWEST;
gridBag.setConstraints(newOrgButton,constraints);
newOrgButton.setToolTipText(strings.getString("namepanel.neworgbutton.tooltip"));
add(newOrgButton);
// No organization button
constraints = new GridBagConstraints();
constraints.gridwidth=GridBagConstraints.REMAINDER;
constraints.gridheight=1;
constraints.fill=GridBagConstraints.NONE;
constraints.anchor=GridBagConstraints.NORTHWEST;
gridBag.setConstraints(noOrgButton,constraints);
noOrgButton.setToolTipText(strings.getString("namepanel.noorgbutton.tooltip"));
add(noOrgButton);
}
public void updateSource(Source source) {
source.setName(name.getText());
source.setPosition(position.getText());
source.setOrganizationID(orgID);
}
public void updateGUI(Source source) {
name.setText(source.getName());
position.setText(source.getPosition());
orgID = source.getOrganizationID();
if(orgID>0) {
Source orgSource = null;
try {
orgSource = LoginContext.server.getSourceByID(orgID, LoginContext.sessionKey);
}
catch (java.rmi.RemoteException re) {
log.error("Error getting sourceByID " + orgID + " from server", re);
}
organization.setText(orgSource.getName());
}
}
public void addLabel(JLabel label) {
GridBagConstraints constraints = new GridBagConstraints();
constraints.gridwidth=1;
constraints.gridheight=1;
constraints.fill=GridBagConstraints.NONE;
constraints.anchor=GridBagConstraints.NORTHEAST;
gridBag.setConstraints(label,constraints);
add(label);
}
public void addTextField(JTextField textField) {
GridBagConstraints constraints = new GridBagConstraints();
constraints.gridwidth=GridBagConstraints.REMAINDER;
constraints.gridheight=1;
constraints.fill=GridBagConstraints.NONE;
constraints.anchor=GridBagConstraints.NORTHWEST;
textField.setHorizontalAlignment(SwingConstants.LEFT);
gridBag.setConstraints(textField,constraints);
add(textField);
}
void setOrganization(Source source) {
organization.setText(source.getName());
orgID=source.getID();
}
void orgButton_actionPerformed() {
orgSearchDialog = new JDialog();
SourceSearchPanel ssp = new SourceSearchPanel();
ssp.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Source source = (Source)e.getSource();
setOrganization(source);
closeSearchDialog();
}
});
orgSearchDialog.getContentPane().add(ssp);
orgSearchDialog.pack();
orgSearchDialog.setVisible(true);
}
void newOrgButton_actionPerformed() {
newSourceDialog = new JDialog();
SourcePanel sp = new SourcePanel(new Source());
sp.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(e.getSource() == null)
return;
Source s = (Source)e.getSource();
closeNewSourceDialog();
setOrganization(s);
}
});
newSourceDialog.getContentPane().add(sp);
newSourceDialog.pack();
newSourceDialog.setVisible(true);
}
void noOrgButton_actionPerformed() {
orgID=-1;
organization.setText("");
}
void closeSearchDialog() {
orgSearchDialog.dispose();
}
void closeNewSourceDialog() {
newSourceDialog.dispose();
}
}
private class TabbedPanel extends JPanel {
SourceInfoPanel sourceInfoPanel;
NotesPanel notesPanel;
CategoryPanel categoryPanel;
ContactPanel contactPanel;
public TabbedPanel() {
JTabbedPane tabbedPane = new JTabbedPane();
notesPanel = new NotesPanel();
categoryPanel = new CategoryPanel();
contactPanel = new ContactPanel();
sourceInfoPanel = new SourceInfoPanel();
tabbedPane.addTab(strings.getString("tab.sourceinfo"),null,sourceInfoPanel);
JScrollPane jsp = new JScrollPane(categoryPanel);
JPanel p = new JPanel();
p.setLayout(new BorderLayout());
p.add(jsp,BorderLayout.CENTER);
p.setPreferredSize(new Dimension(300,300));
tabbedPane.addTab(strings.getString("tab.notes"),null,notesPanel);
tabbedPane.addTab(strings.getString("tab.categories"),null,p);
tabbedPane.addTab(strings.getString("tab.contacts"),null,contactPanel);
add(tabbedPane);
}
public void updateSource(Source source) {
sourceInfoPanel.updateSource(source);
notesPanel.updateSource(source);
categoryPanel.updateSource(source);
contactPanel.updateSource(source);
}
public void updateGUI(Source source) {
sourceInfoPanel.updateGUI(source);
notesPanel.updateGUI(source);
categoryPanel.updateGUI(source);
contactPanel.updateGUI(source);
}
}
private class NotesPanel extends JPanel {
JTextArea notes = new JTextArea();
public NotesPanel() {
JScrollPane jsp = new JScrollPane(notes);
setLayout(new GridLayout(1,1));
add(jsp);
notes.setWrapStyleWord(true);
notes.setLineWrap(true);
}
public void updateSource(Source source) {
source.setNotes(notes.getText());
}
public void updateGUI(Source source) {
notes.setText(source.getNotes());
}
}
private class CategoryPanel extends JPanel {
java.util.List<Category> categories;
java.util.List<Category> memberships;
JButton newCategoryButton;
JButton editCategoryButton;
JPanel categoriesPane;
List<CategoryContainer> containers = new ArrayList<CategoryContainer>();
public CategoryPanel() {
addCategoriesList();
}
private void addCategoriesList() {
removeAll();
try {
categories = LoginContext.server.getSourceCategories(LoginContext.sessionKey);
} catch(java.rmi.RemoteException e) {
log.error("Error retrieving source categories from server", e);
}
setLayout(new BorderLayout());
if(categories != null){
categoriesPane = new JPanel();
categoriesPane.setLayout(new GridLayout(categories.size(),1));
for(int i = 0;i<categories.size();i++)
addCategory(categories,i,2);
add(categoriesPane, BorderLayout.NORTH);
}
JPanel buttonPanel = new JPanel();
newCategoryButton = new JButton(strings.getString("newCategoryButton.text"),
new ImageIcon(getClass().getResource(strings.getString("newCategoryButton.icon"))));
newCategoryButton.setToolTipText(strings.getString("newCategoryButton.tooltip"));
newCategoryButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
newCategoryButton_actionPerformed(e);
}
});
editCategoryButton = new JButton(strings.getString("editCategoryButton.text"),
new ImageIcon(getClass().getResource(strings.getString("editCategoryButton.icon"))));
editCategoryButton.setToolTipText(strings.getString("editCategoryButton.tooltip"));
editCategoryButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
editCategoryButton_actionPerformed(e);
}
});
buttonPanel.add(newCategoryButton);
buttonPanel.add(editCategoryButton);
add(buttonPanel, BorderLayout.SOUTH);
validate();
}
public void updateSource(Source source) {
java.util.List<Category> members = getMembers(containers);
try {
LoginContext.server.updateSourceCategoryMemberships(source.getID(),members, LoginContext.sessionKey);
} catch (java.rmi.RemoteException re) {
log.error("Error updating sourcecategory memberships with server", re);
}
}
private java.util.List<Category> getMembers(java.util.List<CategoryContainer> containers) {
java.util.List<Category> upMemb = new ArrayList<Category>();
for (CategoryContainer container : containers) {
if (container.getCheckBox().isSelected())
upMemb.add(container.getCategory());
}
return upMemb;
}
public void updateGUI(Source source) {
try {
memberships = LoginContext.server.getCategoriesBySource(source.getID(), LoginContext.sessionKey);
} catch(java.rmi.RemoteException e) {
log.error("Error retrieving categories by source {} from server", source.getID(), e);
}
updateMemberships();
}
public void updateMemberships() {
for (CategoryContainer container : containers) {
container.updateCheckBox(memberships);
}
}
public void addCategory(java.util.List<Category> categories, int i, int width) {
if(categories == null || categories.size() == 0) // Failsafe...
return;
JCheckBox checkBox = new JCheckBox(categories.get(i).getName());
CategoryContainer container = new CategoryContainer(categories.get(i),checkBox);
containers.add(container);
categoriesPane.add(checkBox);
}
public void newCategoryButton_actionPerformed(ActionEvent e) {
Component o = this;
while(!(o instanceof JFrame))
o = o.getParent();
CategoryDialog d = new CategoryDialog((JFrame) o, strings);
d.pack();
d.setVisible(true);
if(!d.wasAccepted())
return;
Category cat = new Category();
cat.setName(d.getCategory());
try {
LoginContext.server.addSourceCategory(cat, LoginContext.sessionKey);
} catch (RemoteException rx) {
JOptionPane.showMessageDialog(this,"Could not add category due to server error!");
log.error("Error: Requesting bad location in document", rx);
}
addCategoriesList();
updateGUI(source);
}
public void editCategoryButton_actionPerformed(ActionEvent e) {
Component o = this;
while(!(o instanceof JFrame))
o = o.getParent();
EditCategoryDialog d = new EditCategoryDialog((JFrame) o, strings, categories.toArray(new Category[categories.size()]));
d.pack();
d.setVisible(true);
addCategoriesList();
updateGUI(source);
}
private class CategoryContainer {
Category category;
JCheckBox checkBox;
public CategoryContainer(Category category,JCheckBox checkBox) {
this.category=category;
this.checkBox=checkBox;
}
public void updateCheckBox(java.util.List<Category> memberships) {
for (Category membership : memberships) {
if (membership.getID() == category.getID())
checkBox.setSelected(true);
}
}
public Category getCategory() {
return category;
}
public JCheckBox getCheckBox() {
return checkBox;
}
}
}
class ContactPanel extends JPanel {
List<Contact> contacts = new ArrayList<Contact>();
ContactTableModel model;
JTable table;
JScrollPane jsp;
Source source;
ActionListener contactDialogListener;
JButton newButton,editButton,removeButton;
public ContactPanel() {
model = new ContactTableModel(contacts);
table = new JTable(model);
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
table.getColumnModel().getColumn(0).setMaxWidth(150);
table.getColumnModel().getColumn(0).setPreferredWidth(100);
table.setPreferredScrollableViewportSize(new Dimension(50,50));
table.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2 && SwingUtilities.isLeftMouseButton(e))
editButton.doClick();
}
});
jsp = new JScrollPane(table);
contactDialogListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
cd_actionPerformed(e);
}
};
setLayout(new BorderLayout());
JPanel buttons = new JPanel();
buttons.setLayout(new FlowLayout(FlowLayout.LEFT));
newButton = new JButton();
newButton.setIcon(new ImageIcon(getClass().getResource(strings.getString("newButton.icon"))));
newButton.setToolTipText(strings.getString("newButton.tooltip"));
newButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
newButton_actionPerformed(e);
}
});
buttons.add(newButton);
removeButton = new JButton();
removeButton.setIcon(new ImageIcon(getClass().getResource(strings.getString("removeButton.icon"))));
removeButton.setToolTipText(strings.getString("removeButton.tooltip"));
removeButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
removeButton_actionPerformed(e);
}
});
buttons.add(removeButton);
editButton = new JButton();
editButton.setIcon(new ImageIcon(getClass().getResource(strings.getString("editButton.icon"))));
editButton.setToolTipText(strings.getString("editButton.tooltip"));
editButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
editButton_actionPerformed(e);
}
});
buttons.add(editButton);
newButton.setMargin(new Insets(0,0,0,0));
removeButton.setMargin(new Insets(0,0,0,0));
editButton.setMargin(new Insets(0,0,0,0));
add(buttons,BorderLayout.NORTH);
add(jsp,BorderLayout.CENTER);
}
void newButton_actionPerformed(ActionEvent e) {
Contact newContact = new Contact(source.getID());
ContactDialog cd = new ContactDialog(newContact);
cd.setNew(true);
cd.addActionListener(contactDialogListener);
cd.pack();
Component o = this;
while(!(o instanceof Frame))
o = o.getParent();
Point p = o.getLocation();
cd.setLocation(p.x + 50, p.y + 50);
cd.setVisible(true);
}
void removeButton_actionPerformed(ActionEvent e) {
int selected = table.getSelectedRow();
if(selected >= 0) {
model.removeContact(selected);
}
}
void editButton_actionPerformed(ActionEvent e) {
int row = table.getSelectedRow();
if(row<0)
return;
Contact contact = model.getContact(row);
ContactDialog cd = new ContactDialog(contact);
cd.setNew(false);
cd.addActionListener(contactDialogListener);
cd.pack();
Component o = this;
while(!(o instanceof Frame))
o = o.getParent();
Point p = o.getLocation();
cd.setLocation(p.x + 50, p.y + 50);
cd.setVisible(true);
}
public void updateGUI(Source source) {
this.source=source;
contacts = source.getContacts();
model.setContacts(contacts);
}
public void updateSource(Source source) {
source.setContacts(model.getContacts());
}
void cd_actionPerformed(ActionEvent e) {
ContactDialog cd = (ContactDialog)e.getSource();
if(cd.isNew())
model.addContact(cd.getContact());
model.fireTableDataChanged();
}
class ContactTableModel extends javax.swing.table.AbstractTableModel {
List<Contact> contacts;
DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM);
String[] columns = {"Date","Notes"};
public ContactTableModel(List<Contact> contacts) {
this.contacts = new ArrayList<Contact>(contacts);
columns[0] = strings.getString("contact.dateColumn");
columns[1] = strings.getString("contact.notesColumn");
}
public int getRowCount() {
return contacts.size();
}
public int getColumnCount() {
return 2;
}
public Object getValueAt(int row,int col) {
Contact c = contacts.get(row);
switch(col) {
case 0: {
return dateFormat.format(c.getDate());
}
case 1: {
String notes = c.getNotes();
int i = notes.indexOf("\n");
if(i>=0)
return notes.substring(0,i);
else
return c.getNotes();
}
}
return null;
}
public String getColumnName(int col) {
return columns[col];
}
public void setContacts(List<Contact> contacts) {
this.contacts = new ArrayList<Contact>(contacts);
fireTableDataChanged();
}
public Contact getContact(int row) {
return contacts.get(row);
}
public List<Contact> getContacts() {
return contacts;
}
public void addContact(Contact contact) {
contacts.add(0, contact);
fireTableDataChanged();
}
public void removeContact(int contact) {
contacts.remove(contact);
fireTableDataChanged();
}
}
private class ContactDialog extends JDialog {
JPanel center = new JPanel();
EventListenerList listenerList = new EventListenerList();
JLabel mainHeader = new JLabel(strings.getString("contact.dialog.header"));
JLabel dateLabel = new JLabel(strings.getString("contact.dialog.date") +": ");
JLabel notesLabel = new JLabel(strings.getString("contact.dialog.notes") +": ");
JLabel date = new JLabel();
JTextArea notes = new JTextArea(6,20);
Contact contact;
boolean isNew = true;
public ContactDialog(Contact contact) {
super();
this.contact=contact;
setModal(true);
mainHeader.setFont(new Font("SansSerif", Font.PLAIN, 18));
if(isNew()) {
mainHeader.setText(strings.getString("contact.dialog.header.new"));
}
notes.setText(contact.getNotes());
DateFormat df = DateFormat.getDateInstance(DateFormat.LONG);
date.setText(df.format(contact.getDate()));
getContentPane().setLayout(new BorderLayout());
getContentPane().add(mainHeader,BorderLayout.NORTH);
JScrollPane jsp = new JScrollPane(notes);
jsp.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
notes.setWrapStyleWord(true);
notes.setLineWrap(true);
GridBagLayout gridBag = new GridBagLayout();
center.setLayout(gridBag);
GridBagConstraints constraints = new GridBagConstraints();
constraints.gridwidth=1;
constraints.fill=GridBagConstraints.NONE;
constraints.anchor=GridBagConstraints.NORTHWEST;
constraints.ipadx=10;
constraints.ipady=10;
gridBag.setConstraints(dateLabel,constraints);
center.add(dateLabel);
// date
constraints = new GridBagConstraints();
constraints.gridwidth=GridBagConstraints.REMAINDER;
constraints.fill=GridBagConstraints.NONE;
constraints.anchor=GridBagConstraints.NORTHWEST;
constraints.ipadx=10;
constraints.ipady=10;
gridBag.setConstraints(date,constraints);
center.add(date);
// notesLabel
constraints = new GridBagConstraints();
constraints.gridwidth=1;
constraints.fill=GridBagConstraints.NONE;
constraints.anchor=GridBagConstraints.NORTHWEST;
constraints.ipadx=10;
constraints.ipady=10;
gridBag.setConstraints(notesLabel,constraints);
center.add(notesLabel);
// notes (scrollpane)
constraints = new GridBagConstraints();
constraints.gridwidth=GridBagConstraints.REMAINDER;
constraints.fill=GridBagConstraints.NONE;
constraints.anchor=GridBagConstraints.NORTHWEST;
constraints.ipadx=10;
constraints.ipady=10;
gridBag.setConstraints(jsp,constraints);
center.add(jsp);
// Add the center panel
getContentPane().add(center,BorderLayout.CENTER);
JPanel bottom = new JPanel();
bottom.setLayout(new FlowLayout());
JButton saveButton = new JButton(strings.getString("button.ok"));
saveButton.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent ae) {
saveButton_actionPerformed(ae);
}
});
JButton cancelButton = new JButton(strings.getString("button.cancel"));
cancelButton.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent ae) {
cancelButton_actionPerformed(ae);
}
});
bottom.add(saveButton);
bottom.add(cancelButton);
// Add the bottom panel
getContentPane().add(bottom,BorderLayout.SOUTH);
}
public Contact getContact() {
return contact;
}
void setNew(boolean isNew) {
this.isNew = isNew;
if(isNew())
mainHeader.setText(strings.getString("contact.dialog.header.new"));
else
mainHeader.setText(strings.getString("contact.dialog.header"));
}
boolean isNew() {
return isNew;
}
void saveButton_actionPerformed(ActionEvent e) {
contact.setNotes(notes.getText());
fireContactSaved(contact);
dispose();
}
void cancelButton_actionPerformed(ActionEvent e) {
dispose();
}
public void addActionListener(ActionListener listener) {
listenerList.add(ActionListener.class, listener);
}
public void fireContactSaved(Object contact) {
Object[] listeners = listenerList.getListenerList();
for (Object listener : listeners) {
if (listener instanceof ActionListener) {
ActionEvent event = new ActionEvent(this, 0, "Contact saved");
((ActionListener) listener).actionPerformed(event);
}
}
}
}
}
void deleteButton_actionPerformed(ActionEvent e) {
int answer = JOptionPane.showConfirmDialog(this, strings.getString("deleteSourceQuery.label"), strings.getString("deleteSourceQuery.title"), JOptionPane.YES_NO_OPTION);
if(answer == JOptionPane.NO_OPTION)
return;
try {
LoginContext.server.deleteSource(source, LoginContext.sessionKey);
} catch (java.rmi.RemoteException rx) {
JOptionPane.showMessageDialog(this,"Could not delete source due to server error!");
log.error("Error deleting source " + source.getID() + " from server", rx);
}
// Done to trigger the exit button.
ActionEvent event = new ActionEvent(source,0,"Sourcebutton pressed");
fireActionListeners(event);
}
void saveButton_actionPerformed(ActionEvent e) {
ActionEvent event = new ActionEvent(source,0,"Sourcebutton pressed");
saveSource();
}
public void saveSource() {
updateSource();
int id =-1;
try {
id = LoginContext.server.saveSource(source, LoginContext.sessionKey);
}
catch (Exception e) {
log.error("Error saving source {} to server", source.getID(), e);
}
if(id>0) {
source.setID(id);
mainHeader.setText(strings.getString("mainHeader.edit"));
}
}
public void updateSource() {
tabbedPanel.updateSource(source);
}
public void updateGUI(Source source) {
if(source.getID()==-1)
mainHeader.setText(strings.getString("mainHeader.new"));
tabbedPanel.updateGUI(source);
}
public void addActionListener(ActionListener listener) {
listenerList.add(ActionListener.class, listener);
}
private class ContactInfoDialog extends JDialog {
JFrame frame;
ResourceBundle strings;
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints constraints = new GridBagConstraints();
Container container = this.getContentPane();
boolean accepted = false;
JComboBox typeCombo = new JComboBox();
JTextField numberField = new JTextField();
JTextField descField = new JTextField();
public ContactInfoDialog(JFrame frame, ResourceBundle strings) {
super(frame, strings.getString("contactinfodialog.title.text"), true);
this.strings = strings;
createUI();
Point p = frame.getLocation();
this.setLocation(p.x + 125, p.y+200);
}
private void createUI() {
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
JLabel typeLabel = new JLabel(strings.getString("contactinfodialog.type.text"));
JLabel numberLabel = new JLabel(strings.getString("contactinfodialog.number.text"));
JLabel descLabel = new JLabel(strings.getString("contactinfodialog.desc.text"));
// FIXME: This should be fetched from the lauguage properties file! (handegar)
typeCombo.addItem(PhoneNumberType.Telefon);
typeCombo.addItem(PhoneNumberType.Mobil);
typeCombo.addItem(PhoneNumberType.Telefaks);
typeCombo.addItem(PhoneNumberType.Annet);
typeCombo.setSelectedIndex(0);
JButton okButton = new JButton(strings.getString("button.ok"));
JButton cancelButton = new JButton(strings.getString("button.cancel"));
container.setLayout(gridbag);
constraints.fill = GridBagConstraints.HORIZONTAL;
constraints.insets = new Insets(5, 5, 0, 5);
add(typeLabel, 0,0,0);
add(typeCombo, 1,0,1);
add(numberLabel, 0,1,0);
add(numberField, 1,1,1);
add(descLabel, 0,2,0);
add(descField, 1,2,1);
constraints.insets = new Insets(5, 5, 5, 5);
constraints.anchor = GridBagConstraints.WEST;
add(okButton, 0, 3, 0);
constraints.anchor = GridBagConstraints.EAST;
add(cancelButton, 1, 3, 0);
okButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
okPerformed();
}
});
cancelButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
cancelPerformed();
}
});
}
private void add(Component component, int x, int y, int weight) {
constraints.gridx = x;
constraints.gridy = y;
constraints.weightx = weight;
gridbag.setConstraints(component, constraints);
container.add(component);
}
private void cancelPerformed() {
this.accepted = false;
ContactInfoDialog.this.dispose();
}
private void okPerformed() {
this.accepted = true;
ContactInfoDialog.this.dispose();
}
public boolean wasAccepted() {
return this.accepted;
}
public PhoneNumberType getPhoneNumberType() {
return (PhoneNumberType) this.typeCombo.getSelectedItem();
}
public String getNumber() {
return this.numberField.getText();
}
public String getDescription() {
return this.descField.getText();
}
public void setPhoneNumberType(PhoneNumberType type) {
this.typeCombo.setSelectedItem(type);
}
public void setNumber(String number) {
this.numberField.setText(number);
}
public void setDescription(String desc) {
this.descField.setText(desc);
}
}
private class EditCategoryDialog extends JDialog {
JFrame frame;
ResourceBundle strings;
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints constraints = new GridBagConstraints();
Container container = this.getContentPane();
Category categories[];
JComboBox categoryList = new JComboBox();
public EditCategoryDialog(JFrame frame, ResourceBundle strings, Category categories[]) {
super(frame, strings.getString("editcategorydialog.title.text"), true);
this.categories = categories; // Fetching this as a parameter. No point in bothering the server...
this.strings = strings;
createUI();
Point p = frame.getLocation();
this.setLocation(p.x + 125, p.y+200);
}
private void populateCategoryList() {
if(categories == null || categories.length == 0){
Category cat = new Category();
cat.setName(strings.getString("editcategorydialog.empty.text"));
categoryList.addItem(cat.getName());
return;
}
for (Category category : categories) categoryList.addItem(category.getName());
}
private void createUI() {
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
JButton okButton = new JButton(strings.getString("button.finished"));
JButton renameButton = new JButton(strings.getString("button.rename"),
new ImageIcon(getClass().getResource(strings.getString("editButton.icon"))));
JButton deleteButton = new JButton(strings.getString("button.delete"),
new ImageIcon(getClass().getResource(strings.getString("removeButton.icon"))));
JPanel buttonPanel = new JPanel();
buttonPanel.add(renameButton);
buttonPanel.add(deleteButton);
populateCategoryList();
container.setLayout(gridbag);
constraints.fill = GridBagConstraints.HORIZONTAL;
constraints.insets = new Insets(15, 5, 5, 5);
add(categoryList, 0,0,0);
constraints.insets = new Insets(5, 5, 5, 5);
constraints.anchor = GridBagConstraints.CENTER;
add(buttonPanel,0,1,0);
constraints.insets = new Insets(15, 5, 5, 5);
constraints.anchor = GridBagConstraints.WEST;
add(okButton, 0, 2, 0);
okButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
okPerformed();
}
});
renameButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
renamePerformed();
}
});
deleteButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
deletePerformed();
}
});
}
private void add(Component component, int x, int y, int weight) {
constraints.gridx = x;
constraints.gridy = y;
constraints.weightx = weight;
gridbag.setConstraints(component, constraints);
container.add(component);
}
private void okPerformed() {
EditCategoryDialog.this.dispose();
}
private void renamePerformed() {
if(categories == null || categories.length == 0) //failsafe
return;
Component o = this;
while(!(o instanceof JFrame))
o = o.getParent();
CategoryDialog d = new CategoryDialog((JFrame) o, strings);
d.setCategory((String) categoryList.getSelectedItem());
d.pack();
d.setVisible(true);
if(!d.wasAccepted())
return;
Category cat = categories[categoryList.getSelectedIndex()];
cat.setName(d.getCategory());
try {
LoginContext.server.updateSourceCategory(cat, LoginContext.sessionKey);
} catch (java.rmi.RemoteException rx) {
JOptionPane.showMessageDialog(this,"Could not update category due to server error!");
rx.printStackTrace();
}
}
private void deletePerformed() {
if(categories == null || categories.length == 0)
return;
int answer = JOptionPane.showConfirmDialog(this, strings.getString("editcategorydialog.delete.label"), strings.getString("editcategorydialog.delete.title"), JOptionPane.YES_NO_OPTION);
if(answer == JOptionPane.NO_OPTION)
return;
try {
LoginContext.server.deleteSourceCategory(categories[categoryList.getSelectedIndex()], LoginContext.sessionKey);
} catch (java.rmi.RemoteException rx) {
JOptionPane.showMessageDialog(this,"Could not delete category due to server error!");
rx.printStackTrace();
}
}
}
private class CategoryDialog extends JDialog {
JFrame frame;
ResourceBundle strings;
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints constraints = new GridBagConstraints();
Container container = this.getContentPane();
boolean accepted = false;
JTextField categoryField = new JTextField();
public CategoryDialog(JFrame frame, ResourceBundle strings) {
super(frame, strings.getString("categorydialog.title.text"), true);
this.strings = strings;
createUI();
Point p = frame.getLocation();
this.setLocation(p.x + 125, p.y+200);
}
private void createUI() {
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
JLabel label = new JLabel(strings.getString("categorydialog.label"));
JButton okButton = new JButton(strings.getString("button.ok"));
JButton cancelButton = new JButton(strings.getString("button.cancel"));
categoryField.setColumns(20);
container.setLayout(gridbag);
constraints.fill = GridBagConstraints.HORIZONTAL;
constraints.insets = new Insets(5, 5, 0, 5);
add(label, 0,0,0);
add(categoryField, 1,0,1);
constraints.insets = new Insets(5, 5, 5, 5);
constraints.anchor = GridBagConstraints.WEST;
add(okButton, 0, 3, 0);
constraints.anchor = GridBagConstraints.EAST;
add(cancelButton, 1, 3, 0);
okButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
okPerformed();
}
});
cancelButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
cancelPerformed();
}
});
}
private void add(Component component, int x, int y, int weight) {
constraints.gridx = x;
constraints.gridy = y;
constraints.weightx = weight;
gridbag.setConstraints(component, constraints);
container.add(component);
}
public String getCategory() {
return this.categoryField.getText();
}
public void setCategory(String s) {
this.categoryField.setText(s);
}
private void cancelPerformed() {
this.accepted = false;
CategoryDialog.this.dispose();
}
private void okPerformed() {
this.accepted = this.categoryField.getText().length() != 0;
CategoryDialog.this.dispose();
}
public boolean wasAccepted() {
return this.accepted;
}
}
}