1 24 25 package org.objectweb.cjdbc.console.views; 26 27 import java.awt.Dimension ; 28 import java.awt.GridLayout ; 29 import java.awt.event.ActionEvent ; 30 import java.io.File ; 31 import java.io.FileOutputStream ; 32 import java.io.PrintStream ; 33 34 import javax.swing.AbstractAction ; 35 import javax.swing.JFileChooser ; 36 import javax.swing.JFrame ; 37 import javax.swing.JMenu ; 38 import javax.swing.JMenuBar ; 39 import javax.swing.JMenuItem ; 40 import javax.swing.JOptionPane ; 41 import javax.swing.JPanel ; 42 import javax.swing.JScrollPane ; 43 import javax.swing.JTable ; 44 import javax.swing.KeyStroke ; 45 import javax.swing.WindowConstants ; 46 import javax.swing.table.AbstractTableModel ; 47 48 import org.objectweb.cjdbc.common.i18n.Translate; 49 50 57 public abstract class InfoViewer 58 { 59 60 private InfoTableSorter sorter; 61 private JPanel panel; 62 private JFrame frame; 63 private InfoTableModel model; 64 65 private String [] columnNames; 67 protected String frameTitle; 68 protected String infoViewerMenuBarString; 69 protected String actionToolTipText; 70 protected String actionErrorMessage; 71 protected String actionSuccessMessage; 72 protected String tableHeaderToolTipText; 73 74 private Object [][] data; 75 76 81 public InfoViewer(Object [][] data) 82 { 83 if (data != null) 84 { 85 this.data = getDataTypes(data); 86 columnNames = getColumnNames(); 87 setLabels(); 88 } 89 } 90 91 98 protected abstract Object [][] getDataTypes(Object [][] stats); 99 100 105 public abstract String [] getColumnNames(); 106 107 112 public int[] getTraceableColumns() 113 { 114 return new int[0]; 115 } 116 117 120 public abstract void setLabels(); 121 122 127 public void updateData(Object [][] data) 128 { 129 this.data = getDataTypes(data); 130 if (frame != null) 131 { 132 frame.repaint(); 133 frame.setVisible(true); 134 model.setData(data); 135 } 136 else 137 { 138 createAndShowGUI(); 139 } 140 } 141 142 146 private void createAndShowGUI() 147 { 148 panel = new JPanel (new GridLayout (1, 0)); 149 model = new InfoTableModel(data); 150 151 sorter = new InfoTableSorter(model); 152 JTable table = new JTable (sorter); sorter.addMouseListenerToHeaderInTable(table); table.setPreferredScrollableViewportSize(new Dimension (640, 200)); 156 table.getColumnModel().getColumn(0).setPreferredWidth(340); 157 for (int i = 1; i < columnNames.length; i++) 158 table.getColumnModel().getColumn(i).setPreferredWidth(50); 159 160 table.getTableHeader().setToolTipText(tableHeaderToolTipText); 162 163 JScrollPane scrollPane = new JScrollPane (table); 165 166 panel.add(scrollPane); 168 169 frame = new JFrame (frameTitle); 171 frame.setJMenuBar(new InfoViewerMenuBar()); 172 frame.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE); 173 panel.setOpaque(true); frame.setContentPane(panel); 175 176 frame.pack(); 178 frame.setVisible(true); 179 } 180 181 187 public String displayText(Object [][] data) 188 { 189 this.data = getDataTypes(data); 190 columnNames = getColumnNames(); 191 setLabels(); 192 return displayText(getDataTypes(data)); 193 } 194 195 201 public String displayText(String [][] data) 202 { 203 if (data == null) 204 { 205 return ""; 206 } 207 208 209 210 final String columnPadding = " "; 212 final String nameValueSeparator = ": "; 213 214 String [] columns = getColumnNames(); 216 217 int maxNameLength = 0; 220 for (int i = 0; i < columns.length; i++) 221 { 222 maxNameLength = Math.max(maxNameLength, columns[i].length()); 223 } 224 225 int maxValueLength = 0; 228 for (int i = 0; i < data.length; i++) 229 { 230 for (int j = 0; j < data[i].length; j++) 231 { 232 maxValueLength = Math.max(maxValueLength, data[i][j].length()); 233 } 234 } 235 236 char[] separator = new char[columnPadding.length() + maxNameLength 239 + nameValueSeparator.length() + maxValueLength + 1]; 243 for (int i = 0; i < separator.length; i++) 244 { 245 separator[i] = '-'; 246 } 247 separator[separator.length - 1] = '\n'; 248 249 StringBuffer sb = new StringBuffer (); 251 for (int i = 0; i < data.length; i++) 252 { 253 sb.append(separator); 254 for (int j = 0; j < data[i].length; j++) 255 { 256 char[] namePadding = new char[maxNameLength - columns[j].length()]; 259 for (int x = 0; x < namePadding.length; x++) 260 { 261 namePadding[x] = ' '; 262 } 263 264 sb.append(columnPadding); 265 sb.append(columns[j]); 266 sb.append(nameValueSeparator); 267 sb.append(namePadding); 268 sb.append(data[i][j]); 269 sb.append("\n"); 270 } 271 if (i + 1 == data.length) 272 { 273 sb.append(separator); 274 } 275 } 276 return sb.toString(); 277 } 278 279 282 public void display() 283 { 284 javax.swing.SwingUtilities.invokeLater(new Runnable () 287 { 288 public void run() 289 { 290 createAndShowGUI(); 291 } 292 }); 293 } 294 295 296 protected final class InfoViewerMenuBar extends JMenuBar 297 { 298 299 300 private InfoViewerMenuBar() 301 { 302 JMenu menu = new JMenu (infoViewerMenuBarString); 303 JMenuItem menuItem = new JMenuItem (new ExportAction()); 304 menuItem.setText("Save As..."); 305 menuItem.setMnemonic('S'); 306 menuItem.setAccelerator(KeyStroke 307 .getKeyStroke('s', ActionEvent.CTRL_MASK)); 308 menu.add(menuItem); 309 add(menu); 310 } 311 } 312 313 314 protected class ExportAction extends AbstractAction 315 { 316 317 protected static final String SEPARATOR = "\t"; 318 protected File outputFile; 319 320 323 public void actionPerformed(ActionEvent e) 324 { 325 JFileChooser chooser = new JFileChooser (outputFile); 327 chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); 328 chooser.setApproveButtonText("Export"); 329 chooser.setApproveButtonMnemonic('s'); 330 chooser.setApproveButtonToolTipText(actionToolTipText); 331 chooser.setDialogTitle("Choose the file name"); 332 333 if (chooser.showSaveDialog(frame) == JFileChooser.APPROVE_OPTION) 334 { 335 outputFile = chooser.getSelectedFile(); 336 if (outputFile != null) 337 { 338 try 340 { 341 PrintStream out = new PrintStream (new FileOutputStream (outputFile)); 342 int columnNumber, rowNumber; 343 columnNumber = sorter.getColumnCount(); 344 rowNumber = sorter.getRowCount(); 345 for (int i = 0; i < rowNumber; i++) 346 { 347 for (int j = 0; j < columnNumber; j++) 348 { 349 out.print(sorter.getValueAt(i, j)); 350 out.print(SEPARATOR); 351 } 352 out.println(); 353 } 354 out.close(); 355 } 356 catch (Exception ex) 357 { 358 JOptionPane.showMessageDialog(frame, Translate.get( 359 actionErrorMessage, ex), "Unexpected Error", 360 JOptionPane.ERROR_MESSAGE); 361 return; 362 } 363 JOptionPane.showMessageDialog(frame, Translate.get( 364 actionSuccessMessage, outputFile), "Action Performed", 365 JOptionPane.INFORMATION_MESSAGE); 366 } 367 } 368 } 369 } 370 371 376 class InfoTableModel extends AbstractTableModel 377 { 378 private Object [][] data; 379 380 385 public InfoTableModel(Object [][] stats) 386 { 387 this.data = stats; 388 } 389 390 395 public void setData(Object [][] data) 396 { 397 this.data = data; 398 } 399 400 403 public int getColumnCount() 404 { 405 return columnNames.length; 406 } 407 408 411 public int getRowCount() 412 { 413 return data.length; 414 } 415 416 419 public String getColumnName(int col) 420 { 421 return columnNames[col]; 422 } 423 424 427 public Object getValueAt(int row, int col) 428 { 429 return data[row][col]; 430 } 431 432 437 public Class getColumnClass(int c) 438 { 439 return getValueAt(0, c).getClass(); 440 } 441 442 445 public boolean isCellEditable(int row, int col) 446 { 447 return false; 448 } 449 } 450 451 454 public String getFrameTitle() 455 { 456 return frameTitle; 457 } 458 459 462 public Object [][] getData() 463 { 464 return data; 465 } 466 } | Popular Tags |