View Javadoc
1   package org.opentrafficsim.swing.gui;
2   
3   import java.awt.BorderLayout;
4   import java.awt.Component;
5   import java.awt.Dimension;
6   import java.awt.FlowLayout;
7   
8   import javax.swing.JFileChooser;
9   import javax.swing.JPanel;
10  
11  /**
12   * Small helper class that adds some components to a JFileChooser, the contents of which can be used to retrieve user settings
13   * for output.
14   * <p>
15   * Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
16   * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
17   * <p>
18   * @version $Revision$, $LastChangedDate$, by $Author$, initial version 14 okt. 2018 <br>
19   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
20   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
21   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
22   */
23  public class JFileChooserWithSettings extends JFileChooser
24  {
25      /** */
26      private static final long serialVersionUID = 20181014L;
27  
28      /**
29       * Constructor that adds components to the left of the 'Save' and 'Cancel' button.
30       * @param components Component...; Components... components to add
31       */
32      public JFileChooserWithSettings(final Component... components)
33      {
34          JPanel saveCancelPanel = (JPanel) ((JPanel) this.getComponent(3)).getComponent(3);
35          // insert leftVsRight panel in original place of saveCancelPanel
36          JPanel leftVsRightPanel = new JPanel();
37          leftVsRightPanel.setLayout(new FlowLayout(FlowLayout.RIGHT, 0, 0));
38          saveCancelPanel.getParent().add(leftVsRightPanel);
39          // saveCancelPanel goes in there to the right
40          leftVsRightPanel.add(saveCancelPanel);
41          // to the left a panel that places it's contents to the south
42          JPanel allignSouthPanel = new JPanel();
43          allignSouthPanel.setPreferredSize(new Dimension(300, 50));
44          leftVsRightPanel.add(allignSouthPanel, 0);
45          // in that left area to the south, a panel for the setting components right to left
46          JPanel settingsPanel = new JPanel();
47          allignSouthPanel.setLayout(new BorderLayout());
48          allignSouthPanel.add(settingsPanel, BorderLayout.SOUTH);
49          settingsPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
50          // add components in that final panel
51          int index = 0;
52          for (Component component : components)
53          {
54              settingsPanel.add(component, index);
55              index++;
56          }
57      }
58  
59      /** {@inheritDoc} */
60      @Override
61      public String toString()
62      {
63          return "JFileChooserWithSettings []";
64      }
65  
66  }