001: package net.xoetrope.builder.editor;
002:
003: import java.io.BufferedInputStream;
004: import java.io.File;
005: import java.io.InputStream;
006: import java.io.OutputStream;
007:
008: import java.awt.Component;
009: import java.awt.Container;
010: import java.awt.Dimension;
011: import java.awt.Toolkit;
012: import java.awt.Window;
013:
014: import net.xoetrope.xui.XProjectManager;
015: import net.xoetrope.xui.XResourceManager;
016:
017: /**
018: * A collection of useful methods
019: * <p> Copyright (c) Xoetrope Ltd., 2002-2003</p>
020: * <p> $Revision: 1.12 $</p>
021: * <p> License: see License.txt</p>
022: */
023: public class XEditorUtilities {
024: /**
025: * Copy a file specified by the source path to the target path
026: * @param clazz a class whos classloader will be used to load the resource
027: * @param source the file which we want to copy
028: * @param target the file we want to create
029: * @return true if the copy operation was successful, otherwise false is returned
030: */
031: public static boolean copyFile(Class clazz, String source,
032: String target) {
033: byte[] fileBytes = new byte[0];
034: InputStream is = null;
035: if (clazz != null)
036: is = new BufferedInputStream(clazz.getClassLoader()
037: .getResourceAsStream(source));
038: if (is == null)
039: is = XProjectManager.getResourceManager()
040: .getBufferedInputStream(source);
041:
042: try {
043: byte[] b = new byte[1000];
044: if (is != null) {
045: OutputStream fos = XResourceManager
046: .getBufferedOutputStream(target, false);
047: int i = is.read(b);
048: while (i != -1) {
049: fileBytes = new byte[i];
050: System.arraycopy(b, 0, fileBytes, 0, i);
051: fos.write(fileBytes);
052: b = new byte[1000];
053: i = is.read(b);
054: }
055: fos.flush();
056: fos.close();
057: } else
058: return false;
059: } catch (Exception ex) {
060: ex.printStackTrace();
061: return false;
062: }
063:
064: return true;
065: }
066:
067: /**
068: * Copy a file specified by the source path to the target path
069: * @param source the file which we want to copy
070: * @param target the file we want to create
071: * @return true if the copy operation was successful, otherwise false is returned
072: */
073: public static boolean copyFile(String source, String target) {
074: return copyFile(null, source, target);
075: }
076:
077: /**
078: * Create the directory specified by filename
079: * @param filename the directory to create
080: */
081: public static void createDir(String filename) {
082: File f = new File(filename);
083: f.mkdirs();
084: }
085:
086: /**
087: * Set the default font for all the components in this container.
088: * @param cont the container
089: */
090: public static void setDefaultFont(Container cont) {
091: cont.setFont(XuiDefaults.defaultFont);
092:
093: int numComponents = cont.getComponentCount();
094: for (int i = 0; i < numComponents; i++) {
095: Component c = cont.getComponent(i);
096:
097: if (c instanceof Container)
098: setDefaultFont((Container) c);
099: else
100: c.setFont(XuiDefaults.defaultFont);
101: }
102: }
103:
104: public static void centreWindow(Window window, Dimension windowSize) {
105: Dimension screenSize = Toolkit.getDefaultToolkit()
106: .getScreenSize();
107: Dimension frameSize = new Dimension(Math.min(windowSize.width,
108: screenSize.width), Math.min(windowSize.height,
109: screenSize.height));
110: if (frameSize.height > screenSize.height)
111: frameSize.height = screenSize.height;
112:
113: if (frameSize.width > screenSize.width)
114: frameSize.width = screenSize.width;
115:
116: window.setLocation((screenSize.width - frameSize.width) / 2,
117: (screenSize.height - frameSize.height) / 2);
118: window.setSize(frameSize);
119: }
120:
121: public static String getComponentClassName(Component c) {
122: String compName = c.getClass().getName();
123: if (compName.indexOf("XLabelProxy") > 0)
124: return "XLabel";
125: if (compName.indexOf("XPanelProxy") > 0)
126: return "XPanel";
127: else
128: return c.getClass().getName();
129: }
130:
131: // public Component getProxiedComponent( net.xoetrope.builder.editor.components.swing.XComponentProxy c )
132: // {
133: //
134: // }
135: }
|