List of usage examples for javax.swing SwingWorker SwingWorker
public SwingWorker()
From source file:op.controlling.PnlControlling.java
private JPanel createContentPanel4Nursing() { JPanel pnlContent = new JPanel(new VerticalLayout()); /***//from w w w . j ava2 s . co m * __ __ _ * \ \ / /__ _ _ _ __ __| |___ * \ \ /\ / / _ \| | | | '_ \ / _` / __| * \ V V / (_) | |_| | | | | (_| \__ \ * \_/\_/ \___/ \__,_|_| |_|\__,_|___/ * */ JPanel pnlWounds = new JPanel(new BorderLayout()); final JButton btnWounds = GUITools.createHyperlinkButton("opde.controlling.nursing.wounds", null, null); int woundsMonthsBack; try { woundsMonthsBack = Integer.parseInt(OPDE.getProps().getProperty("opde.controlling::woundsMonthsBack")); } catch (NumberFormatException nfe) { woundsMonthsBack = 7; } final JTextField txtWoundsMonthsBack = GUITools.createIntegerTextField(1, 12, woundsMonthsBack); txtWoundsMonthsBack.setToolTipText(SYSTools.xx("misc.msg.monthsback")); btnWounds.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { OPDE.getMainframe().setBlocked(true); SwingWorker worker = new SwingWorker() { @Override protected Object doInBackground() throws Exception { SYSPropsTools.storeProp("opde.controlling::woundsMonthsBack", txtWoundsMonthsBack.getText(), OPDE.getLogin().getUser()); SYSFilesTools.print( getWounds(Integer.parseInt(txtWoundsMonthsBack.getText()), progressClosure), false); return null; } @Override protected void done() { OPDE.getDisplayManager().setProgressBarMessage(null); OPDE.getMainframe().setBlocked(false); } }; worker.execute(); } }); pnlWounds.add(btnWounds, BorderLayout.WEST); pnlWounds.add(txtWoundsMonthsBack, BorderLayout.EAST); pnlContent.add(pnlWounds); /*** * ____ _ _ _____ _ * / ___| ___ ___(_) __ _| | |_ _(_)_ __ ___ ___ ___ * \___ \ / _ \ / __| |/ _` | | | | | | '_ ` _ \ / _ \/ __| * ___) | (_) | (__| | (_| | | | | | | | | | | | __/\__ \ * |____/ \___/ \___|_|\__,_|_| |_| |_|_| |_| |_|\___||___/ * */ JPanel pblSocialTimes = new JPanel(new BorderLayout()); final JButton btnSocialTimes = GUITools.createHyperlinkButton("opde.controlling.nursing.social", null, null); final JComboBox cmbSocialTimes = new JComboBox( SYSCalendar.createMonthList(new LocalDate().minusYears(1), new LocalDate())); btnSocialTimes.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { OPDE.getMainframe().setBlocked(true); SwingWorker worker = new SwingWorker() { @Override protected Object doInBackground() throws Exception { LocalDate month = (LocalDate) cmbSocialTimes.getSelectedItem(); SYSFilesTools.print(NReportTools.getTimes4SocialReports(month, progressClosure), false); return null; } @Override protected void done() { OPDE.getDisplayManager().setProgressBarMessage(null); OPDE.getMainframe().setBlocked(false); } }; worker.execute(); } }); cmbSocialTimes.setRenderer(new ListCellRenderer() { @Override public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { return new DefaultListCellRenderer().getListCellRendererComponent(list, monthFormatter.format(((LocalDate) value).toDate()), index, isSelected, cellHasFocus); } }); cmbSocialTimes.setSelectedIndex(cmbSocialTimes.getItemCount() - 2); pblSocialTimes.add(btnSocialTimes, BorderLayout.WEST); pblSocialTimes.add(cmbSocialTimes, BorderLayout.EAST); pnlContent.add(pblSocialTimes); return pnlContent; }
From source file:op.controlling.PnlControlling.java
private JPanel createContentPanel4Drugs() { JPanel pnlContent = new JPanel(new VerticalLayout()); /***/*from www . j a v a2 s . c o m*/ * ____ ____ _ _ _ _ _ * | _ \ _ __ _ _ __ _ / ___|___ _ __ | |_ _ __ ___ | | | | (_)___| |_ * | | | | '__| | | |/ _` | | | / _ \| '_ \| __| '__/ _ \| | | | | / __| __| * | |_| | | | |_| | (_| | | |__| (_) | | | | |_| | | (_) | | | |___| \__ \ |_ * |____/|_| \__,_|\__, | \____\___/|_| |_|\__|_| \___/|_| |_____|_|___/\__| * |___/ */ JPanel pnlDrugControl = new JPanel(new BorderLayout()); final JButton btnDrugControl = GUITools.createHyperlinkButton("opde.controlling.drugs.controllist", null, null); final JComboBox cmbStation = new JComboBox(StationTools.getAll4Combobox(false)); btnDrugControl.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { OPDE.getMainframe().setBlocked(true); SwingWorker worker = new SwingWorker() { @Override protected Object doInBackground() throws Exception { return MedStockTools.getListForMedControl((Station) cmbStation.getSelectedItem(), progressClosure); } @Override protected void done() { try { SYSFilesTools.print(get().toString(), true); } catch (ExecutionException ee) { OPDE.fatal(ee); } catch (InterruptedException ie) { // nop } OPDE.getDisplayManager().setProgressBarMessage(null); OPDE.getMainframe().setBlocked(false); } }; worker.execute(); } }); pnlDrugControl.add(btnDrugControl, BorderLayout.WEST); pnlDrugControl.add(cmbStation, BorderLayout.EAST); pnlContent.add(pnlDrugControl); /*** * __ __ _ _ _ ____ _ _ _ _ _ _ * \ \ / /__(_) __ _| |__ | |_ / ___|___ _ __ | |_ _ __ ___ | | | \ | | __ _ _ __ ___ ___ | |_(_) ___ ___ * \ \ /\ / / _ \ |/ _` | '_ \| __| | / _ \| '_ \| __| '__/ _ \| | | \| |/ _` | '__/ __/ _ \| __| |/ __/ __| * \ V V / __/ | (_| | | | | |_| |__| (_) | | | | |_| | | (_) | | | |\ | (_| | | | (_| (_) | |_| | (__\__ \ * \_/\_/ \___|_|\__, |_| |_|\__|\____\___/|_| |_|\__|_| \___/|_| |_| \_|\__,_|_| \___\___/ \__|_|\___|___/ * |___/ */ JPanel pnlWeightControllNarcotics = new JPanel(new BorderLayout()); final JButton btnWeightControl = GUITools .createHyperlinkButton("opde.controlling.prescription.narcotics.weightcontrol", null, null); // final JComboBox cmbStation = new JComboBox(StationTools.getAll4Combobox(false)); btnWeightControl.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { OPDE.getMainframe().setBlocked(true); SwingWorker worker = new SwingWorker() { @Override protected Object doInBackground() throws Exception { return MedStockTools.getNarcoticsWeightList(new LocalDate().minusMonths(1), new LocalDate()); } @Override protected void done() { try { SYSFilesTools.print(get().toString(), true); } catch (ExecutionException ee) { OPDE.fatal(ee); } catch (InterruptedException ie) { // nop } OPDE.getDisplayManager().setProgressBarMessage(null); OPDE.getMainframe().setBlocked(false); } }; worker.execute(); } }); final JToggleButton btnNotify = new JToggleButton(SYSConst.icon22mailOFF); btnNotify.setSelectedIcon(SYSConst.icon22mailON); btnNotify.setSelected(NotificationTools.find(OPDE.getLogin().getUser(), NotificationTools.NKEY_DRUG_WEIGHT_CONTROL) != null); btnNotify.setToolTipText(SYSTools.xx("opde.notification.enable.for.this.topic")); btnNotify.addItemListener(new ItemListener() { @Override public void itemStateChanged(ItemEvent e) { EntityManager em = OPDE.createEM(); try { em.getTransaction().begin(); Users user = em.merge(OPDE.getLogin().getUser()); em.lock(user, LockModeType.OPTIMISTIC_FORCE_INCREMENT); if (e.getStateChange() == ItemEvent.SELECTED) { Notification myNotification = em .merge(new Notification(NotificationTools.NKEY_DRUG_WEIGHT_CONTROL, user)); user.getNotifications().add(myNotification); } else { Notification myNotification = em.merge(NotificationTools.find(OPDE.getLogin().getUser(), NotificationTools.NKEY_DRUG_WEIGHT_CONTROL)); user.getNotifications().remove(myNotification); em.remove(myNotification); } em.getTransaction().commit(); OPDE.getLogin().setUser(user); } catch (OptimisticLockException ole) { OPDE.warn(ole); if (em.getTransaction().isActive()) { em.getTransaction().rollback(); } if (ole.getMessage().indexOf("Class> entity.info.Resident") > -1) { OPDE.getMainframe().emptyFrame(); OPDE.getMainframe().afterLogin(); } OPDE.getDisplayManager().addSubMessage(DisplayManager.getLockMessage()); } catch (Exception ex) { if (em.getTransaction().isActive()) { em.getTransaction().rollback(); } OPDE.fatal(ex); } finally { em.close(); } } }); pnlWeightControllNarcotics.add(btnWeightControl, BorderLayout.WEST); pnlWeightControllNarcotics.add(btnNotify, BorderLayout.EAST); pnlContent.add(pnlWeightControllNarcotics); return pnlContent; }
From source file:op.controlling.PnlControlling.java
private JPanel createContentPanel4Nutrition() { JPanel pnlContent = new JPanel(new VerticalLayout()); /***// w w w . j av a2s . c o m * _ _ _ _ _ _ * | (_) __ _ _ _(_) __| | | |__ __ _| | __ _ _ __ ___ ___ * | | |/ _` | | | | |/ _` | | '_ \ / _` | |/ _` | '_ \ / __/ _ \ * | | | (_| | |_| | | (_| | | |_) | (_| | | (_| | | | | (_| __/ * |_|_|\__, |\__,_|_|\__,_| |_.__/ \__,_|_|\__,_|_| |_|\___\___| * |_| */ JPanel pnlLiquidBalance = new JPanel(new BorderLayout()); final JButton btnLiquidBalance = GUITools.createHyperlinkButton("opde.controlling.nutrition.liquidbalance", null, null); final JComboBox cmbLiquidBalanceMonth = new JComboBox( SYSCalendar.createMonthList(new LocalDate().minusYears(1), new LocalDate())); btnLiquidBalance.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { OPDE.getMainframe().setBlocked(true); SwingWorker worker = new SwingWorker() { @Override protected Object doInBackground() throws Exception { LocalDate month = (LocalDate) cmbLiquidBalanceMonth.getSelectedItem(); SYSFilesTools.print(ResValueTools.getLiquidBalance(month, progressClosure), false); return null; } @Override protected void done() { OPDE.getDisplayManager().setProgressBarMessage(null); OPDE.getMainframe().setBlocked(false); } }; worker.execute(); } }); cmbLiquidBalanceMonth.setRenderer(new ListCellRenderer() { @Override public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { return new DefaultListCellRenderer().getListCellRendererComponent(list, monthFormatter.format(((LocalDate) value).toDate()), index, isSelected, cellHasFocus); } }); cmbLiquidBalanceMonth.setSelectedIndex(cmbLiquidBalanceMonth.getItemCount() - 2); pnlLiquidBalance.add(btnLiquidBalance, BorderLayout.WEST); pnlLiquidBalance.add(cmbLiquidBalanceMonth, BorderLayout.EAST); pnlContent.add(pnlLiquidBalance); /*** * _ _ _ _ _ _ _ _ * __ _____(_) __ _| |__ | |_ ___| |_ __ _| |_(_)___| |_(_) ___ ___ * \ \ /\ / / _ \ |/ _` | '_ \| __| / __| __/ _` | __| / __| __| |/ __/ __| * \ V V / __/ | (_| | | | | |_ \__ \ || (_| | |_| \__ \ |_| | (__\__ \ * \_/\_/ \___|_|\__, |_| |_|\__| |___/\__\__,_|\__|_|___/\__|_|\___|___/ * |___/ */ JPanel pnlWeight = new JPanel(new BorderLayout()); final JButton btnWeightStats = GUITools.createHyperlinkButton("opde.controlling.nutrition.weightstats", null, null); int wsMonthsBack; try { wsMonthsBack = Integer.parseInt(OPDE.getProps().getProperty("opde.controlling::wsMonthsBack")); } catch (NumberFormatException nfe) { wsMonthsBack = 7; } final JTextField txtWSMonthsBack = GUITools.createIntegerTextField(1, 24, wsMonthsBack); txtWSMonthsBack.setToolTipText(SYSTools.xx("misc.msg.monthsback")); btnWeightStats.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { OPDE.getMainframe().setBlocked(true); SwingWorker worker = new SwingWorker() { @Override protected Object doInBackground() throws Exception { SYSPropsTools.storeProp("opde.controlling::wsMonthsBack", txtWSMonthsBack.getText(), OPDE.getLogin().getUser()); SYSFilesTools.print(ResValueTools.getWeightStats( Integer.parseInt(txtWSMonthsBack.getText()), progressClosure), false); return null; } @Override protected void done() { OPDE.getDisplayManager().setProgressBarMessage(null); OPDE.getMainframe().setBlocked(false); } }; worker.execute(); } }); pnlWeight.add(btnWeightStats, BorderLayout.WEST); pnlWeight.add(txtWSMonthsBack, BorderLayout.EAST); pnlContent.add(pnlWeight); return pnlContent; }
From source file:org.biomart.configurator.controller.MartController.java
/** * Runs the given {@link ConstructorRunnable} and monitors it's progress. * /*from w w w . j a v a 2s . co m*/ * @param constructor * the constructor that will build a mart. */ public void requestMonitorConstructorRunnable(JDialog parent, final ConstructorRunnable constructor) { final ProgressDialog progressMonitor = ProgressDialog.getInstance(parent); final SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() { @Override protected Void doInBackground() throws Exception { try { constructor.run(); } catch (final Throwable t) { SwingUtilities.invokeLater(new Runnable() { public void run() { StackTrace.showStackTrace(t); } }); } finally { progressMonitor.setVisible(false); } return null; } @Override protected void done() { progressMonitor.setVisible(false); } }; worker.execute(); progressMonitor.start("processing ..."); }
From source file:org.broad.igv.cbio.FilterGeneNetworkUI.java
private void loadcBioData(final List<String> geneLoci) { final IndefiniteProgressMonitor indefMonitor = new IndefiniteProgressMonitor(60); final ProgressBar progressBar = ProgressBar.showProgressDialog((Frame) getOwner(), "Loading cBio data...", indefMonitor, true);/*from w w w. ja v a 2 s . c o m*/ progressBar.setIndeterminate(true); indefMonitor.start(); final Runnable showUI = new Runnable() { @Override public void run() { initComponents(); initComponentData(); setVisible(true); } }; final Runnable runnable = new Runnable() { @Override public void run() { WaitCursorManager.CursorToken token = null; try { token = WaitCursorManager.showWaitCursor(); network = GeneNetwork.getFromCBIO(geneLoci); if (network.vertexSet().size() == 0) { MessageUtils .showMessage("No results found for " + HttpUtils.buildURLString(geneLoci, ", ")); } else { network.annotateAll(IGV.getInstance().getAllTracks()); UIUtilities.invokeOnEventThread(showUI); } } catch (Exception e) { e.printStackTrace(); log.error(e.getMessage()); MessageUtils.showMessage("Error loading data: " + e.getMessage()); } finally { WaitCursorManager.removeWaitCursor(token); if (progressBar != null) { progressBar.close(); indefMonitor.stop(); } } } }; // If we're on the dispatch thread spawn a worker, otherwise just execute. if (SwingUtilities.isEventDispatchThread()) { SwingWorker worker = new SwingWorker() { @Override protected Object doInBackground() throws Exception { runnable.run(); return null; } }; worker.execute(); } else { runnable.run(); } }
From source file:org.codinjutsu.tools.jenkins.view.BuildParamDialog.java
private void onOK() { final Map<String, String> paramValueMap = getParamValueMap(); new SwingWorker<Void, Void>() { //FIXME don't use swing worker @Override/*from w ww. j a v a2 s .co m*/ protected Void doInBackground() throws Exception { requestManager.runParameterizedBuild(job, configuration, paramValueMap); return null; } @Override protected void done() { dispose(); try { get(); runBuildCallback.notifyOnOk(job); } catch (InterruptedException e) { logger.log(Level.WARN, "Exception occured while...", e); } catch (ExecutionException e) { runBuildCallback.notifyOnError(job, e); logger.log(Level.WARN, "Exception occured while trying to invoke build", e); } } }.execute(); }
From source file:org.dishevelled.brainstorm.BrainStorm.java
/** * Save.// ww w . j av a 2 s. co m */ private void save() { final String baseName = StringUtils.isBlank(fileName) ? getFirstWord() : fileName.substring(0, fileName.indexOf(".")); final String text = textArea.getText(); (new SwingWorker<String, Object>() { /** @{inheritDoc} */ public String doInBackground() { try { File file = new File(baseName + ".txt"); File revisions = new File(baseName + "-revisions"); revisions.mkdir(); File latestRevision = new File(revisions, baseName + "-rev" + System.currentTimeMillis() + ".txt"); if (file.exists()) { FileUtils.copyFile(file, latestRevision); } FileUtils.writeStringToFile(file, text); return "Saved"; } catch (Exception e) { e.printStackTrace(); return "Not saved"; } // todo: show status? } }).execute(); }
From source file:org.genedb.jogra.plugins.TermRationaliser.java
/** * Supplies the JPanel which is displayed in the main Jogra window. *//*from w w w .j a va 2 s. c om*/ public JPanel getMainWindowPlugin() { final JPanel ret = new JPanel(); final JButton loadButton = new JButton("Load Term Rationaliser"); final JLabel chooseType = new JLabel("Select term: "); final JComboBox termTypeBox = new JComboBox(instances.keySet().toArray()); final JCheckBox showEVCFilter = new JCheckBox("Highlight terms with evidence codes", true); loadButton.addActionListener(new ActionListener() { public void actionPerformed(final ActionEvent ae) { new SwingWorker<JFrame, Void>() { @Override protected JFrame doInBackground() throws Exception { ret.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); setTermType(instances.get((String) termTypeBox.getSelectedItem())); setShowEVC(showEVCFilter.isSelected()); return makeWindow(); } @Override public void done() { try { final GeneDBMessage e = new OpenWindowEvent(TermRationaliser.this, get()); EventBus.publish(e); } catch (final InterruptedException exp) { exp.printStackTrace(); } catch (final ExecutionException exp) { exp.printStackTrace(); } ret.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); } }.execute(); } }); Box verticalBox = Box.createVerticalBox(); Box horizontalBox = Box.createHorizontalBox(); horizontalBox.add(chooseType); horizontalBox.add(termTypeBox); verticalBox.add(horizontalBox); verticalBox.add(loadButton); verticalBox.add(showEVCFilter); ret.add(verticalBox); return ret; }
From source file:org.genedb.jogra.plugins.TermRationaliser.java
/** * Populates the JLists with data from the database * This happens right at the start to load the rationaliser * and then any time the user decides to refresh the * models from the database. We use two parallel threads * here to fetch the terms, but will eventually implement * some sort of caching mechanism./* www. ja v a 2s. co m*/ */ private void initModels() { JograProgressBar jpb = new JograProgressBar("Loading terms from database..."); //Progress bar added for better user information this.setSelectedTaxonsAndScopeLabel(jogra.getSelectedOrganismNames()); logger.info("Are we in EDT when loading data? " + SwingUtilities.isEventDispatchThread()); /* Loading the terms from the database */ SwingWorker<Void, Void> worker_1 = new SwingWorker<Void, Void>() { @Override public Void doInBackground() { try { long startTime = System.nanoTime(); long endTime; terms = termService.getTerms(getSelectedTaxons(), getTermType()); /* Java Collections sort is a modified mergesort guaranteeing * n log(n) performance. The question is, is it slower or faster * than doing a sql order by in postgres? It appears postgres * uses qsort so the 'average' performance should also be n log(n). * After some experimentation, we use Java's sort here since it appeared * marginally faster. */ Collections.sort(terms); endTime = System.nanoTime(); logger.info("Doing sorting in SQL (specific) took : " + (endTime - startTime) + " ns."); for (Term term : terms) { if (isShowEVC()) { //Fetch the evidence codes for the terms if the user wants them term.setEvidenceCodes(termService.getEvidenceCodes(term)); } } } catch (SQLException se) { se.printStackTrace(); //How do we process this exception? } return null; } @Override public void done() { logger.info("Finished worker 1"); } }; SwingWorker<Void, Void> worker_2 = new SwingWorker<Void, Void>() { @Override public Void doInBackground() { try { long startTime = System.nanoTime(); long endTime; allTerms = termService.getAllTerms(getTermType()); Collections.sort(allTerms); endTime = System.nanoTime(); logger.info("Doing sorting in SQL (general) took : " + (endTime - startTime) + " ns."); for (Term term : allTerms) { if (isShowEVC()) { //Fetch the evidence codes for the terms if the user wants them term.setEvidenceCodes(termService.getEvidenceCodes(term)); } } } catch (SQLException se) { se.printStackTrace(); } return null; } @Override public void done() { logger.info("Finished worker 2"); } }; logger.info("Inside swing worker 1 to fetch and sort the specific terms"); writeMessage("Fetching organism-specific terms from the database...\n"); worker_1.run(); logger.info("Inside swing worker 2 to fetch and sort all the cv terms"); writeMessage("Fetching all the terms in the cv from the database...\n"); worker_2.run(); //After the data is available... fromList.addAll(terms); toList.addAll(allTerms); productCountLabel.setText(String.format("Number of terms for selected organisms: %d terms found (%s)", terms.size(), getTermType())); textField.setText(""); //Re-set the editable text box fromList.clearSelection(); //Clear any previous selections toList.clearSelection(); fromList.repaint(); toList.repaint(); jpb.stop(); }
From source file:org.gofleet.module.routing.RoutingMap.java
@Override public void actionPerformed(final ActionEvent e) { SwingWorker<Object, Object> sw = new SwingWorker<Object, Object>() { @Override/* w w w . ja v a 2s .c o m*/ protected Object doInBackground() throws Exception { String mapMenuNewPlanning = i18n.getString("map.menu.new.planning"); try { if (e.getActionCommand().equals(mapMenuNewPlanning)) { LatLon from = RoutingMap.this.mapView.getLatLon( RoutingMap.this.mapView.lastMEvent.getPoint().x, RoutingMap.this.mapView.lastMEvent.getPoint().y); newPlan(from); } else { log.error( "ActionCommand desconocido: " + e.getActionCommand() + " vs " + mapMenuNewPlanning); } } catch (Throwable t) { log.error("Error al ejecutar la accion del menu contextual", t); } return null; } }; sw.execute(); }