1 30 package com.genimen.djeneric.tools.specifier.editor; 31 32 import java.awt.BorderLayout ; 33 import java.awt.FlowLayout ; 34 import java.awt.event.ActionEvent ; 35 import java.awt.event.MouseEvent ; 36 import java.util.ArrayList ; 37 38 import javax.swing.DefaultListModel ; 39 import javax.swing.JButton ; 40 import javax.swing.JList ; 41 import javax.swing.JPanel ; 42 import javax.swing.JScrollPane ; 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 _editorEventListeners = new ArrayList (); 57 String _eventContext = null; 58 BorderLayout borderLayout1 = new BorderLayout (); 59 JPanel jPanel1 = new JPanel (); 60 JPanel _pnlButtons = new JPanel (); 61 BorderLayout borderLayout2 = new BorderLayout (); 62 JPanel _pnlButtonsRight = new JPanel (); 63 FlowLayout flowLayout1 = new FlowLayout (); 64 JButton _butOk = new JButton (); 65 JButton _ButCancel = new JButton (); 66 BorderLayout borderLayout3 = new BorderLayout (); 67 JScrollPane jScrollPane1 = new JScrollPane (); 68 JList _lstEvents = new JList (); 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 ()); 84 } 85 catch (Exception ex) 86 { 87 DjLogger.log(ex); 88 } 89 } 90 91 void jbInit() throws Exception 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 eventName) throws DjenericException 113 { 114 String 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 getEventContext() 137 { 138 return _eventContext; 139 } 140 141 public void setEventContext(String string) 142 { 143 _eventContext = string; 144 } 145 146 public void registerEvent(String eventName, String eventTitle, String eventOption) 147 { 148 if ("cancel".equals(eventOption)) 149 { 150 _ButCancel.setVisible(false); 151 152 JButton newButton = new JButton (); 153 154 newButton.setText(eventTitle); 155 newButton.setActionCommand(eventName + "," + eventOption); 156 newButton.addActionListener(new java.awt.event.ActionListener () 157 { 158 public void actionPerformed(ActionEvent e) 159 { 160 fireCustomEvent(e); 161 } 162 }); 163 164 _pnlButtonsRight.add(newButton, null, _customButtonCount++); 165 Util.sizeButtons(_pnlButtonsRight); 166 } 167 else 168 { 169 DefaultListModel model = (DefaultListModel ) _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 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 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 e) 206 { 207 String event = e.getActionCommand(); 208 String 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 event = eventDef.getEventName(); 221 String 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 x) 240 { 241 setStatusMessage(x); 242 } 243 244 } 245 246 void _butOk_actionPerformed(ActionEvent 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 e) 255 { 256 if (e.getClickCount() > 1) _butOk_actionPerformed(null); 257 } 258 259 class EventDef 260 { 261 String _eventName; 262 String _eventTitle; 263 String _option; 264 265 EventDef(String eventName, String eventTitle, String option) 266 { 267 _eventName = eventName; 268 _eventTitle = eventTitle; 269 _option = option; 270 } 271 272 public String getOption() 273 { 274 return _option; 275 } 276 277 public String getEventName() 278 { 279 return _eventName; 280 } 281 282 public String toString() 283 { 284 return _eventTitle; 285 } 286 } 287 288 } 289 290 class DispatchPanel__ButCancel_actionAdapter implements java.awt.event.ActionListener 291 { 292 DispatchPanel adaptee; 293 294 DispatchPanel__ButCancel_actionAdapter(DispatchPanel adaptee) 295 { 296 this.adaptee = adaptee; 297 } 298 299 public void actionPerformed(ActionEvent e) 300 { 301 adaptee._ButCancel_actionPerformed(e); 302 } 303 } 304 305 class DispatchPanel__butOk_actionAdapter implements java.awt.event.ActionListener 306 { 307 DispatchPanel adaptee; 308 309 DispatchPanel__butOk_actionAdapter(DispatchPanel adaptee) 310 { 311 this.adaptee = adaptee; 312 } 313 314 public void actionPerformed(ActionEvent e) 315 { 316 adaptee._butOk_actionPerformed(e); 317 } 318 } 319 320 class DispatchPanel__lstEvents_mouseAdapter extends java.awt.event.MouseAdapter 321 { 322 DispatchPanel adaptee; 323 324 DispatchPanel__lstEvents_mouseAdapter(DispatchPanel adaptee) 325 { 326 this.adaptee = adaptee; 327 } 328 329 public void mouseClicked(MouseEvent e) 330 { 331 adaptee._lstEvents_mouseClicked(e); 332 } 333 } | Popular Tags |