KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > ant > debugger > breakpoints > PersistenceManager


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.ant.debugger.breakpoints;
21
22 import java.beans.PropertyChangeEvent JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import org.netbeans.api.debugger.Breakpoint;
25 import org.netbeans.api.debugger.DebuggerEngine;
26
27 import org.netbeans.api.debugger.DebuggerManager;
28 import org.netbeans.api.debugger.DebuggerManagerListener;
29 import org.netbeans.api.debugger.LazyDebuggerManagerListener;
30 import org.netbeans.api.debugger.Properties;
31 import org.netbeans.api.debugger.Session;
32 import org.netbeans.api.debugger.Watch;
33
34 /**
35  * Listens on DebuggerManager and:
36  * - loads all breakpoints & watches on startup
37  * - listens on all changes of breakpoints and watches (like breakoint / watch
38  * added / removed, or some property change) and saves a new values
39  *
40  * @author Jan Jancura
41  */

42 public class PersistenceManager implements LazyDebuggerManagerListener {
43     
44     public Breakpoint[] initBreakpoints () {
45         Properties p = Properties.getDefault ().getProperties ("debugger").
46             getProperties (DebuggerManager.PROP_BREAKPOINTS);
47         Breakpoint[] breakpoints = (Breakpoint[]) p.getArray (
48             "ant",
49             new Breakpoint [0]
50         );
51         for (int i = 0; i < breakpoints.length; i++) {
52             breakpoints[i].addPropertyChangeListener(this);
53         }
54         return breakpoints;
55     }
56     
57     public void initWatches () {
58     }
59     
60     public String JavaDoc[] getProperties () {
61         return new String JavaDoc [] {
62             DebuggerManager.PROP_BREAKPOINTS_INIT,
63             DebuggerManager.PROP_BREAKPOINTS,
64         };
65     }
66     
67     public void breakpointAdded (Breakpoint breakpoint) {
68         if (breakpoint instanceof AntBreakpoint) {
69             Properties p = Properties.getDefault ().getProperties ("debugger").
70                 getProperties (DebuggerManager.PROP_BREAKPOINTS);
71             p.setArray (
72                 "ant",
73                 getBreakpoints ()
74             );
75             breakpoint.addPropertyChangeListener(this);
76         }
77     }
78
79     public void breakpointRemoved (Breakpoint breakpoint) {
80         if (breakpoint instanceof AntBreakpoint) {
81             Properties p = Properties.getDefault ().getProperties ("debugger").
82                 getProperties (DebuggerManager.PROP_BREAKPOINTS);
83             p.setArray (
84                 "ant",
85                 getBreakpoints ()
86             );
87             breakpoint.removePropertyChangeListener(this);
88         }
89     }
90     public void watchAdded (Watch watch) {
91     }
92     
93     public void watchRemoved (Watch watch) {
94     }
95     
96     public void propertyChange (PropertyChangeEvent JavaDoc evt) {
97         if (evt.getSource() instanceof Breakpoint) {
98             Properties.getDefault ().getProperties ("debugger").
99                 getProperties (DebuggerManager.PROP_BREAKPOINTS).setArray (
100                     "ant",
101                     getBreakpoints ()
102                 );
103         }
104     }
105     
106     public void sessionAdded (Session session) {}
107     public void sessionRemoved (Session session) {}
108     public void engineAdded (DebuggerEngine engine) {}
109     public void engineRemoved (DebuggerEngine engine) {}
110     
111     
112     private static Breakpoint[] getBreakpoints () {
113         Breakpoint[] bs = DebuggerManager.getDebuggerManager ().
114             getBreakpoints ();
115         int i, k = bs.length;
116         ArrayList JavaDoc bb = new ArrayList JavaDoc ();
117         for (i = 0; i < k; i++)
118             // Don't store hidden breakpoints
119
if (bs[i] instanceof AntBreakpoint)
120                 bb.add (bs [i]);
121         bs = new Breakpoint [bb.size ()];
122         return (Breakpoint[]) bb.toArray (bs);
123     }
124 }
125
126
Popular Tags