/
evernotenkai
/
DataBaseXJavaProject
Обзор
Документация
Войти
/
evernotenkai
/
DataBaseXJavaProject
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
src/app/ui/screens/AppShell.java
187 строк
7 KB
Makson4ik
Весь проект с src db и прочим делом
12 май 2026, 22:43
12 май 2026, 22:43
431f4aa
Код
Авторство
О чём код?
package app.ui.screens; import java.lang.reflect.Method; import java.util.LinkedHashMap; import java.util.Map; import app.model.UserSession; import app.ui.components.UiKit; import javafx.geometry.Insets; import javafx.geometry.Pos; import javafx.scene.Node; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.layout.BorderPane; import javafx.scene.layout.Region; import javafx.scene.control.ScrollPane; import javafx.scene.layout.StackPane; import javafx.scene.layout.VBox; public class AppShell extends BorderPane { private final BorderPane sidebarPane = new BorderPane(); private final VBox topBox = new VBox(12); private final VBox navBox = new VBox(8); private final StackPane contentPane = new StackPane(); private final Map<String, Button> buttons = new LinkedHashMap<String, Button>(); private final UserSession session; private final Runnable logoutHandler; private String firstKey; private String activeKey; public AppShell(String subtitle, UserSession session, Runnable logoutHandler) { this.session = session; this.logoutHandler = logoutHandler; getStyleClass().add("app-shell"); buildSidebar(subtitle); contentPane.setMinSize(0, 0); ScrollPane scroll = new ScrollPane(contentPane); scroll.setFitToWidth(true); scroll.setFitToHeight(false); scroll.getStyleClass().add("content-scroll"); setLeft(sidebarPane); setCenter(scroll); } private void buildSidebar(String subtitle) { sidebarPane.getStyleClass().add("sidebar"); sidebarPane.setPadding(new Insets(18, 16, 18, 16)); sidebarPane.setMinWidth(252); sidebarPane.setPrefWidth(284); topBox.getChildren().clear(); navBox.getChildren().clear(); topBox.getStyleClass().add("sidebar-top"); navBox.getStyleClass().add("nav-box"); navBox.setAlignment(Pos.TOP_LEFT); VBox brand = new VBox(4); brand.getChildren().add(UiKit.title("Mini LMS")); brand.getChildren().add(UiKit.subtitle(subtitle)); topBox.getChildren().add(brand); String displayName = resolveDisplayName(); Label avatar = new Label(initials(displayName)); avatar.getStyleClass().add("profile-avatar"); VBox profileInfo = new VBox(4); profileInfo.getChildren().add(UiKit.labeled(displayName, "profile-name")); profileInfo.getChildren().add(UiKit.labeled("Логин: " + session.getLogin(), "profile-login")); profileInfo.getChildren().add(UiKit.labeled("Роль доступа: " + session.getRoleLabel(), "profile-login")); VBox profileContent = new VBox(10); profileContent.getChildren().add(new javafx.scene.layout.HBox(12, avatar, profileInfo)); VBox userCard = UiKit.card(profileContent); userCard.getStyleClass().add("profile-card"); topBox.getChildren().add(userCard); topBox.getChildren().add(UiKit.labeled("Разделы", "sidebar-label")); topBox.getChildren().add(navBox); VBox.setVgrow(navBox, javafx.scene.layout.Priority.ALWAYS); sidebarPane.setTop(topBox); Button logout = UiKit.secondaryButton("Выйти из аккаунта"); logout.setMaxWidth(Double.MAX_VALUE); logout.setOnAction(e -> { if (logoutHandler != null) logoutHandler.run(); }); VBox bottom = new VBox(logout); bottom.setSpacing(10); bottom.getStyleClass().add("sidebar-bottom"); sidebarPane.setBottom(bottom); BorderPane.setMargin(bottom, new Insets(14, 0, 0, 0)); } private String resolveDisplayName() { String fullName = session.getFullName(); String role = session.getRole(); if (fullName == null || fullName.trim().isEmpty()) { if ("admin".equalsIgnoreCase(role)) return "Администратор системы"; if ("teacher".equalsIgnoreCase(role)) return "Преподаватель"; if ("student".equalsIgnoreCase(role)) return "Студент"; return session.getLogin() == null ? "Пользователь" : session.getLogin(); } String normalized = fullName.trim().toLowerCase(); if ("admin".equalsIgnoreCase(role) && (normalized.equals("администратор") || normalized.equals("administrator") || normalized.equals("admin"))) { return "Администратор системы"; } if ("teacher".equalsIgnoreCase(role) && (normalized.equals("преподаватель") || normalized.equals("teacher"))) { return "Преподаватель"; } if ("student".equalsIgnoreCase(role) && (normalized.equals("студент") || normalized.equals("student"))) { return "Студент"; } return fullName; } private String roleChipClass(String role) { if (role == null) return "chip-gray"; if ("admin".equalsIgnoreCase(role)) return "chip-violet"; if ("teacher".equalsIgnoreCase(role)) return "chip-blue"; if ("student".equalsIgnoreCase(role)) return "chip-green"; return "chip-gray"; } private String initials(String value) { if (value == null || value.trim().isEmpty()) { return "?"; } String[] parts = value.trim().split("\\s+"); if (parts.length == 1) { return parts[0].substring(0, Math.min(2, parts[0].length())).toUpperCase(); } String a = parts[0].substring(0, 1); String b = parts[1].substring(0, 1); return (a + b).toUpperCase(); } public void addNav(final String key, String text, final Node view) { if (firstKey == null) firstKey = key; final Button button = UiKit.navButton(text); buttons.put(key, button); button.setOnAction(e -> show(key, view)); navBox.getChildren().add(button); if (contentPane.getChildren().isEmpty()) { show(key, view); } } public void finishSidebar() { if (firstKey != null && activeKey == null && buttons.containsKey(firstKey)) { Button b = buttons.get(firstKey); b.fire(); } } private void show(String key, Node view) { activeKey = key; for (Button button : buttons.values()) { button.getStyleClass().remove("nav-button-active"); } Button active = buttons.get(key); if (active != null && !active.getStyleClass().contains("nav-button-active")) { active.getStyleClass().add("nav-button-active"); } refreshIfSupported(view); contentPane.getChildren().setAll(view); if (view instanceof Region) { ((Region) view).setMaxWidth(Double.MAX_VALUE); ((Region) view).setMinWidth(0); } StackPane.setMargin(view, new Insets(0)); } private void refreshIfSupported(Node view) { try { Method refresh = view.getClass().getMethod("refresh"); refresh.setAccessible(true); refresh.invoke(view); } catch (NoSuchMethodException e) { // view doesn't expose refresh() } catch (Exception e) { throw new RuntimeException("Не удалось обновить экран " + view.getClass().getSimpleName(), e); } } }