private ProgressMonitorInputStream getMonitorableStream(InputStream stream, String message) { final ProgressMonitorInputStream pmis = new ProgressMonitorInputStream(this, message, stream); ProgressMonitor progress = pmis.getProgressMonitor(); progress.setMillisToDecideToPopup(1); progress.setMillisToPopup(1); return pmis; }
/** * Test a synapse on the specified spiking protocol or a series of spiking protocols derived from * initial and final protocols by interpolation over one or two dimensions. The synapse is tested * over all specified parameter configurations. * * @param synapse The SynapseCollection containing the synapse to test (the first synapse is * used). The first configuration (at index 0) in the collection will be replaced with each * configuration specified by the <em>configurations</em> argument. * @param configurationLabels An array containing the labels for each parameter configuration to * test on. These are used to label the data sets in the returned TestResults. * @param configurations An array containing the parameter configurations to test on. * @param timeResolution The time resolution to use in the simulation, see {@link * com.ojcoleman.bain.NeuralNetwork} * @param period The period of the spike pattern in seconds. * @param repetitions The number of times to apply the spike pattern. * @param patterns Array containing spike patterns, in the form [initial, dim 1, dim 2][pre, * post][spike number] = spike time. The [spike number] array contains the times (s) of each * spike, relative to the beginning of the pattern. See {@link * com.ojcoleman.bain.neuron.spiking.FixedProtocolNeuronCollection}. * @param refSpikeIndexes Array specifying indexes of the two spikes to use as timing variation * references for each variation dimension, in the form [dim 1, dim 2][reference spike, * relative spike] = spike index. * @param refSpikePreOrPost Array specifying whether the timing variation reference spikes * specified by refSpikeIndexes belong to the pre- or post-synaptic neurons, in the form [dim * 1, dim 2][base spike, relative spike] = Constants.PRE or Constants.POST. * @param logSpikesAndStateVariables Whether to record pre- and post-synaptic spikes and any state * variables exposed by the synapse model in the test results. * @param progressMonitor If not null, this will be updated to display the progress of the test. * @return For a single spike protocol, a TestResults object with type {@link TYPE#STDP} * consisting of series labelled "Time" and "Efficacy" and if logSpikesAndStateVariables == * true then also "Pre-synaptic spikes", "Post-synaptic spikes" and any state variables * exposed by the synapse model. For a protocol varied over one dimension, a TestResults * object with type {@link TYPE#STDP_1D} consisting of series labelled "Time delta" and * "Efficacy". For a protocol varied over two dimensions, a TestResults object with type * {@link TYPE#STDP_2D} consisting of series labelled "Time delta 1", "Time delta 2" and * "Efficacy". */ public static TestResults[] testPattern( SynapseCollection<? extends ComponentConfiguration> synapse, String[] configurationLabels, ComponentConfiguration[] configurations, int timeResolution, double period, int repetitions, double[][][] patterns, int[][] refSpikeIndexes, int[][] refSpikePreOrPost, boolean logSpikesAndStateVariables, ProgressMonitor progressMonitor) throws IllegalArgumentException { int configCount = configurationLabels.length; TestResults[] results = new TestResults[configCount]; if (synapse.getConfigurationCount() == 0) { synapse.addConfiguration(configurations[0]); } ProgressMonitor progressMonitorSub = null; if (progressMonitor != null) { progressMonitor.setMinimum(0); progressMonitor.setMaximum(configCount); progressMonitor.setMillisToDecideToPopup(0); progressMonitorSub = new ProgressMonitor(null, null, "Performing test...", 0, 0); } for (int c = 0; c < configCount; c++) { if (progressMonitor != null) { progressMonitor.setProgress(c); progressMonitor.setNote("Testing " + configurationLabels[c]); } synapse.setConfiguration(0, configurations[c]); results[c] = testPattern( synapse, timeResolution, period, repetitions, patterns, refSpikeIndexes, refSpikePreOrPost, logSpikesAndStateVariables, progressMonitorSub); results[c].setProperty("label", configurationLabels[c]); } if (progressMonitorSub != null) { progressMonitorSub.close(); } return results; }
/** * Read word list from file with name WORDLISTFILENAME, and pass a Set containing those words to * the computer player to intialize its lexicon. */ private void initLexicon(InputStream stream) { ProgressMonitorInputStream pmis; ProgressMonitor progress = null; pmis = new ProgressMonitorInputStream(this, "reading words", stream); progress = pmis.getProgressMonitor(); progress.setMillisToDecideToPopup(10); Scanner s = new Scanner(pmis); myLexicon.load(s); try { pmis.close(); } catch (IOException e) { JOptionPane.showMessageDialog( null, "Error Closing Stream", "Error", JOptionPane.ERROR_MESSAGE); } }
private ProgressMonitorInputStream getMonitorableStream(File file, String message) { try { FileInputStream stream = new FileInputStream(file); if (stream == null) { System.out.println("null on " + file.getCanonicalPath()); } final ProgressMonitorInputStream pmis = new ProgressMonitorInputStream(this, message, stream); ProgressMonitor progress = pmis.getProgressMonitor(); progress.setMillisToDecideToPopup(1); progress.setMillisToPopup(1); return pmis; } catch (IOException e) { showError("could not open " + file.getName()); e.printStackTrace(); return null; } }
public void readPlugins(String base, JFrame parent) throws Exception { String baseURL = base; ProgressMonitor monitor; if (base == null) { baseURL = TiledConfiguration.root().get("pluginsDir", "plugins"); } File dir = new File(baseURL); if (!dir.exists() || !dir.canRead()) { // FIXME: removed for webstart // throw new Exception( // "Could not open directory for reading plugins: " + // baseURL); return; } int total = 0; File[] files = dir.listFiles(); for (File file : files) { String aPath = file.getAbsolutePath(); if (aPath.endsWith(".jar")) { total++; } } // Start the progress monitor monitor = new ProgressMonitor(parent, "Loading plugins", "", 0, total - 1); monitor.setProgress(0); monitor.setMillisToPopup(0); monitor.setMillisToDecideToPopup(0); for (int i = 0; i < files.length; i++) { String aPath = files[i].getAbsolutePath(); String aName = aPath.substring(aPath.lastIndexOf(File.separatorChar) + 1); // Skip non-jar files. if (!aPath.endsWith(".jar")) { continue; } try { monitor.setNote("Reading " + aName + "..."); JarFile jf = new JarFile(files[i]); monitor.setProgress(i); if (jf.getManifest() == null) continue; String readerClassName = jf.getManifest().getMainAttributes().getValue("Reader-Class"); String writerClassName = jf.getManifest().getMainAttributes().getValue("Writer-Class"); Class<Object> readerClass = null; Class<Object> writerClass = null; // Verify that the jar has the necessary files to be a // plugin if (readerClassName == null && writerClassName == null) { continue; } monitor.setNote("Loading " + aName + "..."); addURL(new File(aPath).toURI().toURL()); if (readerClassName != null) { JarEntry reader = jf.getJarEntry(readerClassName.replace('.', '/') + ".class"); if (reader != null) { readerClass = loadFromJar(jf, reader, readerClassName); } else System.err.println( "Manifest entry " + readerClassName + " does not match any class in the jar."); } if (writerClassName != null) { JarEntry writer = jf.getJarEntry(writerClassName.replace('.', '/') + ".class"); if (writer != null) { writerClass = loadFromJar(jf, writer, writerClassName); } else System.err.println( "Manifest entry " + writerClassName + " does not match any class in the jar."); } boolean bPlugin = false; if (isReader(readerClass)) { bPlugin = true; } if (isWriter(writerClass)) { bPlugin = true; } if (bPlugin) { if (readerClass != null) _add(readerClass); if (writerClass != null) _add(writerClass); // System.out.println( // "Added " + files[i].getCanonicalPath()); } } catch (IOException e) { e.printStackTrace(); } } }
/** @param evt */ public void actionPerformed(ActionEvent evt) { // Close if (evt.getSource() == close) { dispose(); return; } final String newPassphrase = new String(keygen.getNewPassphrase()).trim(); final String oldPassphrase = new String(keygen.getOldPassphrase()).trim(); if ((keygen.getAction() == KeygenPanel.GENERATE_KEY_PAIR) || (keygen.getAction() == KeygenPanel.CHANGE_PASSPHRASE)) { if (newPassphrase.length() == 0) { if (JOptionPane.showConfirmDialog( this, "Passphrase is empty. Are you sure?", "Empty Passphrase", JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION) { return; } } } final File inputFile = new File(keygen.getInputFilename()); final File outputFile = new File(keygen.getOutputFilename()); final File publicFile = new File(keygen.getOutputFilename() + ".pub"); // Check if the output file was supplied if ((keygen.getAction() == KeygenPanel.CONVERT_IETF_SECSH_TO_OPENSSH) || (keygen.getAction() == KeygenPanel.CONVERT_OPENSSH_TO_IETF_SECSH) || (keygen.getAction() == KeygenPanel.GENERATE_KEY_PAIR)) { if (keygen.getOutputFilename().length() == 0) { JOptionPane.showMessageDialog( this, "No Output file supplied.", "Error", JOptionPane.ERROR_MESSAGE); return; } // Check if the output file exists, and confirm overwrit if it does if (outputFile.exists()) { if (JOptionPane.showConfirmDialog( this, "Output file " + outputFile.getName() + " exists. Are you sure?", "File exists", JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION) { return; } } // Make sure the output file is writeable if (outputFile.exists() && !outputFile.canWrite()) { JOptionPane.showMessageDialog( this, "Output file " + outputFile.getName() + " can not be written.", "Unwriteable file", JOptionPane.ERROR_MESSAGE); return; } } // If this is a conversion, check the input file is provided if ((keygen.getAction() == KeygenPanel.CONVERT_IETF_SECSH_TO_OPENSSH) || (keygen.getAction() == KeygenPanel.CONVERT_OPENSSH_TO_IETF_SECSH)) { if (keygen.getInputFilename().length() == 0) { JOptionPane.showMessageDialog( this, "No Input file supplied.", "Error", JOptionPane.ERROR_MESSAGE); return; } } else if (keygen.getAction() == KeygenPanel.GENERATE_KEY_PAIR) { // Check if the public key file is writeable. We should test if it exists // as thats just too many questions for the user if (publicFile.exists() && !publicFile.canWrite()) { JOptionPane.showMessageDialog( this, "Public key file " + publicFile.getName() + " can not be written.", "Unwriteable file", JOptionPane.ERROR_MESSAGE); return; } } // Now generate the key final ProgressMonitor monitor = new ProgressMonitor(this, "Generating keys", "Generating", 0, 100); monitor.setMillisToDecideToPopup(0); monitor.setMillisToPopup(0); Runnable r = new Runnable() { public void run() { try { if (keygen.getAction() == KeygenPanel.CHANGE_PASSPHRASE) { monitor.setNote("Changing passphrase"); SshKeyGenerator.changePassphrase(inputFile, oldPassphrase, newPassphrase); monitor.setNote("Complete"); JOptionPane.showMessageDialog( Main.this, "Passphrase changed", "Passphrase changed", JOptionPane.INFORMATION_MESSAGE); } else if (keygen.getAction() == KeygenPanel.CONVERT_IETF_SECSH_TO_OPENSSH) { monitor.setNote("Converting key file"); writeString( outputFile, SshKeyGenerator.convertPublicKeyFile(inputFile, new OpenSSHPublicKeyFormat())); monitor.setNote("Complete"); JOptionPane.showMessageDialog( Main.this, "Key converted", "Key converted", JOptionPane.INFORMATION_MESSAGE); } else if (keygen.getAction() == KeygenPanel.CONVERT_OPENSSH_TO_IETF_SECSH) { monitor.setNote("Converting key file"); writeString( outputFile, SshKeyGenerator.convertPublicKeyFile(inputFile, new SECSHPublicKeyFormat())); monitor.setNote("Complete"); JOptionPane.showMessageDialog( Main.this, "Key converted", "Key converted", JOptionPane.INFORMATION_MESSAGE); } else { monitor.setNote("Creating generator"); SshKeyGenerator generator = new SshKeyGenerator(); monitor.setNote("Generating"); String username = System.getProperty("user.name"); generator.generateKeyPair( keygen.getType(), keygen.getBits(), outputFile.getAbsolutePath(), username, newPassphrase); monitor.setNote("Complete"); JOptionPane.showMessageDialog( Main.this, "Key generated to " + outputFile.getName(), "Complete", JOptionPane.INFORMATION_MESSAGE); } } catch (Exception e) { JOptionPane.showMessageDialog( Main.this, e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE); } finally { monitor.close(); } } }; Thread t = new Thread(r); t.start(); }