blob: b59af739bcdb8dc3bbcaddb787d2f0e7689d441f [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.ecommons.text.ui;
import org.eclipse.core.filesystem.IFileStore;
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.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 IFileStore}.
*/
public class OpenFileHyperlink implements IHyperlink {
private final IRegion region;
private final IFileStore file;
public OpenFileHyperlink(final IRegion region, final IFileStore file) {
this.region= 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_OpenFile_label, this.file.toString());
}
@Override
public void open() {
try {
IDE.openEditorOnFileStore(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 ));
}
}
}