KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > tools > specifier > editor > DispatchPanel


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 modification, is permitted
5  * provided that the following conditions are met:
6  * - Redistributions of source code must retain the above copyright notice, this list of conditions
7  * and the following disclaimer.
8  * - Redistributions in binary form must reproduce the above copyright notice, this list of
9  * conditions and the following disclaimer in the documentation and/or other materials
10  * provided with the distribution.
11  * - All advertising materials mentioning features or use of this software must display the
12  * following acknowledgment: "This product includes Djeneric."
13  * - Products derived from this software may not be called "Djeneric" nor may
14  * "Djeneric" appear in their names without prior written permission of Genimen BV.
15  * - Redistributions of any form whatsoever must retain the following acknowledgment: "This
16  * product includes 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,
22  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30 package com.genimen.djeneric.tools.specifier.editor;
31
32 import java.awt.BorderLayout JavaDoc;
33 import java.awt.FlowLayout JavaDoc;
34 import java.awt.event.ActionEvent JavaDoc;
35 import java.awt.event.MouseEvent JavaDoc;
36 import java.util.ArrayList JavaDoc;
37
38 import javax.swing.DefaultListModel JavaDoc;
39 import javax.swing.JButton JavaDoc;
40 import javax.swing.JList JavaDoc;
41 import javax.swing.JPanel JavaDoc;
42 import javax.swing.JScrollPane JavaDoc;
43
44 import com.genimen.djeneric.language.Messages;
45 import com.genimen.djeneric.repository.exceptions.DjenericException;
46 import com.genimen.djeneric.tools.specifier.base.BasePanel;
47 import com.genimen.djeneric.tools.specifier.interfaces.EditorEventListener;
48 import com.genimen.djeneric.tools.specifier.interfaces.ScriptEventDispatcher;
49 import com.genimen.djeneric.tools.specifier.interfaces.SpecifierPanelContainer;
50 import com.genimen.djeneric.ui.Util;
51 import com.genimen.djeneric.util.DjLogger;
52
53 public class DispatchPanel extends BasePanel implements ScriptEventDispatcher
54 {
55   private static final long serialVersionUID = 1L;
56   ArrayList JavaDoc _editorEventListeners = new ArrayList JavaDoc();
57   String JavaDoc _eventContext = null;
58   BorderLayout JavaDoc borderLayout1 = new BorderLayout JavaDoc();
59   JPanel JavaDoc jPanel1 = new JPanel JavaDoc();
60   JPanel JavaDoc _pnlButtons = new JPanel JavaDoc();
61   BorderLayout JavaDoc borderLayout2 = new BorderLayout JavaDoc();
62   JPanel JavaDoc _pnlButtonsRight = new JPanel JavaDoc();
63   FlowLayout JavaDoc flowLayout1 = new FlowLayout JavaDoc();
64   JButton JavaDoc _butOk = new JButton JavaDoc();
65   JButton JavaDoc _ButCancel = new JButton JavaDoc();
66   BorderLayout JavaDoc borderLayout3 = new BorderLayout JavaDoc();
67   JScrollPane JavaDoc jScrollPane1 = new JScrollPane JavaDoc();
68   JList JavaDoc _lstEvents = new JList JavaDoc();
69   SpecifierPanelContainer _editorPanelContainer;
70   int _customButtonCount = 0;
71
72   public DispatchPanel()
73   {
74   }
75
76   public void construct(SpecifierPanelContainer editorPanelContainer)
77   {
78     try
79     {
80       _editorPanelContainer = editorPanelContainer;
81
82       jbInit();
83       _lstEvents.setModel(new DefaultListModel JavaDoc());
84     }
85     catch (Exception JavaDoc ex)
86     {
87       DjLogger.log(ex);
88     }
89   }
90
91   void jbInit() throws Exception JavaDoc
92   {
93     this.setLayout(borderLayout1);
94     _pnlButtons.setLayout(borderLayout2);
95     _pnlButtonsRight.setLayout(flowLayout1);
96     _butOk.setText(Messages.getString("global.Ok"));
97     _butOk.addActionListener(new DispatchPanel__butOk_actionAdapter(this));
98     _ButCancel.setText(Messages.getString("global.Cancel"));
99     _ButCancel.addActionListener(new DispatchPanel__ButCancel_actionAdapter(this));
100     jPanel1.setLayout(borderLayout3);
101     _lstEvents.addMouseListener(new DispatchPanel__lstEvents_mouseAdapter(this));
102     this.add(jPanel1, BorderLayout.CENTER);
103     this.add(_pnlButtons, BorderLayout.SOUTH);
104     _pnlButtons.add(_pnlButtonsRight, BorderLayout.EAST);
105     _pnlButtonsRight.add(_ButCancel, null);
106     _pnlButtonsRight.add(_butOk, null);
107     jPanel1.add(jScrollPane1, BorderLayout.CENTER);
108     jScrollPane1.getViewport().add(_lstEvents, null);
109     Util.sizeButtons(_pnlButtonsRight);
110   }
111
112   protected void fireEvent(String JavaDoc eventName) throws DjenericException
113   {
114     String JavaDoc fullEventName;
115
116     if (_eventContext == null) fullEventName = eventName;
117     else fullEventName = _eventContext + "." + eventName;
118
119     for (int i = 0; i < _editorEventListeners.size(); i++)
120     {
121       EditorEventListener eel = (EditorEventListener) _editorEventListeners.get(i);
122       eel.handleEvent(fullEventName);
123     }
124   }
125
126   public void addEditorEventListener(EditorEventListener lsnr)
127   {
128     _editorEventListeners.add(lsnr);
129   }
130
131   public void removeEditorEventListener(EditorEventListener lsnr)
132   {
133     _editorEventListeners.remove(lsnr);
134   }
135
136   public String JavaDoc getEventContext()
137   {
138     return _eventContext;
139   }
140
141   public void setEventContext(String JavaDoc string)
142   {
143     _eventContext = string;
144   }
145
146   public void registerEvent(String JavaDoc eventName, String JavaDoc eventTitle, String JavaDoc eventOption)
147   {
148     if ("cancel".equals(eventOption))
149     {
150       _ButCancel.setVisible(false);
151
152       JButton JavaDoc newButton = new JButton JavaDoc();
153
154       newButton.setText(eventTitle);
155       newButton.setActionCommand(eventName + "," + eventOption);
156       newButton.addActionListener(new java.awt.event.ActionListener JavaDoc()
157       {
158         public void actionPerformed(ActionEvent JavaDoc e)
159         {
160           fireCustomEvent(e);
161         }
162       });
163
164       _pnlButtonsRight.add(newButton, null, _customButtonCount++);
165       Util.sizeButtons(_pnlButtonsRight);
166     }
167     else
168     {
169       DefaultListModel JavaDoc model = (DefaultListModel JavaDoc) _lstEvents.getModel();
170       model.addElement(new EventDef(eventName, eventTitle, eventOption));
171       if (_lstEvents.getSelectedIndex() == -1)
172       {
173         _lstEvents.setSelectedIndex(0);
174       }
175     }
176   }
177
178   public void setStatusMessage(String JavaDoc msg, boolean isInformative)
179   {
180     _editorPanelContainer.setStatusMessage(msg, isInformative);
181   }
182
183   public boolean canClose()
184   {
185     return true;
186   }
187
188   protected void cleanup() throws DjenericException
189   {
190     fireEvent("close");
191   }
192
193   void _ButCancel_actionPerformed(ActionEvent JavaDoc e)
194   {
195     try
196     {
197       if (close()) fireEvent("cancel");
198     }
199     catch (DjenericException dje)
200     {
201       setStatusMessage(dje.getMessage(), false);
202     }
203   }
204
205   protected void fireCustomEvent(ActionEvent JavaDoc e)
206   {
207     String JavaDoc event = e.getActionCommand();
208     String JavaDoc option = "none";
209     int idx = event.indexOf(",");
210     if (idx != -1)
211     {
212       option = event.substring(idx + 1);
213       event = event.substring(0, idx);
214     }
215     fireCustomEvent(new EventDef(event, event, option));
216   }
217
218   protected void fireCustomEvent(EventDef eventDef)
219   {
220     String JavaDoc event = eventDef.getEventName();
221     String JavaDoc option = eventDef.getOption();
222
223     try
224     {
225       if ("accept".equals(option))
226       {
227         fireEvent(event);
228       }
229       else if ("cancel".equals(option))
230       {
231         if (canClose())
232         {
233           close();
234           fireEvent(event);
235         }
236       }
237       else fireEvent(event);
238     }
239     catch (Exception JavaDoc x)
240     {
241       setStatusMessage(x);
242     }
243
244   }
245
246   void _butOk_actionPerformed(ActionEvent JavaDoc e)
247   {
248     if (_lstEvents.getSelectedIndex() == -1) return;
249     EventDef ed = (EventDef) _lstEvents.getSelectedValue();
250
251     if (close()) fireCustomEvent(ed);
252   }
253
254   void _lstEvents_mouseClicked(MouseEvent JavaDoc e)
255   {
256     if (e.getClickCount() > 1) _butOk_actionPerformed(null);
257   }
258
259   class EventDef
260   {
261     String JavaDoc _eventName;
262     String JavaDoc _eventTitle;
263     String JavaDoc _option;
264
265     EventDef(String JavaDoc eventName, String JavaDoc eventTitle, String JavaDoc option)
266     {
267       _eventName = eventName;
268       _eventTitle = eventTitle;
269       _option = option;
270     }
271
272     public String JavaDoc getOption()
273     {
274       return _option;
275     }
276
277     public String JavaDoc getEventName()
278     {
279       return _eventName;
280     }
281
282     public String JavaDoc toString()
283     {
284       return _eventTitle;
285     }
286   }
287
288 }
289
290 class DispatchPanel__ButCancel_actionAdapter implements java.awt.event.ActionListener JavaDoc
291 {
292   DispatchPanel adaptee;
293
294   DispatchPanel__ButCancel_actionAdapter(DispatchPanel adaptee)
295   {
296     this.adaptee = adaptee;
297   }
298
299   public void actionPerformed(ActionEvent JavaDoc e)
300   {
301     adaptee._ButCancel_actionPerformed(e);
302   }
303 }
304
305 class DispatchPanel__butOk_actionAdapter implements java.awt.event.ActionListener JavaDoc
306 {
307   DispatchPanel adaptee;
308
309   DispatchPanel__butOk_actionAdapter(DispatchPanel adaptee)
310   {
311     this.adaptee = adaptee;
312   }
313
314   public void actionPerformed(ActionEvent JavaDoc e)
315   {
316     adaptee._butOk_actionPerformed(e);
317   }
318 }
319
320 class DispatchPanel__lstEvents_mouseAdapter extends java.awt.event.MouseAdapter JavaDoc
321 {
322   DispatchPanel adaptee;
323
324   DispatchPanel__lstEvents_mouseAdapter(DispatchPanel adaptee)
325   {
326     this.adaptee = adaptee;
327   }
328
329   public void mouseClicked(MouseEvent JavaDoc e)
330   {
331     adaptee._lstEvents_mouseClicked(e);
332   }
333 }
Popular Tags