01: package org.eclipse.pde.internal.ui.editor.actions;
02:
03: import java.lang.reflect.InvocationTargetException;
04: /*******************************************************************************
05: * Copyright (c) 2006, 2007 IBM Corporation and others.
06: * All rights reserved. This program and the accompanying materials
07: * are made available under the terms of the Eclipse Public License v1.0
08: * which accompanies this distribution, and is available at
09: * http://www.eclipse.org/legal/epl-v10.html
10: *
11: * Contributors:
12: * IBM Corporation - initial API and implementation
13: *******************************************************************************/
14: import java.util.Iterator;
15:
16: import org.eclipse.core.resources.IFile;
17: import org.eclipse.core.runtime.CoreException;
18: import org.eclipse.core.runtime.IProgressMonitor;
19: import org.eclipse.jface.operation.IRunnableWithProgress;
20: import org.eclipse.osgi.util.NLS;
21: import org.eclipse.pde.core.IBaseModel;
22: import org.eclipse.pde.core.plugin.IPluginBase;
23: import org.eclipse.pde.core.plugin.IPluginModelBase;
24: import org.eclipse.pde.internal.core.ibundle.IBundleModel;
25: import org.eclipse.pde.internal.core.ibundle.IBundlePluginModelBase;
26: import org.eclipse.pde.internal.core.ibundle.IManifestHeader;
27: import org.eclipse.pde.internal.core.text.bundle.Bundle;
28: import org.eclipse.pde.internal.core.text.bundle.BundleModel;
29: import org.eclipse.pde.internal.core.text.plugin.PluginBaseNode;
30: import org.eclipse.pde.internal.ui.PDEUIMessages;
31: import org.eclipse.pde.internal.ui.util.ModelModification;
32: import org.eclipse.pde.internal.ui.util.PDEModelUtility;
33: import org.eclipse.ui.IFileEditorInput;
34:
35: public class FormatOperation implements IRunnableWithProgress {
36:
37: private Object[] fObjects;
38:
39: public FormatOperation(Object[] objects) {
40: fObjects = objects;
41: }
42:
43: public void run(IProgressMonitor mon)
44: throws InvocationTargetException, InterruptedException {
45: mon.beginTask(PDEUIMessages.FormatManifestOperation_task,
46: fObjects.length);
47: for (int i = 0; !mon.isCanceled() && i < fObjects.length; i++) {
48: Object obj = fObjects[i];
49: if (obj instanceof IFileEditorInput)
50: obj = ((IFileEditorInput) obj).getFile();
51: if (obj instanceof IFile) {
52: mon.subTask(NLS.bind(
53: PDEUIMessages.FormatManifestOperation_subtask,
54: ((IFile) obj).getFullPath().toString()));
55: format((IFile) obj, mon);
56: }
57: mon.worked(1);
58: }
59: }
60:
61: public static void format(IFile file, IProgressMonitor mon) {
62: PDEModelUtility.modifyModel(new ModelModification(file) {
63: protected void modifyModel(IBaseModel model,
64: IProgressMonitor monitor) throws CoreException {
65: if (model instanceof IBundlePluginModelBase) {
66: IBundleModel bundleModel = ((IBundlePluginModelBase) model)
67: .getBundleModel();
68: if (bundleModel.getBundle() instanceof Bundle)
69: formatBundle((Bundle) bundleModel.getBundle());
70: } else if (model instanceof IPluginModelBase) {
71: IPluginBase pluginModel = ((IPluginModelBase) model)
72: .getPluginBase();
73: if (pluginModel instanceof PluginBaseNode)
74: formatXML((PluginBaseNode) pluginModel);
75: }
76: }
77:
78: public boolean saveOpenEditor() {
79: return false;
80: }
81: }, mon);
82: }
83:
84: private static void formatBundle(Bundle bundle) {
85: Iterator headers = bundle.getHeaders().values().iterator();
86: while (headers.hasNext())
87: ((IManifestHeader) headers.next()).update(true);
88: BundleModel model = (BundleModel) bundle.getModel();
89: model.adjustOffsets(model.getDocument());
90: }
91:
92: private static void formatXML(PluginBaseNode node) {
93: // TODO Auto-generated method stub
94:
95: }
96: }
|