You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@pivot.apache.org by "B.L. Zeebub" <ro...@googlemail.com> on 2011/08/08 13:16:25 UTC

First Attempt at Pivot 2.0

Hi

I'm trying to convert from Pivot 1.5 and I'm trying to use the Bindable
interface, but I'm obviously missing something as the initialize() method is
never being called. Sorry, I know that the code is a mess, but I'm just
trying to get things working at this stage.

Any suggestions? This is my first and only Pivot application so please keep
it simple :) 

Regards

<Frame title="Genesis Client" maximized="true"
styles="{showWindowControls:false}"
	 	xmlns:bxml="http://pivot.apache.org/bxml"
    	xmlns:content="org.apache.pivot.wtk.content"
    	xmlns:genesisClientStarter="com.blackbox.x.client"
        xmlns="org.apache.pivot.wtk">
	<content>
		<Border styles="{color:10, cornerRadii:20}">
			<content>
				<TablePane>
					<columns>
						<TablePane.Column />
					</columns>
					<rows>
					<TablePane.Row>
					<Border>
					<content>
					<BoxPane orientation="horizontal" >
					<ImageView bxml:id="car" styles="{verticalAlignment:'center'}"/>
					<ImageView bxml:id="vci_status" styles="{verticalAlignment:'bottom'}"/>
					<ImageView bxml:id="pc" styles="{verticalAlignment:'center'}"/>
					<ImageView bxml:id="pc_status" styles="{verticalAlignment:'bottom'}"/>
					<ImageView bxml:id="server" styles="{verticalAlignment:'center'}"/>
					</BoxPane>
					</content>
					</Border>
					</TablePane.Row>
					<TablePane.Row> 
					<Border>
					<content>
					<BoxPane orientation="horizontal">
					<ImageView bxml:id="rx" />
					<ImageView bxml:id="tx" />
					</BoxPane>
					</content>
					</Border>
					</TablePane.Row>
					
						<TablePane.Row height="300">
							<Border>
								<content>
									<ScrollPane horizontalScrollBarPolicy="fill_to_capacity">
										<view>
											<TableView bxml:id="tableView" selectMode="single">
												<columns>
													<TableView.Column name="message" width="800"
														headerData="Header" />
												</columns>
											</TableView>
										</view>
									</ScrollPane>
								</content>
							</Border>
						</TablePane.Row>
						<TablePane.Row> 
							<Border>
							<content>
								<ActivityIndicator bxml:id="activityIndicator"
									preferredWidth="24" preferredHeight="24" />
									</content>
							</Border>
						</TablePane.Row>
					</rows>
				</TablePane> 
			</content>
		</Border>
	</content>
</Frame>

public class Example implements Application, MessageBusListener, Bindable {

	private static String host;
	private static String port;
	private Window window;
	private Display display;
	private GenesisClient service;
	private List<LogEvent> messages;
	@BXML private ActivityIndicator activityIndicator;
	@BXML private ImageView tx;
	@BXML private ImageView rx;
	@BXML private ImageView car;
	@BXML private ImageView vci_status;
	@BXML private ImageView pc;
	@BXML private ImageView pc_status;
	@BXML private ImageView server;
	@BXML private TableView tv;
	private static String TX_ON = "com/blackbox/x/client/images/TxOn.png";
	private static String RX_ON = "com/blackbox/x/client/images/RxOn.png";
	private static String TX_OFF = "com/blackbox/x/client/images/TxOff.png";
	private static String RX_OFF = "com/blackbox/x/client/images/RxOff.png";
	private static String CAR =
"com/blackbox/x/client/images/transportation_car_32.png";
	private static String VCI_STATUS_OFF =
"com/blackbox/x/client/images/cross.png";
	private static String VCI_STATUS_ON =
"com/blackbox/x/client/images/tick.png";
	private static String PC = "com/blackbox/x/client/images/generic_pc.png";
	private static String PC_STATUS_OFF =
"com/blackbox/x/client/images/cross.png";
	private static String PC_STATUS_ON =
"com/blackbox/x/client/images/tick.png";
	private static String SERVER =
"com/blackbox/x/client/images/redhat_network_server.png";

	private String VERSION ="Pivot Client : Version 0.95.0.3";
	
	private static final Logger logger = Logger
	.getLogger(Example.class.getName());
	
	public void resume() throws Exception {
	

	}
	

	public boolean shutdown(boolean optional) throws Exception {
		if (window != null) {
			window.close();
		}
		return false;
	}

	@SuppressWarnings("unchecked")
	public void startup(Display display, Map&lt;String, String&gt; properties)
			throws Exception {
		
		this.display = display;
		
		registerEventListeners();
		
		BXMLSerializer wtkxSerializer = new BXMLSerializer();
		window = (Window) wtkxSerializer.readObject(Example.class,
"GenesisClientStarter.wtkx");

		window.open(display);
		
        
	}


	@SuppressWarnings("unchecked")
	private void registerEventListeners() {
		MessageBus.subscribe(LogEvent.class, this);
		MessageBus.subscribe(DataWriteEvent.class, this);
		MessageBus.subscribe(DataReadEvent.class, this);
		MessageBus.subscribe(ServerCloseEvent.class, this);
		MessageBus.subscribe(ErrorEvent.class, this);
		MessageBus.subscribe(ConnectEvent.class, this);
		MessageBus.subscribe(ContactEvent.class, this);
	}

		

	public void suspend() throws Exception {
		

	}

	public static void main(String[] args) {
		if (args[0] == null) {
			host = "localhost";
		
		} else {
			host = args[0];
		}
		DesktopApplicationContext.main(Example.class, args);
	}
	
	
	private void sleep(int interval) {
		
		try {
			Thread.sleep(interval);
		} catch (InterruptedException e) {
			
			e.printStackTrace();
		}
			}
	

	private class ClientTask extends Task<String> {


		@Override
		public String execute() throws TaskExecutionException {
			messages.add(new LogEvent(VERSION));
			
			service = new GenesisClient(host);
			try {
				DataEventListener listener = new PivotDataEventListener();
				service.setListener(listener);
				
				service.start();
			} catch (IOException e) {
				logger.error("GenesisClient throws IOException during startup");
				} catch (VciNotFoundException e) {
				logger.error("GenesisClient cannot find Vci device during startup");
				throw new TaskExecutionException(e);		
			}
			return "Done";
		}
		
	}

	public void messageSent(Object message) {
		
		if (message instanceof LogEvent) {
			messages.add((LogEvent) message);
			return;
		} 
		
		
		if (message instanceof ServerCloseEvent) {
			pc_status.setImage(PC_STATUS_OFF);
			Alert.alert(MessageType.ERROR,"The Faultmate Genesis server has closed
the connection. ", window, new DialogCloseListener()
					 {

				public void dialogClosed(Dialog dialog, boolean modal) {
						try {
							window.close();
							shutdown(true);
							
						} catch (Exception e) {
							e.printStackTrace();
						}
					
				}
		
				
			});
        	    
        }
		
		if (message instanceof ErrorEvent) {
			vci_status.setImage(VCI_STATUS_OFF);
			Alert.alert(MessageType.ERROR,((ErrorEvent) message).getMessage(),
window, new DialogCloseListener()
					 {

				public void dialogClosed(Dialog dialog, boolean modal) {
						try {
							window.close();
							shutdown(true);
						} catch (Exception e) {
							e.printStackTrace();
						}
					
				}
		
				
			});
                    
		}
		
		if (message instanceof ContactEvent) {
			vci_status.setImage(VCI_STATUS_OFF);
			messages.add(new LogEvent(((ContactEvent) message).getMessage()));
			Alert.alert(MessageType.ERROR,((ContactEvent) message).getMessage(),
window, new DialogCloseListener()
					 {

				public void dialogClosed(Dialog dialog, boolean modal) {
						try {
							window.close();
							shutdown(true);
							} catch (Exception e) {
							e.printStackTrace();
						}
					
				}
		
				
			});
                    
		}
		
		if (message instanceof ConnectEvent) {
			messages.add(new LogEvent(((ConnectEvent) message).getMessage()));
			if (((ConnectEvent) message).isConnected()) {
				pc_status.setImage(PC_STATUS_ON);
			} else {
				pc_status.setImage(PC_STATUS_OFF);
			}
		}
		
	}
	

	@Override
	public void initialize(Map&lt;String, Object&gt; namespace, URL location,
			Resources resources) {
		messages = new ArrayList<LogEvent>();
		tv.setTableData(messages);
		/* Set the initial Modem light position */
		tx.setImage(TX_OFF);
		rx.setImage(RX_OFF);
		/* Set the initial "Connection" icon status.*/
		car.setImage(CAR);
		vci_status.setImage(VCI_STATUS_OFF);
		pc.setImage(PC);
		pc_status.setImage(PC_STATUS_OFF);
		server.setImage(SERVER);

	}

	

	
	
}

--
View this message in context: http://apache-pivot-users.399431.n3.nabble.com/First-Attempt-at-Pivot-2-0-tp3235262p3235262.html
Sent from the Apache Pivot - Users mailing list archive at Nabble.com.

Re: First Attempt at Pivot 2.0

Posted by Chris Bartlett <cb...@gmail.com>.
Due to the @DefaultProperty annotation on most Pivot WTK classes, you
can simplify your BXML quite a lot now.

See the section titled 'Default Properties' in the BXML Primer
http://pivot.apache.org/tutorials/bxml-primer.html

In the BXML below, it means you no longer need to specify these tags
<content></content>
<view></view>

Unfortunately the annotation won't usually show up in the Javadoc for
the class, but can be seen in the source code.
For example, you can see that 'content' is set as the default property
in the source for Border
@DefaultProperty("content")
http://svn.apache.org/repos/asf/pivot/trunk/wtk/src/org/apache/pivot/wtk/Border.java

The full source can be downloaded here, or grabbed from SVN
http://pivot.apache.org/download.cgi#2.0
http://pivot.apache.org/svn.html

The FAQ explains how to reference the source in Eclipse
http://pivot.apache.org/faq.html

> <Frame title="Genesis Client" maximized="true"
> styles="{showWindowControls:false}"
>                xmlns:bxml="http://pivot.apache.org/bxml"
>        xmlns:content="org.apache.pivot.wtk.content"
>        xmlns:genesisClientStarter="com.blackbox.x.client"
>        xmlns="org.apache.pivot.wtk">
>        <content>
>                <Border styles="{color:10, cornerRadii:20}">
>                        <content>
>                                <TablePane>
>                                        <columns>
>                                                <TablePane.Column />
>                                        </columns>
>                                        <rows>
>                                        <TablePane.Row>
>                                        <Border>
>                                        <content>
>                                        <BoxPane orientation="horizontal" >
>                                        <ImageView bxml:id="car" styles="{verticalAlignment:'center'}"/>
>                                        <ImageView bxml:id="vci_status" styles="{verticalAlignment:'bottom'}"/>
>                                        <ImageView bxml:id="pc" styles="{verticalAlignment:'center'}"/>
>                                        <ImageView bxml:id="pc_status" styles="{verticalAlignment:'bottom'}"/>
>                                        <ImageView bxml:id="server" styles="{verticalAlignment:'center'}"/>
>                                        </BoxPane>
>                                        </content>
>                                        </Border>
>                                        </TablePane.Row>
>                                        <TablePane.Row>
>                                        <Border>
>                                        <content>
>                                        <BoxPane orientation="horizontal">
>                                        <ImageView bxml:id="rx" />
>                                        <ImageView bxml:id="tx" />
>                                        </BoxPane>
>                                        </content>
>                                        </Border>
>                                        </TablePane.Row>
>
>                                                <TablePane.Row height="300">
>                                                        <Border>
>                                                                <content>
>                                                                        <ScrollPane horizontalScrollBarPolicy="fill_to_capacity">
>                                                                                <view>
>                                                                                        <TableView bxml:id="tableView" selectMode="single">
>                                                                                                <columns>
>                                                                                                        <TableView.Column name="message" width="800"
>                                                                                                                headerData="Header" />
>                                                                                                </columns>
>                                                                                        </TableView>
>                                                                                </view>
>                                                                        </ScrollPane>
>                                                                </content>
>                                                        </Border>
>                                                </TablePane.Row>
>                                                <TablePane.Row>
>                                                        <Border>
>                                                        <content>
>                                                                <ActivityIndicator bxml:id="activityIndicator"
>                                                                        preferredWidth="24" preferredHeight="24" />
>                                                                        </content>
>                                                        </Border>
>                                                </TablePane.Row>
>                                        </rows>
>                                </TablePane>
>                        </content>
>                </Border>
>        </content>
> </Frame>

Re: First Attempt at Pivot 2.0

Posted by Chris Bartlett <cb...@gmail.com>.
Have a quick look at the section titled 'The Bindable Interface' in
this if you haven't already
http://pivot.apache.org/tutorials/bxml-primer.html

The initialize method will be called on *just* the root object of the
BXML file, not on the Application.  In your case the root object is a
standard org.apache.pivot.wtk.Frame.

So BXMLSerializer will attempt to call initialize on the 'window' variable here
BXMLSerializer wtkxSerializer = new BXMLSerializer();
window = (Window) wtkxSerializer.readObject(Example.class,
 "GenesisClientStarter.wtkx");

Instead of using a Frame, you can extend Frame and implement Bindable.
 Then when the BXML is loaded by BXMLSerializer, it will call the
initialize method on that custom subclass of Frame.

Here is an example from the 'examples' project in the Pivot SVN repository.
http://svn.apache.org/repos/asf/pivot/trunk/examples/src/org/apache/pivot/examples/scripting/JavaWindow.java
http://svn.apache.org/repos/asf/pivot/trunk/examples/src/org/apache/pivot/examples/scripting/java_window.bxml

Don't hesitate to ask if you have more questions.

Chris
On 8 August 2011 18:16, B.L. Zeebub <ro...@googlemail.com> wrote:
> Hi
>
> I'm trying to convert from Pivot 1.5 and I'm trying to use the Bindable
> interface, but I'm obviously missing something as the initialize() method is
> never being called. Sorry, I know that the code is a mess, but I'm just
> trying to get things working at this stage.
>
> Any suggestions? This is my first and only Pivot application so please keep
> it simple :)
>
> Regards
>
> <Frame title="Genesis Client" maximized="true"
> styles="{showWindowControls:false}"
>                xmlns:bxml="http://pivot.apache.org/bxml"
>        xmlns:content="org.apache.pivot.wtk.content"
>        xmlns:genesisClientStarter="com.blackbox.x.client"
>        xmlns="org.apache.pivot.wtk">
>        <content>
>                <Border styles="{color:10, cornerRadii:20}">
>                        <content>
>                                <TablePane>
>                                        <columns>
>                                                <TablePane.Column />
>                                        </columns>
>                                        <rows>
>                                        <TablePane.Row>
>                                        <Border>
>                                        <content>
>                                        <BoxPane orientation="horizontal" >
>                                        <ImageView bxml:id="car" styles="{verticalAlignment:'center'}"/>
>                                        <ImageView bxml:id="vci_status" styles="{verticalAlignment:'bottom'}"/>
>                                        <ImageView bxml:id="pc" styles="{verticalAlignment:'center'}"/>
>                                        <ImageView bxml:id="pc_status" styles="{verticalAlignment:'bottom'}"/>
>                                        <ImageView bxml:id="server" styles="{verticalAlignment:'center'}"/>
>                                        </BoxPane>
>                                        </content>
>                                        </Border>
>                                        </TablePane.Row>
>                                        <TablePane.Row>
>                                        <Border>
>                                        <content>
>                                        <BoxPane orientation="horizontal">
>                                        <ImageView bxml:id="rx" />
>                                        <ImageView bxml:id="tx" />
>                                        </BoxPane>
>                                        </content>
>                                        </Border>
>                                        </TablePane.Row>
>
>                                                <TablePane.Row height="300">
>                                                        <Border>
>                                                                <content>
>                                                                        <ScrollPane horizontalScrollBarPolicy="fill_to_capacity">
>                                                                                <view>
>                                                                                        <TableView bxml:id="tableView" selectMode="single">
>                                                                                                <columns>
>                                                                                                        <TableView.Column name="message" width="800"
>                                                                                                                headerData="Header" />
>                                                                                                </columns>
>                                                                                        </TableView>
>                                                                                </view>
>                                                                        </ScrollPane>
>                                                                </content>
>                                                        </Border>
>                                                </TablePane.Row>
>                                                <TablePane.Row>
>                                                        <Border>
>                                                        <content>
>                                                                <ActivityIndicator bxml:id="activityIndicator"
>                                                                        preferredWidth="24" preferredHeight="24" />
>                                                                        </content>
>                                                        </Border>
>                                                </TablePane.Row>
>                                        </rows>
>                                </TablePane>
>                        </content>
>                </Border>
>        </content>
> </Frame>
>
> public class Example implements Application, MessageBusListener, Bindable {
>
>        private static String host;
>        private static String port;
>        private Window window;
>        private Display display;
>        private GenesisClient service;
>        private List<LogEvent> messages;
>        @BXML private ActivityIndicator activityIndicator;
>        @BXML private ImageView tx;
>        @BXML private ImageView rx;
>        @BXML private ImageView car;
>        @BXML private ImageView vci_status;
>        @BXML private ImageView pc;
>        @BXML private ImageView pc_status;
>        @BXML private ImageView server;
>        @BXML private TableView tv;
>        private static String TX_ON = "com/blackbox/x/client/images/TxOn.png";
>        private static String RX_ON = "com/blackbox/x/client/images/RxOn.png";
>        private static String TX_OFF = "com/blackbox/x/client/images/TxOff.png";
>        private static String RX_OFF = "com/blackbox/x/client/images/RxOff.png";
>        private static String CAR =
> "com/blackbox/x/client/images/transportation_car_32.png";
>        private static String VCI_STATUS_OFF =
> "com/blackbox/x/client/images/cross.png";
>        private static String VCI_STATUS_ON =
> "com/blackbox/x/client/images/tick.png";
>        private static String PC = "com/blackbox/x/client/images/generic_pc.png";
>        private static String PC_STATUS_OFF =
> "com/blackbox/x/client/images/cross.png";
>        private static String PC_STATUS_ON =
> "com/blackbox/x/client/images/tick.png";
>        private static String SERVER =
> "com/blackbox/x/client/images/redhat_network_server.png";
>
>        private String VERSION ="Pivot Client : Version 0.95.0.3";
>
>        private static final Logger logger = Logger
>        .getLogger(Example.class.getName());
>
>        public void resume() throws Exception {
>
>
>        }
>
>
>        public boolean shutdown(boolean optional) throws Exception {
>                if (window != null) {
>                        window.close();
>                }
>                return false;
>        }
>
>        @SuppressWarnings("unchecked")
>        public void startup(Display display, Map&lt;String, String&gt; properties)
>                        throws Exception {
>
>                this.display = display;
>
>                registerEventListeners();
>
>                BXMLSerializer wtkxSerializer = new BXMLSerializer();
>                window = (Window) wtkxSerializer.readObject(Example.class,
> "GenesisClientStarter.wtkx");
>
>                window.open(display);
>
>
>        }
>
>
>        @SuppressWarnings("unchecked")
>        private void registerEventListeners() {
>                MessageBus.subscribe(LogEvent.class, this);
>                MessageBus.subscribe(DataWriteEvent.class, this);
>                MessageBus.subscribe(DataReadEvent.class, this);
>                MessageBus.subscribe(ServerCloseEvent.class, this);
>                MessageBus.subscribe(ErrorEvent.class, this);
>                MessageBus.subscribe(ConnectEvent.class, this);
>                MessageBus.subscribe(ContactEvent.class, this);
>        }
>
>
>
>        public void suspend() throws Exception {
>
>
>        }
>
>        public static void main(String[] args) {
>                if (args[0] == null) {
>                        host = "localhost";
>
>                } else {
>                        host = args[0];
>                }
>                DesktopApplicationContext.main(Example.class, args);
>        }
>
>
>        private void sleep(int interval) {
>
>                try {
>                        Thread.sleep(interval);
>                } catch (InterruptedException e) {
>
>                        e.printStackTrace();
>                }
>                        }
>
>
>        private class ClientTask extends Task<String> {
>
>
>                @Override
>                public String execute() throws TaskExecutionException {
>                        messages.add(new LogEvent(VERSION));
>
>                        service = new GenesisClient(host);
>                        try {
>                                DataEventListener listener = new PivotDataEventListener();
>                                service.setListener(listener);
>
>                                service.start();
>                        } catch (IOException e) {
>                                logger.error("GenesisClient throws IOException during startup");
>                                } catch (VciNotFoundException e) {
>                                logger.error("GenesisClient cannot find Vci device during startup");
>                                throw new TaskExecutionException(e);
>                        }
>                        return "Done";
>                }
>
>        }
>
>        public void messageSent(Object message) {
>
>                if (message instanceof LogEvent) {
>                        messages.add((LogEvent) message);
>                        return;
>                }
>
>
>                if (message instanceof ServerCloseEvent) {
>                        pc_status.setImage(PC_STATUS_OFF);
>                        Alert.alert(MessageType.ERROR,"The Faultmate Genesis server has closed
> the connection. ", window, new DialogCloseListener()
>                                         {
>
>                                public void dialogClosed(Dialog dialog, boolean modal) {
>                                                try {
>                                                        window.close();
>                                                        shutdown(true);
>
>                                                } catch (Exception e) {
>                                                        e.printStackTrace();
>                                                }
>
>                                }
>
>
>                        });
>
>        }
>
>                if (message instanceof ErrorEvent) {
>                        vci_status.setImage(VCI_STATUS_OFF);
>                        Alert.alert(MessageType.ERROR,((ErrorEvent) message).getMessage(),
> window, new DialogCloseListener()
>                                         {
>
>                                public void dialogClosed(Dialog dialog, boolean modal) {
>                                                try {
>                                                        window.close();
>                                                        shutdown(true);
>                                                } catch (Exception e) {
>                                                        e.printStackTrace();
>                                                }
>
>                                }
>
>
>                        });
>
>                }
>
>                if (message instanceof ContactEvent) {
>                        vci_status.setImage(VCI_STATUS_OFF);
>                        messages.add(new LogEvent(((ContactEvent) message).getMessage()));
>                        Alert.alert(MessageType.ERROR,((ContactEvent) message).getMessage(),
> window, new DialogCloseListener()
>                                         {
>
>                                public void dialogClosed(Dialog dialog, boolean modal) {
>                                                try {
>                                                        window.close();
>                                                        shutdown(true);
>                                                        } catch (Exception e) {
>                                                        e.printStackTrace();
>                                                }
>
>                                }
>
>
>                        });
>
>                }
>
>                if (message instanceof ConnectEvent) {
>                        messages.add(new LogEvent(((ConnectEvent) message).getMessage()));
>                        if (((ConnectEvent) message).isConnected()) {
>                                pc_status.setImage(PC_STATUS_ON);
>                        } else {
>                                pc_status.setImage(PC_STATUS_OFF);
>                        }
>                }
>
>        }
>
>
>        @Override
>        public void initialize(Map&lt;String, Object&gt; namespace, URL location,
>                        Resources resources) {
>                messages = new ArrayList<LogEvent>();
>                tv.setTableData(messages);
>                /* Set the initial Modem light position */
>                tx.setImage(TX_OFF);
>                rx.setImage(RX_OFF);
>                /* Set the initial "Connection" icon status.*/
>                car.setImage(CAR);
>                vci_status.setImage(VCI_STATUS_OFF);
>                pc.setImage(PC);
>                pc_status.setImage(PC_STATUS_OFF);
>                server.setImage(SERVER);
>
>        }
>
>
>
>
>
> }
>
> --
> View this message in context: http://apache-pivot-users.399431.n3.nabble.com/First-Attempt-at-Pivot-2-0-tp3235262p3235262.html
> Sent from the Apache Pivot - Users mailing list archive at Nabble.com.
>