aboutsummaryrefslogtreecommitdiff
path: root/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/editors/export/AbstractPropertiesFieldsPart.java
blob: bc3051e88ecf3c796bf49360e546fb6599c0bd49 (plain)
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Eclipse Public License, Version 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.eclipse.org/org/documents/epl-v10.php
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.ide.eclipse.adt.internal.editors.export;

import com.android.SdkConstants;
import com.android.ide.eclipse.adt.AdtPlugin;
import com.android.ide.eclipse.adt.internal.editors.ui.SectionHelper.ManifestSectionPart;

import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.DocumentEvent;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IRegion;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.Section;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;

/**
 * Section part for editing fields of a properties file in an Export editor.
 * <p/>
 * This base class is intended to be derived and customized.
 */
abstract class AbstractPropertiesFieldsPart extends ManifestSectionPart {

    private final HashMap<String, Control> mNameToField = new HashMap<String, Control>();

    private ExportEditor mEditor;

    private boolean mInternalTextUpdate = false;

    public AbstractPropertiesFieldsPart(Composite body, FormToolkit toolkit, ExportEditor editor) {
        super(body, toolkit, Section.TWISTIE | Section.EXPANDED, true /* description */);
        mEditor = editor;
    }

    protected HashMap<String, Control> getNameToField() {
        return mNameToField;
    }

    protected ExportEditor getEditor() {
        return mEditor;
    }

    protected void setInternalTextUpdate(boolean internalTextUpdate) {
        mInternalTextUpdate = internalTextUpdate;
    }

    protected boolean isInternalTextUpdate() {
        return mInternalTextUpdate;
    }

    /**
     * Adds a modify listener to every text field that will mark the part as dirty.
     *
     * CONTRACT: Derived classes MUST call this at the end of their constructor.
     *
     * @see #setFieldModifyListener(Control, ModifyListener)
     */
    protected void addModifyListenerToFields() {
        ModifyListener markDirtyListener = new ModifyListener() {
            @Override
            public void modifyText(ModifyEvent e) {
                // Mark the part as dirty if a field has been changed.
                // This will force a commit() operation to store the data in the model.
                if (!mInternalTextUpdate) {
                    markDirty();
                }
            }
        };

        for (Control field : mNameToField.values()) {
            setFieldModifyListener(field, markDirtyListener);
        }
    }

    /**
     * Sets a listener that will mark the part as dirty when the control is modified.
     * The base method only handles {@link Text} fields.
     *
     * CONTRACT: Derived classes CAN use this to add a listener to their own controls.
     * The listener must call {@link #markDirty()} when the control is modified by the user.
     *
     * @param field A control previously registered with {@link #getNameToField()}.
     * @param markDirtyListener A {@link ModifyListener} that invokes {@link #markDirty()}.
     *
     * @see #isInternalTextUpdate()
     */
    protected void setFieldModifyListener(Control field, ModifyListener markDirtyListener) {
        if (field instanceof Text) {
            ((Text) field).addModifyListener(markDirtyListener);
        }
    }

    /**
     * Updates the model based on the content of fields. This is invoked when a field
     * has marked the document as dirty.
     *
     * CONTRACT: Derived classes do not need to override this.
     */
    @Override
    public void commit(boolean onSave) {

        // We didn't store any information indicating which field was dirty (we could).
        // Since there are not many fields, just update all the document lines that
        // match our field keywords.

        if (isDirty()) {
            mEditor.wrapRewriteSession(new Runnable() {
                @Override
                public void run() {
                    saveFieldsToModel();
                }
            });
        }

        super.commit(onSave);
    }

    private void saveFieldsToModel() {
        // Get a list of all keywords to process. Go thru the document, replacing in-place
        // the ones we can find and remove them from this set. This will leave the list
        // of new keywords to add at the end of the document.
        HashSet<String> allKeywords = new HashSet<String>(mNameToField.keySet());

        IDocument doc = mEditor.getDocument();
        int numLines = doc.getNumberOfLines();

        String delim = null;
        try {
            delim = numLines > 0 ? doc.getLineDelimiter(0) : null;
        } catch (BadLocationException e1) {
            // ignore
        }
        if (delim == null || delim.length() == 0) {
            delim = SdkConstants.CURRENT_PLATFORM == SdkConstants.PLATFORM_WINDOWS ?
                    "\r\n" : "\n"; //$NON-NLS-1$ //$NON-NLS-2#
        }

        for (int i = 0; i < numLines; i++) {
            try {
                IRegion info = doc.getLineInformation(i);
                String line = doc.get(info.getOffset(), info.getLength());
                line = line.trim();
                if (line.startsWith("#")) {  //$NON-NLS-1$
                    continue;
                }

                int pos = line.indexOf('=');
                if (pos > 0 && pos < line.length() - 1) {
                    String key = line.substring(0, pos).trim();

                    Control field = mNameToField.get(key);
                    if (field != null) {

                        // This is the new line to inject
                        line = key + "=" + getFieldText(field);

                        try {
                            // replace old line by new one. This doesn't change the
                            // line delimiter.
                            mInternalTextUpdate = true;
                            doc.replace(info.getOffset(), info.getLength(), line);
                            allKeywords.remove(key);
                        } finally {
                            mInternalTextUpdate = false;
                        }
                    }
                }

            } catch (BadLocationException e) {
                // TODO log it
                AdtPlugin.log(e, "Failed to replace in export.properties");
            }
        }

        for (String key : allKeywords) {
            Control field = mNameToField.get(key);
            if (field != null) {
                // This is the new line to inject
                String line = key + "=" + getFieldText(field);

                try {
                    // replace old line by new one
                    mInternalTextUpdate = true;

                    numLines = doc.getNumberOfLines();

                    IRegion info = numLines > 0 ? doc.getLineInformation(numLines - 1) : null;
                    if (info != null && info.getLength() == 0) {
                        // last line is empty. Insert right before there.
                        doc.replace(info.getOffset(), info.getLength(), line);
                    } else {
                        if (numLines > 0) {
                            String eofDelim = doc.getLineDelimiter(numLines - 1);
                            if (eofDelim == null || eofDelim.length() == 0) {
                                // The document doesn't end with a line delimiter, so add
                                // one to the line to be written.
                                line = delim + line;
                            }
                        }

                        int len = doc.getLength();
                        doc.replace(len, 0, line);
                    }

                    allKeywords.remove(key);
                } catch (BadLocationException e) {
                    // TODO log it
                    AdtPlugin.log(e, "Failed to append to export.properties: %s", line);
                } finally {
                    mInternalTextUpdate = false;
                }
            }
        }
    }

    /**
     * Used when committing fields values to the model to retrieve the text
     * associated with a field.
     * <p/>
     * The base method only handles {@link Text} controls.
     *
     * CONTRACT: Derived classes CAN use this to support their own controls.
     *
     * @param field A control previously registered with {@link #getNameToField()}.
     * @return A non-null string to write to the properties files.
     */
    protected String getFieldText(Control field) {
        if (field instanceof Text) {
            return ((Text) field).getText();
        }
        return "";
    }

    /**
     * Called after all pages have been created, to let the parts initialize their
     * content based on the document's model.
     * <p/>
     * The model should be acceded via the {@link ExportEditor}.
     *
     * @param editor The {@link ExportEditor} instance.
     */
    public void onModelInit(ExportEditor editor) {

        // Start with a set of all the possible keywords and remove those we
        // found in the document as we read the lines.
        HashSet<String> allKeywords = new HashSet<String>(mNameToField.keySet());

        // Parse the lines in the document for patterns "keyword=value",
        // trimming all whitespace and discarding lines that start with # (comments)
        // then affect to the internal fields as appropriate.
        IDocument doc = editor.getDocument();
        int numLines = doc.getNumberOfLines();
        for (int i = 0; i < numLines; i++) {
            try {
                IRegion info = doc.getLineInformation(i);
                String line = doc.get(info.getOffset(), info.getLength());
                line = line.trim();
                if (line.startsWith("#")) {  //$NON-NLS-1$
                    continue;
                }

                int pos = line.indexOf('=');
                if (pos > 0 && pos < line.length() - 1) {
                    String key = line.substring(0, pos).trim();

                    Control field = mNameToField.get(key);
                    if (field != null) {
                        String value = line.substring(pos + 1).trim();
                        try {
                            mInternalTextUpdate = true;
                            setFieldText(field, value);
                            allKeywords.remove(key);
                        } finally {
                            mInternalTextUpdate = false;
                        }
                    }
                }

            } catch (BadLocationException e) {
                // TODO log it
                AdtPlugin.log(e, "Failed to set field to export.properties value");
            }
        }

        // Clear the text of any keyword we didn't find in the document
        Iterator<String> iterator = allKeywords.iterator();
        while (iterator.hasNext()) {
            String key = iterator.next();
            Control field = mNameToField.get(key);
            if (field != null) {
                try {
                    mInternalTextUpdate = true;
                    setFieldText(field, "");
                    iterator.remove();
                } finally {
                    mInternalTextUpdate = false;
                }
            }
        }
    }

    /**
     * Used when reading the model to set the field values.
     * <p/>
     * The base method only handles {@link Text} controls.
     *
     * CONTRACT: Derived classes CAN use this to support their own controls.
     *
     * @param field A control previously registered with {@link #getNameToField()}.
     * @param value A non-null string to that was read from the properties files.
     *              The value is an empty string if the property line is missing.
     */
    protected void setFieldText(Control field, String value) {
        if (field instanceof Text) {
            ((Text) field).setText(value);
        }
    }

    /**
     * Called after the document model has been changed. The model should be acceded via
     * the {@link ExportEditor} (e.g. getDocument, wrapRewriteSession)
     *
     * @param editor The {@link ExportEditor} instance.
     * @param event Specification of changes applied to document.
     */
    public void onModelChanged(ExportEditor editor, DocumentEvent event) {
        // To simplify and since we don't have many fields, just reload all the values.
        // A better way would to be to look at DocumentEvent which gives us the offset/length
        // and text that has changed.
        if (!mInternalTextUpdate) {
            onModelInit(editor);
        }
    }
}