1 22 23 package org.gjt.sp.jedit.gui; 24 25 import javax.swing.*; 27 import javax.swing.text.*; 28 import javax.swing.event.MouseInputAdapter ; 29 import java.awt.*; 30 import java.awt.event.*; 31 import org.gjt.sp.jedit.*; 32 34 39 public class HistoryText 40 { 41 public HistoryText(JTextComponent text, String name) 43 { 44 this.text = text; 45 setModel(name); 46 index = -1; 47 } 49 public void fireActionPerformed() 51 { 52 } 54 public int getIndex() 56 { 57 return index; 58 } 60 public void setIndex(int index) 62 { 63 this.index = index; 64 } 66 71 public HistoryModel getModel() 72 { 73 return historyModel; 74 } 76 82 public void setModel(String name) 83 { 84 if(name == null) 85 historyModel = null; 86 else 87 historyModel = HistoryModel.getModel(name); 88 index = -1; 89 } 91 96 public void setInstantPopups(boolean instantPopups) 97 { 98 this.instantPopups = instantPopups; 99 } 101 106 public boolean getInstantPopups() 107 { 108 return instantPopups; 109 } 111 115 public void addCurrentToHistory() 116 { 117 if(historyModel != null) 118 historyModel.addItem(getText()); 119 index = 0; 120 } 122 public void doBackwardSearch() 124 { 125 if(historyModel == null) 126 return; 127 128 if(text.getSelectionEnd() != getDocument().getLength()) 129 { 130 text.setCaretPosition(getDocument().getLength()); 131 } 132 133 int start = getInputStart(); 134 String t = getText().substring(0, 135 text.getSelectionStart() - start); 136 if(t == null) 137 { 138 historyPrevious(); 139 return; 140 } 141 142 for(int i = index + 1; i < historyModel.getSize(); i++) 143 { 144 String item = historyModel.getItem(i); 145 if(item.startsWith(t)) 146 { 147 text.replaceSelection(item.substring(t.length())); 148 text.select(getInputStart() + t.length(), 149 getDocument().getLength()); 150 index = i; 151 return; 152 } 153 } 154 155 text.getToolkit().beep(); 156 } 158 public void doForwardSearch() 160 { 161 if(historyModel == null) 162 return; 163 164 if(text.getSelectionEnd() != getDocument().getLength()) 165 { 166 text.setCaretPosition(getDocument().getLength()); 167 } 168 169 int start = getInputStart(); 170 String t = getText().substring(0, 171 text.getSelectionStart() - start); 172 if(t == null) 173 { 174 historyNext(); 175 return; 176 } 177 178 for(int i = index - 1; i >= 0; i--) 179 { 180 String item = historyModel.getItem(i); 181 if(item.startsWith(t)) 182 { 183 text.replaceSelection(item.substring(t.length())); 184 text.select(getInputStart() + t.length(), 185 getDocument().getLength()); 186 index = i; 187 return; 188 } 189 } 190 191 text.getToolkit().beep(); 192 } 194 public void historyPrevious() 196 { 197 if(historyModel == null) 198 return; 199 200 if(index == historyModel.getSize() - 1) 201 text.getToolkit().beep(); 202 else if(index == -1) 203 { 204 current = getText(); 205 setText(historyModel.getItem(0)); 206 index = 0; 207 } 208 else 209 { 210 int newIndex = index + 1; 212 setText(historyModel.getItem(newIndex)); 213 index = newIndex; 214 } 215 } 217 public void historyNext() 219 { 220 if(historyModel == null) 221 return; 222 223 if(index == -1) 224 text.getToolkit().beep(); 225 else if(index == 0) 226 setText(current); 227 else 228 { 229 int newIndex = index - 1; 231 setText(historyModel.getItem(newIndex)); 232 index = newIndex; 233 } 234 } 236 public Document getDocument() 238 { 239 return text.getDocument(); 240 } 242 247 public String getText() 248 { 249 return text.getText(); 250 } 252 257 public void setText(String text) 258 { 259 this.index = -1; 260 this.text.setText(text); 261 } 263 268 public int getInputStart() 269 { 270 return 0; 271 } 273 public void showPopupMenu(String t, int x, int y) 275 { 276 if(historyModel == null) 277 return; 278 279 text.requestFocus(); 280 281 if(popup != null && popup.isVisible()) 282 { 283 popup.setVisible(false); 284 return; 285 } 286 287 popup = new JPopupMenu(); 288 JMenuItem caption = new JMenuItem(jEdit.getProperty( 289 "history.caption")); 290 caption.getModel().setEnabled(false); 291 popup.add(caption); 292 popup.addSeparator(); 293 294 for(int i = 0; i < historyModel.getSize(); i++) 295 { 296 String item = historyModel.getItem(i); 297 if(item.startsWith(t)) 298 { 299 JMenuItem menuItem = new JMenuItem(item); 300 menuItem.setActionCommand(String.valueOf(i)); 301 menuItem.addActionListener( 302 new ActionHandler()); 303 popup.add(menuItem); 304 } 305 } 306 307 GUIUtilities.showPopupMenu(popup,text,x,y,false); 308 } 310 public void showPopupMenu(boolean search) 312 { 313 if(search) 314 showPopupMenu(getText().substring(getInputStart(), 315 text.getSelectionStart()),0,text.getHeight()); 316 else 317 showPopupMenu("",0,text.getHeight()); 318 } 320 private JTextComponent text; 322 private HistoryModel historyModel; 323 private int index; 324 private String current; 325 private JPopupMenu popup; 326 private boolean instantPopups; 327 329 class ActionHandler implements ActionListener 331 { 332 public void actionPerformed(ActionEvent evt) 333 { 334 int ind = Integer.parseInt(evt.getActionCommand()); 335 if(ind == -1) 336 { 337 if(index != -1) 338 setText(current); 339 } 340 else 341 { 342 setText(historyModel.getItem(ind)); 343 index = ind; 344 } 345 if(instantPopups) 346 { 347 addCurrentToHistory(); 348 fireActionPerformed(); 349 } 350 } 351 } } 353 | Popular Tags |