Example usage for java.beans VetoableChangeListener VetoableChangeListener

List of usage examples for java.beans VetoableChangeListener VetoableChangeListener

Introduction

In this page you can find the example usage for java.beans VetoableChangeListener VetoableChangeListener.

Prototype

VetoableChangeListener

Source Link

Usage

From source file:Main.java

public static void main(String[] args) {
    final JFrame frame = new JFrame("Test");

    KeyboardFocusManager.getCurrentKeyboardFocusManager().addVetoableChangeListener("focusedWindow",
            new VetoableChangeListener() {
                private boolean gained = false;

                @Override//  w  w  w .j  av  a2 s.co m
                public void vetoableChange(PropertyChangeEvent evt) throws PropertyVetoException {
                    if (evt.getNewValue() == frame) {
                        gained = true;
                    }
                    if (gained && evt.getNewValue() != frame) {
                        frame.dispose();
                    }
                }
            });

    frame.add(new JTextField(10), BorderLayout.NORTH);
    frame.add(new JTextField(10), BorderLayout.SOUTH);

    frame.pack();
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frame.setVisible(true);
}

From source file:InternalFrameTest.java

/**
 * Creates an internal frame on the desktop.
 * @param c the component to display in the internal frame
 * @param t the title of the internal frame.
 *///from  w  ww.  j  a  v  a2s  . c  om
public void createInternalFrame(Component c, String t) {
    final JInternalFrame iframe = new JInternalFrame(t, true, // resizable
            true, // closable
            true, // maximizable
            true); // iconifiable

    iframe.add(c, BorderLayout.CENTER);
    desktop.add(iframe);

    iframe.setFrameIcon(new ImageIcon("document.gif"));

    // add listener to confirm frame closing
    iframe.addVetoableChangeListener(new VetoableChangeListener() {
        public void vetoableChange(PropertyChangeEvent event) throws PropertyVetoException {
            String name = event.getPropertyName();
            Object value = event.getNewValue();

            // we only want to check attempts to close a frame
            if (name.equals("closed") && value.equals(true)) {
                // ask user if it is ok to close
                int result = JOptionPane.showInternalConfirmDialog(iframe, "OK to close?", "Select an Option",
                        JOptionPane.YES_NO_OPTION);

                // if the user doesn't agree, veto the close
                if (result != JOptionPane.YES_OPTION)
                    throw new PropertyVetoException("User canceled close", event);
            }
        }
    });

    // position frame
    int width = desktop.getWidth() / 2;
    int height = desktop.getHeight() / 2;
    iframe.reshape(nextFrameX, nextFrameY, width, height);

    iframe.show();

    // select the frame--might be vetoed
    try {
        iframe.setSelected(true);
    } catch (PropertyVetoException e) {
    }

    frameDistance = iframe.getHeight() - iframe.getContentPane().getHeight();

    // compute placement for next frame

    nextFrameX += frameDistance;
    nextFrameY += frameDistance;
    if (nextFrameX + width > desktop.getWidth())
        nextFrameX = 0;
    if (nextFrameY + height > desktop.getHeight())
        nextFrameY = 0;
}

From source file:org.adempiere.apps.graph.Graph.java

private void loadData() {

    list = builder.loadData();// www.ja v  a  2 s .com
    JFreeChart chart = builder.createChart(builder.getMGoal().getChartType());
    if (chartPanel != null)
        remove(chartPanel);

    chartPanel = new ChartPanel(chart);
    chartPanel.setSize(getSize());
    chartPanel.addChartMouseListener(this);
    add(chartPanel, BorderLayout.CENTER);

    if (m_userSelection) {
        int AD_Reference_Value_ID = DB.getSQLValue(null,
                "SELECT AD_Reference_ID FROM AD_Reference WHERE Name = ?", "PA_Goal ChartType");
        MLookupInfo info = MLookupFactory.getLookup_List(Env.getLanguage(Env.getCtx()), AD_Reference_Value_ID);
        MLookup mLookup = new MLookup(info, 0);
        VLookup lookup = new VLookup("ChartType", false, false, true, mLookup);
        lookup.addVetoableChangeListener(new VetoableChangeListener() {

            public void vetoableChange(PropertyChangeEvent evt) throws PropertyVetoException {
                Object value = evt.getNewValue();
                if (value == null)
                    return;
                JFreeChart chart = null;
                chart = builder.createChart(value.toString());

                if (chart != null) {
                    if (chartPanel != null)
                        remove(chartPanel);

                    chartPanel = new ChartPanel(chart);
                    chartPanel.setSize(getSize());
                    chartPanel.addChartMouseListener(Graph.this);
                    add(chartPanel, BorderLayout.CENTER);
                    getParent().validate();

                }
            }

        });
        add(lookup, BorderLayout.NORTH);
    }
    this.setMinimumSize(paneldimension);
}

From source file:com.anrisoftware.prefdialog.fields.filechooser.FileChooserField.java

/**
 * @see FileChooserFieldFactory#create(Object, String)
 *///from   w  ww. j a va2 s  .  com
@Inject
FileChooserField(FileChooserFieldLogger logger, UiPanel panel, FileTextField fileTextField,
        OpenDialogAction openDialogAction, @Assisted Object parentObject, @Assisted String fieldName) {
    super(panel, parentObject, fieldName);
    this.panel = panel;
    this.log = logger;
    this.fileTextField = fileTextField;
    this.openDialogAction = openDialogAction;
    this.fileListener = lockedVetoableChangeListener(new VetoableChangeListener() {

        @Override
        public void vetoableChange(PropertyChangeEvent evt) throws PropertyVetoException {
            try {
                fileListener.lock();
                setValue(evt.getNewValue());
            } finally {
                fileListener.unlock();
            }
        }
    });
    this.fileTextFieldValueListener = lockedPropertyChangeListener(new PropertyChangeListener() {

        @Override
        public void propertyChange(PropertyChangeEvent evt) {
            try {
                setValue(evt.getNewValue());
            } catch (PropertyVetoException e) {
            }
        }
    });
    this.fileAction = new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            try {
                setValue(FileChooserField.this.fileTextField.getValue());
            } catch (PropertyVetoException e1) {
            }
        }
    };
    setupPanel();
}

From source file:com.sshtools.common.ui.SshToolsApplicationInternalFrame.java

/**
 *
 *
 * @param application/*from  w w  w .ja va2s. c  o  m*/
 * @param panel
 *
 * @throws SshToolsApplicationException
 */
public void init(final SshToolsApplication application, SshToolsApplicationPanel panel)
        throws SshToolsApplicationException {
    this.panel = panel;
    this.application = application;

    if (application != null) {
        setTitle(ConfigurationLoader.getVersionString(application.getApplicationName(),
                application.getApplicationVersion())); // + " " + application.getApplicationVersion());
    }

    // Register the File menu
    panel.registerActionMenu(new SshToolsApplicationPanel.ActionMenu("File", "File", 'f', 0));
    // Register the Exit action
    if (showExitAction && application != null) {
        panel.registerAction(exitAction = new ExitAction(application, this));

        // Register the New Window Action
    }
    if (showNewWindowAction && application != null) {
        panel.registerAction(newWindowAction = new NewWindowAction(application));

        // Register the Help menu
    }
    panel.registerActionMenu(new SshToolsApplicationPanel.ActionMenu("Help", "Help", 'h', 99));

    // Register the About box action
    if (showAboutBox && application != null) {
        panel.registerAction(aboutAction = new AboutAction(this, application));

    }
    // Register the Changelog box action
    if (showChangelogBox && application != null) {
        panel.registerAction(changelogAction = new ChangelogAction(this, application));

    }
    panel.registerAction(faqAction = new FAQAction());
    panel.registerAction(beginnerAction = new BeginnerAction());
    panel.registerAction(advancedAction = new AdvancedAction());

    getApplicationPanel().rebuildActionComponents();

    JPanel p = new JPanel(new BorderLayout());

    if (panel.getJMenuBar() != null) {
        setJMenuBar(panel.getJMenuBar());
    }

    if (panel.getToolBar() != null) {
        JPanel t = new JPanel(new BorderLayout());
        t.add(panel.getToolBar(), BorderLayout.NORTH);
        t.add(toolSeparator = new JSeparator(JSeparator.HORIZONTAL), BorderLayout.SOUTH);
        toolSeparator.setVisible(panel.getToolBar().isVisible());

        final SshToolsApplicationPanel pnl = panel;
        panel.getToolBar().addComponentListener(new ComponentAdapter() {
            public void componentHidden(ComponentEvent evt) {
                log.debug("Tool separator is now " + pnl.getToolBar().isVisible());
                toolSeparator.setVisible(pnl.getToolBar().isVisible());
            }
        });
        p.add(t, BorderLayout.NORTH);
    }

    p.add(panel, BorderLayout.CENTER);

    if (panel.getStatusBar() != null) {
        p.add(panel.getStatusBar(), BorderLayout.SOUTH);
    }

    getContentPane().setLayout(new GridLayout(1, 1));
    getContentPane().add(p);

    // Watch for the frame closing
    //setDefaultCloseOperation(JInternalFrame.DO_NOTHING_ON_CLOSE);

    addVetoableChangeListener(new VetoableChangeListener() {

        public void vetoableChange(PropertyChangeEvent evt) throws PropertyVetoException {

            if (application != null) {
                application.closeContainer(SshToolsApplicationInternalFrame.this);
            } else {
                if (evt.getPropertyName().equals(IS_CLOSED_PROPERTY)) {
                    boolean changed = ((Boolean) evt.getNewValue()).booleanValue();
                    if (changed) {
                        int confirm = JOptionPane.showOptionDialog(SshToolsApplicationInternalFrame.this,
                                "Close " + getTitle() + "?", "Close Operation", JOptionPane.YES_NO_OPTION,
                                JOptionPane.QUESTION_MESSAGE, null, null, null);
                        if (confirm == 0) {
                            SshToolsApplicationInternalFrame.this.getDesktopPane()
                                    .remove(SshToolsApplicationInternalFrame.this);
                        }
                    }
                }
            }
        }
    });

    /*this.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent evt) {
        if(application!=null)
          application.closeContainer(SshToolsApplicationFrame.this);
        else
          hide();
      }
         });
            
    // If this is the first frame, center the window on the screen
    /*Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
         boolean found = false;
         if (application!=null && application.getContainerCount() != 0) {
      for (int i = 0; (i < application.getContainerCount()) && !found;
           i++) {
        SshToolsApplicationContainer c = application.getContainerAt(i);
        if (c instanceof SshToolsApplicationFrame) {
          SshToolsApplicationFrame f = (SshToolsApplicationFrame) c;
          setSize(f.getSize());
          Point newLocation = new Point(f.getX(), f.getY());
          newLocation.x += 48;
          newLocation.y += 48;
          if (newLocation.x > (screenSize.getWidth() - 64)) {
    newLocation.x = 0;
          }
          if (newLocation.y > (screenSize.getHeight() - 64)) {
    newLocation.y = 0;
          }
          setLocation(newLocation);
          found = true;
        }
      }
         }
         if (!found) {
      // Is there a previous stored geometry we can use?
      if (PreferencesStore.preferenceExists(PREF_LAST_FRAME_GEOMETRY)) {
        setBounds(PreferencesStore.getRectangle(
    PREF_LAST_FRAME_GEOMETRY, getBounds()));
      }
      else {
        pack();
        UIUtil.positionComponent(SwingConstants.CENTER, this);
      }
         }*/

    pack();
}

From source file:org.nuclos.client.ui.collect.CollectController.java

protected void initTab() {
    // prevent that the frame is closed when changes are pending:
    getTab().addVetoableChangeListener(new VetoableChangeListener() {
        @Override//w  w  w . j a va2 s  .co m
        public void vetoableChange(PropertyChangeEvent evt) throws PropertyVetoException {
            if (evt.getPropertyName().equals(JInternalFrame.IS_CLOSED_PROPERTY)) {
                final Boolean bOldValue = (Boolean) evt.getOldValue();
                final Boolean bNewValue = (Boolean) evt.getNewValue();

                if (bOldValue == Boolean.FALSE && bNewValue == Boolean.TRUE) {
                    // We need bFrameMayBeClosed as a member variable here, as it is set in
                    // cmdFrameClosing and must be checked here.
                    // JInternalFrame.setClosed() first sends a frame closing event, then a vetoable change event.
                    // Note that this is totally weird. See JInternalFrame.setClosed()
                    if (!CollectController.this.bFrameMayBeClosed) {
                        throw new PropertyVetoException("do not close", evt);
                    }
                }
            }
        }
    });

    // override close behavior:
    getTab().addMainFrameTabListener(new MainFrameTabAdapter() {
        @Override
        public void tabSelected(MainFrameTab tab) {
            setDefaultButton();
        }

        @Override
        public void tabClosing(MainFrameTab tab, final ResultListener<Boolean> rl) {
            askAndSaveIfNecessary(new ResultListener<Boolean>() {
                @Override
                public void done(Boolean result) {
                    rl.done(Boolean.TRUE.equals(result));
                }
            });
        }

        @Override
        public void tabClosed(MainFrameTab tab) {
            CollectController.this.close();
            tab.removeMainFrameTabListener(this);
        }
    });
}