Example usage for javax.swing JViewport getBounds

List of usage examples for javax.swing JViewport getBounds

Introduction

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

Prototype

public Rectangle getBounds() 

Source Link

Document

Gets the bounds of this component in the form of a Rectangle object.

Usage

From source file:com.att.aro.ui.model.diagnostic.GraphPanelHelper.java

public void SaveImageAs(JViewport pane, String graphPanelSaveDirectory) {

    JFileChooser fc = new JFileChooser(graphPanelSaveDirectory);

    // Set up file types
    String[] fileTypesJPG = new String[2];
    String fileDisplayTypeJPG = ResourceBundleHelper.getMessageString("fileChooser.contentDisplayType.jpeg");
    fileTypesJPG[0] = ResourceBundleHelper.getMessageString("fileChooser.contentType.jpeg");
    fileTypesJPG[1] = ResourceBundleHelper.getMessageString("fileChooser.contentType.jpg");
    FileFilter filterJPG = new ExtensionFileFilter(fileDisplayTypeJPG, fileTypesJPG);

    fc.addChoosableFileFilter(fc.getAcceptAllFileFilter());
    String[] fileTypesPng = new String[1];
    String fileDisplayTypePng = ResourceBundleHelper.getMessageString("fileChooser.contentDisplayType.png");
    fileTypesPng[0] = ResourceBundleHelper.getMessageString("fileChooser.contentType.png");
    FileFilter filterPng = new ExtensionFileFilter(fileDisplayTypePng, fileTypesPng);
    fc.addChoosableFileFilter(filterPng);
    fc.setFileFilter(filterJPG);/*from   www.jav  a 2  s .co m*/
    File plotImageFile = null;

    boolean bSavedOrCancelled = false;
    while (!bSavedOrCancelled) {
        if (fc.showSaveDialog(pane) == JFileChooser.APPROVE_OPTION) {
            String strFile = fc.getSelectedFile().toString();
            String strFileLowerCase = strFile.toLowerCase();
            String fileDesc = fc.getFileFilter().getDescription();
            String fileType = ResourceBundleHelper.getMessageString("fileChooser.contentType.jpg");
            if ((fileDesc.equalsIgnoreCase(
                    ResourceBundleHelper.getMessageString("fileChooser.contentDisplayType.png"))
                    || strFileLowerCase.endsWith(ResourceBundleHelper.getMessageString("fileType.filters.dot")
                            + fileTypesPng[0].toLowerCase()))) {
                fileType = fileTypesPng[0];
            }
            if (strFile.length() > 0) {
                // Save current directory
                graphPanelSaveDirectory = fc.getCurrentDirectory().getPath();

                if ((fileType != null) && (fileType.length() > 0)) {
                    String fileTypeLowerCaseWithDot = ResourceBundleHelper
                            .getMessageString("fileType.filters.dot") + fileType.toLowerCase();
                    if (!strFileLowerCase.endsWith(fileTypeLowerCaseWithDot)) {
                        strFile += ResourceBundleHelper.getMessageString("fileType.filters.dot") + fileType;
                    }
                }
                plotImageFile = new File(strFile);
                boolean bAttemptToWriteToFile = true;
                if (plotImageFile.exists()) {
                    if (MessageDialogFactory.showConfirmDialog(pane,
                            MessageFormat.format(
                                    ResourceBundleHelper.getMessageString("fileChooser.fileExists"),
                                    plotImageFile.getAbsolutePath()),
                            ResourceBundleHelper.getMessageString("fileChooser.confirm"),
                            JOptionPane.YES_NO_OPTION) != JOptionPane.YES_OPTION) {
                        bAttemptToWriteToFile = false;
                    }
                }
                if (bAttemptToWriteToFile) {
                    try {
                        if (fileType != null && fileType.equalsIgnoreCase(fileTypesPng[0])) {
                            BufferedImage bufImage = ImageHelper.createImage(pane.getBounds().width,
                                    pane.getBounds().height);
                            Graphics2D g = bufImage.createGraphics();
                            pane.paint(g);
                            ImageIO.write(bufImage, "png", plotImageFile);
                        } else {
                            BufferedImage bufImage = ImageHelper.createImage(pane.getBounds().width,
                                    pane.getBounds().height);
                            Graphics2D g = bufImage.createGraphics();
                            pane.paint(g);
                            ImageIO.write(bufImage, "jpg", plotImageFile);
                        }
                        bSavedOrCancelled = true;
                    } catch (IOException e) {
                        MessageDialogFactory.showMessageDialog(pane, ResourceBundleHelper
                                .getMessageString("fileChooser.errorWritingToFile" + plotImageFile.toString()));
                    }
                }
            }
        } else {
            bSavedOrCancelled = true;
        }
    }
}