/* Copyright (c) SEMM NL All rights reserved.
Author : Paul Hamaker. Part of JavaLessons.com
This code is for educational purposes only. Use at own risk.*/

1. import java.awt.*; 

2. import java.awt.event.*; 

3. import java.applet.*; 

4. import java.util.*; 

5. import java.io.*; 

6. import java.net.*; 

7.  

8. public class Propslet 

9.     extends Applet 

10.     implements ActionListener 

11.

12.   Properties props ; 

13.    

14.   Button btnload, btnsave ; 

15.   TextArea ta ; 

16.   TextField tf ; 

17.  

18.   public void 

19.   actionPerformed ( ActionEvent ev ) 

20.   { 

21.     Object src = ev.getSource (); 

22.     if ( src == btnload ) 

23.     { 

24.       try

25.         URL url = new URL( getCodeBase(), "settings.txt" ); 

26.         InputStream fis = url.openStream (); 

27.  

28. //FileInputStream fis = new  FileInputStream ("h:\\settings.txt"); 

29.  

30.         props = new Properties() ; 

31.         props.load ( fis ); 

32.  

33.         Iterator itr = props.keySet().iterator(); 

34.         String value ; 

35.  

36.         while ( itr.hasNext() ) 

37.         { 

38.           String key = ( String ) itr.next() ; 

39.           value = props.getProperty( key ) ; 

40.           ta.append ( key + " = " + value + "\n" ) ; 

41.         } 

42.         tf.setText ( props.getProperty( "Volume" ) ) ; 

43.  

44.         btnload.setEnabled (false); 

45.         btnsave.setEnabled(true); 

46.         tf.requestFocus (); 

47.       } 

48.       catch (Exception e) 

49.       { 

50.         ta.append ( "No go " + e + "\n" ) ; 

51.       } 

52.       return

53.     } 

54.     if ( src == btnsave ) 

55.     { 

56.       String tx =  tf.getText() ; 

57.  

58.       if ( tx.trim().length() != 0

59.       { 

60.         props.put ( "Volume", tx ); 

61.       } 

62.       try

63.         FileOutputStream fos = 

64.           new  FileOutputStream ("settings.txt"); 

65.         props.store( fos, "Aurore's settings" ); 

66.         fos.close(); 

67.       } 

68.       catch (Exception e) 

69.       { 

70.         ta.append ( "No go " + e + "\n" ) ; 

71.       } 

72.     } 

73.   } 

74.   public void 

75.   init () 

76.   { 

77.     btnload = new Button ( "Load properties" ) ; 

78.     btnload.addActionListener ( this ) ; 

79.     btnsave = new Button ( "Save properties" ) ; 

80.     btnsave.setEnabled ( false ); 

81.     btnsave.addActionListener ( this ) ; 

82.  

83.     ta = new TextArea ( 6, 40 ) ; 

84.     tf = new TextField ( 5 ) ; 

85.  

86.     add ( btnload ) ; 

87.     add ( btnsave ) ; 

88.     add ( ta ) ; 

89.     add ( new Label ( "Volume" ) ) ; 

90.     add ( tf ) ; 

91.  

92.     setBackground ( Color.pink ); 

93.  

94.     validate () ; 

95.     btnload.requestFocus (); 

96.   } } 

97. /* 

98. ======== settings.properties ============ 

99. #Aurore's settings 

100. #Mon Jan 24 13:30:24 CET 2000 

101. Name=Aurore 

102. Email=aura@nohant.fr 

103. Volume=54 

104. =========== send to server : ============ 

105. URL url = new URL( "http://samehost.org/servlet/recvprop" ); 

106.  

107. URLConnection uc = url.openConnection() ; 

108. uc.setDoOutput( true ); 

109. OutputStream uos = uc.getOutputStream(); 

110. props.store( uos, "Aurore's settings" ); 

111. ====== 

112. str = props.getProperty ( "Znorklevel", "52"  ) ; 

113. ====== 

114. loadFromXML 

115. storeToXML 

116. */