CountryStory.java :  » XML-UI » xui32 » net » xoetrope » swt » demo » Java Open Source

Java Open Source » XML UI » xui32 
xui32 » net » xoetrope » swt » demo » CountryStory.java
package net.xoetrope.swt.demo;

import net.xoetrope.swt.XButton;
import net.xoetrope.swt.XEdit;
import net.xoetrope.swt.XLabel;
import net.xoetrope.swt.XPanel;
import net.xoetrope.swt.XPassword;
import net.xoetrope.swt.XSwtPage;
import net.xoetrope.swt.XTextArea;
import net.xoetrope.swt.XToolBar;
import net.xoetrope.xui.data.XModel;

public class CountryStory extends XSwtPage {

  // Constants
    
  public static final int width = 1024 - 14;
  public static final int height = 768 - 39 - 19;
  public static final int border = 40;
  
  // Attributes
  
  private int xOrigin = 0;
  private int yOrigin = 0;
  private int widthContent = width - 2 * border;
  private int heightContent = height - 2 * border;
  
  private int toolHeight = 0;
  
  private String country;
  private String story;
  
  private String username = null;
  private String password = null;
  
  private XToolBar shellTB;
  private XLabel borderTop, borderLeft, borderRight, borderBottom;
  private XPanel storyP, buttonP, editP;
  private XTextArea storyTA;
  private XEdit usernameE;
  private XPassword passwordP;

  // Constructor
    
  public CountryStory() {
    country = project.getModel().getValueAsString( "country" );
    XModel item = ( XModel ) project.getModel().get( country + "/Informations/story" );
    story = item.getAttribValueAsString( item.getAttribute( "story" ) );
  }
  
  // Automatic Methods
    
  public void pageCreated() {
    shellTB = ( XToolBar ) findComponent( "shellTB" );
    borderTop = ( XLabel ) findComponent( "borderTop" );
    borderLeft = ( XLabel ) findComponent( "borderLeft" );
    borderRight = ( XLabel ) findComponent( "borderRight" );
    borderBottom = ( XLabel ) findComponent( "borderBottom" );
    initBorders();
    storyTA = ( XTextArea ) findComponent( "storyTA" );
    storyP = ( XPanel ) findComponent( "storyP" );
      usernameE = ( XEdit ) findComponent( "usernameE" );
      passwordP = ( XPassword ) findComponent( "passwordP" );
    buttonP = ( XPanel ) findComponent( "buttonP" );
    editP = ( XPanel ) findComponent( "editP" );
    initContent();
  }
  
  public void pageActivated() {
    storyP.setVisible( true );
    //storyB.setVisible( true );
  }
    
  // Private Methods
    
  private void initBorders() {
    toolHeight = shellTB.getBounds().height;
    yOrigin = toolHeight;
    heightContent -= toolHeight;
    System.out.println( "HeightContent : " + heightContent + ", widthContent : " + widthContent );
    borderTop.setBounds( xOrigin, yOrigin, width, border );
    borderLeft.setBounds( xOrigin, yOrigin + border, border, heightContent );
    borderRight.setBounds( xOrigin + widthContent + border, yOrigin + border, border, heightContent );
    borderBottom.setBounds( xOrigin, yOrigin + heightContent + border, width, border );
  }
    
  private void initContent() {
    storyTA.setBounds( xOrigin + border, yOrigin + border, widthContent, 2 * heightContent / 3 );
    storyTA.setText( story );
    storyP.setBounds( xOrigin + border, yOrigin + border + 2 * heightContent / 3, widthContent, 2 * heightContent / 9 );
    buttonP.setBounds( xOrigin + border, yOrigin + border + 8 * heightContent / 9, widthContent, heightContent / 9 );
    editP.setBounds( xOrigin + border, yOrigin + border + 8 * heightContent / 9, widthContent, heightContent / 9 );
    setEditMode( false );
  }
  
  private void setEditMode( boolean edit ) {
    storyTA.setEditable( edit );
    storyP.setVisible( ! edit );
    buttonP.setVisible( ! edit );
    editP.setVisible( edit );
    setVisibleLogin( username == null || username.equals( "" ) );
  }
  
  private void setVisibleLogin( boolean visible ) {
    storyP.setVisible( visible );
    if( visible )
      storyTA.setBounds( xOrigin + border, yOrigin + border, widthContent, 2 * heightContent / 3 );
    else
      storyTA.setBounds( xOrigin + border, yOrigin + border, widthContent, 8 * heightContent / 9 );
  }
    
  // Events Methods
  
  public void goToPreviousPage() {
    reInit();
    cancelEdit();
    pageMgr.showPrevious();
  }
  
  public void goToHomePage() {
    reInit();
    cancelEdit();
    project.getPageManager().showPage( "Home" );
  }
  
  public void editStory() {
    if( username == null || username.equals( "" ) ) {
      username = usernameE.getText();
      password = passwordP.getText();
    }
    if( Home.isValidEditor( username, password ) )
      setEditMode( true );
    else {
      reInit();
      showMessage( "Identification failed", "Your username or password are incorrect !" );
    }
  }
  
  public void saveStory() {
    story = storyTA.getText();
    setEditMode( false );
  }
  
  public void cancelEdit() {
    storyTA.setText( story );
    setEditMode( false );
  }
  
  private void reInit() {
    usernameE.setText( "" );
    passwordP.setText( "" );
    username = null;
    password = null;
  }
  
//  public void goToNextPage() {
//    MouseEvent me = ( MouseEvent ) getCurrentEvent();
//    System.out.println( "( " + me.x + ", " + me.y + " )" );
//    int hotspotIdx = flag.checkHotspot( me );
//    if( hotspotIdx > -1 ) {
//      String s = flag.getName( hotspotIdx );
//      XSwtPage page = ( XSwtPage )( ( XSwtTarget ) pageMgr.getTarget( "content" ) ).getChildComponent( 0 );
//      String nextPage = ( String ) page.getAttribute( s.toLowerCase(), null );
//      if( nextPage != null )
//        project.getPageManager().showPage( nextPage );
//    }
//  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.