source: josm/trunk/src/org/openstreetmap/josm/gui/conflict/pair/AbstractMergePanel.java@ 16553

Last change on this file since 16553 was 16553, checked in by Don-vip, 4 years ago

see #19334 - javadoc fixes + protected constructors for abstract classes

File size: 6.0 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.conflict.pair;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.GridBagLayout;
7import java.util.List;
8
9import javax.swing.AbstractAction;
10import javax.swing.JButton;
11import javax.swing.JComponent;
12import javax.swing.JLabel;
13import javax.swing.JPanel;
14
15import org.openstreetmap.josm.tools.GBC;
16
17/**
18 * A panel used as tab in the merge dialog.
19 * Contains helper methods for creating merge dialog columns.
20 *
21 * @author Michael Zangl
22 * @since 12044
23 */
24public abstract class AbstractMergePanel extends JPanel {
25
26 /**
27 * A helper class to add a row to the layout. Each row has 6 columns.
28 * @author Michael Zangl
29 */
30 protected static class MergeRow {
31 protected int marginTop = 5;
32
33 public MergeRow() {
34 // allow access from subclasses
35 }
36
37 protected JComponent rowTitle() {
38 return null;
39 }
40
41 protected JComponent mineField() {
42 return null;
43 }
44
45 protected JComponent mineButton() {
46 return null;
47 }
48
49 protected JComponent merged() {
50 return null;
51 }
52
53 protected JComponent theirsButton() {
54 return null;
55 }
56
57 protected JComponent theirsField() {
58 return null;
59 }
60
61 void addTo(AbstractMergePanel panel) {
62 JComponent[] buttons = getColumns();
63 for (int columnIndex = 0; columnIndex < buttons.length; columnIndex++) {
64 if (buttons[columnIndex] != null) {
65 GBC constraints = GBC.std(columnIndex, panel.currentRow);
66 addConstraints(constraints, columnIndex);
67 panel.add(buttons[columnIndex], constraints);
68 }
69 }
70 panel.currentRow++;
71 }
72
73 protected JComponent[] getColumns() {
74 return new JComponent[] {
75 rowTitle(),
76 mineField(),
77 mineButton(),
78 merged(),
79 theirsButton(),
80 theirsField()
81 };
82 }
83
84 protected void addConstraints(GBC constraints, int columnIndex) {
85 constraints.anchor(GBC.CENTER);
86 constraints.fill = GBC.BOTH;
87 constraints.weight(0, 0);
88 constraints.insets(3, marginTop, 3, 0);
89 if (columnIndex == 1 || columnIndex == 3 || columnIndex == 5) {
90 // resize those rows
91 constraints.weightx = 1;
92 }
93 }
94 }
95
96 /**
97 * A row that does not contain the merge buttons. Fields in this row fill both the button and filed area.
98 */
99 protected static class MergeRowWithoutButton extends MergeRow {
100 @Override
101 protected JComponent[] getColumns() {
102 return new JComponent[] {
103 rowTitle(),
104 mineField(), // width: 2
105 null,
106 merged(),
107 theirsField(), // width: 2
108 null,
109 };
110 }
111
112 @Override
113 protected void addConstraints(GBC constraints, int columnIndex) {
114 super.addConstraints(constraints, columnIndex);
115
116 if (columnIndex == 1 || columnIndex == 4) {
117 constraints.gridwidth = 2;
118 }
119 }
120 }
121
122 /**
123 * The title for the rows (mine, merged, theirs)
124 */
125 protected static class TitleRow extends MergeRow {
126 public TitleRow() {
127 // allow access from subclasses
128 }
129
130 @Override
131 protected JComponent mineField() {
132 JLabel label = new JLabel(tr("My version (local dataset)"));
133 label.setToolTipText(tr("Properties in my dataset, i.e. the local dataset"));
134 label.setHorizontalAlignment(JLabel.CENTER);
135 return label;
136 }
137
138 @Override
139 protected JComponent merged() {
140 JLabel label = new JLabel(tr("Merged version"));
141 label.setToolTipText(
142 tr("Properties in the merged element. They will replace properties in my elements when merge decisions are applied."));
143 label.setHorizontalAlignment(JLabel.CENTER);
144 return label;
145 }
146
147 @Override
148 protected JComponent theirsField() {
149 JLabel label = new JLabel(tr("Their version (server dataset)"));
150 label.setToolTipText(tr("Properties in their dataset, i.e. the server dataset"));
151 label.setHorizontalAlignment(JLabel.CENTER);
152 return label;
153 }
154 }
155
156 /**
157 * Add the undecide button to the middle of the merged row.
158 */
159 protected abstract static class AbstractUndecideRow extends AbstractMergePanel.MergeRow {
160 @Override
161 protected JComponent merged() {
162 AbstractAction actUndecide = createAction();
163 JButton button = new JButton(actUndecide);
164 button.setName(getButtonName());
165 return button;
166 }
167
168 protected abstract AbstractAction createAction();
169
170 protected abstract String getButtonName();
171
172 @Override
173 protected void addConstraints(GBC constraints, int columnIndex) {
174 super.addConstraints(constraints, columnIndex);
175 constraints.fill(GBC.NONE);
176 }
177 }
178
179 /**
180 * The current row counter. Used when adding new rows.
181 */
182 protected int currentRow;
183
184 /**
185 * Create a new merge panel.
186 */
187 protected AbstractMergePanel() {
188 super(new GridBagLayout());
189 }
190
191 /**
192 * Add the rows to this component.
193 * This needs to be called in the constructor of the child class. That way, all it's fields are initialized.
194 */
195 protected void buildRows() {
196 getRows().forEach(row -> row.addTo(this));
197 }
198
199 /**
200 * Gets the rows.
201 * @return A list of rows that should be displayed in this dialog.
202 */
203 protected abstract List<? extends MergeRow> getRows();
204
205}
Note: See TracBrowser for help on using the repository browser.