blob: d22b8bb4940cd9717a512ecda81b3e03c4e024fe [file] [log] [blame]
/*=============================================================================#
# Copyright (c) 2009, 2021 Stephan Wahlbrink and others.
#
# This program and the accompanying materials are made available under the
# terms of the Eclipse Public License 2.0 which is available at
# https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
# which is available at https://www.apache.org/licenses/LICENSE-2.0.
#
# SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
#
# Contributors:
# Stephan Wahlbrink <sw@wahlbrink.eu> - initial API and implementation
#=============================================================================*/
package org.eclipse.statet.ltk.ui.util;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.hyperlink.IHyperlink;
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.ide.IDE;
import org.eclipse.ui.statushandlers.StatusManager;
import org.eclipse.statet.jcommons.text.core.TextRegion;
import org.eclipse.statet.ecommons.text.core.JFaceTextRegion;
import org.eclipse.statet.ecommons.ui.SharedUIResources;
import org.eclipse.statet.ecommons.ui.util.UIAccess;
import org.eclipse.statet.internal.ltk.ui.EditingMessages;
/**
* Hyperlink opening an editor for a {@link IFile}.
*/
public class OpenWorkspaceFileHyperlink implements IHyperlink {
private final IRegion region;
private final IFile file;
public OpenWorkspaceFileHyperlink(final TextRegion region, final IFile file) {
this.region= JFaceTextRegion.toJFaceRegion(region);
this.file= file;
}
@Override
public String getTypeLabel() {
return null;
}
@Override
public IRegion getHyperlinkRegion() {
return this.region;
}
@Override
public String getHyperlinkText() {
return NLS.bind(EditingMessages.Hyperlink_OpenFile2_label,
this.file.getName(), this.file.getParent().getFullPath() );
}
@Override
public void open() {
try {
IDE.openEditor(UIAccess.getActiveWorkbenchPage(true), this.file);
}
catch (final PartInitException e) {
Display.getCurrent().beep();
StatusManager.getManager().handle(new Status(IStatus.INFO, SharedUIResources.BUNDLE_ID, -1,
NLS.bind(EditingMessages.Hyperlink_OpenFile_error_message, this.file.toString()),
e ));
}
}
}