Path: blob/master/src/demo/share/jfc/Metalworks/MetalworksPrefs.java
41152 views
/*1* Copyright (c) 1998, 2011, Oracle and/or its affiliates. All rights reserved.2*3* Redistribution and use in source and binary forms, with or without4* modification, are permitted provided that the following conditions5* are met:6*7* - Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9*10* - Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* - Neither the name of Oracle nor the names of its15* contributors may be used to endorse or promote products derived16* from this software without specific prior written permission.17*18* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS19* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,20* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR21* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR22* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,23* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,24* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR25* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF26* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING27* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS28* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.29*/3031/*32* This source code is provided to illustrate the usage of a given feature33* or technique and has been deliberately simplified. Additional steps34* required for a production-quality application, such as security checks,35* input validation and proper error handling, might not be present in36* this sample code.37*/38394041import java.awt.BorderLayout;42import java.awt.Component;43import java.awt.Container;44import java.awt.Dimension;45import java.awt.FlowLayout;46import java.awt.GridLayout;47import java.awt.Insets;48import java.awt.LayoutManager;49import java.awt.event.ActionEvent;50import java.awt.event.ActionListener;51import javax.swing.ButtonGroup;52import javax.swing.JButton;53import javax.swing.JCheckBox;54import javax.swing.JComboBox;55import javax.swing.JDialog;56import javax.swing.JFrame;57import javax.swing.JLabel;58import javax.swing.JPanel;59import javax.swing.JRadioButton;60import javax.swing.JTabbedPane;61import javax.swing.UIManager;62import javax.swing.border.TitledBorder;636465/**66* This is dialog which allows users to choose preferences67*68* @author Steve Wilson69* @author Alexander Kouznetsov70*/71@SuppressWarnings("serial")72public final class MetalworksPrefs extends JDialog {7374public MetalworksPrefs(JFrame f) {75super(f, "Preferences", true);76JPanel container = new JPanel();77container.setLayout(new BorderLayout());7879JTabbedPane tabs = new JTabbedPane();80JPanel filters = buildFilterPanel();81JPanel conn = buildConnectingPanel();82tabs.addTab("Filters", null, filters);83tabs.addTab("Connecting", null, conn);848586JPanel buttonPanel = new JPanel();87buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));88JButton cancel = new JButton("Cancel");89cancel.addActionListener(new ActionListener() {9091public void actionPerformed(ActionEvent e) {92CancelPressed();93}94});95buttonPanel.add(cancel);96JButton ok = new JButton("OK");97ok.addActionListener(new ActionListener() {9899public void actionPerformed(ActionEvent e) {100OKPressed();101}102});103buttonPanel.add(ok);104getRootPane().setDefaultButton(ok);105106container.add(tabs, BorderLayout.CENTER);107container.add(buttonPanel, BorderLayout.SOUTH);108getContentPane().add(container);109pack();110centerDialog();111UIManager.addPropertyChangeListener(new UISwitchListener(container));112}113114public JPanel buildFilterPanel() {115JPanel filters = new JPanel();116filters.setLayout(new GridLayout(1, 0));117118JPanel spamPanel = new JPanel();119120spamPanel.setLayout(new ColumnLayout());121spamPanel.setBorder(new TitledBorder("Spam"));122ButtonGroup spamGroup = new ButtonGroup();123JRadioButton file = new JRadioButton("File in Spam Folder");124JRadioButton delete = new JRadioButton("Auto Delete");125JRadioButton bomb = new JRadioButton("Reverse Mail-Bomb");126spamGroup.add(file);127spamGroup.add(delete);128spamGroup.add(bomb);129spamPanel.add(file);130spamPanel.add(delete);131spamPanel.add(bomb);132file.setSelected(true);133filters.add(spamPanel);134135JPanel autoRespond = new JPanel();136autoRespond.setLayout(new ColumnLayout());137autoRespond.setBorder(new TitledBorder("Auto Response"));138139ButtonGroup respondGroup = new ButtonGroup();140JRadioButton none = new JRadioButton("None");141JRadioButton vaca = new JRadioButton("Send Vacation Message");142JRadioButton thx = new JRadioButton("Send Thank You Message");143144respondGroup.add(none);145respondGroup.add(vaca);146respondGroup.add(thx);147148autoRespond.add(none);149autoRespond.add(vaca);150autoRespond.add(thx);151152none.setSelected(true);153filters.add(autoRespond);154155return filters;156}157158public JPanel buildConnectingPanel() {159JPanel connectPanel = new JPanel();160connectPanel.setLayout(new ColumnLayout());161162JPanel protoPanel = new JPanel();163JLabel protoLabel = new JLabel("Protocol");164JComboBox<String> protocol = new JComboBox<>();165protocol.addItem("SMTP");166protocol.addItem("IMAP");167protocol.addItem("Other...");168protoPanel.add(protoLabel);169protoPanel.add(protocol);170171JPanel attachmentPanel = new JPanel();172JLabel attachmentLabel = new JLabel("Attachments");173JComboBox<String> attach = new JComboBox<>();174attach.addItem("Download Always");175attach.addItem("Ask size > 1 Meg");176attach.addItem("Ask size > 5 Meg");177attach.addItem("Ask Always");178attachmentPanel.add(attachmentLabel);179attachmentPanel.add(attach);180181JCheckBox autoConn = new JCheckBox("Auto Connect");182JCheckBox compress = new JCheckBox("Use Compression");183autoConn.setSelected(true);184185connectPanel.add(protoPanel);186connectPanel.add(attachmentPanel);187connectPanel.add(autoConn);188connectPanel.add(compress);189return connectPanel;190}191192protected void centerDialog() {193Dimension screenSize = this.getToolkit().getScreenSize();194Dimension size = this.getSize();195screenSize.height = screenSize.height / 2;196screenSize.width = screenSize.width / 2;197size.height = size.height / 2;198size.width = size.width / 2;199int y = screenSize.height - size.height;200int x = screenSize.width - size.width;201this.setLocation(x, y);202}203204public void CancelPressed() {205this.setVisible(false);206}207208public void OKPressed() {209this.setVisible(false);210}211}212213214class ColumnLayout implements LayoutManager {215216int xInset = 5;217int yInset = 5;218int yGap = 2;219220public void addLayoutComponent(String s, Component c) {221}222223public void layoutContainer(Container c) {224Insets insets = c.getInsets();225int height = yInset + insets.top;226227Component[] children = c.getComponents();228Dimension compSize = null;229for (Component child : children) {230compSize = child.getPreferredSize();231child.setSize(compSize.width, compSize.height);232child.setLocation(xInset + insets.left, height);233height += compSize.height + yGap;234}235236}237238public Dimension minimumLayoutSize(Container c) {239Insets insets = c.getInsets();240int height = yInset + insets.top;241int width = 0 + insets.left + insets.right;242243Component[] children = c.getComponents();244Dimension compSize = null;245for (Component child : children) {246compSize = child.getPreferredSize();247height += compSize.height + yGap;248width = Math.max(width, compSize.width + insets.left + insets.right + xInset249* 2);250}251height += insets.bottom;252return new Dimension(width, height);253}254255public Dimension preferredLayoutSize(Container c) {256return minimumLayoutSize(c);257}258259public void removeLayoutComponent(Component c) {260}261}262263264