View Javadoc

1   package net.sourceforge.simplegamenet.framework.gui;
2   
3   import java.awt.*;
4   import java.awt.event.ActionEvent;
5   import java.awt.event.ActionListener;
6   import javax.swing.*;
7   import net.sourceforge.simplegamenet.util.proportionlayout.ProportionConstraints;
8   import net.sourceforge.simplegamenet.util.proportionlayout.ProportionLayout;
9   
10  public class OptionsDialog extends JDialog implements ActionListener {
11  
12      private SimpleGameNetFrame simpleGameNetFrame;
13  
14      private JTabbedPane tabbedPane = new JTabbedPane();
15  
16      private JButton optionsOkButton;
17      private JButton optionsCancelButton;
18      private JButton optionsApplyButton;
19  
20      private JPanel generalTabPanel;
21      private JLabel lookAndFeelLabel;
22      private JComboBox lookAndFeelComboBox;
23  
24      private UIManager.LookAndFeelInfo[] lookAndFeelInfos;
25  
26      public OptionsDialog(SimpleGameNetFrame simpleGameNetFrame) {
27          super(simpleGameNetFrame, "Options", true);
28          this.simpleGameNetFrame = simpleGameNetFrame;
29          Container contentPanel = getContentPane();
30          ProportionLayout layout = new ProportionLayout();
31          layout.appendColumn(10);                                // 0 empty
32          layout.appendColumn(0, 1.0);                            // 1 empty
33          layout.appendColumn(0, ProportionLayout.NO_PROPORTION); // 2 optionsOkButton
34          layout.appendColumn(10);                                // 3 empty
35          layout.appendColumn(0, ProportionLayout.NO_PROPORTION); // 4 optionsCancelButton
36          layout.appendColumn(10);                                // 5 empty
37          layout.appendColumn(0, ProportionLayout.NO_PROPORTION); // 6 optionsApplyButton
38          layout.appendColumn(10);                                // 7 empty
39          layout.appendRow(10);                                // 0 empty
40          layout.appendRow(0, 1.0);                            // 1 optionsTabPanel
41          layout.appendRow(10);                                // 2 empty
42          layout.appendRow(0, ProportionLayout.NO_PROPORTION); // 3 optionDialogButtons
43          layout.appendRow(10);                                // 4 empty
44          contentPanel.setLayout(layout);
45          ProportionLayout generalTabLayout = new ProportionLayout();
46          generalTabLayout.appendColumn(10);                                // 0 empty
47          generalTabLayout.appendColumn(0, ProportionLayout.NO_PROPORTION); // 1 labels
48          generalTabLayout.appendColumn(10);                                // 2 empty
49          generalTabLayout.appendColumn(0, 1.0);                            // 3 choice
50          generalTabLayout.appendColumn(10);                                // 4 empty
51          generalTabLayout.appendRow(10);                                // 0 empty
52          generalTabLayout.appendRow(0, ProportionLayout.NO_PROPORTION); // 1 lookAndFeel
53          generalTabLayout.appendRow(10);                                // 2 empty
54          generalTabPanel = new JPanel(generalTabLayout);
55          lookAndFeelLabel = new JLabel("Look and feel:");
56          lookAndFeelComboBox = new JComboBox();
57          lookAndFeelInfos = UIManager.getInstalledLookAndFeels();
58          for (int i = 0; i < lookAndFeelInfos.length; i++) {
59              lookAndFeelComboBox.addItem(lookAndFeelInfos[i].getName());
60          }
61          LookAndFeel lookAndFeel = UIManager.getLookAndFeel();
62          if (lookAndFeel != null) {
63              for (int i = 0; i < lookAndFeelInfos.length; i++) {
64                  if (lookAndFeel.getName().equals(lookAndFeelInfos[i].getName())) {
65                      lookAndFeelComboBox.setSelectedIndex(i);
66                      break;
67                  }
68              }
69          }
70          generalTabPanel.add(lookAndFeelLabel, new ProportionConstraints(1, 1));
71          generalTabPanel.add(lookAndFeelComboBox, new ProportionConstraints(3, 1));
72          tabbedPane.add("General", generalTabPanel);
73          contentPanel.add(tabbedPane, new ProportionConstraints(1, 1, 6, 1, 1, 1));
74          optionsOkButton = new JButton("OK");
75          optionsOkButton.addActionListener(this);
76          optionsCancelButton = new JButton("Cancel");
77          optionsCancelButton.addActionListener(this);
78          optionsApplyButton = new JButton("Apply");
79          optionsApplyButton.addActionListener(this);
80          contentPanel.add(optionsOkButton, new ProportionConstraints(2, 3));
81          contentPanel.add(optionsCancelButton, new ProportionConstraints(4, 3));
82          contentPanel.add(optionsApplyButton, new ProportionConstraints(6, 3));
83  
84          setResizable(false);
85          pack();
86          Dimension frameSize = simpleGameNetFrame.getSize();
87          Dimension dialogSize = getSize();
88          setLocation((frameSize.width - dialogSize.width) / 2,
89                  (frameSize.height - dialogSize.height) / 2);
90          setLocationRelativeTo(simpleGameNetFrame);
91      }
92  
93      public void actionPerformed(ActionEvent actionEvent) {
94          if (actionEvent.getSource() == optionsOkButton) {
95              apply();
96              dispose();
97          } else if (actionEvent.getSource() == optionsCancelButton) {
98              dispose();
99          } else if (actionEvent.getSource() == optionsApplyButton) {
100             apply();
101         }
102     }
103 
104     private void apply() {
105         try {
106             UIManager.setLookAndFeel(lookAndFeelInfos[lookAndFeelComboBox.getSelectedIndex()]
107                     .getClassName());
108             SwingUtilities.updateComponentTreeUI(simpleGameNetFrame);
109             SwingUtilities.updateComponentTreeUI(this);
110             pack();
111         } catch (ClassNotFoundException e) {
112             e.printStackTrace();
113         } catch (InstantiationException e) {
114             e.printStackTrace();
115         } catch (IllegalAccessException e) {
116             e.printStackTrace();
117         } catch (UnsupportedLookAndFeelException e) {
118             e.printStackTrace();
119         }
120     }
121 
122 }