|
| 1 | +package nl.rug.jbi.jsm.frontend.ui; |
| 2 | + |
| 3 | +import javax.swing.*; |
| 4 | +import java.awt.*; |
| 5 | +import java.awt.event.ActionEvent; |
| 6 | +import java.awt.event.ActionListener; |
| 7 | + |
| 8 | +public class ExportDialog extends JDialog implements ActionListener { |
| 9 | + |
| 10 | + private Result result = null; |
| 11 | + private boolean exportByScope = false; |
| 12 | + private JTextField exportPath = new JTextField(); |
| 13 | + |
| 14 | + private ExportDialog(final Frame frame) { |
| 15 | + super(frame, "Export Results", true); |
| 16 | + |
| 17 | + final JPanel centerPane = new JPanel(); |
| 18 | + centerPane.setLayout(new BoxLayout(centerPane, BoxLayout.Y_AXIS)); |
| 19 | + |
| 20 | + final JLabel l1 = new JLabel("Please give a path where to export the data."); |
| 21 | + l1.setAlignmentX(0.5f); |
| 22 | + centerPane.add(l1); |
| 23 | + |
| 24 | + final JLabel l2 = new JLabel("The path has to contain '%s' which will get replaced by an identifier for each metric."); |
| 25 | + l2.setAlignmentX(0.5f); |
| 26 | + centerPane.add(l2); |
| 27 | + |
| 28 | + centerPane.add(createExportPathTextField()); |
| 29 | + centerPane.add(createExportTypeSwitch()); |
| 30 | + centerPane.setBorder(BorderFactory.createEmptyBorder(10, 10, 0, 10)); |
| 31 | + |
| 32 | + final Container contentPane = getContentPane(); |
| 33 | + contentPane.add(centerPane, BorderLayout.CENTER); |
| 34 | + contentPane.add(createButtonPane(), BorderLayout.PAGE_END); |
| 35 | + |
| 36 | + pack(); |
| 37 | + setLocationRelativeTo(frame); |
| 38 | + } |
| 39 | + |
| 40 | + public static Result showDialog(final Component frameComp) { |
| 41 | + final Frame frame = JOptionPane.getFrameForComponent(frameComp); |
| 42 | + final ExportDialog ed = new ExportDialog(frame); |
| 43 | + ed.setVisible(true); |
| 44 | + return ed.getResult(); |
| 45 | + } |
| 46 | + |
| 47 | + private JComponent createButtonPane() { |
| 48 | + //Export button |
| 49 | + final JButton exportButton = new JButton("Export"); |
| 50 | + exportButton.setActionCommand("export"); |
| 51 | + exportButton.addActionListener(this); |
| 52 | + //Cancel button |
| 53 | + final JButton cancelButton = new JButton("Cancel"); |
| 54 | + cancelButton.setActionCommand("cancel"); |
| 55 | + cancelButton.addActionListener(this); |
| 56 | + getRootPane().setDefaultButton(cancelButton); |
| 57 | + |
| 58 | + final JPanel buttonPane = new JPanel(); |
| 59 | + buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.LINE_AXIS)); |
| 60 | + buttonPane.setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 10)); |
| 61 | + buttonPane.add(Box.createHorizontalGlue()); |
| 62 | + buttonPane.add(exportButton); |
| 63 | + buttonPane.add(Box.createRigidArea(new Dimension(10, 0))); |
| 64 | + buttonPane.add(cancelButton); |
| 65 | + |
| 66 | + return buttonPane; |
| 67 | + } |
| 68 | + |
| 69 | + private JComponent createExportTypeSwitch() { |
| 70 | + final JRadioButton metricExport = new JRadioButton("Export by Metric"); |
| 71 | + metricExport.setActionCommand("metric-export"); |
| 72 | + metricExport.setSelected(true); |
| 73 | + metricExport.setToolTipText("Each metric will be exported into an individual file."); |
| 74 | + |
| 75 | + final JRadioButton scopeExport = new JRadioButton("Export by Scope"); |
| 76 | + scopeExport.setActionCommand("scope-export"); |
| 77 | + scopeExport.setToolTipText("Each scope will be exported into a file containing all metrics."); |
| 78 | + |
| 79 | + final ButtonGroup group = new ButtonGroup(); |
| 80 | + group.add(metricExport); |
| 81 | + group.add(scopeExport); |
| 82 | + |
| 83 | + metricExport.addActionListener(this); |
| 84 | + scopeExport.addActionListener(this); |
| 85 | + |
| 86 | + final JPanel choicePanel = new JPanel(new FlowLayout()); |
| 87 | + choicePanel.add(metricExport); |
| 88 | + choicePanel.add(scopeExport); |
| 89 | + |
| 90 | + return choicePanel; |
| 91 | + } |
| 92 | + |
| 93 | + private JComponent createExportPathTextField() { |
| 94 | + final JPanel exportPathPanel = new JPanel(); |
| 95 | + exportPathPanel.setLayout(new BoxLayout(exportPathPanel, BoxLayout.LINE_AXIS)); |
| 96 | + |
| 97 | + exportPathPanel.add(new JLabel("Export Path: ")); |
| 98 | + exportPathPanel.add(this.exportPath); |
| 99 | + |
| 100 | + return exportPathPanel; |
| 101 | + } |
| 102 | + |
| 103 | + private Result getResult() { |
| 104 | + return this.result; |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + public void actionPerformed(ActionEvent e) { |
| 109 | + if ("export".equals(e.getActionCommand())) { |
| 110 | + this.result = new Result(this.exportPath.getText(), this.exportByScope); |
| 111 | + this.setVisible(false); |
| 112 | + } else if ("cancel".equals(e.getActionCommand())) { |
| 113 | + this.setVisible(false); |
| 114 | + } else if ("metric-export".equals(e.getActionCommand())) { |
| 115 | + this.exportByScope = false; |
| 116 | + } else if ("scope-export".equals(e.getActionCommand())) { |
| 117 | + this.exportByScope = true; |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + public static class Result { |
| 122 | + public final String exportPath; |
| 123 | + public final boolean groupScopes; |
| 124 | + |
| 125 | + public Result(final String exportPath, final boolean groupScopes) { |
| 126 | + this.exportPath = exportPath; |
| 127 | + this.groupScopes = groupScopes; |
| 128 | + } |
| 129 | + } |
| 130 | +} |
0 commit comments