|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2025 Christoph Läubrich and others. |
| 3 | + * |
| 4 | + * This program and the accompanying materials |
| 5 | + * are made available under the terms of the Eclipse Public License 2.0 |
| 6 | + * which accompanies this distribution, and is available at |
| 7 | + * https://www.eclipse.org/legal/epl-2.0/ |
| 8 | + * |
| 9 | + * SPDX-License-Identifier: EPL-2.0 |
| 10 | + * |
| 11 | + * Contributors: |
| 12 | + * Christoph Läubrich - initial API and implementation |
| 13 | + *******************************************************************************/ |
| 14 | +package org.eclipse.pde.internal.ui.editor.targetdefinition; |
| 15 | + |
| 16 | +import java.io.ByteArrayInputStream; |
| 17 | +import java.nio.charset.StandardCharsets; |
| 18 | +import java.util.Map; |
| 19 | + |
| 20 | +import org.eclipse.core.runtime.ILog; |
| 21 | +import org.eclipse.core.runtime.Status; |
| 22 | +import org.eclipse.core.runtime.jobs.Job; |
| 23 | +import org.eclipse.equinox.frameworkadmin.BundleInfo; |
| 24 | +import org.eclipse.osgi.service.resolver.BundleDescription; |
| 25 | +import org.eclipse.osgi.service.resolver.State; |
| 26 | +import org.eclipse.osgi.service.resolver.StateObjectFactory; |
| 27 | +import org.eclipse.osgi.util.ManifestElement; |
| 28 | +import org.eclipse.pde.core.target.ITargetDefinition; |
| 29 | +import org.eclipse.pde.core.target.TargetBundle; |
| 30 | +import org.eclipse.pde.internal.build.BundleHelper; |
| 31 | +import org.eclipse.pde.internal.core.PDECore; |
| 32 | +import org.eclipse.pde.internal.ui.PDEUIMessages; |
| 33 | +import org.eclipse.pde.internal.ui.views.target.StateTree; |
| 34 | +import org.eclipse.swt.widgets.Composite; |
| 35 | +import org.eclipse.swt.widgets.Control; |
| 36 | +import org.eclipse.ui.PlatformUI; |
| 37 | +import org.eclipse.ui.forms.editor.FormPage; |
| 38 | +import org.osgi.framework.FrameworkUtil; |
| 39 | + |
| 40 | +public class StatePage extends FormPage { |
| 41 | + |
| 42 | + public static final String PAGE_ID = "state"; //$NON-NLS-1$ |
| 43 | + |
| 44 | + private State state; |
| 45 | + |
| 46 | + private StateTree stateTree; |
| 47 | + |
| 48 | + private ITargetDefinition target; |
| 49 | + |
| 50 | + private Job job; |
| 51 | + |
| 52 | + private boolean active; |
| 53 | + |
| 54 | + public StatePage(TargetEditor editor) { |
| 55 | + super(editor, PAGE_ID, PDEUIMessages.StatePage_title); |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public void createPartControl(Composite parent) { |
| 60 | + stateTree = new StateTree(parent); |
| 61 | + stateTree.setInput(state); |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public Control getPartControl() { |
| 66 | + return stateTree; |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public void setActive(boolean active) { |
| 71 | + this.active = active; |
| 72 | + super.setActive(active); |
| 73 | + loadState(); |
| 74 | + } |
| 75 | + |
| 76 | + private void loadState() { |
| 77 | + if (!active || state != null || target == null) { |
| 78 | + return; |
| 79 | + } |
| 80 | + TargetBundle[] targetBundles = target.getBundles(); |
| 81 | + if (targetBundles == null || targetBundles.length == 0) { |
| 82 | + return; |
| 83 | + } |
| 84 | + job = Job.create("Compute Target State", monitor -> { //$NON-NLS-1$ |
| 85 | + try { |
| 86 | + State targetState = BundleHelper.getPlatformAdmin().getFactory().createState(true); |
| 87 | + targetState.setPlatformProperties( |
| 88 | + PDECore.getDefault().getModelManager().getState().getState().getPlatformProperties()); |
| 89 | + StateObjectFactory factory = targetState.getFactory(); |
| 90 | + long id = 1; |
| 91 | + for (TargetBundle targetBundle : targetBundles) { |
| 92 | + if (targetBundle.isSourceBundle()) { |
| 93 | + continue; |
| 94 | + } |
| 95 | + BundleInfo bundleInfo = targetBundle.getBundleInfo(); |
| 96 | + String manifest = bundleInfo.getManifest(); |
| 97 | + if (manifest != null) { |
| 98 | + Map<String, String> bundleManifest; |
| 99 | + try (ByteArrayInputStream stream = new ByteArrayInputStream( |
| 100 | + manifest.getBytes(StandardCharsets.UTF_8))) { |
| 101 | + bundleManifest = ManifestElement.parseBundleManifest(stream); |
| 102 | + } |
| 103 | + BundleDescription bundleDescription = factory.createBundleDescription(targetState, |
| 104 | + FrameworkUtil.asDictionary(bundleManifest), |
| 105 | + String.valueOf(targetBundle.getBundleInfo().getLocation()), id++); |
| 106 | + targetState.addBundle(bundleDescription); |
| 107 | + } |
| 108 | + if (monitor.isCanceled()) { |
| 109 | + return Status.CANCEL_STATUS; |
| 110 | + } |
| 111 | + } |
| 112 | + targetState.resolve(false); |
| 113 | + if (monitor.isCanceled()) { |
| 114 | + return Status.CANCEL_STATUS; |
| 115 | + } |
| 116 | + PlatformUI.getWorkbench().getDisplay().execute(() -> { |
| 117 | + state = targetState; |
| 118 | + if (stateTree != null) { |
| 119 | + stateTree.setInput(targetState); |
| 120 | + } |
| 121 | + }); |
| 122 | + } catch (Exception e) { |
| 123 | + ILog.get().error("Computing target state failed!", e); //$NON-NLS-1$ |
| 124 | + } |
| 125 | + return Status.OK_STATUS; |
| 126 | + }); |
| 127 | + job.schedule(); |
| 128 | + } |
| 129 | + |
| 130 | + public void reset() { |
| 131 | + PlatformUI.getWorkbench().getDisplay().execute(() -> { |
| 132 | + update(null); |
| 133 | + if (stateTree != null) { |
| 134 | + stateTree.setInput(null); |
| 135 | + } |
| 136 | + }); |
| 137 | + } |
| 138 | + |
| 139 | + protected void update(ITargetDefinition target) { |
| 140 | + if (job != null) { |
| 141 | + job.cancel(); |
| 142 | + job = null; |
| 143 | + } |
| 144 | + this.state = null; |
| 145 | + this.target = target; |
| 146 | + } |
| 147 | + |
| 148 | + public void updateTarget(ITargetDefinition target) { |
| 149 | + PlatformUI.getWorkbench().getDisplay().execute(() -> { |
| 150 | + update(target); |
| 151 | + loadState(); |
| 152 | + }); |
| 153 | + } |
| 154 | +} |
0 commit comments