summaryrefslogtreecommitdiff
path: root/src/traffic-incidents-service/org.genivi.trafficinfo.dbus-java/src/main/java/org/freedesktop/dbus/viewer/SaveFileAction.java
blob: 487e22dd1946467b6d1434f5dea2364eda904923 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*
   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._;

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<TextFile>
	{
		boolean iterated = false;
		/** {@inheritDoc} */
		public boolean hasNext()
		{
			return !iterated;
		}

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

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

	}

	SaveFileAction(JTabbedPane tabbedPane)
	{
		super(tabbedPane);
		
		enableAndSetName();
		
		tabbedPane.addChangeListener(this);
	}

	/** {@inheritDoc} */
	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, _("Save ") + getFileName(selectedIndex) + "...");
		setEnabled(enabled);
	}

	/** {@inheritDoc} */
	public Iterator<TextFile> iterator()
	{
		return new SelectedTabIterator();
	}
}