|
What this is
Other links
The source code/* * Sun Public License Notice * * The contents of this file are subject to the Sun Public License * Version 1.0 (the "License"). You may not use this file except in * compliance with the License. A copy of the License is available at * http://www.sun.com/ * * The Original Code is NetBeans. The Initial Developer of the Original * Code is Sun Microsystems, Inc. Portions Copyright 1997-2004 Sun * Microsystems, Inc. All Rights Reserved. */ package org.netbeans.core.ui; import java.awt.event.ActionEvent; import java.awt.event.KeyEvent; import java.beans.*; import java.io.*; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.text.Collator; import java.util.*; import javax.swing.Action; import javax.swing.ActionMap; import javax.swing.event.*; import javax.swing.InputMap; import javax.swing.JComponent; import javax.swing.JFileChooser; import javax.swing.KeyStroke; import javax.swing.AbstractAction; import org.openide.ErrorManager; import org.openide.actions.*; import org.openide.filesystems.*; import org.openide.loaders.*; import org.openide.nodes.*; import org.openide.options.*; import org.openide.util.*; import org.openide.util.actions.*; import org.openide.util.datatransfer.*; import org.netbeans.core.IDESettings; import org.openide.windows.WindowManager; /** Node representing modules. * @author Jesse Glick, Jaroslav Tulach, Rostislav Levy (?), et al. */ public class ModuleNode extends AbstractNode { private static final ResourceBundle bundle = NbBundle.getBundle( ModuleNode.class ); /** Icon bases */ private static final String MODULE_ITEM_ICON_BASE = "org/netbeans/core/resources/moduleItem"; // NOI18N private static final String MODULE_ITEM_DISABLED_BASE = "org/netbeans/core/resources/moduleItemDisabled"; // NOI18N private static final String MODULE_ITEM_ERROR_BASE = "org/netbeans/core/resources/moduleItemError"; // NOI18N private static final String MODULE_TEST_ITEM_ICON_BASE = "org/netbeans/core/resources/testModuleItem"; // NOI18N private static final String MODULES_ICON_BASE = "org/netbeans/core/resources/modules"; // NOI18N /** Last directory used by the install new module file chooser. */ private static File lastChosenDir = null; /** New types */ private static NewType[] newTypes = null; /** The actual data. */ private static ModuleBean.AllModulesBean allModules = ModuleBean.AllModulesBean.getDefault(); /** Creates a new ModulesNode */ public ModuleNode() { this(false); } private boolean simple = false; public ModuleNode(boolean simple) { // #14553: try to keep code name consistent for node path compatibility super(new Modules (simple)); setName("Modules"); // NOI18N setDisplayName(bundle.getString("CTL_Modules_name")); setShortDescription (bundle.getString ("CTL_Modules_hint")); setIconBase (MODULES_ICON_BASE); this.simple = simple; } public HelpCtx getHelpCtx () { return new HelpCtx (ModuleNode.class); } public Node cloneNode () { return new ModuleNode (); } protected SystemAction[] createActions () { return new SystemAction[] { SystemAction.get (NewAction.class), null, SystemAction.get (ToolsAction.class), SystemAction.get (PropertiesAction.class), }; } /** Add a sorting property. */ protected Sheet createSheet () { Sheet.Set set = new Sheet.Set (); set.setName ("sorting"); // NOI18N set.setDisplayName (bundle.getString ("LBL_ModuleNode_sheet_sorting")); set.put (((Modules) getChildren ()).createSortingProperty ()); Sheet sheet = new Sheet (); sheet.put (set); return sheet; } public NewType[] getNewTypes () { if ( newTypes == null ) { int size = Boolean.getBoolean ("netbeans.module.test") ? 2 : 1; Lookup lkp = Lookup.getDefault(); // XXX use Looks in the future instead of this hack Lookup.Result result = lkp.lookup( new Lookup.Template( org.netbeans.core.ModuleNode.ModuleNewType.class ) ); newTypes = new NewType[size + result.allInstances().size()]; newTypes[0] = new NewType () { public String getName () { return bundle.getString ("CTL_NewModuleByFile"); } public void create () throws IOException { addFile (false); } }; if ( Boolean.getBoolean ("netbeans.module.test")) { // NOI18N newTypes[1] = new NewType () { public String getName () { return bundle.getString( "CTL_NewTestModule"); } public void create () throws IOException { addFile (true); } }; } Iterator instances = result.allInstances().iterator(); while (instances.hasNext()) { newTypes[size] = (NewType) instances.next(); size++; } } return newTypes; } /** Allows to add new module by specifying its file. */ void addFile (boolean reloadable) /*throws IOException*/ { // XXX should use File property editor, not JFileChooser directly final JFileChooser chooser = new JFileChooser (); chooser.setFileHidingEnabled(false); // XXX #18270. Enter doesn't work when expecting folder change, // Accessibility problem. We hack default behaviour here. // Only jdk1.3 there is not the action in action map, i.e. also no // key binding. When running on jdk1.4 only remove this part // dealing with setting the key binding. InputMap im = chooser.getInputMap( JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); if(im != null) { KeyStroke enter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0); Object value = im.get(enter); if(value == null) { im.put(enter, "approveSelection"); // NOI18N } } // Add(jdk1.3) or replace(jdk1.4) to parent (UI) map the new action // doing the folder change. ActionMap map = chooser.getActionMap(); if(map != null) { // Get parent map, which is map set by FileChooserUI, // containing the default approveSelection action. ActionMap parent = map.getParent(); if(parent != null) { // Get original action from parent map, set by UI. final Action original = parent.get("approveSelection"); // NOI18N // Replace it by our action which adds the folder change, // if selected is a directory. parent.put("approveSelection", new AbstractAction() { // NOI18N public void actionPerformed(ActionEvent evt) { File file = chooser.getSelectedFile(); if(file != null && file.isDirectory()) { try { // Strip trailing ".." file = file.getCanonicalFile(); } catch (java.io.IOException ioe) { // Ok, use f as is } chooser.setCurrentDirectory(file); } else { if(original != null) { original.actionPerformed(evt); } } } }); } } // End of hacking. if (lastChosenDir != null) chooser.setCurrentDirectory (lastChosenDir); chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); chooser.setApproveButtonText (bundle.getString ("CTL_ModuleSelect")); chooser.setApproveButtonToolTipText (bundle.getString ("CTL_ModuleSelectToolTip")); chooser.setFileFilter (new javax.swing.filechooser.FileFilter () { public String getDescription () { return bundle.getString ("CTL_ModuleSelectFilter"); } public boolean accept (File f) { return f.isDirectory () || f.getName ().endsWith (".jar"); // NOI18N } }); int result = chooser.showOpenDialog (WindowManager.getDefault ().getMainWindow ()); lastChosenDir = chooser.getCurrentDirectory (); if (result == JFileChooser.APPROVE_OPTION) { // install the file File jar = chooser.getSelectedFile(); allModules.create(jar, reloadable); } } /** Class representing node of one standard module */ static class Item extends AbstractNode implements PropertyChangeListener { private ModuleBean item; /** true if usedBy prop has been displayed, which can affect all else */ private boolean listeningToAllModules = false; private boolean simple = false; public Item (ModuleBean item) { super (Children.LEAF); this.item = item; setName (item.getCodeNameBase()); setDefaultAction (SystemAction.get (PropertiesAction.class)); item.addPropertyChangeListener(org.openide.util.WeakListeners.propertyChange(this, item)); updateDisplayStuff(); getCookieSet().add(new InstanceSupport.Instance(item)); // Make sure that settings UI can deal with the Modules/*.xml file correctly. String filename = "Modules/" + item.getCodeNameBase().replace('.', '-') + ".xml"; // NOI18N FileObject xml = Repository.getDefault().getDefaultFileSystem().findResource(filename); if (xml != null) { try { getCookieSet().add(DataObject.find(xml)); } catch (DataObjectNotFoundException donfe) { ErrorManager.getDefault().notify(ErrorManager.WARNING, donfe); } } else { if (item.getJar() != null) { ErrorManager.getDefault().log(ErrorManager.WARNING, "WARNING - ModuleNode.Item could not find " + filename); } // else not a problem - fixed module, no storage expected } } public Item (ModuleBean item, boolean simple) { this(item); this.simple = simple; } private void updateDisplayStuff() { setDisplayName (item.getDisplayName()); setShortDescription(item.getShortDescription()); setIconBase (item.isEnabled () ? (item.isReloadable () ? MODULE_TEST_ITEM_ICON_BASE : MODULE_ITEM_ICON_BASE) : (item.isProblematic() ? MODULE_ITEM_ERROR_BASE : MODULE_ITEM_DISABLED_BASE)); } public void propertyChange(PropertyChangeEvent evt) { // Some aspects of the module may have changed. Redisplay everything. updateDisplayStuff(); firePropertyChange(null, null, null); } public HelpCtx getHelpCtx () { return new HelpCtx (Item.class); } protected SystemAction[] createActions () { return new SystemAction[] { SystemAction.get (DeleteAction.class), // XXX reload action too... null, SystemAction.get (ToolsAction.class), SystemAction.get (PropertiesAction.class), }; } public void destroy () { item.delete(); } public boolean canDestroy () { return item.getJar() != null; } /** Creates properties. */ protected Sheet createSheet () { Sheet s = Sheet.createDefault (); Sheet.Set ss = s.get (Sheet.PROPERTIES); Sheet.Set sse = Sheet.createExpertSet (); s.put(sse); try { Node.Property p; if (!simple) { p = new PropertySupport.Reflection(item, String.class, "getDisplayName", null); // NOI18N p.setName("displayName"); // NOI18N p.setDisplayName(bundle.getString ("PROP_modules_name")); p.setShortDescription(bundle.getString ("HINT_modules_name")); ss.put(p); } p = new PropertySupport.Reflection(item, String.class, "getSpecificationVersion", null); // NOI18N p.setValue("suppressCustomEditor",Boolean.TRUE); p.setName("specificationVersion"); // NOI18N p.setDisplayName(bundle.getString ("PROP_modules_specversion")); p.setShortDescription(bundle.getString ("HINT_modules_specversion")); ss.put(p); if (!simple) { p = new PropertySupport.Reflection(item, String.class, "getImplementationVersion", null); // NOI18N p.setValue("suppressCustomEditor",Boolean.TRUE); p.setName("implementationVersion"); // NOI18N p.setDisplayName(bundle.getString ("PROP_modules_implversion")); p.setShortDescription(bundle.getString ("HINT_modules_implversion")); sse.put(p); p = new PropertySupport.Reflection(item, String.class, "getBuildVersion", null); // NOI18N p.setValue("suppressCustomEditor",Boolean.TRUE); p.setName("buildVersion"); // NOI18N p.setDisplayName(bundle.getString ("PROP_modules_buildversion")); p.setShortDescription(bundle.getString ("HINT_modules_buildversion")); sse.put(p); p = new PropertySupport.Reflection(item, String.class, "getShortDescription", null); // NOI18N p.setValue("suppressCustomEditor",Boolean.TRUE); p.setName("shortDescription"); // NOI18N p.setDisplayName(bundle.getString ("PROP_modules_shortDescription")); p.setShortDescription(bundle.getString ("HINT_modules_shortDescription")); ss.put(p); p = new PropertySupport.Reflection(item, String.class, "getLongDescription", null); // NOI18N p.setValue("suppressCustomEditor",Boolean.TRUE); p.setName("longDescription"); // NOI18N p.setDisplayName(bundle.getString ("PROP_modules_longDescription")); p.setShortDescription(bundle.getString ("HINT_modules_longDescription")); ss.put(p); p = new PropertySupport.Reflection(item, String.class, "getCategory", null); // NOI18N p.setValue("suppressCustomEditor",Boolean.TRUE); p.setName("category"); // NOI18N p.setDisplayName(bundle.getString ("PROP_modules_category")); p.setShortDescription(bundle.getString ("HINT_modules_category")); sse.put(p); } class EnabledOrReloadableProp extends PropertySupport.Reflection { public EnabledOrReloadableProp(String getter, String setter) throws NoSuchMethodException { super(item, Boolean.TYPE, getter, setter); } public boolean canWrite() { if (! super.canWrite()) return false; // not really necessary if (this.getName().equals("enabled")) { // NOI18N return /*! item.isFixed()*/ item.getJar() != null && !item.isProblematic() && !item.isAutoload() && !item.isEager(); } else if (this.getName().equals("reloadable")) { // NOI18N // Need to be able to write to the containing directory to use this feature. return item.getJar() != null && (item.isReloadable() || item.getJar().getParentFile().canWrite()); } else { throw new IllegalStateException(); } } } p = new EnabledOrReloadableProp("isEnabled", "setEnabled"); // NOI18N p.setName("enabled"); // NOI18N p.setDisplayName(bundle.getString("PROP_modules_enabled")); p.setShortDescription(bundle.getString("HINT_modules_enabled")); ss.put(p); if (!simple) { p = new EnabledOrReloadableProp("isReloadable", "setReloadable"); // NOI18N p.setName("reloadable"); // NOI18N p.setDisplayName(bundle.getString("PROP_modules_reloadable")); p.setShortDescription(bundle.getString("HINT_modules_reloadable")); sse.put(p); p = new PropertySupport.Reflection(item, String.class, "getCodeName", null); // NOI18N p.setValue("suppressCustomEditor",Boolean.TRUE); p.setName("codeName"); // NOI18N p.setDisplayName(bundle.getString ("PROP_modules_codename")); p.setShortDescription(bundle.getString ("HINT_modules_codename")); sse.put(p); if (item.getJar() != null) { p = new PropertySupport.Reflection(item, File.class, "getJar", null); // NOI18N p.setName("jar"); // NOI18N p.setDisplayName(bundle.getString ("PROP_modules_jar")); p.setShortDescription(bundle.getString ("HINT_modules_jar")); sse.put(p); } class ClasspathProperty extends PropertySupport.ReadOnly { // See #22466 public ClasspathProperty() { super("classpath", getNbClassPathOrStringClass(), // NOI18N bundle.getString("PROP_modules_classpath"), bundle.getString("HINT_modules_classpath")); } public Object getValue() throws InvocationTargetException { String cp = item.getEffectiveClasspath(); if (getValueType() == String.class) { return cp; } else { try { Constructor c = getValueType().getConstructor(new Class[] {String.class}); return c.newInstance(new Object[] {cp}); } catch (Exception e) { throw new InvocationTargetException(e); } } } public Object getValue(String key) { if ("suppressCustomEditor".equals(key)) { return Boolean.TRUE; } else { return super.getValue(key); } } } sse.put(new ClasspathProperty()); // Though normally in a separate category, still potentially // useful to leave here (e.g. if sorting differently): p = new PropertySupport.Reflection(item, Boolean.TYPE, "isAutoload", null); // NOI18N p.setName("autoload"); // NOI18N p.setDisplayName(bundle.getString("PROP_modules_autoload")); p.setShortDescription(bundle.getString("HINT_modules_autoload")); sse.put(p); p = new PropertySupport.Reflection(item, Boolean.TYPE, "isEager", null); // NOI18N p.setName("eager"); // NOI18N p.setDisplayName(bundle.getString("PROP_modules_eager")); p.setShortDescription(bundle.getString("HINT_modules_eager")); sse.put(p); p = new PropertySupport.Reflection(item, String[].class, "getProvides", null); // NOI18N p.setName("provides"); // NOI18N p.setValue("suppressCustomEditor",Boolean.TRUE); p.setDisplayName(bundle.getString("PROP_modules_provides")); p.setShortDescription(bundle.getString("HINT_modules_provides")); sse.put(p); // #16636: p = new PropertySupport.Reflection(item, String[].class, "getProblemDescriptions", null); // NOI18N p.setName("problemDescriptions"); // NOI18N p.setDisplayName(bundle.getString("PROP_modules_problemDescriptions")); p.setShortDescription(bundle.getString("HINT_modules_problemDescriptions")); sse.put(p); } } catch (NoSuchMethodException nsme) { ErrorManager.getDefault().notify(nsme); } sse.put(new UsedByProp()); return s; } private static Class getNbClassPathOrStringClass() { ClassLoader l = (ClassLoader)Lookup.getDefault().lookup(ClassLoader.class); try { return l.loadClass("org.openide.execution.NbClassPath"); // NOI18N } catch (ClassNotFoundException cnfe) { return String.class; } } /** For enabled modules, shows other modules which are currently * using it, i.e. depending on it to stay enabled. * @see #22504 */ private final class UsedByProp extends PropertySupport.ReadOnly implements ModuleBean.AllModulesBean.RelationCallback, PropertyChangeListener { private String[] value = null; public UsedByProp() { super("usedBy", String[].class, // NOI18N bundle.getString("PROP_modules_usedby"), bundle.getString("HINT_modules_usedby")); } public Object getValue() { if (value != null) { return value; } else { ModuleBean.AllModulesBean amb = ModuleBean.AllModulesBean.getDefault(); amb.getRelations(item, ModuleBean.AllModulesBean.RELATION_TRANSITIVELY_NEEDED_BY, this); // All sorts of changes can now affect this! if (!listeningToAllModules) { listeningToAllModules = true; amb.addPropertyChangeListener(org.openide.util.WeakListeners.propertyChange(this, amb)); ModuleBean[] mods = amb.getModules(); for (int i = 0; i < mods.length; i++) { if (mods[i] != item) { mods[i].addPropertyChangeListener(org.openide.util.WeakListeners.propertyChange(this, mods[i])); } } } return new String[] {bundle.getString("LBL_please_wait_modules_usedby")}; } } public void result(Set modules) { List l = new ArrayList(Math.max(modules.size(), 1)); // List |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.