1 23 24 package org.infoglue.cms.plugins.codeeditor; 25 26 import java.awt.Color ; 27 import java.awt.Font ; 28 import java.awt.Frame ; 29 import java.awt.event.KeyEvent ; 30 import java.awt.event.KeyListener ; 31 import java.awt.event.MouseEvent ; 32 import java.awt.event.MouseListener ; 33 34 import javax.swing.DefaultListModel ; 35 import javax.swing.JDialog ; 36 import javax.swing.JList ; 37 import javax.swing.JScrollPane ; 38 import javax.swing.text.SimpleAttributeSet ; 39 import javax.swing.text.StyleConstants ; 40 41 44 45 public class CodeHelperDialog extends JDialog implements MouseListener , KeyListener 46 { 47 private Frame parentFrame = null; 48 private InfoGlueTextArea infoGlueTextArea = null; 49 private JList list = new JList (); 50 51 52 public CodeHelperDialog(Frame parentFrame, InfoGlueTextArea infoGlueTextArea, Object [] items) 53 { 54 super(parentFrame, true); 55 this.parentFrame = parentFrame; 56 this.infoGlueTextArea = infoGlueTextArea; 57 58 DefaultListModel model = new DefaultListModel (); 59 for (int i=0; i<items.length; i++) 60 { 61 model.addElement(items[i]); 62 } 63 this.list.setModel(model); 64 JScrollPane scroller = new JScrollPane (); 65 scroller.getViewport().add(list); 66 this.getContentPane().add(scroller); 67 this.list.setFont(new Font ("Courier", Font.PLAIN, 12)); 68 this.list.addKeyListener(this); 69 this.list.addMouseListener(this); 70 this.setSize(500, 200); 71 this.list.requestFocus(); 72 } 73 74 public void mousePressed(MouseEvent event) 75 { 76 insertSpecialText(this.infoGlueTextArea.getCaretPosition(), this.list.getSelectedValue().toString()); 78 79 this.list.setSelectedIndex(0); 80 this.setVisible(false); 81 this.infoGlueTextArea.requestFocus(); 82 } 83 84 public void mouseReleased(MouseEvent event) 85 { 86 } 88 89 public void mouseClicked(MouseEvent event) 90 { 91 } 93 94 public void mouseEntered(MouseEvent event) 95 { 96 } 98 99 public void mouseExited(MouseEvent event) 100 { 101 } 103 104 public void keyTyped(KeyEvent e) 105 { 106 if(e.getKeyChar() == KeyEvent.VK_ENTER) 108 { 109 insertSpecialText(this.infoGlueTextArea.getCaretPosition(), this.list.getSelectedValue().toString()); 111 112 this.setVisible(false); 113 this.list.setSelectedIndex(0); 114 this.infoGlueTextArea.requestFocus(); 115 } 116 else 117 { 118 insertSpecialText(this.infoGlueTextArea.getCaretPosition(), "" + e.getKeyChar()); 120 121 this.setVisible(false); 122 this.list.setSelectedIndex(0); 123 this.infoGlueTextArea.requestFocus(); 124 } 125 } 126 127 public void keyPressed(KeyEvent e) 128 { 129 } 131 132 public void keyReleased(KeyEvent e) 133 { 134 } 136 137 public void insertSpecialText(int position, String text) 138 { 139 145 146 SimpleAttributeSet attribs = new SimpleAttributeSet (this.infoGlueTextArea.getParagraphAttributes()); 147 StyleConstants.setBackground(attribs, new Color (230, 230, 230)); 148 149 try 150 { 151 this.infoGlueTextArea.getDocument().insertString(position, text, attribs); 152 } 153 catch(Exception ex) 154 { 155 ex.printStackTrace(); 156 } 157 158 this.infoGlueTextArea.setCharacterAttributes(this.infoGlueTextArea.getParagraphAttributes(), false); 159 } 160 161 162 } 163 164 | Popular Tags |