00001 /* Copyright (C) 2004 Thomas N. Valine 00002 * tvaline@users.sourceforge.net 00003 * 00004 * This program is free software; you can redistribute it and/or 00005 * modify it under the terms of the GNU General Public License 00006 * as published by the Free Software Foundation; either version 2 00007 * of the License, or (at your option) any later version. 00008 * 00009 * This program is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 * GNU General Public License for more details. 00013 * 00014 * You should have received a copy of the GNU General Public License 00015 * along with this program; if not, write to the Free Software 00016 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 00017 * 02111-1307, USA. */ 00018 00019 package com.ohrasys.app; 00020 import java.awt.event.*; 00021 import java.util.*; 00022 import java.util.concurrent.*; 00023 00031 public abstract class AbstractAppController 00032 implements AppViewEventListener, AppModelEventListener { 00034 protected transient AppControllerEventListener model = null; 00035 00037 protected AppOption options[] = null; 00038 00040 protected ThreadFactory pool = Executors.defaultThreadFactory(); 00041 00043 protected transient List<AppControllerEventListener> views; 00044 00057 public AbstractAppController( 00058 AbstractAppModel model, AbstractAppView view, AppOption options[]) 00059 throws AppException { 00060 this.options = options; 00061 try { 00062 model.setController(this); 00063 view.setController(this); 00064 setModel(model); 00065 addView(view); 00066 } catch(TooManyListenersException e) { 00067 throw new AppException(e.getMessage()); 00068 } 00069 } 00070 00082 public abstract ActionEvent processEvent(ActionEvent evt); 00083 00089 public abstract void startController(); 00090 00097 public final void actionPerformed(ActionEvent evt) { 00098 ActionEvent event = processEvent(evt); 00099 notifyModel(event); 00100 notifyViews(event); 00101 } 00102 00108 public synchronized void addView(AppControllerEventListener view) { 00109 if(views == null){views = new ArrayList<AppControllerEventListener>();} 00110 views.add(view); 00111 } 00112 00118 public synchronized void removeView(AppControllerEventListener view) { 00119 if(views != null){views.remove(view);} 00120 } 00121 00127 public String toString(){return super.toString();} 00128 00134 protected void notifyModel(ActionEvent event) { 00135 if((model == null) || (event == null)){return;} 00136 final AppControllerEventListener mod = model; 00137 final ActionEvent evt = event; 00138 pool.newThread(new Runnable() { 00139 public void run(){mod.actionPerformed(evt);} 00140 }).start(); 00141 } 00142 00148 protected void notifyViews(ActionEvent event) { 00149 List list; 00150 synchronized(this) { 00151 if((views == null) || (event == null)){return;} 00152 list = (List)((ArrayList)views).clone(); 00153 } 00154 final ActionEvent evt = event; 00155 for(int i = 0;i < list.size();i++) { 00156 final AppControllerEventListener view = (AppControllerEventListener)list 00157 .get(i); 00158 pool.newThread(new Runnable() { 00159 public void run(){view.actionPerformed(evt);} 00160 }).start(); 00161 } 00162 } 00163 00172 protected synchronized void setModel(AppControllerEventListener model) 00173 throws TooManyListenersException { 00174 if(model == null){this.model = null;} 00175 else { 00176 if(this.model != null){throw new TooManyListenersException();} 00177 this.model = model; 00178 } 00179 } 00180 } // end class AbstractAppController 00181 00182 00183 /* This material is distributed under the GNU General Public License. 00184 * For more information please go to http://www.gnu.org/copyleft/gpl.html */