/*
* Created on 04.02.2006
*
* To change the template for this generated file go to Window>Preferences>Java>Code Generation>Code and Comments
*/
package template;
import java.awt.Color;
import java.awt.Component;
import java.awt.Cursor;
import java.awt.FileDialog;
import java.awt.Frame;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.io.File;
import java.lang.reflect.Method;
import javax.swing.JColorChooser;
import javax.swing.JComponent;
import de.cbse.jeasy.JEObject;
import de.cbse.jeasy.JEOptionPane;
import de.cbse.jeasy.JEProperties;
import de.cbse.jeasy.JEUtil;
/**
* @author hpl
*/
public class GUIUtils {
public final static Cursor defaultCursor = new Cursor(Cursor.DEFAULT_CURSOR);
public final static Cursor waitCursor = new Cursor(Cursor.WAIT_CURSOR);
private static String javaVersion = System.getProperty("java.version");
private static GUIUtils gui = new GUIUtils();
public static String colorDialog(Color color) {
color = JColorChooser.showDialog(null, "Color Chooser", color);
if (color != null) {
String str = "00";
String sr, sg, sb;
sr = Integer.toHexString(color.getRed());
if (sr.length() < 2)
sr = "0" + sr;
sg = Integer.toHexString(color.getGreen());
if (sg.length() < 2)
sg = "0" + sg;
sb = Integer.toHexString(color.getBlue());
if (sb.length() < 2)
sb = "0" + sb;
return sr + sg + sb;
}
return null;
}
/**
* Opens a FileDialog for mode save or load
* @param mode FileDialog.LOAD or FileDialog.SAVE
* @return
*/
public static File fileDialog(int mode) {
if (mode == FileDialog.LOAD) {
FileDialog fileDialog = new FileDialog(new Frame("Select File"));
fileDialog.setName("Select File");
fileDialog.setTitle("Select File");
fileDialog.setMode(FileDialog.LOAD);
fileDialog.setVisible(true);
String file = fileDialog.getFile();
if (file == null) {
return null;
}
String directory = fileDialog.getDirectory();
File f = new File(directory, file);
if (f.exists()) {
return f;
}
return null;
} else if (mode == FileDialog.SAVE) {
FileDialog fileDialog = new FileDialog(new Frame("Save File"));
fileDialog.setName("Save File");
fileDialog.setTitle("Save File");
fileDialog.setMode(FileDialog.SAVE);
fileDialog.setVisible(true);
String file = fileDialog.getFile();
if (file == null) {
return null;
}
String directory = fileDialog.getDirectory();
File f = new File(directory, file);
if (f.exists()) {
return f;
}
return null;
}
return null;
}
/**
* Asks for text in OtionPane
* @param title
* @param text
* @return
*/
public static String askFor(String title, String text) {
// TODO Dialog box
JEOptionPane jo = (JEOptionPane) JEUtil.getJO("OP_InputDialog");
return (String) jo.showOptionPane(JEObject.getRootPane(), title, text);
}
/**
* Asks for yes or no in OtionPane
* @param title
* @param text
* @return
*/
public static int answer(String title, String text) {
JEOptionPane jo = (JEOptionPane) JEUtil.getJO("OP_AnswerDialog");
return ((Integer) jo.showOptionPane(JEObject.getRootPane(), title, text)).intValue();
}
/**
* Asks with several buttons for yes or no in OtionPane
* @param title
* @param text
* @param buttons array of button names
* @return
*/
public static int answer(String title, String text, String[] buttons) {
// TODO Dialog box
JEOptionPane jo = (JEOptionPane) JEUtil.getJO("OP_OptionDialog");
jo.setProperty(JEProperties.optionButtons, buttons);
return ((Integer) jo.showOptionPane(JEObject.getRootPane(), title, text)).intValue();
}
/**
* Tells text in OptionPane
* @param title
* @param text
* @return
*/
public static synchronized String say(String title, String text) {
// TODO Dialog box
JEOptionPane jo = (JEOptionPane) JEUtil.getJO(JEIds.OP_MESSAGE);
return (String) jo.showOptionPane(JEObject.getRootPane(), title, text);
}
/**
* Sets the hourglass cursor
*
* @param b
* true if cursor should show the hourglass
*/
public static void setWaitCursor(boolean b) {
if (b)
// c.setCursor(waitCursor);
getRootPane().setCursor(waitCursor);
else
// c.setCursor(defaultCursor);
getRootPane().setCursor(defaultCursor);
}
/**
* Set cursor for the rootpane
* @param cursor constant out of class Cursor
*/
public static void setCursor(int cursor) {
// JEObject.getRootPane().setCursor(new Cursor(cursor));
getRootPane().setCursor(new Cursor(cursor));
}
private static Component getRootPane() {
// if (!isJavaVersion1_6()) {
// JEComponent root = (JEComponent) JEUtil.getJO(JEObject.getRootPaneId());
// if (root.getWindow() != null)
// return ((JEComponent) JEUtil.getJO(JEObject.getRootPaneId())).getWindow();
// else
// return ((JEComponent) JEUtil.getJO(JEObject.getRootPaneId())).getComponent();
// }
// else
return JEObject.getRootPane();
}
/**
* Gets the java version.
* @return
*/
public static String getJavaVersion() {
return javaVersion;
}
/**
* Checks wether the java version is higher or equal 1.6.
* @return
*/
public static boolean isJavaVersion1_6(){
if (getJavaVersion().compareTo("1.6") < 0) return false;
return true;
}
/**
* Gets the location of the mouse pointer.
* @param me
* @return
*/
public static Point getLocationOnScreen(MouseEvent me) {
if (isJavaVersion1_6()) {
Method jmethod = null;
Class meClass = me.getClass();
try {
jmethod = meClass.getMethod("getLocationOnScreen", new Class[]{});
return (Point)jmethod.invoke(me, new Object[]{});
} catch (Exception e) {
System.out.println("GUIUtils.getLocationOnScreen()"+e);
}
}
Point locOS = ((JComponent)me.getSource()).getLocationOnScreen();
locOS.x += me.getX();
locOS.y += me.getY();
return locOS;
}
}