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

Last change on this file since 12044 was 12044, checked in by michael2402, 7 years ago

Make conflict merge dialog layout more universal, make row titles in all panels the same

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