📄 uiutil.java
字号:
/*
* SSL-Explorer
*
* Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
package com.sshtools.ui.swing;
import java.util.StringTokenizer;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JList;
import javax.swing.KeyStroke;
import javax.swing.SwingConstants;
/**
* Useful UI utilies.
*
* @author $Author: brett $
* @version $Revision: 1.8 $
*/
public class UIUtil implements SwingConstants {
/**
* Select an item in a {@link JList} given an items string value
*
* @param string string to select in list
* @param list list
*/
public static void selectStringInList(String string, JList list) {
for (int i = 0; i < list.getModel().getSize(); i++) {
if (String.valueOf(list.getModel().getElementAt(i)).equals(string)) {
list.setSelectedIndex(i);
return;
}
}
}
/**
* Select an item in a {@link JComboBox} given an items string value
*
* @param string string to select in list
* @param list list
*/
public static void selectStringInList(String string, JComboBox list) {
for (int i = 0; i < list.getModel().getSize(); i++) {
if (String.valueOf(list.getModel().getElementAt(i)).equals(string)) {
list.setSelectedIndex(i);
return;
}
}
}
/**
* Convert a string in the format of <code>x,y,width,height</code> in to a
* {@link Rectangle} object. Suitable for retrieving rectangles from
* property files, XML files etc. The supplied default value will be
* returned if the string is not in the correct format or is
* <code>null</code>.
*
* @param string string in format <code>x,y,width,height</code>
* @param defaultValue default rectangle
* @return rectangle
*/
public static Rectangle stringToRectangle(String string, Rectangle defaultValue) {
if (string == null) {
return defaultValue;
}
StringTokenizer t = new StringTokenizer(string, ",");
try {
return new Rectangle(Integer.parseInt(t.nextToken()), Integer.parseInt(t.nextToken()), Integer.parseInt(t.nextToken()),
Integer.parseInt(t.nextToken()));
} catch (Exception e) {
return defaultValue;
}
}
/**
* Convert a {@link Rectangle} object to a comma separated string in the
* format of <code>x,y,width,height</code>. Suitable for storing
* rectangles in property files, XML files etc.
*
* @param rectangle rectangle
* @return comman separated string x,y,width,height
*/
public static String rectangleToString(Rectangle rectangle) {
StringBuffer buf = new StringBuffer(String.valueOf(rectangle.x));
buf.append(',');
buf.append(String.valueOf(rectangle.y));
buf.append(',');
buf.append(String.valueOf(rectangle.width));
buf.append(',');
buf.append(String.valueOf(rectangle.height));
return buf.toString();
}
/**
* Parse a string in the format of <code>[character]</code> to create an
* Integer that may be used for an action.
*
* @param character mnemonic string
* @return mnemonic
*/
public static Integer parseMnemonicString(String string) {
try {
return new Integer(string);
} catch (Throwable t) {
return new Integer(-1);
}
}
/**
* Parse a string in the format of [ALT+|CTRL+|SHIFT+] <keycode>to create a
* keystroke. This can be used to define accelerators from resource bundles
*
* @param string accelerator string
* @return keystroke
*/
public static KeyStroke parseAcceleratorString(String string) {
if (string == null || string.equals("")) {
return null;
}
StringTokenizer t = new StringTokenizer(string, "+");
int mod = 0;
int key = -1;
while (t.hasMoreTokens()) {
String x = t.nextToken();
if (x.equalsIgnoreCase("ctrl")) {
mod += KeyEvent.CTRL_MASK;
} else if (x.equalsIgnoreCase("shift")) {
mod += KeyEvent.SHIFT_MASK;
} else if (x.equalsIgnoreCase("alt")) {
mod += KeyEvent.ALT_MASK;
} else {
try {
java.lang.reflect.Field f = KeyEvent.class.getField(x);
key = f.getInt(null);
} catch (Throwable ex) {
ex.printStackTrace();
}
}
}
if (key != -1) {
KeyStroke ks = KeyStroke.getKeyStroke(key, mod);
return ks;
}
return null;
}
/**
* Add a component to a container that is using a <code>GridBagLayout</code>,
* together with its constraints and the
* <code>GridBagConstraints.gridwidth</code> value.
*
* @param parent parent container
* @param componentToAdd component to add
* @param constraints contraints
* @param pos grid width position
*
* @throws IllegalArgumentException
*/
public static void jGridBagAdd(JComponent parent, Component componentToAdd, GridBagConstraints constraints, int pos) {
if (!(parent.getLayout() instanceof GridBagLayout)) {
throw new IllegalArgumentException("parent must have a GridBagLayout");
}
//
GridBagLayout layout = (GridBagLayout) parent.getLayout();
//
constraints.gridwidth = pos;
layout.setConstraints(componentToAdd, constraints);
parent.add(componentToAdd);
}
/**
* Position a component on the screen (must be a
* <code>java.awt.Window</code> to be useful)
*
* @param p postion from <code>SwingConstants</code>
* @param c component
*/
public static void positionComponent(int p, Component c) {
positionComponent(p, c, c);
}
public static void positionComponent(int p, Component c, Component o) {
Rectangle d = null;
/*
* TODO This is very lame doesnt require the component to position
* around, just assuming its a window.
*/
try {
// #ifdef JAVA1
/*
* throw new Exception();
*/
// #else
GraphicsConfiguration config = o.getGraphicsConfiguration();
GraphicsDevice dev = config.getDevice();
d = config.getBounds();
// #endif JAVA1
} catch (Throwable t) {
}
positionComponent(p, c, d);
}
public static void positionComponent(int p, Component c, Rectangle d) {
if (d == null) {
Dimension s = Toolkit.getDefaultToolkit().getScreenSize();
d = new Rectangle(0, 0, s != null ? s.width : 800, s != null ? s.height : 600);
System.out.println("Could not get metrics from graphics config, using default " + d);
}
switch (p) {
case NORTH_WEST:
c.setLocation(d.x, d.y);
break;
case NORTH:
c.setLocation(d.x + (d.width - c.getSize().width) / 2, d.y);
break;
case NORTH_EAST:
c.setLocation(d.x + (d.width - c.getSize().width), d.y);
break;
case WEST:
c.setLocation(d.x, d.y + (d.height - c.getSize().height) / 2);
break;
case SOUTH_WEST:
c.setLocation(d.x, d.y + (d.height - c.getSize().height));
break;
case EAST:
c.setLocation(d.x + d.width - c.getSize().width, d.y + (d.height - c.getSize().height) / 2);
break;
case SOUTH_EAST:
c.setLocation(d.x + (d.width - c.getSize().width), d.y + (d.height - c.getSize().height) - 30);
break;
case CENTER:
c.setLocation(d.x + (d.width - c.getSize().width) / 2, d.y + (d.height - c.getSize().height) / 2);
break;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -