View Javadoc

1   /*
2    * This file is a part of CAST project.
3    * (c) Copyright 2007, AGH University of Science & Technology
4    * https://caribou.iisg.agh.edu.pl/trac/cast
5    *
6    * Licensed under the Eclipse Public License, Version 1.0 (the "License").
7    * You may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    * http://www.eclipse.org/legal/epl-v10.html
10   */
11  /*
12   * File: DataSetNameDialog.java
13   * Created: 2009-10-05
14   * Author: entrop
15   * $Id$
16   */
17  
18  package pl.edu.agh.cast.importer.wizard.dialog;
19  
20  import org.eclipse.jface.dialogs.IDialogConstants;
21  import org.eclipse.jface.dialogs.IInputValidator;
22  import org.eclipse.jface.dialogs.InputDialog;
23  import org.eclipse.swt.SWT;
24  import org.eclipse.swt.events.SelectionAdapter;
25  import org.eclipse.swt.events.SelectionEvent;
26  import org.eclipse.swt.widgets.Button;
27  import org.eclipse.swt.widgets.Composite;
28  import org.eclipse.swt.widgets.Shell;
29  
30  import pl.edu.agh.cast.importer.wizard.util.Messages;
31  import pl.edu.agh.cast.ui.util.MsgBoxHelper;
32  
33  /**
34   * Dialog used in <code>ImporterWizard</code> to get target data set name.
35   * 
36   * @author AGH CAST Team
37   */
38  public class DataSetNameDialog extends InputDialog {
39  
40  	private final String defaultName;
41  
42  	private Button defaultNameBtn;
43  
44  	/**
45  	 * Constructor.
46  	 * 
47  	 * @param parent
48  	 *            parent shell for dialog
49  	 * @param initialValue
50  	 *            initial name of data set
51  	 */
52  	public DataSetNameDialog(Shell parent, String initialValue) {
53  		super(parent, Messages.DataSetNameDialog_Title, Messages.DataSetNameDialog_0, initialValue,
54  		        new DataSetNameValidator());
55  		defaultName = initialValue;
56  	}
57  
58  	/**
59  	 * Override this method to remove Cancel button.
60  	 * 
61  	 * {@inheritDoc}
62  	 * 
63  	 * @see org.eclipse.jface.dialogs.InputDialog#createButtonsForButtonBar(org.eclipse.swt.widgets.Composite)
64  	 */
65  	@Override
66  	protected void createButtonsForButtonBar(Composite parent) {
67  
68  		// create SAVE, Cancel and Default buttons
69  		createButton(parent, IDialogConstants.OK_ID, Messages.DataSetNameDialog_Save, true);
70  		// we use client_id (not cancel_id) as we override default action
71  		Button cancelBtn = createButton(parent, IDialogConstants.CLIENT_ID, IDialogConstants.CANCEL_LABEL, false);
72  		cancelBtn.addSelectionListener(new SelectionAdapter() {
73  			@Override
74  			public void widgetSelected(SelectionEvent e) {
75  				if (MsgBoxHelper.showQuestionBox(getParentShell(), Messages.DataSetNameDialog_3,
76  				        Messages.DataSetNameDialog_4) == SWT.YES) {
77  					cancelPressed();
78  				}
79  			}
80  		});
81  		defaultNameBtn = createButton(parent, IDialogConstants.CLIENT_ID + 1, Messages.DataSetNameDialog_1, false);
82  		defaultNameBtn.addSelectionListener(new SelectionAdapter() {
83  
84  			@Override
85  			public void widgetSelected(SelectionEvent e) {
86  				getText().setText(defaultName);
87  				getText().selectAll();
88  			}
89  		});
90  
91  		// do this here because setting the text will set enablement on the ok
92  		// button
93  		getText().setFocus();
94  		if (getValue() != null) {
95  			getText().setText(getValue());
96  			getText().selectAll();
97  		}
98  	}
99  
100 	/**
101 	 * This class validates a data set name. It makes sure that the name is not empty.
102 	 * 
103 	 */
104 	static class DataSetNameValidator implements IInputValidator {
105 		/**
106 		 * Validates the data set name. Returns null for no error or an error message.
107 		 * 
108 		 * Data set name is correct if it is not empty.
109 		 * 
110 		 * {@inheritDoc}
111 		 * 
112 		 * @see org.eclipse.jface.dialogs.IInputValidator#isValid(java.lang.String)
113 		 */
114 		@Override
115 		public String isValid(String newText) {
116 			if (newText == null || newText.trim().length() == 0) {
117 				return Messages.DataSetNameDialog_2;
118 			}
119 
120 			// Input must be OK
121 			return null;
122 		}
123 	}
124 
125 }