/** * 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.swing; import no.dusken.common.model.Person; import org.pegadi.model.LoginContext; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Collections; import java.util.List; import java.util.Locale; import java.util.ResourceBundle; import static org.pegadi.util.PersonUtils.getInitials; /** * An extended JComboBox allowing users to choose a person from. * Note: In order to set the selected user without fireing an actionevent, * use setSelectedUser instead of setSelectedItem. * * @version $Revision$, $Date$ */ public class PersonChooser extends JComboBox { public static final int ALL = 0; public static final int JOURNALISTS = 3; public static final int PHOTOGRAPHERS = 4; private static ResourceBundle strings = ResourceBundle.getBundle("org.pegadi.swing.PersonChooserStrings", Locale.getDefault()); private final Logger log = LoggerFactory.getLogger(getClass()); public PersonChooser() { this(0); } public PersonChooser(int order) { this(order, null); } public PersonChooser(int order, Person selected) { super(); populate(order, selected); } // Support for external persons Person external = makeExternalUser("", "", "", ""); JFrame ownerFrame = null; private boolean nodialog = false; private Object previous_choice = null; private void userHasMadeAChoice() { if (nodialog) return; if (ownerFrame != null) { Person selected = this.getSelectedUser(); if (selected != null && selected.getId() == null) { this.setCursor(new Cursor(Cursor.WAIT_CURSOR)); ExternalDialog exdia = new ExternalDialog(external, ownerFrame, strings); exdia.pack(); this.setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); exdia.setVisible(true); Person result = exdia.getUser(); if (result == null) { setSelectedItem(previous_choice); } else { setSelectedUser(result); } } } previous_choice = this.getSelectedItem(); } public void addExternalOption() { Component parent = this.getParent(); while (parent != null && !(parent instanceof JFrame)) { parent = parent.getParent(); } if (parent == null) { parent = new JFrame(); // dummy-frame } addExternalOption((JFrame) parent); } public void addExternalOption(JFrame owner) { if (ownerFrame == null) { this.addItem(external); } ownerFrame = owner; this.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { userHasMadeAChoice(); } }); } private static Person makeExternalUser(String firstname, String lastname, String initials, String email) { return new Person(null, firstname.trim(), lastname.trim(), "N/A", email.trim()) { public String toString() { return (this.getName() + " " + strings.getString("external")).trim(); } }; } public void setSelectedUser(Person selected) { if (selected == null) return; if (selected.getId() == null) { nodialog = true; this.removeItem(external); external = makeExternalUser(selected.getFirstname(), selected.getSurname(), getInitials(selected), selected.getEmailAddress()); if (ownerFrame == null) { ownerFrame = new JFrame(); // dummy-frame } this.addItem(external); super.setSelectedItem(external); nodialog = false; } else { super.setSelectedItem(selected); } } public Person getSelectedUser() { Object selected = this.getSelectedItem(); if (selected instanceof Person) { return (Person) selected; } else { return null; } } public Person getUserAt(int index) { Object user = this.getItemAt(index); if(user instanceof Person) { return (Person) user; } else { return null; } } private void populate(int order, Person selected) { try { boolean inList = false; this.addItem(strings.getString("choose_user")); List<Person> users = LoginContext.server.getUsers(false, LoginContext.sessionKey); // First add the most important users java.util.List<Person> vips; if (order == JOURNALISTS) { vips = LoginContext.server.getJournalists(LoginContext.sessionKey); } else if (order == PHOTOGRAPHERS) { vips = LoginContext.server.getPhotographers(LoginContext.sessionKey); } else { vips = Collections.emptyList(); } for (Person vip : vips) { this.addItem(vip); if (selected != null && selected.equals(vip)) { inList = true; } // remove the vip from the main user-list for (int j = 0; j < users.size(); j++) { if (vip.equals(users.get(j))) users.remove(users.get(j)); } } this.addItem("---"); // Add all users not yet added to the list for (Person user : users) { if (user == null) continue; this.addItem(user); if (selected != null && selected.equals(user)) { inList = true; } } // If selected user hasn't showed up - add him or her at the end. if (selected == null || selected.getId() == null) { this.setSelectedIndex(0); } else { if (!inList && selected.getId() != null) { this.addItem(selected); } if (selected.getId() == null) { this.addExternalOption(); } this.setSelectedUser(selected); } } catch (Exception e) { log.error("Error populating PersonChooser.", e); } } private static class ExternalDialog extends JDialog { ResourceBundle strings; public ExternalDialog(Person user, JFrame frame, ResourceBundle strings) { super(frame, strings.getString("dialogtitle"), true); this.strings = strings; createUI(); fname.setText(user.getFirstname()); lname.setText(user.getSurname()); initials.setText(getInitials(user)); email.setText(user.getEmailAddress()); } private Person result = null; public Person getUser() { return result; } JTextField fname = new JTextField(); JTextField lname = new JTextField(); JTextField initials = new JTextField(); JTextField email = new JTextField(); GridBagLayout gridbag = new GridBagLayout(); GridBagConstraints constraints = new GridBagConstraints(); Container c = this.getContentPane(); private void createUI() { setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); JButton ok = new JButton(strings.getString("ok")); JButton cancel = new JButton(strings.getString("cancel")); ok.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { ExternalDialog.this.result = makeExternalUser(fname.getText(), lname.getText(), initials.getText(), email.getText()); ExternalDialog.this.dispose(); } }); cancel.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { ExternalDialog.this.dispose(); } }); fname.setColumns(20); lname.setColumns(20); initials.setColumns(20); email.setColumns(20); JLabel flabel = new JLabel(strings.getString("fname")); JLabel llabel = new JLabel(strings.getString("lname")); JLabel ilabel = new JLabel(strings.getString("initials")); JLabel elabel = new JLabel(strings.getString("email")); c.setLayout(gridbag); constraints.fill = GridBagConstraints.HORIZONTAL; constraints.insets = new Insets(5, 5, 0, 5); add(flabel, 0, 0, 0); add(fname, 1, 0, 1); constraints.insets = new Insets(2, 5, 0, 5); add(llabel, 0, 1, 0); add(lname, 1, 1, 1); add(ilabel, 0, 2, 0); add(initials, 1, 2, 1); add(elabel, 0, 3, 0); add(email, 1, 3, 1); constraints.insets = new Insets(5, 5, 5, 5); constraints.fill = GridBagConstraints.NONE; constraints.anchor = GridBagConstraints.WEST; add(ok, 0, 4, 0); constraints.anchor = GridBagConstraints.EAST; add(cancel, 1, 4, 0); } private void add(Component component, int x, int y, int weight) { constraints.gridx = x; constraints.gridy = y; constraints.weightx = weight; gridbag.setConstraints(component, constraints); c.add(component); } } }