View Javadoc

1   /*
2    * Copyright (c) 2003-2008 by Cosylab d. d.
3    *
4    * This file is part of CosyBeans-Common.
5    *
6    * CosyBeans-Common is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU General Public License as published by
8    * the Free Software Foundation, either version 3 of the License, or
9    * (at your option) any later version.
10   *
11   * CosyBeans-Common is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   *
16   * You should have received a copy of the GNU General Public License
17   * along with CosyBeans-Common.  If not, see <http://www.gnu.org/licenses/>.
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   * <code>EditorChooser</code> is convenience implementation, which trys to find
29   * one active editor form list of available editorList.
30   *
31   * @author <a href="mailto:igor.kriznar@cosylab.com">Igor Kriznar</a>
32   * @author <a href="mailto:jernej.kamenik@cosylab.com">Jernej Kamenik</a>
33   * @version $id$ EditorChooser.java,v 1.3 2003/07/19 11:52:10 jkamenik Exp $$
34   *
35   * @since Jul 15, 2003.
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  	 * Default constructor.
45  	 */
46  	public EditorChooser()
47  	{
48  		super();
49  	}
50  
51  	/* (non-Javadoc)
52  	 * @see com.cosylab.gui.components.customizer.Editor#getEditor(java.lang.Object, java.lang.String)
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  		//		if (activeEditor != null) {
62  		//			throw new IllegalStateException(
63  		//			    "Can not produce start new session, while old is not finished.");
64  		//		}
65  		// by jkamenik <not wanted behavior>
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  	/* (non-Javadoc)
81  	 * @see com.cosylab.gui.components.customizer.Editor#canEdit(java.lang.Object, java.lang.String)
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  	/* (non-Javadoc)
97  	 * @see com.cosylab.gui.components.customizer.Editor#applySetting()
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 	/* (non-Javadoc)
109 	 * @see com.cosylab.gui.components.customizer.Editor#revertSettings()
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 	/* (non-Javadoc)
121 	 * @see com.cosylab.gui.components.customizer.Editor#stopEditing()
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 	 * Returns active editor.
136 	 *
137 	 * @return returns active editor
138 	 */
139 	public Editor getActiveEditor()
140 	{
141 		return activeEditor;
142 	}
143 
144 	/**
145 	 * Returns list of all registered editors.
146 	 *
147 	 * @return all registered editors
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 	 * Adds editor to the list of available editorList.
161 	 *
162 	 * @param editor new available editor
163 	 */
164 	public synchronized void addEditor(Editor editor)
165 	{
166 		editorList.add(editor);
167 		editors = null;
168 	}
169 
170 	/**
171 	 * Removes provided editor from this list.
172 	 *
173 	 * @param editor to beremoved
174 	 */
175 	public synchronized void removeEditor(Editor editor)
176 	{
177 		editorList.remove(editor);
178 		editors = null;
179 	}
180 }
181 
182 /* __oOo__ */