~ubuntu-branches/debian/sid/eclipse-cdt/sid

« back to all changes in this revision

Viewing changes to results/plugins/org.eclipse.cdt.make.ui/src/org/eclipse/cdt/make/internal/ui/preferences/AbstractMakefileEditorPreferencePage.java

  • Committer: Package Import Robot
  • Author(s): Jakub Adam
  • Date: 2011-10-06 21:15:04 UTC
  • mfrom: (1.1.4)
  • Revision ID: package-import@ubuntu.com-20111006211504-8dutmljjih0zikfv
Tags: 8.0.1-1
* New upstream release.
* Split the JNI packages into a separate architecture dependent
  package and made eclipse-cdt architecture independent.
* Install JNI libraries into multiarch aware location
* Bumped Standards-Version to 3.9.2.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*******************************************************************************
2
 
 * Copyright (c) 2002, 2008 QNX Software Systems and others.
3
 
 * All rights reserved. This program and the accompanying materials
4
 
 * are made available under the terms of the Eclipse Public License v1.0
5
 
 * which accompanies this distribution, and is available at
6
 
 * http://www.eclipse.org/legal/epl-v10.html
7
 
 *
8
 
 * Contributors:
9
 
 *     QNX Software Systems - Initial API and implementation
10
 
 *******************************************************************************/
11
 
 
12
 
package org.eclipse.cdt.make.internal.ui.preferences;
13
 
 
14
 
import java.io.BufferedReader;
15
 
import java.io.IOException;
16
 
import java.io.InputStreamReader;
17
 
import com.ibm.icu.text.MessageFormat;
18
 
import java.util.HashMap;
19
 
import java.util.Iterator;
20
 
import java.util.Map;
21
 
import java.util.Set;
22
 
 
23
 
import org.eclipse.cdt.make.internal.ui.MakeUIPlugin;
24
 
import org.eclipse.core.runtime.IStatus;
25
 
import org.eclipse.jface.dialogs.DialogPage;
26
 
import org.eclipse.jface.dialogs.IMessageProvider;
27
 
import org.eclipse.jface.preference.PreferencePage;
28
 
import org.eclipse.swt.SWT;
29
 
import org.eclipse.swt.events.ModifyEvent;
30
 
import org.eclipse.swt.events.ModifyListener;
31
 
import org.eclipse.swt.events.SelectionEvent;
32
 
import org.eclipse.swt.events.SelectionListener;
33
 
import org.eclipse.swt.graphics.Font;
34
 
import org.eclipse.swt.layout.GridData;
35
 
import org.eclipse.swt.widgets.Button;
36
 
import org.eclipse.swt.widgets.Composite;
37
 
import org.eclipse.swt.widgets.Control;
38
 
import org.eclipse.swt.widgets.Label;
39
 
import org.eclipse.swt.widgets.Text;
40
 
import org.eclipse.ui.IWorkbench;
41
 
import org.eclipse.ui.IWorkbenchPreferencePage;
42
 
 
43
 
/**
44
 
 * AbstraceMakeEditorPreferencePage
45
 
 */
46
 
public abstract class AbstractMakefileEditorPreferencePage extends PreferencePage implements IWorkbenchPreferencePage {
47
 
        OverlayPreferenceStore fOverlayStore;
48
 
        
49
 
        Map fCheckBoxes= new HashMap();
50
 
        private SelectionListener fCheckBoxListener= new SelectionListener() {
51
 
                public void widgetDefaultSelected(SelectionEvent e) {
52
 
                }
53
 
                public void widgetSelected(SelectionEvent e) {
54
 
                        Button button= (Button) e.widget;
55
 
                        fOverlayStore.setValue((String) fCheckBoxes.get(button), button.getSelection());
56
 
                }
57
 
        };
58
 
        
59
 
        Map fTextFields= new HashMap();
60
 
        private ModifyListener fTextFieldListener= new ModifyListener() {
61
 
                public void modifyText(ModifyEvent e) {
62
 
                        Text text= (Text) e.widget;
63
 
                        fOverlayStore.setValue((String) fTextFields.get(text), text.getText());
64
 
                }
65
 
        };
66
 
 
67
 
        private Map fNumberFields= new HashMap();
68
 
        private ModifyListener fNumberFieldListener= new ModifyListener() {
69
 
                public void modifyText(ModifyEvent e) {
70
 
                        numberFieldChanged((Text) e.widget);
71
 
                }
72
 
        };
73
 
                        
74
 
        public AbstractMakefileEditorPreferencePage() {
75
 
                super();
76
 
                setPreferenceStore(MakeUIPlugin.getDefault().getPreferenceStore());
77
 
                fOverlayStore= createOverlayStore();
78
 
        }
79
 
        
80
 
        protected abstract OverlayPreferenceStore createOverlayStore();
81
 
        
82
 
        /* (non-Javadoc)
83
 
         * @see org.eclipse.ui.IWorkbenchPreferencePage#init(org.eclipse.ui.IWorkbench)
84
 
         */
85
 
        public void init(IWorkbench workbench) {
86
 
        }
87
 
        
88
 
        protected void initializeFields() {
89
 
                Map checkBoxes= getCheckBoxes();
90
 
                Map textFields= getTextFields();
91
 
                Iterator e= checkBoxes.keySet().iterator();
92
 
                while (e.hasNext()) {
93
 
                        Button b= (Button) e.next();
94
 
                        String key= (String) checkBoxes.get(b);
95
 
                        b.setSelection(getOverlayStore().getBoolean(key));
96
 
                }
97
 
                
98
 
                e= textFields.keySet().iterator();
99
 
                while (e.hasNext()) {
100
 
                        Text t= (Text) e.next();
101
 
                        String key= (String) textFields.get(t);
102
 
                        t.setText(getOverlayStore().getString(key));
103
 
                }               
104
 
        }
105
 
        
106
 
        /* (non-Javadoc)
107
 
         * @see org.eclipse.jface.preference.IPreferencePage#performOk()
108
 
         */
109
 
        public boolean performOk() {
110
 
                getOverlayStore().propagate();
111
 
                MakeUIPlugin.getDefault().savePluginPreferences();
112
 
                return true;
113
 
        }
114
 
        
115
 
        protected OverlayPreferenceStore getOverlayStore() {
116
 
                return fOverlayStore;
117
 
        }
118
 
        
119
 
        protected OverlayPreferenceStore setOverlayStore() {
120
 
                return fOverlayStore;
121
 
        }
122
 
        
123
 
        protected Map getCheckBoxes() {
124
 
                return fCheckBoxes;
125
 
        }
126
 
        
127
 
        protected Map getTextFields() {
128
 
                return fTextFields;
129
 
        }
130
 
        
131
 
        protected Map getNumberFields() {
132
 
                return fNumberFields;
133
 
        }
134
 
        
135
 
        /* (non-Javadoc)
136
 
         * @see org.eclipse.jface.preference.PreferencePage#performDefaults()
137
 
         */
138
 
        protected void performDefaults() {
139
 
                getOverlayStore().loadDefaults();
140
 
                initializeFields();
141
 
                handleDefaults();
142
 
                super.performDefaults();
143
 
        }
144
 
        
145
 
        protected abstract void handleDefaults();
146
 
        
147
 
        /* (non-Javadoc)
148
 
         * @see org.eclipse.jface.dialogs.IDialogPage#dispose()
149
 
         */
150
 
        public void dispose() {
151
 
                if (getOverlayStore() != null) {
152
 
                        getOverlayStore().stop();
153
 
                        fOverlayStore= null;
154
 
                }
155
 
                super.dispose();
156
 
        }
157
 
        
158
 
        protected Button addCheckBox(Composite parent, String labelText, String key, int indentation) {         
159
 
                Button checkBox= new Button(parent, SWT.CHECK);
160
 
                checkBox.setText(labelText);
161
 
                checkBox.setFont(parent.getFont());
162
 
                
163
 
                GridData gd= new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
164
 
                gd.horizontalIndent= indentation;
165
 
                gd.horizontalSpan= 2;
166
 
                checkBox.setLayoutData(gd);
167
 
                checkBox.addSelectionListener(fCheckBoxListener);
168
 
                
169
 
                getCheckBoxes().put(checkBox, key);
170
 
                
171
 
                return checkBox;
172
 
        }
173
 
        
174
 
        protected Control addTextField(Composite composite, String labelText, String key, int textLimit, int indentation, String[] errorMessages) {
175
 
                Font font= composite.getFont();
176
 
                
177
 
                Label label= new Label(composite, SWT.NONE);
178
 
                label.setText(labelText);
179
 
                label.setFont(font);
180
 
                GridData gd= new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
181
 
                gd.horizontalIndent= indentation;
182
 
                label.setLayoutData(gd);
183
 
                
184
 
                Text textControl= new Text(composite, SWT.BORDER | SWT.SINGLE);
185
 
                textControl.setFont(font);              
186
 
                gd= new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
187
 
                gd.widthHint= convertWidthInCharsToPixels(textLimit + 1);
188
 
                textControl.setLayoutData(gd);
189
 
                textControl.setTextLimit(textLimit);
190
 
                getTextFields().put(textControl, key);
191
 
                if (errorMessages != null) {
192
 
                        getNumberFields().put(textControl, errorMessages);
193
 
                        textControl.addModifyListener(fNumberFieldListener);
194
 
                } else {
195
 
                        textControl.addModifyListener(fTextFieldListener);
196
 
                }
197
 
                        
198
 
                return textControl;
199
 
        }
200
 
        
201
 
        void numberFieldChanged(Text textControl) {
202
 
                String number= textControl.getText();
203
 
                IStatus status= validatePositiveNumber(number, (String[])getNumberFields().get(textControl));
204
 
                if (!status.matches(IStatus.ERROR)) {
205
 
                        getOverlayStore().setValue((String) getTextFields().get(textControl), number);
206
 
                }
207
 
                updateStatus(status);
208
 
        }
209
 
        
210
 
        private IStatus validatePositiveNumber(String number, String[] errorMessages) {
211
 
                StatusInfo status= new StatusInfo();
212
 
                if (number.length() == 0) {
213
 
                        status.setError(errorMessages[0]);
214
 
                } else {
215
 
                        try {
216
 
                                int value= Integer.parseInt(number);
217
 
                                if (value < 0)
218
 
                                        status.setError(MessageFormat.format(errorMessages[1], new String[]{number}));
219
 
                        } catch (NumberFormatException e) {
220
 
                                status.setError(MessageFormat.format(errorMessages[1], new String[]{number}));
221
 
                        }
222
 
                }
223
 
                return status;
224
 
        }
225
 
        
226
 
        private void updateStatus(IStatus status) {
227
 
                if (!status.matches(IStatus.ERROR)) {
228
 
                        Set keys= getNumberFields().keySet();
229
 
                        for (Iterator iter = keys.iterator(); iter.hasNext();) {
230
 
                                Text text = (Text) iter.next();
231
 
                                IStatus s= validatePositiveNumber(text.getText(), (String[])getNumberFields().get(text));
232
 
                                status= s.getSeverity() > status.getSeverity() ? s : status;
233
 
                        }
234
 
                }       
235
 
                setValid(!status.matches(IStatus.ERROR));
236
 
                applyToStatusLine(this, status);
237
 
        }
238
 
 
239
 
        /*
240
 
         * Applies the status to the status line of a dialog page.
241
 
         */
242
 
        private void applyToStatusLine(DialogPage page, IStatus status) {
243
 
                String message= status.getMessage();
244
 
                switch (status.getSeverity()) {
245
 
                        case IStatus.OK:
246
 
                                page.setMessage(message, IMessageProvider.NONE);
247
 
                                page.setErrorMessage(null);
248
 
                                break;
249
 
                        case IStatus.WARNING:
250
 
                                page.setMessage(message, IMessageProvider.WARNING);
251
 
                                page.setErrorMessage(null);
252
 
                                break;                          
253
 
                        case IStatus.INFO:
254
 
                                page.setMessage(message, IMessageProvider.INFORMATION);
255
 
                                page.setErrorMessage(null);
256
 
                                break;                  
257
 
                        default:
258
 
                                if (message.length() == 0) {
259
 
                                        message= null;
260
 
                                }
261
 
                                page.setMessage(null);
262
 
                                page.setErrorMessage(message);
263
 
                                break;          
264
 
                }
265
 
        }
266
 
        
267
 
        /**
268
 
         * Returns an array of size 2:
269
 
         *  - first element is of type <code>Label</code>
270
 
         *  - second element is of type <code>Text</code>
271
 
         * Use <code>getLabelControl</code> and <code>getTextControl</code> to get the 2 controls.
272
 
         */
273
 
        protected Control[] addLabelledTextField(Composite composite, String label, String key, int textLimit, int indentation, String[] errorMessages) {
274
 
                Label labelControl= new Label(composite, SWT.NONE);
275
 
                labelControl.setText(label);
276
 
                labelControl.setFont(composite.getFont());
277
 
                GridData gd= new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
278
 
                gd.horizontalIndent= indentation;
279
 
                labelControl.setLayoutData(gd);
280
 
        
281
 
                Text textControl= new Text(composite, SWT.BORDER | SWT.SINGLE);         
282
 
                gd= new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
283
 
                gd.widthHint= convertWidthInCharsToPixels(textLimit + 1);
284
 
                textControl.setLayoutData(gd);
285
 
                textControl.setTextLimit(textLimit);
286
 
                textControl.setFont(composite.getFont());
287
 
                fTextFields.put(textControl, key);
288
 
                if (errorMessages != null) {
289
 
                        fNumberFields.put(textControl, errorMessages);
290
 
                        textControl.addModifyListener(fNumberFieldListener);
291
 
                } else {
292
 
                        textControl.addModifyListener(fTextFieldListener);
293
 
                }
294
 
                
295
 
                return new Control[]{labelControl, textControl};
296
 
        }
297
 
        
298
 
        protected String loadPreviewContentFromFile(String filename) {
299
 
                String line;
300
 
                String separator= System.getProperty("line.separator", "\n"); //$NON-NLS-1$ //$NON-NLS-2$
301
 
                StringBuffer buffer= new StringBuffer(512);
302
 
                BufferedReader reader= null;
303
 
                try {
304
 
                        reader= new BufferedReader(new InputStreamReader(getClass().getResourceAsStream(filename)));
305
 
                        while ((line= reader.readLine()) != null) {
306
 
                                buffer.append(line);
307
 
                                buffer.append(separator);
308
 
                        }
309
 
                } catch (IOException io) {
310
 
                        MakeUIPlugin.log(io);
311
 
                } finally {
312
 
                        if (reader != null) {
313
 
                                try { reader.close(); } catch (IOException e) {}
314
 
                        }
315
 
                }
316
 
                return buffer.toString();
317
 
        }
318
 
}