/* 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 javax.swing.*; 

2. import javax.swing.text.*; 

3.  

4. import java.awt.*; 

5. import java.awt.event.*; 

6. import java.awt.geom.*; 

7. import java.awt.print.*; 

8. import java.io.*; 

9.  

10. public class StyleDoc 

11.     extends JApplet 

12.      implements ActionListener, Printable 

13.

14.   DefaultStyledDocument doc ; 

15.   Style stp, stn, stpar ; 

16.   JTextPane tp ; 

17.   JScrollPane sp ; 

18.   JButton but, undo, redo ; 

19.   Container cp ; 

20.  

21.   public StyleDoc() 

22.   { 

23.     StyleContext sc = new StyleContext(); 

24.   doc = new DefaultStyledDocument( sc ) ; 

25.     tp = new  JTextPane () ; 

26.     sp = new JScrollPane ( tp ); 

27.     but = new JButton ( "More text" ); 

28.   } 

29.  

30.   public void init() 

31.   { 

32.     stp = doc.addStyle( "Purty", null ); 

33.     StyleConstants.setForeground( stp, Color.red ); 

34.     StyleConstants.setBackground( stp, Color.yellow ); 

35.     StyleConstants.setFontFamily( stp, "Monospaced" ); 

36.     try

37.     doc.insertString 

38.     ( 0

39.          "All That Is\nIs All there is\tfolks\n"

40.            stp ); 

41.  

42.     stn = doc.addStyle( "Nice", stp ); 

43.     StyleConstants.setForeground( stn, Color.magenta ); 

44.     StyleConstants.setFontSize( stn, 22 ); 

45.  

46.     stpar = doc.addStyle( "Parastyle", null ); 

47.     StyleConstants.setLeftIndent( stpar, 20.0f ); 

48.     StyleConstants.setSpaceAbove( stpar, 20.0f ); 

49.     StyleConstants.setLineSpacing( stpar, 0.5f ); 

50.     StyleConstants.setFirstLineIndent( stpar, 35.0f ); 

51.  

52.     tp.setDocument( doc ); 

53.  

54.   but.addActionListener(this); 

55.     } 

56.     catch ( BadLocationException ex ) 

57.     { ex.printStackTrace(System.out); } 

58.  

59.     cp = getContentPane(); 

60.     cp.setLayout( new FlowLayout() ); 

61.     cp.add( but );  

62.     sp.setPreferredSize( new Dimension(300,400) ); 

63.     cp.add( sp ); 

64.   } 

65.  

66.   public void actionPerformed ( ActionEvent ev ) 

67.   { 

68.     int pos = doc.getLength(); 

69.     String str = "Bundy,\nJames Bundy\n" 

70.        + "Methinks there's a spot u missed, Moneyspendy."

71.  

72.     try

73.     doc.insertString(  pos,  str,  stn ); 

74.     doc.setParagraphAttributes( 

75.        pos, str.length(), 

76.          stpar, true ); 

77.     } 

78.     catch ( BadLocationException ex ) 

79.     { ex.printStackTrace(System.out); } 

80.  

81.     but.setEnabled(false); 

82.   } 

83.      

84. //====  not called :  ================= 

85.   public int print( Graphics gr, PageFormat fm, int pnr ) 

86.   { 

87.     if ( pnr > 2 ) return Printable.NO_SUCH_PAGE

88.  

89.     double poff = pnr * fm.getImageableHeight(); 

90.  

91.     View  vw   = tp.getUI().getRootView( tp ); 

92.  

93.     if ( poff > vw.getPreferredSpan( View.Y_AXIS ) ) 

94.       return Printable.NO_SUCH_PAGE

95.  

96.     ( (Graphics2D)gr).translate( 0d, -poff ); 

97.  

98.     Shape shp = 

99.       new Rectangle2D.Double

100.            fm.getImageableX(), fm.getImageableY(), 

101.            fm.getImageableWidth(), 

102.            fm.getImageableHeight() + poff ); 

103.  

104.     vw.paint( gr, shp ); 

105.     return Printable.PAGE_EXISTS

106.   } 

107. //====  not called :  ================= 

108.   void saveIt() 

109.    { 

110.    try

111.       ObjectOutputStream obos = new ObjectOutputStream 

112.         ( new FileOutputStream("doq.obj") ); 

113.       obos.writeObject( tp.getDocument() ); 

114.       obos.flush(); 

115.    } 

116.    catch(Exception e) { } 

117.    } 

118.

119.