Please use CAPITAL enum constants in LinearLayout.Alignment
zanonmark opened this issue · 3 comments
Would it be possible to change the enum constants in LinearLayout.Alignment
to, respectively, BEGINNING
, CENTER
, END
, FILL
, i.e. capitalized as in GridLayout.Alignment
?
Thanks,
MZ
Changing names of constants in an enumeration is in principle a hard break for version-compatibility and shouldn't be done without deep consideration.
It is possible to create aliases, by just declaring them as static final fields and initializing them to the respective enum-values.
If the enum values were changed to all-uppercase, and the mixed-case words added as aliases, then the impact might be rather small. Only switch/case labels would need to be updated, and within lanterna, all such usages are in the same file as the enum itself: src/main/java/com/googlecode/lanterna/gui2/LinearLayout.java
I would argue that the different spelling of the enum constants in LinearLayout.Alignment
and GridLayout.Alignment
makes them easier to use. This way they can be imported statically and the code remains readable, as can be seen in the example below. I suspect that this use case accounts for the different spelling.
import com.googlecode.lanterna.gui2.*;
import com.googlecode.lanterna.screen.TerminalScreen;
import com.googlecode.lanterna.terminal.DefaultTerminalFactory;
import static com.googlecode.lanterna.gui2.GridLayout.Alignment.*;
import static com.googlecode.lanterna.gui2.GridLayout.createHorizontallyFilledLayoutData;
import static com.googlecode.lanterna.gui2.GridLayout.createLayoutData;
import static com.googlecode.lanterna.gui2.LinearLayout.Alignment.*;
import static com.googlecode.lanterna.gui2.LinearLayout.GrowPolicy.CanGrow;
import static com.googlecode.lanterna.gui2.LinearLayout.createLayoutData;
public class AlignmentEnumShowcase extends AbstractWindow {
public AlignmentEnumShowcase() {
Panel topPanel = new Panel();
topPanel.setLayoutData(createLayoutData(Fill));
topPanel.setLayoutManager(new GridLayout(2));
topPanel.addComponent(new Label("A"), createLayoutData(END, END));
topPanel.addComponent(new Label("B"), createLayoutData(BEGINNING, BEGINNING));
topPanel.addComponent(new Label("C"), createHorizontallyFilledLayoutData(2));
Panel bottomPanel = new Panel();
bottomPanel.setLayoutData(createLayoutData(Fill));
bottomPanel.setLayoutManager(new LinearLayout());
bottomPanel.addComponent(new Label("1"), createLayoutData(Beginning));
bottomPanel.addComponent(new Label("2"), createLayoutData(End));
bottomPanel.addComponent(new Label("3"), createLayoutData(Center, CanGrow));
Button closeBtn = new Button("close", this::close);
setComponent(Panels.vertical(topPanel, bottomPanel, closeBtn));
}
public static void main(String[] args) {
try (TerminalScreen terminalScreen = new DefaultTerminalFactory().createScreen()) {
terminalScreen.startScreen();
MultiWindowTextGUI windowTextGUI = new MultiWindowTextGUI(terminalScreen);
windowTextGUI.addWindowAndWait(new AlignmentEnumShowcase());
} catch (Exception e) {
e.printStackTrace();
}
}
}
I learned that all upper-case was the java convention rather late, for years I thought camel-case was the standard. I guess we could do it on master branch, there's a couple of other API-breaking changes there.