Search in sources :

Example 1 with PatternFormat

use of org.freeplane.features.format.PatternFormat in project freeplane by freeplane.

the class TimeManagement method createDateFormatChooser.

private JComboBox createDateFormatChooser() {
    class DateFormatComboBoxElement {

        private final PatternFormat dateFormat;

        DateFormatComboBoxElement(PatternFormat dateFormat) {
            this.dateFormat = dateFormat;
        }

        PatternFormat getDateFormat() {
            return dateFormat;
        }

        public String toString() {
            return dateFormat.formatObject(getCalendarDate()).toString();
        }
    }
    final String dateFormatPattern = ResourceController.getResourceController().getProperty("date_format");
    final Vector<DateFormatComboBoxElement> values = new Vector<DateFormatComboBoxElement>();
    final List<PatternFormat> datePatterns = FormatController.getController().getDateFormats();
    int selectedIndex = 0;
    for (int i = 0; i < datePatterns.size(); ++i) {
        final PatternFormat patternFormat = datePatterns.get(i);
        values.add(new DateFormatComboBoxElement(patternFormat));
        if (patternFormat.getPattern().equals(dateFormatPattern)) {
            dateFormat = patternFormat;
            selectedIndex = i;
        }
    }
    final JComboBox dateFormatChooser = new JComboBox(values);
    dateFormatChooser.setFocusable(false);
    if (!datePatterns.isEmpty()) {
        dateFormatChooser.setSelectedIndex(selectedIndex);
        dateFormat = ((DateFormatComboBoxElement) (dateFormatChooser.getSelectedItem())).getDateFormat();
    }
    dateFormatChooser.addItemListener(new ItemListener() {

        public void itemStateChanged(final ItemEvent e) {
            dateFormat = ((DateFormatComboBoxElement) e.getItem()).getDateFormat();
            final Component focusOwner = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
            if (focusOwner instanceof JTable) {
                JTable table = (JTable) focusOwner;
                final int[] selectedRows = table.getSelectedRows();
                final int[] selectedColumns = table.getSelectedColumns();
                for (int r : selectedRows) for (int c : selectedColumns) {
                    Object date = table.getValueAt(r, c);
                    if (date instanceof FormattedDate) {
                        final FormattedDate fd = (FormattedDate) date;
                        if (!fd.getDateFormat().equals(dateFormat)) {
                            table.setValueAt(new FormattedDate(fd, dateFormat.getPattern()), r, c);
                        }
                    }
                }
            } else {
                ModeController mController = Controller.getCurrentModeController();
                for (final NodeModel node : mController.getMapController().getSelectedNodes()) {
                    final MTextController textController = (MTextController) TextController.getController();
                    Object date = node.getUserObject();
                    if (date instanceof FormattedDate) {
                        final FormattedDate fd = (FormattedDate) date;
                        if (!fd.getDateFormat().equals(dateFormat)) {
                            textController.setNodeObject(node, new FormattedDate(fd, dateFormat.getPattern()));
                        }
                    }
                }
            }
        }
    });
    dateFormatChooser.setAlignmentX(Component.LEFT_ALIGNMENT);
    return dateFormatChooser;
}
Also used : ItemEvent(java.awt.event.ItemEvent) JComboBox(javax.swing.JComboBox) PatternFormat(org.freeplane.features.format.PatternFormat) ModeController(org.freeplane.features.mode.ModeController) FormattedDate(org.freeplane.features.format.FormattedDate) NodeModel(org.freeplane.features.map.NodeModel) JTable(javax.swing.JTable) ItemListener(java.awt.event.ItemListener) MTextController(org.freeplane.features.text.mindmapmode.MTextController) Component(java.awt.Component) JComponent(javax.swing.JComponent) JTextComponent(javax.swing.text.JTextComponent) Vector(java.util.Vector)

Example 2 with PatternFormat

use of org.freeplane.features.format.PatternFormat in project freeplane by freeplane.

the class PatternFormatTest method testOnePattern.

private void testOnePattern(String pattern) {
    final PatternFormat formatter = PatternFormat.guessPatternFormat(pattern);
    assertNotNull("could not create a formatter for pattern " + pattern, formatter);
    if (formatter.acceptsDate())
        System.err.println(pattern + "->" + formatter.formatObject(new Date()));
    else if (formatter.acceptsNumber())
        System.err.println(pattern + "->" + formatter.formatObject(1223.456789));
    else
        System.err.println(pattern + "->" + formatter.formatObject("Hello world!"));
}
Also used : PatternFormat(org.freeplane.features.format.PatternFormat) Date(java.util.Date)

Aggregations

PatternFormat (org.freeplane.features.format.PatternFormat)2 Component (java.awt.Component)1 ItemEvent (java.awt.event.ItemEvent)1 ItemListener (java.awt.event.ItemListener)1 Date (java.util.Date)1 Vector (java.util.Vector)1 JComboBox (javax.swing.JComboBox)1 JComponent (javax.swing.JComponent)1 JTable (javax.swing.JTable)1 JTextComponent (javax.swing.text.JTextComponent)1 FormattedDate (org.freeplane.features.format.FormattedDate)1 NodeModel (org.freeplane.features.map.NodeModel)1 ModeController (org.freeplane.features.mode.ModeController)1 MTextController (org.freeplane.features.text.mindmapmode.MTextController)1