| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
package net.sourceforge.jeuclid.swing; |
| 20 | |
|
| 21 | |
import java.awt.Color; |
| 22 | |
import java.awt.Font; |
| 23 | |
import java.io.IOException; |
| 24 | |
import java.util.Arrays; |
| 25 | |
import java.util.Collections; |
| 26 | |
import java.util.List; |
| 27 | |
import java.util.Map; |
| 28 | |
|
| 29 | |
import javax.swing.JComponent; |
| 30 | |
import javax.swing.SwingConstants; |
| 31 | |
import javax.swing.UIManager; |
| 32 | |
import javax.xml.parsers.ParserConfigurationException; |
| 33 | |
|
| 34 | |
import net.sourceforge.jeuclid.DOMBuilder; |
| 35 | |
import net.sourceforge.jeuclid.MathMLParserSupport; |
| 36 | |
import net.sourceforge.jeuclid.MathMLSerializer; |
| 37 | |
import net.sourceforge.jeuclid.MutableLayoutContext; |
| 38 | |
import net.sourceforge.jeuclid.context.LayoutContextImpl; |
| 39 | |
import net.sourceforge.jeuclid.context.Parameter; |
| 40 | |
import net.sourceforge.jeuclid.elements.generic.DocumentElement; |
| 41 | |
import net.sourceforge.jeuclid.elements.support.ClassLoaderSupport; |
| 42 | |
|
| 43 | |
import org.apache.commons.logging.Log; |
| 44 | |
import org.apache.commons.logging.LogFactory; |
| 45 | |
import org.w3c.dom.Document; |
| 46 | |
import org.w3c.dom.Node; |
| 47 | |
import org.xml.sax.SAXException; |
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
public final class JMathComponent extends JComponent implements |
| 70 | |
SwingConstants { |
| 71 | |
|
| 72 | |
private static final String FONT_SEPARATOR = ","; |
| 73 | |
|
| 74 | |
|
| 75 | 0 | |
| 76 | 0 | |
| 77 | 4 | private static final Log LOGGER = LogFactory.getLog(JMathComponent.class); |
| 78 | |
|
| 79 | |
|
| 80 | |
private static final long serialVersionUID = 1L; |
| 81 | |
|
| 82 | |
private static String uiClassId; |
| 83 | |
|
| 84 | |
private static Class<?> mathComponentUIClass; |
| 85 | |
|
| 86 | |
private Node document; |
| 87 | 0 | |
| 88 | 4 | private int horizontalAlignment = SwingConstants.CENTER; |
| 89 | 0 | |
| 90 | 4 | private final MutableLayoutContext parameters = new LayoutContextImpl( |
| 91 | |
LayoutContextImpl.getDefaultLayoutContext()); |
| 92 | 0 | |
| 93 | 4 | private int verticalAlignment = SwingConstants.CENTER; |
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
private final CursorListener cursorListener; |
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | 0 | public JMathComponent() { |
| 104 | 4 | this(null); |
| 105 | 4 | } |
| 106 | |
|
| 107 | |
|
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | 0 | |
| 113 | 4 | public JMathComponent(final CursorListener listener) { |
| 114 | 4 | this.cursorListener = listener; |
| 115 | 0 | |
| 116 | 4 | final JMathComponentMouseListener mouseListener = new JMathComponentMouseListener( |
| 117 | 0 | this); |
| 118 | 4 | this.addMouseListener(mouseListener); |
| 119 | 0 | |
| 120 | 4 | this.updateUI(); |
| 121 | 4 | this.fontCompat(); |
| 122 | 4 | this.setDocument(new DocumentElement()); |
| 123 | 4 | } |
| 124 | |
|
| 125 | |
|
| 126 | |
|
| 127 | |
|
| 128 | |
|
| 129 | |
|
| 130 | 0 | public CursorListener getCursorListener() { |
| 131 | 0 | return this.cursorListener; |
| 132 | |
} |
| 133 | |
|
| 134 | |
|
| 135 | |
|
| 136 | |
|
| 137 | 0 | private void fontCompat() { |
| 138 | 4 | final String fontName = this.getFontsSerif().split( |
| 139 | 0 | JMathComponent.FONT_SEPARATOR)[0]; |
| 140 | 4 | final float fontSize = this.getFontSize(); |
| 141 | 4 | super.setFont(new Font(fontName, 0, (int) fontSize)); |
| 142 | 4 | } |
| 143 | |
|
| 144 | |
|
| 145 | |
|
| 146 | |
|
| 147 | |
|
| 148 | |
|
| 149 | |
|
| 150 | 0 | |
| 151 | |
|
| 152 | |
|
| 153 | 0 | public String getContent() { |
| 154 | 0 | return MathMLSerializer.serializeDocument(this.getDocument(), false, |
| 155 | |
false); |
| 156 | |
} |
| 157 | |
|
| 158 | |
|
| 159 | |
|
| 160 | |
|
| 161 | 0 | public Node getDocument() { |
| 162 | 12 | return this.document; |
| 163 | 0 | } |
| 164 | |
|
| 165 | 0 | private static String join(final List<String> list) { |
| 166 | 4 | boolean first = true; |
| 167 | 4 | final StringBuilder b = new StringBuilder(); |
| 168 | 4 | for (final String s : list) { |
| 169 | 44 | if (first) { |
| 170 | 4 | first = false; |
| 171 | 0 | } else { |
| 172 | 40 | b.append(JMathComponent.FONT_SEPARATOR); |
| 173 | 0 | } |
| 174 | 44 | b.append(s); |
| 175 | 0 | } |
| 176 | 4 | return b.toString(); |
| 177 | 0 | } |
| 178 | 0 | |
| 179 | 0 | |
| 180 | |
|
| 181 | 0 | |
| 182 | |
|
| 183 | 0 | |
| 184 | |
|
| 185 | 0 | |
| 186 | |
|
| 187 | |
@SuppressWarnings("unchecked") |
| 188 | 0 | public String getFontsDoublestruck() { |
| 189 | 0 | return JMathComponent.join((List<String>) this.parameters |
| 190 | |
.getParameter(Parameter.FONTS_DOUBLESTRUCK)); |
| 191 | |
} |
| 192 | |
|
| 193 | |
|
| 194 | |
|
| 195 | |
|
| 196 | |
|
| 197 | 0 | |
| 198 | |
|
| 199 | |
|
| 200 | |
@SuppressWarnings("unchecked") |
| 201 | 0 | public String getFontsFraktur() { |
| 202 | 0 | return JMathComponent.join((List<String>) this.parameters |
| 203 | |
.getParameter(Parameter.FONTS_FRAKTUR)); |
| 204 | |
} |
| 205 | |
|
| 206 | |
|
| 207 | |
|
| 208 | |
|
| 209 | 0 | public float getFontSize() { |
| 210 | 4 | return (Float) this.parameters.getParameter(Parameter.MATHSIZE); |
| 211 | |
} |
| 212 | |
|
| 213 | |
|
| 214 | |
|
| 215 | |
|
| 216 | |
|
| 217 | |
|
| 218 | 0 | |
| 219 | |
|
| 220 | |
@SuppressWarnings("unchecked") |
| 221 | 0 | public String getFontsMonospaced() { |
| 222 | 0 | return JMathComponent.join((List<String>) this.parameters |
| 223 | |
.getParameter(Parameter.FONTS_MONOSPACED)); |
| 224 | |
} |
| 225 | |
|
| 226 | |
|
| 227 | |
|
| 228 | |
|
| 229 | |
|
| 230 | 0 | |
| 231 | |
|
| 232 | |
|
| 233 | |
@SuppressWarnings("unchecked") |
| 234 | 0 | public String getFontsSanserif() { |
| 235 | 0 | return JMathComponent.join((List<String>) this.parameters |
| 236 | |
.getParameter(Parameter.FONTS_SANSSERIF)); |
| 237 | |
} |
| 238 | |
|
| 239 | |
|
| 240 | |
|
| 241 | |
|
| 242 | |
|
| 243 | 0 | |
| 244 | |
|
| 245 | |
|
| 246 | |
@SuppressWarnings("unchecked") |
| 247 | 0 | public String getFontsScript() { |
| 248 | 0 | return JMathComponent.join((List<String>) this.parameters |
| 249 | |
.getParameter(Parameter.FONTS_SCRIPT)); |
| 250 | |
} |
| 251 | |
|
| 252 | |
|
| 253 | |
|
| 254 | |
|
| 255 | |
|
| 256 | 0 | |
| 257 | |
|
| 258 | |
|
| 259 | |
@SuppressWarnings("unchecked") |
| 260 | 0 | public String getFontsSerif() { |
| 261 | 4 | return JMathComponent.join((List<String>) this.parameters |
| 262 | |
.getParameter(Parameter.FONTS_SERIF)); |
| 263 | |
} |
| 264 | |
|
| 265 | |
|
| 266 | |
@Override |
| 267 | 0 | public Color getForeground() { |
| 268 | 0 | return (Color) this.parameters.getParameter(Parameter.MATHCOLOR); |
| 269 | 0 | } |
| 270 | |
|
| 271 | |
|
| 272 | |
|
| 273 | |
|
| 274 | |
|
| 275 | |
|
| 276 | 0 | |
| 277 | |
|
| 278 | |
|
| 279 | |
|
| 280 | |
|
| 281 | |
|
| 282 | 0 | public int getHorizontalAlignment() { |
| 283 | 0 | return this.horizontalAlignment; |
| 284 | |
} |
| 285 | |
|
| 286 | |
|
| 287 | |
|
| 288 | |
|
| 289 | |
public MathComponentUI getUI() { |
| 290 | 0 | return (MathComponentUI) this.ui; |
| 291 | 0 | } |
| 292 | 0 | |
| 293 | |
|
| 294 | |
|
| 295 | |
|
| 296 | |
@Override |
| 297 | |
public String getUIClassID() { |
| 298 | 4 | return JMathComponent.uiClassId; |
| 299 | 0 | } |
| 300 | |
|
| 301 | 0 | |
| 302 | |
|
| 303 | |
|
| 304 | |
|
| 305 | |
|
| 306 | |
|
| 307 | 0 | |
| 308 | 0 | |
| 309 | |
|
| 310 | |
|
| 311 | |
public int getVerticalAlignment() { |
| 312 | 0 | return this.verticalAlignment; |
| 313 | |
} |
| 314 | |
|
| 315 | |
private void reval() { |
| 316 | 4 | this.repaint(); |
| 317 | 4 | this.revalidate(); |
| 318 | 4 | } |
| 319 | |
|
| 320 | |
|
| 321 | 0 | @Override |
| 322 | |
public void setBackground(final Color c) { |
| 323 | 0 | super.setBackground(c); |
| 324 | 0 | this.reval(); |
| 325 | 0 | } |
| 326 | 0 | |
| 327 | 0 | |
| 328 | |
|
| 329 | |
|
| 330 | 0 | |
| 331 | |
|
| 332 | 0 | |
| 333 | 0 | public void setContent(final String contentString) { |
| 334 | 0 | try { |
| 335 | 4 | final Document stdDomNode = MathMLParserSupport.parseString(contentString); |
| 336 | 4 | final DocumentElement jEuclidDom = DOMBuilder.getInstance().createJeuclidDom(stdDomNode, |
| 337 | |
true, true); |
| 338 | 4 | this.setDocument(jEuclidDom); |
| 339 | 0 | } catch (final SAXException e) { |
| 340 | 0 | throw new IllegalArgumentException(e); |
| 341 | 0 | } catch (final ParserConfigurationException e) { |
| 342 | 0 | throw new IllegalArgumentException(e); |
| 343 | 0 | } catch (final IOException e) { |
| 344 | 0 | throw new IllegalArgumentException(e); |
| 345 | 4 | } |
| 346 | 0 | |
| 347 | 4 | } |
| 348 | 0 | |
| 349 | 0 | |
| 350 | 0 | |
| 351 | 0 | |
| 352 | 0 | |
| 353 | 0 | |
| 354 | |
|
| 355 | 0 | public void setDebug(final boolean dbg) { |
| 356 | 0 | this.setParameter(Parameter.DEBUG, dbg); |
| 357 | 0 | } |
| 358 | 0 | |
| 359 | |
|
| 360 | 0 | |
| 361 | |
|
| 362 | 0 | |
| 363 | 0 | public void setDocument(final Node doc) { |
| 364 | 8 | final Node oldValue = this.document; |
| 365 | 8 | this.firePropertyChange("document", oldValue, doc); |
| 366 | 8 | this.document = doc; |
| 367 | 8 | if (doc != oldValue) { |
| 368 | 8 | this.revalidate(); |
| 369 | 8 | this.repaint(); |
| 370 | 0 | } |
| 371 | 8 | } |
| 372 | 0 | |
| 373 | 0 | |
| 374 | 0 | |
| 375 | 0 | |
| 376 | 0 | |
| 377 | 0 | |
| 378 | 0 | |
| 379 | |
|
| 380 | 0 | |
| 381 | |
|
| 382 | 0 | |
| 383 | 0 | |
| 384 | 0 | |
| 385 | 0 | |
| 386 | |
|
| 387 | 0 | @Deprecated |
| 388 | |
@Override |
| 389 | 0 | public void setFont(final Font f) { |
| 390 | 0 | super.setFont(f); |
| 391 | 0 | this.setFontSize(f.getSize2D()); |
| 392 | 0 | this.setFontsSerif(f.getFamily() + JMathComponent.FONT_SEPARATOR |
| 393 | |
+ this.getFontsSerif()); |
| 394 | 0 | } |
| 395 | 0 | |
| 396 | 0 | private List<String> splitFonts(final String list) { |
| 397 | 0 | return Arrays.asList(list.split(JMathComponent.FONT_SEPARATOR)); |
| 398 | 0 | } |
| 399 | 0 | |
| 400 | 0 | |
| 401 | |
|
| 402 | |
|
| 403 | 0 | |
| 404 | |
|
| 405 | |
|
| 406 | |
|
| 407 | |
|
| 408 | |
|
| 409 | |
public void setFontsDoublestruck(final String newFonts) { |
| 410 | 0 | this.setParameter(Parameter.FONTS_DOUBLESTRUCK, this |
| 411 | |
.splitFonts(newFonts)); |
| 412 | 0 | } |
| 413 | 0 | |
| 414 | |
|
| 415 | |
|
| 416 | 0 | |
| 417 | |
|
| 418 | 0 | |
| 419 | |
|
| 420 | 0 | |
| 421 | 0 | |
| 422 | 0 | public void setFontsFraktur(final String newFonts) { |
| 423 | 0 | this.setParameter(Parameter.FONTS_FRAKTUR, this.splitFonts(newFonts)); |
| 424 | 0 | } |
| 425 | 0 | |
| 426 | |
|
| 427 | 0 | |
| 428 | |
|
| 429 | 0 | |
| 430 | 0 | |
| 431 | |
|
| 432 | |
|
| 433 | |
|
| 434 | |
public void setParameter(final Parameter key, final Object newValue) { |
| 435 | 0 | this.setParameters(Collections.singletonMap(key, newValue)); |
| 436 | 0 | } |
| 437 | |
|
| 438 | |
|
| 439 | |
|
| 440 | |
|
| 441 | 0 | |
| 442 | 0 | |
| 443 | |
|
| 444 | |
public void setParameters(final Map<Parameter, Object> newValues) { |
| 445 | 0 | for (final Map.Entry<Parameter, Object> entry : newValues.entrySet()) { |
| 446 | 0 | final Parameter key = entry.getKey(); |
| 447 | 0 | final Object oldValue = this.parameters.getParameter(key); |
| 448 | 0 | this.parameters.setParameter(key, entry.getValue()); |
| 449 | 0 | this.firePropertyChange(key.name(), oldValue, this.parameters |
| 450 | 0 | .getParameter(key)); |
| 451 | 0 | } |
| 452 | 0 | this.revalidate(); |
| 453 | 0 | this.repaint(); |
| 454 | 0 | } |
| 455 | 0 | |
| 456 | |
|
| 457 | 0 | |
| 458 | 0 | |
| 459 | 0 | |
| 460 | 0 | |
| 461 | |
|
| 462 | |
public void setFontSize(final float fontSize) { |
| 463 | 0 | this.setParameter(Parameter.MATHSIZE, fontSize); |
| 464 | 0 | } |
| 465 | |
|
| 466 | 0 | |
| 467 | |
|
| 468 | 0 | |
| 469 | 0 | |
| 470 | 0 | |
| 471 | |
|
| 472 | |
|
| 473 | |
|
| 474 | |
public void setFontsMonospaced(final String newFonts) { |
| 475 | 0 | this.setParameter(Parameter.FONTS_MONOSPACED, this |
| 476 | |
.splitFonts(newFonts)); |
| 477 | 0 | } |
| 478 | |
|
| 479 | 0 | |
| 480 | 0 | |
| 481 | 0 | |
| 482 | |
|
| 483 | 0 | |
| 484 | |
|
| 485 | |
|
| 486 | |
|
| 487 | |
public void setFontsSanserif(final String newFonts) { |
| 488 | 0 | this.setParameter(Parameter.FONTS_SANSSERIF, this |
| 489 | |
.splitFonts(newFonts)); |
| 490 | 0 | } |
| 491 | 0 | |
| 492 | 0 | |
| 493 | |
|
| 494 | 0 | |
| 495 | |
|
| 496 | 0 | |
| 497 | |
|
| 498 | |
|
| 499 | |
|
| 500 | |
public void setFontsScript(final String newFonts) { |
| 501 | 0 | this.setParameter(Parameter.FONTS_SCRIPT, this.splitFonts(newFonts)); |
| 502 | 0 | } |
| 503 | 0 | |
| 504 | 0 | |
| 505 | 0 | |
| 506 | |
|
| 507 | 0 | |
| 508 | 0 | |
| 509 | 0 | |
| 510 | 0 | |
| 511 | |
|
| 512 | |
public void setFontsSerif(final String newFonts) { |
| 513 | 0 | this.setParameter(Parameter.FONTS_SERIF, this.splitFonts(newFonts)); |
| 514 | 0 | this.fontCompat(); |
| 515 | 0 | } |
| 516 | |
|
| 517 | |
|
| 518 | |
@Override |
| 519 | 0 | public void setForeground(final Color fg) { |
| 520 | 0 | super.setForeground(fg); |
| 521 | 0 | this.setParameter(Parameter.MATHCOLOR, fg); |
| 522 | 0 | } |
| 523 | |
|
| 524 | |
|
| 525 | |
|
| 526 | 0 | |
| 527 | 0 | |
| 528 | 0 | |
| 529 | |
|
| 530 | |
|
| 531 | 0 | |
| 532 | |
|
| 533 | |
|
| 534 | 0 | |
| 535 | |
|
| 536 | |
public void setHorizontalAlignment(final int hAlignment) { |
| 537 | 0 | this.horizontalAlignment = hAlignment; |
| 538 | 0 | } |
| 539 | |
|
| 540 | |
|
| 541 | |
@Override |
| 542 | |
public void setOpaque(final boolean opaque) { |
| 543 | 4 | super.setOpaque(opaque); |
| 544 | 4 | this.reval(); |
| 545 | 4 | } |
| 546 | 0 | |
| 547 | |
|
| 548 | |
|
| 549 | 0 | |
| 550 | 0 | |
| 551 | 0 | |
| 552 | |
|
| 553 | |
|
| 554 | |
|
| 555 | |
|
| 556 | |
|
| 557 | 0 | |
| 558 | 0 | public void setVerticalAlignment(final int vAlignment) { |
| 559 | 0 | this.verticalAlignment = vAlignment; |
| 560 | 0 | } |
| 561 | |
|
| 562 | |
|
| 563 | |
@Override |
| 564 | |
public void updateUI() { |
| 565 | 4 | if (UIManager.get(this.getUIClassID()) == null) { |
| 566 | 0 | try { |
| 567 | 4 | this |
| 568 | |
.setUI((MathComponentUI) JMathComponent.mathComponentUIClass |
| 569 | 0 | .newInstance()); |
| 570 | 0 | } catch (final InstantiationException e) { |
| 571 | 0 | JMathComponent.LOGGER.warn(e.getMessage()); |
| 572 | 0 | } catch (final IllegalAccessException e) { |
| 573 | 0 | JMathComponent.LOGGER.warn(e.getMessage()); |
| 574 | 4 | } |
| 575 | |
} else { |
| 576 | 0 | this.setUI(UIManager.getUI(this)); |
| 577 | 0 | } |
| 578 | 4 | } |
| 579 | 0 | |
| 580 | 0 | |
| 581 | |
|
| 582 | 0 | |
| 583 | |
public MutableLayoutContext getParameters() { |
| 584 | 8 | return this.parameters; |
| 585 | |
} |
| 586 | |
|
| 587 | |
|
| 588 | |
@Override |
| 589 | |
public void setSize(final int width, final int height) { |
| 590 | 0 | |
| 591 | 0 | super.setSize(width, height); |
| 592 | 0 | } |
| 593 | 0 | |
| 594 | 0 | static { |
| 595 | |
Class<?> uiClass; |
| 596 | |
String id; |
| 597 | 0 | try { |
| 598 | 4 | uiClass = ClassLoaderSupport.getInstance().loadClass( |
| 599 | 0 | "net.sourceforge.jeuclid.swing.MathComponentUI16"); |
| 600 | 0 | id = "MathComponentUI16"; |
| 601 | 4 | } catch (final ClassNotFoundException t) { |
| 602 | 4 | uiClass = MathComponentUI.class; |
| 603 | 4 | id = "MathComponentUI"; |
| 604 | 0 | } |
| 605 | 4 | JMathComponent.uiClassId = id; |
| 606 | 4 | JMathComponent.mathComponentUIClass = uiClass; |
| 607 | 4 | } |
| 608 | 0 | |
| 609 | 0 | } |