📄 cbutility.java
字号:
{
if (debugLevel >= level) log(S);
}
*/
/**
* Simple logging utility. Writes log data to a file or console,
* or ignores it, depending on the value of the logging and logfile
* property (defaults set in JXplorer.java, user sets in dxconfig.txt)
*/
/*
public static void log(String S)
{
S = (new Date(System.currentTimeMillis())).toString() + ": " + S;
switch (loggingStyle)
{
case NOLOG: break; // do nothing
case CONSOLEANDFILE: // log file and console...
case FILE: try // log file only
{
logfile.write(S + "\n");
logfile.flush();
}
catch (Exception e)
{
CBUtility.log("unable to write to log file\nreverting to console\n" + e + "\n"+S);
loggingStyle = CONSOLE;
}
if (loggingStyle == FILE) break;
case CONSOLE: // console
default: System.out.println(S); break; // echo to console
}
}
*/
public static void initDefaultDisplay(Frame owner)
{
displayFrame = owner;
}
public static Frame getDefaultDisplay()
{
return displayFrame;
}
/**
* utility ftn; prints error message to user, and echos to the log ftn.
*
* @return returns false for easy chaining.
*/
public static boolean error(Component owner, String Msg)
{
return error(getParentFrame(owner), Msg, null);
}
/**
* utility ftn; prints error message to user, and echos to the log ftn.
*
* @return returns false for easy chaining.
*/
public static boolean error(Frame owner, String Msg)
{
if (displayFrame == null) // no default display registered
{
log.warning("graphics error: error display not initialised! (root error was: " + Msg + ")");
return false;
}
return error(owner, Msg, null);
}
/**
* utility ftn; prints error message to user, and echos to the log ftn.
*
* @return returns false for easy chaining.
*/
public static boolean error(String Msg)
{
if (displayFrame == null) // no default display registered
{
log.warning("graphics error: error display not initialised! (error was: " + Msg + ")");
return false;
}
return error(displayFrame, Msg, null);
}
/**
* wrapper for the JFrame version of error.
*
* @param Msg a short one line message to display to the user
* @param e the exception to log
* @return returns false for easy chaining.
*/
public static boolean error(String Msg, Exception e)
{
return error(displayFrame, Msg, e);
}
/**
* wrapper for the JFrame version of error.
*
* @param owner the component (from which the parent Frame will be derived)
* @param Msg a short one line message to display to the user
* @param e the exception to log
* @return returns false for easy chaining.
*/
public static boolean error(Component owner, String Msg, Exception e)
{
return error(getParentFrame(owner), Msg, e);
}
/**
* utility ftn; prints error message and the error to the user,
* and echos to the log ftn.
*
* @param owner the parent Frame (required for dialog box drawing)
* @param Msg a short one line message to display to the user
* @param e the exception to log
* @return returns false for easy chaining.
*/
public static boolean error(Frame owner, String Msg, Exception e)
{
if (owner == null) //TE: added this check basically so that I can centre the error window...i.e if there is no owner - there is nothing to centre upon!
{
if (displayFrame == null) // no default display registered
{
log.warning("error display not initialised! (error was: " + Msg + ")");
return false;
}
else
{
owner = displayFrame;
}
}
CBErrorWin errWin = new CBErrorWin(owner, Msg, e);
log.log(Level.WARNING, "error displayed to user: " + Msg, e);
return false;
}
/**
* Utility function. Opens a dialog with a confirmation message.
*
* @param Msg the confirmation message to be displayed.
*/
public static void confirm(String Msg)
{
if (displayFrame == null) // no default display registered
{
log.warning("error display not initialised! (error was: " + Msg + ")");
return;
}
new CBErrorWin(displayFrame, Msg, "Confirmation Message");
}
/**
* utility ftn; prints warning dialog message to the user,
* *without* echoing to the log ftn. Basically wrapper to JOptionPane
*
* @param caller the GUI component calling (required for dialog box drawing)
* @param Msg a short one line message to display to the user
* @return returns false for easy chaining.
*/
public static boolean warning(Component caller, String Msg, String Title)
{
JOptionPane.showMessageDialog(caller, Msg,
Title, JOptionPane.WARNING_MESSAGE);
return false; // for chaining
}
/**
* Short version of warning method - uses default frame, and has the
* title 'Warning'.
*
* @param Msg the warning message to display.
*/
public static boolean warning(String Msg)
{
if (displayFrame == null) // no default display registered
{
log.warning("warning display not initialised! (error was: " + Msg + ")");
return false;
}
return warning(displayFrame, Msg, "Warning");
}
/**
* prints an enumeration...
*/
public static void printEnumeration(Enumeration e)
{
while (e.hasMoreElements())
{
Object raw = e.nextElement();
String value = (raw == null) ? "*null*" : raw.toString();
System.out.println(" " + value);
}
}
/**
* Iterates through a components parents until it finds the
* root frame. Useful for initing JDialogs etc. that require
* a root frame to work properly.
*/
public static Frame getParentFrame(Component c)
{
if (c == null) return null;
Component parent = c.getParent();
while (!(parent instanceof Frame) && (parent != null))
parent = parent.getParent();
return (parent == null) ? null : (Frame) parent;
}
/**
* Converts a 'dos' style file path to a unix style file path
* by exchanging '\' characters for for '/' characters.
*/
public static String convertPathToUnix(String dosPath)
{
String ret = dosPath.replace('\\', '/');
return ret;
}
/**
* This positions a component to the center of another component.
* If both components are showing on the sceen, it uses absolute
* screen co-ordinates, otherwise if only the positioner component
* is showing, it uses relative co-ordinates (since it is unable to
* obtain screen co-ords). If the components share a reference
* frame, these two actions are equivalent (i.e. if they both have
* the same parent). If nothing is showing, the component is unchanged.
* NOTE: if the X & Y coordinates are off the screen, the component to
* center will be centered in the middle of the screen.
*
* @param centerMe the component to center
* @param positioner the component used as the reference center. If null,
* the component will be centered on the screen
*/
public static void center(Component centerMe, Component positioner)
{
if (centerMe == null) return;
if (positioner != null && positioner.isShowing())
{
Rectangle pos = positioner.getBounds(); // relative info.
Point absPos = positioner.getLocationOnScreen(); // absolute info.
int centerX = absPos.x + (pos.width / 2); // center x pos, in screen co-ords
int centerY = absPos.y + (pos.height / 2); // center y pos, in screen co-ords
pos = centerMe.getBounds(); // relative info;
int x = 0;
int y = 0;
if (centerMe.isShowing()) // if centerMe is showing, center it using screen co-ords (no possibility of error)
{
absPos = centerMe.getLocationOnScreen(); // absolute info;
int currentX = absPos.x + (pos.width / 2); // center of centerMe x pos, in screen co-ords
int currentY = absPos.y + (pos.height / 2); // center of centerMe y pos, in screen co-ords
int deltaX = centerX - currentX; // amount to move X
int deltaY = centerY - currentY; // amount to move Y
x = pos.x + deltaX;
y = pos.y + deltaY;
}
else // centerMe isn't showing - can't use screen co-ords, so *assume* both positioner and centerMe have same reference frame
{ // (i.e. components share a common parent...)
x = centerX - (pos.width / 2);
y = centerY - (pos.height / 2);
}
Toolkit toolKit = Toolkit.getDefaultToolkit();
if ((x - 100) < 0 || (x + 100) > toolKit.getScreenSize().width || (y - 100) < 0 || (y + 100) > toolKit.getScreenSize().height) //TE: if off screen (add some padding/a safety margin)...
centerOnScreen(centerMe); //TE: center in middle of screen (bug 2926).
else
centerMe.setLocation(x, y); // move, using local co-ordinates.
}
else
{
centerOnScreen(centerMe);
}
}
/**
* Centers a component on the middle of the screen.
*
* @param centerMe the component to center.
*/
private static void centerOnScreen(Component centerMe)
{
Dimension screen = centerMe.getToolkit().getScreenSize();
Dimension object = centerMe.getSize();
centerMe.setLocation((int) (screen.getWidth() - object.getWidth()) / 2, (int) (screen.getHeight() - object.getHeight()) / 2);
}
/**
* @deprecated use CBParse method instead
*/
public static String bytes2Hex(byte[] bytes)
{
StringBuffer ret = new StringBuffer(bytes.length * 2);
for (int i = 0; i < bytes.length; i++)
{
ret.append(CBParse.byte2Hex(bytes[i]));
}
return ret.toString();
}
/**
* @deprecated use CBParse method instead
*/
public static String string2Hex(String orig)
{
StringBuffer ret = new StringBuffer(orig.length() * 2);
char[] c = orig.toCharArray();
for (int i = 0; i < c.length; i++)
{
ret.append(CBParse.char2Hex(c[i]));
}
return ret.toString();
}
/**
* @deprecated use CBParse method instead
*/
static public String byte2Hex(byte b)
{
// Returns hex String representation of byte b
final char hexDigit[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
char[] array = {hexDigit[(b >> 4) & 0x0f], hexDigit[b & 0x0f]};
return new String(array);
}
/**
* @deprecated use CBParse method instead
*/
static public String char2Hex(char c)
{
// Returns hex String representation of char c
byte hi = (byte) (c >>> 8);
byte lo = (byte) (c & 0xff);
return CBParse.byte2Hex(hi) + CBParse.byte2Hex(lo);
}
/**
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -