Skip to main content
summaryrefslogtreecommitdiffstats
blob: 11da9d2dfdf0adf5137a74ec03bfe76870a78c5f (plain) (blame)
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*******************************************************************************
 * Copyright (c) 2003 - 2005 University Of British Columbia and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     University Of British Columbia - initial API and implementation
 *******************************************************************************/
package org.eclipse.mylar.bugzilla.ui.query;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.mylar.bugzilla.core.BugzillaPlugin;


/**
 * This class manages accessing and persisting named Bugzilla queries.
 */
public class SavedQueryFile
{
	/** The file that the queries are written to */
	private File file;

	/** The directory to where the file is located */
	private File directory;

	/** A list of remembered queries */
	private ArrayList<String> list = new ArrayList<String>();
	private ArrayList<String> nameList = new ArrayList<String>();
	private ArrayList<String> sumList = new ArrayList<String>();

	/**
	 * Constructor
	 * @param dirPath The path to the directory where the favorites file should be written to
	 * @param fileName The file that the favorites should be written to
	 */
	public SavedQueryFile(String dirPath, String fileName) 
	{
		// create a new file and if it exists, read the data from the file
		// else create the file and directories if they dont exist
		file = new File(dirPath + fileName);
		if (file.exists()) {
			readFile();
		}
		else  {
			directory = new File(dirPath);
			if (!directory.exists())
				directory.mkdirs();
			writeFile();
		}
	}

	/**
	 * Add a query to the list
	 * @param entry The query to add
	 */
	public int add(String entry, String name, String sum) 
	{
		// add the entry to the list and write the file to disk
		int index = find(name);
		if (index == -1) {
			list.add(entry);
			nameList.add(name);
			sumList.add(sum);
			writeFile();
		}
		else {
			boolean isDuplicate = MessageDialog.openConfirm(BugzillaPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getShell(),
									"Save Bugzilla Query",
									name + " already exists. Do you want to replace it?"); 
			if (isDuplicate) {
				list.add(index, entry);
				nameList.add(index,name);
				sumList.add(index,sum);
				list.remove(index+1);
				nameList.remove(index+1);
				sumList.remove(index+1);
				writeFile();
			}
		}
	
		index = find(entry);
		return index;
	}

	
	/**
	 * Find a bug in the query list
	 * @param name The name of the query that we are looking for
	 * @return The index of the query in the array if it exists, else -1
	 */
	public int find(String name) 
	{
		for (int i = 0; i < list.size(); i++) {
			String str = nameList.get(i);
			if (name.compareTo(str) == 0)
				return i;
		}
		return -1;
	}

	/**
	 * Get the list of queries
	 * @return The list of queries
	 */
	public ArrayList<String> elements() 
	{
		return list;
	}

	/**
	 * Write the queries to disk
	 */
	private void writeFile() 
	{
		try {
			OutputStream os = new FileOutputStream(file);
			DataOutputStream data = new DataOutputStream(os);
		
			// Write the size of the list so that we can read it back in easier
			data.writeInt(list.size());
		
			// write each element in the array list
			for (int i = 0; i < list.size(); i++) {
				String item = list.get(i);
				String itemName = nameList.get(i);
				String summary = sumList.get(i);
				
				// write the string in a machine independant manner
				data.writeUTF(item);
				data.writeUTF(itemName);
				data.writeUTF(summary);
			}
		
			// close the file
			data.close();
		}
		catch (IOException e) {
			// put up a message and log the error if there is a problem writing to the file
		    BugzillaPlugin.getDefault().logAndShowExceptionDetailsDialog(e, "occurred while saving your Bugzilla queries", "I/O Error");
		}
	}

	/**
	 * Read the queries in from the file on disk
	 */
	private void readFile() 
	{
		try {
			InputStream is = new FileInputStream(file);
			DataInputStream data = new DataInputStream(is);
		
			// get the number of favorites in the file
			int size = data.readInt();
		
			// read in each of the favorites in the file
			for (int nX = 0; nX < size; nX++) {
				String item, name, summary;
			
				// get the data from disk in a machine independant way
				item = data.readUTF();
				name = data.readUTF();
				summary = data.readUTF();
				
				// add the favorite to the favorites list
				list.add(item);
				nameList.add(name);
				sumList.add(summary);
			}
		
			// close the input stream
			data.close();
		}
		catch (IOException e)  {
			// put up a message and log the error if there is a problem reading from the file
		    BugzillaPlugin.getDefault().logAndShowExceptionDetailsDialog(e, "occurred while restoring saved Bugzilla queries.", "I/O Error");
		}
	}

	/**
	 * Remove some queries from the list
	 * @param indicesToRemove An array of the indicies of the queries to be removed
	 */
	public void remove(int[] indicesToRemove) 
	{
		int timesShifted = 0;
	
		// remove each of the indicated items from the array
		for (int i = 0; i < indicesToRemove.length; i++) {
			list.remove(indicesToRemove[i] - timesShifted);
			nameList.remove(indicesToRemove[i] - timesShifted);
			sumList.remove(indicesToRemove[i] - timesShifted);
			timesShifted++;
		}
		
		// rewrite the file so that the data is persistant
		writeFile();
	
		// remove the items from the combo box
		timesShifted = 0;
	}

	/**
	 * Remove all of the items in the favortes menu
	 */
	public void removeAll() {
		// remove every element from the favorites list
		while (list.size() > 0) {
			list.remove(0);
			nameList.remove(0);
			sumList.remove(0);
		}
	
		// rewrite the file so that the data is persistant
		writeFile();
	}
	
	/**
	 * Get the query parameters of the currently selected remembered query
	 * @return The query url
	 */
	public String getQueryParameters(int index) {
		return list.get(index);
	}
	
	/**
	 * Get the summary text of the currently selected remembered query
	 * @return The summary text
	 */
	public String getSummaryText(int index) {
		return sumList.get(index);

	}
	
	public ArrayList<String> getNames() {
		return nameList;
	}
}

Back to the top