Example usage for javax.swing JPopupMenu getFontMetrics

List of usage examples for javax.swing JPopupMenu getFontMetrics

Introduction

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

Prototype

public FontMetrics getFontMetrics(Font font) 

Source Link

Document

Gets the FontMetrics for the specified Font.

Usage

From source file:base.BasePlayer.AddGenome.java

public void actionPerformed(ActionEvent event) {
    if (event.getSource() == download) {
        if (!downloading) {
            downloading = true;//from  w  w w .  j  a v a 2  s  .com
            downloadGenome(genometable.getValueAt(genometable.getSelectedRow(), 0).toString());
            downloading = false;
        }
    } else if (event.getSource() == getLinks) {
        URL[] urls = AddGenome.genomeHash
                .get(genometable.getValueAt(genometable.getSelectedRow(), 0).toString());
        JPopupMenu menu = new JPopupMenu();
        JTextArea area = new JTextArea();
        JScrollPane menuscroll = new JScrollPane();
        area.setFont(Main.menuFont);
        menu.add(menuscroll);
        menu.setPreferredSize(new Dimension(
                menu.getFontMetrics(Main.menuFont).stringWidth(urls[0].toString()) + Main.defaultFontSize * 10,
                (int) menu.getFontMetrics(Main.menuFont).getHeight() * 4));
        //area.setMaximumSize(new Dimension(300, 600));
        area.setLineWrap(true);
        area.setWrapStyleWord(true);
        for (int i = 0; i < urls.length; i++) {
            area.append(urls[i].toString() + "\n");
        }

        area.setCaretPosition(0);
        area.revalidate();
        menuscroll.getViewport().add(area);
        menu.pack();
        menu.show(this, 0, 0);

    } else if (event.getSource() == checkEnsembl) {
        if (ensemblfetch) {
            menu.show(AddGenome.treescroll, 0, 0);
        } else {
            EnsemblFetch fetcher = new EnsemblFetch();
            fetcher.execute();
        }
    } else if (event.getSource() == checkUpdates) {
        URL testfile = null;
        try {
            // kattoo onko paivityksia annotaatioon
            String ref = selectedNode.toString();
            if (AddGenome.genomeHash.get(ref) != null) {
                ArrayList<String> testfiles = new ArrayList<String>();
                if (Main.drawCanvas != null) {
                    for (int i = 0; i < Main.genomehash.get(ref).size(); i++) {
                        testfiles.add(Main.genomehash.get(ref).get(i).getName().replace(".bed.gz", ""));
                    }
                }
                testfile = AddGenome.genomeHash.get(ref)[1];
                String result = Main.checkFile(testfile, testfiles);

                if (result.length() == 0) {
                    Main.showError("You have newest annotation file.", "Note");
                } else {
                    int n = JOptionPane.showConfirmDialog(Main.drawCanvas,
                            "New annotation file found: " + result + "\nDownload it now?", "Note",
                            JOptionPane.YES_NO_OPTION);
                    if (n == JOptionPane.YES_OPTION) {
                        URL fileurl = new URL(testfile.getProtocol() + "://" + testfile.getHost()
                                + testfile.getPath().substring(0, testfile.getPath().lastIndexOf("/") + 1)
                                + result);
                        OutputRunner runner = new OutputRunner(fileurl, ref);
                        runner.downloadAnnotation = true;
                        runner.execute();
                    }
                }
            } else {
                Main.showError("This genome is not from Ensembl list, could not check for updates.", "Note",
                        AddGenome.genometable);
            }
        } catch (Exception e) {
            Main.showError("Cannot connect to " + testfile.getHost() + ".\nTry again later.", "Error");
            e.printStackTrace();
        }
    } else if (event.getSource() == remove) {
        if (!selectedNode.isLeaf()) {
            String removeref = selectedNode.toString();
            //   Boolean same = false;
            try {
                if (Main.drawCanvas != null) {
                    if (removeref.equals(Main.refDropdown.getSelectedItem().toString())) {
                        Main.referenceFile.close();
                        //      same = true;
                        if (ChromDraw.exonReader != null) {
                            ChromDraw.exonReader.close();
                        }
                    }
                }
                if (Main.genomehash.containsKey(removeref)) {
                    for (int i = Main.genomehash.get(removeref).size() - 1; i >= 0; i--) {
                        Main.genomehash.get(removeref).remove(i);
                    }
                    Main.genomehash.remove(removeref);

                }
                if (Main.drawCanvas != null) {
                    Main.refModel.removeElement(removeref);
                    Main.refDropdown.removeItem(removeref);
                    Main.refDropdown.revalidate();
                }

                for (int i = 0; i < Main.genome.getItemCount(); i++) {
                    if (Main.genome.getItem(i).getName() != null) {

                        if (Main.genome.getItem(i).getName().equals(removeref)) {
                            Main.genome.remove(Main.genome.getItem(i));
                            break;
                        }
                    }
                }

                FileUtils.deleteDirectory(new File(Main.genomeDir.getCanonicalPath() + "/" + removeref));
                checkGenomes();
                Main.setAnnotationDrop("");

                if (Main.genomehash.size() == 0) {
                    Main.refDropdown.setSelectedIndex(0);
                    Main.setChromDrop("-1");
                }
            } catch (Exception e) {
                e.printStackTrace();
                try {
                    Main.showError("Could not delete genome folder.\nYou can do it manually by deleting folder "
                            + Main.genomeDir.getCanonicalPath() + "/" + removeref, "Note");
                } catch (IOException e1) {

                    e1.printStackTrace();
                }
            }
        } else {
            try {
                if (Main.drawCanvas != null) {
                    if (ChromDraw.exonReader != null) {
                        ChromDraw.exonReader.close();
                    }
                }

                Main.removeAnnotationFile(selectedNode.getParent().toString(), selectedNode.toString());

                FileUtils.deleteDirectory(new File(Main.genomeDir.getCanonicalPath() + "/"
                        + selectedNode.getParent().toString() + "/annotation/" + selectedNode.toString()));

                //   root.remove(selectedNode.getParent().getIndex(selectedNode));
                //   root.remove
                //   checkGenomes();

            } catch (Exception e) {
                e.printStackTrace();
                try {
                    Main.showError("Could not delete genome folder.\nYou can do it manually by deleting folder "
                            + Main.genomeDir.getCanonicalPath() + "/" + selectedNode.getParent().toString()
                            + "/annotation/" + selectedNode.toString(), "Note");
                } catch (IOException e1) {

                    e1.printStackTrace();
                }
            }
            treemodel.removeNodeFromParent(selectedNode);
        }

    } else if (event.getSource() == add) {

        if (genomeFile == null) {
            if (new File(genomeFileText.getText()).exists()) {
                genomeFile = new File(genomeFileText.getText());

            } else {
                genomeFileText.setText("Select reference genome fasta-file.");
                genomeFileText.setForeground(Color.red);
                return;
            }
        }

        /*if(genomeName.getText().contains("Give name") || genomeName.getText().length() == 0) {
           genomeName.setText("Give name of the genome");
           genomeName.setForeground(Color.red);
           genomeName.revalidate();
                   
        }
        else if(!annotation && new File(Main.userDir +"/genomes/"+genomeName.getText().trim().replace("\\s+", "_")).exists()) {
           genomeName.setText("This genome exists already.");
           genomeName.setForeground(Color.red);
           genomeName.revalidate();
        }
        else */

        if ((genomeFileText.getText().length() == 0
                || genomeFileText.getText().startsWith("Select reference"))) {
            genomeFileText.setText("Select reference genome fasta-file.");
            genomeFileText.setForeground(Color.red);
            genomeFileText.revalidate();
        }

        else {

            OutputRunner runner = new OutputRunner(
                    genomeFile.getName().replace(".fasta", "").replace(".gz", ""), genomeFile, annotationFile);
            runner.execute();
        }

    } else if (event.getSource() == openRef) {
        try {

            JFileChooser chooser = new JFileChooser(Main.downloadDir);
            chooser.setMultiSelectionEnabled(false);
            chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
            chooser.setAcceptAllFileFilterUsed(false);
            MyFilterFasta fastaFilter = new MyFilterFasta();

            chooser.addChoosableFileFilter(fastaFilter);
            chooser.setDialogTitle("Select reference fasta-file");
            if (Main.screenSize != null) {
                chooser.setPreferredSize(new Dimension((int) Main.screenSize.getWidth() / 3,
                        (int) Main.screenSize.getHeight() / 3));
            }

            int returnVal = chooser.showOpenDialog((Component) this.getParent());

            if (returnVal == JFileChooser.APPROVE_OPTION) {
                genomeFile = chooser.getSelectedFile();
                Main.downloadDir = genomeFile.getParent();
                Main.writeToConfig("DownloadDir=" + genomeFile.getParent());
                genomeFileText.setText(genomeFile.getName());
                genomeFileText.revalidate();
                frame.pack();
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    } else if (event.getSource() == openAnno) {
        try {

            JFileChooser chooser = new JFileChooser(Main.downloadDir);
            chooser.setMultiSelectionEnabled(false);
            chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
            chooser.setAcceptAllFileFilterUsed(false);
            MyFilterGFF gffFilter = new MyFilterGFF();

            chooser.addChoosableFileFilter(gffFilter);
            chooser.setDialogTitle("Select annotation gff3-file");
            if (Main.screenSize != null) {
                chooser.setPreferredSize(new Dimension((int) Main.screenSize.getWidth() / 3,
                        (int) Main.screenSize.getHeight() / 3));
            }
            int returnVal = chooser.showOpenDialog((Component) this.getParent());

            if (returnVal == JFileChooser.APPROVE_OPTION) {
                if (genomeFile == null) {
                    genomeFile = Main.fastahash.get(Main.hoverGenome);
                }
                annotationFile = chooser.getSelectedFile();
                Main.downloadDir = annotationFile.getParent();
                Main.writeToConfig("DownloadDir=" + annotationFile.getParent());

                OutputRunner runner = new OutputRunner(
                        genomeFile.getName().replace(".fasta", "").replace(".gz", ""), genomeFile,
                        annotationFile);
                runner.execute();
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}