KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > actions > FormatOperation


1 package org.eclipse.pde.internal.ui.editor.actions;
2
3 import java.lang.reflect.InvocationTargetException JavaDoc;
4 /*******************************************************************************
5  * Copyright (c) 2006, 2007 IBM Corporation and others.
6  * All rights reserved. This program and the accompanying materials
7  * are made available under the terms of the Eclipse Public License v1.0
8  * which accompanies this distribution, and is available at
9  * http://www.eclipse.org/legal/epl-v10.html
10  *
11  * Contributors:
12  * IBM Corporation - initial API and implementation
13  *******************************************************************************/

14 import java.util.Iterator JavaDoc;
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 JavaDoc[] fObjects;
38     
39     public FormatOperation(Object JavaDoc[] objects) {
40         fObjects = objects;
41     }
42     
43     public void run(IProgressMonitor mon) throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
44         mon.beginTask(PDEUIMessages.FormatManifestOperation_task, fObjects.length);
45         for (int i = 0; !mon.isCanceled() && i < fObjects.length; i++) {
46             Object JavaDoc obj = fObjects[i];
47             if (obj instanceof IFileEditorInput)
48                 obj = ((IFileEditorInput)obj).getFile();
49             if (obj instanceof IFile) {
50                 mon.subTask(NLS.bind(
51                         PDEUIMessages.FormatManifestOperation_subtask,
52                         ((IFile)obj).getFullPath().toString()));
53                 format((IFile)obj, mon);
54             }
55             mon.worked(1);
56         }
57     }
58     
59     public static void format(IFile file, IProgressMonitor mon) {
60         PDEModelUtility.modifyModel(new ModelModification(file) {
61             protected void modifyModel(IBaseModel model, IProgressMonitor monitor) throws CoreException {
62                 if (model instanceof IBundlePluginModelBase) {
63                     IBundleModel bundleModel = ((IBundlePluginModelBase)model).getBundleModel();
64                     if (bundleModel.getBundle() instanceof Bundle)
65                         formatBundle((Bundle)bundleModel.getBundle());
66                 } else if (model instanceof IPluginModelBase) {
67                     IPluginBase pluginModel = ((IPluginModelBase)model).getPluginBase();
68                     if (pluginModel instanceof PluginBaseNode)
69                         formatXML((PluginBaseNode)pluginModel);
70                 }
71             }
72             public boolean saveOpenEditor() {
73                 return false;
74             }
75         }, mon);
76     }
77     
78     private static void formatBundle(Bundle bundle) {
79         Iterator JavaDoc headers = bundle.getHeaders().values().iterator();
80         while (headers.hasNext())
81             ((IManifestHeader)headers.next()).update(true);
82         BundleModel model = (BundleModel)bundle.getModel();
83         model.adjustOffsets(model.getDocument());
84     }
85     
86     private static void formatXML(PluginBaseNode node) {
87         // TODO Auto-generated method stub
88

89     }
90 }
91
Popular Tags