Skip to content

Commit fdffa03

Browse files
committed
Fix alignment issues.
1 parent e9af2ed commit fdffa03

File tree

6 files changed

+38
-50
lines changed

6 files changed

+38
-50
lines changed

sierra.dtd

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
<!ENTITY % java.awt.Component "%org.httprpc.sierra.UILoader; background CDATA enabled (true|false) focusTraversalKeysEnabled (true|false) focusable (true|false) font CDATA foreground CDATA ignoreRepaint (true|false) name CDATA visible (true|false) ">
33
<!ENTITY % java.awt.Container "%java.awt.Component; focusCycleRoot (true|false) focusTraversalPolicyProvider (true|false) font CDATA ">
44
<!ENTITY % javax.swing.JComponent "%java.awt.Container; alignmentX CDATA alignmentY CDATA autoscrolls (true|false) background CDATA debugGraphicsOptions CDATA doubleBuffered (true|false) enabled (true|false) font CDATA foreground CDATA inheritsPopupMenu (true|false) opaque (true|false) requestFocusEnabled (true|false) toolTipText CDATA verifyInputWhenFocusTarget (true|false) visible (true|false) ">
5-
<!ENTITY % javax.swing.AbstractButton "%javax.swing.JComponent; actionCommand CDATA borderPainted (true|false) contentAreaFilled (true|false) disabledIcon CDATA disabledSelectedIcon CDATA displayedMnemonicIndex CDATA enabled (true|false) focusPainted (true|false) hideActionText (true|false) horizontalAlignment (left|right|leading|trailing|center) horizontalTextPosition CDATA icon CDATA iconTextGap CDATA label CDATA mnemonic CDATA multiClickThreshhold CDATA pressedIcon CDATA rolloverEnabled (true|false) rolloverIcon CDATA rolloverSelectedIcon CDATA selected (true|false) selectedIcon CDATA text CDATA verticalAlignment (top|bottom|center) verticalTextPosition CDATA ">
5+
<!ENTITY % javax.swing.AbstractButton "%javax.swing.JComponent; actionCommand CDATA borderPainted (true|false) contentAreaFilled (true|false) disabledIcon CDATA disabledSelectedIcon CDATA displayedMnemonicIndex CDATA enabled (true|false) focusPainted (true|false) hideActionText (true|false) horizontalAlignment (left|right|center|leading|trailing) horizontalTextPosition CDATA icon CDATA iconTextGap CDATA label CDATA mnemonic CDATA multiClickThreshhold CDATA pressedIcon CDATA rolloverEnabled (true|false) rolloverIcon CDATA rolloverSelectedIcon CDATA selected (true|false) selectedIcon CDATA text CDATA verticalAlignment (top|bottom|center) verticalTextPosition CDATA ">
66
<!ENTITY % javax.swing.JColorChooser "%javax.swing.JComponent; color CDATA dragEnabled (true|false) ">
77
<!ELEMENT color-chooser EMPTY>
88
<!ATTLIST color-chooser %javax.swing.JColorChooser;>
99
<!ENTITY % javax.swing.JComboBox "%javax.swing.JComponent; actionCommand CDATA editable (true|false) enabled (true|false) lightWeightPopupEnabled (true|false) maximumRowCount CDATA popupVisible (true|false) selectedIndex CDATA ">
1010
<!ELEMENT combo-box EMPTY>
1111
<!ATTLIST combo-box %javax.swing.JComboBox;>
12-
<!ENTITY % javax.swing.JLabel "%javax.swing.JComponent; disabledIcon CDATA displayedMnemonic CDATA displayedMnemonicIndex CDATA horizontalAlignment (left|right|leading|trailing|center) horizontalTextPosition CDATA icon CDATA iconTextGap CDATA text CDATA verticalAlignment (top|bottom|center) verticalTextPosition CDATA ">
12+
<!ENTITY % javax.swing.JLabel "%javax.swing.JComponent; disabledIcon CDATA displayedMnemonic CDATA displayedMnemonicIndex CDATA horizontalAlignment (left|right|center|leading|trailing) horizontalTextPosition CDATA icon CDATA iconTextGap CDATA text CDATA verticalAlignment (top|bottom|center) verticalTextPosition CDATA ">
1313
<!ELEMENT label EMPTY>
1414
<!ATTLIST label %javax.swing.JLabel;>
1515
<!ENTITY % javax.swing.JList "%javax.swing.JComponent; dragEnabled (true|false) dropMode (use-selection|on|insert|insert-rows|insert-cols|on-or-insert|on-or-insert-rows|on-or-insert-cols) fixedCellHeight CDATA fixedCellWidth CDATA layoutOrientation CDATA selectedIndex CDATA selectionBackground CDATA selectionForeground CDATA selectionMode CDATA valueIsAdjusting (true|false) visibleRowCount CDATA ">
@@ -53,13 +53,13 @@
5353
<!ENTITY % org.httprpc.sierra.ActivityIndicator "%javax.swing.JComponent; indicatorSize CDATA ">
5454
<!ELEMENT activity-indicator EMPTY>
5555
<!ATTLIST activity-indicator %org.httprpc.sierra.ActivityIndicator;>
56-
<!ENTITY % org.httprpc.sierra.ImagePane "%javax.swing.JComponent; horizontalAlignment (left|right|leading|trailing|center) image CDATA scaleMode (none|fill-width|fill-height) verticalAlignment (top|bottom|center) ">
56+
<!ENTITY % org.httprpc.sierra.ImagePane "%javax.swing.JComponent; horizontalAlignment (left|right|center|leading|trailing) image CDATA scaleMode (none|fill-width|fill-height) verticalAlignment (top|bottom|center) ">
5757
<!ELEMENT image-pane EMPTY>
5858
<!ATTLIST image-pane %org.httprpc.sierra.ImagePane;>
5959
<!ENTITY % org.httprpc.sierra.Spacer "%javax.swing.JComponent; ">
6060
<!ELEMENT spacer EMPTY>
6161
<!ATTLIST spacer %org.httprpc.sierra.Spacer;>
62-
<!ENTITY % org.httprpc.sierra.TextPane "%javax.swing.JComponent; horizontalAlignment (left|right|leading|trailing|center) text CDATA verticalAlignment (top|bottom|center) wrapText (true|false) ">
62+
<!ENTITY % org.httprpc.sierra.TextPane "%javax.swing.JComponent; horizontalAlignment (left|right|center|leading|trailing) text CDATA verticalAlignment (top|bottom|center) wrapText (true|false) ">
6363
<!ELEMENT text-pane EMPTY>
6464
<!ATTLIST text-pane %org.httprpc.sierra.TextPane;>
6565
<!ENTITY % javax.swing.JButton "%javax.swing.AbstractButton; defaultCapable (true|false) ">
@@ -74,7 +74,7 @@
7474
<!ENTITY % javax.swing.JTextArea "%javax.swing.text.JTextComponent; columns CDATA font CDATA lineWrap (true|false) rows CDATA tabSize CDATA wrapStyleWord (true|false) ">
7575
<!ELEMENT text-area EMPTY>
7676
<!ATTLIST text-area %javax.swing.JTextArea;>
77-
<!ENTITY % javax.swing.JTextField "%javax.swing.text.JTextComponent; columns CDATA font CDATA horizontalAlignment (left|right|leading|trailing|center) scrollOffset CDATA placeholderText CDATA showClearButton (true|false) leadingIcon CDATA trailingIcon CDATA ">
77+
<!ENTITY % javax.swing.JTextField "%javax.swing.text.JTextComponent; columns CDATA font CDATA horizontalAlignment (left|right|center|leading|trailing) scrollOffset CDATA placeholderText CDATA showClearButton (true|false) leadingIcon CDATA trailingIcon CDATA ">
7878
<!ELEMENT text-field EMPTY>
7979
<!ATTLIST text-field %javax.swing.JTextField;>
8080
<!ENTITY % javax.swing.JToggleButton "%javax.swing.AbstractButton; ">
@@ -106,13 +106,13 @@
106106
<!ELEMENT radio-button-menu-item EMPTY>
107107
<!ATTLIST radio-button-menu-item %javax.swing.JRadioButtonMenuItem;>
108108
<!ENTITY % org.httprpc.sierra.BoxPanel "%org.httprpc.sierra.LayoutPanel; spacing CDATA ">
109-
<!ENTITY % org.httprpc.sierra.MenuButton "%javax.swing.JButton; popupHorizontalAlignment (left|right|leading|trailing|center) popupVerticalAlignment (top|bottom|center) ">
109+
<!ENTITY % org.httprpc.sierra.MenuButton "%javax.swing.JButton; popupHorizontalAlignment (left|right|center|leading|trailing) popupVerticalAlignment (top|bottom|center) ">
110110
<!ELEMENT menu-button (ANY)>
111111
<!ATTLIST menu-button %org.httprpc.sierra.MenuButton;>
112112
<!ENTITY % org.httprpc.sierra.NumberField "%javax.swing.JTextField; value CDATA ">
113113
<!ELEMENT number-field EMPTY>
114114
<!ATTLIST number-field %org.httprpc.sierra.NumberField;>
115-
<!ENTITY % org.httprpc.sierra.Picker "%javax.swing.JTextField; popupHorizontalAlignment (left|right|leading|trailing|center) popupVerticalAlignment (top|bottom|center) ">
115+
<!ENTITY % org.httprpc.sierra.Picker "%javax.swing.JTextField; popupHorizontalAlignment (left|right|center|leading|trailing) popupVerticalAlignment (top|bottom|center) ">
116116
<!ENTITY % org.httprpc.sierra.StackPanel "%org.httprpc.sierra.LayoutPanel; ">
117117
<!ELEMENT stack-panel (ANY)>
118118
<!ATTLIST stack-panel %org.httprpc.sierra.StackPanel;>

sierra/src/main/java/org/httprpc/sierra/HorizontalAlignment.java

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
package org.httprpc.sierra;
1616

17+
import javax.swing.JComponent;
1718
import javax.swing.SwingConstants;
1819

1920
/**
@@ -31,19 +32,19 @@ public enum HorizontalAlignment implements ConstantAdapter {
3132
RIGHT("right", SwingConstants.RIGHT),
3233

3334
/**
34-
* Leading alignment.
35+
* Center alignment.
3536
*/
36-
LEADING("leading", SwingConstants.LEADING),
37+
CENTER("center", SwingConstants.CENTER),
3738

3839
/**
39-
* Trailing alignment.
40+
* Leading alignment.
4041
*/
41-
TRAILING("trailing", SwingConstants.TRAILING),
42+
LEADING("leading", SwingConstants.LEADING),
4243

4344
/**
44-
* Center alignment.
45+
* Trailing alignment.
4546
*/
46-
CENTER("center", SwingConstants.CENTER);
47+
TRAILING("trailing", SwingConstants.TRAILING);
4748

4849
private final String key;
4950
private final int value;
@@ -62,4 +63,12 @@ public String getKey() {
6263
public int getValue() {
6364
return value;
6465
}
66+
67+
HorizontalAlignment getLocalizedValue(JComponent component) {
68+
return switch (this) {
69+
case LEFT, RIGHT, CENTER -> this;
70+
case LEADING -> component.getComponentOrientation().isLeftToRight() ? LEFT : RIGHT;
71+
case TRAILING -> component.getComponentOrientation().isLeftToRight() ? RIGHT : LEFT;
72+
};
73+
}
6574
}

sierra/src/main/java/org/httprpc/sierra/ImagePane.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -112,16 +112,11 @@ private void paint(Graphics2D graphics) {
112112
var scaledImageWidth = scale * imageWidth;
113113
var scaledImageHeight = scale * imageHeight;
114114

115-
var x = switch (horizontalAlignment) {
116-
case LEFT, RIGHT, LEADING, TRAILING -> {
117-
if (horizontalAlignment == HorizontalAlignment.LEFT
118-
|| getComponentOrientation().isLeftToRight() ^ horizontalAlignment == HorizontalAlignment.TRAILING) {
119-
yield 0;
120-
} else {
121-
yield width - scaledImageWidth;
122-
}
123-
}
115+
var x = switch (horizontalAlignment.getLocalizedValue(ImagePane.this)) {
116+
case LEFT -> 0;
117+
case RIGHT -> width - scaledImageWidth;
124118
case CENTER -> (width - scaledImageWidth) / 2;
119+
default -> throw new UnsupportedOperationException();
125120
};
126121

127122
var y = switch (verticalAlignment) {

sierra/src/main/java/org/httprpc/sierra/MenuButton.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -86,16 +86,11 @@ public void setPressed(boolean pressed) {
8686
var size = getSize();
8787
var popupMenuSize = popupMenu.getPreferredSize();
8888

89-
var x = switch (popupHorizontalAlignment) {
90-
case LEFT, RIGHT, LEADING, TRAILING -> {
91-
if (popupHorizontalAlignment == HorizontalAlignment.LEFT
92-
|| getComponentOrientation().isLeftToRight() ^ popupHorizontalAlignment == HorizontalAlignment.TRAILING) {
93-
yield 0;
94-
} else {
95-
yield size.width - popupMenuSize.width;
96-
}
97-
}
89+
var x = switch (popupHorizontalAlignment.getLocalizedValue(MenuButton.this)) {
90+
case LEFT -> 0;
91+
case RIGHT -> size.width - popupMenuSize.width;
9892
case CENTER -> (size.width - popupMenuSize.width) / 2;
93+
default -> throw new UnsupportedOperationException();
9994
};
10095

10196
var y = switch (popupVerticalAlignment) {

sierra/src/main/java/org/httprpc/sierra/Picker.java

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -204,17 +204,11 @@ protected void showPopup() {
204204
var size = getSize();
205205
var popupSize = popupComponent.getPreferredSize();
206206

207-
var x = switch (popupHorizontalAlignment) {
208-
case LEFT, RIGHT, LEADING, TRAILING -> {
209-
if (popupHorizontalAlignment == HorizontalAlignment.LEFT
210-
|| getComponentOrientation().isLeftToRight() ^ popupHorizontalAlignment == HorizontalAlignment.TRAILING) {
211-
yield 0;
212-
} else {
213-
yield size.width - popupSize.width;
214-
}
215-
216-
}
207+
var x = switch (popupHorizontalAlignment.getLocalizedValue(Picker.this)) {
208+
case LEFT -> 0;
209+
case RIGHT -> size.width - popupSize.width;
217210
case CENTER -> (size.width - popupSize.width) / 2;
211+
default -> throw new UnsupportedOperationException();
218212
};
219213

220214
var y = switch (popupVerticalAlignment) {

sierra/src/main/java/org/httprpc/sierra/TextPane.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -153,16 +153,11 @@ private void paint(Graphics2D graphics) {
153153

154154
var lineWidth = textBounds.getWidth();
155155

156-
var x = switch (horizontalAlignment) {
157-
case LEFT, RIGHT, LEADING, TRAILING -> {
158-
if (horizontalAlignment == HorizontalAlignment.LEFT
159-
|| getComponentOrientation().isLeftToRight() ^ horizontalAlignment == HorizontalAlignment.TRAILING) {
160-
yield insets.left;
161-
} else {
162-
yield size.width - (lineWidth + insets.right);
163-
}
164-
}
156+
var x = switch (horizontalAlignment.getLocalizedValue(TextPane.this)) {
157+
case LEFT -> insets.left;
158+
case RIGHT -> size.width - (lineWidth + insets.right);
165159
case CENTER -> insets.left + (width - lineWidth) / 2;
160+
default -> throw new UnsupportedOperationException();
166161
};
167162

168163
graphics.drawGlyphVector(glyphVector, (float)x, (float)y + ascent);

0 commit comments

Comments
 (0)