View Javadoc

1   package com.melloware.jukes.gui.view.preferences;
2   
3   import java.awt.BorderLayout;
4   import java.awt.Frame;
5   import java.awt.event.ActionEvent;
6   import java.util.prefs.BackingStoreException;
7   import java.util.prefs.Preferences;
8   
9   import javax.swing.AbstractAction;
10  import javax.swing.Action;
11  import javax.swing.JButton;
12  import javax.swing.JComponent;
13  import javax.swing.JPanel;
14  import javax.swing.JTabbedPane;
15  
16  import org.apache.commons.logging.Log;
17  import org.apache.commons.logging.LogFactory;
18  
19  import com.jgoodies.forms.factories.Borders;
20  import com.jgoodies.forms.factories.ButtonBarFactory;
21  import com.jgoodies.forms.layout.Sizes;
22  import com.jgoodies.uif.AbstractDialog;
23  import com.jgoodies.uif.application.Application;
24  import com.jgoodies.uif.application.ResourceIDs;
25  import com.jgoodies.uif.util.Resizer;
26  import com.jgoodies.uif.util.ResourceUtils;
27  import com.jgoodies.uifextras.panel.HeaderPanel;
28  import com.l2fprod.common.propertysheet.Property;
29  import com.l2fprod.common.propertysheet.PropertySheetPanel;
30  import com.melloware.jukes.gui.tool.Settings;
31  import com.melloware.jukes.util.GuiUtil;
32  import com.melloware.jukes.util.MessageUtil;
33  
34  /**
35   * Builds the preferences dialog.
36   * <p>
37   * Copyright (c) 1999-2007 Melloware, Inc. <http://www.melloware.com>
38   * @author Emil A. Lefkof III <info@melloware.com>
39   * @version 4.0
40   */
41  public final class PreferencesDialog
42      extends AbstractDialog {
43      private static final Log LOG = LogFactory.getLog(PreferencesDialog.class);
44  
45      /**
46       * Holds the panel for the look&amp;feel choice and preview.
47       */
48      private LookAndFeelPanel lafPanel;
49  
50      /**
51       * Holds the panel for the settings editor
52       */
53      private PropertySheetSettings settingsSheet;
54  
55      /**
56       * Refers to the settings to be edited.
57       */
58      private final Settings settings;
59      
60      private JComponent mainPanel;
61      private JTabbedPane tabbedPane;
62  
63      /**
64       * Constructs a <code>PreferencesDialog</code>.
65       *
66       * @param owner      this dialog's parent frame
67       * @param settings   the settings to edit
68       */
69      public PreferencesDialog(Frame owner, Settings settings) {
70          super(owner);
71          this.settings = settings;
72      }
73  
74      /* (non-Javadoc)
75       * @see com.jgoodies.swing.AbstractBoundDialog#doApply()
76       */
77      public void doApply() {
78      	LOG.debug("Apply pressed");
79          GuiUtil.setBusyCursor(this, true);
80  
81          // close all editors editing
82          GuiUtil.stopTableEditing(settingsSheet.getSheet().getTable());
83  
84          // loop through the properties and set the Settings
85          final PropertySheetPanel sheet = settingsSheet.getSheet();
86          for (int i = 0; i < sheet.getPropertyCount(); i++) {
87              final Property property = sheet.getProperties()[i];
88              property.writeToObject(settings);
89          }
90  
91          GuiUtil.setBusyCursor(this, false);
92          super.doApply();
93      }
94  
95  
96      /**
97       * Builds and returns the preference's content pane.
98       */
99      protected JComponent buildContent() {
100     	mainPanel = new JPanel(new BorderLayout());
101         JButton[] buttons = new JButton[3];
102         Action restore = new AbstractAction("Restore Defaults") {
103             public void actionPerformed(ActionEvent evt) {
104                 restoreDefaults(evt);
105             }
106         };
107         JButton buttonRestore = new JButton(restore);
108         JButton buttonApply = createAcceptButton("Apply", true);
109         buttons[0] = buttonRestore;
110         buttons[1] = buttonApply;
111         buttons[2] = createCloseButton(true);
112         JPanel buttonBar = ButtonBarFactory.buildRightAlignedBar(buttons);
113         buttonBar.setBorder(Borders.BUTTON_BAR_GAP_BORDER);
114         tabbedPane = buildTabbedPane();
115         mainPanel.add(tabbedPane , BorderLayout.CENTER);
116         mainPanel.add(buttonBar, BorderLayout.SOUTH);
117         return mainPanel;
118     }
119 
120     /**
121     * Builds and returns the dialog's system tab.
122     *
123     * @return the system tab component
124     */
125     protected JComponent buildGeneralTab() {
126         settingsSheet = new PropertySheetSettings(settings);
127         int width = Sizes.dialogUnitXAsPixel(300, settingsSheet);
128         settingsSheet.setPreferredSize(Resizer.DEFAULT.fromWidth(width));
129         settingsSheet.setBorder(Borders.DIALOG_BORDER);
130         return settingsSheet;
131     }
132 
133     /**
134      * Builds and returns the preference's header.
135      */
136     protected JComponent buildHeader() {
137         return new HeaderPanel("Preferences",
138                                "You can see and edit the tool's persistent preferences."
139                                + "\nClick on a tab to select a different category.",
140                                ResourceUtils.getIcon(ResourceIDs.PREFERENCES_ICON));
141     }
142 
143     /**
144      * Builds and returns the tabbed pane.
145      */
146     protected JTabbedPane buildTabbedPane() {
147     	//final LafChoiceModel model = new LafChoiceModel(getTriggerChannel());
148         lafPanel = new LookAndFeelPanel(getTriggerChannel());
149 
150         JTabbedPane pane = new JTabbedPane();
151         pane.addTab("General", buildGeneralTab());
152         pane.addTab("Look & Feel", lafPanel);
153         return pane;
154     }
155 
156     /**
157      * Closes the window.
158      */
159     protected void doCloseWindow() {
160         doCancel();
161     }
162 
163     /**
164      * Unlike the default try to get an aspect ratio of 1:1.
165      */
166     protected void resizeHook(JComponent component) {
167         Resizer.ONE2ONE.resizeDialogContent(component);
168     }
169 
170     /**
171      * Restore defaults to the Settings.
172      * <p>
173      * @param aEvt the ActionEvent fired
174      */
175     protected void restoreDefaults(ActionEvent aEvt) {
176     	LOG.debug("Restore Defaults");
177     	
178     	if (!MessageUtil.confirmUpdate(this)) {
179     		return;
180     	}
181     	
182         Preferences prefs = Application.getUserPreferences();
183         try {
184             prefs.clear();
185             settings.restoreFrom(prefs);
186             settingsSheet = new PropertySheetSettings(settings);
187             tabbedPane.remove(0);
188             tabbedPane.insertTab("General", null, settingsSheet, "General", 0);
189             tabbedPane.setSelectedIndex(0);
190             mainPanel.updateUI();
191         } catch (BackingStoreException ex) {
192             LOG.error("BackingStoreException", ex);
193         }
194 
195     }
196 
197 }