Subject : Java 1.1 AWT deprecated methods

Solution Description:
=====================

. 아래의 예제는 jdk1.0.2에서 작성한 소스코드이다.

www% vi DialogWindow.java

import java.awt.*;

public class DialogWindow extends Frame {
    private boolean inAnApplet = true;
    private SimpleDialog dialog;
    private TextArea textArea;

    public DialogWindow() {
        textArea = new TextArea(5, 40);
        textArea.setEditable(false);
        add("Center", textArea);
        Button button = new Button("Click to bring up dialog");
        Panel panel = new Panel();
        panel.add(button);
        add("South", panel);
    }

    public boolean handleEvent(Event event) {
        if (event.id == Event.WINDOW_DESTROY) {
            if (inAnApplet) {
                dispose();
            } else {
                System.exit(0);
            }
        }   
        return super.handleEvent(event);
    }

    public boolean action(Event event, Object arg) {
        if (dialog == null) {
            dialog = new SimpleDialog(this, "A Simple Dialog");
        }
        dialog.show();
        return true;
    }

    public void setText(String text) {
        textArea.appendText(text + "\n");
    }

    public static void main(String args[]) {
        DialogWindow window = new DialogWindow();
        window.inAnApplet = false;

        window.setTitle("DialogWindow Application");
        window.pack();
        window.show();
    }
}

class SimpleDialog extends Dialog {
    TextField field;
    DialogWindow parent;
    Button setButton;

    SimpleDialog(Frame dw, String title) {
        super(dw, title, false);
        parent = (DialogWindow)dw;

        //Create middle section.
	Panel p1 = new Panel();
        Label label = new Label("Enter random text here:");
        p1.add(label);
        field = new TextField(40);
	p1.add(field);
        add("Center", p1);

        //Create bottom row.
        Panel p2 = new Panel();
        p2.setLayout(new FlowLayout(FlowLayout.RIGHT));
        Button b = new Button("Cancel");
        setButton = new Button("Set");
        p2.add(b);
        p2.add(setButton);
        add("South", p2);

	//Initialize this dialog to its preferred size.
	pack();
    }

    public boolean action(Event event, Object arg) {
        if ( (event.target == setButton)
           | (event.target == field)) {
            parent.setText(field.getText());
        }
        field.selectAll();
        hide();
        return true;
    }
}

. 위의 코드를 jdk1.1.x 환경에서 컴파일하면 아래와 같은 message가 print가
  되는 것을 볼수 있다.
  자바는 jdk1.1.x로 upgrade되면서 변화가 있었다. 
  특히 Window Programming 관련하여 많은 변화가 있었다.
  아래의 message는 jdk1.1은 더좋은 method가 있으니 바꾸어서 jdk1.1로 최적화
  시킬수 있다는 의미이다. 물론 conversion을 하지 않아도 실행하는데는
  문제가 없다. 즉 1.1은 1.0.2를 수용한다는 의미이다.


www% javac DialogWindow.java
Note: DialogWindow.java uses a deprecated API.  Recompile with "-deprecation" for details.
1 warning

. 상세한 정보를 보려면 다음과 같은 option을 줄수도 있다.

www% javac -deprecation DialogWindow.java
DialogWindow.java:18: Note: The method boolean handleEvent(java.awt.Event) in class java.awt.Component has been deprecated, and class DialogWindow (which is not deprecated) overrides it.
    public boolean handleEvent(Event event) {
                   ^
DialogWindow.java:26: Note: The method boolean handleEvent(java.awt.Event) in class java.awt.Component has been deprecated.
        return super.handleEvent(event);
                                ^
DialogWindow.java:29: Note: The method boolean action(java.awt.Event, java.lang.Object) in class java.awt.Component has been deprecated, and class DialogWindow (which is not deprecated) overrides it.
    public boolean action(Event event, Object arg) {
                   ^
DialogWindow.java:38: Note: The method void appendText(java.lang.String) in class java.awt.TextArea has been deprecated.
        textArea.appendText(text + "\n");
                           ^
DialogWindow.java:81: Note: The method boolean action(java.awt.Event, java.lang.Object) in class java.awt.Component has been deprecated, and class SimpleDialog (which is not deprecated) overrides it.
    public boolean action(Event event, Object arg) {
                   ^
DialogWindow.java:87: Note: The method void hide() in class java.awt.Component has been deprecated.
        hide();
            ^
Note: DialogWindow.java uses a deprecated API.  Please consult the documentation for a better alternative.
7 warnings
www% 

. 다음은 위의 코드를 1.1에 맞께끔 conversion하는 과정이다.

  먼저 아래의 테이블을 살펴보자.

                     AWT 1.1 Deprecated Method Table

--------------------------------------------------------------------------------
    Deprecated Method        Class Where            1.1 Replacement
                             Deprecated
--------------------------------------------------------------------------------
                                            See How to Convert
 action                   Component         Event-Handling Code for
                                            examples of handling action
                                            events.
 allowsMultipleSelections List              isMultipleMode
 appendText               TextArea          append
 bounds                   Component         getBounds
 clear                    List              removeAll
 countComponents          Container         getComponentCount

 countItems               Choice, List,     getItemCount
                          Menu
 countMenus               MenuBar           getMenuCount

 deliverEvent             Component,        dispatchEvent
                          Container
 disable()                MenuItem          setEnabled(false)

 enable()                 Component,        setEnabled(true)
                          MenuItem
 enable(expression)       Component         setEnabled(expression)
 getBoundingBox           Polygon           getBounds
 getClipRect              Graphics          getClipBounds
 getCurrent               CheckboxGroup     getSelectedCheckbox
 getCursorType            Frame             getCursor method in Component
 getLineIncrement         Scrollbar         getUnitIncrement
 getPageIncrement         Scrollbar         getBlockIncrement
 getPeer                  Component         No replacement.
 getVisible               Scrollbar         getVisibleAmount
                                            processFocusEvent
                                            See How to Convert
 gotFocus                 Component         Event-Handling Code for
                                            information on preferred ways
                                            to handle events.
                                            processEvent
                                            See How to Convert
 handleEvent              Component         Event-Handling Code for
                                            information on preferred ways
                                            to handle events.
 hide                     Component         setVisible(false)
 insertText               TextArea          insert
 insets                   Container         getInsets
                          Component,
 inside                   Polygon,          contains
                          Rectangle
 isSelected               List              isIndexSelected
                                            processKeyEvent
 keyDown                  Component         See How to Convert
                                            Event-Handling Code.
                                            processKeyEvent
 keyUp                    Component         See How to Convert
                                            Event-Handling Code.
                          Component,
 layout                   Container,        doLayout
                          ScrollPane

 locate                   Component,        getComponentAt
                          Container
 location                 Component         getLocation
                                            processFocusEvent
 lostFocus                Component         See How to Convert
                                            Event-Handling Code.
                          Component,
 minimumSize              Container,        getMinimumSize
                          TextArea,
                          TextField
                                            processMouseEvent
 mouseDown                Component         See How to Convert
                                            Event-Handling Code.
                                            processMouseMotionEvent
 mouseDrag                Component         See How to Convert
                                            Event-Handling Code.
                                            processMouseEvent
 mouseEnter               Component         See How to Convert
                                            Event-Handling Code.
                                            processMouseEvent
 mouseExit                Component         See How to Convert
                                            Event-Handling Code.
                                            processMouseMotionEvent
 mouseMove                Component         See How to Convert
                                            Event-Handling Code.
                                            processMouseEvent
 mouseUp                  Component         See How to Convert
                                            Event-Handling Code.

 move                     Component,        setLocation
                          Rectangle
                          Component,
 nextFocus                Container,        transferFocus
                          Window

 postEvent                Component,        dispatchEvent
                          Window
                          Component,
 preferredSize            Container,        getPreferredSize
                          TextArea,
                          TextField
 replaceText              TextArea          replaceRange

 reshape                  Component,        setBounds
                          Rectangle

 resize                   Component,        setSize
                          Rectangle
 setCurrent               CheckboxGroup     setSelectedCheckbox
 setCursor                Frame             setCursor method in Component
 setEchoCharacter         TextField         setEchoChar
 setLineIncrement         Scrollbar         setUnitIncrement
 setMultipleSelections    List              setMultipleMode
 setPageIncrement         Scrollbar         setBlockIncrement
 show()                   Component         setVisible(true)
 show(expression)         Component         setVisible(expression)
 size                     Component         getSize
--------------------------------------------------------------------------------

. 원래의 1.0.2의 코드를 위의 테이블을 참조하여 수작업으로 할 수도 있지만
  다음과 같은 tool을 이용하여 자동으로 conversion하는 게 편리하다.

www% JAVA_HOME/java/docs/guide/awt/updateAWT DialogWindow.java > tmp.java

www% diff tmp.java DialogWindow.java
33c33
<         dialog.setVisible(true);
---
>         dialog.show();
38c38
<         textArea.append(text + "\n");
---
>         textArea.appendText(text + "\n");
47c47
<         window.setVisible(true);
---
>         window.show();
87c87
<         setVisible(false);
---
>         hide();
www% 
www% cp tmp.java DialogWindow.java
www% javac -deprecation DialogWindow.java
DialogWindow.java:18: Note: The method boolean handleEvent(java.awt.Event) in class java.awt.Component has been deprecated, and class DialogWindow (which is not deprecated) overrides it.
    public boolean handleEvent(Event event) {
                   ^
DialogWindow.java:26: Note: The method boolean handleEvent(java.awt.Event) in class java.awt.Component has been deprecated.
        return super.handleEvent(event);
                                ^
DialogWindow.java:29: Note: The method boolean action(java.awt.Event, java.lang.Object) in class java.awt.Component has been deprecated, and class DialogWindow (which is not deprecated) overrides it.
    public boolean action(Event event, Object arg) {
                   ^
DialogWindow.java:81: Note: The method boolean action(java.awt.Event, java.lang.Object) in class java.awt.Component has been deprecated, and class SimpleDialog (which is not deprecated) overrides it.
    public boolean action(Event event, Object arg) {
                   ^
Note: DialogWindow.java uses a deprecated API.  Please consult the documentation for a better alternative.
5 warnings
www% 

. 위의 과정을 보면 updateAWT를 이용하여 완전하게 convert할 수 없음을 알게
  되었을 것이다. 즉 위의 예제에서 보면 handleEvent, action등 소위 Event Code등은
  자동으로 변환이 안되고 아래의 테이블을 참조하여 수작업으로 고쳐주어야 한다.


                      Event-Handling Code Conversion Table

--------------------------------------------------------------------------------
                     1.0                 |                              1.1
--------------------------------------------------------------------------------
  Event/Method         |Generated By     |  Interface      |   Methods
--------------------------------------------------------------------------------
                       |Button, List,    |                 |
                       |MenuItem,        |ActionListener   |actionPerformed
                       |                 |                 |(ActionEvent)
                       |TextField        |                 |
 ACTION_EVENT/action   |--------------------------------------------------------
                       |Checkbox,        |                 |
                       |CheckboxMenuItem,|ItemListener     |itemStateChanged
                       |                 |                 |(ItemEvent)
                       |Choice           |                 |
--------------------------------------------------------------------------------
                       |                 |                 |windowClosing
                       |                 |                 |(WindowEvent)
                       |                 |                 |windowOpened
                       |                 |                 |(WindowEvent)
                       |                 |                 |windowIconified
                       |                 |                 |(WindowEvent)
 WINDOW_DESTROY        |                 |                 |windowDeiconified
                       |                 |                 |(WindowEvent)
 WINDOW_EXPOSE         |                 |                 |windowClosed
                       |                 |                 |(WindowEvent) (no
 WINDOW_ICONIFY        |Dialog, Frame    |WindowListener   |1.0 equivalent)
 WINDOW_DEICONIFY      |                 |                 |windowActivated
                       |                 |                 |(WindowEvent) (no
                       |                 |                 |1.0 equivalent)
                       |                 |                 |windowDeactivated
                       |                 |                 |(WindowEvent) (no
                       |                 |                 |1.0 equivalent)
--------------------------------------------------------------------------------
                       |                 |                 |componentMoved
                       |                 |                 |(ComponentEvent)
                       |                 |                 |componentHidden
                       |                 |                 |(ComponentEvent)
                       |                 |                 |(no 1.0 equivalent)
 WINDOW_MOVED          |Dialog, Frame    |ComponentListener|componentResized
                       |                 |                 |(ComponentEvent)
                       |                 |                 |(no 1.0 equivalent)
                       |                 |                 |componentShown
                       |                 |                 |(ComponentEvent)
                       |                 |                 |(no 1.0 equivalent)
--------------------------------------------------------------------------------
 SCROLL_LINE_UP        |                 |                 |
 SCROLL_LINE_DOWN      |                 |                 |
 SCROLL_PAGE_UP        |                 |AdjustmentListener|
 SCROLL_PAGE_DOWN      |Scrollbar        |(or use the new  |adjustmentValueC
                       |                 |                 |hanged(Adjustment
 SCROLL_ABSOLUTE       |                 |ScrollPane class)|Event)
 SCROLL_BEGIN          |                 |                 |
 SCROLL_END            |                 |                 |
--------------------------------------------------------------------------------
 LIST_SELECT           |Checkbox,        |                 |
 LIST_DESELECT         |CheckboxMenuItem,|ItemListener     |itemStateChanged
                       |                 |                 |(ItemEvent)
                       |Choice, List     |                 |
--------------------------------------------------------------------------------
 MOUSE_DRAG/mouseDrag  |Canvas, Dialog,  |                 |mouseDragged
                       |                 |                 |(MouseEvent)
 MOUSE_MOVE/mouseMove  |Frame, Panel,    |MouseMotionListener|mouseMoved
                       |                 |                 |(MouseEvent)
                       |Window           |                 |
--------------------------------------------------------------------------------
                       |                 |                 |mousePressed
                       |                 |                 |(MouseEvent)
 MOUSE_DOWN/mouseDown  |                 |                 |mouseReleased
                       |                 |                 |(MouseEvent)
 MOUSE_UP/mouseUp      |Canvas, Dialog,  |                 |mouseEntered
                       |                 |                 |(MouseEvent)
 MOUSE_ENTER/mouseEnter|Frame, Panel,    |MouseListener    |mouseExited
                       |                 |                 |(MouseEvent)
 MOUSE_EXIT/mouseExit  |Window           |                 |mouseClicked
                       |                 |                 |(MouseEvent) (no
                       |                 |                 |1.0 equivalent)
--------------------------------------------------------------------------------
 KEY_PRESS/keyDown     |                 |                 |
 KEY_RELEASE/keyUp     |                 |                 |keyPressed
                       |                 |                 |(KeyEvent)
 KEY_ACTION/keyDown    |Component        |KeyListener      |keyReleased
                       |                 |                 |(KeyEvent)
 KEY_ACTION_RELEASE/keyUp|               |                 |keyTyped(KeyEvent) 
                       |                 |                 |(no 1.0 equivalent)
--------------------------------------------------------------------------------
 GOT_FOCUS/gotFocus    |                 |                 |focusGained
                       |                 |                 |(FocusEvent)
 LOST_FOCUS/lostFocus  |Component        |FocusListener    |focusLost
                       |                 |                 |(FocusEvent)
--------------------------------------------------------------------------------
 No 1.0 equivalent.    |                 |ContainerListener|componentAdded 
                       |                 |                 |(ContainerEvent)
                       |                 |                 |componentRemoved 
                       |                 |                 |(ContainerEvent)
--------------------------------------------------------------------------------
 No 1.0 equivalent.    |                 |TextListener     |textValueChanged
                       |                 |                 |(TextEvent)
--------------------------------------------------------------------------------
 
. 아래는 conversion을 마친후의 예제 코드이다.

www% vi DialogWindow.java

import java.awt.*;
import java.awt.event.*;

public class DialogWindow extends Frame 
			  implements WindowListener,
			             ActionListener {
    private boolean inAnApplet = true;
    private SimpleDialog dialog;
    private TextArea textArea;

    public DialogWindow() {
        textArea = new TextArea(5, 40);
        textArea.setEditable(false);
        add("Center", textArea);
        Button button = new Button("Click to bring up dialog");
	button.addActionListener(this);
        Panel panel = new Panel();
        panel.add(button);
        add("South", panel);
	addWindowListener(this);
    }

    public void windowClosed(WindowEvent event) {
    }

    public void windowDeiconified(WindowEvent event) {
    }

    public void windowIconified(WindowEvent event) {
    }

    public void windowActivated(WindowEvent event) {
    }

    public void windowDeactivated(WindowEvent event) {
    }

    public void windowOpened(WindowEvent event) {
    }

    public void windowClosing(WindowEvent event) {
        if (inAnApplet) {
            dispose();
        } else {
            System.exit(0);
        }
    }

    public void actionPerformed(ActionEvent event) {
        if (dialog == null) {
            dialog = new SimpleDialog(this, "A Simple Dialog");
        }
        dialog.setVisible(true);
    }

    public void setText(String text) {
        textArea.append(text + "\n");
    }

    public static void main(String args[]) {
        DialogWindow window = new DialogWindow();
        window.inAnApplet = false;

        window.setTitle("DialogWindow Application");
        window.pack();
        window.setVisible(true);
    }
}

class SimpleDialog extends Dialog implements ActionListener {
    TextField field;
    DialogWindow parent;
    Button setButton;

    SimpleDialog(Frame dw, String title) {
        super(dw, title, false);
        parent = (DialogWindow)dw;

        //Create middle section.
	Panel p1 = new Panel();
        Label label = new Label("Enter random text here:");
        p1.add(label);
        field = new TextField(40);
	field.addActionListener(this);
	p1.add(field);
        add("Center", p1);

        //Create bottom row.
        Panel p2 = new Panel();
        p2.setLayout(new FlowLayout(FlowLayout.RIGHT));
        Button b = new Button("Cancel");
	b.addActionListener(this);
        setButton = new Button("Set");
	setButton.addActionListener(this);
        p2.add(b);
        p2.add(setButton);
        add("South", p2);

	//Initialize this dialog to its preferred size.
	pack();
    }

    public void actionPerformed(ActionEvent event) {
	Object source = event.getSource();
        if ( (source == setButton)
           | (source == field)) {
            parent.setText(field.getText());
        }
        field.selectAll();
        setVisible(false);
    }
}

----------------------------------------------------------------------------

Revision History

작성일자 : 97.07.21
작성자 : 이민호

수정일자 :
수정자 :