/** * * 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.event.ListSelectionListener; import javax.swing.event.ListSelectionEvent; import javax.swing.ListModel; import javax.swing.ListSelectionModel; /** * @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 ListView extends JPanel implements CaretListener, ListSelectionListener, OntologyView, TouchTypeSupport { // ========================================== // Section Constants // ========================================== // ========================================== // Section Properties // ========================================== private FilteredList filteredList; private JTextComponent textComponent; private boolean isAdjusting; private Hashtable ontologyTermFromWord; private JScrollPane descriptionPane; private JTextArea description; private boolean useLocalSearchField; private boolean showDescription; private JPanel searchFieldPanel; private JTextField localSearchField; private OntologySource ontologySource; private OntologyViewListener ontologyViewListener; // ========================================== // Section Construction // ========================================== public ListView() { setLayout(new GridBagLayout() ); ontologyTermFromWord = new Hashtable(); useLocalSearchField= false; showDescription = false; createDescription(); createLocalSearchFieldPanel(); filteredList = new FilteredList(); filteredList.setPrototypeCellValue("aaaaaaaaaaaaaaaaa"); filteredList.addListSelectionListener(this); 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; } public void useLocalSearchField(boolean _useLocalSearchField) { useLocalSearchField = _useLocalSearchField; } private void createLocalSearchFieldPanel() { 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; localSearchField = new JTextField(); registerTouchTypeSource(localSearchField); panel.add(localSearchField, 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; if ( useLocalSearchField == true) { panelGC.fill = GridBagConstraints.HORIZONTAL; panelGC.weightx = 100; add(searchFieldPanel, panelGC); panelGC.gridy++; } //end if () if ( showDescription == true) { panelGC.fill = GridBagConstraints.BOTH; panelGC.weighty = 10; panelGC.weightx = 100; add(descriptionPane, panelGC); panelGC.gridy++; } //end if () JScrollPane scrollPane = new JScrollPane(filteredList); panelGC.anchor = GridBagConstraints.NORTHWEST; panelGC.fill = GridBagConstraints.BOTH; panelGC.weightx = 100; panelGC.weighty = 90; add(scrollPane, panelGC); } public void registerTouchTypeSource(JTextComponent text) { if ( textComponent != null) { textComponent.removeCaretListener(this); } //end if () this.textComponent = text; textComponent.addCaretListener(this); } public void deregisterTouchTypeSource() { if ( textComponent != null) { textComponent.removeCaretListener(this); } //end if () } // ========================================== // Section Accessors // ========================================== // ========================================== // Section Mutators // ========================================== // ========================================== // Section Validation // ========================================== // ========================================== // Section Errors // ========================================== // ========================================== // Section Interfaces // ========================================== //Interface: CaretListener public void caretUpdate(CaretEvent event) { if ( textComponent == null) { return; } //end if () //if character before the current position is a space or //if current position is zero reset int currentPosition = textComponent.getCaretPosition(); String textFieldValue = textComponent.getText(); if ( currentPosition == 0) { filteredList.reset(); } //end if () else if (textFieldValue.charAt(currentPosition - 1) == ' ') { filteredList.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 () filteredList.filterList(filterMask); } //end else if ( filteredList.getItemCount() > 0) { filteredList.setSelectedIndex(0); } //end if () } //Interface: OntologyViewComponent public String getName() { return "List View"; } public boolean canView(OntologySource ontologySource) { return true; } /** * resets the display. */ public void reset() { filteredList.reset(); if ( useLocalSearchField == true) { //if the component is using its own text field //reset it localSearchField.setText(""); } //end if () } public void setSource(OntologySource ontologySource) { this.ontologySource = ontologySource; filteredList.reset(); filteredList.clearSelection(); description.setText(ontologySource.getDescription() ); OntologyTerm[] ontologyTerms = ontologySource.getTerms(); String[] terms = new String[ontologyTerms.length]; for ( int i = 0; i < ontologyTerms.length; i++) { terms[i] = ontologyTerms[i].getTerm(); ontologyTermFromWord.put(terms[i], ontologyTerms[i]); } // end for () Arrays.sort(terms); filteredList.setTerms(terms); } public OntologyTerm[] getSelectedTerms() { String[] selectedValues = (String[]) filteredList.getSelectedValues(); OntologyTerm[] ontologyTerms = new OntologyTerm[selectedValues.length]; for ( int i = 0; i < selectedValues.length; i++) { ontologyTerms[i] = (OntologyTerm) ontologyTermFromWord.get(selectedValues[i]); } // end for () return ontologyTerms; } public void setOntologyViewListener(OntologyViewListener ontologyViewListener) { this.ontologyViewListener = ontologyViewListener; } public JComponent getComponent() { return this; } //Interface: ListSelectionListener public void valueChanged(ListSelectionEvent event) { if ( event.getValueIsAdjusting() == false) { Object[] values = filteredList.getSelectedValues(); OntologyTerm[] selectedTerms = new OntologyTerm[values.length]; for ( int i = 0; i < selectedTerms.length; i++) { String selectedValue = (String) values[i]; selectedTerms[i] = (OntologyTerm) ontologyTermFromWord.get(selectedValue); } // end for () ListSelectionModel selectionModel = filteredList.getSelectionModel(); int leadSelectionIndex = selectionModel.getLeadSelectionIndex(); ListModel listModel = filteredList.getModel(); if ( filteredList.getItemCount() == 0) { return; } //end if () String lastSelectedValue = (String) listModel.getElementAt(leadSelectionIndex); OntologyTerm lastSelectedTerm = (OntologyTerm) ontologyTermFromWord.get(lastSelectedValue); ontologyViewListener.setSelectedOntologyTerms(selectedTerms); ontologyViewListener.setLastSelectedTerm(lastSelectedTerm); } //end if () } // ========================================== // Section Overload // ========================================== }