1 package com.jbidwatcher.ui.config;
2 /*
3  * Copyright (c) 2000-2007, CyberFOX Software, Inc. All Rights Reserved.
4  *
5  * Developed by mrs (Morgan Schweers)
6  */
7 
8 import com.cyberfox.util.platform.Platform;
9 import com.jbidwatcher.util.config.*;
10 import com.jbidwatcher.util.Constants;
11 import com.jbidwatcher.ui.util.JBidFrame;
12 import com.jbidwatcher.ui.util.OptionUI;
13 
14 import java.awt.*;
15 import java.awt.event.ActionEvent;
16 import java.awt.event.ActionListener;
17 import java.awt.event.WindowAdapter;
18 import java.awt.event.WindowEvent;
19 import java.util.List;
20 import java.util.ArrayList;
21 
22 import javax.swing.*;
23 import javax.swing.border.TitledBorder;
24 
25 /**
26  * Implements the "Configure" frames. This holds all the configuration options.
27  *
28  * @version $Revision: 1.38 $
29  */
30 public class JConfigFrame implements ActionListener {
31   private JFrame mainFrame;
32   private boolean buttonPressed = false;
33   private List<JConfigTab> allTabs;
34   private static int cfgCount = 1;
35   private static JButton advancedToggleButton;
36   private JConfigTab quickTab, advancedTab;
37   private JPanel cards;
38 
spinWait()39   public void spinWait() {
40     while(!buttonPressed) {
41       try { //noinspection MultiplyOrDivideByPowerOfTwo,BusyWait
42         Thread.sleep(Constants.ONE_SECOND/2);
43       } catch(InterruptedException ignored) {
44         //  We don't care that we caught an exception, just that we woke up.
45       }
46     }
47   }
48 
JConfigFrame()49   public JConfigFrame() {
50     mainFrame = createConfigFrame();
51     Rectangle rec = OptionUI.findCenterBounds(mainFrame.getPreferredSize());
52     mainFrame.setLocation(rec.x, rec.y);
53     show();
54   }
55 
show()56   public final void show() {
57     for (JConfigTab jct : allTabs) {
58       jct.updateValues();
59     }
60     mainFrame.setState(Frame.NORMAL);
61     mainFrame.setVisible(true);
62   }
63 
applyAll()64   private void applyAll() {
65     for (JConfigTab jct : allTabs) {
66       jct.apply();
67     }
68   }
69 
cancelAll()70   private void cancelAll() {
71     for (JConfigTab jct : allTabs) {
72       jct.cancel();
73     }
74   }
75 
actionPerformed(ActionEvent ae)76   public void actionPerformed(ActionEvent ae) {
77     String actionString = ae.getActionCommand();
78 
79     if(actionString.equals("Save")) {
80       applyAll();
81       JConfig.updateComplete();
82       JConfig.saveConfiguration();
83     } else if(actionString.equals("Cancel")) {
84       cancelAll();
85     } else if(actionString.equals("Advanced")) {
86       CardLayout swap = (CardLayout) cards.getLayout();
87       advancedToggleButton.setText("Quick");
88       advancedToggleButton.setActionCommand("Quick");
89       JConfig.setConfiguration("config.level", "advanced");
90 
91       for (JConfigTab jct : allTabs) {
92         jct.updateValues();
93       }
94 
95       swap.show(cards, ADVANCED_CARD);
96       return;
97     } else if(actionString.equals("Quick")) {
98       CardLayout swap = (CardLayout) cards.getLayout();
99       advancedToggleButton.setText("Advanced");
100       advancedToggleButton.setActionCommand("Advanced");
101       JConfig.setConfiguration("config.level", "quick");
102       quickTab.updateValues();
103       swap.show(cards, QUICK_CARD);
104       return;
105     }
106 
107     mainFrame.setVisible(false);
108     buttonPressed = true;
109   }
110 
buildButtonPane(ActionListener al)111   public static JPanel buildButtonPane(ActionListener al) {
112     JPanel tp = new JPanel();
113 
114     JButton cancelButton = new JButton("Cancel");
115     cancelButton.setToolTipText("Cancel any changes made.");
116     JButton saveButton = new JButton("Save");
117     saveButton.setToolTipText("Apply changes and save settings.");
118 
119     boolean isInQuickMode = JConfig.queryConfiguration("config.level", "quick").equals("quick");
120     advancedToggleButton = new JButton(isInQuickMode ? "Advanced" : "Quick");
121     advancedToggleButton.setToolTipText("Switch between advanced and quick configuration");
122 
123     tp.add(advancedToggleButton, BorderLayout.WEST);
124     tp.add(cancelButton, BorderLayout.CENTER);
125     tp.add(  saveButton, BorderLayout.EAST);
126 
127     advancedToggleButton.addActionListener(al);
128     cancelButton.addActionListener(al);
129     saveButton.addActionListener(al);
130 
131     return(tp);
132   }
133 
anotherConfig()134   private static void anotherConfig() {
135     cfgCount++;
136   }
137 
138   private static String QUICK_CARD = "Quick Configuration";
139   private static String ADVANCED_CARD = "Advanced Configuration";
140 
createConfigFrame()141   private JFrame createConfigFrame() {
142     JTabbedPane jtpAllTabs = new JTabbedPane();
143     final JFrame w;
144 
145     if(cfgCount == 2) {
146       w = new JBidFrame("Configuration Manager (2)");
147     } else {
148       anotherConfig();
149       w = new JBidFrame("Configuration Manager");
150     }
151 
152     Container contentPane = w.getContentPane();
153     contentPane.setLayout(new BorderLayout());
154     establishCards(jtpAllTabs, contentPane);
155 
156     allTabs = new ArrayList<JConfigTab>();
157 
158     //  Add all non-server-specific tabs here.
159     allTabs.add(new JConfigGeneralTab());
160     allTabs.add(new JConfigEbayTab(false));
161     allTabs.add(quickTab);
162 
163     //  Stub the browser tab under MacOSX, so they don't try to use it.
164     if(Platform.isMac()) {
165       allTabs.add(new JConfigMacBrowserTab());
166     } else {
167       allTabs.add(new JConfigBrowserTab());
168     }
169     allTabs.add(new JConfigFirewallTab());
170     allTabs.add(new JConfigSnipeTab());
171 //    if(JConfig.queryConfiguration("allow.my_jbidwatcher", "false").equals("true"))
172       allTabs.add(new JConfigMyJBidwatcherTab());
173     allTabs.add(new JConfigFilePathTab());
174     allTabs.add(new JConfigWebserverTab());
175     allTabs.add(new JConfigDatabaseTab());
176 
177     allTabs.add(new JConfigSecurityTab());
178     allTabs.add(new JConfigAdvancedTab());
179 
180     //  HACKHACK -- Presently all tabs created need to have 3 rows of
181     //  GridLayout.  In general, all tabs have to have the same number
182     //  of rows.  This is likely to suck, in the long run.  For now,
183     //  it's the requirement.  If you have more or less, the display
184     //  either for that tab (if it has less), or for all the others
185     //  (if it has more) will look somewhat wonky.
186 
187     //  Loop over all tabs, and add them to the display.
188     for (JConfigTab allTab : allTabs) {
189       if(allTab != quickTab) {
190         allTab.setOpaque(true);
191         jtpAllTabs.addTab(allTab.getTabName(), allTab);
192       }
193     }
194 
195     jtpAllTabs.setSelectedIndex(0);
196     contentPane.add(buildButtonPane(this), BorderLayout.SOUTH);
197 
198     w.addWindowListener(new IconifyingWindowAdapter(w));
199     w.pack();
200     w.setResizable(false);
201     return w;
202   }
203 
establishCards(JTabbedPane jtpAllTabs, Container contentPane)204   private void establishCards(JTabbedPane jtpAllTabs, Container contentPane) {CardLayout swapper = new CardLayout();
205     cards = new JPanel(swapper);
206     contentPane.add(cards, BorderLayout.CENTER);
207     quickTab = new JConfigEbayTab(true);
208     JPanel quickPanel = new JPanel(new BorderLayout());
209     quickPanel.setBorder(BorderFactory.createTitledBorder(null, "Quick Start Configuration", TitledBorder.CENTER, TitledBorder.ABOVE_TOP));
210     quickPanel.add(quickTab, BorderLayout.CENTER);
211 
212     // First added is default
213     cards.add(quickPanel, QUICK_CARD);
214     cards.add(jtpAllTabs, ADVANCED_CARD);
215     if(JConfig.queryConfiguration("config.level", "quick").equals("advanced")) {
216       swapper.show(cards, ADVANCED_CARD);
217     }
218   }
219 
220   private class JConfigSecurityTab extends JConfigStubTab {
getTabName()221     public String getTabName() { return("Security"); }
222   }
223 
224   private final class JConfigMacBrowserTab extends JConfigStubTab {
getTabName()225     public String getTabName() { return("Browser"); }
226 
JConfigMacBrowserTab()227     JConfigMacBrowserTab() {
228       JLabel newLabel = new JLabel("Under MacOSX, the browser does not need to be configured.");
229       setLayout(new BorderLayout());
230       add(newLabel, BorderLayout.CENTER);
231     }
232   }
233 
234   public static class IconifyingWindowAdapter extends WindowAdapter {
235     private final JFrame _window;
236 
IconifyingWindowAdapter(JFrame window)237     public IconifyingWindowAdapter(JFrame window) {
238       _window = window;
239     }
240 
windowIconified(WindowEvent we)241     public void windowIconified(WindowEvent we) {
242       super.windowIconified(we);
243       if(Platform.supportsTray() && Platform.isTrayEnabled()) {
244         if(JConfig.queryConfiguration("windows.tray", "true").equals("true") &&
245            JConfig.queryConfiguration("windows.minimize", "true").equals("true")) {
246           _window.setVisible(false);
247         }
248       }
249     }
250 
windowDeiconified(WindowEvent we)251     public void windowDeiconified(WindowEvent we) {
252       super.windowDeiconified(we);
253       if(Platform.supportsTray() && Platform.isTrayEnabled()) {
254         if(JConfig.queryConfiguration("windows.tray", "true").equals("true") &&
255            JConfig.queryConfiguration("windows.minimize", "true").equals("true")) {
256           _window.setState(Frame.NORMAL);
257           _window.setVisible(true);
258         }
259       }
260     }
261   }
262 }
263