Example usage for javax.swing SwingWorker SwingWorker

List of usage examples for javax.swing SwingWorker SwingWorker

Introduction

In this page you can find the example usage for javax.swing SwingWorker SwingWorker.

Prototype

public SwingWorker() 

Source Link

Document

Constructs this SwingWorker .

Usage

From source file:mainGUI.TrainingJFrame.java

private void saveNetwork(final NeuralNet net, final String filename) {
    SwingWorker<Boolean, Void> worker = new SwingWorker<Boolean, Void>() {
        @Override/*from   ww  w  .  j ava2 s . c o m*/
        protected Boolean doInBackground() throws Exception {

            try {
                // Serialize data object to a file
                ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(filename));
                out.writeObject(net);
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return true;
        }

        @Override
        protected void done() {
            System.out.println(filename + " saved");
        }
    };
    worker.execute();
    System.out.println("Saving file " + filename);
}

From source file:com.demonwav.mcdev.creator.ForgeProjectSettingsWizard.java

@Override
public JComponent getComponent() {
    settings = (ForgeProjectConfiguration) creator.getSettings().get(PlatformType.FORGE);
    if (settings == null) {
        return null;
    }//from  w ww  .  j a  va2s  .com

    pluginNameField.setText(WordUtils.capitalize(creator.getArtifactId()));
    pluginVersionField.setText(creator.getVersion());

    if (settings != null && !settings.isFirst) {
        pluginNameField.setEditable(false);
        pluginVersionField.setEditable(false);
    }

    mainClassField
            .setText(this.creator.getGroupId().toLowerCase() + '.' + this.creator.getArtifactId().toLowerCase()
                    + '.' + WordUtils.capitalize(this.creator.getArtifactId()));

    if (creator.getSettings().size() > 1) {
        mainClassField.setText(mainClassField.getText() + PlatformType.FORGE.getNormalName());
    }

    loadingBar.setIndeterminate(true);

    if (settings instanceof SpongeForgeProjectConfiguration) {
        title.setIcon(PlatformAssets.SPONGE_FORGE_ICON_2X);
        title.setText("<html><font size=\"5\">SpongeForge Settings</font></html>");
        generateDocsCheckbox.setVisible(true);

        minecraftVersionLabel.setText("    Sponge API");
    }

    try {
        new SwingWorker() {
            @Override
            protected Object doInBackground() throws Exception {
                mcpVersion = McpVersion.downloadData();
                forgeVersion = ForgeVersion.downloadData();
                return null;
            }

            @Override
            protected void done() {
                if (mcpVersion == null) {
                    return;
                }

                minecraftVersionBox.removeAllItems();

                // reverse order the versions
                if (!(settings instanceof SpongeForgeProjectConfiguration)) {
                    forgeVersion.getSortedMcVersions().forEach(minecraftVersionBox::addItem);
                    final String recommended = forgeVersion.getRecommended(mcpVersion.getVersions());

                    int index = 0;
                    for (int i = 0; i < minecraftVersionBox.getItemCount(); i++) {
                        if (minecraftVersionBox.getItemAt(i).equals(recommended)) {
                            index = i;
                        }
                    }
                    minecraftVersionBox.setSelectedIndex(index);
                } else {
                    minecraftVersionBox.addItem("4.1.0");
                    minecraftVersionBox.addItem("5.0.0");
                    minecraftVersionBox.setSelectedIndex(1);
                }

                if (mcpVersion != null) {
                    mcpVersion.setMcpVersion(mcpVersionBox, getVersion(), mcpBoxActionListener);
                }

                if (forgeVersion == null) {
                    return;
                }

                setForgeVersion();

                loadingBar.setIndeterminate(false);
                loadingBar.setVisible(false);
            }
        }.execute();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return panel;
}

From source file:groovesquid.Main.java

public static void saveConfig() {
    SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() {
        @Override//from  w w w. jav a 2  s.c  o  m
        protected Void doInBackground() throws Exception {
            File configFile = new File(configDir + File.separator + "config.json");
            try {
                FileUtils.writeStringToFile(configFile, gson.toJson(config));
            } catch (IOException ex) {
                log.log(Level.SEVERE, null, ex);
            }
            return null;
        }
    };
    worker.execute();
}

From source file:net.sourceforge.atunes.kernel.actions.RemoveFromDiskAction.java

private void fromOtherViews() {
    final List<AudioFile> files = ControllerProxy.getInstance().getNavigationController()
            .getFilesSelectedInNavigator();
    RepositoryHandler.getInstance().remove(files);
    VisualHandler.getInstance().showIndeterminateProgressDialog(LanguageTool.getString("PLEASE_WAIT"));
    new SwingWorker<Void, Void>() {
        @Override//from  ww w  . jav  a 2 s .  com
        protected Void doInBackground() {
            for (AudioFile audioFile : files) {
                File file = audioFile.getFile();
                if (file != null) {
                    file.delete();
                }
            }
            return null;
        }

        @Override
        protected void done() {
            VisualHandler.getInstance().hideIndeterminateProgressDialog();
        }
    }.execute();
}

From source file:com.github.rholder.gradle.intellij.DependencyViewer.java

public DependencyViewer(Project p, ToolWindow t) {
    super(true, true);
    this.project = p;
    this.toolWindow = t;
    this.splitter = new Splitter(false, 0.75f);
    this.information = new JTextArea();
    this.toolingLogger = initToolingLogger();

    this.dependencyCellRenderer = new DependencyCellRenderer();
    this.dependencyCellRenderer.omittedSelected = JBColor.MAGENTA;
    this.dependencyCellRenderer.omittedUnselected = JBColor.GRAY;
    this.dependencyCellRenderer.normalSelected = JBColor.foreground();
    this.dependencyCellRenderer.normalUnselected = JBColor.BLACK;
    this.information.setEditable(false);

    this.shouldPromptForCurrentProject = true;

    // TODO clean all of this up
    GradleService gradleService = ServiceManager.getService(project, GradleService.class);
    gradleService.addListener(new ViewActionListener() {
        @Override/*w  ww . j a v  a2 s .  c  o m*/
        public void refresh() {
            if (shouldPromptForCurrentProject) {
                switch (useCurrentProjectBuild()) {
                case 0:
                    gradleBaseDir = project.getBasePath();
                    break;
                default: // do nothing, stay null
                }
                shouldPromptForCurrentProject = false;
            }

            if (gradleBaseDir == null) {
                promptForGradleBaseDir();
            }

            updateView(null, null);

            new SwingWorker<GradleNode, Void>() {
                protected GradleNode doInBackground() throws Exception {
                    try {
                        Map<String, GradleNode> dependencyMap = loadProjectDependenciesFromModel(gradleBaseDir,
                                toolingLogger);
                        GradleNode rootDependency = dependencyMap.get("root");

                        GradleNode target = dependencyCellRenderer.selectedGradleNode;
                        GradleNode selectedDependency;
                        if (target != null && target.group != null) {
                            selectedDependency = target;
                        } else {
                            selectedDependency = new GradleNode("No dependency selected");
                        }

                        updateView(rootDependency, selectedDependency);
                        return rootDependency;
                    } catch (Exception e) {
                        e.printStackTrace();
                        toolingLogger.log(ExceptionUtils.getFullStackTrace(e));
                        throw new RuntimeException(e);
                    }
                }
            }.execute();
        }

        @Override
        public void toggleShowReplaced() {
            dependencyCellRenderer.showReplaced = !dependencyCellRenderer.showReplaced;
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    splitter.repaint();
                    splitter.validate();
                }
            });
        }

        @Override
        public void reset() {
            gradleBaseDir = null;
            refresh();
        }
    });
    gradleService.refresh();

    setContent(splitter);
    final ActionManager actionManager = ActionManager.getInstance();
    ActionToolbar actionToolbar = actionManager.createActionToolbar("Gradle View Toolbar",
            (DefaultActionGroup) actionManager.getAction("GradleView.NavigatorActionsToolbar"), true);

    actionToolbar.setTargetComponent(splitter);
    setToolbar(actionToolbar.getComponent());
}

From source file:info.sugoiapps.xoserver.XOverServer.java

/**
 * Start the file client./*from   w  w  w  .  j  a va2  s.  c o  m*/
 */
private void startFileClient() {
    SwingWorker fileclient = new SwingWorker<Void, Integer>() {
        @Override
        protected Void doInBackground() {
            System.out.println("File client about to start with host IP set to " + hostAddress);
            fc = new FileClient(hostAddress);
            return null;
        }
    };
    fileclient.execute();

}

From source file:com.mirth.connect.client.ui.SettingsPanelServer.java

public void doRefresh() {
    if (PlatformUI.MIRTH_FRAME.alertRefresh()) {
        return;/*from   www .  j  a va2 s  .c o  m*/
    }

    final String workingId = getFrame().startWorking("Loading " + getTabName() + " settings...");

    SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() {

        ServerSettings serverSettings = null;
        UpdateSettings updateSettings = null;

        public Void doInBackground() {
            try {
                serverSettings = getFrame().mirthClient.getServerSettings();
                updateSettings = getFrame().mirthClient.getUpdateSettings();
            } catch (ClientException e) {
                getFrame().alertThrowable(getFrame(), e);
            }

            return null;
        }

        @Override
        public void done() {
            // null if it failed to get the server/update settings or if confirmLeave returned false
            if (serverSettings != null && updateSettings != null) {
                setServerSettings(serverSettings);
                setUpdateSettings(updateSettings);
            }
            getFrame().stopWorking(workingId);
        }
    };

    worker.execute();
}

From source file:com.mirth.connect.plugins.datapruner.DataPrunerPanel.java

@Override
public void doRefresh() {
    if (PlatformUI.MIRTH_FRAME.alertRefresh()) {
        return;//from ww w.  j av a2s.  com
    }

    final String workingId = getFrame().startWorking("Loading " + getTabName() + " properties...");

    final Properties serverProperties = new Properties();

    SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() {

        public Void doInBackground() {
            try {
                Properties propertiesFromServer = plugin.getPropertiesFromServer();

                if (propertiesFromServer != null) {
                    serverProperties.putAll(propertiesFromServer);
                }
            } catch (Exception e) {
                getFrame().alertThrowable(getFrame(), e);
            }
            return null;
        }

        @Override
        public void done() {
            setProperties(serverProperties);
            getFrame().stopWorking(workingId);
        }
    };

    worker.execute();
}

From source file:com.mirth.connect.client.ui.SettingsPanelMap.java

public boolean doSave() {
    final Map<String, ConfigurationProperty> configurationMap = new HashMap<String, ConfigurationProperty>();
    RefreshTableModel model = (RefreshTableModel) configurationMapTable.getModel();
    for (int i = 0; i < model.getRowCount(); i++) {
        String key = (String) model.getValueAt(i, 0);
        String value = (String) model.getValueAt(i, 1);
        String comment = (String) model.getValueAt(i, 2);

        if (StringUtils.isNotBlank(key)) {
            configurationMap.put(key, new ConfigurationProperty(value, comment));
        } else {//ww  w . j  a  v a2 s .  c om
            if (StringUtils.isNotBlank(value) || StringUtils.isNotBlank(comment)) {
                getFrame().alertWarning(this, "Blank keys are not allowed.");
                return false;
            }
        }
    }

    final String workingId = getFrame().startWorking("Saving " + getTabName() + " settings...");

    SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() {

        public Void doInBackground() {
            try {
                getFrame().mirthClient.setConfigurationMap(configurationMap);
            } catch (ClientException e) {
                getFrame().alertThrowable(getFrame(), e);
            }

            return null;
        }

        @Override
        public void done() {
            setSaveEnabled(false);
            getFrame().stopWorking(workingId);
        }
    };

    worker.execute();

    return true;
}

From source file:ZipTest.java

/**
 * Loads a file from the ZIP archive into the text area
 * @param name the name of the file in the archive
 *//*  w  ww  .ja va2 s.co  m*/
public void loadZipFile(final String name) {
    fileCombo.setEnabled(false);
    fileText.setText("");
    new SwingWorker<Void, Void>() {
        protected Void doInBackground() throws Exception {
            try {
                ZipInputStream zin = new ZipInputStream(new FileInputStream(zipname));
                ZipEntry entry;

                // find entry with matching name in archive
                while ((entry = zin.getNextEntry()) != null) {
                    if (entry.getName().equals(name)) {
                        // read entry into text area
                        Scanner in = new Scanner(zin);
                        while (in.hasNextLine()) {
                            fileText.append(in.nextLine());
                            fileText.append("\n");
                        }
                    }
                    zin.closeEntry();
                }
                zin.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }

        protected void done() {
            fileCombo.setEnabled(true);
        }
    }.execute();
}