1 30 package com.genimen.djeneric.ui; 31 32 import java.awt.event.ActionEvent ; 33 import java.awt.event.ActionListener ; 34 import java.util.ArrayList ; 35 import java.util.Collections ; 36 import java.util.HashMap ; 37 import java.util.Iterator ; 38 39 import javax.swing.JMenuItem ; 40 import javax.swing.JPopupMenu ; 41 import javax.swing.text.JTextComponent ; 42 43 import com.genimen.djeneric.util.DjString; 44 import com.genimen.djeneric.util.DjStringReplacer; 45 46 public class DjCodeCompleter extends JPopupMenu implements ActionListener 47 { 48 private static final long serialVersionUID = 3257571706699658544L; 49 50 DjEditorPositionInfo _wi; 51 52 public DjCodeCompleter(HashMap selection, JTextComponent editor, DjEditorPositionInfo wi) 53 { 54 super("Code completion"); 55 ArrayList keys = new ArrayList (); 56 Iterator it = selection.keySet().iterator(); 57 while (it.hasNext()) 58 { 59 keys.add(it.next()); 60 } 61 Collections.sort(keys); 62 _wi = wi; 63 for (int i = 0; i < keys.size(); i++) 64 { 65 JMenuItem itm = new JMenuItem (keys.get(i).toString()); 66 itm.setActionCommand(selection.get(keys.get(i)).toString()); 67 itm.addActionListener(this); 68 add(itm); 69 } 70 } 71 72 public void actionPerformed(ActionEvent e) 73 { 74 int indent = _wi.getStart() - _wi.getLineStart(); 75 String indentStr = ""; 76 if (indent > 0) indentStr = DjString.appendToLength("", " ", indent); 77 String value = e.getActionCommand(); 78 int idx = value.indexOf("|"); 79 if (idx != -1) 80 { 81 DjStringReplacer sr = new DjStringReplacer(value); 82 sr.replace("\n", "\n" + indentStr); 83 idx = value.indexOf("|"); 84 value = sr.replace("|", "").trim() + "\n"; 85 } 86 else idx = value.length(); 87 88 _wi.replaceRange(value, idx); 89 } 90 } | Popular Tags |