set specified external tools configs read only

+ automatic formatting
This commit is contained in:
fros4943 2007-08-22 11:23:50 +00:00
parent 3bb9fda749
commit b67baa7dfc

View File

@ -24,7 +24,7 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* $Id: GUI.java,v 1.55 2007/05/30 10:51:14 fros4943 Exp $
* $Id: GUI.java,v 1.56 2007/08/22 11:23:50 fros4943 Exp $
*/
package se.sics.cooja;
@ -84,6 +84,7 @@ public class GUI {
public static final String EXTERNAL_TOOLS_USER_SETTINGS_FILENAME = ".cooja.user.properties";
private static File externalToolsUserSettingsFile =
new File(System.getProperty("user.home"), EXTERNAL_TOOLS_USER_SETTINGS_FILENAME);
private static boolean externalToolsUserSettingsFileReadOnly = false;
private static String specifiedContikiPath = null;
@ -107,11 +108,13 @@ public class GUI {
*/
public static final FileFilter SAVED_SIMULATIONS_FILES = new FileFilter() {
public boolean accept(File file) {
if (file.isDirectory())
if (file.isDirectory()) {
return true;
}
if (file.getName().endsWith(".csc"))
if (file.getName().endsWith(".csc")) {
return true;
}
return false;
}
@ -208,10 +211,12 @@ public class GUI {
myGUI = this;
mySimulation = null;
myDesktopPane = desktop;
if (menuPlugins == null)
if (menuPlugins == null) {
menuPlugins = new JMenu("Plugins");
if (menuMotePluginClasses == null)
}
if (menuMotePluginClasses == null) {
menuMotePluginClasses = new Vector<Class<? extends Plugin>>();
}
// Load default and overwrite with user settings (if any)
loadExternalToolsDefaultSettings();
@ -328,10 +333,11 @@ public class GUI {
String newHistoryConfig = null;
for (String path: newHistory) {
if (newHistoryConfig == null)
if (newHistoryConfig == null) {
newHistoryConfig = path;
else
} else {
newHistoryConfig += ";" + path;
}
}
setExternalToolsSetting("SIMCFG_HISTORY", newHistoryConfig);
@ -553,10 +559,11 @@ public class GUI {
menu.add(menuItem);
// Plugins menu
if (menuPlugins == null)
if (menuPlugins == null) {
menuPlugins = new JMenu("Plugins");
else
} else {
menuPlugins.setText("Plugins");
}
menuPlugins.setMnemonic(KeyEvent.VK_P);
menuBar.add(menuPlugins);
@ -583,8 +590,9 @@ public class GUI {
menu.add(menuItem);
// Mote plugins popup menu (not available via menu bar)
if (menuMotePluginClasses == null)
if (menuMotePluginClasses == null) {
menuMotePluginClasses = new Vector<Class<? extends Plugin>>();
}
return menuBar;
}
@ -598,8 +606,9 @@ public class GUI {
// Create and set up the window.
frame = new JFrame("COOJA Simulator");
if (maxSize != null)
if (maxSize != null) {
frame.setMaximizedBounds(maxSize);
}
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
// Add menu bar
@ -712,8 +721,9 @@ public class GUI {
frame.setLocationRelativeTo(null);
// Set manual Contiki path if specified
if (contikiPath != null)
if (contikiPath != null) {
setExternalToolsSetting("PATH_CONTIKI", contikiPath);
}
// Parse project directories and create config
if (projectDirs == null) {
@ -747,8 +757,9 @@ public class GUI {
// Check file permissions and paths
logger.info("> Checking paths and file permissions");
if (moteTypeID == null)
if (moteTypeID == null) {
moteTypeID = "mtype1";
}
File contikiBaseDir = new File(getExternalToolsSetting("PATH_CONTIKI"));
File contikiCoreDir = new File(contikiBaseDir,
getExternalToolsSetting("PATH_COOJA_CORE_RELATIVE"));
@ -758,12 +769,15 @@ public class GUI {
+ ContikiMoteType.mapSuffix);
File depFile = new File(ContikiMoteType.tempOutputDirectory, moteTypeID
+ ContikiMoteType.dependSuffix);
if (libFile.exists())
if (libFile.exists()) {
libFile.delete();
if (depFile.exists())
}
if (depFile.exists()) {
depFile.delete();
if (mapFile.exists())
}
if (mapFile.exists()) {
mapFile.delete();
}
if (libFile.exists()) {
logger.fatal(">> Can't delete output file, aborting: " + libFile);
return false;
@ -804,9 +818,10 @@ public class GUI {
logger.info("> Setting up compilation arguments");
Vector<File> filesToCompile = new Vector<File>();
filesToCompile.add(mainProcessFile); // main process file
for (String projectDir : projectDirs)
for (String projectDir : projectDirs) {
// project directories
filesToCompile.add(new File(projectDir));
}
String[] projectSources = // project config sources
gui.getProjectConfig().getStringArrayValue(ContikiMoteType.class,
"C_SOURCES");
@ -833,10 +848,11 @@ public class GUI {
sensors = new Vector<String>();
Vector<String[]> scannedSensorInfo = ContikiMoteTypeDialog
.scanForSensors(contikiCoreDir);
for (String projectDir : projectDirs)
for (String projectDir : projectDirs) {
// project directories
scannedSensorInfo.addAll(ContikiMoteTypeDialog.scanForSensors(new File(
projectDir)));
}
for (String[] sensorInfo : scannedSensorInfo) {
// logger.info(">> Found and added: " + sensorInfo[1] + " (" +
@ -851,10 +867,11 @@ public class GUI {
coreInterfaces = new Vector<String>();
Vector<String[]> scannedCoreInterfaceInfo = ContikiMoteTypeDialog
.scanForInterfaces(contikiCoreDir);
for (String projectDir : projectDirs)
for (String projectDir : projectDirs) {
// project directories
scannedCoreInterfaceInfo.addAll(ContikiMoteTypeDialog
.scanForInterfaces(new File(projectDir)));
}
for (String[] coreInterfaceInfo : scannedCoreInterfaceInfo) {
// logger.info(">> Found and added: " + coreInterfaceInfo[1] + " (" +
@ -888,10 +905,11 @@ public class GUI {
Vector<String> autostartProcesses = new Vector<String>();
Vector<String[]> scannedProcessInfo = ContikiMoteTypeDialog
.scanForProcesses(contikiCoreDir);
for (String projectDir : projectDirs)
for (String projectDir : projectDirs) {
// project directories
scannedProcessInfo.addAll(ContikiMoteTypeDialog
.scanForProcesses(new File(projectDir)));
}
for (String[] processInfo : scannedProcessInfo) {
if (processInfo[0].equals(mainProcessFile.getName())) {
@ -906,8 +924,9 @@ public class GUI {
// processes");
Vector<String> autostarters = ContikiMoteTypeDialog
.parseAutostartProcesses(mainProcessFile);
if (autostarters != null)
if (autostarters != null) {
autostartProcesses.addAll(autostarters);
}
} catch (Exception e) {
logger
.fatal(">>> Error when parsing autostart processes, aborting: "
@ -944,8 +963,9 @@ public class GUI {
// Generate Contiki main source file
logger.info("> Generating Contiki main source file");
if (!ContikiMoteType.tempOutputDirectory.exists())
if (!ContikiMoteType.tempOutputDirectory.exists()) {
ContikiMoteType.tempOutputDirectory.mkdir();
}
if (!ContikiMoteType.tempOutputDirectory.exists()) {
logger.fatal(">> Could not create output directory: "
+ ContikiMoteType.tempOutputDirectory);
@ -969,8 +989,9 @@ public class GUI {
moteTypeID, contikiBaseDir, filesToCompile, false,
ContikiMoteType.CommunicationStack.UIP,
null, System.err);
if (!libFile.exists() || !depFile.exists() || !mapFile.exists())
if (!libFile.exists() || !depFile.exists() || !mapFile.exists()) {
compilationSucceded = false;
}
if (compilationSucceded) {
// logger.info(">> Compilation complete");
@ -1032,14 +1053,16 @@ public class GUI {
ContikiMote mote = (ContikiMote) moteType.generateMote(simulation);
// Set random position
if (mote.getInterfaces().getPosition() != null)
if (mote.getInterfaces().getPosition() != null) {
mote.getInterfaces().getPosition().setCoordinates(
random.nextDouble() * areaSideLength,
random.nextDouble() * areaSideLength, 0);
}
// Set unique mote ID's
if (mote.getInterfaces().getMoteID() != null)
if (mote.getInterfaces().getMoteID() != null) {
mote.getInterfaces().getMoteID().setMoteID(nextMoteID++);
}
// Set unique IP address
if (mote.getInterfaces().getIPAddress() != null) {
@ -1314,9 +1337,10 @@ public class GUI {
if (registerTemporaryPlugin(pluginClass)) {
// logger.info("Reregistered temporary plugin class: " +
// getDescriptionOf(pluginClass));
} else
} else {
logger.warn("Could not reregister temporary plugin class: "
+ getDescriptionOf(pluginClass));
}
}
}
@ -1544,8 +1568,9 @@ public class GUI {
return null;
}
if (newPlugin == null)
if (newPlugin == null) {
return null;
}
// Add to active plugins list
startedPlugins.add(newPlugin);
@ -1580,12 +1605,14 @@ public class GUI {
* @return True if this plugin was registered ok, false otherwise
*/
public boolean registerTemporaryPlugin(Class<? extends Plugin> newPluginClass) {
if (pluginClasses.contains(newPluginClass))
if (pluginClasses.contains(newPluginClass)) {
return false;
}
boolean returnVal = registerPlugin(newPluginClass, true);
if (!returnVal)
if (!returnVal) {
return false;
}
pluginClassesTemporary.add(newPluginClass);
return true;
@ -1603,18 +1630,22 @@ public class GUI {
for (Component menuComponent : menuPlugins.getMenuComponents()) {
if (menuComponent.getClass().isAssignableFrom(JMenuItem.class)) {
JMenuItem menuItem = (JMenuItem) menuComponent;
if (menuItem.getClientProperty("class").equals(pluginClass))
if (menuItem.getClientProperty("class").equals(pluginClass)) {
menuPlugins.remove(menuItem);
}
}
}
if (menuMotePluginClasses.contains(pluginClass))
if (menuMotePluginClasses.contains(pluginClass)) {
menuMotePluginClasses.remove(pluginClass);
}
// Remove from plugin vectors (including temporary)
if (pluginClasses.contains(pluginClass))
if (pluginClasses.contains(pluginClass)) {
pluginClasses.remove(pluginClass);
if (pluginClassesTemporary.contains(pluginClass))
}
if (pluginClassesTemporary.contains(pluginClass)) {
pluginClassesTemporary.remove(pluginClass);
}
}
/**
@ -1734,17 +1765,19 @@ public class GUI {
mySimulation = sim;
// Set frame title
if (frame != null)
if (frame != null) {
frame.setTitle("COOJA Simulator" + " - " + sim.getTitle());
}
// Open standard plugins (if none opened already)
if (startedPlugins.size() == 0)
if (startedPlugins.size() == 0) {
for (Class<? extends Plugin> pluginClass : pluginClasses) {
int pluginType = pluginClass.getAnnotation(PluginType.class).value();
if (pluginType == PluginType.SIM_STANDARD_PLUGIN) {
startPlugin(pluginClass, this, sim, null);
}
}
}
}
/**
@ -1813,8 +1846,9 @@ public class GUI {
int pluginType = startedPlugin.getClass().getAnnotation(
PluginType.class).value();
if (pluginType != PluginType.COOJA_PLUGIN
&& pluginType != PluginType.COOJA_STANDARD_PLUGIN)
&& pluginType != PluginType.COOJA_STANDARD_PLUGIN) {
removePlugin((Plugin) startedPlugin, false);
}
}
// Delete simulation
@ -1921,14 +1955,16 @@ public class GUI {
} catch (UnsatisfiedLinkError e) {
showErrorDialog(frame, "Simulation load error", e, false);
if (progressDialog != null && progressDialog.isDisplayable())
if (progressDialog != null && progressDialog.isDisplayable()) {
progressDialog.dispose();
}
newSim = null;
} catch (SimulationCreationException e) {
showErrorDialog(frame, "Simulation load error", e, false);
if (progressDialog != null && progressDialog.isDisplayable())
if (progressDialog != null && progressDialog.isDisplayable()) {
progressDialog.dispose();
}
newSim = null;
}
if (newSim != null) {
@ -1970,12 +2006,13 @@ public class GUI {
progressDialog.setLocationRelativeTo(frame);
progressDialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
loadThread.start();
if (quick)
if (quick) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
progressDialog.setVisible(true);
}
});
}
}
/**
@ -2042,8 +2079,9 @@ public class GUI {
Element root = new Element("simconf");
root.addContent(simulationElement);
if (pluginsConfig != null)
if (pluginsConfig != null) {
root.addContent(pluginsConfig);
}
Document doc = new Document(root);
XMLOutputter outputter = new XMLOutputter();
@ -2166,8 +2204,9 @@ public class GUI {
if (!saveFile.exists() || saveFile.canWrite()) {
saveSimulationConfig(saveFile);
addToFileHistory(saveFile);
} else
} else {
logger.fatal("No write access to file");
}
} else {
logger.info("Save command cancelled by user...");
@ -2185,12 +2224,14 @@ public class GUI {
Vector<Mote> newMotes = AddMoteDialog.showDialog(frame, mySimulation,
moteType);
if (newMotes != null) {
for (Mote newMote : newMotes)
for (Mote newMote : newMotes) {
mySimulation.addMote(newMote);
}
}
} else
} else {
logger.warn("No simulation active");
}
}
/**
@ -2236,14 +2277,16 @@ public class GUI {
int n = JOptionPane.showOptionDialog(frame, "Sure you want to quit?",
"Close COOJA Simulator", JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE, null, options, s1);
if (n != JOptionPane.YES_OPTION)
if (n != JOptionPane.YES_OPTION) {
return;
}
}
// Clean up resources
Object[] plugins = startedPlugins.toArray();
for (Object plugin : plugins)
for (Object plugin : plugins) {
removePlugin((Plugin) plugin, false);
}
// Restore last frame size and position
if (frame != null) {
@ -2323,8 +2366,9 @@ public class GUI {
*/
public static void loadExternalToolsDefaultSettings() {
String filename = GUI.EXTERNAL_TOOLS_LINUX_SETTINGS_FILENAME;
if (System.getProperty("os.name").startsWith("Win"))
if (System.getProperty("os.name").startsWith("Win")) {
filename = GUI.EXTERNAL_TOOLS_WIN32_SETTINGS_FILENAME;
}
try {
InputStream in = GUI.class.getResourceAsStream(filename);
@ -2377,6 +2421,10 @@ public class GUI {
* Save external tools user settings to file.
*/
public static void saveExternalToolsUserSettings() {
if (externalToolsUserSettingsFileReadOnly) {
return;
}
try {
FileOutputStream out = new FileOutputStream(externalToolsUserSettingsFile);
@ -2458,12 +2506,14 @@ public class GUI {
ExternalToolsDialog.showDialog(frame);
} else if (e.getActionCommand().equals("close plugins")) {
Object[] plugins = startedPlugins.toArray();
for (Object plugin : plugins)
for (Object plugin : plugins) {
removePlugin((Plugin) plugin, false);
}
} else if (e.getActionCommand().equals("remove all motes")) {
if (getSimulation() != null) {
if (getSimulation().isRunning())
if (getSimulation().isRunning()) {
getSimulation().stopSimulation();
}
while (getSimulation().getMotesCount() > 0) {
getSimulation().removeMote(getSimulation().getMote(0));
@ -2481,8 +2531,9 @@ public class GUI {
.getSource()).getClientProperty("class");
Mote mote = (Mote) ((JMenuItem) e.getSource()).getClientProperty("mote");
startPlugin(pluginClass, myGUI, mySimulation, mote);
} else
} else {
logger.warn("Unhandled action: " + e.getActionCommand());
}
}
}
@ -2590,7 +2641,7 @@ public class GUI {
}
}
return new URLClassLoader((URL[]) urls.toArray(new URL[urls.size()]),
return new URLClassLoader(urls.toArray(new URL[urls.size()]),
parent);
}
@ -2640,14 +2691,14 @@ public class GUI {
}
// Parse general command arguments
for (int i = 0; i < args.length; i++) {
if (args[i].startsWith("-contiki=")) {
String arg = args[i].substring("-contiki=".length());
for (String element : args) {
if (element.startsWith("-contiki=")) {
String arg = element.substring("-contiki=".length());
GUI.specifiedContikiPath = arg;
}
if (args[i].startsWith("-external_tools_config=")) {
String arg = args[i].substring("-external_tools_config=".length());
if (element.startsWith("-external_tools_config=")) {
String arg = element.substring("-external_tools_config=".length());
File specifiedExternalToolsConfigFile = new File(arg);
if (!specifiedExternalToolsConfigFile.exists()) {
logger.fatal("Specified external tools configuration not found: " + specifiedExternalToolsConfigFile);
@ -2655,6 +2706,7 @@ public class GUI {
System.exit(1);
} else {
GUI.externalToolsUserSettingsFile = specifiedExternalToolsConfigFile;
GUI.externalToolsUserSettingsFileReadOnly = true;
}
}
}
@ -2685,29 +2737,33 @@ public class GUI {
String arg = args[i].substring("-projects=".length());
String[] argArray = arg.split(",");
projectDirs = new Vector<String>();
for (String argValue : argArray)
for (String argValue : argArray) {
projectDirs.add(argValue);
}
} else if (args[i].startsWith("-sensors=")) {
String arg = args[i].substring("-sensors=".length());
String[] argArray = arg.split(",");
sensors = new Vector<String>();
for (String argValue : argArray)
for (String argValue : argArray) {
sensors.add(argValue);
}
} else if (args[i].startsWith("-interfaces=")) {
String arg = args[i].substring("-interfaces=".length());
String[] argArray = arg.split(",");
coreInterfaces = new Vector<String>();
for (String argValue : argArray)
for (String argValue : argArray) {
coreInterfaces.add(argValue);
}
} else if (args[i].startsWith("-processes=")) {
String arg = args[i].substring("-processes=".length());
String[] argArray = arg.split(",");
userProcesses = new Vector<String>();
for (String argValue : argArray)
for (String argValue : argArray) {
userProcesses.add(argValue);
}
} else if (args[i].equals("-noautostartscan")) {
addAutostartProcesses = false;
@ -2740,8 +2796,9 @@ public class GUI {
boolean ok = quickStartSimulation(moteTypeID, projectDirs, sensors,
coreInterfaces, userProcesses, addAutostartProcesses, numberOfNodes,
areaSideLength, delayTime, startSimulation, filename, contikiPath);
if (!ok)
if (!ok) {
System.exit(1);
}
} else if (args.length > 0 && args[0].startsWith("-nogui")) {
@ -2879,8 +2936,9 @@ public class GUI {
// Create started plugins config
Collection<Element> pluginsConfig = getPluginsConfigXML();
if (pluginsConfig != null)
if (pluginsConfig != null) {
root.addContent(pluginsConfig);
}
// Create and write to document
Document doc = new Document(root);
@ -2914,8 +2972,9 @@ public class GUI {
// Ignore GUI plugins
if (pluginType == PluginType.COOJA_PLUGIN
|| pluginType == PluginType.COOJA_STANDARD_PLUGIN)
|| pluginType == PluginType.COOJA_STANDARD_PLUGIN) {
continue;
}
pluginElement = new Element("plugin");
pluginElement.setText(startedPlugin.getClass().getName());
@ -3114,8 +3173,9 @@ public class GUI {
String message = title;
// Create message
if (exception != null)
if (exception != null) {
message = exception.getMessage();
}
// Create stack trace message list
if (exception != null) {
@ -3140,14 +3200,11 @@ public class GUI {
// Create error dialog
final JDialog errorDialog;
if (parentComponent instanceof Dialog)
if (parentComponent instanceof Dialog) {
errorDialog = new JDialog((Dialog) parentComponent, title, true);
else if (parentComponent instanceof Frame)
} else if (parentComponent instanceof Frame) {
errorDialog = new JDialog((Frame) parentComponent, title, true);
// else if (parentComponent instanceof Window)
// errorDialog = new JDialog((Window) parentComponent, title,
// ModalityType.APPLICATION_MODAL);
else {
} else {
logger.fatal("Bad parent for error dialog");
errorDialog = new JDialog((Frame) null, title + " (Java stack trace)");
}
@ -3283,8 +3340,9 @@ public class GUI {
errorDialog.setLocationRelativeTo(parentComponent);
errorDialog.setVisible(true);
if (errorDialog.getTitle().equals("-RETRY-"))
if (errorDialog.getTitle().equals("-RETRY-")) {
return true;
}
return false;
}