public void propertyChange(PropertyChangeEvent evt) { if ("progress".equalsIgnoreCase(evt.getPropertyName())) { int progress = (Integer) evt.getNewValue(); progressMonitor.setProgress(progress); String message = String.format("%d%% complete.\n", (progress == 1) ? 0 : progress); progressMonitor.setNote(message); if (progressMonitor.isCanceled()) { task.cancel(true); } else if (progress == 100) { mainWin.initGUI(); } } }
// executes in event dispatch thread public void propertyChange(PropertyChangeEvent event) { // if the operation is finished or has been canceled by // the user, take appropriate action if (progressMonitor.isCanceled()) { task.cancel(true); } else if (event.getPropertyName().equals("progress")) { // get the % complete from the progress event // and set it on the progress monitor int progress = ((Integer) event.getNewValue()).intValue(); progressMonitor.setProgress(progress); if (progress >= 100) { progressMonitor.close(); } } }
public boolean stop() { return worker != null ? worker.cancel(true) : false; }
@Override public void shutdown() { chartUpdater.cancel(true); removeAll(); super.shutdown(); }
/** stop currently running threads if any (e.g. when changing a case) */ synchronized void stopAll() { // stop queue worker if (queueWorker != null) { queueWorker.cancel(true); queueWorker = null; } // empty queues scheduler.getFileScheduler().empty(); scheduler.getDataSourceScheduler().empty(); // stop module workers if (abstractFileIngester != null) { // send signals to all file modules for (IngestModuleAbstractFile s : this.abstractFileModules) { if (isModuleRunning(s)) { try { s.stop(); } catch (Exception e) { logger.log( Level.WARNING, "Unexpected exception while stopping module: " + s.getName(), e); } } } // stop fs ingester thread boolean cancelled = abstractFileIngester.cancel(true); if (!cancelled) { logger.log(Level.INFO, "Unable to cancel file ingest worker, likely already stopped"); } abstractFileIngester = null; } List<IngestDataSourceThread> toStop = new ArrayList<IngestDataSourceThread>(); toStop.addAll(dataSourceIngesters); for (IngestDataSourceThread dataSourceWorker : toStop) { IngestModuleDataSource s = dataSourceWorker.getModule(); // stop the worker thread if thread is running boolean cancelled = dataSourceWorker.cancel(true); if (!cancelled) { logger.log( Level.INFO, "Unable to cancel data source ingest worker for module: " + dataSourceWorker.getModule().getName() + " data source: " + dataSourceWorker.getContent().getName()); } // stop notification to module to cleanup resources if (isModuleRunning(s)) { try { dataSourceWorker.getModule().stop(); } catch (Exception e) { logger.log(Level.WARNING, "Exception while stopping module: " + s.getName(), e); } } } logger.log(Level.INFO, "stopped all"); }
@Override public void actionPerformed(final ActionEvent e) { if (workerIsWorking) { if (e.getSource().getClass().equals(JButton.class)) { JButton sourceButton = (JButton) e.getSource(); sourceButton.setText("TabuSearch-Heuristic"); } worker.cancel(false); // cancel the worker workerIsWorking = false; } else { if (e.getSource().getClass().equals(JButton.class)) { JButton sourceButton = (JButton) e.getSource(); sourceButton.setText("Stop the heuristic"); } worker = new SwingWorker<Solution, IntermediateSolution>() { @Override protected Solution doInBackground() throws Exception { MetaSolverInterface solver = new TabuSearchHeuristic() { @Override public void publishSolution(IntermediateSolution intermediateSolution) { publish(intermediateSolution); } @Override public boolean isExternallyCancelled() { return worker.isCancelled(); } }; return solver.improve(solutionPanel.getSolution(), new VrpConfiguration()); } @Override protected void process(List<IntermediateSolution> chunks) { for (IntermediateSolution is : chunks) { if (is.isBestSolution()) { solutionPanel.setSolution(is.getSolution()); solutionPanel.repaint(); } } } protected void done() { if (e.getSource().getClass().equals(JButton.class)) { JButton sourceButton = (JButton) e.getSource(); sourceButton.setText("TabuSearch-Heuristic"); } try { Solution s = get(); solutionPanel.setSolution(s); s.printSolution(); for (Tour t : s.getTours()) { System.out.print( "expected recourse: " + t.getExpectedRecourseCost() + " for customers "); for (Customer c : t.getCustomers()) { System.out.print(c.getCustomerNo() + " "); } System.out.println(); } solutionPanel.repaint(); } catch (InterruptedException e1) { logPanel.log(e1.toString()); } catch (ExecutionException e1) { logPanel.log(e1.toString()); } catch (CancellationException ce) { logPanel.log("Cancelled by user"); } } }; worker.execute(); workerIsWorking = true; } }
@Override public ModuleAction getModuleAction(final Search search) throws ConnectException { if (search.isCommand("weather")) { try { worker.cancel(true); } catch (NullPointerException | CancellationException e) { // Do nothing. It is OK if the worker is not initialized or executed, yet. } // Set up the worker, that will get the necessary data in the background. worker = new SwingWorker<ModuleAction, ModuleAction>() { private String icon; @Override protected ModuleAction doInBackground() throws Exception { String location = search.getParam(0); if (location.equals("")) location = defaultLocation; if (location.split(",").length == 1) { if (search.getParam(1) != null) location += "," + search.getParam(1); else location += ",de"; } String json = OpenWeatherMapBridge.getJSON(location); jsonParser = new JSONParser(json); // Get and format the temperature. float temp = Float.valueOf(jsonParser.get("main.temp")); temp = Math.round(temp); tempFormated = String.format("%s", temp); if (temp == (int) temp) tempFormated = String.format("%s", (int) temp); String text = jsonParser.get("name") + ": <b>" + tempFormated + "\u00B0C</b>"; icon = (jsonParser.getArrayList("weather").get(0).get("icon")); icon = icon.substring(0, 2) + ".png"; return new ModuleAction(KEY, text, new ImageIcon("res/weather-icons/white/" + icon)) { @Override public ModuleWindow getModuleWindow(Search search) { final String city = search.getParam(0).split(",")[0]; ModuleWindow window = new ModuleWindow("Weather " + city); JPanel container = new JPanel(); container.setBackground(Color.WHITE); JLabel iconLabel = new JLabel(new ImageIcon("res/weather-icons/" + icon)); container.add(iconLabel); JPanel panelTable = new JPanel(); GridLayout gridLayout = new GridLayout(0, 2); gridLayout.setHgap(50); gridLayout.setVgap(10); panelTable.setLayout(gridLayout); panelTable.setBackground(Color.WHITE); panelTable.add(new Label(jsonParser.get("name"))); // Creates empty space (needed since there is no real adressing using GridLayout) panelTable.add(new Label("")); panelTable.add(new Label(tempFormated + "\u00B0C", true, 60)); // Creates empty space (needed since there is no real addressing using GridLayout) panelTable.add(new Label()); panelTable.add(new Label("Humidity")); panelTable.add(new Label(jsonParser.get("main.humidity") + "%", true)); panelTable.add(new Label(("Windspeed"))); panelTable.add(new Label(jsonParser.get("wind.speed") + "mps", true)); panelTable.add(new Label("Cloudiness")); panelTable.add(new Label(jsonParser.get("clouds.all") + "%", true)); String rain = jsonParser.get("rain.3h"); if (rain != null) { panelTable.add(new Label("Rain")); panelTable.add(new Label(rain + "mm per 3 hours")); } container.add(panelTable); window.add(container); return window; } }; } @Override protected void done() { try { getMainController().updateModule(get()); } catch (InterruptedException | ExecutionException e) { getMainController() .updateModule( new WarningModuleAction( KEY, "No known city found.", new ImageIcon("res/weather-icons/weather-icon.png"))); try { getModuleAction(search); } catch (ConnectException e1) { e1.printStackTrace(); } } super.done(); } }; worker.execute(); return new LoadingModuleAction(KEY, "Loading weather..."); } return null; }