001 package nl.cwi.sen1.tide.tool; 002 003 import java.io.IOException; 004 import java.util.ArrayList; 005 import java.util.HashMap; 006 import java.util.Iterator; 007 import java.util.Map; 008 import java.util.Properties; 009 010 import javax.swing.JOptionPane; 011 012 import nl.cwi.sen1.gui.Studio; 013 import nl.cwi.sen1.gui.StudioImplWithPredefinedLayout; 014 import nl.cwi.sen1.tide.PreferenceSet; 015 import nl.cwi.sen1.tide.tool.ruleinspector.RuleInspector; 016 import nl.cwi.sen1.tide.tool.ruleinspector.RuleInspectorFactory; 017 import nl.cwi.sen1.tide.tool.support.DebugAdapter; 018 import nl.cwi.sen1.tide.tool.support.DebugProcess; 019 import nl.cwi.sen1.tide.tool.support.Rule; 020 021 public class ToolManager { 022 private Studio studio; 023 024 private Map<String, TideToolFactory> tideTools; 025 026 private java.util.List<String> processToolList; 027 028 private java.util.List<ProcessAction> processActionList; 029 030 private Map<String, ProcessToolFactory> processTools; 031 032 private java.util.List<String> adapterToolList; 033 034 private java.util.List<AdapterAction> adapterActionList; 035 036 private Map<String, AdapterToolFactory> adapterTools; 037 038 private Map<String, Map<Object, TideTool>> toolInstances; 039 040 private PreferenceSet preferences; 041 042 public ToolManager(Studio studio, Properties defaults) { 043 this.studio = studio; 044 045 tideTools = new HashMap<String, TideToolFactory>(); 046 047 processToolList = new ArrayList<String>(); 048 processActionList = new ArrayList<ProcessAction>(); 049 processTools = new HashMap<String, ProcessToolFactory>(); 050 051 adapterToolList = new ArrayList<String>(); 052 adapterActionList = new ArrayList<AdapterAction>(); 053 adapterTools = new HashMap<String, AdapterToolFactory>(); 054 055 toolInstances = new HashMap<String, Map<Object, TideTool>>(); 056 057 preferences = new PreferenceSet(defaults); 058 059 try { 060 preferences.loadPreferences(); 061 } catch (IOException e) { 062 // Preferences could not be loaded because ~/.tiderc does not exist 063 // probably 064 } 065 } 066 067 public PreferenceSet getPreferences() { 068 return preferences; 069 } 070 071 public TideTool getTool(String toolName, Object target) { 072 Map<Object, TideTool> tools = toolInstances.get(toolName); 073 if (tools != null) { 074 return tools.get(target); 075 } 076 077 return null; 078 } 079 080 public void putTool(String toolName, Object target, TideTool tool) { 081 tool.setName(toolName); 082 tool.setTarget(target); 083 Map<Object, TideTool> tools = toolInstances.get(toolName); 084 if (tools == null) { 085 tools = new HashMap<Object, TideTool>(); 086 toolInstances.put(toolName, tools); 087 } 088 089 tools.put(target, tool); 090 } 091 092 public void removeTool(TideTool tool) { 093 Map<Object, TideTool> tools = toolInstances.get(tool.getName()); 094 studio.removeComponent(tool); 095 tools.remove(tool.getTarget()); 096 } 097 098 public void registerTool(TideToolFactory factory) { 099 String name = factory.getName(); 100 tideTools.put(name, factory); 101 } 102 103 public void registerProcessTool(ProcessToolFactory factory) { 104 String name = factory.getName(); 105 processToolList.add(name); 106 processActionList.add(new ProcessAction(name, factory.getIcon(), this)); 107 processTools.put(name, factory); 108 } 109 110 public void registerAdapterTool(AdapterToolFactory factory) { 111 String name = factory.getName(); 112 113 adapterToolList.add(name); 114 adapterActionList.add(new AdapterAction(name, factory.getIcon(), this)); 115 adapterTools.put(name, factory); 116 } 117 118 public Iterator<String> processToolIterator() { 119 return processToolList.iterator(); 120 } 121 122 public Iterator<String> adapterToolIterator() { 123 return adapterToolList.iterator(); 124 } 125 126 public Iterator<ProcessAction> processActionIterator() { 127 return processActionList.iterator(); 128 } 129 130 public Iterator<AdapterAction> adapterActionIterator() { 131 return adapterActionList.iterator(); 132 } 133 134 public void displayError(String msg) { 135 JOptionPane.showMessageDialog(null, "Tide error", msg, 136 JOptionPane.ERROR_MESSAGE); 137 } 138 139 public TideTool launchTool(String toolName) { 140 TideTool tool = getTool(toolName, this); 141 if (tool == null) { 142 TideToolFactory factory = tideTools.get(toolName); 143 tool = factory.createTool(); 144 putTool(toolName, this, tool); 145 146 ((StudioImplWithPredefinedLayout) studio).addComponent(tool, 147 StudioImplWithPredefinedLayout.TOP_LEFT); 148 } 149 150 return tool; 151 152 } 153 154 public ProcessTool launchProcessTool(String toolName, DebugProcess process) { 155 ProcessTool tool = (ProcessTool) getTool(toolName, process); 156 if (tool == null) { 157 ProcessToolFactory factory = processTools.get(toolName); 158 tool = factory.createTool(process); 159 putTool(toolName, process, tool); 160 161 ((StudioImplWithPredefinedLayout) studio).addComponent(tool, 162 StudioImplWithPredefinedLayout.TOP_RIGHT); 163 } 164 165 return tool; 166 } 167 168 public AdapterTool launchAdapterTool(String toolName, DebugAdapter adapter) { 169 AdapterTool tool = (AdapterTool) getTool(toolName, adapter); 170 if (tool == null) { 171 AdapterToolFactory factory = adapterTools.get(toolName); 172 tool = factory.createTool(adapter); 173 putTool(toolName, adapter, tool); 174 ((StudioImplWithPredefinedLayout) studio).addComponent(tool, 175 StudioImplWithPredefinedLayout.BOTTOM_RIGHT); 176 } 177 return tool; 178 } 179 180 public void setCurrentProcess(DebugProcess process) { 181 Iterator<ProcessAction> iter = processActionIterator(); 182 while (iter.hasNext()) { 183 ProcessAction action = iter.next(); 184 action.setProcess(process); 185 } 186 } 187 188 public void setCurrentAdapter(DebugAdapter adapter) { 189 Iterator<AdapterAction> iter = adapterActionIterator(); 190 while (iter.hasNext()) { 191 AdapterAction action = iter.next(); 192 action.setAdapter(adapter); 193 } 194 } 195 196 public void editRule(DebugProcess process, Rule rule) { 197 String toolName = RuleInspectorFactory.NAME; 198 RuleInspector inspector = (RuleInspector) launchProcessTool(toolName, 199 process); 200 inspector.editRule(rule); 201 } 202 203 }