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

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。

最佳答案

标准模式和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;
    }

}

09-12 05:08