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