Skip to content

Commit

Permalink
update project creator ui for high dpi screens
Browse files Browse the repository at this point in the history
  • Loading branch information
K-Meech committed Aug 3, 2023
1 parent 1523923 commit f4ca9ce
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 55 deletions.
130 changes: 78 additions & 52 deletions src/main/java/org/embl/mobie/lib/create/ui/ProjectsCreatorPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,13 @@ public class ProjectsCreatorPanel extends JFrame {
static { net.imagej.patcher.LegacyInjector.preinit(); }

private ProjectCreator projectsCreator;
private JPanel contentGrid;
private JComboBox<String> datasetComboBox;
private JComboBox<String> sourcesComboBox;
private JComboBox<String> groupsComboBox;
private JComboBox<String> viewsComboBox;
private final int labelPaddingX = 15;
private final int labelPaddingY = 15;

private static ProjectCreator.ImageType imageType = ProjectCreator.ImageType.image;
private static ImageDataFormat imageDataFormat = ImageDataFormat.BdvN5;
Expand All @@ -101,19 +104,27 @@ public ProjectsCreatorPanel ( File projectLocation ) throws IOException {
File dataDirectory = ProjectCreatorHelper.getDataLocation( projectLocation );
this.projectsCreator = new ProjectCreator( dataDirectory );

addDatasetPanel();
addSourcesPanel();
this.getContentPane().add(new JSeparator(SwingConstants.HORIZONTAL));
this.getContentPane().add( Box.createVerticalStrut( 10 ) );
addViewsPanel();
this.getContentPane().setLayout( new BoxLayout(this.getContentPane(), BoxLayout.Y_AXIS ) );

// Panel for main content - with a 10 pixel border
contentGrid = new JPanel();
contentGrid.setBorder( BorderFactory.createEmptyBorder(10, 10, 10, 10) );
contentGrid.setLayout( new GridBagLayout());

addDatasetsToGrid( 0 );
addSourcesToGrid( 1 );
addSeparatorToGrid( 2 );
addViewsToGrid(3 );

this.getContentPane().add( contentGrid );
addButtonsPanel();

String shortenedProjectName = projectLocation.getName();
if ( shortenedProjectName.length() > 50 ) {
shortenedProjectName = shortenedProjectName.substring( 0, 47 ) + "...";
}

this.setTitle( "Editing MoBIE Project: " + shortenedProjectName );
this.getContentPane().setLayout( new BoxLayout(this.getContentPane(), BoxLayout.Y_AXIS ) );
this.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );
}

Expand All @@ -130,11 +141,10 @@ public ProjectCreator getProjectsCreator() {
return projectsCreator;
}

private void addDatasetPanel() {
final JPanel horizontalLayoutPanel = SwingUtils.horizontalLayoutPanel();
private void addDatasetsToGrid(int rowIndex ) {

final JButton addButton = SwingHelper.createButton("Add");
final JButton editButton = SwingHelper.createButton("Edit");
final JButton addButton = new JButton("Add");
final JButton editButton = new JButton("Edit");

createDatasetComboBox();
addButton.addActionListener( e ->
Expand All @@ -147,13 +157,11 @@ private void addDatasetPanel() {
new Thread( () -> { editDatasetDialog(); } ).start();
} );

horizontalLayoutPanel.add( SwingHelper.getJLabel("dataset", 60, 10));
horizontalLayoutPanel.add(datasetComboBox);
horizontalLayoutPanel.add(addButton);
horizontalLayoutPanel.add(editButton);
horizontalLayoutPanel.setAlignmentX( Component.LEFT_ALIGNMENT );

this.getContentPane().add(horizontalLayoutPanel);
contentGrid.add( createJLabel("dataset"),
createGridBagConstraints(0.5, 0, rowIndex, labelPaddingX, labelPaddingY));
contentGrid.add( datasetComboBox, createGridBagConstraints(1, 1, rowIndex, 0, 0) );
contentGrid.add( addButton, createGridBagConstraints(0.5, 2, rowIndex, 0, 0) );
contentGrid.add( editButton, createGridBagConstraints(0.5, 3, rowIndex, 0, 0) );
}

private String[] getDatasetNames() {
Expand All @@ -169,11 +177,22 @@ private String[] getDatasetNames() {
}
}

private GridBagConstraints createGridBagConstraints(double weightX, int gridX, int gridY, int ipadX, int ipadY) {
GridBagConstraints constraints = new GridBagConstraints();
constraints.fill = GridBagConstraints.HORIZONTAL;
constraints.weightx = weightX;
constraints.gridx = gridX;
constraints.gridy = gridY;
constraints.ipadx = ipadX;
constraints.ipady = ipadY;
return constraints;
}

private void createDatasetComboBox() {
String[] datasetNames = getDatasetNames();
datasetComboBox = new JComboBox<>( datasetNames );
datasetComboBox.setSelectedItem( datasetNames[0] );
setComboBoxDimensions(datasetComboBox);
setComboBoxDisplayParameters(datasetComboBox);
datasetComboBox.setPrototypeDisplayValue( UserInterfaceHelper.PROTOTYPE_DISPLAY_VALUE);
datasetComboBox.addItemListener( new SyncAllWithDatasetComboBox() );
}
Expand All @@ -189,7 +208,7 @@ private void createSoucesComboBox() {
sourcesComboBox = new JComboBox<>( new String[] {""} );
sourcesComboBox.setSelectedItem( "" );
}
setComboBoxDimensions(sourcesComboBox);
setComboBoxDisplayParameters(sourcesComboBox);
sourcesComboBox.setPrototypeDisplayValue( UserInterfaceHelper.PROTOTYPE_DISPLAY_VALUE);
}

Expand All @@ -207,7 +226,7 @@ private void createGroupsCombobox() {
groupsComboBox = new JComboBox<>( new String[] {""} );
groupsComboBox.setSelectedItem( "" );
}
setComboBoxDimensions(groupsComboBox);
setComboBoxDisplayParameters(groupsComboBox);
groupsComboBox.setPrototypeDisplayValue( UserInterfaceHelper.PROTOTYPE_DISPLAY_VALUE);
groupsComboBox.addItemListener( new SyncGroupAndViewComboBox() );
}
Expand All @@ -228,15 +247,23 @@ private void createViewsCombobox() {
viewsComboBox.setSelectedItem( "" );
}

setComboBoxDimensions(viewsComboBox);
setComboBoxDisplayParameters(viewsComboBox);
viewsComboBox.setPrototypeDisplayValue( UserInterfaceHelper.PROTOTYPE_DISPLAY_VALUE);
}

public static void setComboBoxDimensions( JComboBox< String > comboBox )
private JLabel createJLabel( String text ) {
JLabel label = new JLabel( text );
SwingHelper.alignJLabel( label );
return label;
}

private void setComboBoxDisplayParameters(JComboBox< String > comboBox )
{
comboBox.setPrototypeDisplayValue( UserInterfaceHelper.PROTOTYPE_DISPLAY_VALUE );
comboBox.setPreferredSize( new Dimension( 350, 20 ) );
comboBox.setMaximumSize( new Dimension( 350, 20 ) );
// Keep preferred height as is, but double the preferred width of the combobox
// (to ensure there's room to view long names). By basing this on .getPreferredSize(), it will scale with the
// screen dpi used
comboBox.setPreferredSize( new Dimension(comboBox.getPreferredSize().width*2, comboBox.getPreferredSize().height ) );
}

private class SyncAllWithDatasetComboBox implements ItemListener {
Expand All @@ -258,13 +285,12 @@ public void itemStateChanged(ItemEvent event) {
}
}

private void addSourcesPanel() {
final JPanel horizontalLayoutPanel = SwingUtils.horizontalLayoutPanel();
private void addSourcesToGrid(int rowIndex ) {

final JButton addButton = SwingHelper.createButton( "Add" );
final JButton addButton = new JButton( "Add" );
// for now we don't support editing any image properties, but this is likely to change in future,
// so keep this code for now
// final JButton editButton = createButton("Edit");
// final JButton editButton = new JButton("Edit");

createSoucesComboBox();
addButton.addActionListener( e ->
Expand All @@ -277,36 +303,36 @@ private void addSourcesPanel() {
// new Thread( () -> { editImageDialog(); } ).start();
// } );

horizontalLayoutPanel.add( SwingHelper.getJLabel("source", 60, 10));
horizontalLayoutPanel.add(sourcesComboBox);
horizontalLayoutPanel.add( addButton );
horizontalLayoutPanel.add( Box.createHorizontalStrut( SwingHelper.BUTTON_DIMENSION.width ) );
// horizontalLayoutPanel.add( editButton );
horizontalLayoutPanel.setAlignmentX( Component.LEFT_ALIGNMENT );

this.getContentPane().add(horizontalLayoutPanel);
contentGrid.add( createJLabel("source"),
createGridBagConstraints(0.5, 0, rowIndex, labelPaddingX, labelPaddingY) );
contentGrid.add( sourcesComboBox, createGridBagConstraints(1, 1, rowIndex, 0, 0) );
contentGrid.add( addButton, createGridBagConstraints(0.5, 2, rowIndex, 0, 0) );
}

private void addViewsPanel() {
final JPanel groupPanel = SwingUtils.horizontalLayoutPanel();
final JPanel viewsPanel = SwingUtils.horizontalLayoutPanel();
private void addViewsToGrid(int rowIndex ) {

createGroupsCombobox();
createViewsCombobox();

groupPanel.add( SwingHelper.getJLabel("group", 60, 10));
viewsPanel.add( SwingHelper.getJLabel("view", 60, 10));
groupPanel.add( groupsComboBox );
viewsPanel.add( viewsComboBox );
groupPanel.add( Box.createHorizontalStrut( SwingHelper.BUTTON_DIMENSION.width ) );
groupPanel.add( Box.createHorizontalStrut( SwingHelper.BUTTON_DIMENSION.width ) );
viewsPanel.add( Box.createHorizontalStrut( SwingHelper.BUTTON_DIMENSION.width ) );
viewsPanel.add( Box.createHorizontalStrut( SwingHelper.BUTTON_DIMENSION.width ) );
groupPanel.setAlignmentX( Component.LEFT_ALIGNMENT );
viewsPanel.setAlignmentX( Component.LEFT_ALIGNMENT );

this.getContentPane().add( groupPanel );
this.getContentPane().add( viewsPanel );
contentGrid.add( createJLabel("group"),
createGridBagConstraints(0.5, 0, rowIndex, labelPaddingX, labelPaddingY) );
contentGrid.add( groupsComboBox, createGridBagConstraints(1, 1, rowIndex, 0, 0) );
contentGrid.add( createJLabel("view"),
createGridBagConstraints(0.5, 0, rowIndex + 1, labelPaddingX, labelPaddingY));
contentGrid.add( viewsComboBox, createGridBagConstraints(1, 1, rowIndex+1, 0, 0) );
}

private void addSeparatorToGrid( int rowIndex ) {
GridBagConstraints separatorConstraints = createGridBagConstraints( 0.5, 0, rowIndex, 0, 30 );
separatorConstraints.gridwidth = contentGrid.getWidth();

// Have to wrap the separator in another panel to get it to centre properly vertically
JPanel wrapper = new JPanel(new GridBagLayout());
GridBagConstraints wrapperConstraints = createGridBagConstraints( 1, 0, 0, 0, 0);
wrapperConstraints.gridwidth = GridBagConstraints.REMAINDER;
wrapper.add( new JSeparator(SwingConstants.HORIZONTAL), wrapperConstraints );

contentGrid.add( wrapper, separatorConstraints );
}

private void addButtonsPanel() {
Expand Down
10 changes: 7 additions & 3 deletions src/main/java/org/embl/mobie/lib/ui/SwingHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,16 @@ public static JLabel getJLabel( String text, int width, int height )
{
final JLabel comp = new JLabel( text );
comp.setPreferredSize( new Dimension( width,height ) );
comp.setHorizontalAlignment( SwingConstants.LEFT );
comp.setHorizontalTextPosition( SwingConstants.LEFT );
comp.setAlignmentX( Component.LEFT_ALIGNMENT );
alignJLabel( comp );
return comp;
}

public static void alignJLabel( JLabel label ) {
label.setHorizontalAlignment( SwingConstants.LEFT );
label.setHorizontalTextPosition( SwingConstants.LEFT );
label.setAlignmentX( Component.LEFT_ALIGNMENT );
}

public static JButton createButton( String buttonLabel )
{
return createButton( buttonLabel, BUTTON_DIMENSION );
Expand Down

0 comments on commit f4ca9ce

Please sign in to comment.