summaryrefslogtreecommitdiffstats
path: root/tdehtml/java/tests/badapplets/BadApplet.java
blob: c8217d2568bf1f05eb278c166fdce37877d44bd5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import java.io.*;
import java.net.*;
import java.awt.datatransfer.*;

public class BadApplet extends JApplet {
    JTabbedPane tabs        = new JTabbedPane();
    JPanel FileSystemTests  = new JPanel();
    JPanel NetworkTests     = new JPanel();
    JPanel EnvironmentTests = new JPanel();

    JButton writeFileButton      = new JButton("Write File");
    JButton readFileButton       = new JButton("Read File");
    JButton connectSocketButton  = new JButton("Connect Socket");
    JButton frameButton          = new JButton("Open Frame Without Warning Tag");
    JButton readSystemPropButton = new JButton("Read System Property");
    JButton printButton          = new JButton("Print");
    JButton clipBoardButton      = new JButton("Read Clipboard");

    JTextField writePath         = new JTextField( "/amd/ns/root/home/sbarnes/test.txt" );
    JTextField readPath          = new JTextField("/amd/ns/root/home/sbarnes/test.txt");
    JTextField url               = new JTextField("URL");
    JTextField port              = new JTextField("port");
    JTextField systemProp        = new JTextField("os.name");
    JTextField output            = new JTextField();

    //Construct the applet
    public BadApplet() {
        try {
            //event handlers ******************************************************
            writeFileButton.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    writeFileButton_actionPerformed(e);
                }
            });
            readFileButton.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    readFileButton_actionPerformed(e);
                }
            });
            connectSocketButton.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    connectSocketButton_actionPerformed(e);
                }
            });
            frameButton.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(ActionEvent e) {
                frameButton_actionPerformed(e);
                }
            });
            readSystemPropButton.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    readSystemPropButton_actionPerformed(e);
                }
            });
            printButton.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    printButton_actionPerformed(e);
                }
            });
            clipBoardButton.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    clipBoard_actionPerformed(e);
                }
            });

            //do layout ***********************************************************
            getContentPane().setLayout( new BorderLayout() );

            FileSystemTests.setLayout( new FlowLayout( FlowLayout.LEFT ) );
            FileSystemTests.add( writeFileButton );
            FileSystemTests.add( writePath );
            FileSystemTests.add( readFileButton );
            FileSystemTests.add( readPath );

            NetworkTests.setLayout( new FlowLayout( FlowLayout.LEFT ) );
            NetworkTests.add( connectSocketButton );
            NetworkTests.add( url );
            NetworkTests.add( port );

            EnvironmentTests.setLayout( new FlowLayout( FlowLayout.LEFT ) );
            EnvironmentTests.add( frameButton );
            EnvironmentTests.add( readSystemPropButton );
            EnvironmentTests.add( systemProp );
            EnvironmentTests.add( printButton );
            EnvironmentTests.add( clipBoardButton );

            tabs.add( FileSystemTests, "File System" );
            tabs.add( NetworkTests, "Network" );
            tabs.add( EnvironmentTests, "Environment" );

            this.getContentPane().add( tabs, BorderLayout.CENTER );
            this.getContentPane().add( output, BorderLayout.SOUTH );
        }
        catch(Exception e) {
            e.printStackTrace();
        }
    }

    public void paint( Graphics g )
    {
        System.out.println( "graphics g = " + g );
        System.out.println( "clip area = " + g.getClip() );
        System.out.println( "bounds of the clip area = " + g.getClipBounds() );

        super.paint( g );
    }

    //Initialize the applet
    public void init() {}

    void writeFileButton_actionPerformed(ActionEvent e) {
        try{
            PrintWriter writer = new PrintWriter(new FileOutputStream(writePath.getText()));
            writer.println("Here is some text");
            writer.close();
            output.setText("Write was successful");
        } catch (Exception ex){output.setText(ex.getMessage());}
    }

    void readSystemPropButton_actionPerformed(ActionEvent e) {
        try{
            output.setText(System.getProperty(systemProp.getText()));
        } catch (Exception ex){output.setText("Error getting prop: " + ex.getMessage());}
    }

    void readFileButton_actionPerformed(ActionEvent e) {
        try{
            BufferedReader reader = new BufferedReader(new FileReader(readPath.getText()));
            output.setText("Read was successful: " + reader.readLine());
        } catch (Exception ex){output.setText(ex.getMessage());}
    }

    void connectSocketButton_actionPerformed(ActionEvent e) {
        try{
            Integer thePort = new Integer(port.getText());
            Socket socket = new Socket(url.getText(), thePort.intValue());
            socket.getOutputStream();
            output.setText("Socket connection successful");
        } catch (Exception ex){output.setText("Socket unsuccessfull: " + ex.getMessage());}
    }

    void frameButton_actionPerformed(ActionEvent e) {
        JFrame frame = new JFrame("Does this Frame have a warning sign");
        frame.setSize(200,200);
        frame.show();
        if (frame.getWarningString() == null)
            output.setText("No warning string in frame");
        else
            output.setText(frame.getWarningString());
    }

    void clipBoard_actionPerformed(ActionEvent e) {
        try {
            Clipboard clip = Toolkit.getDefaultToolkit().getSystemClipboard();

            Transferable trans = clip.getContents(null);
            if (trans == null){
                output.setText("Clipboard is empty");
                return;
            }
            output.setText((String)trans.getTransferData(DataFlavor.stringFlavor));
        }catch(Exception ex){ex.getMessage();}
    }

    void printButton_actionPerformed(ActionEvent e) {
        try{
            JFrame testFrame = new JFrame("test");
            testFrame.getContentPane().add(this, BorderLayout.CENTER);
            PrintJob printer = Toolkit.getDefaultToolkit().getPrintJob(testFrame, "Applet Print Test", null);

            if (printer == null){
                output.setText("PrintJob is null");
                return;
            }

            Graphics g = printer.getGraphics();
            g.drawString("This is the applet print test", 50, 50);
            g.dispose();
            printer.end();
        }catch(Exception ex){ex.getMessage();}
    }

    //Main method
    public static void main(String[] args) {
        BadApplet applet = new BadApplet();

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation( WindowConstants.DISPOSE_ON_CLOSE );
        frame.setTitle("Applet Frame");
        frame.getContentPane().add(applet, BorderLayout.CENTER);
        frame.setSize(400,320);
        frame.setVisible(true);

        applet.init();
        applet.start();
    }

}