javauis/lcdui_qt/src/javax/microedition/lcdui/Form.java
changeset 57 59b3b4473dc8
parent 35 85266cc22c7f
equal deleted inserted replaced
56:abc41079b313 57:59b3b4473dc8
    32 {
    32 {
    33 
    33 
    34     /**
    34     /**
    35      * Abstract layouting policy.
    35      * Abstract layouting policy.
    36      */
    36      */
    37     private FormLayoutPolicy layoutPolicy;
    37     private FormLayouter formLayouter;
    38 
    38 
    39     /**
    39     /**
    40      * Item vector.
    40      * Item vector.
    41      */
    41      */
    42     private Vector items = new Vector();
    42     private Vector items = new Vector();
    67      */
    67      */
    68     public Form(String title)
    68     public Form(String title)
    69     {
    69     {
    70         super(title);
    70         super(title);
    71         construct();
    71         construct();
    72         layoutPolicy = new DefaultFormInteraction(this);
    72         formLayouter = new FormLayouter(this);
    73     }
    73     }
    74 
    74 
    75     /**
    75     /**
    76      * Constructor.
    76      * Constructor.
    77      *
    77      *
   115         ScrollBar vBar = getContentComp().getVerticalBar();
   115         ScrollBar vBar = getContentComp().getVerticalBar();
   116         if(vBar != null)
   116         if(vBar != null)
   117         {
   117         {
   118             vBar.addSelectionListener(fsbl);
   118             vBar.addSelectionListener(fsbl);
   119         }
   119         }
   120         layoutPolicy.handleShowCurrentEvent();
   120         formLayouter.handleShowCurrentEvent();
   121     }
   121     }
   122 
   122 
   123     /* (non-Javadoc)
   123     /* (non-Javadoc)
   124      * @see Displayable#eswtHandleHideCurrentEvent()
   124      * @see Displayable#eswtHandleHideCurrentEvent()
   125      */
   125      */
   130         ScrollBar vBar = getContentComp().getVerticalBar();
   130         ScrollBar vBar = getContentComp().getVerticalBar();
   131         if(vBar != null)
   131         if(vBar != null)
   132         {
   132         {
   133             vBar.removeSelectionListener(fsbl);
   133             vBar.removeSelectionListener(fsbl);
   134         }
   134         }
   135         layoutPolicy.handleHideCurrentEvent();
   135         formLayouter.handleHideCurrentEvent();
   136     }
   136     }
   137 
   137 
   138     /* (non-Javadoc)
   138     /* (non-Javadoc)
   139      * @see Displayable#eswtHandleResizeEvent(int, int)
   139      * @see Displayable#eswtHandleResizeEvent(int, int)
   140      */
   140      */
   145         {
   145         {
   146             formComposite.setRedraw(false);
   146             formComposite.setRedraw(false);
   147             formComposite.setOrigin(0, 0, true);
   147             formComposite.setOrigin(0, 0, true);
   148             formComposite.setSize(width, height);
   148             formComposite.setSize(width, height);
   149             formComposite.setRedraw(true);
   149             formComposite.setRedraw(true);
   150             layoutPolicy.handleResizeEvent(width, height);
   150             formLayouter.handleResizeEvent(width, height);
   151             resetLayoutTimer(0);
   151             resetLayoutTimer(0);
   152         }
   152         }
   153     }
   153     }
   154 
   154 
   155     /**
   155     /**
   164         {
   164         {
   165             e.doit = false;
   165             e.doit = false;
   166         }
   166         }
   167         else
   167         else
   168         {
   168         {
   169             layoutPolicy.handleKeyEvent(e.keyCode, e.type);
   169             formLayouter.handleKeyEvent(e.keyCode, e.type);
   170         }
   170         }
   171     }
   171     }
   172 
   172 
   173     /**
   173     /**
   174      * Append Item to a Form.
   174      * Append Item to a Form.
   187         {
   187         {
   188             throw new IllegalStateException(
   188             throw new IllegalStateException(
   189                 MsgRepository.FORM_EXCEPTION_ITEM_OWNED_BY_CONTAINER);
   189                 MsgRepository.FORM_EXCEPTION_ITEM_OWNED_BY_CONTAINER);
   190         }
   190         }
   191         int returnValue = -1;
   191         int returnValue = -1;
   192         synchronized(layoutPolicy)
   192         synchronized(formLayouter)
   193         {
   193         {
   194             item.setParent(this);
   194             item.setParent(this);
   195             items.addElement(item);
   195             items.addElement(item);
   196             returnValue = items.indexOf(item);
   196             returnValue = items.indexOf(item);
   197             resetLayoutTimer(returnValue);
   197             resetLayoutTimer(returnValue);
   253         if ((position < 0) || (position > items.size() )) 
   253         if ((position < 0) || (position > items.size() )) 
   254         {
   254         {
   255             throw new IndexOutOfBoundsException(
   255             throw new IndexOutOfBoundsException(
   256                 MsgRepository.FORM_EXCEPTION_INVALID_ITEM_INDEX);
   256                 MsgRepository.FORM_EXCEPTION_INVALID_ITEM_INDEX);
   257         }
   257         }
   258         synchronized(layoutPolicy)
   258         synchronized(formLayouter)
   259         {
   259         {
   260             item.setParent(this);
   260             item.setParent(this);
   261             items.insertElementAt(item, position);
   261             items.insertElementAt(item, position);
   262             resetLayoutTimer(items.indexOf(item));
   262             resetLayoutTimer(items.indexOf(item));
   263         }
   263         }
   274         if((position < 0) || (position > (items.size() - 1)))
   274         if((position < 0) || (position > (items.size() - 1)))
   275         {
   275         {
   276             throw new IndexOutOfBoundsException(
   276             throw new IndexOutOfBoundsException(
   277                 MsgRepository.FORM_EXCEPTION_INVALID_ITEM_INDEX);
   277                 MsgRepository.FORM_EXCEPTION_INVALID_ITEM_INDEX);
   278         }
   278         }
   279         synchronized(layoutPolicy)
   279         synchronized(formLayouter)
   280         {
   280         {
   281             ((Item) items.elementAt(position)).setParent(null);
   281         	Item item = ((Item) items.elementAt(position));
       
   282             item.clearParent();
       
   283         	formLayouter.removeLayoutObject(item);			
   282             items.removeElementAt(position);
   284             items.removeElementAt(position);
   283             resetLayoutTimer(position - 1);
   285             resetLayoutTimer(position - 1);
   284         }
   286         }
   285     }
   287     }
   286 
   288 
   287     /**
   289     /**
   288      * Delete all items from the formComposite.
   290      * Delete all items from the formComposite.
   289      */
   291      */
   290     public void deleteAll()
   292     public void deleteAll()
   291     {
   293     {
   292         synchronized(layoutPolicy)
   294         synchronized(formLayouter)
   293         {
   295         {
   294             for(int i = 0; i < items.size(); i++)
   296             for(int i = 0; i < items.size(); i++)
   295             {
   297             {
   296                 ((Item) items.elementAt(i)).setParent(null);
   298 	        	Item item = ((Item) items.elementAt(i));
       
   299                 item.clearParent();
       
   300 	        	formLayouter.removeLayoutObject(item);
   297             }
   301             }
   298             items.removeAllElements();
   302             items.removeAllElements();
   299             resetLayoutTimer(0);
   303             resetLayoutTimer(0);
   300         }
   304         }
   301     }
   305     }
   322         if((position < 0) || (position > (items.size() - 1)))
   326         if((position < 0) || (position > (items.size() - 1)))
   323         {
   327         {
   324             throw new IndexOutOfBoundsException(
   328             throw new IndexOutOfBoundsException(
   325                 MsgRepository.FORM_EXCEPTION_INVALID_ITEM_INDEX);
   329                 MsgRepository.FORM_EXCEPTION_INVALID_ITEM_INDEX);
   326         }
   330         }
   327         synchronized(layoutPolicy)
   331         synchronized(formLayouter)
   328         {
   332         {
   329             newItem.setParent(this);
   333             newItem.setParent(this);
   330             // clear reference to a form for "old" item.
   334             // clear reference to a form for "old" item.
   331             get(position).setParent(null);
   335             Item oldItem = get(position);
       
   336 			oldItem.clearParent();
       
   337 			formLayouter.removeLayoutObject(oldItem);
   332             items.setElementAt(newItem, position);
   338             items.setElementAt(newItem, position);
   333             resetLayoutTimer(position);
   339             resetLayoutTimer(position);
   334         }
   340         }
   335     }
   341     }
   336 
   342 
   342      * @throws IndexOutOfBoundsException if position is incorrect.
   348      * @throws IndexOutOfBoundsException if position is incorrect.
   343      */
   349      */
   344     public Item get(int position)
   350     public Item get(int position)
   345     {
   351     {
   346         Item returnItem = null;
   352         Item returnItem = null;
   347         synchronized(layoutPolicy)
   353         synchronized(formLayouter)
   348         {
   354         {
   349             try
   355             try
   350             {
   356             {
   351                 returnItem = (Item) items.elementAt(position);
   357                 returnItem = (Item) items.elementAt(position);
   352             }
   358             }
   384      *
   390      *
   385      * @param item The Item which should be set visible.
   391      * @param item The Item which should be set visible.
   386      */
   392      */
   387     void setCurrentItem(Item item)
   393     void setCurrentItem(Item item)
   388     {
   394     {
   389         layoutPolicy.setCurrentItem(item);
   395         formLayouter.setCurrentItem(item);
   390     }
   396     }
   391 
   397 
   392     /**
   398     /**
   393      * Notify item state listener about state change.
   399      * Notify item state listener about state change.
   394      *
   400      *
   417      *
   423      *
   418      * @param item
   424      * @param item
   419      * @param updateReason
   425      * @param updateReason
   420      * @param param additional parameter
   426      * @param param additional parameter
   421      */
   427      */
   422     void updateItemState(Item item, int updateReason, Object param)
   428 	void updateItemState(Item item, int updateReason, Object param)
   423     {
   429 	{
   424         if(item != null && item.getParent() == this)
   430 		if(item != null && item.getParent() == this)
   425         {
   431 		{
   426             if((updateReason & Item.UPDATE_WIDTH_CHANGED) != 0)
   432 			if(layoutTimer != null)
   427             {
   433 			{
   428                 synchronized(layoutPolicy)
   434 				formLayouter.updateItemState(item, updateReason, param);
   429                 {
   435 			}
   430                     resetLayoutTimer(items.indexOf(item));
   436 			
   431                 }
   437 			if((updateReason & Item.UPDATE_SIZE_CHANGED) != 0)
   432             }
   438 			{
   433             else
   439 				synchronized(formLayouter)
   434             {
   440 				{
   435                 if(layoutPolicy instanceof DefaultFormInteraction)
   441 					resetLayoutTimer(items.indexOf(item));
   436                 {
   442 				}
   437                     if(layoutTimer != null)
   443 			}
   438                     {
   444 		}
   439                         ((DefaultFormInteraction) layoutPolicy)
   445 	}
   440                         .updateItemState(item, updateReason, param);
       
   441                     }
       
   442                 }
       
   443             }
       
   444         }
       
   445     }
       
   446 
   446 
   447     /**
   447     /**
   448      * Get Vector of items.
   448      * Get Vector of items.
   449      *
   449      *
   450      * @return Vector of items
   450      * @return Vector of items
   462         return formComposite;
   462         return formComposite;
   463     }
   463     }
   464 
   464 
   465 
   465 
   466     /**
   466     /**
   467      * Returns form layout policy.
   467      * Returns form Form Layouter.
   468      * @return Reference to layout policy.
   468      * @return Reference to layout policy.
   469      *
   469      *
   470      */
   470      */
   471     FormLayoutPolicy getLayoutPolicy()
   471     FormLayouter getFormLayouter()
   472     {
   472     {
   473         return layoutPolicy;
   473         return formLayouter;
   474     }
   474     }
   475 
   475 
   476     // =========================================================================
   476     // =========================================================================
   477     // TODO: remove this
   477     // TODO: remove this
   478 
   478 
   481     public Form(boolean leftRightLanguage, String title)
   481     public Form(boolean leftRightLanguage, String title)
   482     {
   482     {
   483         super(title);
   483         super(title);
   484         construct();
   484         construct();
   485         layoutLR = leftRightLanguage;
   485         layoutLR = leftRightLanguage;
   486         layoutPolicy = new DefaultFormInteraction(this);
   486         formLayouter = new FormLayouter(this);
   487     }
   487     }
   488 
   488 
   489     // =========================================================================
   489     // =========================================================================
   490 
   490 
   491     /**
   491     /**
   508                 formTimerTask.cancel();
   508                 formTimerTask.cancel();
   509                 formTimerTask = null;
   509                 formTimerTask = null;
   510             }
   510             }
   511             // schedule new timer
   511             // schedule new timer
   512             startIndex = Math.max(0, Math.min(newStartIndex, startIndex));
   512             startIndex = Math.max(0, Math.min(newStartIndex, startIndex));
   513             // layoutPolicy.layoutForm(startIndex);
   513             // formLayouter.layoutForm(startIndex);
   514             formTimerTask = new FormTimerTask(startIndex);
   514             formTimerTask = new FormTimerTask(startIndex);
   515             layoutTimer.schedule(formTimerTask, Config.FORM_LAYOUT_TIMER_DELAY);
   515             layoutTimer.schedule(formTimerTask, Config.FORM_LAYOUT_TIMER_DELAY);
   516         }
   516         }
   517     }
   517     }
   518 
   518 
   519     /**
   519     /**
       
   520      * Cancel Layout Timer.
       
   521      */
       
   522     private void cancelLayoutTimer()
       
   523     {
       
   524         if(layoutTimer != null)
       
   525         {
       
   526             if(formTimerTask != null)
       
   527             {
       
   528                 formTimerTask.cancel();
       
   529                 formTimerTask = null;
       
   530             }
       
   531             layoutTimer.cancel();
       
   532 			layoutTimer = null;
       
   533         }
       
   534     }
       
   535 
       
   536     /**
   520      * Form Timer task. Triggers the formComposite to Layout.
   537      * Form Timer task. Triggers the formComposite to Layout.
   521      */
   538      */
   522     class FormTimerTask extends TimerTask
   539     class FormTimerTask extends TimerTask
   523     {
   540     {
   524 
   541 
   530         }
   547         }
   531 
   548 
   532         public void run()
   549         public void run()
   533         {
   550         {
   534             Logger.method(Form.this, "layout");
   551             Logger.method(Form.this, "layout");
   535             layoutPolicy.layoutForm(index);
   552             formLayouter.layoutForm(index);
   536             startIndex = items.size();
   553             startIndex = items.size();
   537         }
   554         }
   538 
   555 
   539     }
   556     }
   540 
   557 
   548             if (e.widget instanceof Control) {
   565             if (e.widget instanceof Control) {
   549                 // translate this control's coordinates to display coordinates
   566                 // translate this control's coordinates to display coordinates
   550                 Point p = ((Control) e.widget).toDisplay(new Point(e.x, e.y));
   567                 Point p = ((Control) e.widget).toDisplay(new Point(e.x, e.y));
   551                 // translate display coordinates to composite coordinates
   568                 // translate display coordinates to composite coordinates
   552                 p = formComposite.toControl(p);
   569                 p = formComposite.toControl(p);
   553                 layoutPolicy.handlePointerEvent(p.x, p.y, e.type);
   570                 formLayouter.handlePointerEvent(p.x, p.y, e.type);
   554             }
   571             }
   555         }
   572         }
   556     }*/
   573     }*/
   557 
   574 
   558     /**
   575     /**
   566         }
   583         }
   567 
   584 
   568         public void widgetSelected(SelectionEvent se)
   585         public void widgetSelected(SelectionEvent se)
   569         {
   586         {
   570             ScrollBar sb = (ScrollBar) se.widget;
   587             ScrollBar sb = (ScrollBar) se.widget;
   571             layoutPolicy.updateScrolling(sb.getSelection(), false);
   588             formLayouter.updateScrolling(sb.getSelection(), false);
   572         }
   589         }
   573     }
   590     }
   574 
   591 
   575     /**
   592     /**
   576      * Dispose Form.
   593      * Dispose Form.
   577      */
   594      */
   578     void dispose()
   595     void dispose()
   579     {
   596     {
       
   597         cancelLayoutTimer();
   580         super.dispose();
   598         super.dispose();
   581         layoutTimer.cancel();
       
   582         layoutTimer = null;
       
   583         deleteAll();
   599         deleteAll();
   584         ((DefaultFormInteraction) layoutPolicy).dispose();
   600         formLayouter.dispose();
   585         layoutPolicy = null;
   601 		items = null;
       
   602         formLayouter = null;
   586     }
   603     }
   587 
   604 
   588     /*
   605     /*
   589      * Dispatcher thread calls.
   606      * Dispatcher thread calls.
   590      */
   607      */
   591     void doCallback(LCDUIEvent event)
   608     void doCallback(LCDUIEvent event)
   592     {
   609     {
   593         if((event.type & LCDUIEvent.CUSTOMITEMBIT) != 0)
   610         if((event.type & LCDUIEvent.CUSTOMITEMBIT) != 0)
   594         {
   611         {
   595             // Synchronize with Form content modification operations.
   612             // Synchronize with Form content modification operations.
   596             synchronized(layoutPolicy)
   613             synchronized(formLayouter)
   597             {
   614             {
   598                 // If the eSWT widget has been explicitly disposed while the event
   615                 // If the eSWT widget has been explicitly disposed while the event
   599                 // was in the queue then ignore the event.
   616                 // was in the queue then ignore the event.
   600                 if(event.widget != null && event.widget.isDisposed())
   617                 if(event.widget != null && event.widget.isDisposed())
   601                 {
   618                 {