1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
|
/*****************************************************************************
* Copyright (c) 2010 CEA LIST.
*
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Remi Schnekenburger (CEA LIST) remi.schnekenburger@cea.fr - Initial API and implementation
*****************************************************************************/
package org.eclipse.papyrus.uml.diagram.common.figure.node;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Stack;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.Platform;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.LayoutListener;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.text.BlockFlow;
import org.eclipse.draw2d.text.FlowPage;
import org.eclipse.draw2d.text.TextFlow;
import org.eclipse.gmf.runtime.draw2d.ui.internal.figures.AnimatableScrollPane;
import org.eclipse.gmf.runtime.draw2d.ui.text.TextFlowEx;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.resource.FontDescriptor;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.papyrus.uml.diagram.common.Activator;
import org.eclipse.papyrus.uml.diagram.common.parser.HTMLCleaner;
import org.eclipse.papyrus.uml.diagram.common.preferences.RichtextPreferencePage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
/**
* Corner bend figure able to display styled text formatted in html
*/
public class HTMLCornerBentFigure extends CornerBentFigure implements ILabelFigure, IMultilineEditableFigure {
/**
* HTML renderer extension ID
*
* @since 2.0
*/
public static final String EXTENSION_ID = Activator.ID + ".htmlRendererContributor";
/** indicates if the figure should use local coordinates or not */
protected boolean useLocalCoordinates = false;
/** background color for this figure */
static final Color THIS_BACK = new Color(null, 248, 249, 214);
/** font used by default by this figure */
private static final FontData DEFAULT_FONT = new FontData("Arial", 8, SWT.NORMAL);
/** font used for sampleCode by this figure */
private static final FontData CODE_SAMPLE_FONT = new FontData("Lucida Console", 8, SWT.NORMAL);
/** font used for quote by this figure */
private static final FontData QUOTE_FONT = new FontData("Monotype Corsiva", 10, SWT.NORMAL);
/** key for the font style, corresponding to the type of font */
private static final String FONT_NAME = "face";
/** key for the font style, corresponding to the type of font */
private static final String FONT_SIZE = "size";
/** main flow page */
protected FlowPage page;
/** Content for html renderer content */
protected AnimatableScrollPane contentPane;
/**
* HTML renderer from extensions (if any)
*
* @since 2.0
*/
protected HTMLRenderer htmlRenderer;
/**
* Preference boolean
*/
protected Boolean useHtmlRenderer = null;
/**
* Body size to remember so layout changes are not fired infinitely
*
* @since 2.0
*/
protected int oldWidth = 0;
/**
* @since 2.0
*/
protected int oldHeight = 0;
/** text styles stack */
private final List<List<Styles>> myStyles = new ArrayList<List<Styles>>();
/** font used for the figure */
private FontData currentFontData;
private String text = ""; // Used as the default text (instead of null)
/**
* set of font datas used by this comment. It will be cleaned juste after
* the creation of the comment
*/
private Set<FontData> cachedFontDatas = new HashSet<FontData>();
/**
* Creates a new HTMLCornerBentFigure.
*/
public HTMLCornerBentFigure() {
super();
createContents();
}
/**
* return the label thath contains the icon.
*
* @return the label that contains the icon
*/
public Label getIconLabel() {
return this.iconLabel;
}
/**
* Generates the basic contents for this figure
*/
protected void createContents() {
if (!useHtmlRenderer()) {
page = new FlowPage();
page.setForegroundColor(getForegroundColor());
this.add(page);
} else {
if (htmlRenderer == null) {
IConfigurationElement[] config = Platform.getExtensionRegistry().getConfigurationElementsFor(EXTENSION_ID);
for (IConfigurationElement e : config) {
if (!"contributor".equals(e.getName())) {
continue;
}
try {
Object instance = e.createExecutableExtension("contributor");
if (instance instanceof HTMLRenderer) {
htmlRenderer = (HTMLRenderer) instance;
break;
}
} catch (Exception ex) {
Activator.log.warn("Invalid html renderer contribution from: " + e.getContributor());
}
}
}
IFigure htmlContent = null;
if (htmlRenderer != null) {
htmlContent = htmlRenderer.getFigure();
}
if (htmlContent != null) {
this.add(htmlContent);
addHTMLLayoutListener();
} else {
page = new FlowPage();
page.setForegroundColor(getForegroundColor());
this.add(page);
}
}
}
private HTMLLayoutListener htmlLayoutListener;
private class HTMLLayoutListener extends LayoutListener.Stub {
@Override
public void postLayout(IFigure container) {
if (oldWidth != container.getClientArea().width || oldHeight != container.getClientArea().height) {
oldWidth = container.getClientArea().width;
oldHeight = container.getClientArea().height;
paintHTML();
}
}
}
private void addHTMLLayoutListener() {
if (htmlLayoutListener == null) {
htmlLayoutListener = new HTMLLayoutListener();
HTMLCornerBentFigure.this.addLayoutListener(htmlLayoutListener);
}
}
private void paintHTML() {
if (htmlRenderer != null) {
try {
if (this.isVisible()) {
if (Display.getDefault() != null) {
int width = getClientArea().width;
int height = getClientArea().height;
if (width > 0 && height > 0) { // width and height can be nil before figures are displayed
htmlRenderer.paintHTML(text, width, height, 0, 0);
}
}
}
} catch (Exception e) {
Activator.log.error(e);
}
}
}
/**
* @see org.eclipse.draw2d.Figure#useLocalCoordinates()
*
* @return <code>true</code> if this Figure uses local coordinates
*/
@Override
protected boolean useLocalCoordinates() {
return useLocalCoordinates;
}
/**
* Returns the current instance of this class
*
* @return the current instance of this class
*/
public HTMLCornerBentFigure getCornerBentFigure() {
return this;
}
/**
* @see org.eclipse.papyrus.uml.diagram.common.figure.node.ILabelFigure#getIcon()
*
* @return
*/
@Override
public Image getIcon() {
return null;
}
/**
* @see org.eclipse.papyrus.uml.diagram.common.figure.node.ILabelFigure#getText()
*
* @return
*/
@Override
public String getText() {
return text;
}
/**
* @see org.eclipse.papyrus.uml.diagram.common.figure.node.ILabelFigure#setIcon(org.eclipse.swt.graphics.Image)
*
* @param icon
*/
@Override
public void setIcon(Image icon) {
// TODO Auto-generated method stub
}
/**
* @see org.eclipse.papyrus.uml.diagram.common.figure.node.ILabelFigure#setText(java.lang.String)
*
* @param text
*/
@Override
public void setText(String text) {
if (equals(text, this.text)) {
return;
}
this.text = text;
if (!useHtmlRenderer() || htmlRenderer == null) {
if (page != null) {
// remove all children from page.
page.removeAll();
// init the first font data
currentFontData = new FontData("Wingdings", 8, SWT.NORMAL);
// generates new ones
generateBlockForText(text, page);
}
} else {
paintHTML();
}
}
private static boolean equals(String s1, String s2) {
if (s1 == null && s2 == null) {
return true;
}
if (s1 != null) {
return s1.equals(s2);
} else {
return s2.equals(s1);
}
}
/**
* Generates block list for the given text, and adds it to the root flow
* page
*
* @param text
* the string to display
*/
protected void generateBlockForText(String text, FlowPage page) {
// parse the HMTL text and transforms it into a tree. "Body" tags
// enforce the character chain to be a valid xml chain
NodeList nodeList = generateNodeList("<body>" + text + "</body>");
myStyles.clear();
// generate blocks from this list and adds it to the flow page children
if (nodeList.getLength() > 0) {
generateBlocksFromNodeList(nodeList, page, new Stack<Styles>());
} else {
// problem during parsing
// return only one text flow with the content of the text
TextFlowEx textFlow = new TextFlowEx(text);
page.add(textFlow);
}
}
/**
* Builds the structure and content of block flows for a given list of nodes
*
* @param nodeList
* the list of nodes from which to generates the blockflows
*/
protected void generateBlocksFromNodeList(NodeList nodeList, BlockFlow parentFlow, Stack<Styles> parentStyles) {
// for each element in the list, generates the corresponding blocks
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
String nodeName = node.getNodeName();
short nodeType = node.getNodeType();
if (nodeType == Node.TEXT_NODE) {
String text = HTMLCleaner.cleanHTMLTags(node.getNodeValue());
if (text == null || text.trim().isEmpty()) {
continue;
}
List<Styles> styles = Collections.list(parentStyles.elements());
myStyles.add(styles);
generateTextFromTextNode(text, parentFlow, styles);
} else {
try {
switch (HTMLTags.valueOf(nodeName)) {
case body: // main tag for the comment body
// create a block for the body
generateBlocksFromBodyNode(node, parentFlow, parentStyles);
break;
case h3:
generateBlocksFromH3Node(node, parentFlow, parentStyles);
break;
case h4: // sub section heading
generateBlocksFromH4Node(node, parentFlow, parentStyles);
break;
case h5: // sub sub section heading
generateBlocksFromH5Node(node, parentFlow, parentStyles);
break;
case strong: // bold character
case b:
generateBlocksFromStrongNode(node, parentFlow, parentStyles);
break;
case em: // italic
generateBlocksFromItalicNode(node, parentFlow, parentStyles);
break;
case u: // underline
generateBlocksFromUnderlineNode(node, parentFlow, parentStyles);
break;
case sub: // subscript
break;
case sup: // superscript
break;
case blockquote: // indent left or right
break;
case table: // table
break;
case p: // paragraph
generateBlocksFromParagraphNode(node, parentFlow, parentStyles);
break;
case br:
generateBlocksFromBRNode(node, parentFlow);
break;
case font:
generateBlocksForFontNode(node, parentFlow, parentStyles);
break;
default:
break;
}
} catch (IllegalArgumentException ex) {
// Ignored. Unsupported HTML Tag.
}
}
}
}
/**
* Generates code from a node representing an underlined text.
*
* @param node
* the node from which to generate belowk flows
* @param parentFlow
* the parent block flow which will contain the block created
*/
protected void generateBlocksFromUnderlineNode(Node node, BlockFlow parentFlow, Stack<Styles> styles) {
NodeList childrenNodes = node.getChildNodes();
styles.push(Styles.underline);
generateBlocksFromNodeList(childrenNodes, parentFlow, styles);
styles.pop();
}
/**
* Overides Figure.setFont() method
* setup font for all TextFlow childes of this figure
*
*/
@Override
public void setFont(Font f) {
if (page != null) {
super.setFont(f);
List<TextFlow> textFlowList = findTextFlowChildList(page);
int i = 0;
for (TextFlow nextTextFlow : textFlowList) {
if (i == myStyles.size()) {
// make code more robust
break;
}
nextTextFlow.setFont(calculateCurrentFont(myStyles.get(i)));
i++;
}
}
}
/**
* @see HTMLCornerBentFigure.setFont() for using
*
* @param parent
* @return TextFlow childs list
*/
private List<TextFlow> findTextFlowChildList(IFigure parent) {
List<TextFlow> result = new ArrayList<TextFlow>();
for (Object nextFigure : parent.getChildren()) {
if (!(nextFigure instanceof TextFlow)) {
result.addAll(findTextFlowChildList((IFigure) nextFigure));
continue;
}
result.add((TextFlow) nextFigure);
}
return result;
}
/**
* Generates code from a node representing a text.
* IFigure default font value will setup if styles list is empty.
*
* @param node
* the node from which to generate belowk flows
* @param parentFlow
* the parent block flow which will contain the block created
*/
protected void generateTextFromTextNode(String text, BlockFlow parentFlow, List<Styles> styles) {
TextFlowEx textFlow = new TextFlowEx(text);
textFlow.setTextUnderline(getUnderLineFromStyles(styles));
textFlow.setFont(calculateCurrentFont(styles));
parentFlow.add(textFlow);
}
/**
* Calculates current font as default figure font + styles
* styles has overrides default font
* It's main method for getting font value by this figure
*
* @return
*/
private Font calculateCurrentFont(List<Styles> styles) {
return composeFontWithStyles(getDefaultFontData(), styles);
}
/**
* Extracts underLine value from styles
*
* @see generateTextFromTextNode() for using
* @return
*/
private boolean getUnderLineFromStyles(List<Styles> styles) {
boolean result = false;
for (Styles style : styles) {
switch (style) {
case underline:
result = true;
break;
default:
break;
}
}
return result;
}
/**
* Clculates default fontData without styles
*
* @return
*/
private FontData getDefaultFontData() {
boolean quote = false;
boolean codeSample = false;
if (codeSample) {
return CODE_SAMPLE_FONT;
}
if (quote) {
return QUOTE_FONT;
}
FontData fromDefaultFigure = getCurrentFigureFontData();
if (fromDefaultFigure == null) {
return DEFAULT_FONT;
}
return fromDefaultFigure;
}
/**
* Font -> FontData converter
*
* @return Current Figure FonData value
*/
private FontData getCurrentFigureFontData() {
if (getFont() == null) {
return null;
}
if (getFont().getFontData() == null || getFont().getFontData().length == 0) {
return null;
}
return getFont().getFontData()[0];
}
/**
* Compose figure font with styles
*
* @param defaultFontData
* @return
*/
private Font composeFontWithStyles(FontData defaultFontData, List<Styles> styles) {
if (styles == null || styles.isEmpty()) {
return (Font) JFaceResources.getResources().get(FontDescriptor.createFrom(defaultFontData));
}
FontData defaultFontDataCopy = FontDescriptor.copy(defaultFontData);
int defaultStyle = defaultFontDataCopy.getStyle();
boolean quote = false;
boolean codeSample = false;
// calculate the font to apply
for (Styles style : styles) {
switch (style) {
case italic:
defaultStyle = defaultStyle | SWT.ITALIC;
break;
case strong:
defaultStyle = defaultStyle | SWT.BOLD;
break;
case quote:
quote = true;
break;
case code:
codeSample = true;
break;
case font:
if (Styles.font.getData().get(FONT_NAME) != null) {
defaultFontDataCopy.setName((String) Styles.font.getData().get(FONT_NAME));
}
if (Styles.font.getData().get(FONT_SIZE) != null) {
// font size = [1..7] in html, but does not correspond to system
// size... 2 by default => 8 in real size.
// so: real size = (html font size)+6
defaultFontDataCopy.setHeight(((Integer) Styles.font.getData().get(FONT_SIZE)) * 2 + 4);
}
break;
default:
break;
}
}
defaultFontDataCopy.setStyle(defaultStyle);
if (codeSample) {
defaultFontDataCopy = CODE_SAMPLE_FONT;
} else if (quote) {
defaultFontDataCopy = QUOTE_FONT;
}
return (Font) JFaceResources.getResources().get(FontDescriptor.createFrom(defaultFontDataCopy));
}
/**
* Generates code from a node representing a bolded text.
*
* @param node
* the node from which to generate block flows
* @param parentFlow
* the parent block flow which will contain the block created
*/
protected void generateBlocksFromStrongNode(Node node, BlockFlow parentFlow, Stack<Styles> styles) {
NodeList childrenNodes = node.getChildNodes();
styles.push(Styles.strong);
generateBlocksFromNodeList(childrenNodes, parentFlow, styles);
styles.pop();
}
/**
* Generates code from a node representing an italic styled text.
*
* @param node
* the node from which to generate belowk flows
* @param parentFlow
* the parent block flow which will contain the block created
*/
protected void generateBlocksFromItalicNode(Node node, BlockFlow parentFlow, Stack<Styles> styles) {
NodeList childrenNodes = node.getChildNodes();
styles.push(Styles.italic);
generateBlocksFromNodeList(childrenNodes, parentFlow, styles);
styles.pop();
}
/**
* Generates code from a node with new Font.
*
* @param node
* the node from which to generate belowk flows
* @param parentFlow
* the parent block flow which will contain the block created
*/
protected void generateBlocksForFontNode(Node node, BlockFlow parentFlow, Stack<Styles> styles) {
// retrieves the font to apply
Node fontNameNode = node.getAttributes().getNamedItem("face");
Node fontSizeNode = node.getAttributes().getNamedItem("size");
String oldFont = "";
int oldSize = 8;
if (fontNameNode != null) {
String fontName = fontNameNode.getNodeValue();
oldFont = (Styles.font.getData().get(FONT_NAME) != null) ? (String) Styles.font.getData().get(FONT_NAME) : "Arial";
Styles.font.getData().put(FONT_NAME, fontName);
styles.push(Styles.font);
}
if (fontSizeNode != null) {
int fontSize = Integer.parseInt(fontSizeNode.getNodeValue());
oldSize = (Styles.font.getData().get(FONT_SIZE) != null) ? ((Integer) Styles.font.getData().get(FONT_SIZE)) : 2;
Styles.font.getData().put(FONT_SIZE, fontSize);
styles.push(Styles.font);
}
NodeList childrenNodes = node.getChildNodes();
generateBlocksFromNodeList(childrenNodes, parentFlow, styles);
if (fontNameNode != null) {
Styles.font.getData().put(FONT_NAME, oldFont);
styles.pop();
}
if (fontSizeNode != null) {
Styles.font.getData().put(FONT_SIZE, oldSize);
styles.pop();
}
}
/**
* Generates code from a node representing a H3 section (header section).
*
* @param node
* the node from which to generate belowk flows
* @param parentFlow
* the parent block flow which will contain the block created
*/
protected void generateBlocksFromH3Node(Node node, BlockFlow parentFlow, Stack<Styles> styles) {
BlockFlow blockFlow = new BlockFlow();
NodeList childrenNodes = node.getChildNodes();
styles.push(Styles.header3);
generateBlocksFromNodeList(childrenNodes, blockFlow, styles);
styles.pop();
parentFlow.add(blockFlow);
}
/**
* Generates code from a node representing a H3 section (header section).
*
* @param node
* the node from which to generate belowk flows
* @param parentFlow
* the parent block flow which will contain the block created
*/
protected void generateBlocksFromH4Node(Node node, BlockFlow parentFlow, Stack<Styles> styles) {
BlockFlow blockFlow = new BlockFlow();
NodeList childrenNodes = node.getChildNodes();
styles.push(Styles.header4);
generateBlocksFromNodeList(childrenNodes, blockFlow, styles);
styles.pop();
parentFlow.add(blockFlow);
}
/**
* Generates code from a node representing a H3 section (header section).
*
* @param node
* the node from which to generate belowk flows
* @param parentFlow
* the parent block flow which will contain the block created
*/
protected void generateBlocksFromH5Node(Node node, BlockFlow parentFlow, Stack<Styles> styles) {
BlockFlow blockFlow = new BlockFlow();
NodeList childrenNodes = node.getChildNodes();
styles.push(Styles.header5);
generateBlocksFromNodeList(childrenNodes, blockFlow, styles);
styles.pop();
parentFlow.add(blockFlow);
}
/**
* Pretty prints the list of child nodes
*
* @param childNodes
*/
@SuppressWarnings("unused")
private void debug(NodeList childNodes) {
for (int i = 0; i < childNodes.getLength(); i++) {
System.err.println("[" + i + "] " + childNodes.item(i).getNodeName());
}
}
/**
* Generates code from a node representing a body.
*
* @param node
* the node from which to generate block flows
* @param parentFlow
* the parent block flow which will contain the block created
*/
protected void generateBlocksFromBodyNode(Node node, BlockFlow parentFlow, Stack<Styles> styles) {
BlockFlow blockFlow = new BlockFlow();
NodeList childrenNodes = node.getChildNodes();
generateBlocksFromNodeList(childrenNodes, blockFlow, styles);
parentFlow.add(blockFlow);
}
/**
* Generates code from a node representing a paragraph.
*
* @param node
* the node from which to generate block flows
* @param parentFlow
* the parent block flow which will contain the block created
*/
protected void generateBlocksFromParagraphNode(Node node, BlockFlow parentFlow, Stack<Styles> styles) {
BlockFlow blockFlow = new BlockFlow();
// perhaps a style is associated to the paragraph (class="code sample"
// for example)
NamedNodeMap attributes = node.getAttributes();
Node classNode = attributes.getNamedItem("class");
boolean hasToPop = false;
if (classNode != null) {
String classNodeValue = classNode.getNodeValue();
if ("codeSample".equals(classNodeValue)) {
hasToPop = true;
styles.push(Styles.code);
} else if ("quote".equals(classNodeValue)) {
styles.push(Styles.quote);
hasToPop = true;
}
}
NodeList childrenNodes = node.getChildNodes();
generateBlocksFromNodeList(childrenNodes, blockFlow, styles);
if (hasToPop) {
styles.pop();
}
parentFlow.add(blockFlow);
}
/**
* Generates code from a node representing a carraige return.
*
* @param node
* the node from which to generate block flows
* @param parentFlow
* the parent block flow which will contain the block created
*/
protected void generateBlocksFromBRNode(Node node, BlockFlow parentFlow) {
BlockFlow blockFlow = new BlockFlow();
parentFlow.add(blockFlow);
}
/**
* Generates a list of nodes from the parse of an html text
*
* @param text
* the text to parse
* @return the parsed text under the form of a list of nodes
*/
protected NodeList generateNodeList(String text) {
return HTMLCommentParser.parse(text);
}
/**
* Valid HTML tags enumeration
*/
protected enum HTMLTags {
body(""), // main tag for the comment body
h3(""), // section heading
h4(""), // sub section heading
h5(""), // sub sub section heading
strong(""), // bold character
b(""), // bold character
em(""), // italic
u(""), // underline
sub(""), // subscript
sup(""), // superscript
blockquote(""), // indent left or right
table(""), // table
p(""), // paragraph
br(""), // new line
font(""); // specific font
/** additional data for this enum */
protected String data;
HTMLTags(String data) {
this.data = data;
}
/**
* Sets the data for this enum
*
* @param data
* the data to set
*/
public void setData(String data) {
this.data = data;
}
/**
* Returns the data associated to this enum
*
* @return the data associated to this enum
*/
public String getData() {
return data;
}
}
/**
* Styles to apply to the text
*/
protected enum Styles {
strong, header3, header4, header5, underline, italic, code, subscript, supscript, quote, font(new HashMap<String, Object>());
/** additional data */
private Map<String, Object> data;
Styles() {
this.data = null;
}
Styles(Map<String, Object> data) {
this.data = data;
}
/**
* sets the data associated to this enum
*
* @param data
* the data to set
*/
public void setData(Map<String, Object> data) {
this.data = data;
}
/**
* Returns the data for this enum
*
* @return the data for this enum
*/
public Map<String, Object> getData() {
return data;
}
}
/**
*
* @see org.eclipse.papyrus.uml.diagram.common.figure.node.IMultilineEditableFigure#getEditionLocation()
*
*/
@Override
public Point getEditionLocation() {
return getBounds().getTopLeft();
}
private boolean useHtmlRenderer() {
if (useHtmlRenderer == null) {
useHtmlRenderer = false;
IPreferenceStore store = Activator.getDefault().getPreferenceStore();
if (store != null) {
useHtmlRenderer = store.getBoolean(RichtextPreferencePage.USE_HTML_RENDERER);
}
}
return useHtmlRenderer;
}
}
|