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