|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2024 Vector Informatik GmbH and others. |
| 3 | + * |
| 4 | + * This program and the accompanying materials are made available under the terms of the Eclipse |
| 5 | + * Public License 2.0 which accompanies this distribution, and is available at |
| 6 | + * https://www.eclipse.org/legal/epl-2.0/ |
| 7 | + * |
| 8 | + * SPDX-License-Identifier: EPL-2.0 |
| 9 | + * |
| 10 | + * Contributors: Vector Informatik GmbH - initial API and implementation |
| 11 | + *******************************************************************************/ |
| 12 | + |
| 13 | +package org.eclipse.ui.ide.fileSystem.zip; |
| 14 | + |
| 15 | +import java.lang.reflect.InvocationTargetException; |
| 16 | +import java.net.URISyntaxException; |
| 17 | + |
| 18 | +import org.eclipse.core.commands.AbstractHandler; |
| 19 | +import org.eclipse.core.commands.ExecutionEvent; |
| 20 | +import org.eclipse.core.resources.IFile; |
| 21 | +import org.eclipse.core.resources.ZipFileTransformer; |
| 22 | +import org.eclipse.core.runtime.CoreException; |
| 23 | +import org.eclipse.core.runtime.IProgressMonitor; |
| 24 | +import org.eclipse.jface.dialogs.MessageDialog; |
| 25 | +import org.eclipse.jface.dialogs.ProgressMonitorDialog; |
| 26 | +import org.eclipse.jface.operation.IRunnableWithProgress; |
| 27 | +import org.eclipse.jface.viewers.ISelection; |
| 28 | +import org.eclipse.jface.viewers.IStructuredSelection; |
| 29 | +import org.eclipse.swt.widgets.Shell; |
| 30 | +import org.eclipse.ui.handlers.HandlerUtil; |
| 31 | + |
| 32 | +/** |
| 33 | + * This class represents a handler for opening zip files. |
| 34 | + * |
| 35 | + * @since 3.132 |
| 36 | + */ |
| 37 | +public class OpenZipFileHandler extends AbstractHandler { |
| 38 | + |
| 39 | + /** |
| 40 | + * Executes the handler action, which involves opening a zip file selected by |
| 41 | + * the user. |
| 42 | + * |
| 43 | + * @param event The event triggering the execution of this handler. |
| 44 | + */ |
| 45 | + @Override |
| 46 | + public Object execute(ExecutionEvent event) { |
| 47 | + Shell shell = HandlerUtil.getActiveShell(event); |
| 48 | + ProgressMonitorDialog dialog = new ProgressMonitorDialog(shell); |
| 49 | + ISelection selection = HandlerUtil.getCurrentSelection(event); |
| 50 | + if (!(selection instanceof IStructuredSelection)) { |
| 51 | + return null; |
| 52 | + } |
| 53 | + |
| 54 | + Object element = ((IStructuredSelection) selection).getFirstElement(); |
| 55 | + |
| 56 | + if (!(element instanceof IFile)) { |
| 57 | + return null; |
| 58 | + } |
| 59 | + try { |
| 60 | + dialog.run(true, false, new IRunnableWithProgress() { |
| 61 | + @Override |
| 62 | + public void run(IProgressMonitor monitor) throws InterruptedException { |
| 63 | + monitor.beginTask("Opening Zip File", 5); //$NON-NLS-1$ |
| 64 | + try { |
| 65 | + ZipFileTransformer.openZipFile((IFile) element, monitor, true); |
| 66 | + } catch (URISyntaxException | CoreException e) { |
| 67 | + throw new InterruptedException(e.getMessage()); |
| 68 | + } |
| 69 | + monitor.worked(1); |
| 70 | + } |
| 71 | + }); |
| 72 | + } catch (InterruptedException | InvocationTargetException e) { |
| 73 | + MessageDialog.openError(shell, "Error opening zip file", e.getMessage()); //$NON-NLS-1$ |
| 74 | + } |
| 75 | + return null; |
| 76 | + } |
| 77 | +} |
0 commit comments