View Javadoc

1   package org.catacomb.druid.swing;
2   
3   import org.catacomb.interlish.content.IntPosition;
4   import org.catacomb.interlish.interact.DComponent;
5   import org.catacomb.interlish.structure.TextPainter;
6   
7   import java.awt.*;
8   
9   import javax.swing.JPanel;
10  
11  import javax.swing.Scrollable;
12  
13  
14  
15  public class DTextCanvas extends JPanel implements DComponent, Scrollable {
16      private static final long serialVersionUID = 1L;
17  
18  
19  
20      BasicStroke bs1 = new BasicStroke((float)1.0);
21  
22      Color bgColor = new Color(32, 232, 238);  // ADHOC
23      boolean antialias;
24  
25      TextPainter textPainter;
26  
27      int txtHeight;
28  
29      DPanel parentPanel;
30  
31      public DTextCanvas() {
32          setFont(new Font("sansserif", Font.PLAIN, 12));
33          txtHeight=400;
34      }
35  
36  
37  
38      public void setTooltip(String s) {
39          setToolTipText(s);
40      }
41  
42  
43  
44  
45      public void setTextPainter(TextPainter tp) {
46          textPainter = tp;
47      }
48  
49      public void paintComponent(Graphics g0) {
50  
51          g0.setColor(bgColor);
52          g0.fillRect(0, 0, getWidth(), getHeight());
53          Graphics2D g = (Graphics2D)g0;
54  
55          if (antialias) {
56              g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
57                                 RenderingHints.VALUE_ANTIALIAS_ON);
58          } else {
59              g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
60                                 RenderingHints.VALUE_ANTIALIAS_OFF);
61          }
62  
63          simpleStroke(g);
64  
65          paint2D(g);
66  
67      }
68  
69      final void simpleStroke(Graphics2D g) {
70          g.setStroke(bs1);
71      }
72  
73      public void paint2D(Graphics2D g) {
74  
75          if (textPainter != null) {
76              textPainter.paintText(g);
77  
78              txtHeight = textPainter.getFullTextHeight();
79          }
80  
81  
82      }
83  
84      public void requestRepaint() {
85          repaint();
86      }
87  
88      public IntPosition getScreenPosition() {
89          Point p = getLocationOnScreen();
90          return new IntPosition((int)(p.getX()), (int)(p.getY()));
91      }
92  
93      public void setAntialias(boolean b) {
94          antialias = b;
95          repaint();
96      }
97  
98  
99      public boolean containsPoint(IntPosition pos) {
100         int x = pos.getX();
101         int y = pos.getY();
102         boolean ret = (x > 5 &&  y > 10 &&
103                        x < getWidth() - 10 && y < getHeight() - 10);
104         return ret;
105     }
106 
107 
108 
109     public void contentSizeChanged() {
110         invalidate();
111         if (parentPanel != null) {
112             parentPanel.validate();
113         }
114         repaint();
115     }
116 
117 
118 
119     public Dimension getPreferredSize() {
120         return new Dimension(getWidth(), txtHeight);
121     }
122 
123 
124     public Dimension getPreferredScrollableViewportSize() {
125         Dimension d = getPreferredSize();
126         Dimension ret = d; // new Dimension((int)(d.getWidth()), txtHeight);
127         return ret;
128     }
129 
130     public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) {
131         return 25;
132     }
133 
134     public boolean getScrollableTracksViewportHeight() {
135         return false;
136     }
137 
138     public boolean getScrollableTracksViewportWidth() {
139         return true;
140     }
141 
142     public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) {
143         return 25;
144     }
145 
146 
147 
148 
149 
150 
151     public void setParentContainer(DPanel dp) {
152         parentPanel = dp;
153     }
154 
155 
156 
157 
158 
159 
160 
161 }
162 
163