Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
167 ashish 1
package in.shop2020.hotspot.dashbaord.client.inbox;
2
/**
3
 * <pre>
4
 *
5
 * Description:
6
 *
7
 *	Generic printing class
8
 *	Can be used to print the Window it self, DOM.Elements, UIObjects (Widgets) and plain HTML
9
 *
10
 * Usage:
11
 *
12
 *	You must insert this iframe in your host page:
13
 *		<iframe id="__printingFrame" style="width:0;height:0;border:0"></iframe>
14
 *
15
 *	Window:
16
 *		Print.it();
17
 *
18
 *	Objects/HTML:
19
 *		Print.it(RootPanel.get("myId"));
20
 *		Print.it(DOM.getElementById("myId"));
21
 *		Print.it("Just <b>Print.it()</b>!");
22
 *
23
 *	Objects/HTML using styles:
24
 *		Print.it("<link rel=StyleSheet type=text/css media=paper href=/paperStyle.css>", RootPanel.get("myId"));
25
 *		Print.it("<style type=text/css media=paper> .newPage { page-break-after: always; } </style>",
26
 *				"Hi<p class=newPage></p>By");
27
 *
28
 *	Objects/HTML using styles and DocType:
29
 *		Print.it("<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01//EN' 'http://www.w3.org/TR/html4/strict.dtd'>",
30
 *                       "<link rel=StyleSheet type=text/css media=paper href=/paperStyle.css>",
31
 *                       RootPanel.get("myId"));
32
 *
33
 * OBS:
34
 *
35
 *	Warning: You can't use \" in your style String
36
 *
37
 *	Obs: If your machine is to slow to render the page and you keep getting blank pages, change USE_TIMER to true and
38
 *	     play with TIMER_DELAY
39
 *
40
 *	Obs: If you try to print Form elements, like TextArea and ListBox they will show default status 
41
 *
42
 * </pre>
43
 */
44
 
45
import com.google.gwt.user.client.Command;
46
import com.google.gwt.user.client.DOM;
47
import com.google.gwt.user.client.DeferredCommand;
48
import com.google.gwt.user.client.Element;
49
import com.google.gwt.user.client.Window;
50
import com.google.gwt.user.client.ui.UIObject;
51
import com.google.gwt.user.client.Timer;
52
 
53
public class Print {
54
 
55
    /**
56
     * If true, use a Timer instead of DeferredCommand to print the internal fram
57
     */
58
    public static boolean USE_TIMER	= false;
59
 
60
    /**
61
     * Time in seconds to wait before printing the internal frame when using Timer
62
     */
63
    public static int TIMER_DELAY	= 2;
64
 
65
 
66
    public static native void it() /*-{
67
	$wnd.print();
68
    }-*/;
69
 
70
    public static void it(UIObject obj) {
71
	it("", obj);
72
    }
73
 
74
    public static void it(Element element) {
75
	it("", element);
76
    }
77
 
78
    public static void it(String style, UIObject obj) {
79
	it(style, obj.getElement());
80
    }
81
 
82
    public static void it(String style, Element element) {
83
	it("", style, element);
84
    }
85
 
86
    public static void it(String docType, String style, Element element) {
87
	it(docType, style, DOM.toString(element));
88
    }
89
 
90
    public static void it(String docType, String style, String it) {
91
	it(docType
92
	   +"<html>"
93
	   +"<head>"
94
	   +"<meta http-equiv=\"Content-Type\"		content=\"text/html; charset=utf-8\">"
95
	   +"<meta http-equiv=\"Content-Style-Type\"	content=\"text/css\">"
96
	   +	style
97
	   +"</head>"+"<body>"
98
	   +	it
99
	   +"</body>"+
100
	   "</html>");
101
    }
102
 
103
    public static void it(String html) {
104
	try {
105
	    buildFrame(html);
106
 
107
	    if (USE_TIMER) {
108
		Timer timer	= new Timer() {
109
			public void run() {
110
			    printFrame();
111
			}
112
		    };
113
		timer.schedule(TIMER_DELAY * 1000);
114
	    } else {
115
		DeferredCommand.addCommand(new Command() {
116
			public void execute() {
117
			    printFrame();
118
			}
119
		    });
120
	    }
121
 
122
	} catch (Throwable exc) {
123
	    Window.alert(exc.getMessage());
124
	}
125
    }
126
 
127
    public static native void buildFrame(String html) /*-{
128
	var frame = $doc.getElementById('__printingFrame');
129
	if (!frame) {
130
	    $wnd.alert("Error: Can't find printing frame.");
131
	    return;
132
        }
133
	var doc	= frame.contentWindow.document;
134
	doc.open();
135
	doc.write(html);
136
	doc.close();
137
 
138
    }-*/;
139
 
140
    public static native void printFrame() /*-{
141
	var frame = $doc.getElementById('__printingFrame');
142
	frame = frame.contentWindow;
143
	frame.focus();
144
	frame.print();
145
    }-*/;
146
 
147
} // end of class Print