/** * * Copyright (c) 2003 University of Mancester. * @author Kevin Garwood (garwood@cs.man.ac.uk) * @version 1.0 * */ package pedro.ontology; import java.awt.Container; import javax.swing.JMenu; import javax.swing.JMenuItem; import javax.swing.JMenuBar; import javax.swing.JDialog; import javax.swing.JButton; import java.util.Hashtable; import java.util.ArrayList; import javax.swing.JPanel; import java.awt.GridBagLayout; import java.awt.GridBagConstraints; import javax.swing.JSplitPane; import javax.swing.JComponent; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import javax.swing.text.JTextComponent; /* Code RoadMap: Section Constants Section Properties Section Construction Section Accessors Section Mutators Section Validation Section Errors Section Interfaces Section Overload */ public class DefaultOntologyViewer extends JDialog implements ActionListener, OntologyViewer, OntologyViewListener { public static void main (String[] args) { /* DefaultOntologyViewer viewer = new DefaultOntologyViewer(); viewer.setShowReset(true); DummyTreeProvenanceSource1 dummy1 = new DummyTreeProvenanceSource1(); DummyProvenanceSource dummy2 = new DummyProvenanceSource(); DummyTreeProvenanceSource2 dummy3 = new DummyTreeProvenanceSource2(); //Note: this test case captures situation when there is only //one appropriate view. Get rid of menu! DummyProvenanceSource4 dummy4 = new DummyProvenanceSource4(); //viewer.setSource(dummy6); viewer.setSource(dummy3); //viewer.showProvenance(false); //viewer.buildUI(); viewer.show(); */ } // end main () // ========================================== // Section Constants // ========================================== // ========================================== // Section Properties // ========================================== private boolean showReset; private boolean showProvenance; private boolean allowOtherViews; private TreeView treeView; protected ListView listView; private SummaryView summaryView; protected OntologyView currentView; private ProvenancePanel provenancePanel; protected ArrayList ontologyViews; private JMenu viewMenu; private JMenu optionsMenu; private JMenuItem chooseDelimiter; private Hashtable viewFromMenuName; private JButton reset; private JButton useTerms; private JButton close; protected OntologySource source; protected OntologyTerm[] selectedOntologyTerms; protected String targetFieldName; protected JTextComponent targetTextComponent; private String delimiter; // ========================================== // Section Construction // ========================================== public DefaultOntologyViewer() { allowOtherViews = true; showReset = false; provenancePanel = new ProvenancePanel(); treeView = new TreeView(); treeView.setOntologyViewListener(this); summaryView = new SummaryView(); summaryView.setOntologyViewListener(this); listView = new ListView(); listView.setOntologyViewListener(this); listView.useLocalSearchField(true); listView.buildUI(); currentView = listView; ontologyViews = new ArrayList(); ontologyViews.add(treeView); ontologyViews.add(summaryView); ontologyViews.add(listView); viewMenu = new JMenu("View"); viewFromMenuName = new Hashtable(); chooseDelimiter = new JMenuItem("Choose delimiter..."); chooseDelimiter.addActionListener(this); optionsMenu = new JMenu("Options"); optionsMenu.add(chooseDelimiter); reset = new JButton("Reset"); reset.addActionListener(this); useTerms = new JButton("Use Terms"); useTerms.addActionListener(this); close = new JButton("Close"); close.addActionListener(this); delimiter=";"; } public void buildUI() { JPanel panel = new JPanel(new GridBagLayout() ); GridBagConstraints panelGC = new GridBagConstraints(); panelGC.weightx = 0; panelGC.anchor = GridBagConstraints.NORTHWEST; panelGC.gridx = 0; panelGC.gridy = 0; panelGC.fill = GridBagConstraints.BOTH; panelGC.weightx = 100; panelGC.weighty = 100; panel.add(createTopPanel(), panelGC); panelGC.gridy++; panelGC.anchor = GridBagConstraints.SOUTHEAST; panelGC.fill = GridBagConstraints.NONE; panelGC.weightx = 0; panelGC.weighty = 0; panel.add(createButtonPanel(), panelGC); Container contentPane = getContentPane(); contentPane.removeAll(); contentPane.add(panel); pack(); } private JComponent createTopPanel() { JComponent currentViewComponent = currentView.getComponent(); if ( showProvenance == true) { JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, currentViewComponent, provenancePanel); return splitPane; } //end if () else { return currentViewComponent; } //end else } private JPanel createButtonPanel() { JPanel panel = new JPanel(new GridBagLayout() ); GridBagConstraints panelGC = new GridBagConstraints(); panelGC.weightx = 0; panelGC.anchor = GridBagConstraints.SOUTHEAST; panelGC.fill = GridBagConstraints.NONE; panelGC.gridx = 0; panelGC.gridy = 0; if ( showReset == true) { panel.add(reset, panelGC); panelGC.gridx++; } //end if () panel.add(useTerms, panelGC); panelGC.gridx++; panel.add(close, panelGC); return panel; } // ========================================== // Section Accessors // ========================================== // ========================================== // Section Mutators // ========================================== public void showProvenance(boolean showProvenance) { this.showProvenance = showProvenance; } public void setShowReset(boolean showReset) { this.showReset = showReset; } public void addOntologyView(OntologyView ontologyView) { ontologyViews.add(0,ontologyView); } public void useTreeView(boolean allowOtherViews) { allowOtherViews = allowOtherViews; currentView = treeView; } public void useListView(boolean allowOtherViews) { allowOtherViews = allowOtherViews; currentView = listView; } public void setOntologyView(OntologyView ontologyView, boolean allowOtherViews) { if ( ontologyViews.contains(ontologyView) == true) { currentView = ontologyView; } //end if () allowOtherViews = allowOtherViews; } public void setSource(OntologySource source) { this.source = source; setTitle(source.getName() ); //determine which views can support this source JMenuBar menuBar = new JMenuBar(); if ( allowOtherViews == true) { viewMenu.removeAll(); viewFromMenuName.clear(); int numberOfViews = ontologyViews.size(); boolean firstAbleViewFound = false; for ( int i = 0; i < numberOfViews; i++) { OntologyView view = (OntologyView) ontologyViews.get(i); if ( view.canView(source) == true) { if ( firstAbleViewFound == false) { firstAbleViewFound = true; currentView = view; } //end if () String viewName = view.getName(); JMenuItem viewMenuItem = new JMenuItem(viewName); viewMenuItem.addActionListener(this); viewMenu.add(viewMenuItem); viewFromMenuName.put(view.getName(), view); } // end for () } //end if () if ( currentView == null) { currentView = listView; } //end if () if ( viewMenu.getItemCount() > 1) { menuBar.add(viewMenu); setJMenuBar(menuBar); } //end if () } else { //try using the current view if ( currentView.canView(source) == false) { currentView = listView; } //end if () } //end else //menuBar.add(optionsMenu); if ( source instanceof OntologyProvenance) { showProvenance = true; } //end if () currentView.setSource(source); buildUI(); } public void setTargetDataField(String fieldName, JTextComponent textComponent) { targetFieldName = fieldName; targetTextComponent = textComponent; } // ========================================== // Section Validation // ========================================== // ========================================== // Section Errors // ========================================== // ========================================== // Section Interfaces // ========================================== //Interface: OntologyViewListener public void setSelectedOntologyTerms(OntologyTerm[] _selectedOntologyTerms) { selectedOntologyTerms = _selectedOntologyTerms; } public void setLastSelectedTerm(OntologyTerm ontologyTerm) { if ( showProvenance == false) { return; } //end if () if ( source instanceof OntologyProvenance) { if ( source instanceof URLProvenance) { provenancePanel.setProvenance(ontologyTerm.getProvenanceLink() ); } //end if () else if ( source instanceof SummaryProvenance) { provenancePanel.setProvenance(ontologyTerm.getDefinition() ); } //end else () } //end if () } //Interface: ActionListener public void actionPerformed(ActionEvent event) { Object button = event.getSource(); if (button == reset) { reset(); } //end if () else if ( button == useTerms) { useTerms(); } //end else () else if ( button == close) { close(); } //end else () else if ( button == chooseDelimiter) { chooseDelimiter(); } //end else () else { JMenuItem menuItem = (JMenuItem) button; String menuName = menuItem.getText(); currentView = (OntologyView) viewFromMenuName.get(menuName); currentView.setSource(source); buildUI(); } //end else } protected void close() { dispose(); } protected void reset() { currentView.reset(); } protected void useTerms() { if ( selectedOntologyTerms == null) { return; } //end if () if ( selectedOntologyTerms.length == 0) { return; } //end if () if ( selectedOntologyTerms == null) { return; } //end if () StringBuffer termList = new StringBuffer(); termList.append(selectedOntologyTerms[0].getTerm() ); for ( int i = 1; i < selectedOntologyTerms.length;i++) { termList.append(delimiter); termList.append(selectedOntologyTerms[i].getTerm() ); } // end for () if ( targetTextComponent.isEditable() == true) { //insert term at current CaretPosition int caretPosition = targetTextComponent.getCaretPosition(); String currentValue = targetTextComponent.getText(); StringBuffer updatedValue = new StringBuffer(); if (caretPosition == 0) { updatedValue.append(termList.toString()); updatedValue.append(currentValue); } //end if () else { updatedValue.append(currentValue.substring(0,caretPosition) ); updatedValue.append(termList.toString()); updatedValue.append(currentValue.substring(caretPosition)); } //end else targetTextComponent.setText(updatedValue.toString() ); } //end if () else { String currentValue = targetTextComponent.getText(); StringBuffer updatedValue = new StringBuffer(); updatedValue.append(currentValue); if ( currentValue.equals("") == false) { //there's already text updatedValue.append(delimiter); } //end if () updatedValue.append(termList.toString() ); targetTextComponent.setText(updatedValue.toString() ); } //end else } private void chooseDelimiter() { } public void show() { super.show(); } // ========================================== // Section Overload // ========================================== }