1 11 package org.eclipse.jdt.internal.ui.text; 12 13 import org.eclipse.swt.custom.StyledText; 14 import org.eclipse.swt.graphics.Font; 15 import org.eclipse.swt.graphics.GC; 16 import org.eclipse.swt.graphics.Point; 17 import org.eclipse.swt.widgets.Composite; 18 import org.eclipse.swt.widgets.Control; 19 import org.eclipse.swt.widgets.Shell; 20 21 import org.eclipse.jface.resource.JFaceResources; 22 23 import org.eclipse.jface.text.Assert; 24 import org.eclipse.jface.text.BadLocationException; 25 import org.eclipse.jface.text.IDocument; 26 import org.eclipse.jface.text.IRegion; 27 import org.eclipse.jface.text.ITextViewerExtension; 28 29 import org.eclipse.jdt.ui.text.IJavaPartitions; 30 31 import org.eclipse.jdt.internal.ui.text.java.hover.SourceViewerInformationControl; 32 33 38 public class CustomSourceInformationControl extends SourceViewerInformationControl { 39 40 41 private static final String SYMBOLIC_FONT_NAME= "org.eclipse.jdt.ui.editors.textfont"; 43 44 int fMaxWidth= Integer.MAX_VALUE; 45 46 int fMaxHeight= Integer.MAX_VALUE; 47 48 49 private String fPartition; 50 51 private int fHorizontalScrollPixel; 52 53 56 public void setSizeConstraints(int maxWidth, int maxHeight) { 57 fMaxWidth= maxWidth; 58 fMaxHeight= maxHeight; 59 } 60 61 67 public CustomSourceInformationControl(Shell parent, String partition) { 68 super(parent); 69 setViewerFont(); 70 setStartingPartitionType(partition); 71 } 72 73 76 public Point computeSizeHint() { 77 Point size= super.computeSizeHint(); 78 size.x= Math.min(size.x, fMaxWidth); 79 size.y= Math.min(size.y, fMaxHeight); 80 return size; 81 } 82 83 86 private void setViewerFont() { 87 Font font= JFaceResources.getFont(SYMBOLIC_FONT_NAME); 88 89 if (getViewer().getDocument() != null) { 90 91 Point selection= getViewer().getSelectedRange(); 92 int topIndex= getViewer().getTopIndex(); 93 94 StyledText styledText= getViewer().getTextWidget(); 95 Control parent= styledText; 96 if (getViewer() instanceof ITextViewerExtension) { 97 ITextViewerExtension extension= (ITextViewerExtension) getViewer(); 98 parent= extension.getControl(); 99 } 100 101 parent.setRedraw(false); 102 103 styledText.setFont(font); 104 105 getViewer().setSelectedRange(selection.x , selection.y); 106 getViewer().setTopIndex(topIndex); 107 108 if (parent instanceof Composite) { 109 Composite composite= (Composite) parent; 110 composite.layout(true); 111 } 112 113 parent.setRedraw(true); 114 115 } else { 116 StyledText styledText= getViewer().getTextWidget(); 117 styledText.setFont(font); 118 } 119 } 120 121 126 public void setStartingPartitionType(String partition) { 127 if (partition == null) 128 fPartition= IDocument.DEFAULT_CONTENT_TYPE; 129 else 130 fPartition= partition; 131 } 132 133 136 public void setInformation(String content) { 137 super.setInformation(content); 138 IDocument doc= getViewer().getDocument(); 139 if (doc == null) 140 return; 141 142 ensureScrollable(); 144 145 String start= null; 146 if (IJavaPartitions.JAVA_DOC.equals(fPartition)) { 147 start= "/**" + doc.getLegalLineDelimiters()[0]; } else if (IJavaPartitions.JAVA_MULTI_LINE_COMMENT.equals(fPartition)) { 149 start= "/*" + doc.getLegalLineDelimiters()[0]; } 151 if (start != null) { 152 try { 153 doc.replace(0, 0, start); 154 int startLen= start.length(); 155 getViewer().setDocument(doc, startLen, doc.getLength() - startLen); 156 } catch (BadLocationException e) { 157 Assert.isTrue(false); 159 } 160 } 161 162 getViewer().getTextWidget().setHorizontalPixel(fHorizontalScrollPixel); 163 } 164 165 170 private void ensureScrollable() { 171 IDocument doc= getViewer().getDocument(); 172 if (doc == null) 173 return; 174 175 StyledText widget= getViewer().getTextWidget(); 176 if (widget == null || widget.isDisposed()) 177 return; 178 179 int last= doc.getNumberOfLines() - 1; 180 GC gc= new GC(widget); 181 gc.setFont(widget.getFont()); 182 int maxWidth= 0; 183 String content= new String (); 184 185 try { 186 for (int i= 0; i <= last; i++) { 187 IRegion line; 188 line= doc.getLineInformation(i); 189 content= doc.get(line.getOffset(), line.getLength()); 190 int width= gc.textExtent(content).x; 191 if (width > maxWidth) { 192 maxWidth= width; 193 } 194 } 195 } catch (BadLocationException e) { 196 return; 197 } finally { 198 gc.dispose(); 199 } 200 201 fMaxWidth= Math.max(0, Math.min(fMaxWidth, maxWidth - fHorizontalScrollPixel + 8)); 204 } 205 206 209 public boolean hasContents() { 210 return super.hasContents() && fMaxWidth > 0; 211 } 212 213 218 public void setHorizontalScrollPixel(int scrollIndex) { 219 scrollIndex= Math.max(0, scrollIndex); 220 fHorizontalScrollPixel= scrollIndex; 221 } 222 } 223 | Popular Tags |