2012-06-11 93 views
0

是否有可能避免棄用警告,同時與實用的方法,如編譯代碼:如何避免NetBeans Java項目中GWT Horizo​​ntalSplitPanel的棄用警告?

public static void doSthForHorizontalSplitPanel(HorizontalSplitPanel hsp) {...} 

和明確的聲明和/或Horizo​​ntalSplitPanel widget的創建,如:

protected HorizontalSplitPanel main; 
... 
    main = new HorizontalSplitPanel(); 

我的目標是消除這些警告沒有刪除HorizontalSplitPanel的用法,沒有給予全局編譯器標誌(-Xlint:-deprecation)和藉助於最小的手動重構根據可能的im使用Horizo​​ntalSplitPanel和我的工具方法(即儘可能少的代碼更改)。

註解@SuppressWarnings("deprecation")由於import HorizontalSplitPanel語句在方法或類級別似乎不工作,因此不推薦使用(現在)替換棄用的Horizo​​ntalSplitPanel類。

我的目標有可能實現嗎?如果是這樣,那麼最好的方法是什麼?

我使用的是NetBeans 7.1,Java 1.6,GWT 2.3。

回答

1

我的方法如下。

替換的HorizontalSplitPanel將具有如下定義HorizontalSplitPanelWrapper每次使用,然後修復導入 - 這將消除import HorizontalSplitPanel並添加import HorizontalSplitPanelWrapper。完成。

@SuppressWarnings("deprecation") 
public class HorizontalSplitPanelWrapper implements IsWidget { 

    private Panel hsp = new com.google.gwt.user.client.ui.HorizontalSplitPanel(); 

    public Widget asWidget() { 
     return hsp; 
    } 

    public com.google.gwt.user.client.Element getElement() { 
     return hsp.getElement(); 
    } 

    public <H extends EventHandler> HandlerRegistration addHandler(final H handler, GwtEvent.Type<H> type) { 
     return hsp.addHandler(handler, type); 
    } 

    public boolean isResizing() { 
     return ((com.google.gwt.user.client.ui.HorizontalSplitPanel) hsp).isResizing(); 
    } 

    public void setWidth(String width) { 
     hsp.setWidth(width); 
    } 

    public void setSplitPosition(String pos) { 
     ((com.google.gwt.user.client.ui.HorizontalSplitPanel) hsp).setSplitPosition(pos); 
    } 

    public void add(IsWidget w) { 
     hsp.add(w); 
    } 
} 

補充說明。

我的解決方案使用小技巧與GWT IsWidget接口 - 這最大限度地減少代碼的影響,因爲Widget可以在撥打電話至GTW的API實現的IsWidget被取代。

在我的代碼中使用的HorizontalSplitPanel每種方法,現在由HorizontalSplitPanelWrapper實現的,只是委託給由hsp場內部存儲Horizo​​ntalSplitPanel。

不能將Horizo​​ntalSplitPanel的字段和方法聲明爲類型/參數/結果,因爲無論@SuppressWarnings("deprecation")如何,它總是會產生棄用警告。因此,hsp字段被聲明爲Panel

如果在代碼的其餘部分使用了更多的Horizo​​ntalSplitPanel方法,那麼在HorizontalSplitPanelWrapper中對於其中的每一個都必須有dummy委託方法。在每個這樣的方法中,必須從字段hsp中檢索正確的Horizo​​ntalSplitPanel對象並進行顯式強制轉換。

就是這樣。由於仍然可以使用Horizo​​ntalSplitPanel,因此不再存在棄用警告。

3

標準模式和SplitlayoutPanel的效果比棄用的Horizo​​ntalSplitPanel要好。 試試這個代碼,你必須用Horizo​​ntalSplitLayoutPanel來替換Horizo​​ntalSplitPanel。 該代碼實際上使用了最新的SplitLayoutPanel,並且這些方法等同於Deprecated Horizo​​ntalSplitPanel。好處是你不必更改代碼。還爲VerticalSplitPanel替代VerticalSplitLayoutPanel粘貼代碼。 VerticalSplitLayoutPanel經過單元測試,工作正常。

import com.google.gwt.user.client.DOM; 
import com.google.gwt.user.client.Event; 
import com.google.gwt.user.client.ui.ScrollPanel; 
import com.google.gwt.user.client.ui.SplitLayoutPanel; 
import com.google.gwt.user.client.ui.Widget; 

/** 
* author: MKK 
* Date: 4/29/13 
* Time: 10:41 AM 
* /** 
* 
* GWT depecrated HorizontalSplitpanel as of version 2.0. 
* This is a wrapper class which extends new SplitLayoutPanel and has all the methods of deprecated old Splitpanels for 
* seamless integration with existing code without breaking. 
* Replacement of old HorizontalSplitLayoutPanel with new SplitLayoutPanel 
* 
* 
*/ 
public class HorizontalSplitLayoutPanel extends SplitLayoutPanel{ 


    private ScrollPanel leftScrollPanel = new ScrollPanel(); 
    private ScrollPanel rightScrollPanel = new ScrollPanel(); 
    private Widget leftWidget; 
    private Widget rightWidget; 


    public HorizontalSplitLayoutPanel(){ 
     super(); 
     init(); 

    } 
    boolean dragged; 
    public void onBrowserEvent(Event event) { 
     super.onBrowserEvent(event); 
     switch (DOM.eventGetType(event)) { 
      case Event.ONMOUSEDOWN: 
       dragged = true; 
       break; 
      case Event.ONMOUSEUP: 
       dragged = false; 
       break; 
      case Event.ONMOUSEMOVE: 
       break; 
     } 
    } 

    public boolean isResizing(){ 
     return dragged; 
    } 

    private void init() { 
     setSize("100%", "100%"); 
     addWest(leftScrollPanel, 300); 
     add(rightScrollPanel); 
     sinkEvents(Event.ONMOUSEDOWN | Event.ONMOUSEUP); 
    } 

    public HorizontalSplitLayoutPanel(int splitterSize) { 
     super(splitterSize); 
     init(); 
    } 


    public void setLeftWidget(Widget widget){ 
     this.leftWidget = widget; 
     leftScrollPanel.clear(); 
     leftScrollPanel.add(widget); 
     setSplitPosition("30%"); 
     setWidgetToggleDisplayAllowed(leftScrollPanel,true); 
    } 

    public void setRightWidget(Widget widget){ 
     try { 
      this.rightWidget = widget; 
      rightScrollPanel.clear(); 
      rightScrollPanel.add(widget); 
      setSplitPosition("30%"); 

     } catch (Exception e) { 
      e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. 
     } 
    } 


    public void removeWidget(Widget widget){ 
     try { 
      if(leftScrollPanel.getWidget()== widget){ 
       leftScrollPanel.remove(widget); 
       return; 
      } 
      rightScrollPanel.remove(widget); 
     } catch (Exception e) { 
     } 

    } 


    public void setSplitPosition(String percent){ 
     if(percent.toLowerCase().indexOf("px") > -1){ 
      percent = percent.replace("px","").trim(); 
      int p = Integer.parseInt(percent); 
      setSplitPosition(p); 
      return; 
     } 
     percent = percent.replace("%","").trim(); 
     int p = Integer.parseInt(percent); 
     double size = (getOffsetWidth()*p)/100.0; 
     if(p < 1.0){ 
      size = 600; 
     } 
     setWidgetSize(leftScrollPanel, size); 
    } 

    public void setSplitPosition(int pixels){ 
     setWidgetSize(leftScrollPanel, pixels); 

    } 


    public void hideLeftWidget() { 
     leftScrollPanel.clear(); 
     setWidgetMinSize(leftScrollPanel,0); 
    } 

    public void showLeftWidget(){ 
     leftScrollPanel.add(leftWidget); 
    } 
    public void hideRightWidget() { 
     rightScrollPanel.clear(); 
     setWidgetMinSize(rightScrollPanel,0); 
    } 

    public void showRightWidget(){ 
     rightScrollPanel.add(rightWidget); 
    } 
} 

import com.google.gwt.user.client.DOM; 
import com.google.gwt.user.client.Event; 
import com.google.gwt.user.client.Window; 
import com.google.gwt.user.client.ui.ScrollPanel; 
import com.google.gwt.user.client.ui.SplitLayoutPanel; 
import com.google.gwt.user.client.ui.Widget; 

/** 
* 
* GWT depecrated VerticalSplitPanel as of version 2.0. 
* This is a wrapper class which extends new SplitLayoutPanel and has all the methods of deprecated old Splitpanels for 
* seamless integration with existing code without breaking. 
* Replacement of old VerticalSplitPanel with new SplitLayoutPanel 
* 
* 
*/ 
public class VerticalSplitLayoutPanel extends SplitLayoutPanel { 

    private Widget topWidget; 
    private Widget bottomWidget; 
    private int offset=100; 

    private ScrollPanel topScrollPanel = new ScrollPanel(); 
    private ScrollPanel bottomScrollPanel = new ScrollPanel(); 




    public VerticalSplitLayoutPanel() { 
     super(); 
     init(); 
    } 

    public VerticalSplitLayoutPanel(int splitterSize) { 
     super(splitterSize); 
     init(); 

    } 

    private void init() { 
     setSize("100%", "100%"); 
     //int clientH = Window.getClientHeight()-offset; 
     // double size = clientH * 50/100; 
     addNorth(topScrollPanel, getOffsetHeight()/2.0); 
     add(bottomScrollPanel); 

     sinkEvents(Event.ONMOUSEDOWN | Event.ONMOUSEUP); 
    } 

    boolean dragged; 
    public void onBrowserEvent(Event event) { 
     super.onBrowserEvent(event); 
     switch (DOM.eventGetType(event)) { 
      case Event.ONMOUSEDOWN: 
       dragged = true; 
       break; 
      case Event.ONMOUSEUP: 
       dragged = false; 
       break; 
      case Event.ONMOUSEMOVE: 
       break; 
     } 
    } 

    public boolean isResizing(){ 
     return dragged; 
    } 

    public void setTopWidget(Widget widget){ 
     topScrollPanel.add(widget); 
    } 

    public void setBottomWidget(Widget widget){ 
     bottomScrollPanel.add(widget); 
    } 


    public void removeWidget(Widget widget){ 
     try { 
      if(topScrollPanel.getWidget()== widget){ 
       topScrollPanel.remove(widget); 
       return; 
      } 
      bottomScrollPanel.remove(widget); 
     } catch (Exception e) { 
     } 

    } 


    public void setSplitPosition(String percent){ 
     if(percent.toLowerCase().indexOf("px") > -1){ 
      percent = percent.replace("px","").trim(); 
      int p = Integer.parseInt(percent); 
      setSplitPosition(p); 
      return; 
     } 
     percent = percent.replace("%","").trim(); 
     int p = Integer.parseInt(percent); 
     int oH = getOffsetHeight(); 
     if(oH == 0){ 
      oH = (Window.getClientHeight()-offset); 
     } 
     double h = (oH*p)/100.0; 
     setWidgetSize(topScrollPanel, h); 

    } 


    public void setSplitPosition(int pixels){ 
     setWidgetSize(topScrollPanel, pixels); 

    } 



    public void setOffset(int size){ 
     this.offset = size; 
    } 

}