JSNI를 통해 GWT 웹 페이지 내용 인쇄

4104 단어 gwt
GWT에서 JSNI를 통해 JS 스크립트를 호출하여 웹 페이지 인쇄를 수행합니다.
먼저 인쇄 인터페이스를 미리 봅니다. 코드는 다음과 같습니다.

package net.carefx.sde.editor.web.editor.client.partogram;

import net.carefx.component.editor.web.client.jsni.PartogramScript;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.Scroll;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.google.gwt.dom.client.Element;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Image;
/**
 *          
 * @author icarefx-lqy
 *
 */
public class PartogramPreview extends Dialog 
{
	//              :imgUrl + imgName =     
//	private String imgUrl = "http://localhost:8080/chanchengtu/img?";
	private String imgUrl = null;
	private String imgName;
	private PartogramScript script;

	public PartogramPreview (String imgName, String imgUrl)
	{
		this.imgUrl = imgUrl;
		this.imgName = imgName;
		script = new PartogramScript();
		initUI();
	}
	
	private void initUI ()
	{
		setSize (960, Window.getClientHeight () - 64);
		setHeading("     ");
		setLayout(new FitLayout());
		
		final LayoutContainer lc = new LayoutContainer();
		lc.setId("imgContainner");//set div.id="imgContainner";
		Image img = new Image();
		img.setUrl(imgUrl+imgName);
		lc.add(img);
		lc.setScrollMode(Scroll.AUTO);
		this.setButtons("");
		this.setButtonAlign(HorizontalAlignment.CENTER);
		this.add(lc);
		
		Button print = new Button("  ");
		print.addSelectionListener(new SelectionListener<ButtonEvent>() {
			
			@Override
			public void componentSelected(ButtonEvent ce) {
				// TODO       
				script.excutePrint(imgUrl+imgName);
			}
		});
		
		Button cancel = new Button("  ");
		cancel.addSelectionListener(new SelectionListener<ButtonEvent>() {

			@Override
			public void componentSelected(ButtonEvent ce) {
				// TODO       
				hide();
			}
		});
		this.addButton(print);
		this.addButton(cancel);
	
	}	
}


그런 다음 JSNI 호출:

package net.carefx.component.editor.web.client.jsni;
/**
 *        
 * @author icarefx-lqy
 *
 */
public class PartogramScript {

	public PartogramScript()
	{}
	
	public native void excutePrint(String id)
	/*-{
		$wnd._divprint(id);
	}-*/;
	
	public native void getElementById (String id)
	/*-{
	 	$wnd._getdivbyid(id);
	}-*/;
}

JSNI에서 호출된 JS 스크립트:

/**
 *   DIV     
 * @param printdiv 
 * @returns {Boolean} 
 */
function _divprint (id)
{
	var win = window.open('','','width=925,height=1055');;
	var headstr = "<html><head><title></title></head><body>"; 
	var img = "<img src='"+id+"' />";
	var footstr = "</body></html>"; 
	win.document.open("text/html","replace");
	win.document.writeln(headstr);
	win.document.writeln(img);
	win.document.write(footstr);
	win.document.close();
	win.print();
}

좋은 웹페이지 즐겨찾기