0

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

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

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

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

我的目标是在不删除使用情况的情况下消除这些警告HorizontalSplitPanel,不提供全局编译器标志-Xlint:-deprecation

由于语句,方法或类级别的注释@SuppressWarnings("deprecation")似乎不起作用import HorizontalSplitPanel,替换不推荐使用的 Horizo​​ntalSplitPanel 类不是一个选项(目前)。

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

我正在使用 NetBeans 7.1、Java 1.6、GWT 2.3。

4

2 回答 2

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;
    }

}
于 2013-05-06T17:06:12.220 回答
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);
    }
}

补充说明。

我的解决方案使用IsWidget来自 GWT 的接口的小技巧 - 这可以最大限度地减少代码影响,因为Widget可以IsWidget在大多数对 GTW API 的调用中替换为实现。

我的代码中使用的每个方法HorizontalSplitPanel现在都由字段HorizontalSplitPanelWrapper存储的内部 Horizo​​ntalSplitPanel 实现并委托给它。hsp

不能将 Horizo​​ntalSplitPanel 声明为 type/param/result 的字段和方法,因为无论@SuppressWarnings("deprecation"). 因此,hsp字段被声明为Panel.

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

而已。由于 Horizo​​ntalSplitPanel,不再有弃用警告,仍然可以使用。

于 2012-06-13T11:16:06.767 回答