如何避免NetBeans Java项目中GWT Horizo​​ntalSplitPanel的弃用警告?

时间:2012-06-11 19:48:25

标签: java gwt netbeans deprecated

使用实用程序方法编译代码时是否可以避免弃用警告:

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

并明确声明和/或创建Horizo​​ntalSplitPanel小部件,例如:

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

我的目标是消除这些警告而不删除HorizontalSplitPanel用法,不借助最小的手动重构来提供全局编译器标记(-Xlint:-deprecation)和 在使用Horizo​​ntalSplitPanel和我的实用程序方法对代码可能产生的影响方面(即代码变化尽可能少)。

方法或类级别的注释@SuppressWarnings("deprecation")似乎不起作用,因为import HorizontalSplitPanel语句,在一个选项中替换已弃用的Horizo​​ntalSplitPanel类(暂时)。

我的目标是否可以实现?如果是这样,最好的方法是什么?

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

2 个答案:

答案 0 :(得分: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;
    }

}

答案 1 :(得分: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,类型/ param / result必须没有声明字段和方法,因为无论@SuppressWarnings("deprecation")如何,它都会产生弃用警告。因此,hsp字段被声明为Panel

如果在其余代码中使用了更多Horizo​​ntalSplitPanel方法,则HorizontalSplitPanelWrapper中的每个方法都必须有伪委托方法。必须从字段hsp中检索正确的Horizo​​ntalSplitPanel对象,并在每个此类方法中使用显式强制转换。

就是这样。由于仍然可以使用Horizo​​ntalSplitPanel,不再有弃用警告。