OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 93 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 93 Rev 180
1
/*
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 * 
3
 * 
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
5
 * 
5
 * 
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
7
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
7
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
8
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
8
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
9
 * language governing permissions and limitations under the License.
9
 * language governing permissions and limitations under the License.
10
 * 
10
 * 
11
 * When distributing the software, include this License Header Notice in each file.
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
12
 */
13
 
13
 
14
 package org.openconcerto.ui.component;
14
 package org.openconcerto.ui.component;
15
 
15
 
16
import java.awt.Component;
16
import java.awt.Component;
17
import java.awt.TextComponent;
17
import java.awt.TextComponent;
18
 
18
 
19
import javax.swing.text.JTextComponent;
19
import javax.swing.text.JTextComponent;
20
 
20
 
21
public enum InteractionMode {
21
public enum InteractionMode {
22
 
22
 
23
    /** All interactions are disabled */
23
    /** All interactions are disabled */
24
    DISABLED(false, false),
24
    DISABLED(false, false),
25
    /** Content can be read, only read-only interaction enabled */
25
    /** Content can be read, only read-only interaction enabled */
26
    READ_ONLY(true, false),
26
    READ_ONLY(true, false),
27
    /** Content can be modified, all interactions enabled */
27
    /** Content can be modified, all interactions enabled */
28
    READ_WRITE(true, true);
28
    READ_WRITE(true, true);
29
 
29
 
30
    private final boolean enabled, editable;
30
    private final boolean enabled, editable;
31
 
31
 
32
    private InteractionMode(final boolean enabled, final boolean editable) {
32
    private InteractionMode(final boolean enabled, final boolean editable) {
33
        this.enabled = enabled;
33
        this.enabled = enabled;
34
        this.editable = editable;
34
        this.editable = editable;
35
    }
35
    }
36
 
36
 
37
    /**
37
    /**
38
     * Whether some interactions are allowed.
38
     * Whether some interactions are allowed.
39
     * 
39
     * 
40
     * @return <code>true</code> if at least some interactions are allowed.
40
     * @return <code>true</code> if at least some interactions are allowed.
41
     */
41
     */
42
    public final boolean isEnabled() {
42
    public final boolean isEnabled() {
43
        return this.enabled;
43
        return this.enabled;
44
    }
44
    }
45
 
45
 
46
    /**
46
    /**
47
     * Whether the content can be modified.
47
     * Whether the content can be modified.
48
     * 
48
     * 
49
     * @return <code>true</code> if the content can be modified.
49
     * @return <code>true</code> if the content can be modified.
50
     */
50
     */
51
    public final boolean isEditable() {
51
    public final boolean isEditable() {
52
        return this.editable;
52
        return this.editable;
53
    }
53
    }
54
 
54
 
55
    /**
55
    /**
56
     * Try to apply this mode to the passed component. This method has special cases for known
56
     * Try to apply this mode to the passed component. This method has special cases for known
57
     * component (.e.g {@link JTextComponent}) otherwise generic components are presumed to not have
57
     * component (.e.g {@link JTextComponent}) otherwise generic components are presumed to not have
58
     * a {@link #READ_ONLY} mode. For complete control, implement {@link InteractionComponent}.
58
     * a {@link #READ_ONLY} mode. For complete control, implement {@link InteractionComponent}.
59
     * 
59
     * 
60
     * @param comp a component.
60
     * @param comp a component.
61
     * @return the passed component.
61
     * @return the passed component.
62
     * @see #from(Component)
62
     * @see #from(Component)
63
     */
63
     */
64
    public Component applyTo(final Component comp) {
64
    public Component applyTo(final Component comp) {
65
        if (comp instanceof InteractionComponent) {
65
        if (comp instanceof InteractionComponent) {
66
            ((InteractionComponent) comp).setInteractionMode(this);
66
            ((InteractionComponent) comp).setInteractionMode(this);
67
        } else if (comp instanceof JTextComponent) {
67
        } else if (comp instanceof JTextComponent) {
68
            comp.setEnabled(this.isEnabled());
68
            comp.setEnabled(this.isEnabled());
69
            ((JTextComponent) comp).setEditable(this.isEditable());
69
            ((JTextComponent) comp).setEditable(this.isEditable());
70
        } else if (comp instanceof TextComponent) {
70
        } else if (comp instanceof TextComponent) {
71
            comp.setEnabled(this.isEnabled());
71
            comp.setEnabled(this.isEnabled());
72
            ((TextComponent) comp).setEditable(this.isEditable());
72
            ((TextComponent) comp).setEditable(this.isEditable());
73
        } else {
73
        } else {
74
            comp.setEnabled(this.isEditable());
74
            comp.setEnabled(this.isEditable());
75
        }
75
        }
76
        return comp;
76
        return comp;
77
    }
77
    }
78
 
78
 
-
 
79
    public final InteractionMode and(InteractionMode m) {
-
 
80
        return from(this.isEnabled() && m.isEnabled(), this.isEditable() && m.isEditable());
-
 
81
    }
-
 
82
 
79
    /**
83
    /**
80
     * Try to infer the mode of the passed component. This method has special cases for known
84
     * Try to infer the mode of the passed component. This method has special cases for known
81
     * component (.e.g {@link JTextComponent}) otherwise generic components are presumed to not have
85
     * component (.e.g {@link JTextComponent}) otherwise generic components are presumed to not have
82
     * a {@link #READ_ONLY} mode. For complete control, implement {@link InteractionComponent}.
86
     * a {@link #READ_ONLY} mode. For complete control, implement {@link InteractionComponent}.
83
     * 
87
     * 
84
     * @param comp a component.
88
     * @param comp a component.
85
     * @return the inferred mode.
89
     * @return the inferred mode.
86
     * @see #applyTo(Component)
90
     * @see #applyTo(Component)
87
     */
91
     */
88
    public static InteractionMode from(final Component comp) {
92
    public static InteractionMode from(final Component comp) {
89
        if (comp instanceof InteractionComponent) {
93
        if (comp instanceof InteractionComponent) {
90
            return ((InteractionComponent) comp).getInteractionMode();
94
            return ((InteractionComponent) comp).getInteractionMode();
91
        } else if (comp instanceof TextComponent || comp instanceof JTextComponent) {
95
        } else if (comp instanceof TextComponent || comp instanceof JTextComponent) {
92
            final boolean enabled = comp.isEnabled();
96
            final boolean enabled = comp.isEnabled();
93
            // optimization to avoid casting and calling isEditable()
97
            // optimization to avoid casting and calling isEditable()
94
            if (!enabled)
98
            if (!enabled)
95
                return DISABLED;
99
                return DISABLED;
96
            final boolean editable = comp instanceof TextComponent && ((TextComponent) comp).isEditable() || comp instanceof JTextComponent && ((JTextComponent) comp).isEditable();
100
            final boolean editable = comp instanceof TextComponent && ((TextComponent) comp).isEditable() || comp instanceof JTextComponent && ((JTextComponent) comp).isEditable();
97
            return from(enabled, editable);
101
            return from(enabled, editable);
98
        } else {
102
        } else {
99
            // most components do not have the concept or R/O
103
            // most components do not have the concept or R/O
100
            return comp.isEnabled() ? READ_WRITE : DISABLED;
104
            return comp.isEnabled() ? READ_WRITE : DISABLED;
101
        }
105
        }
102
    }
106
    }
103
 
107
 
104
    public static InteractionMode from(final boolean enabled, final boolean editable) {
108
    public static InteractionMode from(final boolean enabled, final boolean editable) {
105
        if (!enabled)
109
        if (!enabled)
106
            return DISABLED;
110
            return DISABLED;
107
        else if (editable)
111
        else if (editable)
108
            return READ_WRITE;
112
            return READ_WRITE;
109
        else
113
        else
110
            return READ_ONLY;
114
            return READ_ONLY;
111
    }
115
    }
112
 
116
 
113
    public static interface InteractionComponent {
117
    public static interface InteractionComponent {
114
        public void setInteractionMode(InteractionMode mode);
118
        public void setInteractionMode(InteractionMode mode);
115
 
119
 
116
        public InteractionMode getInteractionMode();
120
        public InteractionMode getInteractionMode();
117
    }
121
    }
118
}
122
}