KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > cms > plugins > codeeditor > CodeHelperDialog


1 /* ===============================================================================
2  *
3  * Part of the InfoGlue Content Management Platform (www.infoglue.org)
4  *
5  * ===============================================================================
6  *
7  * Copyright (C)
8  *
9  * This program is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License version 2, as published by the
11  * Free Software Foundation. See the file LICENSE.html for more information.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
19  * Place, Suite 330 / Boston, MA 02111-1307 / USA.
20  *
21  * ===============================================================================
22  */

23  
24 package org.infoglue.cms.plugins.codeeditor;
25
26 import java.awt.Color JavaDoc;
27 import java.awt.Font JavaDoc;
28 import java.awt.Frame JavaDoc;
29 import java.awt.event.KeyEvent JavaDoc;
30 import java.awt.event.KeyListener JavaDoc;
31 import java.awt.event.MouseEvent JavaDoc;
32 import java.awt.event.MouseListener JavaDoc;
33
34 import javax.swing.DefaultListModel JavaDoc;
35 import javax.swing.JDialog JavaDoc;
36 import javax.swing.JList JavaDoc;
37 import javax.swing.JScrollPane JavaDoc;
38 import javax.swing.text.SimpleAttributeSet JavaDoc;
39 import javax.swing.text.StyleConstants JavaDoc;
40
41 /**
42  * This class is the dialog showing available methods in the templateLogic-object.
43  */

44
45 public class CodeHelperDialog extends JDialog JavaDoc implements MouseListener JavaDoc, KeyListener JavaDoc
46 {
47     private Frame JavaDoc parentFrame = null;
48     private InfoGlueTextArea infoGlueTextArea = null;
49     private JList JavaDoc list = new JList JavaDoc();
50     
51     
52     public CodeHelperDialog(Frame JavaDoc parentFrame, InfoGlueTextArea infoGlueTextArea, Object JavaDoc[] items)
53     {
54         super(parentFrame, true);
55         this.parentFrame = parentFrame;
56         this.infoGlueTextArea = infoGlueTextArea;
57         
58         DefaultListModel JavaDoc model = new DefaultListModel JavaDoc();
59         for (int i=0; i<items.length; i++)
60         {
61             model.addElement(items[i]);
62         }
63         this.list.setModel(model);
64         JScrollPane JavaDoc scroller = new JScrollPane JavaDoc();
65         scroller.getViewport().add(list);
66         this.getContentPane().add(scroller);
67         this.list.setFont(new Font JavaDoc("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 JavaDoc event)
75     {
76         //this.infoGlueTextArea.insert(this.list.getSelectedValue().toString(), this.infoGlueTextArea.getCaretPosition());
77
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 JavaDoc event)
85     {
86         //System.out.println("mouseReleased:" + event);
87
}
88
89     public void mouseClicked(MouseEvent JavaDoc event)
90     {
91         //System.out.println("mouseClicked:" + event);
92
}
93
94     public void mouseEntered(MouseEvent JavaDoc event)
95     {
96         //System.out.println("mouseEntered:" + event);
97
}
98
99     public void mouseExited(MouseEvent JavaDoc event)
100     {
101         //System.out.println("mouseExited:" + event);
102
}
103     
104     public void keyTyped(KeyEvent JavaDoc e)
105     {
106         //System.out.println("Hepp:" + e.getKeyChar());
107
if(e.getKeyChar() == KeyEvent.VK_ENTER)
108         {
109             //this.infoGlueTextArea.insert(this.list.getSelectedValue().toString(), this.infoGlueTextArea.getCaretPosition());
110
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             //this.infoGlueTextArea.insert("" + e.getKeyChar(), this.infoGlueTextArea.getCaretPosition());
119
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 JavaDoc e)
128     {
129         //System.out.println("Hopp:" + e.getKeyChar());
130
}
131
132     public void keyReleased(KeyEvent JavaDoc e)
133     {
134         //System.out.println("Happ:" + e.getKeyChar());
135
}
136     
137     public void insertSpecialText(int position, String JavaDoc text)
138     {
139         //SimpleAttributeSet s = new SimpleAttributeSet();
140
/*StyleConstants.setFontFamily(s, "Courier");
141         StyleConstants.setFontSize(s, 12);
142         StyleConstants.setForeground(s, Color.red);
143         StyleConstants.setBackground(s, new Color(240, 240, 240));
144         */

145         
146         SimpleAttributeSet JavaDoc attribs = new SimpleAttributeSet JavaDoc(this.infoGlueTextArea.getParagraphAttributes());
147         StyleConstants.setBackground(attribs, new Color JavaDoc(230, 230, 230));
148                 
149         try
150         {
151             this.infoGlueTextArea.getDocument().insertString(position, text, attribs);
152         }
153         catch(Exception JavaDoc ex)
154         {
155             ex.printStackTrace();
156         }
157         
158         this.infoGlueTextArea.setCharacterAttributes(this.infoGlueTextArea.getParagraphAttributes(), false);
159     }
160
161
162 }
163
164
Popular Tags