1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
|
////////////////////////////////////////////////////////////////
// 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);
}
}
}
} |
Partager