Skip to main content
summaryrefslogtreecommitdiffstats
blob: ac6c9ab0e29cb930b3951cdee56655b18458bb6a (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
package org.eclipse.mylar.tasklist.ui.wizards;

import org.eclipse.jface.dialogs.IDialogSettings;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.mylar.core.MylarPlugin;
import org.eclipse.mylar.tasklist.MylarTasklistPlugin;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Text;

/**
 * Wizard Page for the Task Data Export Wizard
 * 
 * @author Wesley Coelho
 */
public class TaskDataExportWizardPage extends WizardPage {

	private final static String PAGE_TITLE = "Export Mylar Task Data";
	
	//Control fields
	private Button taskListCheckBox = null;
	private Button taskActivationHistoryCheckBox = null;
	private Button taskContextsCheckBox = null;;
	private Button browseButton = null;
	private Text destDirText = null; 
	private Button overwriteCheckBox = null;
	
	//Key values for the dialog settings object
	private final static String SETTINGS_SAVED = "Settings saved";
	private final static String TASKLIST_SETTING = "Tasklist setting";
	private final static String ACTIVATION_HISTORY_SETTING = "Activation history setting";
	private final static String CONTEXTS_SETTING = "Contexts setting";
	private final static String DEST_DIR_SETTING = "Destination directory setting";
	private final static String OVERWRITE_SETTING = "Overwrite setting";
	

	public TaskDataExportWizardPage(){
		super("org.eclipse.mylar.tasklist.exportPage", PAGE_TITLE, 
				MylarTasklistPlugin.imageDescriptorFromPlugin(MylarTasklistPlugin.PLUGIN_ID, "icons/wizban/banner-export.gif"));
		
		setPageComplete(false);
	}
	
	
	/**
	 * Create the widgets on the page
	 */
	public void createControl(Composite parent) {
		try {
			Composite container = new Composite(parent, SWT.NONE);
			GridLayout layout = new GridLayout(1, false);
			container.setLayout(layout);
			
			createFileSelectionControl(container);
			createExportDirectoryControl(container);
			
			overwriteCheckBox = createCheckBox(container, "Overwrite existing files without warning");
			
			initSettings();
			
			setControl(container);
			
			setPageComplete(validate());
		} catch (RuntimeException e) {
			MylarPlugin.fail(e, "Could not create export wizard page", true);
		} 
	}

	/**
	 * Create widgets for selecting the data files to export
	 */
	private void createFileSelectionControl(Composite parent){
		Group group = new Group(parent, SWT.SHADOW_ETCHED_IN);	
		GridLayout gl = new GridLayout(1, false);
		group.setLayout(gl);
		GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
		group.setLayoutData(gridData);
		group.setText("Select data to export:");
		
		taskListCheckBox = createCheckBox(group, "Task List");
		taskActivationHistoryCheckBox = createCheckBox(group, "Task Activation History");
		taskContextsCheckBox = createCheckBox(group, "Task Contexts");
	}
	
	/**
	 * Create widgets for specifying the destination directory
	 */
	private void createExportDirectoryControl(Composite parent) {
		Group destDirGroup= new Group(parent, SWT.SHADOW_ETCHED_IN);
		destDirGroup.setText("Export destination folder");
		destDirGroup.setLayout(new GridLayout(2, false));
		destDirGroup.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		
		destDirText = new Text(destDirGroup, SWT.BORDER);		
		destDirText.setEditable(false);
		destDirText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		destDirText.addModifyListener(new ModifyListener(){
			public void modifyText(ModifyEvent e) {
				controlChanged();
			}
		});
		
		
		browseButton = new Button(destDirGroup, SWT.PUSH);
		browseButton.setText("Browse...");
		if (!MylarPlugin.getContextManager().hasActiveContext()) {
			browseButton.setEnabled(true);
		} else {
			browseButton.setEnabled(false);
		}
		browseButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				DirectoryDialog dialog = new DirectoryDialog(getShell());
				dialog.setText("Folder Selection");
				dialog.setMessage("Specify the destination folder for task data");
				String dir = destDirText.getText();
				dir = dir.replaceAll("\\\\", "/");
				dialog.setFilterPath(dir);

				dir = dialog.open();
				if(dir == null || dir.equals(""))
					return;
				destDirText.setText(dir);
			}
		});        
	}	
	
	
	/**
	 * Initializes controls with values from the Dialog Settings object
	 */
	protected void initSettings(){
		IDialogSettings settings = getDialogSettings();
		
		if (settings.get(SETTINGS_SAVED) == null){
			//Set default values
			taskListCheckBox.setSelection(true);
			taskActivationHistoryCheckBox.setSelection(true);
			taskContextsCheckBox.setSelection(true);
			destDirText.setText("");
			overwriteCheckBox.setSelection(true);
		}
		else{
			//Retrieve previous values from the dialog settings
			taskListCheckBox.setSelection(settings.getBoolean(TASKLIST_SETTING));
			taskActivationHistoryCheckBox.setSelection(settings.getBoolean(ACTIVATION_HISTORY_SETTING));
			taskContextsCheckBox.setSelection(settings.getBoolean(CONTEXTS_SETTING));
			String directory = settings.get(DEST_DIR_SETTING);
			if(directory != null){
				destDirText.setText(settings.get(DEST_DIR_SETTING));
			}	
			overwriteCheckBox.setSelection(settings.getBoolean(OVERWRITE_SETTING));
		}
	}
	
	/**
	 * Saves the control values in the dialog settings to be used as
	 * defaults the next time the page is opened
	 */
	public void saveSettings(){
		IDialogSettings settings = getDialogSettings();
		
		settings.put(TASKLIST_SETTING, taskListCheckBox.getSelection());
		settings.put(ACTIVATION_HISTORY_SETTING, taskActivationHistoryCheckBox.getSelection());
		settings.put(CONTEXTS_SETTING, taskContextsCheckBox.getSelection());
		settings.put(DEST_DIR_SETTING, destDirText.getText());
		settings.put(OVERWRITE_SETTING, overwriteCheckBox.getSelection());

		settings.put(SETTINGS_SAVED, SETTINGS_SAVED);
	}
	
	/** Convenience method for creating a new checkbox */
	protected Button createCheckBox(Composite parent, String text){
		Button newButton= new Button(parent, SWT.CHECK);
		newButton.setText(text);
		
		newButton.addSelectionListener(new SelectionListener(){

			public void widgetSelected(SelectionEvent e) {
				controlChanged();
			}

			public void widgetDefaultSelected(SelectionEvent e) {
				//No action required
			}
		});
		
		return newButton;
	}
	
	/** Called to indicate that a control's value has changed */
	public void controlChanged(){
		setPageComplete(validate());
	}

	/** Returns true if the information entered by the user is valid */
	protected boolean validate(){
		
		//Check that at least one type of data has been selected
		if (!taskListCheckBox.getSelection() && 
				!taskActivationHistoryCheckBox.getSelection() && 
				!taskContextsCheckBox.getSelection()){
			return false;
		}
		
		//Check that a destination dir has been specified
		if (destDirText.getText().equals("")){
			return false;
		}
		
		return true;
	}
	
	/** Returns the directory where data files are to be saved */
	public String getDestinationDirectory(){
		return destDirText.getText();
	}
	
	/** True if the user wants to export the task list */
	public boolean exportTaskList(){
		return taskListCheckBox.getSelection();
	}
	
	/** True if the user wants to export task activation history */
	public boolean exportActivationHistory(){
		return taskActivationHistoryCheckBox.getSelection();
	}
	
	/** True if the user wants to export task context files */
	public boolean exportTaskContexts(){
		return taskContextsCheckBox.getSelection();
	}
	
	/** True if the user wants to overwrite files by default*/
	public boolean overwrite(){
		return overwriteCheckBox.getSelection();
	}
}

Back to the top