001 /** 002 * planningtool - A Planning Tool with Critiquing Support. 003 * 004 * Copyright (C) 2006 olale 005 006 * This program is free software; you can redistribute it and/or 007 * modify it under the terms of the GNU General Public License 008 * as published by the Free Software Foundation; either version 2 009 * of the License, or (at your option) any later version. 010 011 * This program is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 014 * GNU General Public License for more details. 015 016 * You should have received a copy of the GNU General Public License 017 * along with this program; if not, write to the Free Software 018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 019 020 * Contact information: 021 * E-mail: olale@ida.liu.se 022 * olale@lysator.liu.se 023 */ 024 package se.liu.ida.critiquer.scenarios.standard; 025 026 import java.io.PrintStream; 027 import java.lang.reflect.Constructor; 028 import java.lang.reflect.InvocationTargetException; 029 import java.util.ArrayList; 030 import java.util.HashMap; 031 032 import javax.swing.JFrame; 033 import javax.swing.JOptionPane; 034 035 import se.liu.ida.critiquer.activities.Activity; 036 import se.liu.ida.critiquer.activities.ActivityFactory; 037 import se.liu.ida.critiquer.activities.ToplevelActivity; 038 import se.liu.ida.critiquer.constraints.ConstraintFactory; 039 040 public class StandardActivityFactory implements ActivityFactory { 041 042 private interface ActivityCreator { 043 044 Activity createActivity(); 045 046 } 047 048 private HashMap<String, ActivityCreator> activityTemplates; 049 050 public ArrayList<Class<? extends Activity>> getActivityClasses() { 051 ArrayList<Class<? extends Activity>> activityClasses = new ArrayList<Class<? extends Activity>>(); 052 activityClasses.add(ToplevelActivity.class); 053 activityClasses.add(DefaultActivity.class); 054 activityClasses.add(DispatchActivity.class); 055 activityClasses.add(FirstAidActivity.class); 056 activityClasses.add(RecoverActivity.class); 057 activityClasses.add(RoadBlockActivity.class); 058 return activityClasses; 059 } 060 061 public StandardActivityFactory() { 062 activityTemplates = new HashMap<String, ActivityCreator>(); 063 064 activityTemplates.put("Rescue", new ActivityCreator() { 065 066 public Activity createActivity() { 067 return createRescueActivity(); 068 } 069 070 }); 071 activityTemplates.put("Block roads", new ActivityCreator() { 072 073 public Activity createActivity() { 074 return createRoadBlockActivity(); 075 } 076 077 }); 078 } 079 080 public Activity selectActivityType(JFrame parentFrame) { 081 String message = "Select activity type"; 082 String selectedKey = (String) JOptionPane.showInputDialog(parentFrame, 083 message, 084 message, 085 JOptionPane.PLAIN_MESSAGE, 086 null, 087 activityTemplates.keySet().toArray(), 088 null); 089 if (selectedKey != null) { 090 return activityTemplates.get(selectedKey).createActivity(); 091 } else { 092 return null; 093 } 094 } 095 096 private Activity createRoadBlockActivity() { 097 Activity roadBlockActivity = new DefaultActivity(null); 098 new DispatchActivity(roadBlockActivity); 099 new RoadBlockActivity(roadBlockActivity); 100 return roadBlockActivity; 101 } 102 103 private Activity createRescueActivity() { 104 Activity rescueActivity = new DefaultActivity(null); 105 new DispatchActivity(rescueActivity); 106 new FirstAidActivity(rescueActivity); 107 new RecoverActivity(rescueActivity); 108 return rescueActivity; 109 } 110 111 /** 112 * Create an activity using the constructor which takes only a parent 113 * activity and pass <code>null</code> as reference to the parent. 114 * 115 */ 116 @SuppressWarnings("unchecked") 117 public <T extends Activity> T createActivity(Class<T> activityClass) { 118 T activity = null; 119 PrintStream debug = System.err; 120 if (Activity.class.isAssignableFrom(activityClass)) { 121 try { 122 Constructor constructor = activityClass.getConstructor(new Class[] { Activity.class }); 123 activity = (T) constructor.newInstance(new Object[] { null }); 124 ConstraintFactory.connectStartToEnd(activity); 125 } catch (SecurityException e) { 126 // TODO Auto-generated catch block 127 e.printStackTrace(); 128 } catch (IllegalArgumentException e) { 129 // TODO Auto-generated catch block 130 e.printStackTrace(); 131 } catch (NoSuchMethodException e) { 132 // TODO Auto-generated catch block 133 e.printStackTrace(); 134 } catch (InstantiationException e) { 135 // TODO Auto-generated catch block 136 e.printStackTrace(); 137 } catch (IllegalAccessException e) { 138 // TODO Auto-generated catch block 139 e.printStackTrace(); 140 } catch (InvocationTargetException e) { 141 debug.println("ActivityNode: Constructor for " + activityClass.getSimpleName() 142 + " threw exception " 143 + e.getCause().getMessage()); 144 debug.println("Stack trace of cause: "); 145 e.getCause().printStackTrace(debug); 146 147 } 148 } else { 149 debug.println("Trying to initialize non-activity class: " + activityClass.getSimpleName()); 150 } 151 return activity; 152 } 153 154 }