1 11 package org.eclipse.ui.internal.forms.widgets; 12 13 import java.io.*; 14 import java.util.ArrayList ; 15 import java.util.Hashtable ; 16 import java.util.Vector ; 17 18 import org.eclipse.swt.graphics.*; 19 import org.eclipse.swt.graphics.GC; 20 import org.eclipse.ui.forms.HyperlinkSettings; 21 22 26 public class Paragraph { 27 public static final String HTTP = "http://"; 29 private Vector segments; 30 31 private boolean addVerticalSpace = true; 32 33 public Paragraph(boolean addVerticalSpace) { 34 this.addVerticalSpace = addVerticalSpace; 35 } 36 37 public int getIndent() { 38 return 0; 39 } 40 41 public boolean getAddVerticalSpace() { 42 return addVerticalSpace; 43 } 44 45 48 public ParagraphSegment[] getSegments() { 49 if (segments == null) 50 return new ParagraphSegment[0]; 51 return (ParagraphSegment[]) segments 52 .toArray(new ParagraphSegment[segments.size()]); 53 } 54 55 public void addSegment(ParagraphSegment segment) { 56 if (segments == null) 57 segments = new Vector (); 58 segments.add(segment); 59 } 60 61 public void parseRegularText(String text, boolean expandURLs, boolean wrapAllowed, 62 HyperlinkSettings settings, String fontId) { 63 parseRegularText(text, expandURLs, wrapAllowed, settings, fontId, null); 64 } 65 66 public void parseRegularText(String text, boolean expandURLs, boolean wrapAllowed, 67 HyperlinkSettings settings, String fontId, String colorId) { 68 if (text.length() == 0) 69 return; 70 if (expandURLs) { 71 int loc = text.indexOf(HTTP); 72 73 if (loc == -1) 74 addSegment(new TextSegment(text, fontId, colorId, wrapAllowed)); 75 else { 76 int textLoc = 0; 77 while (loc != -1) { 78 addSegment(new TextSegment(text.substring(textLoc, loc), 79 fontId, colorId, wrapAllowed)); 80 boolean added = false; 81 for (textLoc = loc; textLoc < text.length(); textLoc++) { 82 char c = text.charAt(textLoc); 83 if (Character.isSpaceChar(c)) { 84 addHyperlinkSegment(text.substring(loc, textLoc), 85 settings, fontId); 86 added = true; 87 break; 88 } 89 } 90 if (!added) { 91 addHyperlinkSegment(text.substring(loc), settings, 93 fontId); 94 break; 95 } 96 loc = text.indexOf(HTTP, textLoc); 97 } 98 if (textLoc < text.length()) { 99 addSegment(new TextSegment(text.substring(textLoc), fontId, 100 colorId, wrapAllowed)); 101 } 102 } 103 } else { 104 addSegment(new TextSegment(text, fontId, colorId, wrapAllowed)); 105 } 106 } 107 108 private void addHyperlinkSegment(String text, HyperlinkSettings settings, 109 String fontId) { 110 TextHyperlinkSegment hs = new TextHyperlinkSegment(text, settings, 111 fontId); 112 hs.setWordWrapAllowed(false); 113 hs.setHref(text); 114 addSegment(hs); 115 } 116 117 protected void computeRowHeights(GC gc, int width, Locator loc, 118 int lineHeight, Hashtable resourceTable) { 119 ParagraphSegment[] segments = getSegments(); 120 Locator hloc = loc.create(); 122 ArrayList heights = new ArrayList (); 123 hloc.heights = heights; 124 hloc.rowCounter = 0; 125 int innerWidth = width - loc.marginWidth*2; 126 for (int j = 0; j < segments.length; j++) { 127 ParagraphSegment segment = segments[j]; 128 segment.advanceLocator(gc, innerWidth, hloc, resourceTable, true); 129 } 130 hloc.collectHeights(); 131 loc.heights = heights; 132 loc.rowCounter = 0; 133 } 134 135 public void layout(GC gc, int width, Locator loc, int lineHeight, 136 Hashtable resourceTable, IHyperlinkSegment selectedLink) { 137 ParagraphSegment[] segments = getSegments(); 138 if (segments.length > 0) { 140 145 if (loc.heights == null) 147 computeRowHeights(gc, width, loc, lineHeight, resourceTable); 148 for (int j = 0; j < segments.length; j++) { 149 ParagraphSegment segment = segments[j]; 150 boolean doSelect = false; 151 if (selectedLink != null && segment.equals(selectedLink)) 152 doSelect = true; 153 segment.layout(gc, width, loc, resourceTable, doSelect); 154 } 155 loc.heights = null; 156 loc.y += loc.rowHeight; 157 } else { 158 loc.y += lineHeight; 159 } 160 } 161 162 public void paint(GC gc, Rectangle repaintRegion, 163 Hashtable resourceTable, IHyperlinkSegment selectedLink, 164 SelectionData selData) { 165 ParagraphSegment[] segments = getSegments(); 166 167 for (int i = 0; i < segments.length; i++) { 168 ParagraphSegment segment = segments[i]; 169 if (!segment.intersects(repaintRegion)) 170 continue; 171 boolean doSelect = false; 172 if (selectedLink != null && segment.equals(selectedLink)) 173 doSelect = true; 174 segment.paint(gc, false, resourceTable, doSelect, selData, repaintRegion); 175 } 176 } 177 178 public void computeSelection(GC gc, Hashtable resourceTable, IHyperlinkSegment selectedLink, 179 SelectionData selData) { 180 ParagraphSegment[] segments = getSegments(); 181 182 for (int i = 0; i < segments.length; i++) { 183 ParagraphSegment segment = segments[i]; 184 segment.computeSelection(gc, resourceTable, selData); 188 } 189 } 190 191 public String getAccessibleText() { 192 ParagraphSegment[] segments = getSegments(); 193 StringWriter swriter = new StringWriter(); 194 PrintWriter writer = new PrintWriter(swriter); 195 for (int i = 0; i < segments.length; i++) { 196 ParagraphSegment segment = segments[i]; 197 if (segment instanceof TextSegment) { 198 String text = ((TextSegment) segment).getText(); 199 writer.print(text); 200 } 201 } 202 writer.println(); 203 swriter.flush(); 204 return swriter.toString(); 205 } 206 207 public ParagraphSegment findSegmentAt(int x, int y) { 208 if (segments != null) { 209 for (int i = 0; i < segments.size(); i++) { 210 ParagraphSegment segment = (ParagraphSegment) segments.get(i); 211 if (segment.contains(x, y)) 212 return segment; 213 } 214 } 215 return null; 216 } 217 public void clearCache(String fontId) { 218 if (segments != null) { 219 for (int i = 0; i < segments.size(); i++) { 220 ParagraphSegment segment = (ParagraphSegment) segments.get(i); 221 segment.clearCache(fontId); 222 } 223 } 224 } 225 } 226 | Popular Tags |