solving of bug number 3043572
[indepmod.git] / Plugins / EPC / NotationEPC / src / cz / cvut / promod / epc / settings / settingPages / GeneralPage.java
blob1d10d8393997cf3696227ee250fd261ef2d64c3a
1 package cz.cvut.promod.epc.settings.settingPages;
3 import com.jgoodies.binding.PresentationModel;
4 import com.jgoodies.binding.adapter.SpinnerAdapterFactory;
5 import com.jgoodies.binding.value.BufferedValueModel;
6 import com.jgoodies.forms.builder.PanelBuilder;
7 import com.jgoodies.forms.factories.Borders;
8 import com.jgoodies.forms.layout.CellConstraints;
9 import com.jgoodies.forms.layout.FormLayout;
10 import cz.cvut.promod.epc.resources.Resources;
11 import cz.cvut.promod.epc.settings.EPCSettings;
12 import cz.cvut.promod.epc.settings.EPCSettingsModel;
13 import cz.cvut.promod.gui.settings.SettingPagePanel;
14 import cz.cvut.promod.services.ModelerSession;
15 import cz.cvut.promod.services.componentFactoryService.ComponentFactoryService;
16 import org.apache.log4j.Logger;
18 import javax.swing.*;
19 import javax.swing.event.ChangeEvent;
20 import javax.swing.event.ChangeListener;
21 import java.awt.*;
22 import java.awt.event.ActionEvent;
24 /**
25 * ProMod, master thesis project
26 * User: Petr Zverina, petr.zverina@gmail.com
27 * Date: 11:28:01, 26.1.2010
29 * General Page for Setting Dialog of the EPCNotation plugin.
31 public class GeneralPage extends SettingPagePanel{
33 private static Logger LOG = Logger.getLogger(GeneralPage.class);
35 private static final int UNDO_SPINNER_COLUMNS = 3;
37 private static final String GENERALS_LABEL = Resources.getResources().getString("epc.settings.generals");
39 private final JLabel undoLimitLabel = ModelerSession.getComponentFactoryService().createLabel(
40 Resources.getResources().getString("epc.settings.undo.limit")
42 protected final JSpinner undoLimitSpinner = ModelerSession.getComponentFactoryService().createSpinner();
44 private final BufferedValueModel undoLimitModel;
46 private final PresentationModel<EPCSettingsModel> presentation;
48 private final ApplyAction applyAction = new ApplyAction();
49 private final CancelAction cancelAction = new CancelAction();
52 public GeneralPage(final PresentationModel<EPCSettingsModel> presentation, final BufferedValueModel undoLimitModel){
53 //super(SETTINGS_LABEL);
55 this.presentation = presentation;
56 this.undoLimitModel = undoLimitModel;
59 public void lazyInitialize() {
60 initValues();
62 initLayout();
64 getSpinnerTextField(undoLimitSpinner).setColumns(UNDO_SPINNER_COLUMNS);
66 initEventHandling();
69 @Override
70 public AbstractAction getApplyAction() {
71 return this.applyAction;
74 @Override
75 public AbstractAction getCancelAction() {
76 return this.cancelAction;
79 @Override
80 public AbstractAction getOkAction() {
81 return this.applyAction;
84 private void initValues() {
85 undoLimitSpinner.setValue(EPCSettings.getInstance().getUndoLimit());
88 private void initEventHandling() {
89 final int initUndoLimitValue = (Integer) undoLimitModel.getValue();
90 final SpinnerNumberModel scaleSpinnerModel = SpinnerAdapterFactory.createNumberAdapter(
91 undoLimitModel,
92 initUndoLimitValue,
93 EPCSettingsModel.MIN_UNDO,
94 EPCSettingsModel.MAX_UNDO,
95 EPCSettingsModel.INIT_UNDO_STEP
98 undoLimitSpinner.setModel(scaleSpinnerModel);
100 undoLimitSpinner.addChangeListener(new ChangeListener(){
101 public void stateChanged(ChangeEvent e) {
102 //fireButtonEvent(ButtonEvent.ENABLE_BUTTON, ButtonNames.APPLY);
103 fireApplyActionEnable();
108 private void initLayout() {
109 final FormLayout layout = new FormLayout(
110 "pref, 3dlu, pref, pref:grow",
111 "pref, 3dlu, pref"
113 setBorder(Borders.createEmptyBorder(ComponentFactoryService.DEFAULT_FORM_BORDER));
115 final PanelBuilder panelBuilder = new PanelBuilder(layout);
116 final CellConstraints cellConstraints = new CellConstraints();
118 int row = 1;
119 panelBuilder.addSeparator(GENERALS_LABEL, cellConstraints.xyw(1, row, 4));
121 row += 2;
122 panelBuilder.add(undoLimitLabel, cellConstraints.xy(1, row));
123 panelBuilder.add(undoLimitSpinner, cellConstraints.xy(3, row));
125 setLayout(new BorderLayout());
126 add(panelBuilder.getPanel(), BorderLayout.CENTER);
129 public JFormattedTextField getSpinnerTextField(final JSpinner spinner) {
130 final JComponent editor = spinner.getEditor();
131 if (editor instanceof JSpinner.DefaultEditor) {
132 return ((JSpinner.DefaultEditor)editor).getTextField();
133 } else {
134 LOG.error("Unexpected editor of JSpinner.");
135 return null;
141 * Commits the Trigger used to buffer the editor contents.
143 private final class ApplyAction extends AbstractAction {
145 public void actionPerformed(ActionEvent e) {
146 presentation.triggerCommit();
153 * Flushed the Trigger used to buffer the editor contents.
155 private final class CancelAction extends AbstractAction {
157 public void actionPerformed(ActionEvent e) {
158 presentation.triggerFlush();