001: /*
002: * Created on Mar 10, 2003
003: *
004: * Dbmjui is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU General Public License version 2 as
006: * published by the Free Software Foundation.
007: *
008: * Dbmjui is distributed in the hope that it will be useful,
009: * but WITHOUT ANY WARRANTY; without even the implied warranty of
010: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
011: * General Public License for more details.
012: *
013: * You should have received a copy of the GNU General Public
014: * License along with dbmjui; see the file COPYING. If not,
015: * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
016: * Boston, MA 02111-1307, USA.
017: *
018: */
019: package fr.aliacom.form.swt.utils;
020:
021: import org.apache.log4j.Logger;
022: import org.eclipse.swt.SWT;
023: import org.eclipse.swt.events.DisposeEvent;
024: import org.eclipse.swt.events.DisposeListener;
025: import org.eclipse.swt.graphics.Color;
026: import org.eclipse.swt.graphics.Font;
027: import org.eclipse.swt.graphics.FontData;
028: import org.eclipse.swt.graphics.GC;
029: import org.eclipse.swt.graphics.Image;
030: import org.eclipse.swt.widgets.Control;
031: import org.eclipse.swt.widgets.Display;
032: import org.eclipse.swt.widgets.Widget;
033:
034: import fr.aliacom.form.common.Toolkit;
035: import fr.aliacom.form.common.ToolkitManager;
036:
037: /**
038: * @author tom
039: *
040: * (c) 2001, 2003 Thomas Cataldo
041: */
042: public final class SWTUtils {
043:
044: private static Toolkit tk;
045: private static Logger log;
046:
047: static {
048: tk = ToolkitManager.getToolkit();
049: log = Logger.getLogger(SWTUtils.class);
050: }
051:
052: public static void addDisposeHandler(Widget w, final Image img) {
053: if (img != null) {
054: w.addDisposeListener(new DisposeListener() {
055: public void widgetDisposed(DisposeEvent de) {
056: log.debug("Disposing image");
057: img.dispose();
058: }
059: });
060: }
061: }
062:
063: public static void addDisposeHandler(Widget w, final Font font) {
064: if (font != null) {
065: w.addDisposeListener(new DisposeListener() {
066: public void widgetDisposed(DisposeEvent de) {
067: log.debug("Disposing font");
068: font.dispose();
069: }
070: });
071: }
072: }
073:
074: public static void addDisposeHandler(Widget w, final Color color) {
075: if (color != null) {
076: w.addDisposeListener(new DisposeListener() {
077: public void widgetDisposed(DisposeEvent de) {
078: log.debug("Disposing color");
079: color.dispose();
080: }
081: });
082: }
083: }
084:
085: public static void setMonospacedFont(Control c) {
086: String font;
087: if ((font = tk.getMonospaceFontName()) != null) {
088: int height = c.getFont().getFontData()[0].getHeight();
089: FontData fd = new FontData(font, height, SWT.NORMAL);
090: Font mono = new Font(c.getDisplay(), fd);
091: c.setFont(mono);
092: addDisposeHandler(c, mono);
093: }
094: }
095:
096: public static Image getCheckBoxImage(boolean checked) {
097: final Display disp = Display.getCurrent();
098: Image ret = new Image(disp, 16, 16);
099: Color col = new Color(disp, 0, 0, 0);
100: GC gc = new GC(ret);
101: gc.setForeground(col);
102: gc.drawRectangle(1, 1, 13, 13);
103: if (checked) {
104: gc.drawLine(1, 1, 13, 13);
105: gc.drawLine(13, 1, 1, 13);
106: }
107: gc.dispose();
108: col.dispose();
109: return ret;
110: }
111:
112: }
|