1 19 20 21 package org.netbeans.modules.search.types; 22 23 24 import javax.swing.JEditorPane ; 25 import javax.swing.SwingUtilities ; 26 import javax.swing.text.Caret ; 27 28 import org.openide.cookies.EditorCookie; 29 import org.openide.cookies.LineCookie; 30 import org.openide.loaders.DataObject; 31 import org.openide.text.Line; 32 33 import java.awt.*; 34 import org.openidex.search.SearchHistory; 35 import org.openidex.search.SearchPattern; 36 37 38 44 public class TextDetail extends Object { 45 46 47 public static final int DH_SHOW = 1; 48 49 public static final int DH_GOTO = 2; 50 51 public static final int DH_HIDE = 3; 52 53 54 private DataObject dobj; 55 56 private int line; 57 58 private String lineText; 59 60 private int column; 61 62 private int markLength; 63 64 private Line lineObj; 65 66 private SearchPattern searchPattern; 67 68 69 70 73 public TextDetail(DataObject dobj, SearchPattern pattern) { 74 this.dobj = dobj; 75 this.searchPattern = pattern; 76 } 77 78 87 public void showDetail(int how) { 88 if (dobj == null) { 89 Toolkit.getDefaultToolkit().beep(); 90 return; 91 } 92 if (lineObj == null) { LineCookie lineCookie = (LineCookie) dobj.getCookie(LineCookie.class); 94 if (lineCookie != null) { 95 Line.Set lineSet = lineCookie.getLineSet(); 96 try { 97 lineObj = lineSet.getOriginal(line - 1); 98 } catch (IndexOutOfBoundsException ioobex) { 99 lineObj = lineSet.getOriginal(findMaxLine(lineSet)); 101 column = markLength = 0; 102 } 103 } 104 if (lineObj == null) { 105 Toolkit.getDefaultToolkit().beep(); 106 return; 107 } 108 } 109 110 if (how == DH_HIDE) { 111 return; 112 } 113 EditorCookie edCookie = (EditorCookie) dobj.getCookie(EditorCookie.class); 114 if (edCookie != null) 115 edCookie.open(); 116 if (how == DH_SHOW) { 117 lineObj.show(Line.SHOW_TRY_SHOW, column - 1); 118 } 119 else if (how == DH_GOTO) { 120 lineObj.show(Line.SHOW_GOTO, column - 1); 121 } 122 if (markLength > 0 && edCookie != null) { 123 final JEditorPane [] panes = edCookie.getOpenedPanes(); 124 if (panes != null && panes.length > 0) { 125 SwingUtilities.invokeLater(new Runnable () { 127 public void run() { 128 Caret caret = panes[0].getCaret(); caret.moveDot(caret.getDot() + markLength); 130 } 131 }); 132 } 133 } 134 SearchHistory.getDefault().setLastSelected(searchPattern); 135 } 136 137 138 public String getLineText() { 139 return lineText; 140 } 141 142 143 public void setLineText(String text) { 144 lineText = text; 145 } 146 147 148 153 public DataObject getDataObject() { 154 return dobj; 155 } 156 157 158 public int getLine() { 159 return line; 160 } 161 162 163 public void setLine(int line) { 164 this.line = line; 165 } 166 167 168 public int getColumn() { 169 return column; 170 } 171 172 173 public void setColumn(int col) { 174 column = col; 175 } 176 177 178 public void setMarkLength(int len) { 179 markLength = len; 180 } 181 182 183 public int getMarkLength() { 184 return markLength; 185 } 186 187 195 private static int findMaxLine(Line.Set set) { 196 int from = 0; 197 int to = 32000; 198 199 for (;;) { 200 try { 201 set.getOriginal(to); 202 from = to; 205 to *= 2; 206 } catch (IndexOutOfBoundsException ex) { 207 break; 208 } 209 } 210 211 while (from < to) { 212 int middle = (from + to + 1) / 2; 213 214 try { 215 set.getOriginal(middle); 216 from = middle; 218 } catch (IndexOutOfBoundsException ex) { 219 to = middle - 1; 221 } 222 } 223 224 return from; 225 } 226 227 } 228 | Popular Tags |