//////////////////////////////////////////////////////////////// // DataExportTable.java // // Copyright (C) 2002-2003 by ObjectPlanet, Inc. //////////////////////////////////////////////////////////////// package com.objectplanet.gui.examples; import com.objectplanet.gui.*; import java.awt.*; import java.awt.event.*; import java.awt.datatransfer.*; import java.lang.reflect.*; /** * This class adds a popup menu to export data from the table applet. * * @author Bjorn J. Kvande. */ public class DataExportTable extends Table implements TableListener, ActionListener { // constants /** * The export data popup menu item. */ public static final String MENU_EXPORT_DATA = "Export Data"; /** * The get data button label. */ private static final String BUTTON_GET_DATA = "Get Data"; /** * The select all button label. */ private static final String BUTTON_SELECT_ALL = "Select All"; // object connections /** * The popup menue for this table. */ private PopupMenu popup; /** * The data export window set by the exportData() method. */ private TextArea dataExportText; /** * The data export separator text field set by the exportData() method. */ private TextField dataExportSeparatorField; /** * A reference to the window frame for the data export. */ private Frame dataExportFrame; // construction /** * Constructs a new data export table. */ public DataExportTable() { addTableListener(this); popup = new PopupMenu(); popup.addActionListener(this); add(popup); addPopupItem(MENU_EXPORT_DATA, MENU_EXPORT_DATA); } // services /** * Adds an item to the popup menu. * @param label The label of the popup menu item. * @param command The command of the popup menu item. */ public void addPopupItem(String label, String command) { if (label != null && command != null) { MenuItem item = popup.add(new MenuItem(label)); item.setActionCommand(command); } else if (label != null) { popup.add(label); } } // overridden table methods for customization /** * Opens a frame with a text area containing the visible data in * CSV format (or any other separator you choose). * @param width The width of the window. * @param height The height of the window. * @param separator The character used to separate the fields. * @param header True if the header should be included. */ public Frame exportData(int width, int height, char separator) { if (dataExportFrame == null) { // get the data and the export window frame dataExportFrame = super.exportData(width, height, separator); dataExportFrame.addWindowListener(new WindowEventHandler()); dataExportText = (TextArea) dataExportFrame.getComponent(0); // add the ability to get the data again with a new separator Panel controls = new Panel(new FlowLayout(FlowLayout.LEFT)); dataExportSeparatorField = new TextField(1); dataExportSeparatorField.setText(separator + ""); Button getdata_button = new Button(BUTTON_GET_DATA); getdata_button.addActionListener(this); controls.add(new Label("Separator:", Label.RIGHT)); controls.add(dataExportSeparatorField); controls.add(getdata_button); // add the ability to select all Button selection_button = new Button(BUTTON_SELECT_ALL); selection_button.addActionListener(this); controls.add(selection_button); // add the controls dataExportFrame.add("South", controls); dataExportFrame.doLayout(); controls.doLayout(); controls.update(controls.getGraphics()); } else { dataExportFrame.show(); } // return the export window return dataExportFrame; } // event handlers /** * The right click listener displays the popup menu. * @param event The table event. */ public void tableSelection(TableEvent event) { // we got a right click, start a thread to display the popup int type = event.getType(); if (type == TableEvent.RIGHT_CLICK || type == TableEvent.DOUBLE_CLICK) { Point position = event.getMousePosition(); if (position != null && popup != null) { PopupThread thread = new PopupThread(position, event.getFields()); thread.start(); } } } /** * An entry in the popup menu was selected. Tell the traffic applet about it. * This also handles the export window buttons. */ public void actionPerformed(ActionEvent event) { // an entry in the popup menu was selected, send a traffic table event // notifying the listeners about the event if (event.getActionCommand().equals(MENU_EXPORT_DATA)) { exportData(620,400,'|'); } // select all the text in the action button window else if (event.getActionCommand().equals(BUTTON_SELECT_ALL)) { if (dataExportText != null) { dataExportText.requestFocus(); dataExportText.selectAll(); dataExportText.repaint(); } } // export again this time with another separator else if (event.getActionCommand().equals(BUTTON_GET_DATA)) { if (dataExportSeparatorField != null && dataExportText != null) { String separator = dataExportSeparatorField.getText(); if (separator != null && separator.length() > 0) { String data = getExportData(separator.charAt(0)); dataExportText.requestFocus(); dataExportText.setText(data); dataExportText.selectAll(); } } } } /** * Handles the export window open and close, and makes sure only one * copy of the export window is opened. */ class WindowEventHandler extends WindowAdapter { public void windowClosed(WindowEvent e) { if (e.getSource() == dataExportFrame) { dataExportFrame = null; } } } // thread that displays the popup, this is done becaus // the popup.show() call blocks until something is selected /** * Thread used to display the thread. */ class PopupThread extends Thread { /** * The position of popup menu - this is the mouse position. */ private Point position; /** * The row that was selected when right-clicking. */ private Object[] row; /** * Creates a new popup thread. * @param position The position of the popup menu. * @param row The row that was selected when right-clicking. */ public PopupThread(Point position, Object[] row) { this.position = position; this.row = row; } /** * Displays the popup. */ public void run() { if (popup != null) { popup.show(DataExportTable.this, position.x, position.y+10); } } } }