1 19 20 package org.netbeans.modules.xml.multiview.ui; 21 22 import java.awt.BorderLayout ; 23 import java.beans.*; 24 import javax.swing.JComponent ; 25 import javax.swing.ActionMap ; 26 import javax.swing.Action ; 27 import javax.swing.KeyStroke ; 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 ; 36 import org.openide.util.Lookup; 37 38 45 46 public abstract class AbstractDesignEditor extends TopComponent implements ExplorerManager.Provider { 47 public static final String PROPERTY_FLUSH_DATA = "Flush Data"; 49 private static final String ACTION_INVOKE_HELP = "invokeHelp"; protected JComponent structureView; 51 protected PanelView contentView; 52 protected javax.swing.Action helpAction; 53 private ExplorerManager manager; 54 55 56 protected static String iconURL = "/org/netbeans/modules/form/resources/inspector.gif"; 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 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 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"); map.put("save", act); 79 associateLookup(ExplorerUtils.createLookup(manager, map)); 80 81 manager.addPropertyChangeListener(new NodeSelectedListener()); 82 setLayout(new BorderLayout ()); 83 } 84 85 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 130 public void setRootContext(Node node) { 131 getExplorerManager().setRootContext(node); 132 } 133 134 138 public PanelView getContentView(){ 139 return contentView; 140 } 141 142 146 public JComponent getStructureView(){ 147 if (structureView ==null){ 148 structureView = createStructureComponent(); 149 structureView.addPropertyChangeListener(new NodeSelectedListener()); 150 } 151 return structureView; 152 } 153 157 abstract public JComponent createStructureComponent() ; 158 159 abstract public ErrorPanel getErrorPanel(); 160 161 165 public void open(){ 166 if (contentView!=null) 167 ((PanelView)contentView).open(); 168 } 169 170 174 public HelpCtx getHelpCtx() { 175 return new HelpCtx("ComponentPanel"); } 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 { 194 HelpCtx.Provider provider = null; 195 public HelpAction() { 196 super(org.openide.util.NbBundle.getMessage(AbstractDesignEditor.class,"CTL_Help"), 197 new javax.swing.ImageIcon ( 198 AbstractDesignEditor.this.getClass().getResource("/org/netbeans/modules/xml/multiview/resources/help.gif"))); } 200 201 public boolean isEnabled() { 202 return getContext() != null; 203 } 204 205 public void actionPerformed(java.awt.event.ActionEvent e) { 206 HelpCtx ctx = getContext(); 207 if (ctx == null) { 208 java.awt.Toolkit.getDefaultToolkit().beep(); 209 return; 210 } 211 212 try { 213 Class c = ((ClassLoader )Lookup.getDefault().lookup( 215 ClassLoader .class)).loadClass( 216 "org.netbeans.api.javahelp.Help"); 218 Object o = Lookup.getDefault().lookup(c); 219 if (o != null) { 220 Method m = c.getMethod("showHelp", new Class [] {HelpCtx.class}); 222 223 if (m != null) { m.invoke(o, new Object [] {ctx}); 225 } 226 return; 227 } 228 } catch (ClassNotFoundException cnfe) { 229 } catch (Exception ee) { 231 org.openide.ErrorManager.getDefault().notify( 233 org.openide.ErrorManager.INFORMATIONAL, ee); 234 } 235 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 propertyName, Object oldValue, Object newValue) 249 throws PropertyVetoException { 250 super.fireVetoableChange(propertyName, oldValue, newValue); 251 } 252 } 253 | Popular Tags |