All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.freedesktop.dbus.viewer.SaveFileAction Maven / Gradle / Ivy

Go to download

Improved version of the DBus-Java library provided by freedesktop.org (https://dbus.freedesktop.org/doc/dbus-java/).

There is a newer version: 3.3.2
Show newest version
/*
   D-Bus Java Viewer
   Copyright (c) 2006 Peter Cox

   This program is free software; you can redistribute it and/or modify it
   under the terms of either the GNU Lesser General Public License Version 2 or the
   Academic Free Licence Version 2.1.

   Full licence texts are included in the COPYING file with this program.
*/
package org.freedesktop.dbus.viewer;

import static org.freedesktop.dbus.Gettext.t;

import java.util.Iterator;
import java.util.NoSuchElementException;

import javax.swing.Action;
import javax.swing.JTabbedPane;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

@SuppressWarnings("serial")
class SaveFileAction extends TabbedSaveAction implements ChangeListener {
    private class SelectedTabIterator implements Iterator {
        // CHECKSTYLE:OFF
        boolean iterated = false;
        // CHECKSTYLE:ON

        /** {@inheritDoc} */
        @Override
        public boolean hasNext() {
            return !iterated;
        }

        /** {@inheritDoc} */
        @Override
        public TextFile next() {
            if (iterated) {
                throw new NoSuchElementException(t("Already iterated"));
            }
            iterated = true;
            return getTextFile(tabbedPane.getSelectedIndex());
        }

        /** {@inheritDoc} */
        @Override
        public void remove() {
            throw new UnsupportedOperationException();
        }

    }

    SaveFileAction(JTabbedPane tabbedPane) {
        super(tabbedPane);

        enableAndSetName();

        tabbedPane.addChangeListener(this);
    }

    /** {@inheritDoc} */
    @Override
    public void stateChanged(ChangeEvent e) {
        enableAndSetName();
    }

    /**
     * Enable and set the name of the action based on the shown tab
     */
    void enableAndSetName() {
        int selectedIndex = tabbedPane.getSelectedIndex();
        boolean enabled = selectedIndex > -1;
        putValue(Action.NAME, t("Save ") + getFileName(selectedIndex) + "...");
        setEnabled(enabled);
    }

    /** {@inheritDoc} */
    @Override
    public Iterator iterator() {
        return new SelectedTabIterator();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy