📄 staxmonitorutil.java
字号:
/*****************************************************************************//* Software Testing Automation Framework (STAF) *//* (C) Copyright IBM Corp. 2002, 2004 *//* *//* This software is licensed under the Common Public License (CPL) V1.0. *//*****************************************************************************/package com.ibm.staf.service.stax;import java.awt.*;import java.awt.event.*;import java.util.*;import java.io.*;import java.lang.Math.*;import javax.swing.*;import javax.swing.border.*;import javax.swing.event.*;import javax.swing.tree.*;import javax.swing.table.*;import java.lang.reflect.*;import com.ibm.staf.*;import com.ibm.staf.service.*;import java.text.SimpleDateFormat;public class STAXMonitorUtil{ private static STAFHandle mainMonitorHandle = null; private static Vector staticHandleVector = new Vector(); private static Hashtable nonStaticHandleTable = new Hashtable(); private static boolean debug = false; public static STAFHandle getNewSTAFHandle(String name) throws STAFException { // First ensure that we have a mainMonitorHandle // Next try to see if the user has ALLOWMULTIREG configured // Next try a static handle STAFException allowMultiRegException = null; if (mainMonitorHandle == null) { try { mainMonitorHandle = new STAFHandle("STAXMonitor/MainHandle"); if (debug) System.out.println("Created mainMonitorHandle: " + mainMonitorHandle.getHandle()); } catch (STAFException e) { // could not create the mainMonitorHandle throw e; } } STAFHandle newHandle = null; try { newHandle = new STAFHandle(name); if (newHandle.getHandle() == mainMonitorHandle.getHandle()) { // ALLOWMULTIREG is not set; newHandle = null; } else { nonStaticHandleTable.put(new Integer(newHandle.getHandle()), newHandle); if (debug) System.out.println("Created multireg handle " + name + ": " + newHandle.getHandle()); } } catch (STAFException e) { // ALLOWMULTIREG is not set, newHandle will be null // Save the exception since we may need to throw it allowMultiRegException = e; } if (newHandle == null) { STAFResult createHandleResult = mainMonitorHandle.submit2( "local", "HANDLE", "CREATE HANDLE NAME " + STAFUtil.wrapData(name)); if (createHandleResult.rc == 0) { try { newHandle = new STAFHandle(new Integer(createHandleResult.result).intValue()); } catch (NoSuchMethodError ex) { throw allowMultiRegException; } staticHandleVector.add(new Integer(createHandleResult.result)); if (debug) System.out.println("Created static handle " + name + ": " + newHandle.getHandle()); } else { if (debug) System.out.println("RC:" + createHandleResult.rc + " " + createHandleResult.result); throw allowMultiRegException; } } return newHandle; } public static void freeHandle(int handle) { if (staticHandleVector.contains(new Integer(handle))) { staticHandleVector.remove(new Integer(handle)); STAFResult deleteHandleResult = mainMonitorHandle.submit2( "local", "HANDLE", "DELETE HANDLE " + handle); if (debug) { System.out.println("Deleting handle " + handle); } if (deleteHandleResult.rc != 0) { System.out.println("Error deleting handle " + handle + "rc=" + deleteHandleResult.rc + " " + deleteHandleResult.result); } } else if (nonStaticHandleTable.containsKey(new Integer(handle))) { STAFHandle unRegHandle = (STAFHandle)nonStaticHandleTable.get(new Integer(handle)); try { if (debug) System.out.println("Unregistering handle " + handle); unRegHandle.unRegister(); } catch (STAFException e) { // Ignore since this can occur if STAF is shutdown before // the STAX Monitor } nonStaticHandleTable.remove(new Integer(handle)); } } public static void cleanupHandles() { if (!(staticHandleVector.isEmpty())) { for (int i = 0; i < staticHandleVector.size(); i++) { int handle = ((Integer)(staticHandleVector.elementAt(i))).intValue(); staticHandleVector.remove(new Integer(handle)); STAFResult deleteHandleResult = mainMonitorHandle.submit2( "local", "handle", "delete handle " + handle); if (debug) { System.out.println("Cleaning up handle " + handle); } if (deleteHandleResult.rc != 0) { System.out.println("Error deleting handle " + handle + " rc=" + deleteHandleResult.rc + " " + deleteHandleResult.result); } } } Enumeration nonStaticHandles = nonStaticHandleTable.elements(); while (nonStaticHandles.hasMoreElements()) { STAFHandle handle = (STAFHandle)nonStaticHandles.nextElement(); try { if (debug) { System.out.println("Cleaning up handle " + handle.getHandle()); } handle.unRegister(); } catch (STAFException e) { // Ignore since can happen if STAF is shutdown before // the STAX Monitor is exited } } } public static void unregisterMainHandle() throws STAFException { mainMonitorHandle.unRegister(); if (debug) System.out.println("Unregistering main handle " + mainMonitorHandle.getHandle()); } public static void sizeColumnsToFitText(JTable table) { int tableWidth = 0; FontMetrics metrics = table.getFontMetrics(table.getFont()); for (int i = 0; i < table.getColumnCount(); i++) { int width = 0; int maxWidth = 0; Vector data = new Vector(); data.addElement(table.getColumnModel().getColumn(i). getHeaderValue()); for (int j = 0; j < table.getRowCount(); j++) { try { Object obj = table.getValueAt(j,i); String cellText = ""; if (obj != null) { cellText = table.getValueAt(j,i).toString(); } BufferedReader reader = new BufferedReader(new StringReader(cellText)); String line; try { while ((line = reader.readLine()) != null) { data.addElement(line); } } catch(IOException ex) { ex.printStackTrace(); } finally { try { reader.close(); } catch (IOException ex) { ex.printStackTrace(); } } } catch(Exception ex) { ex.printStackTrace(); } } Enumeration e = data.elements(); while (e.hasMoreElements()) { width = metrics.stringWidth((String)e.nextElement()); if (width > maxWidth) { maxWidth = width; } } Insets insets = ((JComponent)table.getCellRenderer(0,i)).getInsets(); // need to pad a little extra for everything to look right maxWidth += insets.left + insets.right + (maxWidth*.15); table.getColumnModel().getColumn(i).setPreferredWidth(maxWidth); tableWidth += maxWidth; } Dimension d = table.getSize(); d.width = tableWidth; table.setSize(d); } public static String getElapsedTime(Calendar started) { if (started == null) { return ""; } SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss"); SimpleDateFormat dayOfYearFormat = new SimpleDateFormat("D"); SimpleDateFormat yearFormat = new SimpleDateFormat("y"); Calendar currTimestamp = Calendar.getInstance(); long startMillis = started.getTime().getTime(); long currentMillis = currTimestamp.getTime().getTime(); long elapsedMillis = currentMillis - startMillis; long seconds = elapsedMillis / 1000 % 60; long minutes = elapsedMillis / 1000 / 60 % 60; long hours = elapsedMillis / 1000 / 60 / 60; //long hours = elapsedMillis / 1000 / 60 / 60 % 24; //long days = elapsedMillis / 1000 / 60 / 60 / 24; String elapsedTime = ""; /* if (days > 0) { elapsedTime = (new Long(days)).toString(); if (days == 1) elapsedTime += " day, "; else elapsedTime += " days, "; } */ elapsedTime += getTimeFormat((new Long(hours)).toString()) + ":"; elapsedTime += getTimeFormat((new Long(minutes)).toString()) + ":"; elapsedTime += getTimeFormat((new Long(seconds)).toString()); return elapsedTime; } public static String getTimeFormat(String in) { if (in.length() == 1) { return "0" + in; } else { return in; } } public static void updateRowHeights(JTable table, int multiLineColumn) { int numLines = 1; for (int i = 0 ; i < table.getRowCount() ; i++) { int height = (new JTextArea((String)table.getValueAt(i, multiLineColumn))).getPreferredSize().height + 5; table.setRowHeight(i, height); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -