Java tutorial
/******************************************************************************* * Copyright (C) 2013 Alex Beatty, Clayton Bodendein, Kyle Hartmann, * Scott Goble and Peter Brewer * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. ******************************************************************************/ package org.fhaes.FHRecorder; import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartPanel; import org.jfree.chart.plot.PlotOrientation; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTabbedPane; import javax.swing.ScrollPaneConstants; import net.miginfocom.swing.MigLayout; import javax.swing.JCheckBox; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; import javax.swing.JScrollBar; import java.awt.event.AdjustmentListener; import java.awt.event.AdjustmentEvent; /** * Performs a series of initializations for the panels, tabs, and layout for the GUI * @author Alex Beatty, Clayton Bodendein, Kyle Hartmann, Scott Goble */ public class FireHistoryRecorder extends JDialog { private static final long serialVersionUID = 1L; private SampleInputPanel sampleInput; private SiteInfoPanel siteInfo; private CommentsPanel comments; private FileErrorPanel fileErrors; private SummaryPanel summaryPanel; private JScrollPane graphicsScrollable; private GraphicsPanel graphicsPanel; private Hotkeys hotkeys = new Hotkeys(); private static final int FILE_ERRORS_TAB_NUM = 4; /** * Creates new form PrimaryWindow */ public FireHistoryRecorder() { initComponents(); this.setTitle(Controller.progName); hotkeys.setVisible(false); } private void initComponents() { addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { closeAfterRunningChecks(); } }); setMinimumSize(new java.awt.Dimension(950, 675)); setResizable(true); getContentPane().setLayout(new MigLayout("", "[810px,grow]", "[466.00,grow][42.00]")); tabbedPane = new JTabbedPane(JTabbedPane.TOP); getContentPane().add(tabbedPane, "cell 0 0,grow"); sampleInputHolder = new JPanel(); siteInfoHolder = new JPanel(); sampleInputHolder.setLayout(new BorderLayout()); siteInfoHolder.setLayout(new BorderLayout()); tabbedPane.addTab("Data", null, sampleInputHolder, null); tabbedPane.addTab("Metadata", null, siteInfoHolder, null); summaryHolder = new JPanel(); tabbedPane.addTab("Summary", null, summaryHolder, null); graphicsHolder = new JPanel(); tabbedPane.addTab("Graphics", null, graphicsHolder, null); graphicsHolder.setLayout(new BorderLayout(0, 0)); chartScrollBar = new JScrollBar(); chartScrollBar.setMaximum(110); chartScrollBar.addAdjustmentListener(new AdjustmentListener() { public void adjustmentValueChanged(AdjustmentEvent arg0) { if (graphicsPanel != null) { graphicsPanel.updateVisibleYears(chartScrollBar.getValue()); } } }); chartScrollBar.setOrientation(JScrollBar.HORIZONTAL); graphicsHolder.add(chartScrollBar, BorderLayout.SOUTH); fileErrorHolder = new JPanel(); tabbedPane.addTab("File Errors", null, fileErrorHolder, null); panelButtonBar = new JPanel(); getContentPane().add(panelButtonBar, "cell 0 1,alignx right,growy"); useLimitsCheckBox = new JCheckBox("Keep FHX2 Limitations"); panelButtonBar.add(useLimitsCheckBox); useLimitsCheckBox.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { siteInfo.setLimitRestriction(useLimitsCheckBox.isSelected()); } }); btnSave = new JButton("Save"); panelButtonBar.add(btnSave); btnSave.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { saveMenuItemActionPerformed(e); } }); btnClose = new JButton("Close"); btnClose.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { closeAfterRunningChecks(); } }); panelButtonBar.add(btnClose); btnCancel = new JButton("Discard changes"); panelButtonBar.add(btnCancel); btnCancel.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { Controller.filePath = null; setVisible(false); } }); pack(); } /** * Closes the dialog but only after running necessary checks * to ensure user doesn't inadvertently loose data * */ private void closeAfterRunningChecks() { if (Controller.isModified()) { Object[] options = { "Save", "Close without saving", "Cancel" }; int n = JOptionPane.showOptionDialog(Controller.thePrimaryWindow, "Save changes to file before closing?", "Confirm", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[2]); if (n == JOptionPane.YES_OPTION) { Controller.save(); setVisible(false); } else if (n == JOptionPane.NO_OPTION) { Controller.setIsModified(false); Controller.setIsChangedSinceOpened(false); setVisible(false); } else if (n == JOptionPane.CANCEL_OPTION) { return; } } setVisible(false); } public void selectFirstSample() { try { this.sampleInput.lstSamples.setSelectedIndex(0); } catch (Exception e) { } } private void saveMenuItemActionPerformed(java.awt.event.ActionEvent evt) { updateOptionalData(); Controller.save(); } private JTabbedPane tabbedPane; private JPanel sampleInputHolder = new JPanel(); private JPanel siteInfoHolder = new JPanel(); private JPanel panelButtonBar; private JButton btnSave; private JButton btnCancel; private JButton btnClose; private JPanel fileErrorHolder; private JCheckBox useLimitsCheckBox; private JPanel summaryHolder; private JPanel graphicsHolder; private JScrollBar chartScrollBar; // Diplays all valid screens public void generateScreens(FHX2File inFHX2File) { sampleInput = new SampleInputPanel(inFHX2File.getRequiredPart()); siteInfo = new SiteInfoPanel(inFHX2File.getOptionalPart()); comments = new CommentsPanel(inFHX2File.getOptionalPart()); fileErrors = new FileErrorPanel(); summaryPanel = new SummaryPanel(); graphicsPanel = new GraphicsPanel(); tabbedPane.addChangeListener(new TabChangeListener(tabbedPane, summaryPanel, graphicsPanel)); sampleInputHolder.add(sampleInput, BorderLayout.CENTER); siteInfoHolder.add(siteInfo, BorderLayout.CENTER); siteInfoHolder.add(comments, BorderLayout.SOUTH); fileErrorHolder.add(fileErrors, BorderLayout.CENTER); summaryHolder.add(summaryPanel, BorderLayout.WEST); graphicsHolder.add(graphicsPanel, BorderLayout.CENTER); } public void showInput() { this.tabbedPane.setSelectedIndex(0); this.redrawInputSamplePanel(); } public void showInfo() { this.tabbedPane.setSelectedIndex(1); } // This is going to cause problems if it's ever called (especially if there is no tab there) public void showComments() { this.tabbedPane.setSelectedIndex(2); } public void redrawInputSamplePanel() { sampleInput.redrawSamplePanel(Controller.getSelectedSampleIndex()); } private void updateOptionalData() { siteInfo.saveInfoToData(); comments.saveComments(); } /** * Sets the file error text to errorText * Also opens up the file error tab, so the user will know about it immediately * @param errorText The error string you want to set the file error text to */ public void setFileError(String errorText) { fileErrors.setErrorText(errorText); tabbedPane.setSelectedIndex(FILE_ERRORS_TAB_NUM); } }