Search in sources :

Example 41 with SwingWorker

use of javax.swing.SwingWorker in project jgnash by ccavanaugh.

the class BudgetPanel method initBudgetCombo.

private void initBudgetCombo() {
    budgetCombo = new BudgetComboBox();
    SwingWorker<StoredObject, Void> worker = new SwingWorker<StoredObject, Void>() {

        @Override
        protected StoredObject doInBackground() throws Exception {
            Preferences preferences = Preferences.userNodeForPackage(BudgetPanel.class);
            String lastBudgetUUID = preferences.get(LAST_BUDGET, null);
            StoredObject o = null;
            if (lastBudgetUUID != null) {
                o = engine.getBudgetByUuid(lastBudgetUUID);
            }
            return o;
        }

        @Override
        protected void done() {
            try {
                StoredObject o = get();
                if (o != null && o instanceof Budget) {
                    budgetCombo.setSelectedBudget((Budget) o);
                    activeBudget = (Budget) o;
                }
                if (activeBudget == null) {
                    List<Budget> budgets = engine.getBudgetList();
                    if (!budgets.isEmpty()) {
                        budgetCombo.setSelectedBudget(budgets.get(0));
                        activeBudget = budgets.get(0);
                    }
                }
                // the combo takes the full toolbar space unless limited
                budgetCombo.setMaximumSize(new Dimension(COMBO_BOX_WIDTH, budgetCombo.getPreferredSize().height * 3));
                budgetCombo.addActionListener(e -> {
                    if (activeBudget != budgetCombo.getSelectedBudget()) {
                        refreshDisplay();
                    }
                });
            } catch (final InterruptedException | ExecutionException e) {
                logger.log(Level.SEVERE, e.getLocalizedMessage(), e);
            }
        }
    };
    worker.execute();
}
Also used : Dimension(java.awt.Dimension) StoredObject(jgnash.engine.StoredObject) SwingWorker(javax.swing.SwingWorker) Budget(jgnash.engine.budget.Budget) Preferences(java.util.prefs.Preferences) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

SwingWorker (javax.swing.SwingWorker)41 ExecutionException (java.util.concurrent.ExecutionException)15 IOException (java.io.IOException)14 List (java.util.List)10 ResourceBundle (java.util.ResourceBundle)10 ArrayList (java.util.ArrayList)9 Preferences (java.util.prefs.Preferences)8 File (java.io.File)6 JFileChooser (javax.swing.JFileChooser)6 JPanel (javax.swing.JPanel)6 BorderLayout (java.awt.BorderLayout)5 FileNameExtensionFilter (javax.swing.filechooser.FileNameExtensionFilter)5 Engine (jgnash.engine.Engine)5 FileNotFoundException (java.io.FileNotFoundException)4 InstanceNotFoundException (javax.management.InstanceNotFoundException)4 IntrospectionException (javax.management.IntrospectionException)4 MBeanInfo (javax.management.MBeanInfo)4 ReflectionException (javax.management.ReflectionException)4 JScrollPane (javax.swing.JScrollPane)4 ResourceAccessException (org.springframework.web.client.ResourceAccessException)4