1 /*
   2  * Copyright (c) 1998, 2011, Oracle and/or its affiliates. All rights reserved.
   3  *
   4  * Redistribution and use in source and binary forms, with or without
   5  * modification, are permitted provided that the following conditions
   6  * are met:
   7  *
   8  *   - Redistributions of source code must retain the above copyright
   9  *     notice, this list of conditions and the following disclaimer.
  10  *
  11  *   - Redistributions in binary form must reproduce the above copyright
  12  *     notice, this list of conditions and the following disclaimer in the
  13  *     documentation and/or other materials provided with the distribution.
  14  *
  15  *   - Neither the name of Oracle nor the names of its
  16  *     contributors may be used to endorse or promote products derived
  17  *     from this software without specific prior written permission.
  18  *
  19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  20  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  21  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  23  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  24  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  26  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  27  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  28  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  29  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30  */
  31 
  32 /*
  33  * This source code is provided to illustrate the usage of a given feature
  34  * or technique and has been deliberately simplified. Additional steps
  35  * required for a production-quality application, such as security checks,
  36  * input validation and proper error handling, might not be present in
  37  * this sample code.
  38  */
  39 
  40 
  41 
  42 import java.awt.BorderLayout;
  43 import java.awt.Component;
  44 import java.awt.Container;
  45 import java.awt.Dimension;
  46 import java.awt.FlowLayout;
  47 import java.awt.GridLayout;
  48 import java.awt.Insets;
  49 import java.awt.LayoutManager;
  50 import java.awt.event.ActionEvent;
  51 import java.awt.event.ActionListener;
  52 import javax.swing.ButtonGroup;
  53 import javax.swing.JButton;
  54 import javax.swing.JCheckBox;
  55 import javax.swing.JComboBox;
  56 import javax.swing.JDialog;
  57 import javax.swing.JFrame;
  58 import javax.swing.JLabel;
  59 import javax.swing.JPanel;
  60 import javax.swing.JRadioButton;
  61 import javax.swing.JTabbedPane;
  62 import javax.swing.UIManager;
  63 import javax.swing.border.TitledBorder;
  64 
  65 
  66 /**
  67  * This is dialog which allows users to choose preferences
  68  *
  69  * @author Steve Wilson
  70  * @author Alexander Kouznetsov
  71  */
  72 @SuppressWarnings("serial")
  73 public final class MetalworksPrefs extends JDialog {
  74 
  75     public MetalworksPrefs(JFrame f) {
  76         super(f, "Preferences", true);
  77         JPanel container = new JPanel();
  78         container.setLayout(new BorderLayout());
  79 
  80         JTabbedPane tabs = new JTabbedPane();
  81         JPanel filters = buildFilterPanel();
  82         JPanel conn = buildConnectingPanel();
  83         tabs.addTab("Filters", null, filters);
  84         tabs.addTab("Connecting", null, conn);
  85 
  86 
  87         JPanel buttonPanel = new JPanel();
  88         buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
  89         JButton cancel = new JButton("Cancel");
  90         cancel.addActionListener(new ActionListener() {
  91 
  92             public void actionPerformed(ActionEvent e) {
  93                 CancelPressed();
  94             }
  95         });
  96         buttonPanel.add(cancel);
  97         JButton ok = new JButton("OK");
  98         ok.addActionListener(new ActionListener() {
  99 
 100             public void actionPerformed(ActionEvent e) {
 101                 OKPressed();
 102             }
 103         });
 104         buttonPanel.add(ok);
 105         getRootPane().setDefaultButton(ok);
 106 
 107         container.add(tabs, BorderLayout.CENTER);
 108         container.add(buttonPanel, BorderLayout.SOUTH);
 109         getContentPane().add(container);
 110         pack();
 111         centerDialog();
 112         UIManager.addPropertyChangeListener(new UISwitchListener(container));
 113     }
 114 
 115     public JPanel buildFilterPanel() {
 116         JPanel filters = new JPanel();
 117         filters.setLayout(new GridLayout(1, 0));
 118 
 119         JPanel spamPanel = new JPanel();
 120 
 121         spamPanel.setLayout(new ColumnLayout());
 122         spamPanel.setBorder(new TitledBorder("Spam"));
 123         ButtonGroup spamGroup = new ButtonGroup();
 124         JRadioButton file = new JRadioButton("File in Spam Folder");
 125         JRadioButton delete = new JRadioButton("Auto Delete");
 126         JRadioButton bomb = new JRadioButton("Reverse Mail-Bomb");
 127         spamGroup.add(file);
 128         spamGroup.add(delete);
 129         spamGroup.add(bomb);
 130         spamPanel.add(file);
 131         spamPanel.add(delete);
 132         spamPanel.add(bomb);
 133         file.setSelected(true);
 134         filters.add(spamPanel);
 135 
 136         JPanel autoRespond = new JPanel();
 137         autoRespond.setLayout(new ColumnLayout());
 138         autoRespond.setBorder(new TitledBorder("Auto Response"));
 139 
 140         ButtonGroup respondGroup = new ButtonGroup();
 141         JRadioButton none = new JRadioButton("None");
 142         JRadioButton vaca = new JRadioButton("Send Vacation Message");
 143         JRadioButton thx = new JRadioButton("Send Thank You Message");
 144 
 145         respondGroup.add(none);
 146         respondGroup.add(vaca);
 147         respondGroup.add(thx);
 148 
 149         autoRespond.add(none);
 150         autoRespond.add(vaca);
 151         autoRespond.add(thx);
 152 
 153         none.setSelected(true);
 154         filters.add(autoRespond);
 155 
 156         return filters;
 157     }
 158 
 159     public JPanel buildConnectingPanel() {
 160         JPanel connectPanel = new JPanel();
 161         connectPanel.setLayout(new ColumnLayout());
 162 
 163         JPanel protoPanel = new JPanel();
 164         JLabel protoLabel = new JLabel("Protocol");
 165         JComboBox<String> protocol = new JComboBox<>();
 166         protocol.addItem("SMTP");
 167         protocol.addItem("IMAP");
 168         protocol.addItem("Other...");
 169         protoPanel.add(protoLabel);
 170         protoPanel.add(protocol);
 171 
 172         JPanel attachmentPanel = new JPanel();
 173         JLabel attachmentLabel = new JLabel("Attachments");
 174         JComboBox<String> attach = new JComboBox<>();
 175         attach.addItem("Download Always");
 176         attach.addItem("Ask size > 1 Meg");
 177         attach.addItem("Ask size > 5 Meg");
 178         attach.addItem("Ask Always");
 179         attachmentPanel.add(attachmentLabel);
 180         attachmentPanel.add(attach);
 181 
 182         JCheckBox autoConn = new JCheckBox("Auto Connect");
 183         JCheckBox compress = new JCheckBox("Use Compression");
 184         autoConn.setSelected(true);
 185 
 186         connectPanel.add(protoPanel);
 187         connectPanel.add(attachmentPanel);
 188         connectPanel.add(autoConn);
 189         connectPanel.add(compress);
 190         return connectPanel;
 191     }
 192 
 193     protected void centerDialog() {
 194         Dimension screenSize = this.getToolkit().getScreenSize();
 195         Dimension size = this.getSize();
 196         screenSize.height = screenSize.height / 2;
 197         screenSize.width = screenSize.width / 2;
 198         size.height = size.height / 2;
 199         size.width = size.width / 2;
 200         int y = screenSize.height - size.height;
 201         int x = screenSize.width - size.width;
 202         this.setLocation(x, y);
 203     }
 204 
 205     public void CancelPressed() {
 206         this.setVisible(false);
 207     }
 208 
 209     public void OKPressed() {
 210         this.setVisible(false);
 211     }
 212 }
 213 
 214 
 215 class ColumnLayout implements LayoutManager {
 216 
 217     int xInset = 5;
 218     int yInset = 5;
 219     int yGap = 2;
 220 
 221     public void addLayoutComponent(String s, Component c) {
 222     }
 223 
 224     public void layoutContainer(Container c) {
 225         Insets insets = c.getInsets();
 226         int height = yInset + insets.top;
 227 
 228         Component[] children = c.getComponents();
 229         Dimension compSize = null;
 230         for (Component child : children) {
 231             compSize = child.getPreferredSize();
 232             child.setSize(compSize.width, compSize.height);
 233             child.setLocation(xInset + insets.left, height);
 234             height += compSize.height + yGap;
 235         }
 236 
 237     }
 238 
 239     public Dimension minimumLayoutSize(Container c) {
 240         Insets insets = c.getInsets();
 241         int height = yInset + insets.top;
 242         int width = 0 + insets.left + insets.right;
 243 
 244         Component[] children = c.getComponents();
 245         Dimension compSize = null;
 246         for (Component child : children) {
 247             compSize = child.getPreferredSize();
 248             height += compSize.height + yGap;
 249             width = Math.max(width, compSize.width + insets.left + insets.right + xInset
 250                     * 2);
 251         }
 252         height += insets.bottom;
 253         return new Dimension(width, height);
 254     }
 255 
 256     public Dimension preferredLayoutSize(Container c) {
 257         return minimumLayoutSize(c);
 258     }
 259 
 260     public void removeLayoutComponent(Component c) {
 261     }
 262 }