/**
* 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;
// Pegadi imports
import org.pegadi.model.LoginContext;
import org.pegadi.sources.search.*;
import org.pegadi.sqlsearch.AndTerm;
import org.pegadi.sqlsearch.OrTerm;
import org.pegadi.sqlsearch.SearchTerm;
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.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.ResourceBundle;
import java.util.Vector;
public class SourceSearchPanel extends JPanel {
private final Logger log = LoggerFactory.getLogger(getClass());
protected static ResourceBundle strings;
SourceTableModel model;
JTable table;
JScrollPane scrollPane;
JTextField searchField;
JLabel searchLabel;
JSplitPane splitPane;
JTabbedPane tabPane;
JButton searchButton;
JPanel choicePanel = new JPanel();
TermsPanel termsPanel;
AdvancedSearchPanel advancedSearchPane;
SearchTerm lastTerm = null;
public SourceSearchPanel() {
strings = ResourceBundle.getBundle("org.pegadi.sources.SourceSearchPanelStrings");
setLayout(new BorderLayout());
model = new SourceTableModel(new ArrayList<Source>());
searchLabel = new JLabel(strings.getString("searchLabel") +": ");
searchField = new JTextField(15);
searchField.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent e) {
searchField_actionPerformed();
}
});
table = new JTable(model);
table.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
table_mouseClicked(e);
}
});
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
scrollPane = new JScrollPane(table);
advancedSearchPane = new AdvancedSearchPanel();
advancedSearchPane.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
advancedSearch_actionPerformed();
}
});
JPanel simpleSearchPane = new JPanel();
simpleSearchPane.setLayout(new FlowLayout(FlowLayout.LEFT));
simpleSearchPane.add(searchLabel);
simpleSearchPane.add(searchField);
tabPane = new JTabbedPane();
tabPane.addTab(strings.getString("simpleSearchTab.text"),simpleSearchPane);
tabPane.addTab(strings.getString("advancedSearchTab.text"),advancedSearchPane);
splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
splitPane.setLeftComponent(tabPane);
splitPane.setRightComponent(scrollPane);
add(splitPane);
}
void setFocusToSearchField() {
searchField.setRequestFocusEnabled(true);
searchField.requestFocus();
}
public void repeatSearch() {
java.util.List<Source> sources = new ArrayList<Source>();
try {
sources = LoginContext.server.getSourcesBySearchTerm(lastTerm, LoginContext.sessionKey);
}
catch(RemoteException e) {
log.error("Error retrieving sources by searchterm " + lastTerm + " from server", e);
}
model.setSources(sources);
}
void searchField_actionPerformed() {
java.util.List<Source> sources = new ArrayList<Source>();
String searchString = searchField.getText();
lastTerm = new OrTerm(new NameTerm(searchString), new PositionTerm(searchString));
try {
sources = LoginContext.server.getSourcesBySearchTerm(lastTerm, LoginContext.sessionKey);
}
catch(RemoteException e) {
log.error("Error retrieving sources by searchterm " + lastTerm + " from server", e);
}
model.setSources(sources);
}
void advancedSearch_actionPerformed() {
lastTerm = advancedSearchPane.getSearchTerm();
java.util.List<Source> sources = new ArrayList<Source>();
try {
sources = LoginContext.server.getSourcesBySearchTerm(lastTerm, LoginContext.sessionKey);
}
catch(RemoteException e) {
log.error("Error retrieving sources by searchterm " + lastTerm + " from server", e);
}
model.setSources(sources);
}
void table_mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2 && SwingUtilities.isLeftMouseButton(e)) {
int row = table.getSelectedRow();
Object source = model.getSource(row);
fireSourceDoubleClicked(source);
}
}
public void addActionListener(ActionListener listener) {
listenerList.add(ActionListener.class, listener);
}
public void fireSourceDoubleClicked(Object selectedSource) {
Object[] listeners = listenerList.getListenerList();
for (Object listener : listeners) {
if (listener instanceof ActionListener) {
ActionEvent event = new ActionEvent(selectedSource, 0, "Article selected");
((ActionListener) listener).actionPerformed(event);
}
}
}
class AdvancedSearchPanel extends JPanel {
JComboBox andOrCombo;
JComboBox termsCombo = new JComboBox();
String[] andOrStrings = {"All","Any"};
ResourceBundle strings;
public AdvancedSearchPanel() {
super();
strings = ResourceBundle.getBundle("org.pegadi.sources.SourceSearchPanelStrings");
andOrStrings[0] = strings.getString("all");
andOrStrings[1] = strings.getString("any");
andOrCombo = new JComboBox(andOrStrings);
searchButton = new JButton(strings.getString("searchButton.text"),new ImageIcon(getClass().getResource(strings.getString("searchButton.icon"))));
setBorder(BorderFactory.createTitledBorder(strings.getString("panelBorder.title")));
setLayout(new BorderLayout());
termsCombo.addItem(strings.getString("termsCombo.chooseTerm"));
termsCombo.addItem(new TermComboContainer(NameTermPanel.class));
termsCombo.addItem(new TermComboContainer(CategoryTermPanel.class));
termsCombo.addItem(new TermComboContainer(NotesTermPanel.class));
termsCombo.addItem(new TermComboContainer(EmailTermPanel.class));
termsCombo.addItem(new TermComboContainer(PositionTermPanel.class));
searchButton.setEnabled(false);
termsCombo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Object theObject = termsCombo.getSelectedItem();
if(theObject instanceof String)
return;
SearchTermPanel thePanel = ((TermComboContainer)(theObject)).getPanel();
termsPanel.addTermPanel(thePanel);
revalidate();
repaint();
}
});
searchButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ActionEvent ev = new ActionEvent(this,-1,"fireSearch");
fireSearch(ev);
}
});
choicePanel.setLayout(new FlowLayout(FlowLayout.LEFT));
choicePanel.add(termsCombo);
choicePanel.add(andOrCombo);
choicePanel.add(searchButton);
add(choicePanel,BorderLayout.NORTH);
termsPanel = new TermsPanel();
termsPanel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
termsPanel_actionPerformed();
}
});
JScrollPane jsp = new JScrollPane(termsPanel,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
add(jsp,BorderLayout.CENTER);
}
public SearchTerm getSearchTerm() {
SearchTerm[] terms = termsPanel.getSearchTerms();
if(andOrCombo.getSelectedIndex()==0)
return new AndTerm(terms);
else
return new OrTerm(terms);
}
void termsPanel_actionPerformed() {
searchButton.setEnabled(true);
}
public void addActionListener(ActionListener al) {
listenerList.add(ActionListener.class,al);
}
public void fireSearch(ActionEvent e) {
Object[] listeners = listenerList.getListenerList();
for (Object listener : listeners) {
if (listener instanceof ActionListener) {
((ActionListener) listener).actionPerformed(e);
}
}
repaint();
}
}
class TermsPanel extends JPanel {
Vector panels = new Vector();
GridBagLayout gridBag = new GridBagLayout();
public TermsPanel() {
setLayout(gridBag);
/* As a small hack to beautify the search-component, an empty
* panel is here added to the end of the list of search-criterias.
* This makes sure all the SearchTermPanels line up nicely at the top.
*/
JPanel panel = new JPanel();
GridBagConstraints constraints = new GridBagConstraints();
constraints.gridwidth=GridBagConstraints.REMAINDER;
constraints.gridheight=GridBagConstraints.REMAINDER;
constraints.fill=GridBagConstraints.BOTH;
constraints.weighty=1.0;
constraints.weightx=1.0;
constraints.gridy=500; // ~infinity
gridBag.setConstraints(panel,constraints);
add(panel);
}
void remPanel_actionPerformed(ActionEvent e) {
SearchTermPanel stp = (SearchTermPanel)e.getSource();
remove(stp);
panels.removeElement(stp);
if(termsPanel.getSearchTerms().length == 0)
searchButton.setEnabled(false);
revalidate();
repaint();
}
private int last_y = 0;
public void addTermPanel(SearchTermPanel panel) {
panels.addElement(panel);
panel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
remPanel_actionPerformed(e);
}
});
GridBagConstraints constraints = new GridBagConstraints();
constraints.gridwidth=GridBagConstraints.REMAINDER;
constraints.gridheight=1;
constraints.fill=GridBagConstraints.HORIZONTAL;
constraints.anchor=GridBagConstraints.NORTH;
constraints.weighty=0.0;
constraints.weightx=1.0;
constraints.gridy=last_y++;
gridBag.setConstraints(panel,constraints);
add(panel);
fireRemovedPanel();
}
public void addActionListener(ActionListener al) {
listenerList.add(ActionListener.class,al);
}
public void fireRemovedPanel() {
ActionEvent ae = new ActionEvent(this,0,"remove searchterm");
Object[] listeners = listenerList.getListenerList();
for (Object listener : listeners) {
if (listener instanceof ActionListener) {
((ActionListener) listener).actionPerformed(ae);
}
}
}
public SearchTerm[] getSearchTerms() {
SearchTerm[] terms = new SearchTerm[panels.size()];
for(int i=0;i<terms.length;i++)
terms[i] = ((SearchTermPanel)panels.elementAt(i)).getSearchTerm();
return terms;
}
}
class TermComboContainer {
Class panel;
public TermComboContainer(Class panel) {
this.panel=panel;
}
public String toString() {
return strings.getString(panel.getName());
}
public SearchTermPanel getPanel() {
try {
return (SearchTermPanel)panel.newInstance();
}
catch (Exception e) {
return null;
}
}
}
}
abstract class SearchTermPanel extends JPanel {
public SearchTermPanel() {
String borderString = SourceSearchPanel.strings.getString(this.getClass().getName());
setBorder(BorderFactory.createTitledBorder(borderString));
setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
JButton jb = new JButton(new ImageIcon(getClass().getResource(SourceSearchPanel.strings.getString("searchTermPanel.removeButton.icon"))));
jb.setMargin(new Insets(0,0,0,0));
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
fireRemove();
}
});
add(jb);
add(new JLabel(" ")); // Adding some space before text.
}
public abstract SearchTerm getSearchTerm();
public void addActionListener(ActionListener al) {
listenerList.add(ActionListener.class,al);
}
public void fireRemove() {
ActionEvent ae = new ActionEvent(this,0,"remove searchterm");
Object[] listeners = listenerList.getListenerList();
for (Object listener : listeners) {
if (listener instanceof ActionListener) {
((ActionListener) listener).actionPerformed(ae);
}
}
}
}
class CategoryTermPanel extends SearchTermPanel {
JComboBox categories;
public CategoryTermPanel() {
super();
categories = new JComboBox();
populateCategoriesComboBox();
add(new JLabel(SourceSearchPanel.strings.getString("CategoryTermPanel.label")));
add(new JLabel(" "));
add(categories);
}
void populateCategoriesComboBox() {
java.util.List<Category> list;
try {
list = LoginContext.server.getSourceCategories(LoginContext.sessionKey);
} catch (RemoteException e) {
return;
}
for (Category aList : list) categories.addItem(aList);
}
public SearchTerm getSearchTerm() {
return new CategoryTerm(((Category)categories.getSelectedItem()).getID());
}
}
class NameTermPanel extends SearchTermPanel {
JTextField textField;
public NameTermPanel() {
super();
textField = new JTextField(15);
add(new JLabel(SourceSearchPanel.strings.getString("NameTermPanel.label")));
add(new JLabel(" "));
add(textField);
}
public SearchTerm getSearchTerm() {
return new NameTerm(textField.getText());
}
}
class NotesTermPanel extends SearchTermPanel {
JTextField textField;
public NotesTermPanel() {
super();
textField = new JTextField(15);
add(new JLabel(SourceSearchPanel.strings.getString("NotesTermPanel.label")));
add(new JLabel(" "));
add(textField);
}
public SearchTerm getSearchTerm() {
return new NotesTerm(textField.getText());
}
}
class EmailTermPanel extends SearchTermPanel {
JTextField textField;
public EmailTermPanel() {
super();
textField = new JTextField(15);
add(new JLabel(SourceSearchPanel.strings.getString("EmailTermPanel.label")));
add(new JLabel(" "));
add(textField);
}
public SearchTerm getSearchTerm() {
return new EmailTerm(textField.getText());
}
}
class PositionTermPanel extends SearchTermPanel {
JTextField textField;
public PositionTermPanel() {
super();
textField = new JTextField(15);
add(new JLabel(SourceSearchPanel.strings.getString("PositionTermPanel.label")));
add(new JLabel(" "));
add(textField);
}
public SearchTerm getSearchTerm() {
return new PositionTerm(textField.getText());
}
}