KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > ui > actions > ImportBreakpointsOperation


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.debug.ui.actions;
13
14 import java.io.FileInputStream JavaDoc;
15 import java.io.FileNotFoundException JavaDoc;
16 import java.io.InputStreamReader JavaDoc;
17 import java.io.UnsupportedEncodingException JavaDoc;
18 import java.lang.reflect.InvocationTargetException JavaDoc;
19 import com.ibm.icu.text.MessageFormat;
20 import java.util.ArrayList JavaDoc;
21
22 import org.eclipse.core.resources.IMarker;
23 import org.eclipse.core.resources.IResource;
24 import org.eclipse.core.resources.IWorkspaceRoot;
25 import org.eclipse.core.resources.IWorkspaceRunnable;
26 import org.eclipse.core.resources.ResourcesPlugin;
27 import org.eclipse.core.runtime.CoreException;
28 import org.eclipse.core.runtime.IAdaptable;
29 import org.eclipse.core.runtime.IProgressMonitor;
30 import org.eclipse.core.runtime.IStatus;
31 import org.eclipse.core.runtime.Status;
32 import org.eclipse.debug.core.DebugPlugin;
33 import org.eclipse.debug.core.model.IBreakpoint;
34 import org.eclipse.debug.internal.core.BreakpointManager;
35 import org.eclipse.debug.internal.ui.IInternalDebugUIConstants;
36 import org.eclipse.debug.internal.ui.importexport.breakpoints.IImportExportConstants;
37 import org.eclipse.debug.internal.ui.importexport.breakpoints.ImportExportMessages;
38 import org.eclipse.debug.ui.IDebugUIConstants;
39 import org.eclipse.jface.operation.IRunnableWithProgress;
40 import org.eclipse.ui.IMemento;
41 import org.eclipse.ui.IWorkingSet;
42 import org.eclipse.ui.IWorkingSetManager;
43 import org.eclipse.ui.PlatformUI;
44 import org.eclipse.ui.XMLMemento;
45
46 /**
47  * Imports breakpoints from a file into the workspace.
48  * <p>
49  * This class may be instantiated; not intended to be subclassed.
50  * <p>
51  * @since 3.2
52  */

53 public class ImportBreakpointsOperation implements IRunnableWithProgress {
54
55     private boolean fOverwriteAll = false;
56
57     private String JavaDoc fFileName = null;
58
59     private boolean fCreateWorkingSets = false;
60
61     private ArrayList JavaDoc fAdded = new ArrayList JavaDoc();
62
63     private BreakpointManager fManager = (BreakpointManager) DebugPlugin.getDefault().getBreakpointManager();
64
65     /**
66      * Constructs an operation to import breakpoints.
67      *
68      * @param fileName the file to read breakpoints from - the file should have been
69      * created from an export operation
70      * @param overwrite whether imported breakpoints will overwrite existing equivalent breakpoints
71      * @param createWorkingSets whether breakpoint working sets should be created. Breakpoints
72      * are exported with information about the breakpoint working sets they belong to. Those
73      * working sets can be optionally re-created on import if they do not already exist in the
74      * workspace.
75      */

76     public ImportBreakpointsOperation(String JavaDoc fileName, boolean overwrite, boolean createWorkingSets) {
77         fFileName = fileName;
78         fOverwriteAll = overwrite;
79         fCreateWorkingSets = createWorkingSets;
80     }
81
82     /* (non-Javadoc)
83      * @see org.eclipse.core.resources.IWorkspaceRunnable#run(org.eclipse.core.runtime.IProgressMonitor)
84      */

85     public void run(final IProgressMonitor monitor) throws InvocationTargetException JavaDoc {
86         IWorkspaceRunnable wr = new IWorkspaceRunnable() {
87             public void run(IProgressMonitor wmonitor) throws CoreException {
88                 try {
89                     XMLMemento memento = XMLMemento.createReadRoot(new InputStreamReader JavaDoc(new FileInputStream JavaDoc(fFileName), "UTF-8")); //$NON-NLS-1$
90
IMemento[] nodes = memento.getChildren(IImportExportConstants.IE_NODE_BREAKPOINT);
91                     IWorkspaceRoot workspace = ResourcesPlugin.getWorkspace().getRoot();
92                     IMemento node = null;
93                     monitor.beginTask(ImportExportMessages.ImportOperation_0, nodes.length);
94                     for(int i = 0; i < nodes.length; i++) {
95                         if(!monitor.isCanceled()) {
96                             node = nodes[i].getChild(IImportExportConstants.IE_NODE_RESOURCE);
97                             IResource resource = workspace.findMember(node.getString(IImportExportConstants.IE_NODE_PATH));
98                             // filter resource breakpoints that do not exist in this workspace
99
if(resource != null) {
100                                 // create a marker, we must do each one, as a straight copy set values as Objects, destroying
101
// the actual value types that they are.
102
node = nodes[i].getChild(IImportExportConstants.IE_NODE_MARKER);
103                                 IMarker marker = findGeneralMarker(resource, node.getString(IMarker.LINE_NUMBER),
104                                         node.getString(IImportExportConstants.IE_NODE_TYPE),
105                                         node.getInteger(IImportExportConstants.CHARSTART));
106                                 if(marker == null) {
107                                     marker = resource.createMarker(node.getString(IImportExportConstants.IE_NODE_TYPE));
108                                     restoreBreakpoint(marker, nodes[i]);
109                                 }
110                                 else {
111                                     if(fOverwriteAll) {
112                                         marker.setAttributes(null);
113                                         restoreBreakpoint(marker, nodes[i]);
114                                     }
115                                 }
116                             }
117                             monitor.worked(i+1);
118                         } else {
119                             return;
120                         }
121                     }
122                     fManager.addBreakpoints((IBreakpoint[])fAdded.toArray(new IBreakpoint[fAdded.size()]));
123                 }
124                 catch(FileNotFoundException JavaDoc e) {
125                     throw new CoreException(new Status(IStatus.ERROR, IDebugUIConstants.PLUGIN_ID, IDebugUIConstants.INTERNAL_ERROR,
126                             MessageFormat.format(ImportExportMessages.ImportBreakpointsOperation_0, new String JavaDoc[]{fFileName}), e));
127                 }
128                 catch (UnsupportedEncodingException JavaDoc e) {
129                     throw new CoreException(new Status(IStatus.ERROR, IDebugUIConstants.PLUGIN_ID, IDebugUIConstants.INTERNAL_ERROR,
130                             MessageFormat.format(ImportExportMessages.ImportBreakpointsOperation_1, new String JavaDoc[]{fFileName}), e));
131                 }
132             }
133         };
134         try {
135             ResourcesPlugin.getWorkspace().run(wr, monitor);
136         } catch(CoreException e) {
137             throw new InvocationTargetException JavaDoc(e);
138         }
139     }
140
141     /**
142      * Restores a breakpoint on the given marker with information from the passed memento
143      * @param marker the marker to restore to
144      * @param node the memento to get the restore information from
145      */

146     private void restoreBreakpoint(IMarker marker, IMemento node) throws CoreException {
147         IMemento[] childnodes = null;
148         IMemento child = null;
149         // get the marker attributes
150
child = node.getChild(IImportExportConstants.IE_NODE_MARKER);
151         marker.setAttribute(IMarker.LINE_NUMBER, child.getInteger(IMarker.LINE_NUMBER));
152         marker.setAttribute(IImportExportConstants.IE_NODE_TYPE, child.getString(IImportExportConstants.IE_NODE_TYPE));
153         marker.setAttribute(IImportExportConstants.CHARSTART, child.getString(IImportExportConstants.CHARSTART));
154         childnodes = child.getChildren(IImportExportConstants.IE_NODE_ATTRIB);
155         String JavaDoc workingsets = ""; //$NON-NLS-1$
156
for (int j = 0; j < childnodes.length; j++) {
157             // get the attribute and try to convert it to either Integer, Boolean or leave it alone (String)
158
String JavaDoc name = childnodes[j].getString(IImportExportConstants.IE_NODE_NAME),
159                    value = childnodes[j].getString(IImportExportConstants.IE_NODE_VALUE);
160             if (value != null & name != null) {
161                 if (name.equals(IInternalDebugUIConstants.WORKING_SET_NAME)) {
162                     workingsets = value;
163                 }
164                 try {
165                     marker.setAttribute(name, Integer.valueOf(value));
166                 } catch (NumberFormatException JavaDoc e) {
167                     if (value.equalsIgnoreCase("false") || value.equalsIgnoreCase("true")) { //$NON-NLS-1$ //$NON-NLS-2$
168
marker.setAttribute(name, Boolean.valueOf(value));
169                     }
170                     else {
171                         marker.setAttribute(name, value);
172                     }
173                 }
174             }
175         }
176         // create the breakpoint
177
IBreakpoint breakpoint = fManager.createBreakpoint(marker);
178         breakpoint.setEnabled(Boolean.valueOf(node.getString(IImportExportConstants.IE_BP_ENABLED)).booleanValue());
179         breakpoint.setPersisted(Boolean.valueOf(node.getString(IImportExportConstants.IE_BP_PERSISTANT)).booleanValue());
180         breakpoint.setRegistered(Boolean.valueOf(node.getString(IImportExportConstants.IE_BP_REGISTERED)).booleanValue());
181         // bug fix 110080
182
fAdded.add(breakpoint);
183         if (fCreateWorkingSets) {
184             String JavaDoc[] names = workingsets.split("\\" + IImportExportConstants.DELIMITER); //$NON-NLS-1$
185
for (int m = 1; m < names.length; m++) {
186                 createWorkingSet(names[m], breakpoint);
187             }
188         }
189     }
190
191     /**
192      * Creates a working set and sets the values
193      * @param breakpoint the restored breakpoint to add to the new working set
194      */

195     private void createWorkingSet(String JavaDoc setname, IAdaptable element) {
196         IWorkingSetManager wsmanager = PlatformUI.getWorkbench().getWorkingSetManager();
197         IWorkingSet set = wsmanager.getWorkingSet(setname);
198         if (set == null) {
199             set = wsmanager.createWorkingSet(setname, new IAdaptable[] {});
200             set.setId(IDebugUIConstants.BREAKPOINT_WORKINGSET_ID);
201             wsmanager.addWorkingSet(set);
202         }
203         if (!setContainsBreakpoint(set, (IBreakpoint) element)) {
204             IAdaptable[] elements = set.getElements();
205             IAdaptable[] newElements = new IAdaptable[elements.length + 1];
206             newElements[newElements.length - 1] = element;
207             System.arraycopy(elements, 0, newElements, 0, elements.length);
208             set.setElements(newElements);
209         }
210     }
211
212     /**
213      * Method to ensure markers and breakpoints are not both added to the working set
214      * @param set the set to check
215      * @param breakpoint the breakpoint to check for existence
216      * @return true if it is present false otherwise
217      */

218     private boolean setContainsBreakpoint(IWorkingSet set, IBreakpoint breakpoint) {
219         IAdaptable[] elements = set.getElements();
220         for (int i = 0; i < elements.length; i++) {
221             if (elements[i].equals(breakpoint)) {
222                 return true;
223             }
224         }
225         return false;
226     }
227
228     /**
229      * This method is used internally to find a non-specific marker on a given resource.
230      * With this method we can search for similar markers even though they may have differing IDs
231      *
232      * @param resource the resource to search for the marker
233      * @param line the line number or <code>null</code>
234      * @param type the type of the marker
235      * @param charstart the charstart attribute of the marker or <code>null</code>
236      * @return the marker if found, or <code>null</code>
237      */

238     private IMarker findGeneralMarker(IResource resource, String JavaDoc line, String JavaDoc type, Integer JavaDoc charstart) throws CoreException {
239         IMarker[] markers = resource.findMarkers(null, false, IResource.DEPTH_ZERO);
240         if (type != null) {
241             for (int i = 0; i < markers.length; i++) {
242                 Object JavaDoc localline = markers[i].getAttribute(IMarker.LINE_NUMBER);
243                 String JavaDoc localtype = markers[i].getType();
244                 if (type.equals(localtype)) {
245                     if (localline != null & line != null) {
246                         if (line.equals(localline.toString())) {
247                             Integer JavaDoc markerCharstart = (Integer JavaDoc) markers[i].getAttribute(IImportExportConstants.CHARSTART);
248                             if (charstart == null) {
249                                 if (markerCharstart == null) {
250                                     return markers[i];
251                                 }
252                             } else if (charstart.equals(markerCharstart)) {
253                                 return markers[i];
254                             }
255                         }
256                     } else {
257                         return markers[i];
258                     }
259                 }
260             }
261         }
262         return null;
263     }
264 }
265
Popular Tags