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