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

« back to all changes in this revision

Viewing changes to build/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, 2010 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<Control, String> fCheckBoxes= new HashMap<Control, String>();
 
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(fCheckBoxes.get(button), button.getSelection());
 
56
                }
 
57
        };
 
58
        
 
59
        Map<Control, String> fTextFields= new HashMap<Control, String>();
 
60
        private ModifyListener fTextFieldListener= new ModifyListener() {
 
61
                public void modifyText(ModifyEvent e) {
 
62
                        Text text= (Text) e.widget;
 
63
                        fOverlayStore.setValue(fTextFields.get(text), text.getText());
 
64
                }
 
65
        };
 
66
 
 
67
        private Map<Text, String[]> fNumberFields= new HashMap<Text, String[]>();
 
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<Control, String> checkBoxes= getCheckBoxes();
 
90
                Map<Control, String> textFields= getTextFields();
 
91
                Iterator<Control> e= checkBoxes.keySet().iterator();
 
92
                while (e.hasNext()) {
 
93
                        Button b= (Button) e.next();
 
94
                        String key= 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= 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
        @Override
 
110
        public boolean performOk() {
 
111
                getOverlayStore().propagate();
 
112
                MakeUIPlugin.getDefault().savePluginPreferences();
 
113
                return true;
 
114
        }
 
115
        
 
116
        protected OverlayPreferenceStore getOverlayStore() {
 
117
                return fOverlayStore;
 
118
        }
 
119
        
 
120
        protected OverlayPreferenceStore setOverlayStore() {
 
121
                return fOverlayStore;
 
122
        }
 
123
        
 
124
        protected Map<Control, String> getCheckBoxes() {
 
125
                return fCheckBoxes;
 
126
        }
 
127
        
 
128
        protected Map<Control, String> getTextFields() {
 
129
                return fTextFields;
 
130
        }
 
131
        
 
132
        protected Map<Text, String[]> getNumberFields() {
 
133
                return fNumberFields;
 
134
        }
 
135
        
 
136
        /* (non-Javadoc)
 
137
         * @see org.eclipse.jface.preference.PreferencePage#performDefaults()
 
138
         */
 
139
        @Override
 
140
        protected void performDefaults() {
 
141
                getOverlayStore().loadDefaults();
 
142
                initializeFields();
 
143
                handleDefaults();
 
144
                super.performDefaults();
 
145
        }
 
146
        
 
147
        protected abstract void handleDefaults();
 
148
        
 
149
        /* (non-Javadoc)
 
150
         * @see org.eclipse.jface.dialogs.IDialogPage#dispose()
 
151
         */
 
152
        @Override
 
153
        public void dispose() {
 
154
                if (getOverlayStore() != null) {
 
155
                        getOverlayStore().stop();
 
156
                        fOverlayStore= null;
 
157
                }
 
158
                super.dispose();
 
159
        }
 
160
        
 
161
        protected Button addCheckBox(Composite parent, String labelText, String key, int indentation) {         
 
162
                Button checkBox= new Button(parent, SWT.CHECK);
 
163
                checkBox.setText(labelText);
 
164
                checkBox.setFont(parent.getFont());
 
165
                
 
166
                GridData gd= new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
 
167
                gd.horizontalIndent= indentation;
 
168
                gd.horizontalSpan= 2;
 
169
                checkBox.setLayoutData(gd);
 
170
                checkBox.addSelectionListener(fCheckBoxListener);
 
171
                
 
172
                getCheckBoxes().put(checkBox, key);
 
173
                
 
174
                return checkBox;
 
175
        }
 
176
        
 
177
        protected Control addTextField(Composite composite, String labelText, String key, int textLimit, int indentation, String[] errorMessages) {
 
178
                Font font= composite.getFont();
 
179
                
 
180
                Label label= new Label(composite, SWT.NONE);
 
181
                label.setText(labelText);
 
182
                label.setFont(font);
 
183
                GridData gd= new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
 
184
                gd.horizontalIndent= indentation;
 
185
                label.setLayoutData(gd);
 
186
                
 
187
                Text textControl= new Text(composite, SWT.BORDER | SWT.SINGLE);
 
188
                textControl.setFont(font);              
 
189
                gd= new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
 
190
                gd.widthHint= convertWidthInCharsToPixels(textLimit + 1);
 
191
                textControl.setLayoutData(gd);
 
192
                textControl.setTextLimit(textLimit);
 
193
                getTextFields().put(textControl, key);
 
194
                if (errorMessages != null) {
 
195
                        getNumberFields().put(textControl, errorMessages);
 
196
                        textControl.addModifyListener(fNumberFieldListener);
 
197
                } else {
 
198
                        textControl.addModifyListener(fTextFieldListener);
 
199
                }
 
200
                        
 
201
                return textControl;
 
202
        }
 
203
        
 
204
        void numberFieldChanged(Text textControl) {
 
205
                String number= textControl.getText();
 
206
                IStatus status= validatePositiveNumber(number, getNumberFields().get(textControl));
 
207
                if (!status.matches(IStatus.ERROR)) {
 
208
                        getOverlayStore().setValue(getTextFields().get(textControl), number);
 
209
                }
 
210
                updateStatus(status);
 
211
        }
 
212
        
 
213
        private IStatus validatePositiveNumber(String number, String[] errorMessages) {
 
214
                StatusInfo status= new StatusInfo();
 
215
                if (number.length() == 0) {
 
216
                        status.setError(errorMessages[0]);
 
217
                } else {
 
218
                        try {
 
219
                                int value= Integer.parseInt(number);
 
220
                                if (value < 0)
 
221
                                        status.setError(MessageFormat.format(errorMessages[1], new String[]{number}));
 
222
                        } catch (NumberFormatException e) {
 
223
                                status.setError(MessageFormat.format(errorMessages[1], new String[]{number}));
 
224
                        }
 
225
                }
 
226
                return status;
 
227
        }
 
228
        
 
229
        private void updateStatus(IStatus status) {
 
230
                if (!status.matches(IStatus.ERROR)) {
 
231
                        Set<Text> keys= getNumberFields().keySet();
 
232
                        for (Iterator<Text> iter = keys.iterator(); iter.hasNext();) {
 
233
                                Text text = iter.next();
 
234
                                IStatus s= validatePositiveNumber(text.getText(), getNumberFields().get(text));
 
235
                                status= s.getSeverity() > status.getSeverity() ? s : status;
 
236
                        }
 
237
                }       
 
238
                setValid(!status.matches(IStatus.ERROR));
 
239
                applyToStatusLine(this, status);
 
240
        }
 
241
 
 
242
        /*
 
243
         * Applies the status to the status line of a dialog page.
 
244
         */
 
245
        private void applyToStatusLine(DialogPage page, IStatus status) {
 
246
                String message= status.getMessage();
 
247
                switch (status.getSeverity()) {
 
248
                        case IStatus.OK:
 
249
                                page.setMessage(message, IMessageProvider.NONE);
 
250
                                page.setErrorMessage(null);
 
251
                                break;
 
252
                        case IStatus.WARNING:
 
253
                                page.setMessage(message, IMessageProvider.WARNING);
 
254
                                page.setErrorMessage(null);
 
255
                                break;                          
 
256
                        case IStatus.INFO:
 
257
                                page.setMessage(message, IMessageProvider.INFORMATION);
 
258
                                page.setErrorMessage(null);
 
259
                                break;                  
 
260
                        default:
 
261
                                if (message.length() == 0) {
 
262
                                        message= null;
 
263
                                }
 
264
                                page.setMessage(null);
 
265
                                page.setErrorMessage(message);
 
266
                                break;          
 
267
                }
 
268
        }
 
269
        
 
270
        /**
 
271
         * Returns an array of size 2:
 
272
         *  - first element is of type <code>Label</code>
 
273
         *  - second element is of type <code>Text</code>
 
274
         * Use <code>getLabelControl</code> and <code>getTextControl</code> to get the 2 controls.
 
275
         */
 
276
        protected Control[] addLabelledTextField(Composite composite, String label, String key, int textLimit, int indentation, String[] errorMessages) {
 
277
                Label labelControl= new Label(composite, SWT.NONE);
 
278
                labelControl.setText(label);
 
279
                labelControl.setFont(composite.getFont());
 
280
                GridData gd= new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
 
281
                gd.horizontalIndent= indentation;
 
282
                labelControl.setLayoutData(gd);
 
283
        
 
284
                Text textControl= new Text(composite, SWT.BORDER | SWT.SINGLE);         
 
285
                gd= new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
 
286
                gd.widthHint= convertWidthInCharsToPixels(textLimit + 1);
 
287
                textControl.setLayoutData(gd);
 
288
                textControl.setTextLimit(textLimit);
 
289
                textControl.setFont(composite.getFont());
 
290
                fTextFields.put(textControl, key);
 
291
                if (errorMessages != null) {
 
292
                        fNumberFields.put(textControl, errorMessages);
 
293
                        textControl.addModifyListener(fNumberFieldListener);
 
294
                } else {
 
295
                        textControl.addModifyListener(fTextFieldListener);
 
296
                }
 
297
                
 
298
                return new Control[]{labelControl, textControl};
 
299
        }
 
300
        
 
301
        protected String loadPreviewContentFromFile(String filename) {
 
302
                String line;
 
303
                String separator= System.getProperty("line.separator", "\n"); //$NON-NLS-1$ //$NON-NLS-2$
 
304
                StringBuffer buffer= new StringBuffer(512);
 
305
                BufferedReader reader= null;
 
306
                try {
 
307
                        reader= new BufferedReader(new InputStreamReader(getClass().getResourceAsStream(filename)));
 
308
                        while ((line= reader.readLine()) != null) {
 
309
                                buffer.append(line);
 
310
                                buffer.append(separator);
 
311
                        }
 
312
                } catch (IOException io) {
 
313
                        MakeUIPlugin.log(io);
 
314
                } finally {
 
315
                        if (reader != null) {
 
316
                                try { reader.close(); } catch (IOException e) {}
 
317
                        }
 
318
                }
 
319
                return buffer.toString();
 
320
        }
 
321
}