package net.sourceforge.pain.tools.guitool;
import javax.swing.*;
import java.awt.*;
import java.io.*;
public class GuiTool {
public static ApplicationFrame appFrame;
public static void main(String[] args) throws Exception {
appFrame = new ApplicationFrame(GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration());
appFrame.go();
}
public static void log(String message) {
GuiTool.appFrame.workspacePanel.logPanel.message(message);
}
public static void statusLog(String message) {
GuiTool.appFrame.workspacePanel.logPanel.message(message);
GuiTool.appFrame.statusBar.setContextMessage(message);
}
public static String readTextFile(Class clazz, String fileName) throws IOException {
if (clazz == null) {
clazz = GuiTool.class;
fileName = "/net/sourceforge/pain/tools/guitool/resources/" + fileName; //default path
}
final InputStream is = clazz.getResourceAsStream(fileName);
if (is != null) {
try {
byte[] arr = new byte[is.available()];
is.read(arr);
return new String(arr);
} finally {
is.close();
}
} else {
return "resource not found: " + fileName;
}
}
public static ImageIcon loadImage(String imageName, String desc) {
String path = "/net/sourceforge/pain/tools/guitool/resources/images/" + imageName;
return new ImageIcon(GuiTool.class.getResource(path), desc);
}
/**
* Handles error messages
*
* @param data is allowed to contain:
* <ul>
* <li> Object[]{"Message", Exception}
* <li> Exception
* <li> Message(String)
* </ul>
*/
public static void showFailInfo(Object data) {
if (data == null) {
JOptionPane.showMessageDialog(appFrame.workspacePanel, "<<no addition info>>", "Error", JOptionPane.ERROR_MESSAGE);
} else if (data instanceof Exception) {
Exception e = (Exception) data;
JOptionPane.showMessageDialog(appFrame.workspacePanel, e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
} else if (Object.class.equals(data.getClass().getComponentType())) {
Object[] arr = (Object[]) data;
if (arr.length == 0) {
JOptionPane.showMessageDialog(appFrame.workspacePanel, "<<no addition info>>", "Error", JOptionPane.ERROR_MESSAGE);
}
if (arr.length == 1) {
showFailInfo(arr[0]);
} else {
String message = (String) arr[0];
Exception e = (Exception) arr[1];
JOptionPane.showMessageDialog(appFrame.workspacePanel, message + "\n" + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
}
} else { //treat data as String
JOptionPane.showMessageDialog(appFrame.workspacePanel, data, "Error", JOptionPane.ERROR_MESSAGE);
}
}
}