/** * * Copyright (c) 2003 University of Mancester. * @author Kevin Garwood (garwood@cs.man.ac.uk) * */ package pedro.ontology; import javax.swing.JTextField; import javax.swing.JPanel; import javax.swing.JLabel; import javax.swing.text.JTextComponent; import javax.swing.JTextArea; import javax.swing.JScrollPane; import java.awt.GridBagLayout; import java.awt.GridBagConstraints; import java.util.Hashtable; import java.util.Arrays; import pedro.util.FilteredList; import javax.swing.event.CaretListener; import javax.swing.event.CaretEvent; import javax.swing.JComponent; import javax.swing.JTable; import javax.swing.event.ListSelectionListener; import javax.swing.ListSelectionModel; import javax.swing.event.ListSelectionEvent; import javax.swing.ListModel; /** * @author Kevin Garwood * @date * @version 1.0 */ /* Code RoadMap: Section Constants Section Properties Section Construction Section Accessors Section Mutators Section Validation Section Errors Section Interfaces Section Overload */ public class SummaryView extends JPanel implements CaretListener, ListSelectionListener, OntologyView { // ========================================== // Section Constants // ========================================== // ========================================== // Section Properties // ========================================== private JScrollPane dictionaryPane; private JTable dictionaryTable; private DictionaryTableModel dictionaryModel; private boolean showDescription; private JScrollPane descriptionPane; private JPanel searchFieldPanel; private JTextField searchField; private JTextArea description; private OntologySource ontologySource; private OntologyViewListener ontologyViewListener; // ========================================== // Section Construction // ========================================== public SummaryView() { setLayout(new GridBagLayout() ); showDescription = false; createDescription(); createSearchFieldPanel(); dictionaryModel = new DictionaryTableModel(); dictionaryTable = new JTable(dictionaryModel); dictionaryTable.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN); ListSelectionModel selectionModel = dictionaryTable.getSelectionModel(); selectionModel.addListSelectionListener(this); dictionaryTable.doLayout(); dictionaryPane = new JScrollPane(dictionaryTable); buildUI(); } private void createDescription() { description = new JTextArea(4, 30); description.setLineWrap(true); description.setWrapStyleWord(true); description.setEditable(false); description.setBackground( getBackground() ); descriptionPane = new JScrollPane(description); } public void showDescription(boolean _showDescription) { this.showDescription = _showDescription; } private void createSearchFieldPanel() { JPanel panel = new JPanel(new GridBagLayout() ); GridBagConstraints panelGC = new GridBagConstraints(); panelGC.anchor = GridBagConstraints.NORTHWEST; panelGC.fill = GridBagConstraints.NONE; panelGC.gridx = 0; panelGC.gridy = 0; panelGC.weightx = 0; panelGC.weighty = 0; panel.add(new JLabel("Search:") ); panelGC.gridx++; panelGC.fill = GridBagConstraints.HORIZONTAL; panelGC.weightx = 100; searchField = new JTextField(); searchField.addCaretListener(this); panel.add(searchField, panelGC); searchFieldPanel = panel; } public void buildUI() { removeAll(); GridBagConstraints panelGC = new GridBagConstraints(); panelGC.anchor = GridBagConstraints.NORTHWEST; panelGC.fill = GridBagConstraints.HORIZONTAL; panelGC.gridx = 0; panelGC.gridy = 0; panelGC.weightx = 0; panelGC.weighty = 0; panelGC.fill = GridBagConstraints.HORIZONTAL; panelGC.weightx = 100; add(searchFieldPanel, panelGC); panelGC.gridy++; if ( showDescription == true) { panelGC.fill = GridBagConstraints.BOTH; panelGC.weighty = 10; panelGC.weightx = 100; add(descriptionPane, panelGC); panelGC.gridy++; } //end if () panelGC.anchor = GridBagConstraints.NORTHWEST; panelGC.fill = GridBagConstraints.BOTH; panelGC.weightx = 100; panelGC.weighty = 90; add(dictionaryPane, panelGC); } // ========================================== // Section Accessors // ========================================== // ========================================== // Section Mutators // ========================================== // ========================================== // Section Validation // ========================================== // ========================================== // Section Errors // ========================================== // ========================================== // Section Interfaces // ========================================== //Interface: CaretListener public void caretUpdate(CaretEvent event) { //if character before the current position is a space or //if current position is zero reset int currentPosition = searchField.getCaretPosition(); String textFieldValue = searchField.getText(); if ( currentPosition == 0) { dictionaryModel.reset(); } //end if () else if (textFieldValue.charAt(currentPosition - 1) == ' ') { dictionaryModel.reset(); } //end else () else { String filterMask = textFieldValue.substring(0,currentPosition); int lastSpaceIndex = filterMask.lastIndexOf(" "); if ( lastSpaceIndex != -1) { filterMask = filterMask.substring(lastSpaceIndex, currentPosition); filterMask = filterMask.trim(); } //end if () dictionaryModel.filter(filterMask); } //end else if ( dictionaryModel.getRowCount() > 0) { dictionaryTable.setRowSelectionInterval(0,0); } //end if () dictionaryTable.updateUI(); } //Interface: OntologyViewComponent public String getName() { return "Summary View"; } public boolean canView(OntologySource ontologySource) { if ( ontologySource instanceof SummaryProvenance) { return true; } //end if () return false; } /** * resets the display. */ public void reset() { dictionaryModel.reset(); dictionaryTable.clearSelection(); searchField.setText(""); } public void setSource(OntologySource ontologySource) { this.ontologySource = ontologySource; description.setText(ontologySource.getDescription() ); OntologyTerm[] ontologyTerms = ontologySource.getTerms(); dictionaryModel.setMasterList(ontologyTerms); dictionaryTable.clearSelection(); } public OntologyTerm[] getSelectedTerms() { int[] selectedRows = dictionaryTable.getSelectedRows(); OntologyTerm[] results = new OntologyTerm[selectedRows.length]; for ( int i = 0; i < selectedRows.length; i++) { results[i] = dictionaryModel.getTermFromRow(i); } // end for () return results; } public void setOntologyViewListener(OntologyViewListener ontologyViewListener) { this.ontologyViewListener = ontologyViewListener; } public JComponent getComponent() { return this; } //Interface: ListSelectionListener public void valueChanged(ListSelectionEvent event) { int[] selectedRows = dictionaryTable.getSelectedRows(); OntologyTerm[] selectedTerms = new OntologyTerm[selectedRows.length]; for ( int i = 0; i < selectedRows.length; i++) { selectedTerms[i] = dictionaryModel.getTermFromRow(selectedRows[i]); } // end for () if ( dictionaryModel.getRowCount() == 0) { return; } //end if () ListSelectionModel selectionModel = dictionaryTable.getSelectionModel(); int leadSelectionIndex = selectionModel.getLeadSelectionIndex(); OntologyTerm lastSelectedTerm = (OntologyTerm) dictionaryModel.getTermFromRow(leadSelectionIndex); ontologyViewListener.setSelectedOntologyTerms(selectedTerms); ontologyViewListener.setLastSelectedTerm(lastSelectedTerm); } // ========================================== // Section Overload // ========================================== }