/** * 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.storysketch.views.plugins; import java.awt.BorderLayout; import java.awt.Component; import java.awt.Container; import java.awt.Cursor; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.Insets; import java.awt.Rectangle; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.net.URL; import java.util.HashMap; import java.util.Map; import javax.swing.AbstractAction; import javax.swing.BorderFactory; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JComponent; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.JTextArea; import javax.swing.JTextField; import javax.swing.ListSelectionModel; import javax.swing.Scrollable; import javax.swing.SwingConstants; import javax.swing.SwingUtilities; import javax.swing.table.AbstractTableModel; import javax.swing.table.TableColumn; import org.apache.xerces.parsers.DOMParser; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; import org.w3c.dom.Text; public class NTNUPhoneSearch extends PersonSearchPanel { String tabName = "NTNU telefon"; JTextField searchField; JTable results; JScrollPane resultsScroll; DetailsPanel detailsPanel; DOMParser parser; JComboBox institution; public NTNUPhoneSearch() { setLayout(new BorderLayout()); JPanel formPanel = new JPanel(); formPanel.add(new JLabel()); searchField = new JTextField(15); // Search button URL searchUrl = getClass().getClassLoader().getResource("images/org/pegadi/storysketch/swing_search.gif"); ImageIcon searchIcon = new ImageIcon(searchUrl); JButton searchButton = new JButton(new AbstractAction("", searchIcon) { public void actionPerformed(ActionEvent e) { setCursor(new Cursor(Cursor.WAIT_CURSOR)); performSearch(); setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); } }); searchField.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { setCursor(new Cursor(Cursor.WAIT_CURSOR)); performSearch(); setCursor(new Cursor(Cursor.DEFAULT_CURSOR)); } }); formPanel.add(searchField); formPanel.add(searchButton); String[] institutions = {"NTNU", "SINTEF", "HiST", "Andre", "Alle"}; institution = new JComboBox(institutions); formPanel.add(institution); add(formPanel,BorderLayout.NORTH); results = new JTable(new ResultsTableModel()); results.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); results.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { results_mouseClicked(e); } }); updateColumnSizes(results); resultsScroll = new JScrollPane(results); add(resultsScroll, BorderLayout.CENTER); } public void clear() { searchField.setText(""); results.setModel(new ResultsTableModel()); resultsScroll.setViewportView(results); } public void setSearchString(String searchString) { searchField.setText(searchString); } public void results_mouseClicked(MouseEvent e) { if(e.getClickCount() == 2) { int row = results.rowAtPoint(e.getPoint()); ResultsTableModel model = (ResultsTableModel) results.getModel(); DOMParser parser = new DOMParser(); String url = "http://underdusken.no:8888/storysketch/ntnukat/info?ldap=" + model.getLDAP(row); try { parser.parse(url); } catch (Exception se) { JOptionPane.showMessageDialog(SwingUtilities.windowForComponent(results), "Kunne ikke utføre søk", "Feil ved søk", JOptionPane.ERROR_MESSAGE); se.printStackTrace(); } detailsPanel = new DetailsPanel(this, parser.getDocument().getDocumentElement()); resultsScroll.setViewportView(detailsPanel); resultsScroll.getViewport().setViewSize(new Dimension(400,150)); } } public void applyResults() { } public void addDataImportListener(ActionListener l) { listenerList.add(ActionListener.class, l); } public Map getImportData() { Map map = new HashMap(); map.put("name",detailsPanel.getField("name")); map.put("organization",detailsPanel.getOrganization()); map.put("position",detailsPanel.getField("position")); map.put("phone",detailsPanel.getField("phone")); map.put("email",detailsPanel.getField("email")); map.put("address",detailsPanel.getField("address")); return map; } private void fireDataImport(ActionEvent e) { e.setSource(this); Object[] listeners = listenerList.getListenerList(); for (int i = listeners.length - 2; i >= 0; i -=2 ) { if (listeners[i] == ActionListener.class) { ((ActionListener)listeners[i+1]).actionPerformed(e); } } } public String getTabName() { return tabName; } public void updateColumnSizes(JTable table) { TableColumn column; for (int i = 0; i < 4; i++) { column = table.getColumnModel().getColumn(i); if (i == 0) { column.setPreferredWidth(150); } else { column.setPreferredWidth(70); } } table.setPreferredScrollableViewportSize(new Dimension(400,150)); } public void performSearch() { if(parser == null) parser = new DOMParser(); String encoded; try { encoded = java.net.URLEncoder.encode(searchField.getText(), "iso-8859-1"); } catch (java.io.UnsupportedEncodingException e) { encoded = searchField.getText(); } String url = "http://underdusken.no:8888/storysketch/ntnukat/search?org=" + institution.getSelectedItem().toString() +"&navn=" + encoded; try { parser.parse(url); } catch (Exception se) { se.printStackTrace(); JOptionPane.showMessageDialog(SwingUtilities.windowForComponent(results), "Fikk ikke kontakt med søkeserveren.", "Feil ved søk", JOptionPane.ERROR_MESSAGE); return; } results.setModel(new ResultsTableModel(parser.getDocument().getDocumentElement())); updateColumnSizes(results); if(results.getModel().getRowCount() == 0) { resultsScroll.setViewportView(new JLabel("<html><font size=\"+1\"><b>Beklager</b></font><br>Fant ingen personer. (\"" +searchField.getText() +"\")</html>")); } else { resultsScroll.setViewportView(results); } } class ResultsTableModel extends AbstractTableModel { Element results; public ResultsTableModel(Element results) { this.results = results; } public ResultsTableModel() { results = null; } public String getColumnName(int col) { switch(col) { case 0: return "Navn"; case 1: return "Organisasjon"; case 2: return "Stilling"; case 3: return "Telefon"; } return ""; } public int getColumnCount() { return 4; } public int getRowCount() { if(results == null) { return 0; } else { NodeList nl = results.getElementsByTagName("person"); return nl.getLength(); } } public Object getValueAt(int row, int col) { Element e = (Element) results.getElementsByTagName("person").item(row); switch (col) { case 0: return extractValue(e, "name"); case 1: return extractValue(e, "organization"); case 2: return extractValue(e, "position"); case 3: return extractValue(e, "phone"); } return ""; } public String getLDAP(int row) { Element e = (Element) results.getElementsByTagName("person").item(row); return extractValue(e,"ldap"); } } class DetailsPanel extends JPanel implements Scrollable { Element results; Element person; JTextField nameField = new JTextField(20); JTextArea organizationArea = new JTextArea(2,20); JTextField phoneField = new JTextField(20); JTextField emailField = new JTextField(20); JTextField positionField = new JTextField(20); JTextField addressField = new JTextField(20); JTextField officeField = new JTextField(20); NTNUPhoneSearch p; public DetailsPanel(NTNUPhoneSearch p, Element results) { this.results = results; this.p = p; NodeList nl = results.getElementsByTagName("person"); if(nl.getLength() == 0) { add(new JLabel("Beklager, fant ingen informasjon")); return; } else { person = (Element)nl.item(0); } JPanel info = new JPanel(); GridBagLayout gridbag = new GridBagLayout(); GridBagConstraints c = new GridBagConstraints(); info.setLayout(gridbag); makeLabel(new JLabel("Navn:"), info, gridbag, c); makeField(nameField, info, gridbag, c); nameField.setText(getText("name")); nameField.setEditable(false); makeLabel(new JLabel("Organisasjon:"), info, gridbag, c); makeField(new JScrollPane(organizationArea), info, gridbag, c); organizationArea.setText(getText("organization")); organizationArea.setLineWrap(true); organizationArea.setWrapStyleWord(true); organizationArea.setEditable(false); makeLabel(new JLabel("Telefon:"), info, gridbag, c); makeField(phoneField, info, gridbag, c); phoneField.setText(getText("phone")); phoneField.setEditable(false); makeLabel(new JLabel("E-post:"), info, gridbag, c); makeField(emailField, info, gridbag, c); emailField.setText(getText("email")); emailField.setEditable(false); makeLabel(new JLabel("Stilling:"), info, gridbag, c); makeField(positionField, info, gridbag, c); positionField.setText(getText("position")); positionField.setEditable(false); makeLabel(new JLabel("Gateadresse:"), info, gridbag, c); makeField(addressField, info, gridbag, c); addressField.setText(getText("address")); addressField.setEditable(false); makeLabel(new JLabel("Kontor:"), info, gridbag, c); makeField(officeField, info, gridbag, c); officeField.setText(getText("office")); officeField.setEditable(false); setLayout(new BorderLayout()); JLabel label = new JLabel(); label.setFont(label.getFont().deriveFont((float)15)); label.setBorder(BorderFactory.createEmptyBorder(5,5,5,5)); label.setHorizontalAlignment(SwingConstants.CENTER); label.setText(extractValue(person,"name")); JPanel topPanel = new JPanel(); topPanel.setLayout(new FlowLayout()); topPanel.add(label); JButton importButton = new JButton("Bruk info"); importButton.setToolTipText("<html>Bruk denne informasjonen om personen.<br>Disse opplysningene vil erstatte eventuelle data du har fra før</html>"); importButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { importButton_actionPerformed(e); } }); topPanel.add(importButton); add(topPanel, BorderLayout.NORTH); add(info, BorderLayout.CENTER); } public String getField(String name) { return extractValue(person,name); } private void importButton_actionPerformed(ActionEvent e) { p.fireDataImport(e); } private void makeLabel(Component comp, Container p, GridBagLayout gridbag, GridBagConstraints c) { c.gridwidth = 1; c.anchor = GridBagConstraints.EAST; c.insets = new Insets(3,3,3,3); gridbag.setConstraints(comp,c); p.add(comp); } public String getText(String name) { if(name.equals("organization")) { return getOrganization(); } else { return extractValue(person,name); } } private void makeField(JComponent component, Container p, GridBagLayout gridbag, GridBagConstraints c) { c.gridwidth = GridBagConstraints.REMAINDER; c.anchor = GridBagConstraints.WEST; gridbag.setConstraints(component,c); p.add(component); } private String getOrganization() { NodeList nl = person.getElementsByTagName("organization"); StringBuffer value = new StringBuffer(); if(nl.getLength() > 0) { // For all <unit>s NodeList us = ((Element) nl.item(0)).getElementsByTagName("unit"); for(int i = 0; i < us.getLength(); i++) { NodeList children = us.item(i).getChildNodes(); // Extract text from child nodes for(int k = 0; k < children.getLength(); k++) { if(children.item(k) instanceof Text) { value.append(((Text) children.item(k)).getData()); } } // Append a linebreak between <unit>s if(i < us.getLength()-1) { value.append("\n"); } } return value.toString(); } else { return ""; } } public Dimension getPreferredScrollableViewportSize() { return new Dimension(400,150); } public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) { return 20; } public boolean getScrollableTracksViewportHeight() { return false; } public boolean getScrollableTracksViewportWidth() { return true; } public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) { return 4; } } private static String extractValue(Element e, String name) { NodeList nl = e.getElementsByTagName(name); StringBuffer value = new StringBuffer(); if(nl.getLength() > 0) { NodeList children = nl.item(0).getChildNodes(); for(int i = 0; i < children.getLength(); i++) { if(children.item(i) instanceof Text) { value.append(((Text) children.item(i)).getData()); } } return value.toString(); } else { return ""; } } }