aboutsummaryrefslogtreecommitdiffstats
path: root/netx/net/sourceforge/jnlp/controlpanel/DocumentAdapter.java
blob: 2bcb0c21d7506d0865a9e304a7f9dab5ea1f7ef4 (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
/* DocumentAdapter.java -- Updates properties.
Copyright (C) 2010 Red Hat

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */
package net.sourceforge.jnlp.controlpanel;

import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;

import net.sourceforge.jnlp.config.DeploymentConfiguration;

/**
 * Updates the property as it happens.
 * 
 * @author Andrew Su (asu@redhat.com, andrew.su@utoronto.ca)
 *
 */
public class DocumentAdapter implements DocumentListener {

    String[] fields;
    int index;
    String property;
    DeploymentConfiguration config;
    int mode;

    /**
     * This creates a new instance of DocumentAdapter.
     * 
     * @param fields The list of property.
     * @param index Location of property to modify.
     */
    public DocumentAdapter(String[] fields, int index) {
        this.fields = fields;
        this.index = index;
        mode = 1;
    }

    /**
     * This creates a new instance of DocumentAdapter. This allows modifying 
     * the configuration directly.
     * 
     * @param config ConfigurationFile containing the properties.
     * @param property Name of property to modify.
     */
    public DocumentAdapter(DeploymentConfiguration config, String property) {
        this.property = property;
        this.config = config;
        mode = 2;
    }

    @Override
    public void insertUpdate(DocumentEvent e) {
        update(e);
    }

    @Override
    public void removeUpdate(DocumentEvent e) {
        update(e);
    }

    @Override
    public void changedUpdate(DocumentEvent e) {

    }

    /**
     * Update the property as on the appropriate items.
     * 
     * @param e The event that caused the call.
     */
    private void update(DocumentEvent e) {
        Document d = e.getDocument();
        try {
            String value = d.getText(0, d.getLength()).trim();
            value = (value.length() == 0) ? null : value;
            if (mode == 1) {
                fields[index] = value;
            } else if (mode == 2) {
                config.setProperty(property, value);
            }
        } catch (BadLocationException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    }

}