summaryrefslogtreecommitdiff
path: root/org/freedesktop/dbus/viewer/FileSaver.java
blob: 78b94c785b5dce2c46dd92b68195854cd6215d45 (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
   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 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.awt.Component;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;

import javax.swing.JOptionPane;

final class FileSaver implements Runnable
{
	private static final String CANCEL = "Cancel";

	private static final String SKIP_ALL = "Skip All";

	private static final String SKIP = "Skip";

	private static final String OVERWRITE = "Overwrite";

	private static final String OVERWRITE_ALL = "Overwrite All";

	private final File parentDirectory;

	private final Component parentComponent;

	private final Iterable<TextFile> textFiles;

	FileSaver(Component parentComponent, File parentDirectory,
			Iterable<TextFile> files)
	{
		this.parentComponent = parentComponent;
		this.parentDirectory = parentDirectory;
		this.textFiles = files;
	}

	/** {@inheritDoc} */
	public void run()
	{
		saveFiles();
	}

	private void saveFiles()
	{
		String overwritePolicy = null;
		final Iterator<TextFile> iterator = textFiles.iterator();
		while (iterator.hasNext())
		{
			final TextFile textFile = iterator.next();
			String fileName = textFile.getFileName();
			File fileToSave = new File(parentDirectory, fileName);
			File parentFile = fileToSave.getParentFile();
			if (parentFile.exists() || parentFile.mkdirs())
			{
				boolean doSave = !fileToSave.exists()
						|| OVERWRITE_ALL.equals(overwritePolicy);
				if (!doSave && !SKIP_ALL.equals(overwritePolicy))
				{
					String[] selectionValues;
					if (iterator.hasNext())
					{
						selectionValues = new String[] { OVERWRITE,
								OVERWRITE_ALL, SKIP, SKIP_ALL, CANCEL };
					}
					else
					{
						selectionValues = new String[] { OVERWRITE, CANCEL };
					}
					int option = JOptionPane.showOptionDialog(parentComponent,
							"File exists: " + fileName, "Save",
							JOptionPane.YES_NO_CANCEL_OPTION,
							JOptionPane.QUESTION_MESSAGE, null,
							selectionValues, null);
					if (option == -1)
					{
						break;
					}
					overwritePolicy = selectionValues[option];
					if (CANCEL.equals(overwritePolicy))
					{
						break;
					}

					doSave = OVERWRITE.equals(overwritePolicy)
							|| OVERWRITE_ALL.equals(overwritePolicy);
				}
				if (doSave)
				{
					try
					{
						String contents = textFile.getContents();
						writeFile(fileToSave, contents);
					}
					catch (final IOException ex)
					{
						/* Can't access parent directory for saving */
						final String errorMessage = "Could not save "
								+ fileName + ": " + ex.getLocalizedMessage();
						if (iterator.hasNext())
						{

							int confirm = JOptionPane.showConfirmDialog(
									parentComponent, errorMessage
											+ ".\n"+_("Try saving other files?"),
									_("Save Failed"),
									JOptionPane.OK_CANCEL_OPTION,
									JOptionPane.ERROR_MESSAGE);
							if (confirm != JOptionPane.OK_OPTION)
							{
								break;
							}
						}
						else
						{
							JOptionPane.showMessageDialog(parentComponent,
									errorMessage + ".", _("Save Failed"),
									JOptionPane.ERROR_MESSAGE);
						}
					}
				}
			}
			else
			{

				final String errorMessage = _("Could not access parent directory for ")
						+ fileName;
				if (iterator.hasNext())
				{

					int confirm = JOptionPane.showConfirmDialog(
							parentComponent, errorMessage
									+ ".\n"+_("Try saving other files?"),
							_("Save Failed"), JOptionPane.OK_CANCEL_OPTION,
							JOptionPane.ERROR_MESSAGE);
					if (confirm != JOptionPane.OK_OPTION)
					{
						break;
					}
				}
				else
				{
					JOptionPane.showMessageDialog(parentComponent, errorMessage
							+ ".", _("Save Failed"), JOptionPane.ERROR_MESSAGE);
				}
			}
		}
	}

	/**
	 * @param fileToSave
	 * @param contents
	 * @throws IOException
	 */
	private void writeFile(File fileToSave, String contents) throws IOException
	{
		FileWriter fileWriter = null;
		try
		{
			fileWriter = new FileWriter(fileToSave);
			BufferedWriter writer = new BufferedWriter(fileWriter);
			writer.append(contents);
			writer.flush();
		}
		finally
		{
			if (fileWriter != null)
			{
				try
				{
					fileWriter.close();
				}
				catch (IOException e1)
				{
				}
			}
		}
	}
}