KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > multiview > ui > AbstractDesignEditor


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-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.xml.multiview.ui;
21
22 import java.awt.BorderLayout JavaDoc;
23 import java.beans.*;
24 import javax.swing.JComponent JavaDoc;
25 import javax.swing.ActionMap JavaDoc;
26 import javax.swing.Action JavaDoc;
27 import javax.swing.KeyStroke JavaDoc;
28
29 import org.openide.nodes.*;
30 import org.openide.explorer.ExplorerManager;
31 import org.openide.explorer.ExplorerUtils;
32 import org.openide.windows.TopComponent;
33 import org.openide.util.HelpCtx;
34 import org.openide.actions.SaveAction;
35 import java.lang.reflect.Method JavaDoc;
36 import org.openide.util.Lookup;
37
38 /**
39  * The ComponentPanel three pane editor. This is basically a container that implements the ExplorerManager
40  * interface. It coordinates the selection of a node in the structure pane and the display of a panel by the a PanelView
41  * in the content pane and the nodes properties in the properties pane. It will populate the tree view in the structure pane
42  * from the root node of the supplied PanelView.
43  *
44  **/

45
46 public abstract class AbstractDesignEditor extends TopComponent implements ExplorerManager.Provider {
47     public static final String JavaDoc PROPERTY_FLUSH_DATA = "Flush Data"; // NOI18N
48

49     private static final String JavaDoc ACTION_INVOKE_HELP = "invokeHelp"; //NOI18N
50
protected JComponent JavaDoc structureView;
51     protected PanelView contentView;
52     protected javax.swing.Action JavaDoc helpAction;
53     private ExplorerManager manager;
54     
55     /** The icon for ComponentInspector */
56     protected static String JavaDoc iconURL = "/org/netbeans/modules/form/resources/inspector.gif"; // NOI18N
57

58     protected static final long serialVersionUID =1L;
59     
60     public AbstractDesignEditor() {
61         init();
62     }
63     
64     private void init() {
65         manager = new ExplorerManager();
66         helpAction = new HelpAction();
67         final ActionMap JavaDoc map = AbstractDesignEditor.this.getActionMap();
68         getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(
69             KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F1, 0), ACTION_INVOKE_HELP);
70         map.put(ACTION_INVOKE_HELP, helpAction);
71         
72         SaveAction act = (SaveAction) org.openide.util.actions.SystemAction.get(SaveAction.class);
73         KeyStroke JavaDoc stroke = KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S,
74                 java.awt.Toolkit.getDefaultToolkit().getMenuShortcutKeyMask());
75
76         getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(stroke, "save"); //NOI18N
77
map.put("save", act); //NOI18N
78

79         associateLookup(ExplorerUtils.createLookup(manager, map));
80         
81         manager.addPropertyChangeListener(new NodeSelectedListener());
82         setLayout(new BorderLayout JavaDoc());
83     }
84
85     /**
86      * Creates a new instance of ComponentPanel
87      * @param contentView The PanelView which will provide the node tree for the structure view
88      * and the set of panels the nodes map to.
89      */

90     public AbstractDesignEditor(PanelView contentView){
91         init();
92         this.contentView = contentView;
93         setRootContext(contentView.getRoot());
94     }
95     
96     public void setContentView(PanelView panelView) {
97         contentView = panelView;
98         setRootContext(panelView.getRoot());
99     }
100     
101     public ExplorerManager getExplorerManager() {
102         return manager;
103     }
104     
105     public void componentActivated() {
106         ExplorerUtils.activateActions(manager, true);
107     }
108     
109     public void componentDeactivated() {
110         ExplorerUtils.activateActions(manager, false);
111     }
112     
113     public void componentClosed() {
114         super.componentClosed();
115     }
116     public void componentOpened() {
117         super.componentOpened();
118     }
119     public void componentShowing() {
120         super.componentShowing();
121     }
122     public void componentHidden() {
123         super.componentShowing();
124     }
125     
126     /**
127      * Sets the root context for the ExplorerManager
128      * @param node The new root context.
129      */

130     public void setRootContext(Node node) {
131         getExplorerManager().setRootContext(node);
132     }
133  
134     /**
135      * Used to get the JComponent used for the content pane. Usually a subclass of PanelView.
136      * @return the JComponent
137      */

138     public PanelView getContentView(){
139         return contentView;
140     }
141     
142     /**
143      * Used to get the JComponent used for the structure pane. Usually a container for the structure component or the structure component itself.
144      * @return the JComponent
145      */

146     public JComponent JavaDoc getStructureView(){
147         if (structureView ==null){
148             structureView = createStructureComponent();
149             structureView.addPropertyChangeListener(new NodeSelectedListener());
150         }
151         return structureView;
152     }
153     /**
154      * Used to create an instance of the JComponent used for the structure component. Usually a subclass of BeanTreeView.
155      * @return the JComponent
156      */

157     abstract public JComponent JavaDoc createStructureComponent() ;
158
159     abstract public ErrorPanel getErrorPanel();
160     
161     /**
162      * A parent TopComponent can use this method to notify the ComponentPanel and it PanelView children that it was opened
163      * and lets them do any needed initialization as a result. Default implementation just delegates to the PanelView.
164      */

165     public void open(){
166         if (contentView!=null)
167             ((PanelView)contentView).open();
168     }
169     
170     /**
171      * returns the HelpCtx for this component.
172      * @return the HelpCtx
173      */

174     public HelpCtx getHelpCtx() {
175         return new HelpCtx("ComponentPanel"); // NOI18N
176
}
177     
178     class NodeSelectedListener implements PropertyChangeListener {
179         public void propertyChange(PropertyChangeEvent evt) {
180             if (contentView.isSectionHeaderClicked()) {
181                 contentView.setSectionHeaderClicked(false);
182                 return;
183             }
184             if (!ExplorerManager.PROP_SELECTED_NODES.equals(evt.getPropertyName()))
185                 return;
186
187             Node[] selectedNodes = getExplorerManager().getSelectedNodes();
188             if (selectedNodes!=null && selectedNodes.length>0)
189                 contentView.showSelection(selectedNodes);
190         }
191     }
192     
193     final class HelpAction extends javax.swing.AbstractAction JavaDoc {
194         HelpCtx.Provider provider = null;
195         public HelpAction() {
196             super(org.openide.util.NbBundle.getMessage(AbstractDesignEditor.class,"CTL_Help"),
197                   new javax.swing.ImageIcon JavaDoc (
198                       AbstractDesignEditor.this.getClass().getResource("/org/netbeans/modules/xml/multiview/resources/help.gif"))); //NOI18N
199
}
200         
201         public boolean isEnabled() {
202             return getContext() != null;
203         }
204         
205         public void actionPerformed(java.awt.event.ActionEvent JavaDoc e) {
206             HelpCtx ctx = getContext();
207             if (ctx == null) {
208                 java.awt.Toolkit.getDefaultToolkit().beep();
209                 return;
210             }
211             
212             try {
213                 //Copied from original property sheet implementation
214
Class JavaDoc c = ((ClassLoader JavaDoc)Lookup.getDefault().lookup(
215                 ClassLoader JavaDoc.class)).loadClass(
216                 "org.netbeans.api.javahelp.Help"); // NOI18N
217

218                 Object JavaDoc o = Lookup.getDefault().lookup(c);
219                 if (o != null) {
220                     Method JavaDoc m = c.getMethod("showHelp", // NOI18N
221
new Class JavaDoc[] {HelpCtx.class});
222
223                     if (m != null) { //Unit tests
224
m.invoke(o, new Object JavaDoc[] {ctx});
225                     }
226                     return;
227                 }
228             } catch (ClassNotFoundException JavaDoc cnfe) {
229                 // ignore - maybe javahelp module is not installed, not so strange
230
} catch (Exception JavaDoc ee) {
231                 // potentially more serious
232
org.openide.ErrorManager.getDefault().notify(
233                     org.openide.ErrorManager.INFORMATIONAL, ee);
234             }
235             // Did not work.
236
java.awt.Toolkit.getDefaultToolkit().beep();
237         }
238         
239         private HelpCtx getContext() {
240             Node[] selectedNodes = getExplorerManager().getSelectedNodes();
241             if (selectedNodes!=null && selectedNodes.length>0)
242                 return selectedNodes[0].getHelpCtx();
243             else
244                 return null;
245         }
246     }
247
248     public void fireVetoableChange(String JavaDoc propertyName, Object JavaDoc oldValue, Object JavaDoc newValue)
249             throws PropertyVetoException {
250         super.fireVetoableChange(propertyName, oldValue, newValue);
251     }
252 }
253
Popular Tags