| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
package org.homeunix.thecave.buddi.view; |
| 5 | |
|
| 6 | |
import java.awt.BorderLayout; |
| 7 | |
import java.awt.FlowLayout; |
| 8 | |
import java.awt.event.ActionEvent; |
| 9 | |
import java.awt.event.ActionListener; |
| 10 | |
import java.util.List; |
| 11 | |
import java.util.logging.Level; |
| 12 | |
import java.util.logging.Logger; |
| 13 | |
|
| 14 | |
import javax.swing.JButton; |
| 15 | |
import javax.swing.JOptionPane; |
| 16 | |
import javax.swing.JPanel; |
| 17 | |
import javax.swing.JTabbedPane; |
| 18 | |
|
| 19 | |
import org.homeunix.thecave.buddi.i18n.BuddiKeys; |
| 20 | |
import org.homeunix.thecave.buddi.i18n.keys.ButtonKeys; |
| 21 | |
import org.homeunix.thecave.buddi.model.prefs.PrefsModel; |
| 22 | |
import org.homeunix.thecave.buddi.plugin.BuddiPluginFactory; |
| 23 | |
import org.homeunix.thecave.buddi.plugin.api.BuddiPreferencePlugin; |
| 24 | |
import org.homeunix.thecave.buddi.plugin.api.exception.PluginException; |
| 25 | |
import org.homeunix.thecave.buddi.plugin.api.util.TextFormatter; |
| 26 | |
import org.homeunix.thecave.buddi.util.InternalFormatter; |
| 27 | |
import org.homeunix.thecave.buddi.view.menu.bars.BuddiMenuBar; |
| 28 | |
|
| 29 | |
import ca.digitalcave.moss.common.ClassLoaderFunctions; |
| 30 | |
import ca.digitalcave.moss.common.OperatingSystemUtil; |
| 31 | |
import ca.digitalcave.moss.swing.ApplicationModel; |
| 32 | |
import ca.digitalcave.moss.swing.MossFrame; |
| 33 | |
|
| 34 | |
public class PreferencesFrame extends MossFrame implements ActionListener { |
| 35 | |
public static final long serialVersionUID = 0; |
| 36 | |
|
| 37 | |
private final JTabbedPane tabs; |
| 38 | |
private final List<BuddiPreferencePlugin> preferencePanels; |
| 39 | |
|
| 40 | |
private final JButton okButton; |
| 41 | |
private final JButton cancelButton; |
| 42 | |
|
| 43 | |
@SuppressWarnings("unchecked") |
| 44 | |
public PreferencesFrame() { |
| 45 | 695 | super("Preferences"); |
| 46 | 695 | this.setIconImage(ClassLoaderFunctions.getImageFromClasspath("img/BuddiFrameIcon.gif")); |
| 47 | 695 | tabs = new JTabbedPane(); |
| 48 | |
|
| 49 | 695 | preferencePanels = (List<BuddiPreferencePlugin>) BuddiPluginFactory.getPlugins(BuddiPreferencePlugin.class); |
| 50 | 695 | okButton = new JButton(PrefsModel.getInstance().getTranslator().get(ButtonKeys.BUTTON_OK)); |
| 51 | 695 | cancelButton = new JButton(PrefsModel.getInstance().getTranslator().get(ButtonKeys.BUTTON_CANCEL)); |
| 52 | 695 | } |
| 53 | |
|
| 54 | |
@Override |
| 55 | |
public void init() { |
| 56 | 695 | super.init(); |
| 57 | |
|
| 58 | |
|
| 59 | 695 | for (BuddiPreferencePlugin preferencePlugin : preferencePanels) { |
| 60 | |
JPanel panel; |
| 61 | 3475 | if (preferencePlugin.isUseWrapper()) |
| 62 | 2780 | panel = getWrapperPanel(preferencePlugin.getPreferencesPanel()); |
| 63 | |
else |
| 64 | 695 | panel = preferencePlugin.getPreferencesPanel(); |
| 65 | 3475 | tabs.addTab(PrefsModel.getInstance().getTranslator().get(preferencePlugin.getName()), panel); |
| 66 | |
|
| 67 | 3475 | } |
| 68 | |
|
| 69 | 695 | JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT)); |
| 70 | 695 | if (OperatingSystemUtil.isMac()){ |
| 71 | 0 | buttonPanel.add(cancelButton); |
| 72 | 0 | buttonPanel.add(okButton); |
| 73 | |
} |
| 74 | |
else { |
| 75 | 695 | buttonPanel.add(okButton); |
| 76 | 695 | buttonPanel.add(cancelButton); |
| 77 | |
} |
| 78 | |
|
| 79 | 695 | okButton.setPreferredSize(InternalFormatter.getButtonSize(okButton)); |
| 80 | 695 | cancelButton.setPreferredSize(InternalFormatter.getButtonSize(cancelButton)); |
| 81 | |
|
| 82 | 695 | okButton.addActionListener(this); |
| 83 | 695 | cancelButton.addActionListener(this); |
| 84 | |
|
| 85 | 695 | for (BuddiPreferencePlugin panel : preferencePanels) { |
| 86 | |
try { |
| 87 | 3475 | panel.load(); |
| 88 | |
} |
| 89 | 0 | catch (PluginException pe){ |
| 90 | 0 | Logger.getLogger(this.getClass().getName()).log(Level.WARNING, "Plugin Exception", pe); |
| 91 | 6950 | } |
| 92 | |
} |
| 93 | |
|
| 94 | 695 | this.setJMenuBar(new BuddiMenuBar(this)); |
| 95 | 695 | this.setTitle(TextFormatter.getTranslation(BuddiKeys.PREFERENCES)); |
| 96 | 695 | this.getRootPane().setDefaultButton(okButton); |
| 97 | 695 | this.setLayout(new BorderLayout()); |
| 98 | 695 | this.add(tabs, BorderLayout.CENTER); |
| 99 | 695 | this.add(buttonPanel, BorderLayout.SOUTH); |
| 100 | 695 | } |
| 101 | |
|
| 102 | |
|
| 103 | |
@Override |
| 104 | |
public void closeWindowWithoutPrompting() { |
| 105 | 90 | PrefsModel.getInstance().putWindowLocation(BuddiKeys.PREFERENCES.toString(), this.getLocation()); |
| 106 | 90 | PrefsModel.getInstance().save(); |
| 107 | |
|
| 108 | 90 | MainFrame.updateAllContent(); |
| 109 | |
|
| 110 | 90 | super.closeWindowWithoutPrompting(); |
| 111 | 90 | } |
| 112 | |
|
| 113 | |
public void actionPerformed(ActionEvent e) { |
| 114 | 86 | if (e.getSource().equals(okButton)){ |
| 115 | 43 | boolean restart = false; |
| 116 | 43 | for (BuddiPreferencePlugin panel : preferencePanels) { |
| 117 | |
try { |
| 118 | 215 | if (panel.save()) |
| 119 | 1 | restart = true; |
| 120 | |
} |
| 121 | 0 | catch (PluginException pe){ |
| 122 | 0 | Logger.getLogger(this.getClass().getName()).log(Level.WARNING, "Plugin Exception", pe); |
| 123 | 430 | } |
| 124 | |
} |
| 125 | |
|
| 126 | 43 | PrefsModel.getInstance().save(); |
| 127 | |
|
| 128 | 43 | if (restart){ |
| 129 | 1 | String[] options = new String[1]; |
| 130 | 1 | options[0] = TextFormatter.getTranslation(ButtonKeys.BUTTON_OK); |
| 131 | |
|
| 132 | 1 | JOptionPane.showOptionDialog( |
| 133 | |
null, |
| 134 | |
TextFormatter.getTranslation(BuddiKeys.MESSAGE_PREFERENCES_CHANGED_RESTART_NEEDED), |
| 135 | |
TextFormatter.getTranslation(BuddiKeys.MESSAGE_PREFERENCES_CHANGED_RESTART_NEEDED_TITLE), |
| 136 | |
JOptionPane.OK_CANCEL_OPTION, |
| 137 | |
JOptionPane.INFORMATION_MESSAGE, |
| 138 | |
null, |
| 139 | |
options, |
| 140 | |
options[0] |
| 141 | |
); |
| 142 | |
} |
| 143 | |
|
| 144 | 42 | for (MossFrame frame : ApplicationModel.getInstance().getOpenFrames()) { |
| 145 | 85 | if (frame instanceof MainFrame){ |
| 146 | 42 | MainFrame main = (MainFrame) frame; |
| 147 | 42 | main.fireStructureChanged(); |
| 148 | 42 | main.updateContent(); |
| 149 | 42 | main.updateMenus(); |
| 150 | 85 | } |
| 151 | |
} |
| 152 | |
|
| 153 | 42 | this.closeWindow(); |
| 154 | 42 | } |
| 155 | 43 | else if (e.getSource().equals(cancelButton)){ |
| 156 | 43 | this.closeWindow(); |
| 157 | |
} |
| 158 | 85 | } |
| 159 | |
|
| 160 | |
private JPanel getWrapperPanel(JPanel panel){ |
| 161 | 2780 | JPanel wrapper = new JPanel(new BorderLayout()); |
| 162 | 2780 | wrapper.add(panel, BorderLayout.NORTH); |
| 163 | 2780 | return wrapper; |
| 164 | |
} |
| 165 | |
} |