1 19 package org.openide.explorer; 20 21 import org.netbeans.core.IDESettings; 22 import org.openide.nodes.Node; 23 import org.openide.util.HelpCtx; 24 import org.openide.util.NbBundle; 25 import org.openide.util.NbPreferences; 26 import org.openide.util.WeakListeners; 27 import org.openide.util.io.*; 28 import org.openide.windows.TopComponent; 29 import org.openide.windows.WindowManager; 30 import org.openide.windows.Workspace; 31 32 import java.awt.event.ActionEvent ; 33 import java.awt.event.ActionListener ; 34 35 import java.beans.PropertyChangeEvent ; 36 import java.beans.PropertyChangeListener ; 37 38 import java.io.*; 39 40 import java.text.MessageFormat ; 41 42 import javax.swing.Timer ; 43 44 45 58 public class ExplorerPanel extends TopComponent implements ExplorerManager.Provider { 59 60 static final long serialVersionUID = 5522528786650751459L; 61 62 63 private static MessageFormat formatExplorerTitle; 64 65 66 private static final int INIT_DELAY = 70; 67 68 69 private static final int MAX_DELAY = 350; 70 private static Boolean scheduleAcivatedNodes; 71 72 75 private static java.util.WeakHashMap panels = new java.util.WeakHashMap (); 76 77 78 private ExplorerManager manager; 79 80 81 transient private final PropertyChangeListener managerListener = new PropL(); 82 83 84 private ExplorerActions actions; 85 private transient DelayedSetter delayedSetter; 86 87 90 public ExplorerPanel(ExplorerManager manager) { 91 this(manager, null); 92 } 93 94 96 public ExplorerPanel() { 97 this(null, null); 98 } 99 100 103 ExplorerPanel(ExplorerManager manager, boolean confirm) { 104 this(manager, Boolean.valueOf(confirm)); 105 } 106 107 private ExplorerPanel(ExplorerManager manager, Boolean confirm) { 108 if (manager == null) { 109 manager = new ExplorerManager(); 110 } 111 112 this.manager = manager; 113 panels.put(manager, new java.lang.ref.WeakReference (this)); 114 115 setLayout(new java.awt.BorderLayout ()); 116 initActionMap(confirm); 117 initListening(); 118 } 119 120 123 124 private void initActionMap(Boolean confirm) { 125 ExplorerActions a = new ExplorerActions(false); 126 127 if (confirm != null) { 128 a.setConfirmDelete(confirm.booleanValue()); 129 } 130 131 a.attach(getExplorerManager()); 132 } 133 134 141 static void associateActions(ExplorerActions actions, ExplorerManager em) { 142 java.lang.ref.Reference ref = (java.lang.ref.Reference ) panels.get(em); 143 ExplorerPanel p = (ref == null) ? null : (ExplorerPanel) ref.get(); 144 145 if (p != null) { 146 p.getActionMap().put(javax.swing.text.DefaultEditorKit.copyAction, actions.copyAction()); 147 p.getActionMap().put(javax.swing.text.DefaultEditorKit.cutAction, actions.cutAction()); 148 p.getActionMap().put(javax.swing.text.DefaultEditorKit.pasteAction, actions.pasteAction()); 149 p.getActionMap().put("delete", actions.deleteAction() ); 151 152 p.actions = actions; 154 } 155 } 156 157 158 private void initListening() { 159 ExplorerManager man = getExplorerManager(); 161 man.addPropertyChangeListener(org.openide.util.WeakListeners.propertyChange(managerListener, man)); 162 setActivatedNodes(manager.getSelectedNodes()); 163 } 164 165 168 public void open() { 169 open(WindowManager.getDefault().getCurrentWorkspace()); 170 } 171 172 175 public void open(Workspace workspace) { 176 super.open(workspace); 177 setActivatedNodes(getExplorerManager().getSelectedNodes()); 178 updateTitle(); 179 } 180 181 184 public ExplorerManager getExplorerManager() { 185 return manager; 186 } 187 188 190 protected void componentActivated() { 191 if (actions != null) { 192 actions.attach(getExplorerManager()); 193 } 194 } 195 196 198 protected void componentDeactivated() { 199 if (actions != null) { 200 actions.detach(); 201 } 202 } 203 204 207 protected void updateTitle() { 208 String name = ""; 210 ExplorerManager em = getExplorerManager(); 211 212 if (em != null) { 213 Node n = em.getExploredContext(); 214 215 if (n != null) { 216 String nm = n.getDisplayName(); 217 218 if (nm != null) { 219 name = nm; 220 } 221 } 222 } 223 224 if (formatExplorerTitle == null) { 225 formatExplorerTitle = new MessageFormat (NbBundle.getMessage(ExplorerPanel.class, "explorerTitle")); 226 } 227 228 setName(formatExplorerTitle.format(new Object [] { name })); 229 } 230 231 236 public HelpCtx getHelpCtx() { 237 return getHelpCtx(getExplorerManager().getSelectedNodes(), new HelpCtx(ExplorerPanel.class)); 238 } 239 240 248 public static HelpCtx getHelpCtx(Node[] sel, HelpCtx def) { 249 return ExplorerUtils.getHelpCtx(sel, def); 250 } 251 252 255 public static void setConfirmDelete(boolean confirmDelete) { 256 NbPreferences.root().node("/org/netbeans/core").putBoolean("confirmDelete",confirmDelete); } 258 259 262 public static boolean isConfirmDelete() { 263 return NbPreferences.root().node("/org/netbeans/core").getBoolean("confirmDelete",true); } 265 266 267 public void writeExternal(ObjectOutput oo) throws IOException { 268 super.writeExternal(oo); 269 oo.writeObject(new NbMarshalledObject(manager)); 270 } 271 272 276 public void readExternal(ObjectInput oi) throws IOException, ClassNotFoundException { 277 super.readExternal(oi); 278 279 Object anObj = oi.readObject(); 280 281 if (anObj instanceof ExplorerManager) { 282 manager = (ExplorerManager) anObj; 283 panels.put(manager, new java.lang.ref.WeakReference (this)); 284 initActionMap(null); 285 initListening(); 286 287 return; 288 } 289 290 NbMarshalledObject obj = (NbMarshalledObject) anObj; 291 292 try { 294 manager = (ExplorerManager) obj.get(); 295 panels.put(manager, new java.lang.ref.WeakReference (this)); 296 initActionMap(null); 297 initListening(); 298 } catch (SafeException se) { 299 throw se; 300 } catch (IOException ioe) { 301 throw new SafeException(ioe); 302 } 303 } 304 305 private boolean delayActivatedNodes() { 307 if (scheduleAcivatedNodes == null) { 308 if (System.getProperty("netbeans.delay.tc") != null) { scheduleAcivatedNodes = Boolean.getBoolean("netbeans.delay.tc") ? Boolean.TRUE : Boolean.FALSE; } else { 311 scheduleAcivatedNodes = Boolean.FALSE; 312 } 313 } 314 315 return scheduleAcivatedNodes.booleanValue(); 316 } 317 318 private final void scheduleActivatedNodes(Node[] nodes) { 320 synchronized (this) { 321 if (delayedSetter == null) { 322 delayedSetter = new DelayedSetter(); 323 } 324 } 325 326 delayedSetter.scheduleActivatedNodes(nodes); 327 } 328 329 332 private final class PropL extends Object implements PropertyChangeListener { 333 PropL() { 334 } 335 336 public void propertyChange(PropertyChangeEvent evt) { 337 if (evt.getSource() != manager) { 338 return; 339 } 340 341 if (ExplorerManager.PROP_SELECTED_NODES.equals(evt.getPropertyName())) { 342 if (delayActivatedNodes()) { 343 scheduleActivatedNodes(manager.getSelectedNodes()); 344 } else { 345 setActivatedNodes(manager.getSelectedNodes()); 346 } 347 348 return; 349 } 350 351 if (ExplorerManager.PROP_EXPLORED_CONTEXT.equals(evt.getPropertyName())) { 352 updateTitle(); 353 354 return; 355 } 356 } 357 } 358 359 private class DelayedSetter implements ActionListener { 360 private Node[] nodes; 361 private Timer timer; 362 private boolean firstChange = true; 363 364 DelayedSetter() { 365 } 366 367 public void scheduleActivatedNodes(Node[] nodes) { 368 synchronized (this) { 369 this.nodes = nodes; 370 371 if (timer == null) { 372 timer = new Timer (INIT_DELAY, this); 374 timer.setCoalesce(true); 375 timer.setRepeats(false); 376 } 377 378 if (timer.isRunning()) { 379 if (timer.getInitialDelay() < MAX_DELAY) { 381 timer.setInitialDelay(timer.getInitialDelay() * 2); 382 } 383 384 firstChange = false; 385 } else { 386 setActivatedNodes(nodes); 388 firstChange = true; 389 } 390 391 timer.restart(); 393 } 394 } 395 396 public void actionPerformed(ActionEvent evt) { 397 synchronized (this) { 398 synchronized (this) { 399 timer.stop(); 400 } 401 } 402 403 if (!firstChange) { 405 setActivatedNodes(nodes); 406 } 407 } 408 } 409 } 410 | Popular Tags |