| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
package org.homeunix.thecave.buddi.plugin.builtin.preference; |
| 5 | |
|
| 6 | |
import java.awt.Component; |
| 7 | |
import java.awt.FlowLayout; |
| 8 | |
import java.util.List; |
| 9 | |
|
| 10 | |
import javax.swing.BoxLayout; |
| 11 | |
import javax.swing.DefaultListCellRenderer; |
| 12 | |
import javax.swing.JCheckBox; |
| 13 | |
import javax.swing.JComboBox; |
| 14 | |
import javax.swing.JLabel; |
| 15 | |
import javax.swing.JList; |
| 16 | |
import javax.swing.JPanel; |
| 17 | |
|
| 18 | |
import org.homeunix.thecave.buddi.i18n.BuddiKeys; |
| 19 | |
import org.homeunix.thecave.buddi.model.prefs.PrefsModel; |
| 20 | |
import org.homeunix.thecave.buddi.plugin.BuddiPluginFactory; |
| 21 | |
import org.homeunix.thecave.buddi.plugin.api.BuddiPreferencePlugin; |
| 22 | |
import org.homeunix.thecave.buddi.plugin.api.BuddiTransactionCellRendererPlugin; |
| 23 | |
import org.homeunix.thecave.buddi.plugin.api.util.TextFormatter; |
| 24 | |
import org.homeunix.thecave.buddi.plugin.builtin.cellrenderer.DefaultTransactionCellRenderer; |
| 25 | |
|
| 26 | |
import ca.digitalcave.moss.swing.model.BackedComboBoxModel; |
| 27 | |
|
| 28 | |
public class AdvancedPreferences extends BuddiPreferencePlugin { |
| 29 | |
public static final long serialVersionUID = 0; |
| 30 | |
|
| 31 | |
private final JComboBox numberOfBackups; |
| 32 | |
private final JComboBox transactionCellRenderer; |
| 33 | |
private final JComboBox autosavePeriod; |
| 34 | |
private final JCheckBox showPromptForDataFile; |
| 35 | |
|
| 36 | |
private final JCheckBox showUpdateNotifications; |
| 37 | |
private final JCheckBox hideNegativeSign; |
| 38 | |
|
| 39 | |
|
| 40 | |
@SuppressWarnings("unchecked") |
| 41 | 695 | public AdvancedPreferences() { |
| 42 | 695 | transactionCellRenderer = new JComboBox(new BackedComboBoxModel<BuddiTransactionCellRendererPlugin>((List<BuddiTransactionCellRendererPlugin>) BuddiPluginFactory.getPlugins(BuddiTransactionCellRendererPlugin.class))); |
| 43 | 695 | numberOfBackups = new JComboBox(new Integer[]{3, 5, 10, 25, 50}); |
| 44 | 695 | autosavePeriod = new JComboBox(new Integer[]{15, 30, 60, 120, 300}); |
| 45 | 695 | showPromptForDataFile = new JCheckBox(TextFormatter.getTranslation(BuddiKeys.PREFERENCE_PROMPT_FOR_DATA_FILE_AT_STARTUP)); |
| 46 | |
|
| 47 | 695 | showUpdateNotifications = new JCheckBox(TextFormatter.getTranslation(BuddiKeys.PREFERENCE_ENABLE_UPDATE_NOTIFICATIONS)); |
| 48 | 695 | hideNegativeSign = new JCheckBox(TextFormatter.getTranslation(BuddiKeys.PREFERENCE_HIDE_NEGATIVE_SIGNS)); |
| 49 | 695 | } |
| 50 | |
|
| 51 | |
@Override |
| 52 | |
public JPanel getPreferencesPanel() { |
| 53 | |
|
| 54 | 695 | autosavePeriod.setRenderer(new DefaultListCellRenderer(){ |
| 55 | |
public static final long serialVersionUID = 0; |
| 56 | |
|
| 57 | |
@Override |
| 58 | |
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { |
| 59 | 6995 | super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); |
| 60 | 6995 | if (((Integer) value) < 60) |
| 61 | 2798 | this.setText(value + " " + TextFormatter.getTranslation(BuddiKeys.SECONDS)); |
| 62 | 4197 | else if (((Integer) value) == 60) |
| 63 | 1399 | this.setText(((Integer) value) / 60 + " " + TextFormatter.getTranslation(BuddiKeys.MINUTE)); |
| 64 | |
else |
| 65 | 2798 | this.setText(((Integer) value) / 60 + " " + TextFormatter.getTranslation(BuddiKeys.MINUTES)); |
| 66 | 6995 | return this; |
| 67 | |
} |
| 68 | |
}); |
| 69 | |
|
| 70 | 695 | transactionCellRenderer.setRenderer(new DefaultListCellRenderer(){ |
| 71 | |
public static final long serialVersionUID = 0; |
| 72 | |
|
| 73 | |
@Override |
| 74 | |
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { |
| 75 | 5596 | super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); |
| 76 | |
|
| 77 | 5596 | if (value instanceof BuddiTransactionCellRendererPlugin){ |
| 78 | 5596 | this.setText(((BuddiTransactionCellRendererPlugin) value).getName()); |
| 79 | |
} |
| 80 | |
else { |
| 81 | 0 | this.setText(" "); |
| 82 | |
} |
| 83 | |
|
| 84 | 5596 | return this; |
| 85 | |
} |
| 86 | |
}); |
| 87 | |
|
| 88 | 695 | JPanel panel = new JPanel(); |
| 89 | 695 | panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); |
| 90 | |
|
| 91 | 695 | JPanel autosavePeriodPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT)); |
| 92 | 695 | JPanel numberOfBackupsPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT)); |
| 93 | 695 | JPanel updatePanel = new JPanel(new FlowLayout(FlowLayout.RIGHT)); |
| 94 | 695 | JPanel editLanguagesPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT)); |
| 95 | 695 | JPanel editTypesPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT)); |
| 96 | 695 | JPanel promptForDataFilePanel = new JPanel(new FlowLayout(FlowLayout.RIGHT)); |
| 97 | 695 | JPanel checkForUpdatesPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT)); |
| 98 | |
|
| 99 | 695 | JPanel hideNegativePanel = new JPanel(new FlowLayout(FlowLayout.RIGHT)); |
| 100 | 695 | JPanel transactionCellRendererPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT)); |
| 101 | |
|
| 102 | 695 | JLabel autosavePeriodLabel = new JLabel(TextFormatter.getTranslation(BuddiKeys.AUTOSAVE_PERIOD)); |
| 103 | 695 | JLabel numberOfBackupsLabel = new JLabel(TextFormatter.getTranslation(BuddiKeys.NUMBER_OF_BACKUPS)); |
| 104 | 695 | JLabel transactionCellRendererLabel = new JLabel(TextFormatter.getTranslation(BuddiKeys.TRANSACTION_CELL_RENDERER)); |
| 105 | |
|
| 106 | 695 | transactionCellRendererPanel.add(transactionCellRendererLabel); |
| 107 | 695 | transactionCellRendererPanel.add(transactionCellRenderer); |
| 108 | |
|
| 109 | 695 | autosavePeriodPanel.add(autosavePeriodLabel); |
| 110 | 695 | autosavePeriodPanel.add(autosavePeriod); |
| 111 | |
|
| 112 | 695 | numberOfBackupsPanel.add(numberOfBackupsLabel); |
| 113 | 695 | numberOfBackupsPanel.add(numberOfBackups); |
| 114 | |
|
| 115 | 695 | transactionCellRendererPanel.add(transactionCellRendererLabel); |
| 116 | 695 | transactionCellRendererPanel.add(transactionCellRenderer); |
| 117 | |
|
| 118 | 695 | promptForDataFilePanel.add(showPromptForDataFile); |
| 119 | 695 | checkForUpdatesPanel.add(showUpdateNotifications); |
| 120 | |
|
| 121 | |
|
| 122 | 695 | hideNegativePanel.add(hideNegativeSign); |
| 123 | |
|
| 124 | 695 | panel.add(autosavePeriodPanel); |
| 125 | 695 | panel.add(numberOfBackupsPanel); |
| 126 | 695 | panel.add(transactionCellRendererPanel); |
| 127 | 695 | panel.add(editLanguagesPanel); |
| 128 | 695 | panel.add(editTypesPanel); |
| 129 | 695 | panel.add(updatePanel); |
| 130 | 695 | panel.add(promptForDataFilePanel); |
| 131 | 695 | panel.add(hideNegativePanel); |
| 132 | 695 | panel.add(checkForUpdatesPanel); |
| 133 | |
|
| 134 | |
|
| 135 | 695 | return panel; |
| 136 | |
} |
| 137 | |
|
| 138 | |
@SuppressWarnings("unchecked") |
| 139 | |
public void load() { |
| 140 | 695 | BuddiTransactionCellRendererPlugin renderer = new DefaultTransactionCellRenderer(); |
| 141 | 695 | for (BuddiTransactionCellRendererPlugin r : (List<BuddiTransactionCellRendererPlugin>) BuddiPluginFactory.getPlugins(BuddiTransactionCellRendererPlugin.class)) { |
| 142 | 695 | if (r.getClass().getCanonicalName().equals(PrefsModel.getInstance().getTransactionCellRenderer())){ |
| 143 | 695 | renderer = r; |
| 144 | 695 | break; |
| 145 | |
} |
| 146 | |
} |
| 147 | |
|
| 148 | 695 | transactionCellRenderer.setSelectedItem(renderer); |
| 149 | 695 | autosavePeriod.setSelectedItem(PrefsModel.getInstance().getAutosaveDelay()); |
| 150 | 695 | numberOfBackups.setSelectedItem(PrefsModel.getInstance().getNumberOfBackups()); |
| 151 | 695 | showPromptForDataFile.setSelected(PrefsModel.getInstance().isShowPromptAtStartup()); |
| 152 | 695 | showUpdateNotifications.setSelected(PrefsModel.getInstance().isShowUpdateNotifications()); |
| 153 | |
|
| 154 | 695 | hideNegativeSign.setSelected(PrefsModel.getInstance().isDontShowNegativeSign()); |
| 155 | 695 | } |
| 156 | |
|
| 157 | |
public boolean save() { |
| 158 | 43 | PrefsModel.getInstance().setTransactionCellRenderer(transactionCellRenderer.getSelectedItem().getClass().getCanonicalName()); |
| 159 | 43 | PrefsModel.getInstance().setAutosaveDelay((Integer) autosavePeriod.getSelectedItem()); |
| 160 | 43 | PrefsModel.getInstance().setNumberOfBackups((Integer) numberOfBackups.getSelectedItem()); |
| 161 | 43 | PrefsModel.getInstance().setShowPromptAtStartup(showPromptForDataFile.isSelected()); |
| 162 | 43 | PrefsModel.getInstance().setShowUpdateNotifications(showUpdateNotifications.isSelected()); |
| 163 | |
|
| 164 | 43 | PrefsModel.getInstance().setShowNegativeSign(hideNegativeSign.isSelected()); |
| 165 | |
|
| 166 | 43 | return false; |
| 167 | |
} |
| 168 | |
|
| 169 | |
public String getName() { |
| 170 | 695 | return BuddiKeys.ADVANCED.toString(); |
| 171 | |
} |
| 172 | |
} |