1 | package com.framsticks.gui; |
---|
2 | |
---|
3 | import org.apache.logging.log4j.Logger; |
---|
4 | import org.apache.logging.log4j.LogManager; |
---|
5 | |
---|
6 | |
---|
7 | import javax.swing.*; |
---|
8 | import java.awt.*; |
---|
9 | import java.awt.event.ActionEvent; |
---|
10 | import java.awt.event.ActionListener; |
---|
11 | |
---|
12 | import static com.framsticks.structure.TreeOperations.*; |
---|
13 | |
---|
14 | /** |
---|
15 | * @author Piotr Sniegowski |
---|
16 | */ |
---|
17 | @SuppressWarnings("serial") |
---|
18 | public abstract class ModifiablePanel extends TreePanel { |
---|
19 | |
---|
20 | private static final Logger log = LogManager.getLogger(ModifiablePanel.class.getName()); |
---|
21 | |
---|
22 | /** |
---|
23 | * Pane to which components will be added. |
---|
24 | */ |
---|
25 | |
---|
26 | protected final JLabel label; |
---|
27 | protected final JButton applyButton; |
---|
28 | protected final JButton revertButton; |
---|
29 | protected final JPanel centerPanel; |
---|
30 | |
---|
31 | protected Component contentComponent; |
---|
32 | |
---|
33 | public ModifiablePanel(TreePanel.Parameters parameters) { |
---|
34 | super(parameters); |
---|
35 | log.debug("create panel for type: {}", className); |
---|
36 | |
---|
37 | JPanel pageEndPanel = new JPanel(); |
---|
38 | pageEndPanel.setLayout(new BoxLayout(pageEndPanel, BoxLayout.X_AXIS)); |
---|
39 | pageEndPanel.add(Box.createHorizontalGlue()); |
---|
40 | |
---|
41 | applyButton = new JButton("Apply"); |
---|
42 | applyButton.setName("apply"); |
---|
43 | |
---|
44 | revertButton = new JButton("Revert"); |
---|
45 | revertButton.setName("revert"); |
---|
46 | |
---|
47 | pageEndPanel.add(applyButton); |
---|
48 | pageEndPanel.add(Box.createHorizontalStrut(10)); |
---|
49 | |
---|
50 | pageEndPanel.add(revertButton); |
---|
51 | pageEndPanel.add(Box.createHorizontalStrut(10)); |
---|
52 | |
---|
53 | pageEndPanel.setPreferredSize(new Dimension(0, 30)); |
---|
54 | |
---|
55 | applyButton.addActionListener(new ActionListener() { |
---|
56 | public void actionPerformed(ActionEvent e) { |
---|
57 | applyChanges(); |
---|
58 | } |
---|
59 | }); |
---|
60 | |
---|
61 | revertButton.addActionListener(new ActionListener() { |
---|
62 | public void actionPerformed(ActionEvent e) { |
---|
63 | revertChanges(); |
---|
64 | } |
---|
65 | }); |
---|
66 | |
---|
67 | label = new JLabel(); |
---|
68 | centerPanel = new JPanel(); |
---|
69 | centerPanel.setLayout(new BorderLayout()); |
---|
70 | centerPanel.add(Box.createHorizontalStrut(10), BorderLayout.LINE_START); |
---|
71 | centerPanel.add(Box.createHorizontalStrut(10), BorderLayout.LINE_END); |
---|
72 | centerPanel.add(label, BorderLayout.PAGE_START); |
---|
73 | |
---|
74 | |
---|
75 | centerPanel.add(pageEndPanel, BorderLayout.PAGE_END); |
---|
76 | |
---|
77 | this.setLayout(new BorderLayout()); |
---|
78 | this.add(centerPanel, BorderLayout.CENTER); |
---|
79 | //this.add(new ViewerTest(), BorderLayout.PAGE_END); |
---|
80 | } |
---|
81 | |
---|
82 | protected abstract void applyChanges(); |
---|
83 | protected abstract void revertChanges(); |
---|
84 | |
---|
85 | protected void setupContentComponent(Component contentComponent) { |
---|
86 | this.contentComponent = contentComponent; |
---|
87 | centerPanel.add(contentComponent, BorderLayout.CENTER); |
---|
88 | } |
---|
89 | |
---|
90 | protected void refreshControlButtons() { |
---|
91 | assert frame.isActive(); |
---|
92 | boolean hasChanges = hasSideNotes(getTree(), getCurrentObject(), treeAtFrame.getUserChangesKey()); |
---|
93 | applyButton.setEnabled(hasChanges); |
---|
94 | revertButton.setEnabled(hasChanges); |
---|
95 | } |
---|
96 | |
---|
97 | } |
---|