Search in sources :

Example 46 with HeadlessException

use of java.awt.HeadlessException in project JMRI by JMRI.

the class InputWindow method storeFile.

/**
     * Save the contents of this input window to a file.
     *
     * @param fileChooser the chooser to select the file with
     * @return true if successful; false otherwise
     */
protected boolean storeFile(JFileChooser fileChooser) {
    boolean results = false;
    File file = getFile(fileChooser);
    if (file != null) {
        try {
            // check for possible overwrite
            if (file.exists()) {
                int selectedValue = JOptionPane.showConfirmDialog(null, "File " + file.getName() + " already exists, overwrite it?", "Overwrite file?", JOptionPane.OK_CANCEL_OPTION);
                if (selectedValue != JOptionPane.OK_OPTION) {
                    // user clicked no to override
                    results = false;
                    return results;
                }
            }
            StringBuilder fileData = new StringBuilder(area.getText());
            try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) {
                writer.append(fileData);
            }
        } catch (HeadlessException | IOException e) {
            log.error("Unhandled problem in storeFile: " + e);
        }
    } else {
        // If the file is null then the user has clicked cancel.
        results = true;
    }
    return results;
}
Also used : HeadlessException(java.awt.HeadlessException) FileWriter(java.io.FileWriter) IOException(java.io.IOException) File(java.io.File) BufferedWriter(java.io.BufferedWriter)

Example 47 with HeadlessException

use of java.awt.HeadlessException in project JMRI by JMRI.

the class RosterEntry method updateFile.

/**
     * Write the contents of this RosterEntry back to a file, preserving all
     * existing decoder CV content.
     * <p>
     * This writes the file back in place, with the same decoder-specific
     * content.
     */
public void updateFile() {
    LocoFile df = new LocoFile();
    String fullFilename = LocoFile.getFileLocation() + getFileName();
    // read in the content
    try {
        mRootElement = df.rootFromName(fullFilename);
    } catch (JDOMException | IOException e) {
        log.error("Exception while loading loco XML file: " + getFileName() + " exception: " + e);
    }
    try {
        File f = new File(fullFilename);
        // do backup
        df.makeBackupFile(LocoFile.getFileLocation() + getFileName());
        // and finally write the file
        df.writeFile(f, mRootElement, this.store());
    } catch (Exception e) {
        log.error("error during locomotive file output", e);
        try {
            JOptionPane.showMessageDialog(null, ResourceBundle.getBundle("jmri.jmrit.roster.JmritRosterBundle").getString("ErrorSavingText") + "\n" + e.getMessage(), ResourceBundle.getBundle("jmri.jmrit.roster.JmritRosterBundle").getString("ErrorSavingTitle"), JOptionPane.ERROR_MESSAGE);
        } catch (HeadlessException he) {
        // silently ignore inability to display dialog
        }
    }
}
Also used : HeadlessException(java.awt.HeadlessException) IOException(java.io.IOException) JDOMException(org.jdom2.JDOMException) File(java.io.File) JDOMException(org.jdom2.JDOMException) HeadlessException(java.awt.HeadlessException) ParseException(java.text.ParseException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 48 with HeadlessException

use of java.awt.HeadlessException in project processdash by dtuma.

the class WBSEditor method main.

public static void main(String[] args) {
    args = maybeParseJnlpArgs(args);
    if (isDumpAndExitMode())
        System.setProperty("java.awt.headless", "true");
    ExternalLocationMapper.getInstance().loadDefaultMappings();
    RuntimeUtils.autoregisterPropagatedSystemProperties();
    for (String prop : PROPS_TO_PROPAGATE) RuntimeUtils.addPropagatedSystemProperty(prop, null);
    String[] locations = args;
    boolean bottomUp = Boolean.getBoolean("teamdash.wbs.bottomUp");
    boolean indivMode = Boolean.getBoolean("teamdash.wbs.indiv");
    String indivInitials = System.getProperty("teamdash.wbs.indivInitials");
    boolean showTeam = Boolean.getBoolean("teamdash.wbs.showTeamMemberList");
    boolean readOnly = Boolean.getBoolean("teamdash.wbs.readOnly") || TeamServerSelector.isHistoricalModeEnabled();
    String syncURL = System.getProperty("teamdash.wbs.syncURL");
    String itemHref = System.getProperty("teamdash.wbs.showItem");
    String owner = System.getProperty("teamdash.wbs.owner");
    try {
        createAndShowEditor(locations, bottomUp, indivMode, indivInitials, showTeam, syncURL, true, readOnly, itemHref, owner);
    } catch (HeadlessException he) {
        he.printStackTrace();
        System.exit(1);
    }
    new Timer(DAY_MILLIS, new UsageLogAction()).start();
    maybeNotifyOpened();
}
Also used : HeadlessException(java.awt.HeadlessException) Timer(javax.swing.Timer)

Example 49 with HeadlessException

use of java.awt.HeadlessException in project Lucee by lucee.

the class Image method toBufferedImage.

// This method returns a buffered image with the contents of an image
public static BufferedImage toBufferedImage(java.awt.Image image) {
    if (image instanceof BufferedImage) {
        return (BufferedImage) image;
    }
    // This code ensures that all the pixels in the image are loaded
    image = new ImageIcon(image).getImage();
    // Determine if the image has transparent pixels; for this method's
    boolean hasAlpha = hasAlpha(image);
    // Create a buffered image with a format that's compatible with the screen
    BufferedImage bimage = null;
    GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
    try {
        // Determine the type of transparency of the new buffered image
        int transparency = Transparency.OPAQUE;
        if (hasAlpha) {
            transparency = Transparency.BITMASK;
        }
        // Create the buffered image
        GraphicsDevice gs = ge.getDefaultScreenDevice();
        GraphicsConfiguration gc = gs.getDefaultConfiguration();
        bimage = gc.createCompatibleImage(image.getWidth(null), image.getHeight(null), transparency);
    } catch (HeadlessException e) {
    // The system does not have a screen
    }
    if (bimage == null) {
        // Create a buffered image using the default color model
        int type = BufferedImage.TYPE_INT_RGB;
        if (hasAlpha) {
            type = BufferedImage.TYPE_INT_ARGB;
        }
        bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);
    }
    // Copy image to buffered image
    Graphics g = bimage.createGraphics();
    // Paint the image onto the buffered image
    g.drawImage(image, 0, 0, null);
    g.dispose();
    return bimage;
}
Also used : Graphics(java.awt.Graphics) ImageIcon(javax.swing.ImageIcon) GraphicsDevice(java.awt.GraphicsDevice) HeadlessException(java.awt.HeadlessException) GraphicsEnvironment(java.awt.GraphicsEnvironment) BufferedImage(java.awt.image.BufferedImage) Point(java.awt.Point) GraphicsConfiguration(java.awt.GraphicsConfiguration)

Example 50 with HeadlessException

use of java.awt.HeadlessException in project eclipse.platform.releng by eclipse.

the class AwtScreenshot method main.

public static void main(String[] args) {
    try {
        System.setProperty("java.awt.headless", "false");
        Robot robot = new Robot();
        Rectangle rect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
        BufferedImage image = robot.createScreenCapture(rect);
        File file = new File(args[0]);
        ImageIO.write(image, "png", file);
        System.out.println("AWT screenshot saved to: " + file.getAbsolutePath());
    } catch (HeadlessException e) {
        e.printStackTrace();
    } catch (AWTException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : HeadlessException(java.awt.HeadlessException) Rectangle(java.awt.Rectangle) IOException(java.io.IOException) Robot(java.awt.Robot) File(java.io.File) BufferedImage(java.awt.image.BufferedImage) AWTException(java.awt.AWTException)

Aggregations

HeadlessException (java.awt.HeadlessException)63 IOException (java.io.IOException)19 File (java.io.File)16 Point (java.awt.Point)8 GraphicsConfiguration (java.awt.GraphicsConfiguration)7 BufferedImage (java.awt.image.BufferedImage)7 Dimension (java.awt.Dimension)6 Rectangle (java.awt.Rectangle)6 ActionEvent (java.awt.event.ActionEvent)5 PrintService (javax.print.PrintService)5 StreamPrintService (javax.print.StreamPrintService)5 JFrame (javax.swing.JFrame)5 GraphicsDevice (java.awt.GraphicsDevice)4 GraphicsEnvironment (java.awt.GraphicsEnvironment)4 PrinterException (java.awt.print.PrinterException)4 DialogTypeSelection (javax.print.attribute.standard.DialogTypeSelection)4 ImageIcon (javax.swing.ImageIcon)4 JFileChooser (javax.swing.JFileChooser)4 Frame (java.awt.Frame)3 Graphics (java.awt.Graphics)3