1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.cosylab.gui.components.customizer;
21
22 import java.util.ArrayList;
23
24 import javax.swing.JComponent;
25
26
27
28
29
30
31
32
33
34
35
36
37 public class EditorChooser implements Editor
38 {
39 private ArrayList editorList = new ArrayList();
40 private Editor[] editors;
41 private Editor activeEditor;
42
43
44
45
46 public EditorChooser()
47 {
48 super();
49 }
50
51
52
53
54 public JComponent getEditorComponent(Object displayer, String aspect)
55 throws IllegalArgumentException
56 {
57 if (displayer == null) {
58 throw new NullPointerException("displayer==null");
59 }
60
61
62
63
64
65
66 Editor[] ed = getEditors();
67
68 for (int i = 0; i < ed.length; i++) {
69 if (ed[i].canEdit(displayer, aspect)) {
70 return (activeEditor = ed[i]).getEditorComponent(displayer,
71 aspect);
72 }
73 }
74
75 throw new IllegalArgumentException("Editor for displayer '" + displayer
76 + "' in aspect of '" + aspect + "' not found from "
77 + editorList.size() + " of avaliable editors.");
78 }
79
80
81
82
83 public boolean canEdit(Object displayer, String aspect)
84 {
85 Editor[] ed = getEditors();
86
87 for (int i = 0; i < ed.length; i++) {
88 if (ed[i].canEdit(displayer, aspect)) {
89 return true;
90 }
91 }
92
93 return false;
94 }
95
96
97
98
99 public void applySettings()
100 {
101 Editor[] ed = getEditors();
102
103 for (int i = 0; i < ed.length; i++) {
104 ed[i].applySettings();
105 }
106 }
107
108
109
110
111 public void revertSettings()
112 {
113 Editor[] ed = getEditors();
114
115 for (int i = 0; i < ed.length; i++) {
116 ed[i].revertSettings();
117 }
118 }
119
120
121
122
123 public void stopEditing()
124 {
125 Editor[] ed = getEditors();
126
127 for (int i = 0; i < ed.length; i++) {
128 ed[i].stopEditing();
129 }
130
131 activeEditor = null;
132 }
133
134
135
136
137
138
139 public Editor getActiveEditor()
140 {
141 return activeEditor;
142 }
143
144
145
146
147
148
149 public synchronized Editor[] getEditors()
150 {
151 if (editors == null) {
152 editors = new Editor[editorList.size()];
153 editorList.toArray(editors);
154 }
155
156 return editors;
157 }
158
159
160
161
162
163
164 public synchronized void addEditor(Editor editor)
165 {
166 editorList.add(editor);
167 editors = null;
168 }
169
170
171
172
173
174
175 public synchronized void removeEditor(Editor editor)
176 {
177 editorList.remove(editor);
178 editors = null;
179 }
180 }
181
182