1 19 package org.netbeans.modules.xml.catalog; 20 21 import java.awt.event.*; 22 import java.awt.*; 23 import java.beans.*; 24 import java.util.*; 25 import java.io.*; 26 import java.net.*; 27 28 import javax.swing.event.*; 29 30 import org.openide.*; 31 import org.openide.nodes.*; 32 import org.openide.util.actions.*; 33 import org.openide.util.datatransfer.*; 34 import org.openide.actions.*; 35 36 import org.netbeans.modules.xml.catalog.spi.*; 37 import org.netbeans.modules.xml.catalog.impl.*; 38 import org.netbeans.modules.xml.catalog.settings.CatalogSettings; 39 import org.openide.util.HelpCtx; 40 import java.awt.event.ActionEvent ; 41 import javax.swing.*; 42 43 60 public final class CatalogRootNode extends AbstractNode implements Node.Cookie { 61 62 63 public CatalogRootNode() { 64 super(new RootChildren()); 65 setName("XML-CATALOG"); setDisplayName (Util.THIS.getString("TEXT_catalog_root")); setIconBaseWithExtension("org/netbeans/modules/xml/catalog/resources/catalog-root.gif"); setShortDescription(Util.THIS.getString("PROP_catalog_root_desc")); 69 getCookieSet().add(this); 70 } 71 72 protected SystemAction[] createActions() { 73 return new SystemAction[] { 74 SystemAction.get(CatalogRootNode.MountAction.class), 75 null, 76 SystemAction.get(PropertiesAction.class) 77 }; 78 } 79 80 81 85 88 class CatalogMounter extends NewType implements ActionListener { 89 90 CatalogMounterModel model = null; 91 Dialog myDialog = null; 92 93 public void create() throws IOException { 94 95 Iterator it = ProvidersRegistry.getProviderClasses(new Class [] {CatalogReader.class}); 96 97 model = new CatalogMounterModel(it); 98 Object rpanel = new CatalogMounterPanel(model); 99 DialogDescriptor dd = new DialogDescriptor(rpanel, 100 Util.THIS.getString ("PROP_Mount_Catalog"), true, this); 101 dd.setHelpCtx(new HelpCtx(CatalogMounterPanel.class)); 102 myDialog = DialogDisplayer.getDefault().createDialog(dd); 103 104 106 JTextArea template = new JTextArea(); 107 template.setColumns(60); 108 template.setRows(8 + 2); Dimension dimension = template.getPreferredSize(); 110 111 final int insets = 12; 113 114 int heightInsets = dimension.height + 10 * insets; int widthInsets = dimension.width + 4 * insets; 118 Dimension fullDimension = new Dimension(widthInsets, heightInsets); 119 myDialog.setSize(fullDimension); 121 myDialog.setVisible(true); 122 } 123 124 public void actionPerformed(ActionEvent ae) { 125 if (ae.getSource() == DialogDescriptor.OK_OPTION) { 126 127 Object catalog = model.getCatalog(); 128 if (catalog == null) return; 129 CatalogSettings mounted = CatalogSettings.getDefault(); 130 mounted.addCatalog((CatalogReader)catalog); 131 132 } 133 if (myDialog != null) { 134 myDialog.dispose(); 135 myDialog = null; 136 } 137 } 138 139 public String getName() { 140 return Util.THIS.getString ("LBL_mount"); } 142 } 143 144 145 147 private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { 148 if ( Util.THIS.isLoggable() ) Util.THIS.debug("Reading CatalogRoot node " + this); 150 in.defaultReadObject(); 151 } 152 153 private void writeObject(ObjectOutputStream out) throws IOException { 154 if ( Util.THIS.isLoggable() ) Util.THIS.debug("Writing " + this); 156 out.defaultWriteObject(); 157 } 158 159 public HelpCtx getHelpCtx() { 160 return HelpCtx.DEFAULT_HELP; 162 } 163 164 166 167 171 private static class RootChildren extends Children.Keys implements Comparator, PropertyChangeListener { 172 173 174 private final TreeSet keys = new TreeSet(this); 175 176 179 public synchronized void addNotify() { 180 CatalogSettings mounted = CatalogSettings.getDefault(); 181 mounted.addPropertyChangeListener(this); 182 createKeys(mounted); 183 } 184 185 188 public synchronized void removeNotify() { 189 CatalogSettings mounted = CatalogSettings.getDefault(); 190 if (mounted != null) mounted.removePropertyChangeListener(this); 191 keys.clear(); 192 setKeys(keys); 193 } 194 195 198 public Node[] createNodes(Object key) { 199 try { 200 return new Node[] { new CatalogNode((CatalogReader)key) }; 201 } catch (IntrospectionException ex) { 202 return new Node[] {}; 203 } 204 } 205 206 210 public synchronized void propertyChange(PropertyChangeEvent e) { 211 if (CatalogSettings.PROP_MOUNTED_CATALOGS.equals(e.getPropertyName())) { 212 createKeys((CatalogSettings)e.getSource()); 213 } else if (CatalogSettings.PROP_PRJ_INSTANCE.equals(e.getPropertyName())) { 214 215 217 CatalogSettings mounted = (CatalogSettings) e.getOldValue(); 218 if (mounted != null) { 219 mounted.removePropertyChangeListener(this); 220 } 221 mounted = (CatalogSettings) e.getNewValue(); 222 if (mounted != null) mounted.addPropertyChangeListener(this); 223 createKeys(mounted); 224 } 225 } 226 227 230 private void createKeys(CatalogSettings mounted) { 231 keys.clear(); 232 if (mounted != null) { 233 Iterator it = mounted.getCatalogs(new Class [] {CatalogReader.class}); 234 while (it.hasNext()) { 235 keys.add(it.next()); } 239 } 240 setKeys(keys); 241 } 242 243 246 public boolean equals(java.lang.Object peer) { 247 return peer.getClass().equals(getClass()); 248 } 249 250 254 public int compare(java.lang.Object one,java.lang.Object two) { 255 if (one == two) return 0; 256 if (one instanceof SystemCatalogReader) return -1; 257 if (two instanceof SystemCatalogReader) return 1; 258 if (one instanceof CatalogDescriptor && two instanceof CatalogDescriptor) { 259 int test = (((CatalogDescriptor)one).getDisplayName()).compareTo( 260 ((CatalogDescriptor)two).getDisplayName() 261 ); 262 if (test != 0) return test; 263 264 } else { 265 if (one instanceof CatalogDescriptor) return -1; 266 if (two instanceof CatalogDescriptor) return 1; 267 } 268 return (long)one.hashCode() - (long)two.hashCode() > 0L ? 1 : -1; 270 } 271 272 } 273 274 275 278 private static final class MountAction extends NodeAction { 279 280 private static final long serialVersionUID = -3608629636833099065L; 281 282 public MountAction() { 283 } 284 285 public String getName() { 286 return Util.THIS.getString("LBL_mount"); 287 } 288 289 public HelpCtx getHelpCtx() { 290 return new HelpCtx(MountAction.class); 291 } 292 293 protected synchronized boolean enable(Node[] activatedNodes) { 294 return activatedNodes.length > 0; 295 } 296 297 protected boolean asynchronous() { 298 return false; 299 } 300 301 protected synchronized void performAction(Node[] activatedNodes) { 302 if (enable(activatedNodes) == false) return; 303 try { 304 Node current = activatedNodes[0]; 305 CatalogRootNode me = (CatalogRootNode) current.getCookie(CatalogRootNode.class); 306 CatalogMounter newType = me.new CatalogMounter(); 307 newType.create(); 308 } catch (IOException ex) { 309 Util.THIS.debug(ex); 310 } finally { 311 } 312 } 313 } 314 315 } 316 | Popular Tags |