Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/hartmath/FormulaIcons.java


1   /*
2    *  FormulaIcons.java
3    *  Copyright (C) 2001 Klaus Hartlage
4    *
5    *  This program is free software; you can redistribute it and/or
6    *  modify it under the terms of the GNU General Public License
7    *  as published by the Free Software Foundation; either version 2
8    *  of the License, or any later version.
9    *
10   *  This program is distributed in the hope that it will be useful,
11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   *  GNU General Public License for more details.
14   *
15   *  You should have received a copy of the GNU General Public License
16   *  along with this program; if not, write to the Free Software
17   *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18   */
19  package org.hartmath;
20  
21  import javax.swing.text.*;
22  import javax.swing.*;
23  import java.awt.*;
24  import java.util.Vector;
25  import java.util.ArrayList;
26  
27  import com.hartmath.prettyprint.*;
28  import com.hartmath.expression.HObject;
29  /**
30   *  Description of the Class
31   *
32   *@author     khartlage
33   *@created    16. Juli 2001
34   */
35  public class FormulaIcons {
36  
37          // private members
38          private JEditorPane editorPane;
39          private Document outputDocument;
40          private PrettyPrintBox box;
41          private BoxMetrics size;
42          private Vector icons;
43          private int max_width;
44  
45  
46          /**
47           *  Constructor for the FormulaIcons object
48           *
49           *@param  editorPane  Description of Parameter
50           *@param  expr        Description of Parameter
51           *@param  font        Description of Parameter
52           *@param  fontSize    Description of Parameter
53           *@param  color       Description of Parameter
54           */
55          public FormulaIcons(
56                          JEditorPane editorPane,
57                          HObject expr,
58                          String font,
59                          int fontSize,
60                          Color color) {
61                  this.editorPane = editorPane;
62                  // this.max_width = editorPane.getWidth();
63                  this.max_width = editorPane.getVisibleRect().width;
64                  outputDocument = editorPane.getDocument();
65  
66                  icons = new Vector();
67                  // initial box:
68                  box = ToPrettyPrinter.convert(expr, font, fontSize, 0);
69                  size = box.getBoxMetrics();
70  
71                  if (size.width > max_width) {
72                          ArrayList splitBox;
73                          while (size.width > max_width &&
74                                          max_width > 20) {
75  
76                                  splitBox = box.split(max_width);
77                                  if (splitBox == null) {
78                                          break;
79                                  }
80                                  icons.add(new ExpressionIcon((PrettyPrintBox) splitBox.get(0),
81                                                  font,
82                                                  fontSize,
83                                                  color));
84                                  box = (PrettyPrintBox) splitBox.get(1);
85                                  if (box == null) {
86                                          break;
87                                  }
88                                  size = box.getBoxMetrics();
89                          }
90                  }
91                  if (box != null) {
92                          icons.add(new ExpressionIcon(box,
93                                          font,
94                                          fontSize,
95                                          color));
96                  }
97          }
98  
99  
100         /**
101          *  Gets the vector attribute of the FormulaIcons object
102          *
103          *@return    The vector value
104          */
105         public Vector getVector() {
106                 return icons;
107         }
108 
109 
110         /**
111          *  Return true if the icon's width, is greater than the editorKit's width
112          *
113          *@return    true, if the icon is to big
114          */
115         public boolean isOverSized() {
116                 for (int i = 0; i < icons.size(); i++) {
117                         if (((ExpressionIcon) icons.elementAt(i)).getIconWidth() > max_width) {
118                                 return true;
119                         }
120                         ;
121                 }
122                 return false;
123         }
124 
125 
126         /**
127          *  Description of the Method
128          */
129         public void printOutput() {
130                 for (int i = 0; i < icons.size(); i++) {
131                         printOutput((ExpressionIcon) icons.elementAt(i));
132                 }
133 
134         }
135 
136 
137 
138         /**
139          *  Description of the Method
140          *
141          *@param  text  Description of Parameter
142          */
143         private void printOutput(String text) {
144                 SimpleAttributeSet style = new SimpleAttributeSet();
145                 style.addAttribute(StyleConstants.Foreground, Color.black);
146                 try {
147                         outputDocument.insertString(
148                                         outputDocument.getLength(),
149                                         text, style);
150                 }
151                 catch (BadLocationException bl) {
152                         bl.printStackTrace();
153                 }
154                 editorPane.setCaretPosition(outputDocument.getLength());
155         }
156 
157 
158         /**
159          *  Description of the Method
160          *
161          *@param  icon  Description of Parameter
162          */
163         private void printOutput(Icon icon) {
164                 SimpleAttributeSet style = new SimpleAttributeSet();
165 
166                 StyleConstants.setIcon(style, icon);
167 
168                 try {
169                         outputDocument.insertString(
170                                         outputDocument.getLength(), "\n", null);
171                         outputDocument.insertString(
172                                         outputDocument.getLength(), " ", style);
173                 }
174                 catch (BadLocationException bl) {
175                         bl.printStackTrace();
176                 }
177                 editorPane.setCaretPosition(outputDocument.getLength());
178         }
179 
180 
181         /**
182          *  Description of the Class
183          *
184          *@author     khartlage
185          *@created    24. September 2001
186          */
187         public class ExpressionIcon implements Icon {
188 
189                 // private members
190                 private Color color;
191                 private PrettyPrintBox box;
192                 private BoxMetrics size;
193 
194 
195                 /**
196                  *  Constructor for the ExpressionIcon object
197                  *
198                  *@param  font      Description of Parameter
199                  *@param  fontSize  Description of Parameter
200                  *@param  color     Description of Parameter
201                  *@param  box       Description of Parameter
202                  */
203                 public ExpressionIcon(
204                                 PrettyPrintBox box,
205                                 String font,
206                                 int fontSize,
207                                 Color color) {
208                         // throws InvalidArgumentsException
209 
210                         //  box = expr.prettyPrint(font,fontSize);
211                         // ToPrettyPrinter tpp = new ToPrettyPrinter(font, fontSize);
212                         this.box = box;
213                         size = box.getBoxMetrics();
214                         this.color = color;
215                 }
216 
217 
218                 /**
219                  *  Gets the iconWidth attribute of the HartMathPrettyPrinter object
220                  *
221                  *@return    The iconWidth value
222                  */
223                 public int getIconWidth() {
224                         return size.width;
225                 }
226 
227 
228                 /**
229                  *  Gets the iconHeight attribute of the HartMathPrettyPrinter object
230                  *
231                  *@return    The iconHeight value
232                  */
233                 public int getIconHeight() {
234                         return size.height;
235                 }
236 
237 
238                 /**
239                  *  paint the formula in pretty printer mode
240                  *
241                  *@param  c  Description of Parameter
242                  *@param  g  Description of Parameter
243                  *@param  x  Description of Parameter
244                  *@param  y  Description of Parameter
245                  */
246                 public void paintIcon(Component c, Graphics g, int x, int y) {
247                         g.setColor(color);
248                         if (box != null) {
249                                 box.paint(g, size, x, y);
250                         }
251                 }
252 
253         }
254 }