some utilities for swing
public class GraphicLocation {
static Toolkit toolkit = Toolkit.getDefaultToolkit();
static Dimension screenSize = toolkit.getScreenSize();
public static int windowWidth(int x)
{
return (screenSize.width - x) / 2;
}
public static int windowHeight(int x)
{
return (screenSize.height-x)/2;
}
public static int mainwindowWidth(int x)
{
return (screenSize.width - x) /3;
}
public static int mainwindowHeight(int x)
{
return (screenSize.height-x)/3;
}
}
//to sort the vector/arraylist
public class Sorter implements Comparator
public int compare(Object arg0, Object arg1) {
return arg0.toString().toUpperCase().compareTo(arg1.toString().toUpperCase());
}
}
// to assign custom image to the tree node
UIManager.put("Tree.leafIcon", getImage(TreeProperties.getProperty("leafIcon").toString()));
UIManager.put("Tree.openIcon", getImage(TreeProperties.getProperty("openIcon").toString()));
UIManager.put("Tree.closedIcon",getImage(TreeProperties.getProperty("closeIcon").toString()));
UIManager.put("Tree.expandedIcon", getImage(TreeProperties.getProperty("expandIcon").toString()));
UIManager.put("Tree.collapsedIcon",getImage(TreeProperties.getProperty("collapseIcon").toString()));
DefaultTreeCellRenderer render;
render.setTextNonSelectionColor(color);
render.setBackgroundSelectionColor(color);
render.setTextSelectionColor(color);
/**
* returns the given image path into ImageIcon.
* @param path
* @return
*/
public ImageIcon getImage(String path) {
if (path.isEmpty()) {
return null;
} else {
return new ImageIcon(path);
}
}
/**
* Creates an ImageIcon from an image file
*
* @param path
* - The directory path of the image file
* @param description
* - The description of the ImageIcon to be returned
* @return ImageIcon - null if failed
*
*/
public static ImageIcon createImageIcon(String path, String description) {
java.net.URL imageURL = DisplayNodes.class.getClassLoader()
.getResource(path);
if (imageURL == null) {
return null;
} else {
return new ImageIcon(imageURL, description);
}
}
/**
* Creates an ImageIcon from an image file
*
* @param path
* - The directory path of the image file
* @param description
* - The description of the ImageIcon to be returned
* @return ImageIcon - null if failed
*
*/
protected static BufferedImage createImageIconFromFile(String path,
String description) {
BufferedImage img = null;
try {
img = javax.imageio.ImageIO.read(new File(path));
} catch (IOException e) {
}
return img;
}
public class TransferFiles {
File srcDir, destDir;
final static Logger log=Logger.getLogger(TransferFiles.class);
/**
* transfer the fileList to the destination path.
*
* @param fileList
* @param outFile
* @param srcpath
* @return
*/
public static boolean writeToFile(String file, String outFile, String srcpath) {
String line;
BufferedReader in;
BufferedWriter out;
File fileName;
try {
// for (String file : fileList) {
fileName = new File(file);
in = new BufferedReader(new FileReader(srcpath + "\\" + fileName));
out = new BufferedWriter(new FileWriter(outFile + "\\" + fileName));
if (!in.ready()) {
throw new IOException();
}
while ((line = in.readLine()) != null) {
out.append(line);
out.newLine();
}
in.close();
out.close();
// }
return true;
} catch (Exception ex) {
log.error(ex.getMessage(), ex);
return false;
}
}
public static void uploadFiles(String fileList[], String destpath, String srcPath) {
File fileName;
FileOutputStream fileOut;
FileInputStream fileIn;
try {
for (String file : fileList) {
fileName = new File(file);
fileIn = new FileInputStream(srcPath + "\\" + fileName);
fileOut = new FileOutputStream(new File(destpath + "\\" + file));
int c = 0;
while ((c = fileIn.read()) != -1) {
fileOut.write((byte) c);
}
fileIn.close();
fileOut.close();
}
} catch (Exception e) {
System.out.println("error @ uploadFiles method : " + e.getMessage());
}
}
}
/**
* This method is to verify the string is in HHMM formate
* @param s
* @return boolean
*/
public static boolean validTimeHHMM(String s) {
if (s.length() > 4) {
return false;
}
Pattern p = Pattern.compile("^([0-1][0-9]|2[0-3])[0-5][0-9]$");
Matcher m = p.matcher(s);
if (!m.matches()) {
return false;
}
return true;
}
public static String getCurrentDate() {
DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
Date date = new Date();
return dateFormat.format(date);
}
public static String getCurrentTime() {
DateFormat dateFormat = new SimpleDateFormat("HH-mm-ss");
Date date = new Date();
return dateFormat.format(date);
}
/**
* it converts the utf format string into natural language.
* @param inputStr
* @return
*/
public static String parse(String inputStr)
{
if (inputStr.contains("\\u")) {
inputStr = inputStr.replace("\\u", "#@");
StringBuffer sb = new StringBuffer();
Pattern p = Pattern.compile("[a-fA-F0-9]+{4}");
Matcher m = p.matcher(inputStr);
int iCurrent = 0;
while (m.find()) {
sb.append(inputStr.substring(iCurrent, m.start()));
sb.append((char)Integer.parseInt(m.group(), 16));
iCurrent = m.end();
}
sb.append(inputStr.substring(iCurrent));
return sb.toString().replaceAll("#@", "");
}
return inputStr;
}
Comments