/* * GuessDatesDialog.java * * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard * * This file is part of BEAST. * See the NOTICE file distributed with this work for additional * information regarding copyright ownership and licensing. * * BEAST is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * BEAST 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 Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with BEAST; if not, write to the * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, * Boston, MA 02110-1301 USA */ package dr.app.beauti.tipdatepanel; import dr.app.beauti.options.DateGuesser; import dr.app.beauti.util.TextUtil; import dr.app.gui.components.RealNumberField; import jam.mac.Utils; import jam.panels.OptionsPanel; import javax.swing.*; import javax.swing.border.EmptyBorder; import java.awt.*; import java.awt.event.*; import java.util.Calendar; import java.util.GregorianCalendar; import java.util.prefs.Preferences; /** * @author Andrew Rambaut * @author Alexei Drummond * @version $Id: PriorDialog.java,v 1.4 2006/09/05 13:29:34 rambaut Exp $ */ public class GuessDatesDialog { public static Preferences PREFS = Preferences.userNodeForPackage(GuessDatesDialog.class); public static final String DELIMIT_RADIO_KEY = "delimitRadio"; public static final String ORDER_COMBO_KEY = "orderCombo"; public static final String PREFIX_TEXT_KEY = "prefixText"; public static final String REGEX_TEXT_KEY = "regexText"; public static final String LOAD_TEXT_KEY = "loadText"; public static final String PARSE_RADIO_KEY = "parseRadio"; public static final String OFFSET_CHECK_KEY = "offsetCheck"; public static final String OFFSET_TEXT_KEY = "offsetText"; public static final String UNLESS_CHECK_KEY = "unlessCheck"; public static final String UNLESS_TEXT_KEY = "unlessText"; public static final String OFFSET2_TEXT_KEY = "offset2Text"; public static final String DATE_FORMAT_TEXT_KEY = "dateFormatText"; private JFrame frame; private final OptionsPanel optionPanel; private final JRadioButton orderRadio = new JRadioButton("Defined just by its order", true); private final JComboBox orderCombo = new JComboBox(new String[]{"first", "second", "third", "fourth", "fourth from last", "third from last", "second from last", "last"}); private final JLabel orderLabel = new JLabel("Order:"); private final JLabel prefixLabel = new JLabel("Prefix:"); private final JRadioButton prefixRadio = new JRadioButton("Defined by a prefix and its order", false); private final JTextField prefixText = new JTextField(16); private final JRadioButton regexRadio = new JRadioButton("Defined by regular expression (REGEX)", false); private final JTextField regexText = new JTextField(16); private final JRadioButton numericalRadio = new JRadioButton("Parse as a number", true); private final JRadioButton calendarRadio = new JRadioButton("Parse as a calendar date", true); private final JRadioButton calendar2Radio = new JRadioButton("Parse calendar dates with variable precision", true); private final JCheckBox offsetCheck = new JCheckBox("Add the following value to each: ", false); private final RealNumberField offsetText = new RealNumberField(); private final JCheckBox unlessCheck = new JCheckBox("...unless less than:", false); private final RealNumberField unlessText = new RealNumberField(); private final RealNumberField offset2Text = new RealNumberField(); private final JTextField dateFormatText = new JTextField(16); private String description = "Extract dates from taxon labels"; private final int defaultDelimitRadioOption; private final int defaultOrderCombo; private final String defaultPrefixText; private final String defaultRegexText; private final int defaultParseRadioOption; private final boolean defaultOffsetCheckOption; private final String defaultOffsetText; private final boolean defaultUnlessCheckOption; private final String defaultUnlessText; private final String defaultOffset2Text; private final String defaultDateFormatText; public GuessDatesDialog(final JFrame frame) { this.frame = frame; defaultDelimitRadioOption = PREFS.getInt(DELIMIT_RADIO_KEY, 0); defaultOrderCombo = PREFS.getInt(ORDER_COMBO_KEY, 0); defaultPrefixText = PREFS.get(PREFIX_TEXT_KEY, ""); defaultRegexText = PREFS.get(REGEX_TEXT_KEY, ""); defaultParseRadioOption = PREFS.getInt(PARSE_RADIO_KEY, 0); defaultOffsetCheckOption = PREFS.getBoolean(OFFSET_CHECK_KEY, false); defaultOffsetText = PREFS.get(OFFSET_TEXT_KEY, "1900"); defaultUnlessCheckOption = PREFS.getBoolean(UNLESS_CHECK_KEY, false); defaultUnlessText = PREFS.get(UNLESS_TEXT_KEY, "16"); defaultOffset2Text = PREFS.get(OFFSET2_TEXT_KEY, "2000"); defaultDateFormatText = PREFS.get(DATE_FORMAT_TEXT_KEY, "yyyy-MM-dd"); optionPanel = new OptionsPanel(12, 12); } private void setupPanel(boolean parsingFromFile) { optionPanel.removeAll(); if (parsingFromFile) { } else { optionPanel.addLabel("The date is given by a numerical field in the taxon label that is:"); optionPanel.addSpanningComponent(orderRadio); // optionPanel.addSeparator(); optionPanel.addSpanningComponent(prefixRadio); optionPanel.addComponents(orderLabel, orderCombo); optionPanel.addComponents(prefixLabel, prefixText); prefixLabel.setEnabled(false); prefixText.setEnabled(false); regexText.setEnabled(false); optionPanel.addComponents(regexRadio, regexText); optionPanel.addSeparator(); } optionPanel.addSpanningComponent(numericalRadio); offsetText.setValue(1900); offsetText.setColumns(16); offsetText.setEnabled(false); optionPanel.addComponents(offsetCheck, offsetText); Calendar calendar = GregorianCalendar.getInstance(); int year = calendar.get(Calendar.YEAR) - 1999; unlessText.setValue(year); unlessText.setColumns(16); unlessText.setEnabled(false); optionPanel.addComponents(unlessCheck, unlessText); offset2Text.setValue(2000); offset2Text.setColumns(16); offset2Text.setEnabled(false); final JLabel offset2Label = new JLabel("...in which case add:"); optionPanel.addComponents(offset2Label, offset2Text); optionPanel.addSpanningComponent(calendarRadio); final JLabel dateFormatLabel = new JLabel("Date format:"); final JButton helpButton = new JButton(Utils.isMacOSX() ? "" : "?"); helpButton.putClientProperty("JButton.buttonType", "help"); JPanel panel = new JPanel(); panel.add(dateFormatText); panel.add(helpButton); panel.setOpaque(false); optionPanel.addComponents(dateFormatLabel, panel); dateFormatText.setText("yyyy-MM-dd"); optionPanel.addSpanningComponent(calendar2Radio); dateFormatLabel.setEnabled(false); dateFormatText.setEnabled(false); numericalRadio.setToolTipText("Parse the date field as a decimal number"); calendarRadio.setToolTipText("Parse the date field using a standard date format specification"); calendar2Radio.setToolTipText("Parse the date field yyyy[-mm[-dd]] with possibly missing month or day"); helpButton.addActionListener(new ActionListener() { public void actionPerformed(final ActionEvent actionEvent) { JScrollPane scrollPane = TextUtil.createHTMLScrollPane( DATE_FORMAT_HELP, new Dimension(560,480)); JOptionPane.showMessageDialog(frame, scrollPane, "Date format help", JOptionPane.PLAIN_MESSAGE); } }); offsetCheck.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { offsetText.setEnabled(offsetCheck.isSelected()); unlessCheck.setEnabled(offsetCheck.isSelected()); unlessText.setEnabled(offsetCheck.isSelected() && unlessCheck.isSelected()); offset2Label.setEnabled(offsetCheck.isSelected() && unlessCheck.isSelected()); offset2Text.setEnabled(offsetCheck.isSelected() && unlessCheck.isSelected()); } }); unlessCheck.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { unlessText.setEnabled(unlessCheck.isSelected()); offset2Label.setEnabled(unlessCheck.isSelected()); offset2Text.setEnabled(unlessCheck.isSelected()); } }); if (!parsingFromFile) { ButtonGroup group = new ButtonGroup(); group.add(orderRadio); group.add(prefixRadio); group.add(regexRadio); orderRadio.setSelected(true); ItemListener listener = new ItemListener() { public void itemStateChanged(ItemEvent e) { orderLabel.setEnabled(!regexRadio.isSelected()); orderCombo.setEnabled(!regexRadio.isSelected()); prefixLabel.setEnabled(prefixRadio.isSelected()); prefixText.setEnabled(prefixRadio.isSelected()); regexText.setEnabled(regexRadio.isSelected()); } }; orderRadio.addItemListener(listener); prefixRadio.addItemListener(listener); regexRadio.addItemListener(listener); } ButtonGroup group = new ButtonGroup(); group.add(numericalRadio); group.add(calendarRadio); group.add(calendar2Radio); ItemListener listener = new ItemListener() { public void itemStateChanged(ItemEvent e) { boolean nrs = numericalRadio.isSelected(); boolean ocs = offsetCheck.isSelected(); boolean ucs = unlessCheck.isSelected(); boolean crs = calendarRadio.isSelected(); offsetCheck.setEnabled(nrs); offsetText.setEnabled(nrs && ocs); unlessCheck.setEnabled(nrs && ocs); unlessText.setEnabled(nrs && ocs && ucs); offset2Label.setEnabled(nrs && ocs && ucs); offset2Text.setEnabled(nrs && ocs && ucs); dateFormatLabel.setEnabled(crs); dateFormatText.setEnabled(crs); } }; numericalRadio.addItemListener(listener); calendarRadio.addItemListener(listener); calendar2Radio.addItemListener(listener); offsetCheck.setSelected(defaultOffsetCheckOption); offsetText.setText(defaultOffsetText); unlessCheck.setSelected(defaultUnlessCheckOption); unlessText.setText(defaultUnlessText); offset2Text.setText(defaultOffset2Text); dateFormatText.setText(defaultDateFormatText); orderCombo.setSelectedIndex(defaultOrderCombo); prefixText.setText(defaultPrefixText); regexText.setText(defaultRegexText); orderCombo.setSelectedIndex(defaultOrderCombo); prefixText.setText(defaultPrefixText); regexText.setText(defaultRegexText); // set from preferences defaults... switch (defaultDelimitRadioOption) { case 0: orderRadio.setSelected(true); break; case 1: prefixRadio.setSelected(true); break; case 2: regexRadio.setSelected(true); break; //case 3: loadRadio.setSelected(true); break; // Not allow loadRadio in prefs to avoid dialog confusion. default: throw new IllegalArgumentException("unknown radio option"); } switch (defaultParseRadioOption) { case 0: numericalRadio.setSelected(true); break; case 1: calendarRadio.setSelected(true); break; case 2: calendar2Radio.setSelected(true); break; default: throw new IllegalArgumentException("unknown radio option"); } } public int showDialog() { return showDialog(false); } public int showDialog(boolean parsingFromFile) { setupPanel(parsingFromFile); JOptionPane optionPane = new JOptionPane(optionPanel, JOptionPane.QUESTION_MESSAGE, JOptionPane.OK_CANCEL_OPTION, null, null, null); optionPane.setBorder(new EmptyBorder(12, 12, 12, 12)); final JDialog dialog = optionPane.createDialog(frame, description); dialog.pack(); dialog.setVisible(true); int result = JOptionPane.CANCEL_OPTION; Integer value = (Integer) optionPane.getValue(); if (value != null && value != -1) { result = value; setPreferencesFromDialog(); } return result; } private void setPreferencesFromDialog() { PREFS.putInt(DELIMIT_RADIO_KEY, (orderRadio.isSelected() ? 0 : (prefixRadio.isSelected() ? 1 : (regexRadio.isSelected() ? 2 : -1)))); PREFS.putInt(ORDER_COMBO_KEY, orderCombo.getSelectedIndex()); PREFS.put(PREFIX_TEXT_KEY, prefixText.getText()); PREFS.put(REGEX_TEXT_KEY, regexText.getText()); PREFS.putInt(PARSE_RADIO_KEY, (numericalRadio.isSelected() ? 0 : (calendarRadio.isSelected() ? 1 : (calendar2Radio.isSelected() ? 2 : -1)))); PREFS.putBoolean(OFFSET_CHECK_KEY, offsetCheck.isSelected()); PREFS.put(OFFSET_TEXT_KEY, offsetText.getText()); PREFS.putBoolean(UNLESS_CHECK_KEY, unlessCheck.isSelected()); PREFS.put(UNLESS_TEXT_KEY, unlessText.getText()); PREFS.put(OFFSET2_TEXT_KEY, offset2Text.getText()); PREFS.put(DATE_FORMAT_TEXT_KEY, dateFormatText.getText()); } public void setupGuesser(DateGuesser guesser) { guesser.order = orderCombo.getSelectedIndex(); guesser.fromLast = false; if (guesser.order > 3) { guesser.fromLast = true; guesser.order = 8 - guesser.order - 1; } if (orderRadio.isSelected()) { guesser.guessType = DateGuesser.GuessType.ORDER; } else if (prefixRadio.isSelected()) { guesser.guessType = DateGuesser.GuessType.PREFIX; guesser.prefix = prefixText.getText(); } else if (regexRadio.isSelected()) { guesser.guessType = DateGuesser.GuessType.REGEX; guesser.regex = regexText.getText(); } else { throw new IllegalArgumentException("unknown radio button selected"); } guesser.parseCalendarDatesAndPrecision = calendar2Radio.isSelected(); guesser.parseCalendarDates = calendarRadio.isSelected(); guesser.calendarDateFormat = dateFormatText.getText(); guesser.offset = 0.0; guesser.unlessLessThan = 0.0; if (offsetCheck.isSelected()) { guesser.offset = offsetText.getValue(); if (unlessCheck.isSelected()) { guesser.unlessLessThan = unlessText.getValue(); guesser.offset2 = offset2Text.getValue(); } } } public void setDescription(String description) { this.description = description; } private static final String DATE_FORMAT_HELP = "
\n" +
" Date and time formats are specified by date and time pattern\n" +
" strings.\n" +
" Within date and time pattern strings, unquoted letters from\n" +
" 'A' to 'Z' and from 'a' to\n" +
" 'z' are interpreted as pattern letters representing the\n" +
" components of a date or time string.\n" +
" Text can be quoted using single quotes (') to avoid\n" +
" interpretation.\n" +
" \"''\" represents a single quote.\n" +
" All other characters are not interpreted; they're simply copied into the\n" +
" output string during formatting or matched against the input string\n" +
" during parsing.\n" +
"
\n" +
" The following pattern letters are defined (all other characters from\n" +
" 'A' to 'Z' and from 'a' to\n" +
" 'z' are reserved):\n" +
"
\n" + "\n" + " Pattern letters are usually repeated, as their number determines the\n" + " exact presentation:\n" + "\n" + "
\n" + "\n" + " Letter\n" + " Date or Time Component\n" + " Presentation\n" + " Examples\n" + " \n" + " G\n" + "Era designator\n" + " Text\n" + " AD\n" + "\n" + " y\n" + "Year\n" + " Year\n" + " 1996;96\n" + "\n" + " M\n" + "Month in year\n" + " Month\n" + " July;Jul;07\n" + "\n" + " w\n" + "Week in year\n" + " Number\n" + " 27\n" + "\n" + " W\n" + "Week in month\n" + " Number\n" + " 2\n" + "\n" + " D\n" + "Day in year\n" + " Number\n" + " 189\n" + "\n" + " d\n" + "Day in month\n" + " Number\n" + " 10\n" + "\n" + " F\n" + "Day of week in month\n" + " Number\n" + " 2\n" + "\n" + " E\n" + "Day in week\n" + " Text\n" + " Tuesday;Tue\n" + "\n" + " a\n" + "Am/pm marker\n" + " Text\n" + " PM\n" + "\n" + " H\n" + "Hour in day (0-23)\n" + " Number\n" + " 0\n" + "\n" + " k\n" + "Hour in day (1-24)\n" + " Number\n" + " 24\n" + "\n" + " K\n" + "Hour in am/pm (0-11)\n" + " Number\n" + " 0\n" + "\n" + " h\n" + "Hour in am/pm (1-12)\n" + " Number\n" + " 12\n" + "\n" + " m\n" + "Minute in hour\n" + " Number\n" + " 30\n" + "\n" + " s\n" + "Second in minute\n" + " Number\n" + " 55\n" + "\n" + " S\n" + "Millisecond\n" + " Number\n" + " 978\n" + "\n" + " z\n" + "Time zone\n" + " General time zone\n" + " Pacific Standard Time;PST;GMT-08:00\n" + "\n" + " Z\n" + "Time zone\n" + " RFC 822 time zone\n" + " -0800\n" + "
Calendar is the Gregorian\n" +
" calendar, the following rules are applied.SimpleDateFormat must interpret the abbreviated year\n" +
" relative to some century. It does this by adjusting dates to be\n" +
" within 80 years before and 20 years after the time the SimpleDateFormat\n" +
" instance is created. For example, using a pattern of \"MM/dd/yy\" and a\n" +
" SimpleDateFormat instance created on Jan 1, 1997, the string\n" +
" \"01/11/12\" would be interpreted as Jan 11, 2012 while the string \"05/04/64\"\n" +
" would be interpreted as May 4, 1964.\n" +
" During parsing, only strings consisting of exactly two digits, as defined by\n" +
" Character.isDigit(char), will be parsed into the default century.\n" +
" Any other numeric string, such as a one digit string, a three or more digit\n" +
" string, or a two digit string that isn't all digits (for example, \"-1\"), is\n" +
" interpreted literally. So \"01/02/3\" or \"01/02/003\" are parsed, using the\n" +
" same pattern, as Jan 2, 3 AD. Likewise, \"01/02/-3\" is parsed as Jan 2, 4 BC.\n" +
"