⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 comet.zul

📁 ZK是一个Ajax Java Web框架
💻 ZUL
字号:
<?xml version="1.0" encoding="UTF-8"?><!--comet.zul{{IS_NOTE	Purpose:			Description:			History:		Thu Nov 20 11:28:15     2008, Created by jumperchen}}IS_NOTECopyright (C) 2008 Potix Corporation. All Rights Reserved.{{IS_RIGHT}}IS_RIGHT--><window id="demo" apply="org.zkoss.zkdemo.userguide.DemoWindowComposer">
	<html><![CDATA[
		<h4>Comet Server Push</h4>
	]]></html>See the <toolbarbutton id="target" label="org.zkoss.zkdemo.userguide.CometServerPush" popup="details"/> implementation.
	<separator/>
	<popup id="details" fulfill="target.onClick" width="550px">
	<div style="border:1px solid #538BA2; background: #FFF;">
		<html><![CDATA[
<pre style='color:#000000;background:#ffffff;'><span style='color:#7f0055; font-weight:bold; '>public</span> <span style='color:#7f0055; font-weight:bold; '>class</span> CometServerPush {

    <span style='color:#7f0055; font-weight:bold; '>public</span> <span style='color:#7f0055; font-weight:bold; '>static</span> <span style='color:#7f0055; font-weight:bold; '>void</span> start(<span style='color:#7f0055; font-weight:bold; '>Component</span> info, <span style='color:#7f0055; font-weight:bold; '>Component</span> listbox)
            <span style='color:#7f0055; font-weight:bold; '>throws</span> <span style='color:#7f0055; font-weight:bold; '>InterruptedException</span> {
        <span style='color:#7f0055; font-weight:bold; '>final</span> Desktop desktop = Executions.getCurrent().getDesktop();
        <span style='color:#7f0055; font-weight:bold; '>if</span> (desktop.isServerPushEnabled()) {
            Messagebox.show(<span style='color:#2a00ff; '>"Already started"</span>);
        } <span style='color:#7f0055; font-weight:bold; '>else</span> {
            desktop.enableServerPush(<span style='color:#7f0055; font-weight:bold; '>true</span>);
            <span style='color:#7f0055; font-weight:bold; '>new</span> WorkingThread(info, listbox).start();
        }
    }

    <span style='color:#7f0055; font-weight:bold; '>public</span> <span style='color:#7f0055; font-weight:bold; '>static</span> <span style='color:#7f0055; font-weight:bold; '>void</span> stop() <span style='color:#7f0055; font-weight:bold; '>throws</span> <span style='color:#7f0055; font-weight:bold; '>InterruptedException</span> {
        <span style='color:#7f0055; font-weight:bold; '>final</span> Desktop desktop = Executions.getCurrent().getDesktop();
        <span style='color:#7f0055; font-weight:bold; '>if</span> (desktop.isServerPushEnabled()) {
            desktop.enableServerPush(<span style='color:#7f0055; font-weight:bold; '>false</span>);
        } <span style='color:#7f0055; font-weight:bold; '>else</span> {
            Messagebox.show(<span style='color:#2a00ff; '>"Already stopped"</span>);
        }
    }

    <span style='color:#7f0055; font-weight:bold; '>private</span> <span style='color:#7f0055; font-weight:bold; '>static</span> <span style='color:#7f0055; font-weight:bold; '>class</span> WorkingThread <span style='color:#7f0055; font-weight:bold; '>extends</span> <span style='color:#7f0055; font-weight:bold; '>Thread</span> {
        <span style='color:#7f0055; font-weight:bold; '>private</span> <span style='color:#7f0055; font-weight:bold; '>static</span> <span style='color:#7f0055; font-weight:bold; '>Color</span> colors[] = { <span style='color:#7f0055; font-weight:bold; '>Color</span>.blue, <span style='color:#7f0055; font-weight:bold; '>Color</span>.cyan, <span style='color:#7f0055; font-weight:bold; '>Color</span>.green,
                <span style='color:#7f0055; font-weight:bold; '>Color</span>.magenta, <span style='color:#7f0055; font-weight:bold; '>Color</span>.orange, <span style='color:#7f0055; font-weight:bold; '>Color</span>.pink, <span style='color:#7f0055; font-weight:bold; '>Color</span>.red,
                <span style='color:#7f0055; font-weight:bold; '>Color</span>.yellow, <span style='color:#7f0055; font-weight:bold; '>Color</span>.lightGray, <span style='color:#7f0055; font-weight:bold; '>Color</span>.white };

        <span style='color:#7f0055; font-weight:bold; '>private</span> Ellipse2D.<span style='color:#7f0055; font-weight:bold; '>Float</span>[] ellipses;

        <span style='color:#7f0055; font-weight:bold; '>private</span> <span style='color:#7f0055; font-weight:bold; '>double</span> esize[];

        <span style='color:#7f0055; font-weight:bold; '>private</span> <span style='color:#7f0055; font-weight:bold; '>float</span> estroke[];

        <span style='color:#7f0055; font-weight:bold; '>private</span> <span style='color:#7f0055; font-weight:bold; '>double</span> maxSize;

        <span style='color:#7f0055; font-weight:bold; '>private</span> <span style='color:#7f0055; font-weight:bold; '>final</span> Desktop _desktop;

        <span style='color:#7f0055; font-weight:bold; '>private</span> <span style='color:#7f0055; font-weight:bold; '>final</span> <span style='color:#7f0055; font-weight:bold; '>Component</span> _info;

        <span style='color:#7f0055; font-weight:bold; '>private</span> <span style='color:#7f0055; font-weight:bold; '>final</span> Listbox _listbox;

        <span style='color:#7f0055; font-weight:bold; '>private</span> <span style='color:#7f0055; font-weight:bold; '>int</span> _width = 150;

        <span style='color:#7f0055; font-weight:bold; '>private</span> <span style='color:#7f0055; font-weight:bold; '>int</span> _height = 150;

        <span style='color:#7f0055; font-weight:bold; '>private</span> WorkingThread(<span style='color:#7f0055; font-weight:bold; '>Component</span> info, <span style='color:#7f0055; font-weight:bold; '>Component</span> listbox) {
            _desktop = info.getDesktop();
            _info = info;
            _listbox = (Listbox) listbox;
        }

        <span style='color:#7f0055; font-weight:bold; '>public</span> <span style='color:#7f0055; font-weight:bold; '>void</span> run() {
            <span style='color:#7f0055; font-weight:bold; '>try</span> {
                <span style='color:#7f0055; font-weight:bold; '>while</span> (<span style='color:#7f0055; font-weight:bold; '>true</span>) {
                    <span style='color:#7f0055; font-weight:bold; '>if</span> (_info.getDesktop() == <span style='color:#7f0055; font-weight:bold; '>null</span>
                            || !_desktop.isServerPushEnabled()) {
                        _desktop.enableServerPush(<span style='color:#7f0055; font-weight:bold; '>false</span>);
                        <span style='color:#7f0055; font-weight:bold; '>return</span>;
                    }
                    Executions.activate(_desktop);
                    <span style='color:#7f0055; font-weight:bold; '>try</span> {
                        ((<span style='color:#7f0055; font-weight:bold; '>Image</span>) _info).setContent(paint());
                    } <span style='color:#7f0055; font-weight:bold; '>finally</span> {
                        Executions.deactivate(_desktop);
                    }
                    Threads.sleep(1000);
                }
            } <span style='color:#7f0055; font-weight:bold; '>catch</span> (<span style='color:#7f0055; font-weight:bold; '>InterruptedException</span> ex) {
                <span style='color:#7f0055; font-weight:bold; '>System</span>.out.println(<span style='color:#2a00ff; '>"The server push thread interrupted"</span>);
            }
        }

        <span style='color:#7f0055; font-weight:bold; '>public</span> <span style='color:#7f0055; font-weight:bold; '>void</span> init(<span style='color:#7f0055; font-weight:bold; '>int</span> size) {
            <span style='color:#7f0055; font-weight:bold; '>if</span> (size == 60)
                size = 8;
            <span style='color:#7f0055; font-weight:bold; '>else</span> <span style='color:#7f0055; font-weight:bold; '>if</span> (size == 40)
                size = 12;

            <span style='color:#3f7f59; '>// an array of type Ellipse2D.Float</span>
            ellipses = <span style='color:#7f0055; font-weight:bold; '>new</span> Ellipse2D.<span style='color:#7f0055; font-weight:bold; '>Float</span>[size];

            <span style='color:#3f7f59; '>// a double array initialized to the length of the ellipses array</span>
            esize = <span style='color:#7f0055; font-weight:bold; '>new</span> <span style='color:#7f0055; font-weight:bold; '>double</span>[ellipses.length];

            <span style='color:#3f7f59; '>// a float array initialized to the length of the ellipses array</span>
            estroke = <span style='color:#7f0055; font-weight:bold; '>new</span> <span style='color:#7f0055; font-weight:bold; '>float</span>[ellipses.length];
        }

        <span style='color:#7f0055; font-weight:bold; '>public</span> <span style='color:#7f0055; font-weight:bold; '>void</span> getRandomXY(<span style='color:#7f0055; font-weight:bold; '>int</span> i, <span style='color:#7f0055; font-weight:bold; '>double</span> size, <span style='color:#7f0055; font-weight:bold; '>int</span> w, <span style='color:#7f0055; font-weight:bold; '>int</span> h) {
            esize[i] = size;
            estroke[i] = 1.0f;
            <span style='color:#7f0055; font-weight:bold; '>double</span> x = <span style='color:#7f0055; font-weight:bold; '>Math</span>.random() * (w - (maxSize / 2));
            <span style='color:#7f0055; font-weight:bold; '>double</span> y = <span style='color:#7f0055; font-weight:bold; '>Math</span>.random() * (h - (maxSize / 2));
            ellipses[i].setFrame(x, y, size, size);
        }

        <span style='color:#7f0055; font-weight:bold; '>public</span> BufferedImage paint() {
            <span style='color:#7f0055; font-weight:bold; '>int</span> size;
            <span style='color:#7f0055; font-weight:bold; '>try</span> {
                size = <span style='color:#7f0055; font-weight:bold; '>Integer</span>.parseInt(_listbox.getSelectedItem().getLabel());
            } <span style='color:#7f0055; font-weight:bold; '>catch</span> (<span style='color:#7f0055; font-weight:bold; '>NumberFormatException</span> e) {
                size = 20;
            }

            init(size);
            <span style='color:#3f7f59; '>// fills the ellipses array with Ellipse2D.Float objects</span>
            <span style='color:#7f0055; font-weight:bold; '>for</span> (<span style='color:#7f0055; font-weight:bold; '>int</span> i = 0; i &lt; ellipses.length; i++) {
                ellipses[i] = <span style='color:#7f0055; font-weight:bold; '>new</span> Ellipse2D.<span style='color:#7f0055; font-weight:bold; '>Float</span>();
                <span style='color:#3f7f59; '>// gets location for each ellipse with the given random size</span>
                getRandomXY(i, size * <span style='color:#7f0055; font-weight:bold; '>Math</span>.random(), _width, _height);
            }

            BufferedImage bi = <span style='color:#7f0055; font-weight:bold; '>new</span> BufferedImage(_width, _height,
                    BufferedImage.TYPE_INT_RGB);

            Graphics2D g2 = bi.createGraphics();
            g2.setBackground(<span style='color:#7f0055; font-weight:bold; '>Color</span>.BLACK);
            g2.clearRect(0, 0, _width, _height);
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                    RenderingHints.VALUE_ANTIALIAS_ON);

            <span style='color:#3f7f59; '>// sets the color and stroke size and draws each ellipse</span>
            <span style='color:#7f0055; font-weight:bold; '>for</span> (<span style='color:#7f0055; font-weight:bold; '>int</span> i = 0; i &lt; ellipses.length; i++) {
                g2.setColor(colors[i % colors.length]);
                g2.setStroke(<span style='color:#7f0055; font-weight:bold; '>new</span> BasicStroke(estroke[i]));
                g2.draw(ellipses[i]);
            }

            g2.dispose();
            <span style='color:#7f0055; font-weight:bold; '>return</span> bi;
        }
    }
}
</pre>
		]]></html>
		</div>
	</popup>	
	<separator/>
	<tabbox width="100%" tabscroll="false">
		<tabs>
			<tab id="demoView" label="Demo"/>
			<tab id="srcView" label="View Source"/>
		</tabs>
		<tabpanels>
			<tabpanel>
				<window id="view">
				</window>
			</tabpanel>
			
			<tabpanel>
				<panel>
					<panelchildren>
						<textbox id="codeView" class="code" rows="20" width="95%">
			<attribute name="value"><![CDATA[
<zk>
	<style dynamic="true">
		.comet-news {
			color: #0A94F3;
		}
	</style>
	<window> Please press the "Start" button to see the demo "Comet Server Push".
		<button label="Start" onClick="org.zkoss.zkdemo.userguide.CometServerPush.start(info, listbox)"/>
		<button label="Stop" onClick="org.zkoss.zkdemo.userguide.CometServerPush.stop()"/>
		<separator/>
		<hbox>
		Change Maximum Diameter:
		<listbox id="listbox" rows="1" mold="select" onSelect="">
			<listitem label="20" selected="true"/>
			<listitem label="40"/>
			<listitem label="60"/>
		</listbox>
		</hbox>
			
		<image id="info" width="150px" height="150px"/>
	</window>
</zk>
			]]></attribute>
						</textbox>
					</panelchildren>
					<toolbar mold="panel">
						<button id="tryBtn" label="Try me!"/>
						<button id="reloadBtn" label="Reload" height="18px"/>
					</toolbar>
				</panel>
			</tabpanel>
		</tabpanels>
	</tabbox>
</window>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -