Example usage for javax.swing JFileChooser addComponentListener

List of usage examples for javax.swing JFileChooser addComponentListener

Introduction

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

Prototype

public synchronized void addComponentListener(ComponentListener l) 

Source Link

Document

Adds the specified component listener to receive component events from this component.

Usage

From source file:org.slage.handlers.SaveWithChooserDialog.java

/** Save the object */
protected void fire() {
    if (getTarget() instanceof org.slage.SlageGame == false)
        setTarget(org.slage.Slage.getCurrentGame());
    org.slage.SlageGame game = (org.slage.SlageGame) getTarget();

    game.pause();//w w w  .  j a  v  a 2  s  . c  o  m
    javax.swing.JFileChooser chooser = new javax.swing.JFileChooser();
    chooser.addComponentListener(game.getFrame());

    int iReturn = -1;
    try {
        chooser.setFileFilter(org.slage.xml.XMLProcessor.XMLFilter);
        iReturn = chooser.showSaveDialog(game.getFrame());
    } catch (Exception ex) {
        LOG.warn(ex);
        game.resume();
    }

    if (iReturn == JFileChooser.APPROVE_OPTION) {
        String strFile = chooser.getSelectedFile().getAbsolutePath();
        if (strFile.toUpperCase().endsWith(".XML") == false)
            strFile = strFile + ".XML";

        if (strFile != null && getTarget() != null) {
            try {
                XMLProcessor.save(strFile, getTarget());
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    game.resume();
}

From source file:org.zeromeaner.gui.reskin.StandaloneFrame.java

private void createCards() {
    introPanel = new StandaloneLicensePanel();
    content.add(introPanel, CARD_INTRO);

    playCard = new JPanel(new BorderLayout());
    content.add(playCard, CARD_PLAY);//from   ww w  . j a  v  a2  s.  co  m

    JPanel confirm = new JPanel(new GridBagLayout());
    JOptionPane option = new JOptionPane("A game is in open.  End this game?", JOptionPane.QUESTION_MESSAGE,
            JOptionPane.YES_NO_OPTION);
    option.addPropertyChangeListener(JOptionPane.VALUE_PROPERTY, new PropertyChangeListener() {
        @Override
        public void propertyChange(PropertyChangeEvent evt) {
            if (!CARD_PLAY_END.equals(currentCard))
                return;
            if (evt.getNewValue().equals(JOptionPane.YES_OPTION)) {
                gamePanel.shutdown();
                try {
                    gamePanel.shutdownWait();
                } catch (InterruptedException ie) {
                }
                contentCards.show(content, nextCard);
                currentCard = nextCard;
            }
            if (evt.getNewValue().equals(JOptionPane.NO_OPTION)) {
                contentCards.show(content, CARD_PLAY);
                currentCard = CARD_PLAY;
                playButton.setSelected(true);
            }

            ((JOptionPane) evt.getSource()).setValue(-1);
        }
    });
    GridBagConstraints cx = new GridBagConstraints(0, 0, 1, 1, 1, 1, GridBagConstraints.CENTER,
            GridBagConstraints.NONE, new Insets(10, 10, 10, 10), 0, 0);
    confirm.add(option, cx);
    content.add(confirm, CARD_PLAY_END);

    content.add(new StandaloneModeselectPanel(), CARD_MODESELECT);

    netplayCard = new JPanel(new BorderLayout());
    netplayCard.add(netLobby, BorderLayout.SOUTH);
    content.add(netplayCard, CARD_NETPLAY);

    confirm = new JPanel(new GridBagLayout());
    option = new JOptionPane("A netplay game is open.  End this game and disconnect?",
            JOptionPane.QUESTION_MESSAGE, JOptionPane.YES_NO_OPTION);
    option.addPropertyChangeListener(JOptionPane.VALUE_PROPERTY, new PropertyChangeListener() {
        @Override
        public void propertyChange(PropertyChangeEvent evt) {
            if (!CARD_NETPLAY_END.equals(currentCard))
                return;
            if (evt.getNewValue().equals(JOptionPane.YES_OPTION)) {
                gamePanel.shutdown();
                try {
                    gamePanel.shutdownWait();
                } catch (InterruptedException ie) {
                }
                netLobby.disconnect();
                contentCards.show(content, nextCard);
                currentCard = nextCard;
            }
            if (evt.getNewValue().equals(JOptionPane.NO_OPTION)) {
                contentCards.show(content, CARD_NETPLAY);
                currentCard = CARD_NETPLAY;
                netplayButton.setSelected(true);
            }

            ((JOptionPane) evt.getSource()).setValue(-1);
        }
    });
    cx = new GridBagConstraints(0, 0, 1, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.NONE,
            new Insets(10, 10, 10, 10), 0, 0);
    confirm.add(option, cx);
    content.add(confirm, CARD_NETPLAY_END);

    StandaloneKeyConfig kc = new StandaloneKeyConfig(this);
    kc.load(0);
    content.add(kc, CARD_KEYS_1P);
    kc = new StandaloneKeyConfig(this);
    kc.load(1);
    content.add(kc, CARD_KEYS_2P);

    StandaloneGameTuningPanel gt = new StandaloneGameTuningPanel();
    gt.load(0);
    content.add(gt, CARD_TUNING_1P);
    gt = new StandaloneGameTuningPanel();
    gt.load(1);
    content.add(gt, CARD_TUNING_2P);

    StandaloneAISelectPanel ai = new StandaloneAISelectPanel();
    ai.load(0);
    content.add(ai, CARD_AI_1P);
    ai = new StandaloneAISelectPanel();
    ai.load(1);
    content.add(ai, CARD_AI_2P);

    StandaloneGeneralConfigPanel gc = new StandaloneGeneralConfigPanel();
    gc.load();
    content.add(gc, CARD_GENERAL);

    final JFileChooser fc = FileSystemViews.get().fileChooser("replay/");

    fc.setFileFilter(new FileFilter() {
        @Override
        public String getDescription() {
            return "Zeromeaner Replay Files";
        }

        @Override
        public boolean accept(File f) {
            return f.isDirectory() || f.getName().endsWith(".zrep");
        }
    });

    fc.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            if (!e.getActionCommand().equals(JFileChooser.APPROVE_SELECTION))
                return;
            JFileChooser fc = (JFileChooser) e.getSource();
            String path = fc.getSelectedFile().getPath();
            if (!path.contains("replay/"))
                path = "replay/" + path;
            startReplayGame(path);
        }
    });
    fc.addComponentListener(new ComponentAdapter() {
        @Override
        public void componentShown(ComponentEvent e) {
            fc.rescanCurrentDirectory();
        }
    });
    content.add(fc, CARD_OPEN);

    content.add(new StandaloneFeedbackPanel(), CARD_FEEDBACK);
}