~veger/ganttproject/manual-import

« back to all changes in this revision

Viewing changes to ganttproject/src/net/sourceforge/ganttproject/gui/options/OptionsPageBuilder.java

  • Committer: Maarten Bezemer
  • Date: 2012-01-22 12:20:00 UTC
  • Revision ID: maarten.bezemer@gmail.com-20120122122000-qwyec45rjx86wi7o
Updated till 2fe683a778c3 

Show diffs side-by-side

added added

removed removed

Lines of Context:
8
8
import java.awt.Color;
9
9
import java.awt.Component;
10
10
import java.awt.ComponentOrientation;
 
11
import java.awt.Dimension;
 
12
import java.awt.FlowLayout;
11
13
import java.awt.event.ActionEvent;
12
14
import java.awt.event.ActionListener;
13
15
import java.awt.event.FocusAdapter;
121
123
        return resultPanel;
122
124
    }
123
125
 
124
 
    public JComponent createLabeledComponent(GPOption option) {
 
126
    public JComponent createLabeledComponent(GPOption<?> option) {
125
127
        GPOptionGroup fake = new GPOptionGroup("", new GPOption[] {option});
126
128
        fake.setTitled(false);
127
129
        return createGroupComponent(fake);
138
140
        return result;
139
141
    }
140
142
 
141
 
    public JComponent createGroupComponent(GPOptionGroup group, GPOption... options) {
 
143
    public JComponent createGroupComponent(GPOptionGroup group, GPOption<?>... options) {
142
144
        JPanel optionsPanel = new JPanel(new SpringLayout());
143
145
        for (int i = 0; i < options.length; i++) {
144
 
            GPOption nextOption = options[i];
 
146
            GPOption<?> nextOption = options[i];
145
147
            final Component nextComponent = createOptionComponent(group, nextOption);
146
148
            if (needsLabel(group, nextOption)) {
147
149
                Component nextLabel =createOptionLabel(group, options[i]);
170
172
        return optionsPanel;
171
173
    }
172
174
 
173
 
    private boolean needsLabel(GPOptionGroup group, GPOption nextOption) {
 
175
    private boolean needsLabel(GPOptionGroup group, GPOption<?> nextOption) {
174
176
//        if (nextOption instanceof BooleanOption) {
175
177
//            return !isCheckboxOption(group, nextOption);
176
178
//        }
177
179
        return true;
178
180
    }
179
181
 
180
 
    public Component createStandaloneOptionPanel(GPOption option) {
 
182
    public Component createStandaloneOptionPanel(GPOption<?> option) {
181
183
        JPanel optionPanel = new JPanel(new BorderLayout());
182
184
        Component  optionComponent = createOptionComponent(null, option);
183
185
        if (needsLabel(null, option)) {
199
201
        result.add(placeholder, "placeholder");
200
202
        result.add(progressBar, "progressBar");
201
203
        controller.addChangeValueListener(new ChangeValueListener() {
 
204
            @Override
202
205
            public void changeValue(ChangeValueEvent event) {
203
206
                if (Boolean.TRUE.equals(event.getNewValue())) {
204
207
                    progressBar.setIndeterminate(true);
212
215
        });
213
216
        return result;
214
217
    }
215
 
    private Component createOptionLabel(GPOptionGroup group, GPOption option) {
 
218
    private Component createOptionLabel(GPOptionGroup group, GPOption<?> option) {
216
219
        JLabel nextLabel = new JLabel(myi18n.getOptionLabel(group, option));
217
220
        //nextLabel.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 5));
218
221
        return nextLabel;
219
222
    }
220
 
    public Component createOptionComponent(GPOptionGroup group, GPOption option) {
 
223
    public Component createOptionComponent(GPOptionGroup group, GPOption<?> option) {
221
224
        Component result = null;
222
225
        if (option instanceof EnumerationOption) {
223
226
            result = createEnumerationComponent((EnumerationOption) option, group);
238
241
        }
239
242
        else if (option instanceof IntegerOption) {
240
243
            result = createNumericComponent((IntegerOption)option, new NumericParser<Integer>() {
 
244
                @Override
241
245
                public Integer parse(String text) {
242
246
                    return Integer.valueOf(text);
243
247
                }
245
249
        }
246
250
        else if (option instanceof DoubleOption) {
247
251
            result = createNumericComponent((DoubleOption)option, new NumericParser<Double>() {
 
252
                @Override
248
253
                public Double parse(String text) {
249
254
                    return Double.valueOf(text);
250
255
                }
253
258
        if (result == null) {
254
259
            result = new JLabel("Unknown option class=" + option.getClass());
255
260
        }
 
261
        result.setEnabled(option.isWritable());
 
262
        final Component finalResult = result;
 
263
        option.addPropertyChangeListener(new PropertyChangeListener() {
 
264
            @Override
 
265
            public void propertyChange(PropertyChangeEvent evt) {
 
266
                if ("isWritable".equals(evt.getPropertyName())) {
 
267
                    assert evt.getNewValue() instanceof Boolean : "Unexpected value of property isWritable: " + evt.getNewValue();
 
268
                    finalResult.setEnabled((Boolean)evt.getNewValue());
 
269
                }
 
270
            }
 
271
        });
256
272
        return result;
257
273
    }
258
274
 
263
279
    private static void updateTextField(
264
280
            final JTextField textField, final DocumentListener listener, final ChangeValueEvent event) {
265
281
        SwingUtilities.invokeLater(new Runnable() {
 
282
            @Override
266
283
            public void run() {
267
284
                textField.getDocument().removeDocumentListener(listener);
268
285
                if (!textField.getText().equals(event.getNewValue())) {
275
292
 
276
293
    private Component createStringComponent(final StringOption option) {
277
294
        final JTextField result = new JTextField(option.getValue());
 
295
 
278
296
        final DocumentListener documentListener = new DocumentListener() {
 
297
            private void saveValue() {
 
298
                try {
 
299
                    option.setValue(result.getText());
 
300
                    result.setBackground(getValidFieldColor());
 
301
                }
 
302
                catch(ValidationException ex) {
 
303
                    result.setBackground(INVALID_FIELD_COLOR);
 
304
                }
 
305
            }
 
306
            @Override
279
307
            public void insertUpdate(DocumentEvent e) {
280
 
                option.setValue(result.getText());
 
308
                saveValue();
281
309
            }
282
310
 
 
311
            @Override
283
312
            public void removeUpdate(DocumentEvent e) {
284
 
                option.setValue(result.getText());
 
313
                saveValue();
285
314
            }
286
315
 
 
316
            @Override
287
317
            public void changedUpdate(DocumentEvent e) {
288
 
                option.setValue(result.getText());
 
318
                saveValue();
289
319
            }
290
320
        };
291
321
        result.getDocument().addDocumentListener(documentListener);
292
322
        option.addChangeValueListener(new ChangeValueListener() {
 
323
            @Override
293
324
            public void changeValue(final ChangeValueEvent event) {
294
325
                updateTextField(result, documentListener, event);
295
326
            }
299
330
 
300
331
    private Component createButtonComponent(GPOptionGroup optionGroup) {
301
332
        Action action = new AbstractAction(myi18n.getAdvancedActionTitle()) {
 
333
            @Override
302
334
            public void actionPerformed(ActionEvent e) {
303
335
                System.err.println("[OptionsPageBuilder] createButtonComponent: ");
304
336
            }
321
353
        return result;
322
354
    }
323
355
 
324
 
    private boolean isCheckboxOption(GPOptionGroup group, GPOption option) {
 
356
    private boolean isCheckboxOption(GPOptionGroup group, GPOption<?> option) {
325
357
        String yesKey = myi18n.getCanonicalOptionLabelKey(option)+".yes";
326
358
        if ((group==null || group.getI18Nkey(yesKey)==null) && myi18n.getValue(yesKey)==null) {
327
359
            return true;
335
367
 
336
368
    private Component createRadioButtonBooleanComponent(GPOptionGroup group, final BooleanOption option) {
337
369
        final JRadioButton yesButton = new JRadioButton(new AbstractAction("") {
 
370
            @Override
338
371
            public void actionPerformed(ActionEvent e) {
339
372
                if (!option.isChecked()) {
340
373
                    option.toggle();
347
380
        yesButton.setSelected(option.isChecked());
348
381
 
349
382
        final JRadioButton noButton = new JRadioButton(new AbstractAction("") {
 
383
            @Override
350
384
            public void actionPerformed(ActionEvent e) {
351
385
                if (option.isChecked()) {
352
386
                    option.toggle();
384
418
        final EnumerationOptionComboBoxModel model = new EnumerationOptionComboBoxModel(option, group);
385
419
        final JComboBox result = new JComboBox(model);
386
420
        option.addChangeValueListener(new ChangeValueListener() {
 
421
            @Override
387
422
            public void changeValue(ChangeValueEvent event) {
388
423
                model.onValueChange();
389
424
                result.setSelectedItem(model.getSelectedItem());
394
429
 
395
430
    private Component createColorComponent(final ColorOption option) {
396
431
        final JButton colorButton = new JButton();
 
432
        final JPanel label = new JPanel();
 
433
        label.setPreferredSize(new Dimension(8, 8));
 
434
        label.setBackground(option.getValue());
397
435
        Action action = new AbstractAction(myi18n.getColorButtonText(option)) {
 
436
            @Override
398
437
            public void actionPerformed(ActionEvent e) {
399
438
                ActionListener onOkPressing = new ActionListener() {
 
439
                    @Override
400
440
                    public void actionPerformed(ActionEvent e) {
401
441
                        Color color = ourColorChooser.getColor();
402
 
                        colorButton.setBackground(color);
 
442
                        label.setBackground(color);
403
443
                        option.setValue(color);
404
444
                    }
405
445
                };
406
446
                ActionListener onCancelPressing = new ActionListener() {
 
447
                    @Override
407
448
                    public void actionPerformed(ActionEvent e) {
408
449
                        // nothing to do for "Cancel"
409
450
                    }
416
457
            };
417
458
        };
418
459
        colorButton.setAction(action);
419
 
        colorButton.setBackground(option.getValue());
420
 
        return colorButton;
 
460
 
 
461
        JPanel result = new JPanel(new FlowLayout());
 
462
        result.add(label);
 
463
        result.add(colorButton);
 
464
        return result;
421
465
    }
422
466
 
423
467
    public JComponent createDateComponent(final DateOption option) {
424
468
        final JXDatePicker result = new JXDatePicker();
 
469
        result.setLocale(GanttLanguage.getInstance().getDateFormatLocale());
425
470
        result.setDate(option.getValue());
426
471
        class OptionValueUpdater implements ActionListener, PropertyChangeListener {
 
472
            @Override
427
473
            public void actionPerformed(ActionEvent e) {
428
474
                option.setValue(((JXDatePicker)e.getSource()).getDate());
429
475
            }
 
476
            @Override
430
477
            public void propertyChange(PropertyChangeEvent evt) {
431
478
                if (evt.getNewValue() instanceof Date
432
479
                        && !evt.getNewValue().equals(option.getValue())) {
440
487
 
441
488
        if (option instanceof ChangeValueDispatcher) {
442
489
            ((ChangeValueDispatcher)option).addChangeValueListener(new ChangeValueListener() {
 
490
                @Override
443
491
                public void changeValue(ChangeValueEvent event) {
444
492
                    assert event.getNewValue() instanceof Date : "value=" + event.getNewValue();
445
493
                    result.setDate((Date) event.getNewValue());
477
525
                    result.setBackground(INVALID_FIELD_COLOR);
478
526
                }
479
527
            }
 
528
            @Override
480
529
            public void insertUpdate(DocumentEvent e) {
481
530
                saveValue();
482
531
            }
483
532
 
 
533
            @Override
484
534
            public void removeUpdate(DocumentEvent e) {
485
535
                saveValue();
486
536
            }
487
537
 
 
538
            @Override
488
539
            public void changedUpdate(DocumentEvent e) {
489
540
                saveValue();
490
541
            }
491
542
        };
492
543
        result.getDocument().addDocumentListener(listener);
493
544
        option.addChangeValueListener(new ChangeValueListener() {
 
545
            @Override
494
546
            public void changeValue(final ChangeValueEvent event) {
495
547
                updateTextField(result, listener, event);
496
548
            }
530
582
            return getValue(group, canonicalKey);
531
583
        }
532
584
 
533
 
        public String getOptionLabel(GPOptionGroup group, GPOption option) {
 
585
        public String getOptionLabel(GPOptionGroup group, GPOption<?> option) {
534
586
            String canonicalKey = getCanonicalOptionLabelKey(option);
535
587
            return getValue(group, canonicalKey);
536
588
        }
548
600
        public final String getCanonicalOptionGroupLabelKey(GPOptionGroup group) {
549
601
            return myOptionGroupKeyPrefix + group.getID() + ".label";
550
602
        }
551
 
        public final String getCanonicalOptionLabelKey(GPOption option) {
 
603
        public final String getCanonicalOptionLabelKey(GPOption<?> option) {
552
604
            return myOptionKeyPrefix + option.getID() + ".label";
553
605
        }
554
606
        public static final String getCanonicalOptionValueLabelKey(String valueID) {