00001
00025
package org.objectweb.cjdbc.console.views;
00026
00027
import java.awt.Dimension;
00028
import java.awt.GridLayout;
00029
import java.awt.event.ActionEvent;
00030
import java.io.File;
00031
import java.io.FileOutputStream;
00032
import java.io.PrintStream;
00033
00034
import javax.swing.AbstractAction;
00035
import javax.swing.JFileChooser;
00036
import javax.swing.JFrame;
00037
import javax.swing.JMenu;
00038
import javax.swing.JMenuBar;
00039
import javax.swing.JMenuItem;
00040
import javax.swing.JOptionPane;
00041
import javax.swing.JPanel;
00042
import javax.swing.JScrollPane;
00043
import javax.swing.JTable;
00044
import javax.swing.KeyStroke;
00045
import javax.swing.WindowConstants;
00046
import javax.swing.table.AbstractTableModel;
00047
00048
import org.objectweb.cjdbc.common.i18n.Translate;
00049
00057 public abstract class InfoViewer
00058 {
00059
00060 private InfoTableSorter sorter;
00061 private JPanel
panel;
00062 private JFrame
frame;
00063 private InfoTableModel model;
00064
00065
00066 private String[]
columnNames;
00067 protected String
frameTitle;
00068 protected String
infoViewerMenuBarString;
00069 protected String
actionToolTipText;
00070 protected String
actionErrorMessage;
00071 protected String
actionSuccessMessage;
00072 protected String
tableHeaderToolTipText;
00073
00074 private Object[][]
data;
00075
00081 public InfoViewer(Object[][] data)
00082 {
00083
if (data != null)
00084 {
00085
this.data =
getDataTypes(data);
00086
columnNames =
getColumnNames();
00087
setLabels();
00088 }
00089 }
00090
00098
protected abstract Object[][]
getDataTypes(Object[][] stats);
00099
00105
public abstract String[]
getColumnNames();
00106
00112 public int[]
getTraceableColumns()
00113 {
00114
return new int[0];
00115 }
00116
00120
public abstract void setLabels();
00121
00127 public void updateData(Object[][] data)
00128 {
00129
this.data =
getDataTypes(data);
00130
if (
frame != null)
00131 {
00132
frame.repaint();
00133
frame.setVisible(
true);
00134
model.
setData(data);
00135 }
00136
else
00137 {
00138
createAndShowGUI();
00139 }
00140 }
00141
00146 private void createAndShowGUI()
00147 {
00148
panel =
new JPanel(
new GridLayout(1, 0));
00149
model =
new InfoTableModel(
data);
00150
00151
sorter =
new InfoTableSorter(
model);
00152 JTable table =
new JTable(
sorter);
00153
sorter.
addMouseListenerToHeaderInTable(table);
00154
00155 table.setPreferredScrollableViewportSize(
new Dimension(640, 200));
00156 table.getColumnModel().getColumn(0).setPreferredWidth(340);
00157
for (
int i = 1; i <
columnNames.length; i++)
00158 table.getColumnModel().getColumn(i).setPreferredWidth(50);
00159
00160
00161 table.getTableHeader().setToolTipText(
tableHeaderToolTipText);
00162
00163
00164 JScrollPane scrollPane =
new JScrollPane(table);
00165
00166
00167
panel.add(scrollPane);
00168
00169
00170
frame =
new JFrame(
frameTitle);
00171
frame.setJMenuBar(
new InfoViewerMenuBar());
00172
frame.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
00173
panel.setOpaque(
true);
00174
frame.setContentPane(
panel);
00175
00176
00177
frame.pack();
00178
frame.setVisible(
true);
00179 }
00180
00187 public String
displayText(Object[][] data)
00188 {
00189
this.data =
getDataTypes(data);
00190
columnNames =
getColumnNames();
00191
setLabels();
00192
return displayText(
getDataTypes(data));
00193 }
00194
00201 public String
displayText(String[][] data)
00202 {
00203
if (data == null)
00204 {
00205
return "";
00206 }
00207
00208
00209
00210
00211
final String columnPadding =
" ";
00212
final String nameValueSeparator =
": ";
00213
00214
00215 String[] columns =
getColumnNames();
00216
00217
00218
00219
int maxNameLength = 0;
00220
for (
int i = 0; i < columns.length; i++)
00221 {
00222 maxNameLength = Math.max(maxNameLength, columns[i].length());
00223 }
00224
00225
00226
00227
int maxValueLength = 0;
00228
for (
int i = 0; i < data.length; i++)
00229 {
00230
for (
int j = 0; j < data[i].length; j++)
00231 {
00232 maxValueLength = Math.max(maxValueLength, data[i][j].length());
00233 }
00234 }
00235
00236
00237
00238
char[] separator =
new char[columnPadding.length() + maxNameLength
00239 + nameValueSeparator.length() + maxValueLength + 1];
00240
00241
00242
00243
for (
int i = 0; i < separator.length; i++)
00244 {
00245 separator[i] =
'-';
00246 }
00247 separator[separator.length - 1] =
'\n';
00248
00249
00250 StringBuffer sb =
new StringBuffer();
00251
for (
int i = 0; i < data.length; i++)
00252 {
00253 sb.append(separator);
00254
for (
int j = 0; j < data[i].length; j++)
00255 {
00256
00257
00258
char[] namePadding =
new char[maxNameLength - columns[j].length()];
00259
for (
int x = 0; x < namePadding.length; x++)
00260 {
00261 namePadding[x] =
' ';
00262 }
00263
00264 sb.append(columnPadding);
00265 sb.append(columns[j]);
00266 sb.append(nameValueSeparator);
00267 sb.append(namePadding);
00268 sb.append(data[i][j]);
00269 sb.append(
"\n");
00270 }
00271
if (i + 1 == data.length)
00272 {
00273 sb.append(separator);
00274 }
00275 }
00276
return sb.toString();
00277 }
00278
00282 public void display()
00283 {
00284
00285
00286 javax.swing.SwingUtilities.invokeLater(
new Runnable()
00287 {
00288
public void run()
00289 {
00290
createAndShowGUI();
00291 }
00292 });
00293 }
00294
00296 protected class InfoViewerMenuBar extends JMenuBar
00297 {
00298
00300 private InfoViewerMenuBar()
00301 {
00302 JMenu menu =
new JMenu(infoViewerMenuBarString);
00303 JMenuItem menuItem =
new JMenuItem(
new ExportAction());
00304 menuItem.setText(
"Save As...");
00305 menuItem.setMnemonic(
'S');
00306 menuItem.setAccelerator(KeyStroke
00307 .getKeyStroke(
's', ActionEvent.CTRL_MASK));
00308 menu.add(menuItem);
00309 add(menu);
00310 }
00311 }
00312
00314 protected class ExportAction extends AbstractAction
00315 {
00316
00317 protected static final String
SEPARATOR =
"\t";
00318 protected File
outputFile;
00319
00323 public void actionPerformed(ActionEvent e)
00324 {
00325
00326 JFileChooser chooser =
new JFileChooser(
outputFile);
00327 chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
00328 chooser.setApproveButtonText(
"Export");
00329 chooser.setApproveButtonMnemonic(
's');
00330 chooser.setApproveButtonToolTipText(actionToolTipText);
00331 chooser.setDialogTitle(
"Choose the file name");
00332
00333
if (chooser.showSaveDialog(frame) == JFileChooser.APPROVE_OPTION)
00334 {
00335
outputFile = chooser.getSelectedFile();
00336
if (
outputFile != null)
00337 {
00338
00339
try
00340 {
00341 PrintStream out =
new PrintStream(
new FileOutputStream(
outputFile));
00342
int columnNumber, rowNumber;
00343 columnNumber = sorter.getColumnCount();
00344 rowNumber = sorter.getRowCount();
00345
for (
int i = 0; i < rowNumber; i++)
00346 {
00347
for (
int j = 0; j < columnNumber; j++)
00348 {
00349 out.print(sorter.getValueAt(i, j));
00350 out.print(
SEPARATOR);
00351 }
00352 out.println();
00353 }
00354 out.close();
00355 }
00356
catch (Exception ex)
00357 {
00358 JOptionPane.showMessageDialog(frame,
Translate.get(
00359 actionErrorMessage, ex),
"Unexpected Error",
00360 JOptionPane.ERROR_MESSAGE);
00361
return;
00362 }
00363 JOptionPane.showMessageDialog(frame,
Translate.get(
00364 actionSuccessMessage,
outputFile),
"Action Performed",
00365 JOptionPane.INFORMATION_MESSAGE);
00366 }
00367 }
00368 }
00369 }
00370
00377 class InfoTableModel extends AbstractTableModel
00378 {
00379 private Object[][]
data;
00380
00386 public InfoTableModel(Object[][] stats)
00387 {
00388
this.data = stats;
00389 }
00390
00397 public void setData(Object[][] data)
00398 {
00399
this.data = data;
00400 }
00401
00406 public int getColumnCount()
00407 {
00408
return columnNames.length;
00409 }
00410
00415 public int getRowCount()
00416 {
00417
return data.length;
00418 }
00419
00424 public String
getColumnName(
int col)
00425 {
00426
return columnNames[col];
00427 }
00428
00433 public Object
getValueAt(
int row,
int col)
00434 {
00435
return data[row][col];
00436 }
00437
00443 public Class
getColumnClass(
int c)
00444 {
00445
return getValueAt(0, c).getClass();
00446 }
00447
00451 public boolean isCellEditable(
int row,
int col)
00452 {
00453
return false;
00454 }
00455 }
00456
00460 public String
getFrameTitle()
00461 {
00462
return frameTitle;
00463 }
00464
00468 public Object[][]
getData()
00469 {
00470
return data;
00471 }
00472 }