KickJava   Java API By Example, From Geeks To Geeks.

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


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.Component JavaDoc;
28 import java.awt.Dimension JavaDoc;
29 import java.awt.Font JavaDoc;
30 import java.awt.Frame JavaDoc;
31 import java.util.ArrayList JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.List JavaDoc;
34
35 import javax.swing.JScrollPane JavaDoc;
36 import javax.swing.JTextPane JavaDoc;
37 import javax.swing.event.DocumentEvent JavaDoc;
38 import javax.swing.event.DocumentListener JavaDoc;
39 import javax.swing.text.SimpleAttributeSet JavaDoc;
40 import javax.swing.text.Style JavaDoc;
41 import javax.swing.text.StyleConstants JavaDoc;
42 import javax.swing.text.StyleContext JavaDoc;
43 import javax.swing.text.TabSet JavaDoc;
44 import javax.swing.text.TabStop JavaDoc;
45
46 /**
47  * This class is the basic for the editor.
48  */

49
50 public class InfoGlueTextArea extends JTextPane JavaDoc implements DocumentListener JavaDoc //JTextArea
51
{
52     protected InfoGlueCodeEditorController controller = null;
53     protected JScrollPane JavaDoc scrollPane = null;
54     private Font JavaDoc standardFont = new Font JavaDoc("Courier", Font.PLAIN, 12);
55     private CodeHelperDialog methodsCodeHelperDialog = null;
56     private CodeHelperDialog velocityCodeHelperDialog = null;
57     private CodeHelperDialog velocityVariableCodeHelperDialog = null;
58     
59     /**
60      * The constructor for this editor.
61      */

62     
63     public InfoGlueTextArea(InfoGlueCodeEditorController infoGlueCodeEditorController)
64     {
65         Style JavaDoc styDefault = StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE);
66         StyleConstants.setFontFamily(styDefault,"Courier");
67         StyleConstants.setFontSize(styDefault, 14);
68         StyleConstants.setForeground(styDefault, Color.black);
69         this.setLogicalStyle(styDefault);
70         SimpleAttributeSet JavaDoc attribs = new SimpleAttributeSet JavaDoc(this.getParagraphAttributes());
71         StyleConstants.setBackground(attribs, new Color JavaDoc(230, 230, 230));
72         this.setCharacterAttributes(attribs, false);
73         
74         setTabs();
75         this.addKeyListener(new TextAreaInputListener(this));
76         this.controller = infoGlueCodeEditorController;
77     }
78
79     /**
80      * This method sets the tabs on the JTextPane
81      */

82     
83     private void setTabs()
84     {
85         List JavaDoc list = new ArrayList JavaDoc();
86         
87         float pos = 0;
88         for(int i=0; i<20; i++)
89         {
90             pos += 20;
91             int align = TabStop.ALIGN_LEFT;
92             int leader = TabStop.LEAD_NONE;
93             TabStop JavaDoc tstop = new TabStop JavaDoc(pos, align, leader);
94             list.add(tstop);
95         }
96                         
97         TabStop JavaDoc[] tstops = (TabStop JavaDoc[])list.toArray(new TabStop JavaDoc[0]);
98         TabSet JavaDoc tabs = new TabSet JavaDoc(tstops);
99         
100         Style JavaDoc style = this.getLogicalStyle();
101         StyleConstants.setTabSet(style, tabs);
102         this.setLogicalStyle(style);
103     }
104     
105     /**
106      * This method fetches a list of methods available to the template. It does this
107      * by asking the delivery-engine (calling a service). This way the list is allways up2date.
108      */

109     private List JavaDoc getTemplateLogicMethodList()
110     {
111         List JavaDoc methods = new ArrayList JavaDoc();
112         
113         try
114         {
115             String JavaDoc xml = HttpUtilities.getUrlContent(this.controller.getDeliverySettingsUrl());
116             //System.out.println("xml:" + xml);
117

118             int offset = 0;
119             int methodIndex = xml.indexOf("<method>", offset);
120             int methodEndIndex = xml.indexOf("</method>", offset);
121             
122             while(methodIndex > -1 && methodEndIndex > -1)
123             {
124                 String JavaDoc methodDescription = xml.substring(methodIndex + 8, methodEndIndex);
125                 methods.add(methodDescription);
126                 
127                 offset = methodEndIndex + 9;
128                 methodIndex = xml.indexOf("<method>", offset);
129                 methodEndIndex = xml.indexOf("</method>", offset);
130             }
131         }
132         catch(Exception JavaDoc e)
133         {
134             System.out.println("Exception:" + e);
135             e.printStackTrace();
136         }
137         
138         return methods;
139     }
140         
141     public void openTemplateLogicCodeHelper(int xPosition, int yPosition)
142     {
143         if(this.methodsCodeHelperDialog == null)
144         {
145             List JavaDoc methods = getTemplateLogicMethodList();
146             List JavaDoc listItems = new ArrayList JavaDoc();
147             Iterator JavaDoc i = methods.iterator();
148             while(i.hasNext())
149             {
150                 String JavaDoc method = "" + (String JavaDoc)i.next();
151                 listItems.add(method);
152             }
153             
154             methodsCodeHelperDialog = new CodeHelperDialog(getFrame(this), this, listItems.toArray());
155         }
156         
157         int xLocation = getFrame(this).getLocationOnScreen().x + xPosition + 10;
158         int yLocation = getFrame(this).getLocationOnScreen().y + yPosition + 24;
159
160         if(xLocation > this.scrollPane.getWidth())
161             xLocation = this.scrollPane.getWidth();
162
163         if(yLocation > this.scrollPane.getHeight())
164             yLocation = this.scrollPane.getHeight();
165         
166         this.methodsCodeHelperDialog.setLocation(xLocation, yLocation);
167         this.methodsCodeHelperDialog.setVisible(true);
168     }
169     
170
171     public void openVelocityTemplateCodeHelper(int xPosition, int yPosition)
172     {
173         if(this.velocityCodeHelperDialog == null)
174         {
175             List JavaDoc listItems = new ArrayList JavaDoc();
176             listItems.add("if()");
177             listItems.add("else()");
178             listItems.add("elseif()");
179             listItems.add("end()");
180             listItems.add("foreach()");
181             listItems.add("if()");
182             listItems.add("include()");
183             listItems.add("macro()");
184             listItems.add("parse()");
185             listItems.add("set()");
186             listItems.add("stop()");
187             
188             this.velocityCodeHelperDialog = new CodeHelperDialog(getFrame(this), this, listItems.toArray());
189         }
190         
191         int xLocation = getFrame(this).getLocationOnScreen().x + xPosition + 10;
192         int yLocation = getFrame(this).getLocationOnScreen().y + yPosition + 24;
193
194         if(xLocation > this.scrollPane.getWidth())
195             xLocation = this.scrollPane.getWidth();
196
197         if(yLocation > this.scrollPane.getHeight())
198             yLocation = this.scrollPane.getHeight();
199
200
201         this.velocityCodeHelperDialog.setLocation(xLocation, yLocation);
202         this.velocityCodeHelperDialog.setVisible(true);
203     }
204
205     public void openVelocityVariableTemplateCodeHelper(int xPosition, int yPosition)
206     {
207         if(this.velocityVariableCodeHelperDialog == null)
208         {
209             List JavaDoc listItems = new ArrayList JavaDoc();
210             listItems.add("templateLogic");
211             
212             this.velocityVariableCodeHelperDialog = new CodeHelperDialog(getFrame(this), this, listItems.toArray());
213         }
214         
215         int xLocation = getFrame(this).getLocationOnScreen().x + xPosition + 10;
216         int yLocation = getFrame(this).getLocationOnScreen().y + yPosition + 24;
217
218         if(xLocation > this.scrollPane.getWidth())
219             xLocation = this.scrollPane.getWidth();
220
221         if(yLocation > this.scrollPane.getHeight())
222             yLocation = this.scrollPane.getHeight();
223
224         this.velocityVariableCodeHelperDialog.setLocation(xLocation, yLocation);
225         this.velocityVariableCodeHelperDialog.setVisible(true);
226     }
227
228
229     private Frame JavaDoc getFrame(Component JavaDoc parent)
230     {
231         while (!(parent instanceof Frame JavaDoc))
232         {
233             parent = ((Component JavaDoc)parent).getParent();
234             //System.out.println("parent:" + parent.getClass().getName());
235
}
236         
237         //System.out.println("returning :" + parent.getClass().getName());
238
//System.out.println("Position:" + parent.getLocationOnScreen().x + ":" + parent.getLocationOnScreen().y);
239
return (Frame JavaDoc)parent;
240     }
241     
242
243     public void setScrollPane(JScrollPane JavaDoc scrollPane)
244     {
245         this.scrollPane = scrollPane;
246         
247         Rule columnView = new Rule(Rule.HORIZONTAL, true);
248         columnView.setPreferredWidth(500);
249         Rule rowView = new Rule(Rule.VERTICAL, true);
250         rowView.setPreferredHeight(20000);
251         scrollPane.setRowHeaderView(rowView);
252     }
253
254
255     // Handle insertions into the text field
256
public void insertUpdate( DocumentEvent JavaDoc event )
257     {
258     }
259
260     // Handle deletions from the text field
261
public void removeUpdate( DocumentEvent JavaDoc event )
262     {
263     }
264
265     // Handle changes to the text field
266
public void changedUpdate( DocumentEvent JavaDoc event )
267     {
268     }
269     
270     /**
271      * overridden from JEditorPane
272      * to suppress line wraps
273      * @see setSize
274      */

275     
276     public boolean getScrollableTracksViewportWidth()
277     {
278         return false;
279     }
280     
281     /**
282      * overridden from JEditorPane
283      * to suppress line wraps
284      *
285      * @see getScrollableTracksViewportWidth
286      */

287     
288     public void setSize(Dimension JavaDoc d)
289     {
290         if (d.width < getParent().getSize().width)
291         {
292             d.width = getParent().getSize().width;
293         }
294         super.setSize(d);
295     }
296     
297 }
298
Popular Tags