KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > ui > DjPropertyPathCompleter


1 /*
2  * Copyright (c) 2001-2005 by Genimen BV (www.genimen.com) All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, is permitted provided that the following conditions are met: -
6  * Redistributions of source code must retain the above copyright notice, this
7  * list of conditions and the following disclaimer. - Redistributions in binary
8  * form must reproduce the above copyright notice, this list of conditions and
9  * the following disclaimer in the documentation and/or other materials
10  * provided with the distribution. - All advertising materials mentioning
11  * features or use of this software must display the following acknowledgment:
12  * "This product includes Djeneric." - Products derived from this software may
13  * not be called "Djeneric" nor may "Djeneric" appear in their names without
14  * prior written permission of Genimen BV. - Redistributions of any form
15  * whatsoever must retain the following acknowledgment: "This product includes
16  * Djeneric."
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL GENIMEN BV, DJENERIC.ORG, OR CONTRIBUTORS
22  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */

30 package com.genimen.djeneric.ui;
31
32 import java.awt.Point JavaDoc;
33 import java.awt.event.InputEvent JavaDoc;
34 import java.awt.event.KeyEvent JavaDoc;
35 import java.awt.event.KeyListener JavaDoc;
36 import java.util.ArrayList JavaDoc;
37 import java.util.Collections JavaDoc;
38 import java.util.HashMap JavaDoc;
39 import java.util.Iterator JavaDoc;
40
41 import javax.swing.text.JTextComponent JavaDoc;
42
43 import com.genimen.djeneric.repository.DjExtent;
44 import com.genimen.djeneric.repository.DjRelation;
45 import com.genimen.djeneric.repository.exceptions.ObjectNotDefinedException;
46 import com.genimen.djeneric.util.DjStringComparator;
47
48 public class DjPropertyPathCompleter implements KeyListener JavaDoc
49 {
50   JTextComponent JavaDoc _editor;
51   DjExtent _rootExtent;
52   ArrayList JavaDoc _allListeners = new ArrayList JavaDoc();
53   ArrayList JavaDoc _parameterNames = new ArrayList JavaDoc();
54   boolean _includeOne2Many = false;
55   HashMap JavaDoc _parameters = new HashMap JavaDoc();
56
57   public DjPropertyPathCompleter()
58   {
59   }
60
61   public DjPropertyPathCompleter(DjExtent rootExtent, JTextComponent JavaDoc editor)
62   {
63     setEditor(editor);
64     _rootExtent = rootExtent;
65   }
66
67   public void setIncludeOne2Many(boolean b)
68   {
69     _includeOne2Many = b;
70   }
71
72   public void showPopup() throws ObjectNotDefinedException
73   {
74     DjEditorPositionInfo wi = new DjEditorPositionInfo(_editor, true);
75
76     HashMap JavaDoc possibles = getCompletionMap(wi);
77     doShow(possibles, wi);
78   }
79
80   public ArrayList JavaDoc getCompletions() throws ObjectNotDefinedException
81   {
82     DjEditorPositionInfo wi = new DjEditorPositionInfo(_editor, true);
83     ArrayList JavaDoc lst = new ArrayList JavaDoc();
84     Iterator JavaDoc it = getCompletionMap(wi).keySet().iterator();
85     while (it.hasNext())
86       lst.add(it.next());
87     Collections.sort(lst, new DjStringComparator(false));
88     return lst;
89   }
90
91   protected HashMap JavaDoc getCompletionMap(DjEditorPositionInfo wi) throws ObjectNotDefinedException
92   {
93     String JavaDoc propStart = wi.getWord();
94
95     DjExtent extent = _rootExtent;
96     HashMap JavaDoc possibles = new HashMap JavaDoc();
97
98     completeParameters(wi, possibles);
99     completeProperties(wi, possibles);
100     return possibles;
101   }
102
103   private void completeProperties(DjEditorPositionInfo wi, HashMap JavaDoc possibles) throws ObjectNotDefinedException
104   {
105     String JavaDoc propStart = wi.getWord();
106
107     DjExtent extent = _rootExtent;
108     int dotIdx = propStart.indexOf(".");
109     if (dotIdx != -1)
110     {
111       dotIdx = propStart.lastIndexOf(".");
112
113       String JavaDoc leadingPath = propStart.substring(0, dotIdx);
114       wi.setStart(wi.getStart() + leadingPath.length() + 1);
115
116       propStart = propStart.substring(dotIdx + 1);
117
118       extent = extent.resolveType(leadingPath);
119     }
120
121     for (int i = 0; i < extent.getPropertyCount(); i++)
122     {
123       if (extent.getProperty(i).getName().toLowerCase().startsWith(propStart.toLowerCase()))
124       {
125         possibles.put(extent.getProperty(i).getName(), extent.getProperty(i).getName());
126       }
127     }
128     for (int i = 0; i < extent.getDetailRelationCount(); i++)
129     {
130       DjRelation rel = extent.getDetailRelation(i);
131
132       if ((rel.isOneToOne() || _includeOne2Many) && rel.getName().toLowerCase().startsWith(propStart.toLowerCase()))
133       {
134         possibles.put(rel.getName(), rel.getName());
135       }
136     }
137   }
138
139   private void completeParameters(DjEditorPositionInfo wi, HashMap JavaDoc possibles) throws ObjectNotDefinedException
140   {
141     String JavaDoc propStart = wi.getWord();
142     DjExtent extent;
143     for (int i = 0; i < _parameterNames.size(); i++)
144     {
145       String JavaDoc name = ":" + _parameterNames.get(i);
146       if (name.toLowerCase().startsWith(propStart.toLowerCase()))
147       {
148         possibles.put(name, name);
149       }
150     }
151   }
152
153   private void doShow(HashMap JavaDoc possibles, DjEditorPositionInfo wi)
154   {
155     if (possibles.size() > 0)
156     {
157       DjCodeCompleter sel = new DjCodeCompleter(possibles, getEditor(), wi);
158       Point JavaDoc p = getEditor().getCaret().getMagicCaretPosition();
159       int x = 0;
160       int y = 0;
161       if (p != null)
162       {
163         x = p.x;
164         y = p.y;
165       }
166
167       sel.show(getEditor(), x, y);
168     }
169   }
170
171   public JTextComponent JavaDoc getEditor()
172   {
173     return _editor;
174   }
175
176   public void setEditor(JTextComponent JavaDoc editor)
177   {
178     if (_editor != null && _editor != editor) _editor.removeKeyListener(this);
179     _editor = editor;
180     _editor.addKeyListener(this);
181   }
182
183   public void keyPressed(KeyEvent JavaDoc e)
184   {
185     if ((e.getKeyCode() == KeyEvent.VK_SPACE) && ((e.getModifiers() & InputEvent.CTRL_MASK) != 0))
186     {
187       try
188       {
189         showPopup();
190         setStatusMessage("", true);
191       }
192       catch (ObjectNotDefinedException onde)
193       {
194         setStatusMessage(onde.getMessage(), false);
195       }
196     }
197   }
198
199   public void keyReleased(KeyEvent JavaDoc e)
200   {
201   }
202
203   public void keyTyped(KeyEvent JavaDoc e)
204   {
205   }
206
207   public void setStatusMessage(String JavaDoc msg, boolean isInformative)
208   {
209     for (int i = 0; i < _allListeners.size(); i++)
210     {
211       DjStatusListener lsnr = (DjStatusListener) _allListeners.get(i);
212       lsnr.setStatusMessage(msg, isInformative);
213     }
214   }
215
216   public void addStatusListener(DjStatusListener lsnr)
217   {
218     _allListeners.add(lsnr);
219   }
220
221   public void removeStatusListener(DjStatusListener lsnr)
222   {
223     _allListeners.remove(lsnr);
224   }
225
226   public DjExtent getRootExtent()
227   {
228     return _rootExtent;
229   }
230
231   public void setRootExtent(DjExtent rootExtent)
232   {
233     _rootExtent = rootExtent;
234   }
235
236   public void addParameter(String JavaDoc paramName, DjExtent type)
237   {
238     _parameters.put(paramName, type);
239     _parameterNames.add(paramName);
240   }
241
242   public void setParameters(HashMap JavaDoc parameters)
243   {
244     _parameters = parameters;
245     Iterator JavaDoc it = _parameters.keySet().iterator();
246     while (it.hasNext())
247       _parameterNames.add(it.next());
248   }
249 }
Popular Tags