KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > xam > ui > XAMUtils


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.xml.xam.ui;
21
22 import org.netbeans.modules.xml.xam.Component;
23 import org.netbeans.modules.xml.xam.Model;
24 import org.netbeans.modules.xml.xam.ModelSource;
25 import org.netbeans.modules.xml.xam.ui.cookies.GetComponentCookie;
26 import org.netbeans.modules.xml.xam.ui.cookies.ViewComponentCookie;
27 import org.openide.filesystems.FileObject;
28 import org.openide.loaders.DataObject;
29 import org.openide.loaders.DataObjectNotFoundException;
30 import org.openide.nodes.Node;
31
32 /**
33  * Common utilities for XAM user interface module.
34  *
35  * @author Nam Nguyen
36  * @author Nathan Fiedler
37  */

38 public class XAMUtils {
39
40     /**
41      * Retrieve the XAM component for the given node.
42      *
43      * @param node node from which to acquire component.
44      * @return the model component, or null if none.
45      */

46     public static Component getComponent(Node node) {
47         GetComponentCookie cake = (GetComponentCookie) node.getCookie(
48                 GetComponentCookie.class);
49         Component component = null;
50         try {
51             if (cake != null) {
52                 component = cake.getComponent();
53             }
54         } catch (IllegalStateException JavaDoc ise) {
55             // Happens if the component is no longer in the model.
56
// Ignore this here since the caller will deal with it.
57
}
58         if (component == null) {
59             component = (Component) node.getLookup().lookup(Component.class);
60         }
61         return component;
62     }
63
64     /**
65      * Retrieve the cookie for showing the component in the editor.
66      *
67      * @param comp component to be shown.
68      * @param view the desired view in which to show the component.
69      * @return the cookie to view the component.
70      */

71     public static ViewComponentCookie getViewCookie(Component comp,
72             ViewComponentCookie.View view) {
73         if (comp == null) {
74             return null;
75         }
76         try {
77             Model model = comp.getModel();
78             if (model != null) {
79                 FileObject fobj = (FileObject) model.getModelSource().
80                         getLookup().lookup(FileObject.class);
81                 if (fobj != null) {
82                     DataObject dobj = DataObject.find(fobj);
83                     if (dobj != null) {
84                         ViewComponentCookie cake = (ViewComponentCookie) dobj.
85                                 getCookie(ViewComponentCookie.class);
86                         if (cake != null && cake.canView(view, comp)) {
87                             return cake;
88                         }
89                     }
90                 }
91             }
92         } catch (DataObjectNotFoundException donfe) {
93             // fall through to return null
94
}
95         return null;
96     }
97
98     /**
99      * Determine if the given model is writable, which requires that the
100      * source file also be writable.
101      *
102      * @param model the model to be tested.
103      * @return true if model source is editable and the source file is writable.
104      */

105     public static boolean isWritable(Model model) {
106         if (model != null) {
107             ModelSource ms = model.getModelSource();
108             if (ms.isEditable()) {
109                 FileObject fo = (FileObject) ms.getLookup().lookup(FileObject.class);
110                 if (fo != null) {
111                     return fo.canWrite();
112                 }
113             }
114         }
115         return false;
116     }
117 }
118
Popular Tags