Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 44553e5908ffe4348ee4c6b64479a1262b984233 (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
/*******************************************************************************
 * Copyright (c) 2011 Wind River Systems, Inc. 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:
 * Wind River Systems - initial API and implementation
 *******************************************************************************/
package org.eclipse.tcf.te.tcf.processes.ui.internal.dialogs;

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.dialogs.StatusDialog;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.tcf.te.tcf.processes.ui.activator.UIPlugin;
import org.eclipse.tcf.te.tcf.processes.ui.internal.preferences.IPreferenceConsts;
import org.eclipse.tcf.te.tcf.processes.ui.nls.Messages;

/**
 * The dialog to configure the refreshing interval of the process list.
 */
public class IntervalConfigDialog extends StatusDialog implements ModifyListener, IPreferenceConsts {
	// The text field to enter the interval value.
	private Text text;
	// The entered result
	private int result;

	/**
	 * Constructor
	 */
	public IntervalConfigDialog(Shell parent) {
	    super(parent);
    }

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.jface.dialogs.StatusDialog#configureShell(org.eclipse.swt.widgets.Shell)
	 */
	@Override
    protected void configureShell(Shell shell) {
		shell.setText(Messages.IntervalConfigDialog_DialogTitle);
		super.configureShell(shell);
    }
	
	/*
	 * (non-Javadoc)
	 * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
	 */
	@Override
    protected Control createDialogArea(Composite parent) {
	    Composite composite = (Composite) super.createDialogArea(parent);
	    
	    Composite comp1 = new Composite(composite, SWT.NONE);
	    GridData data = new GridData(SWT.FILL, SWT.CENTER, true, false);
	    comp1.setLayoutData(data);
	    GridLayout layout = new GridLayout(3, false);
	    layout.horizontalSpacing = 0;
	    comp1.setLayout(layout);
	    
	    Label label = new Label(comp1, SWT.RADIO);
	    label.setText(Messages.IntervalConfigDialog_ChoiceOneLabel);
	    
	    text = new Text(comp1, SWT.SINGLE | SWT.BORDER);
	    text.setTextLimit(Text.LIMIT);
	    data = new GridData();
	    data.widthHint = 70;
	    text.setLayoutData(data);
	    text.setText(""+result); //$NON-NLS-1$
	    text.selectAll();
	    text.setFocus();
	    text.addModifyListener(this);
	    
	    label = new Label(comp1, SWT.NONE);
	    label.setText(Messages.IntervalConfigDialog_SECONDS);
    	
	    return composite;
    }
	
	/**
	 * Check if the current input is valid and return an IStatus object to return
	 * the checking result, containing the message and the validating code.
	 * 
	 * @return A status to indicate if the input is valid.
	 */
	private IStatus isInputValid() {
		String pluginId = UIPlugin.getUniqueIdentifier();
		String txt = text.getText();
		if (txt == null || txt.trim().length() == 0) {
			return new Status(IStatus.ERROR, pluginId, null);
		}
		try {
			int interval = Integer.parseInt(txt.trim());
			if (interval < 0) return new Status(IStatus.ERROR, pluginId, Messages.IntervalConfigDialog_BiggerThanZero);
			if(interval == 0) return new Status(IStatus.WARNING, pluginId, Messages.IntervalConfigDialog_ZeroWarning);
		}
		catch (NumberFormatException e) {
			return new Status(IStatus.ERROR, pluginId, Messages.IntervalConfigDialog_InvalidNumber);
		}
		return Status.OK_STATUS;
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.jface.dialogs.Dialog#okPressed()
	 */
	@Override
    protected void okPressed() {
		String txt = text.getText().trim();
		result = Integer.parseInt(txt);
	    super.okPressed();
    }
	
	/**
	 * Get the input result, a time interval.
	 * 
	 * @return The input result.
	 */
	public int getResult() {
		return result;
	}

	/**
	 * Validate the current input and update the button and the error message.
	 */
	private void validateInput() {
		IStatus status = isInputValid();
		updateStatus(status);
	}

	/**
	 * The label provider used to display the speed grades in the combo viewer.
	 */
	static class GradeLabelProvider extends LabelProvider {
		@Override
        public String getText(Object element) {
			if(element instanceof IntervalGrade) {
				IntervalGrade grade = (IntervalGrade) element;
				return grade.getName() + " ("+grade.getValue()+" "+Messages.IntervalConfigDialog_SECOND_ABBR+")"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
			}
	        return super.getText(element);
        }
	}

	/**
	 * Get the current the speed grades in an array of Grade.
	 * 
	 * @return The current speed grades.
	 */
	IntervalGrade[] getGrades(){
		List<IntervalGrade> gradeList = new ArrayList<IntervalGrade>();
        IPreferenceStore prefStore = UIPlugin.getDefault().getPreferenceStore();
		String grades = prefStore.getString(PREF_INTERVAL_GRADES);
		Assert.isNotNull(grades);
		StringTokenizer st = new StringTokenizer(grades, "|"); //$NON-NLS-1$
		while(st.hasMoreTokens()) {
			String token = st.nextToken();
			StringTokenizer st2 = new StringTokenizer(token, ":"); //$NON-NLS-1$
			String name = st2.nextToken();
			String value = st2.nextToken();
			try{
				int seconds = Integer.parseInt(value);
				if(seconds > 0) {
					gradeList.add(new IntervalGrade(name, seconds));
				}
			}
			catch (NumberFormatException nfe) {
			}
		}
		return gradeList.toArray(new IntervalGrade[gradeList.size()]);
    }

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.swt.events.ModifyListener#modifyText(org.eclipse.swt.events.ModifyEvent)
	 */
	@Override
    public void modifyText(ModifyEvent e) {
		validateInput();
    }

	/**
	 * Set the current interval to the text field.
	 * 
	 * @param interval The current interval.
	 */
	public void setResult(int interval) {
		this.result = interval;
    }
}

Back to the top