public void updateSelection() { remove(metadataScrollPane); remove(coverImageScrollPane); remove(styleSheetScrollPane); int index = comboBox.getSelectedIndex(); if (index == 0) { add(metadataScrollPane); } else if (index == 1) { add(coverImageScrollPane); } else if (index == 2) { add(styleSheetScrollPane); } GuiUtils.forceRepaint(this); }
public class WelcomeScreen { private MarkdownMessages messages = MarkdownServer.getMessages(); private JLabel newProjectsLabel = new JLabel(messages.welcomeScreenNewProjectsLabel()); private JComboBox newProjectsComboBox = new JComboBox(MarkdownServer.getProjectTemplates()); private JButton newProjectButton = new JButton(messages.welcomeScreenNewProject()); private JLabel recentProjectsLabel = new JLabel(messages.welcomeScreenRecentProjectsLabel()); private JButton openRecentProjectButton = new JButton(messages.welcomeScreenRecentProjectsButton()); private JComboBox recentProjectsComboBox = new JComboBox(MarkdownServer.getRecentProjects()); private JButton openProjectButton = new JButton(messages.welcomeScreenOpenProject()); private ApplicationWindow window = new ApplicationWindow(messages.welcomeScreenTitle()); private Dimension buttonSize = GuiUtils.sameSize( new JComponent[] {newProjectButton, openProjectButton, openRecentProjectButton}); private Dimension labelSize = GuiUtils.sameSize(new JComponent[] {newProjectsLabel, recentProjectsLabel}); private Dimension comboBoxSize = GuiUtils.sameSize(new JComponent[] {newProjectsComboBox, recentProjectsComboBox}); private LayoutPanel layoutPanel = new LayoutPanel(); public WelcomeScreen() { window.getContentPane().setLayout(new BorderLayout()); window.getContentPane().add(layoutPanel, BorderLayout.CENTER); window.pack(); window.setWidthPercentageOfScreen(0.9); window.center(); openRecentProjectButton.setEnabled(recentProjectsComboBox.getItemCount() > 0); new Listener(); } public void setVisible(boolean visible) { window.setVisible(visible); if (visible) { window.toFront(); window.requestFocus(); } } private class LayoutPanel extends JPanel { public LayoutPanel() { setLayout(null); add(newProjectsLabel); add(newProjectsComboBox); add(newProjectButton); add(openProjectButton); add(recentProjectsLabel); add(openRecentProjectButton); add(recentProjectsComboBox); doLayout(); } @Override public void doLayout() { Dimension size = getSize(); comboBoxSize.width = size.width; comboBoxSize.width -= GuiUtils.PADDING * 3; comboBoxSize.width -= buttonSize.width; newProjectsComboBox.setSize(comboBoxSize); recentProjectsComboBox.setSize(comboBoxSize); int x1 = GuiUtils.PADDING; int x2 = size.width - buttonSize.width - GuiUtils.PADDING; int y = GuiUtils.PADDING; newProjectsLabel.setLocation(x1, y); y += labelSize.height + GuiUtils.PADDING; newProjectsComboBox.setLocation(x1, y); newProjectButton.setLocation(x2, y); y += Math.max(comboBoxSize.height, buttonSize.height) + GuiUtils.PADDING; recentProjectsLabel.setLocation(x1, y); y += labelSize.height + GuiUtils.PADDING; recentProjectsComboBox.setLocation(x1, y); openRecentProjectButton.setLocation(x2, y); y += Math.max(comboBoxSize.height, buttonSize.height) + GuiUtils.PADDING; // spacing y += labelSize.height + GuiUtils.PADDING; openProjectButton.setLocation(x2, y); y += buttonSize.height + GuiUtils.PADDING; setPreferredSize(new Dimension(750, y)); } } private class Listener implements ActionListener { public Listener() { newProjectButton.addActionListener(this); openProjectButton.addActionListener(this); openRecentProjectButton.addActionListener(this); } public void actionPerformed(ActionEvent e) { new ActionThread(e).start(); } } private class ActionThread extends Thread { private ActionEvent e; public ActionThread(ActionEvent e) { this.e = e; } @Override public void run() { Object source = e.getSource(); if (source == newProjectButton) { ProjectTemplate template = (ProjectTemplate) newProjectsComboBox.getSelectedItem(); MarkdownServer.welcomeNewProject(template); } else if (source == openProjectButton) { MarkdownServer.welcomeOpenProject(); } else if (source == openRecentProjectButton) { Object selectedObject = recentProjectsComboBox.getSelectedItem(); if (selectedObject != null) { if (selectedObject instanceof RecentProject) { RecentProject recentProject = (RecentProject) selectedObject; if (recentProject.getProjectFile() != null) { MarkdownServer.welcomeOpenRecentProject(recentProject.getProjectFile()); } } } } MarkdownServer.checkForOpenProjects(); } } }