KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > util > browser > core > common > AdminCustomization


1 /*===========================================================================
2
3 ObjectWeb Naming Context Framework
4 Copyright (C) 2002 USTL - LIFL - GOAL
5 Contact: architecture@objectweb.org
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA
21
22 Initial developer(s): Philippe Merle, Jerome Moroy.
23 Contributor(s): ______________________________________.
24
25 ===========================================================================*/

26
27 package org.objectweb.util.browser.core.common;
28
29 import javax.swing.JPopupMenu JavaDoc;
30 import javax.swing.Icon JavaDoc;
31 import java.util.Vector JavaDoc;
32 import org.objectweb.util.browser.api.Context;
33 import org.objectweb.util.browser.api.Entry;
34 import org.objectweb.util.browser.api.DropAction;
35 import org.objectweb.util.browser.api.Panel;
36 import org.objectweb.util.browser.api.Info;
37 import org.objectweb.util.browser.core.wrapper.AssociatedContextImpl;
38 import org.objectweb.util.browser.core.naming.DefaultEntry;
39 import org.objectweb.util.browser.core.naming.DefaultName;
40 import org.objectweb.util.browser.core.popup.AssociatedMenuImpl;
41 import org.objectweb.util.browser.core.popup.AdvancedJPopupMenu;
42 import org.objectweb.util.browser.core.popup.DefaultMenuItemTreeView;
43 import org.objectweb.util.browser.core.panel.AssociatedPanelImpl;
44 import org.objectweb.util.browser.core.api.AssociatedContext;
45 import org.objectweb.util.browser.core.api.AssociatedDropAction;
46 import org.objectweb.util.browser.core.api.AssociatedIcon;
47 import org.objectweb.util.browser.core.api.AssociatedInfo;
48 import org.objectweb.util.browser.core.api.AssociatedMenu;
49 import org.objectweb.util.browser.core.api.AssociatedPanel;
50 import org.objectweb.util.browser.core.api.BrowserProperty;
51 import org.objectweb.util.browser.core.api.ExtendedEntry;
52 import org.objectweb.util.browser.core.api.RootFactory;
53 import org.objectweb.util.browser.core.api.Role;
54 import org.objectweb.util.browser.core.dnd.AssociatedDropActionImpl;
55 import org.objectweb.util.browser.core.icon.AssociatedIconImpl;
56 import org.objectweb.util.browser.core.info.AssociatedInfoImpl;
57
58 /**
59  * Centralizes the different customizations
60  *
61  * @author <a HREF="mailto:Jerome.Moroy@lifl.fr">Jerome Moroy</a>
62  * @version 0.1
63  */

64 public class AdminCustomization {
65
66     /** The associated tree */
67     protected DynamicTree tree_;
68
69     /** The icons map */
70     protected AssociatedIcon icons = null;
71
72     /** The panels map */
73     protected AssociatedPanel panels = null;
74
75     /** The menus map */
76     protected AssociatedMenu menus = null;
77
78     /** The wrappers map */
79     protected AssociatedContext wrappers = null;
80
81     /** The DropAction map */
82     protected AssociatedDropAction dropAction = null;
83
84     /** The infos map */
85     protected AssociatedInfo infos = null;
86
87     /** The associated BrowserProperty */
88     protected BrowserProperty properties_ = null;
89     
90     /** Indicates the target is a tree */
91     public static final int TREE_TARGET = 0;
92     
93     /** Indicates the target is a table */
94     public static final int TABLE_TARGET = TREE_TARGET + 1;
95
96     /**
97      * Default Constructor
98      * @param properties The memory object which contains the properties
99      * @param tree The current tree
100      */

101     public AdminCustomization(BrowserProperty properties, DynamicTree tree) {
102         tree_ = tree;
103         properties_ = properties;
104         if (properties != null) {
105             icons = new AssociatedIconImpl(properties);
106             menus = new AssociatedMenuImpl(properties, tree);
107             panels = new AssociatedPanelImpl(properties);
108             wrappers = new AssociatedContextImpl(properties);
109             dropAction = new AssociatedDropActionImpl(properties);
110             infos = new AssociatedInfoImpl(properties);
111         } else
112             System.out.println("The properties param is null !");
113     }
114
115     /**
116      * Gives the current BrowserProperty.
117      * @return The current BrowserProperty
118      */

119     public BrowserProperty getBrowserProperty() {
120         return properties_;
121     }
122
123     /**
124      * Tries to find a menu for the given entry.
125      * It looks for the menu by contacting the AssociatedMenu instance.
126      *
127      * If the given object is an <code>Entry</code> instance:
128      * <ul>
129      * <li>1. First, its looks for with the type of the wrapped object by the entry</li>
130      * <li>2. Then, its looks for with the type of the value of the entry</li>
131      * </ul>
132      * <ul>
133      * <li>1. First, it looks for with the type of the wrapped object by the entry</li>
134      * <li>2. Then, it looks for with the type of the value of the entry</li>
135      * <li>3. After, it looks for with the type of the wrapper object of the parent of the givent entry</li>
136      * <li>4. After, it looks for with the type of the value of the parent of the givent entry</li>
137      * <li>5. Finally, it merges the four menus</li>
138      * </ul>
139      * @return The associated menu
140      */

141     public JPopupMenu JavaDoc getMenu(Object JavaDoc object, int target) {
142         if (object != null) {
143             if(target == TREE_TARGET) {
144                 ExtendedEntry entry = (ExtendedEntry)object;
145                 AdvancedJPopupMenu objectMenu = menus.getMenu(entry.getWrappedObject(), true, new DefaultMenuItemTreeView(tree_));
146                 AdvancedJPopupMenu compositeMenu = menus.getMenu(entry.getValue(), new DefaultMenuItemTreeView(tree_));
147                 ExtendedEntry parentEntry = (ExtendedEntry) tree_.getSelectedEntryParent();
148                 AdvancedJPopupMenu objectChildMenu = null;
149                 AdvancedJPopupMenu compositeChildMenu = null;
150                 if (parentEntry != null) {
151                     objectChildMenu = menus.getParentMenu(parentEntry.getWrappedObject());
152                     compositeChildMenu = menus.getParentMenu(parentEntry.getValue());
153                 }
154                 AdvancedJPopupMenu fusion = new AdvancedJPopupMenu(tree_);
155                 fusion.add(objectMenu);
156                 fusion.add(compositeMenu);
157                 if (objectMenu != null && objectMenu.hasToInheritTreeMenu() || objectMenu == null) {
158                     fusion.add(objectChildMenu);
159                     fusion.add(compositeChildMenu);
160                 }
161                 return fusion;
162             } else if(target == TABLE_TARGET) {
163                 if(ExtendedEntry.class.isAssignableFrom(object.getClass())){
164                     ExtendedEntry entry = (ExtendedEntry)object;
165                     AdvancedJPopupMenu objectMenu = menus.getMenu(entry.getWrappedObject(), true, new DefaultMenuItemTreeView(tree_, entry));
166                     AdvancedJPopupMenu compositeMenu = menus.getMenu(entry.getValue(), new DefaultMenuItemTreeView(tree_, entry));
167                     AdvancedJPopupMenu fusion = new AdvancedJPopupMenu(tree_);
168                     fusion.add(objectMenu);
169                     fusion.add(compositeMenu);
170                     return fusion;
171                 } else {
172                     return menus.getMenu(object, new DefaultMenuItemTreeView(tree_, new DefaultEntry(object, new DefaultName(object.toString()),null)));
173                 }
174             }
175         }
176         return null;
177     }
178
179     /**
180      * Equals to <code>getMenu(object,TREE_TARGET);</code>.
181      * @see AdminCustomization#getMenu(Object object, boolean isTreeMenu);
182      */

183     public JPopupMenu JavaDoc getMenu(Object JavaDoc object) {
184         return getMenu(object,TREE_TARGET);
185     }
186
187     /**
188      * Tries to find an icon for the given object.
189      * It looks for the icon by contacting the AssociatedIcon instance.
190      *
191      * If the given object is an <code>Entry</code> instance:
192      * <ul>
193      * <li>1. First, its looks for with the type of the wrapped object by the entry</li>
194      * <li>2. Then, its looks for with the type of the value of the entry</li>
195      * </ul>
196      *
197      * @param object The object for which an icon may be defined.
198      * @param disabledJavaLangObject If true, then java.lang.Object is not consider as a valid entry
199      *
200      * @return The associated icon
201      */

202     public Icon JavaDoc getIcon(Object JavaDoc object){
203         if(object != null) {
204             Icon JavaDoc icon = null;
205             if(ExtendedEntry.class.isAssignableFrom(object.getClass())){
206                 ExtendedEntry entry = (ExtendedEntry)object;
207                 icon = icons.getIcon(entry.getWrappedObject(), true);
208                 if (icon == null)
209                     icon = icons.getIcon(entry.getValue());
210                 return icon;
211             } else
212                 return icons.getIcon(object);
213         }
214         return null;
215     }
216
217
218     /**
219      * Tries to find a panel for the given entry.
220      * It looks for the panel by contacting the AssociatedPanel instance.
221      * <ul>
222      * <li>1. First, its looks for with the type of the wrapped object by the entry</li>
223      * <li>2. Then, its looks for with the type of the value of the entry</li>
224      * </ul>
225      * @return The associated icon
226      */

227     public Panel getPanel(ExtendedEntry entry) {
228         if (entry != null) {
229             Panel panel = panels.getPanel(entry.getWrappedObject(), true);
230             if (panel == null)
231                 panel = panels.getPanel(entry.getValue());
232             return panel;
233         }
234         return null;
235     }
236
237     /**
238      * Tries to find a context for the given node.
239      * @return The associated context
240      */

241     public Context getContext(Object JavaDoc node) {
242         if (wrappers != null)
243             return wrappers.getContext(node);
244         return null;
245     }
246
247     /**
248      * Tries to find an action for the given entry.
249      * It looks for the action by contacting the AssociatedDropAction instance.
250      * <ul>
251      * <li>1. First, its looks for with the type of the wrapped object by the entry</li>
252      * <li>2. Then, its looks for with the type of the value of the entry</li>
253      * </ul>
254      * @return The associated drop action
255      */

256     public DropAction getDropAction(ExtendedEntry entry, int theDropAction) {
257         if (entry != null) {
258             DropAction action = dropAction.getDropAction(entry.getWrappedObject(), true, theDropAction);
259             if (action == null)
260                 action = dropAction.getDropAction(entry.getValue(), theDropAction);
261             return action;
262         }
263         return null;
264     }
265
266     /**
267      * Tries to find a description of the drop action if this one is defined
268      * It looks for the action by contacting the AssociatedDropAction instance.
269      * <ul>
270      * <li>1. First, its looks for with the type of the wrapped object by the entry</li>
271      * <li>2. Then, its looks for with the type of the value of the entry</li>
272      * </ul>
273      * @return A description of the Drop action
274      */

275     public String JavaDoc getDropLabel(ExtendedEntry entry, int theDropAction) {
276         if (entry != null) {
277             String JavaDoc label = dropAction.getDropLabel(entry.getWrappedObject(), true, theDropAction);
278             if (label == null)
279                 label = dropAction.getDropLabel(entry.getValue(), theDropAction);
280             return label;
281         }
282         return "";
283     }
284
285     /**
286      * Tries to find an Info object for the given entry.
287      * It looks for the Info by contacting the AssociatedInfo instance.
288      * <ul>
289      * <li>1. First, its looks for with the type of the wrapped object by the entry</li>
290      * <li>2. Then, its looks for with the type of the value of the entry</li>
291      * </ul>
292      * @return The associated Info.
293      */

294     public Info getInfo(ExtendedEntry entry) {
295         if (entry != null) {
296             Info info = infos.getInfo(entry.getWrappedObject(), true);
297             if (info == null)
298                 info = infos.getInfo(entry.getValue());
299             return info;
300         }
301         return null;
302     }
303     
304     /**
305      * Provides the root context depending on the selected role.
306      */

307     public Entry[] getRoot() {
308         Role[] roles = properties_.getInheritedRoles();
309         Vector JavaDoc values = new Vector JavaDoc();
310         Entry[] entries = null;
311         for(int i = 0 ; i < roles.length ; i++) {
312             RootFactory rootFactory = properties_.getRoot(roles[i]);
313             if(rootFactory!=null){
314                 entries = rootFactory.newRootContext();
315                 for(int j = 0 ; j < entries.length ; j++)
316                     values.add(entries[j]);
317             }
318         }
319         return (Entry[])values.toArray(new Entry[0]);
320     }
321     
322     /**
323      * Removes the property loaded in cache.
324      */

325     public void clear(){
326         icons.clear();
327         panels.clear();
328         menus.clear();
329         wrappers.clear();
330         dropAction.clear();
331         infos.clear();
332     }
333 }
334
Popular Tags