File: options.js

package info (click to toggle)
libnb-platform18-java 12.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 729,800 kB
  • sloc: java: 5,059,097; xml: 574,432; php: 78,788; javascript: 29,039; ansic: 10,278; sh: 6,386; cpp: 4,612; jsp: 3,643; sql: 1,097; makefile: 540; objc: 288; perl: 277; haskell: 93
file content (457 lines) | stat: -rw-r--r-- 16,637 bytes parent folder | download | duplicates (3)
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
 *
 * 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.
 */

// references from bg page
var INFOBAR = chrome.extension.getBackgroundPage().NetBeans.INFOBAR;
var NetBeans_Presets = chrome.extension.getBackgroundPage().NetBeans_Presets;
var NetBeans_Preset = chrome.extension.getBackgroundPage().NetBeans_Preset;
var NetBeans_Warnings = chrome.extension.getBackgroundPage().NetBeans_Warnings;

/**
 * Preset customizer.
 */
var NetBeans_PresetCustomizer = {};
// customizer container
NetBeans_PresetCustomizer._container = null;
// help button
NetBeans_PresetCustomizer._moreHelpButton = null;
// presets container
NetBeans_PresetCustomizer._rowContainer = null;
// add button
NetBeans_PresetCustomizer._addPresetButton = null;
// remove button
NetBeans_PresetCustomizer._removePresetButton = null;
// move up button
NetBeans_PresetCustomizer._moveUpPresetButton = null;
// move down button
NetBeans_PresetCustomizer._moveDownPresetButton = null;
// OK button
NetBeans_PresetCustomizer._okButton = null;
// reset warnings button
NetBeans_PresetCustomizer._resetWarningsButton = null;
// presets
NetBeans_PresetCustomizer._presets = null;
// active/selected preset
NetBeans_PresetCustomizer._activePreset = null;
// show customizer
NetBeans_PresetCustomizer.show = function(presets) {
    this._init();
    this._presets = presets;
    this._putPresets(this._presets);
};
/*** ~Private ***/
// customizer init
NetBeans_PresetCustomizer._init = function() {
    if (this._rowContainer !== null) {
        return;
    }
    if (!INFOBAR) {
        document.getElementById('toolbarHeader').style.display = 'none';
    }
    this._moreHelpButton = document.getElementById('moreHelpButton');
    this._rowContainer = document.getElementById('presetCustomizerTable').getElementsByTagName('tbody')[0];
    this._addPresetButton = document.getElementById('addPreset');
    this._removePresetButton = document.getElementById('removePreset');
    this._moveUpPresetButton = document.getElementById('moveUpPreset');
    this._moveDownPresetButton = document.getElementById('moveDownPreset');
    this._okButton = document.getElementById('presetCustomizerOk');
    this._resetWarningsButton = document.getElementById('resetWarnings');
    this._registerEvents();
};
// hide customizer
NetBeans_PresetCustomizer._hide = function() {
    window.close();
};
// register events
NetBeans_PresetCustomizer._registerEvents = function() {
    var that = this;
    this._moreHelpButton.addEventListener('click', function() {
        that._switchHelp();
    }, false);
    this._addPresetButton.addEventListener('click', function() {
        that._addPreset();
    }, false);
    this._removePresetButton.addEventListener('click', function() {
        that._removePreset();
    }, false);
    this._moveUpPresetButton.addEventListener('click', function() {
        that._moveUpPreset();
    }, false);
    this._moveDownPresetButton.addEventListener('click', function() {
        that._moveDownPreset();
    }, false);
    this._okButton.addEventListener('click', function() {
        that._save();
    }, false);
    document.getElementById('presetCustomizerCancel').addEventListener('click', function() {
        that._cancel();
    }, false);
    this._resetWarningsButton.addEventListener('click', function() {
        that._resetWarnings();
    }, false);
};
// put presets to the customizer?
NetBeans_PresetCustomizer._putPresets = function(presets) {
    if (this._presets === null) {
        this._putNoPresets();
        this._enableButtons();
    } else {
        this._putPresetsInternal(presets);
    }
};
// no presets available (netbeans not running)
NetBeans_PresetCustomizer._putNoPresets = function() {
    var row = document.createElement('tr');
    var info = document.createElement('td');
    info.setAttribute('colspan', '5');
    info.setAttribute('class', 'info');
    info.appendChild(document.createTextNode(I18n.message('_WindowSettingsNotAvailable')));
    row.appendChild(info);
    this._rowContainer.appendChild(row);
};
// put presets to the table
NetBeans_PresetCustomizer._putPresetsInternal = function(presets) {
    var that = this;
    var allPresetTypes = NetBeans_Preset.allTypes();
    for (p in presets) {
        var preset = presets[p];
        // row
        var row = document.createElement('tr');
        row.addEventListener('click', function() {
            that._rowSelected(this);
        }, true);
        // type
        var type = document.createElement('td');
        var typeSelect = document.createElement('select');
        for (i in allPresetTypes) {
            var presetType = allPresetTypes[i];
            var option = document.createElement('option');
            option.setAttribute('value', presetType.ident);
            if (preset.type === presetType.ident) {
                option.setAttribute('selected', 'selected');
            }
            option.appendChild(document.createTextNode(presetType.title));
            typeSelect.appendChild(option);
        }
        typeSelect.addEventListener('change', function() {
            that._typeChanged(this);
        }, false);
        type.appendChild(typeSelect);
        row.appendChild(type);
        // name
        var title = document.createElement('td');
        var titleInput = document.createElement('input');
        titleInput.setAttribute('value', preset.displayName);
        titleInput.addEventListener('keyup', function() {
            that._titleChanged(this);
        }, false);
        title.appendChild(titleInput);
        row.appendChild(title);
        // width
        var witdh = document.createElement('td');
        var widthInput = document.createElement('input');
        widthInput.setAttribute('value', preset.width);
        widthInput.className = 'number';
        widthInput.addEventListener('keyup', function() {
            that._widthChanged(this);
        }, false);
        witdh.appendChild(widthInput);
        row.appendChild(witdh);
        // height
        var height = document.createElement('td');
        var heightInput = document.createElement('input');
        heightInput.setAttribute('value', preset.height);
        heightInput.className = 'number';
        heightInput.addEventListener('keyup', function() {
            that._heightChanged(this);
        }, false);
        height.appendChild(heightInput);
        row.appendChild(height);
        // toolbar
        if (INFOBAR) {
            var toolbar = document.createElement('td');
            toolbar.setAttribute('class', 'toolbar');
            var toolbarCheckbox = document.createElement('input');
            toolbarCheckbox.setAttribute('type', 'checkbox');
            if (preset.showInToolbar) {
                toolbarCheckbox.setAttribute('checked', 'checked');
            }
            toolbarCheckbox.addEventListener('click', function() {
                that._toolbarChanged(this);
            }, false);
            toolbar.appendChild(toolbarCheckbox);
            row.appendChild(toolbar);
        }
        // append row
        this._rowContainer.appendChild(row);
        preset['_row'] = row;
        preset['_errors'] = [];
    }
};
// cleanup (remove presets from customizer)
NetBeans_PresetCustomizer._cleanUp = function() {
    this._presets = null;
    while (this._rowContainer.hasChildNodes()) {
        this._rowContainer.removeChild(this._rowContainer.firstChild);
    }
    this._activePreset = null;
    this._enableButtons();
};
// add a new preset
NetBeans_PresetCustomizer._addPreset = function() {
    var preset = new NetBeans_Preset(NetBeans_Preset.DESKTOP.ident, I18n.message('_New_hellip'), '800', '600', true, false);
    this._presets.push(preset);
    this._putPresets([preset]);
    this._enableButtons();
};
// remove the active preset
NetBeans_PresetCustomizer._removePreset = function() {
    // presets
    this._presets.splice(this._presets.indexOf(this._activePreset), 1);
    // ui
    this._rowContainer.removeChild(this._activePreset['_row']);
    this._activePreset = null;
    this._enableButtons();
};
// move the active preset up
NetBeans_PresetCustomizer._moveUpPreset = function() {
    // presets
    this._movePreset(this._activePreset, -1);
    // ui
    var row = this._activePreset['_row'];
    var before = row.previousSibling;
    this._rowContainer.removeChild(row);
    this._rowContainer.insertBefore(row, before);
    this._enableButtons();
};
// move the active preset down
NetBeans_PresetCustomizer._moveDownPreset = function() {
    // presets
    this._movePreset(this._activePreset, +1);
    // ui
    var row = this._activePreset['_row'];
    var after = row.nextSibling;
    this._rowContainer.removeChild(row);
    nbInsertAfter(row, after);
    this._enableButtons();
};
// move the preset up or down
NetBeans_PresetCustomizer._movePreset = function(preset, shift) {
    var index = this._presets.indexOf(preset);
    var tmp = this._presets[index];
    this._presets[index] = this._presets[index + shift];
    this._presets[index + shift] = tmp;
};
// save presets to the central storage and redraw them
NetBeans_PresetCustomizer._save = function() {
    for (i in this._presets) {
        var preset = this._presets[i];
        delete preset['_row'];
        delete preset['_errors'];
    }
    NetBeans_Presets.setPresets(this._presets);
    this._cleanUp();
    this._hide();
};
// cancel customizer
NetBeans_PresetCustomizer._cancel = function() {
    this._cleanUp();
    this._hide();
};
// switch help
NetBeans_PresetCustomizer._switchHelp = function() {
    var help = document.getElementById('help');
    var displayed = help.style.display == 'block';
    help.style.display = displayed ? 'none' : 'block';
    this._moreHelpButton.innerHTML = I18n.message(displayed ? '_More_hellip' : '_Less_hellip');
};
// reset warnings
NetBeans_PresetCustomizer._resetWarnings = function() {
    NetBeans_Warnings.reset();
    this._resetWarningsButton.innerHTML = I18n.message('_Done');
    this._resetWarningsButton.setAttribute('disabled', 'disabled');
};
// callback when row is selected
NetBeans_PresetCustomizer._rowSelected = function(row) {
    if (this._activePreset !== null) {
        if (this._activePreset['_row'] === row) {
            // repeated click => ignore
            return;
        }
        this._activePreset['_row'].className = '';
    }
    // select
    var that = this;
    for (var i in this._presets) {
        var preset = this._presets[i];
        if (preset['_row'] === row) {
            that._activePreset = preset;
            that._activePreset['_row'].className = 'active';
        }
    }
    this._enableButtons();
};
// enable/disable action buttons (based on the active preset)
NetBeans_PresetCustomizer._enableButtons = function() {
    this._enablePresetButtons();
    this._enableMainButtons();
};
// enable/disable preset buttons (based on the active preset)
NetBeans_PresetCustomizer._enablePresetButtons = function() {
    if (this._activePreset !== null) {
        // any preset selected
        if (this._activePreset.isDefault) {
            this._removePresetButton.setAttribute('disabled', 'disabled');
        } else {
            this._removePresetButton.removeAttribute('disabled');
        }
        if (this._activePreset['_row'] !== this._rowContainer.firstChild) {
            this._moveUpPresetButton.removeAttribute('disabled');
        } else {
            this._moveUpPresetButton.setAttribute('disabled', 'disabled');
        }
        if (this._activePreset['_row'] !== this._rowContainer.lastChild) {
            this._moveDownPresetButton.removeAttribute('disabled');
        } else {
            this._moveDownPresetButton.setAttribute('disabled', 'disabled');
        }
    } else {
        if (this._presets === null) {
            // nb not running
            this._addPresetButton.setAttribute('disabled', 'disabled');
        }
        this._removePresetButton.setAttribute('disabled', 'disabled');
        this._moveUpPresetButton.setAttribute('disabled', 'disabled');
        this._moveDownPresetButton.setAttribute('disabled', 'disabled');
    }
};
// enable/disable customizer buttons
NetBeans_PresetCustomizer._enableMainButtons = function() {
    var anyError = false;
    if (this._presets === null) {
        anyError = true;
    } else {
        for (i in this._presets) {
            if (this._presets[i]['_errors'].length) {
                anyError = true;
                break;
            }
        }
    }
    if (anyError) {
        this._okButton.setAttribute('disabled', 'disabled');
    } else {
        this._okButton.removeAttribute('disabled');
    }
};
// callback when preset type changes
NetBeans_PresetCustomizer._typeChanged = function(input) {
    if (this._activePreset === null) {
        // select change event fired before row click event => select the closest row
        var row = input;
        while (true) {
            row = row.parentNode;
            if (row.tagName.toLowerCase() === 'tr') {
                break;
            }
        }
        this._rowSelected(row);
    }
    this._activePreset.type = NetBeans_Preset.typeForIdent(input.value).ident;
};
// callback when preset title changes
NetBeans_PresetCustomizer._titleChanged = function(input) {
    var that = this;
    this._checkField(input, 'displayName', function(value) {
        return that._validateNotEmpty(value);
    });
};
// callback when preset width changes
NetBeans_PresetCustomizer._widthChanged = function(input) {
    var that = this;
    this._checkField(input, 'width', function(value) {
        return that._validateNumber(value);
    });
};
// callback when preset height changes
NetBeans_PresetCustomizer._heightChanged = function(input) {
    var that = this;
    this._checkField(input, 'height', function(value) {
        return that._validateNumber(value);
    });
};
// callback when preset toolbar changes
NetBeans_PresetCustomizer._toolbarChanged = function(input) {
    this._activePreset.showInToolbar = input.checked;
};
// check whether the value is not empty
NetBeans_PresetCustomizer._validateNotEmpty = function(value) {
    return value != null && value.trim().length > 0;
};
// check whether the value is a number
NetBeans_PresetCustomizer._validateNumber = function(value) {
    return value != null && value.search(/^[1-9][0-9]*$/) != -1;
};
// check the given input, for the given key with the given validation callback
NetBeans_PresetCustomizer._checkField = function(input, key, validation) {
    var value = input.value;
    var index = this._activePreset['_errors'].indexOf(key);
    if (validation(value)) {
        nbRemoveCssClass(input, 'error');
        if (index !== -1) {
            this._activePreset['_errors'].splice(index, 1);
        }
    } else {
        nbAddCssClass(input, 'error');
        if (index === -1) {
            this._activePreset['_errors'].push(key);
        }
    }
    this._activePreset[key] = value;
    this._enableMainButtons();
};

/*** ~Helpers ***/
// mirror function to element.insertBefore()
function nbInsertAfter(newElement, targetElement) {
	var parent = targetElement.parentNode;
	if (parent.lastchild === targetElement) {
		parent.appendChild(newElement);
    } else {
		parent.insertBefore(newElement, targetElement.nextSibling);
    }
}
// add CSS class to the given element
function nbAddCssClass(element, cssClass) {
    var className = element.className;
    if (className.indexOf(cssClass) !== -1) {
        // already has this class
        return;
    }
    element.className = (element.className.trim() + ' ' + cssClass);
}
// remove CSS class to the given element
function nbRemoveCssClass(element, cssClass) {
    element.className = element.className.replace(cssClass, '').trim();
}

// run!
window.addEventListener('load', function() {
    NetBeans_PresetCustomizer.show(NetBeans_Presets.getPresets(true));
}, false);