/*******************************************************************************
* Copyright (c) 2009, 2010 Innovation Gate GmbH.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Innovation Gate GmbH - initial API and implementation
******************************************************************************/
package de.innovationgate.eclipse.wgadesigner.console;
import java.io.IOException;
import java.net.URL;
import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.text.TextSelection;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IViewReference;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.console.IConsoleConstants;
import org.eclipse.ui.console.IHyperlink;
import org.eclipse.ui.console.TextConsolePage;
import org.eclipse.ui.part.IPage;
import org.eclipse.ui.part.PageBookView;
import org.eclipse.ui.texteditor.ITextEditor;
import de.innovationgate.eclipse.utils.WorkbenchUtils;
import de.innovationgate.eclipse.utils.wga.WGADesignStructureHelper;
import de.innovationgate.eclipse.wgadesigner.WGADesignerPlugin;
import de.innovationgate.eclipse.wgadesigner.natures.WGARuntime;
import de.innovationgate.eclipse.wgadesigner.tomcat.TomcatUtils;
import de.innovationgate.eclipse.wgadesigner.utils.WGARemoteServices;
import de.innovationgate.utils.WGUtils;
import de.innovationgate.wga.config.ContentStore;
import de.innovationgate.wga.config.WGAConfiguration;
import de.innovationgate.wga.model.WGADesignConfigurationModel;
public class TMLWarningHyperLink implements IHyperlink {
private TMLWarning _warning;
public TMLWarningHyperLink(TMLWarning warning) {
_warning = warning;
}
public void linkActivated() {
try {
WGARuntime runtime = TomcatUtils.getInstance().getCurrentRuntime();
if (runtime != null) {
// determine designcontainer of selected tml warning
IContainer designContainer = null;
if (_warning.getDb() != null) {
if (!_warning.getDb().startsWith("plugin-")) {
WGAConfiguration wgaConfiguration = runtime.retrieveWGAConfig(false);
ContentStore cs = wgaConfiguration.getContentStore(_warning.getDb());
if (cs != null && cs.getDesign() != null && cs.getDesign().getSource() != null && cs.getDesign().getSource().equals("fs-designs")) {
IFolder[] designFolders = runtime.getDesignsAsFolder(true);
for (IFolder designFolder : designFolders) {
if (designFolder.getName().equals(cs.getDesign().getName())) {
designContainer = designFolder;
break;
}
}
}
} else {
IFolder[] pluginFolders = runtime.getPluginsAsFolder(true);
for (IFolder pluginFolder : pluginFolders) {
if (WGADesignStructureHelper.isDesignFolder(pluginFolder)) {
WGADesignStructureHelper helper = new WGADesignStructureHelper(pluginFolder);
WGADesignConfigurationModel model = helper.createModel();
if (model != null && model.hasPluginConfig()) {
String pluginUName = model.getPluginUniqueName();
if (pluginUName != null) {
String pluginDBKey = null;
if (pluginUName.contains(".")) {
pluginDBKey = "plugin-" + pluginUName.substring(pluginUName.lastIndexOf("."));
} else {
pluginDBKey = "plugin-" + pluginUName;
}
if (pluginDBKey != null && _warning.getDb().equalsIgnoreCase(pluginDBKey)) {
designContainer = pluginFolder;
break;
}
}
}
}
}
}
}
if (designContainer != null && WGADesignStructureHelper.isDesignFolder(designContainer)) {
WGADesignStructureHelper helper = new WGADesignStructureHelper(designContainer);
IFile file = helper.findReferencedTMLModule(_warning.getResource(), _warning.getMedium());
if (file != null && file.exists()) {
IEditorPart editor = WorkbenchUtils.openEditor(WGADesignerPlugin.getDefault().getWorkbench(), file);
if (editor instanceof ITextEditor) {
ITextEditor textEditor = (ITextEditor) editor;
int offset = 0;
if (_warning.getLine() != -1) {
offset = textEditor.getDocumentProvider().getDocument(textEditor.getEditorInput()).getLineOffset(_warning.getLine()-1);
}
textEditor.getSelectionProvider().setSelection(new TextSelection(offset, 0));
}
} else {
WorkbenchUtils.showErrorDialog(Display.getCurrent().getActiveShell(), "Unable to open tml warning hyperlink.", "Referenced tml file '" + _warning.getMedium() + "/" + _warning.getResource() + "' not found.");
}
} else {
WorkbenchUtils.showErrorDialog(Display.getCurrent().getActiveShell(), "Unable to open tml warning hyperlink.", "Design container not found.");
}
}
} catch (Exception e) {
WorkbenchUtils.showErrorDialog(WGADesignerPlugin.getDefault(), Display.getCurrent().getActiveShell(), "Unable to active TMLWarning-Hyperlink.", e);
}
}
public void linkEntered() {
StyledText textWidget = retrieveConsoleTextWidget();
if (textWidget != null) {
String message = _warning.getMessage();
try {
textWidget.setToolTipText(WGUtils.toPlainText(message, "\n", false));
} catch (IOException e) {
}
}
}
public void linkExited() {
StyledText textWidget = retrieveConsoleTextWidget();
if (textWidget != null) {
textWidget.setToolTipText("");
}
}
private StyledText retrieveConsoleTextWidget() {
IViewReference[] references = WGADesignerPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getActivePage().getViewReferences();
for (IViewReference ref : references) {
if (ref.getId().equals(IConsoleConstants.ID_CONSOLE_VIEW)) {
IWorkbenchPart part = ref.getPart(false);
if (part != null && part instanceof PageBookView) {
PageBookView view = (PageBookView) part;
IPage page = view.getCurrentPage();
if (page instanceof TextConsolePage) {
TextConsolePage textConsolePage = (TextConsolePage) page;
return textConsolePage.getViewer().getTextWidget();
}
}
}
}
return null;
}
}