From e4175b34b10be243440749e989a5bf503e07d0b1 Mon Sep 17 00:00:00 2001 From: serge-rider Date: Tue, 29 May 2018 00:48:49 +0300 Subject: [PATCH] UI classes refactoring Former-commit-id: eea126f396c8273983ecd2ab47ae549b44c3f752 --- .../src/org/jkiss/dbeaver/core/DBeaverUI.java | 2 +- .../src/org/jkiss/dbeaver/ui/ImageUtils.java | 4 - .../org/jkiss/dbeaver/ui/AbstractUIJob.java | 90 +-- .../src/org/jkiss/dbeaver/ui/ActionUtils.java | 610 +++++++++--------- .../org/jkiss/dbeaver/ui/ClipboardData.java | 0 .../ui/CompositeSelectionProvider.java | 244 +++---- .../dbeaver/ui/CustomSelectionProvider.java | 128 ++-- .../org/jkiss/dbeaver/ui/DBIconBinary.java | 132 ++-- .../org/jkiss/dbeaver/ui/DBeaverIcons.java | 440 ++++++------- .../dbeaver/ui/DynamicFindReplaceTarget.java | 368 +++++------ .../dbeaver/ui/OverlayImageDescriptor.java | 0 .../ui/OverlayImageDescriptorLegacy.java | 0 .../org/jkiss/dbeaver/ui/ProxyPageSite.java | 204 +++--- .../jkiss/dbeaver/ui/SharedTextColors.java | 240 +++---- .../org/jkiss/dbeaver/ui/TableToolTip.java | 246 +++---- 15 files changed, 1352 insertions(+), 1356 deletions(-) rename plugins/{org.jkiss.dbeaver.core => org.jkiss.dbeaver.ui}/src/org/jkiss/dbeaver/ui/AbstractUIJob.java (96%) rename plugins/{org.jkiss.dbeaver.core => org.jkiss.dbeaver.ui}/src/org/jkiss/dbeaver/ui/ActionUtils.java (97%) rename plugins/{org.jkiss.dbeaver.core => org.jkiss.dbeaver.ui}/src/org/jkiss/dbeaver/ui/ClipboardData.java (100%) rename plugins/{org.jkiss.dbeaver.core => org.jkiss.dbeaver.ui}/src/org/jkiss/dbeaver/ui/CompositeSelectionProvider.java (96%) rename plugins/{org.jkiss.dbeaver.core => org.jkiss.dbeaver.ui}/src/org/jkiss/dbeaver/ui/CustomSelectionProvider.java (96%) rename plugins/{org.jkiss.dbeaver.core => org.jkiss.dbeaver.ui}/src/org/jkiss/dbeaver/ui/DBIconBinary.java (96%) rename plugins/{org.jkiss.dbeaver.core => org.jkiss.dbeaver.ui}/src/org/jkiss/dbeaver/ui/DBeaverIcons.java (97%) rename plugins/{org.jkiss.dbeaver.core => org.jkiss.dbeaver.ui}/src/org/jkiss/dbeaver/ui/DynamicFindReplaceTarget.java (96%) rename plugins/{org.jkiss.dbeaver.core => org.jkiss.dbeaver.ui}/src/org/jkiss/dbeaver/ui/OverlayImageDescriptor.java (100%) rename plugins/{org.jkiss.dbeaver.core => org.jkiss.dbeaver.ui}/src/org/jkiss/dbeaver/ui/OverlayImageDescriptorLegacy.java (100%) rename plugins/{org.jkiss.dbeaver.core => org.jkiss.dbeaver.ui}/src/org/jkiss/dbeaver/ui/ProxyPageSite.java (96%) rename plugins/{org.jkiss.dbeaver.core => org.jkiss.dbeaver.ui}/src/org/jkiss/dbeaver/ui/SharedTextColors.java (96%) rename plugins/{org.jkiss.dbeaver.core => org.jkiss.dbeaver.ui}/src/org/jkiss/dbeaver/ui/TableToolTip.java (97%) diff --git a/plugins/org.jkiss.dbeaver.core/src/org/jkiss/dbeaver/core/DBeaverUI.java b/plugins/org.jkiss.dbeaver.core/src/org/jkiss/dbeaver/core/DBeaverUI.java index 2eace570c6..d23f5da543 100644 --- a/plugins/org.jkiss.dbeaver.core/src/org/jkiss/dbeaver/core/DBeaverUI.java +++ b/plugins/org.jkiss.dbeaver.core/src/org/jkiss/dbeaver/core/DBeaverUI.java @@ -242,7 +242,7 @@ public class DBeaverUI implements DBPPlatformUI { */ public static void runInProgressService(final DBRRunnableWithProgress runnable) throws InvocationTargetException, InterruptedException { - getDefaultRunnableContext().run(true, true, runnable::run); + getDefaultRunnableContext().run(true, true, runnable); } /** diff --git a/plugins/org.jkiss.dbeaver.core/src/org/jkiss/dbeaver/ui/ImageUtils.java b/plugins/org.jkiss.dbeaver.core/src/org/jkiss/dbeaver/ui/ImageUtils.java index 427b78affc..332fa44cf9 100644 --- a/plugins/org.jkiss.dbeaver.core/src/org/jkiss/dbeaver/ui/ImageUtils.java +++ b/plugins/org.jkiss.dbeaver.core/src/org/jkiss/dbeaver/ui/ImageUtils.java @@ -20,11 +20,7 @@ package org.jkiss.dbeaver.ui; import org.eclipse.core.runtime.Assert; import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.*; -import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Control; -import org.eclipse.swt.widgets.Shell; -import org.jkiss.dbeaver.core.DBeaverCore; -import org.jkiss.dbeaver.core.DBeaverUI; /** * Image-related utils diff --git a/plugins/org.jkiss.dbeaver.core/src/org/jkiss/dbeaver/ui/AbstractUIJob.java b/plugins/org.jkiss.dbeaver.ui/src/org/jkiss/dbeaver/ui/AbstractUIJob.java similarity index 96% rename from plugins/org.jkiss.dbeaver.core/src/org/jkiss/dbeaver/ui/AbstractUIJob.java rename to plugins/org.jkiss.dbeaver.ui/src/org/jkiss/dbeaver/ui/AbstractUIJob.java index f3410e86d9..a26ba16a3b 100644 --- a/plugins/org.jkiss.dbeaver.core/src/org/jkiss/dbeaver/ui/AbstractUIJob.java +++ b/plugins/org.jkiss.dbeaver.ui/src/org/jkiss/dbeaver/ui/AbstractUIJob.java @@ -1,46 +1,46 @@ -/* - * DBeaver - Universal Database Manager - * Copyright (C) 2010-2017 Serge Rider (serge@jkiss.org) - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.jkiss.dbeaver.ui; - -import org.eclipse.core.runtime.IProgressMonitor; -import org.eclipse.core.runtime.IStatus; -import org.eclipse.ui.progress.UIJob; -import org.jkiss.dbeaver.Log; -import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor; -import org.jkiss.dbeaver.utils.RuntimeUtils; - -/** - * Abstract Database Job - */ -public abstract class AbstractUIJob extends UIJob -{ - static protected final Log log = Log.getLog(AbstractUIJob.class); - - protected AbstractUIJob(String name) - { - super(name); - } - - @Override - public IStatus runInUIThread(IProgressMonitor monitor) - { - return this.runInUIThread(RuntimeUtils.makeMonitor(monitor)); - } - - protected abstract IStatus runInUIThread(DBRProgressMonitor monitor); - +/* + * DBeaver - Universal Database Manager + * Copyright (C) 2010-2017 Serge Rider (serge@jkiss.org) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jkiss.dbeaver.ui; + +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.ui.progress.UIJob; +import org.jkiss.dbeaver.Log; +import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor; +import org.jkiss.dbeaver.utils.RuntimeUtils; + +/** + * Abstract Database Job + */ +public abstract class AbstractUIJob extends UIJob +{ + static protected final Log log = Log.getLog(AbstractUIJob.class); + + protected AbstractUIJob(String name) + { + super(name); + } + + @Override + public IStatus runInUIThread(IProgressMonitor monitor) + { + return this.runInUIThread(RuntimeUtils.makeMonitor(monitor)); + } + + protected abstract IStatus runInUIThread(DBRProgressMonitor monitor); + } \ No newline at end of file diff --git a/plugins/org.jkiss.dbeaver.core/src/org/jkiss/dbeaver/ui/ActionUtils.java b/plugins/org.jkiss.dbeaver.ui/src/org/jkiss/dbeaver/ui/ActionUtils.java similarity index 97% rename from plugins/org.jkiss.dbeaver.core/src/org/jkiss/dbeaver/ui/ActionUtils.java rename to plugins/org.jkiss.dbeaver.ui/src/org/jkiss/dbeaver/ui/ActionUtils.java index 76b648bf39..bfc332b318 100644 --- a/plugins/org.jkiss.dbeaver.core/src/org/jkiss/dbeaver/ui/ActionUtils.java +++ b/plugins/org.jkiss.dbeaver.ui/src/org/jkiss/dbeaver/ui/ActionUtils.java @@ -1,305 +1,305 @@ -/* - * DBeaver - Universal Database Manager - * Copyright (C) 2010-2017 Serge Rider (serge@jkiss.org) - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.jkiss.dbeaver.ui; - -import org.eclipse.core.commands.Command; -import org.eclipse.core.commands.ParameterizedCommand; -import org.eclipse.core.commands.common.NotDefinedException; -import org.eclipse.core.expressions.EvaluationContext; -import org.eclipse.core.expressions.IEvaluationContext; -import org.eclipse.jface.action.Action; -import org.eclipse.jface.action.ActionContributionItem; -import org.eclipse.jface.action.ContributionItem; -import org.eclipse.jface.action.IAction; -import org.eclipse.jface.bindings.Binding; -import org.eclipse.jface.bindings.TriggerSequence; -import org.eclipse.jface.resource.ImageDescriptor; -import org.eclipse.jface.viewers.ISelection; -import org.eclipse.jface.viewers.IStructuredSelection; -import org.eclipse.ui.*; -import org.eclipse.ui.commands.ICommandService; -import org.eclipse.ui.handlers.IHandlerService; -import org.eclipse.ui.keys.IBindingService; -import org.eclipse.ui.menus.CommandContributionItem; -import org.eclipse.ui.menus.CommandContributionItemParameter; -import org.eclipse.ui.services.IEvaluationService; -import org.eclipse.ui.services.IServiceLocator; -import org.jkiss.code.NotNull; -import org.jkiss.code.Nullable; -import org.jkiss.dbeaver.Log; -import org.jkiss.dbeaver.model.DBPImage; - -/** - * Action utils - */ -public class ActionUtils -{ - private static final Log log = Log.getLog(ActionUtils.class); - - public static CommandContributionItem makeCommandContribution(@NotNull IServiceLocator serviceLocator, @NotNull String commandId) - { - return makeCommandContribution(serviceLocator, commandId, CommandContributionItem.STYLE_PUSH); - } - - public static CommandContributionItem makeCommandContribution(@NotNull IServiceLocator serviceLocator, @NotNull String commandId, int style) - { - return new CommandContributionItem(new CommandContributionItemParameter( - serviceLocator, - null, - commandId, - style)); - } - - public static CommandContributionItem makeCommandContribution(@NotNull IServiceLocator serviceLocator, @NotNull String commandId, int style, @Nullable DBPImage icon) - { - CommandContributionItemParameter parameters = new CommandContributionItemParameter( - serviceLocator, - null, - commandId, - style); - parameters.icon = DBeaverIcons.getImageDescriptor(icon); - return new CommandContributionItem(parameters); - } - - public static CommandContributionItem makeCommandContribution(IServiceLocator serviceLocator, String commandId, String name, DBPImage image) - { - return makeCommandContribution(serviceLocator, commandId, name, image, null, false); - } - - public static ContributionItem makeActionContribution( - @NotNull IAction action, - boolean showText) - { - ActionContributionItem item = new ActionContributionItem(action); - if (showText) { - item.setMode(ActionContributionItem.MODE_FORCE_TEXT); - } - return item; - } - - public static CommandContributionItem makeCommandContribution( - @NotNull IServiceLocator serviceLocator, - @NotNull String commandId, - @Nullable String name, - @Nullable DBPImage image, - @Nullable String toolTip, - boolean showText) - { - final CommandContributionItemParameter contributionParameters = new CommandContributionItemParameter( - serviceLocator, - null, - commandId, - null, - image == null ? null : DBeaverIcons.getImageDescriptor(image), - null, - null, - name, - null, - toolTip, - CommandContributionItem.STYLE_PUSH, - null, - false); - if (showText) { - contributionParameters.mode = CommandContributionItem.MODE_FORCE_TEXT; - } - return new CommandContributionItem(contributionParameters); - } - - public static boolean isCommandEnabled(String commandId, IWorkbenchPartSite site) - { - if (commandId != null && site != null) { - try { - //Command cmd = new Command(); - ICommandService commandService = site.getService(ICommandService.class); - if (commandService != null) { - Command command = commandService.getCommand(commandId); - return command != null && command.isEnabled(); - } - } catch (Exception e) { - log.error("Can't execute command '" + commandId + "'", e); - } - } - return false; - } - - @Nullable - public static String findCommandName(String commandId) - { - ICommandService commandService = PlatformUI.getWorkbench().getService(ICommandService.class); - if (commandService != null) { - Command command = commandService.getCommand(commandId); - if (command != null && command.isDefined()) { - try { - return command.getName(); - } catch (NotDefinedException e) { - log.debug(e); - } - } - } - return "???"; - } - - @Nullable - public static String findCommandDescription(String commandId, IServiceLocator serviceLocator, boolean shortcutOnly) - { - String commandName = null; - String shortcut = null; - ICommandService commandService = serviceLocator.getService(ICommandService.class); - if (commandService != null) { - Command command = commandService.getCommand(commandId); - if (command != null && command.isDefined()) { - try { - commandName = command.getName(); - } catch (NotDefinedException e) { - log.debug(e); - } - } - } - IBindingService bindingService = serviceLocator.getService(IBindingService.class); - if (bindingService != null) { - TriggerSequence sequence = null; - for (Binding b : bindingService.getBindings()) { - ParameterizedCommand parameterizedCommand = b.getParameterizedCommand(); - if (parameterizedCommand != null && commandId.equals(parameterizedCommand.getId())) { - sequence = b.getTriggerSequence(); - } - } - if (sequence == null) { - sequence = bindingService.getBestActiveBindingFor(commandId); - } - if (sequence != null) { - shortcut = sequence.format(); - } - } - if (shortcutOnly) { - return shortcut == null ? "?" : shortcut; - } - if (shortcut == null) { - return commandName; - } - if (commandName == null) { - return shortcut; - } - return commandName + " (" + shortcut + ")"; - } - - public static void runCommand(String commandId, IServiceLocator serviceLocator) - { - runCommand(commandId, null, serviceLocator); - } - - public static void runCommand(String commandId, ISelection selection, IServiceLocator serviceLocator) - { - if (commandId != null) { - try { - ICommandService commandService = serviceLocator.getService(ICommandService.class); - IHandlerService handlerService = serviceLocator.getService(IHandlerService.class); - if (commandService != null) { - Command command = commandService.getCommand(commandId); - boolean needContextPatch = false; - if (selection != null) { - needContextPatch = true; - if (serviceLocator instanceof IWorkbenchSite) { - final ISelection curSelection = ((IWorkbenchSite) serviceLocator).getSelectionProvider().getSelection(); - if (curSelection instanceof IStructuredSelection && selection instanceof IStructuredSelection) { - if (((IStructuredSelection) curSelection).size() == ((IStructuredSelection) selection).size() && - ((IStructuredSelection) curSelection).getFirstElement() == ((IStructuredSelection) selection).getFirstElement()) - { - // The same selection - needContextPatch = false; - } - } - } - } - if (selection != null && needContextPatch) { - // Create new eval context - IEvaluationContext context = new EvaluationContext( - handlerService.createContextSnapshot(false), selection); - if (serviceLocator instanceof IWorkbenchPartSite) { - context.addVariable(ISources.ACTIVE_PART_NAME, ((IWorkbenchPartSite) serviceLocator).getPart()); - } - context.addVariable(ISources.ACTIVE_CURRENT_SELECTION_NAME, selection); - - ParameterizedCommand pc = new ParameterizedCommand(command, null); - handlerService.executeCommandInContext(pc, null, context); - } else if (command != null) { - if (command.isEnabled()) { - handlerService.executeCommand(commandId, null); - } else { - log.warn("Command '" + commandId + "' is disabled"); - } - } else { - log.warn("Command '" + commandId + "' not found"); - } - } - } catch (Exception e) { - log.error("Can't execute command '" + commandId + "'", e); - } - } - } - - public static IAction makeAction( - @NotNull final IActionDelegate actionDelegate, - @Nullable IWorkbenchSite site, - @Nullable ISelection selection, - @Nullable String text, - @Nullable ImageDescriptor image, - @Nullable String toolTip) - { - Action actionImpl = new Action() { - @Override - public void run() { - actionDelegate.run(this); - } - }; - if (text != null) { - actionImpl.setText(text); - } - if (image != null) { - actionImpl.setImageDescriptor(image); - } - if (toolTip != null) { - actionImpl.setToolTipText(toolTip); - } - - actionDelegate.selectionChanged(actionImpl, selection); - - if (site != null) { - if (actionDelegate instanceof IObjectActionDelegate && site instanceof IWorkbenchPartSite) { - ((IObjectActionDelegate)actionDelegate).setActivePart(actionImpl, ((IWorkbenchPartSite) site).getPart()); - } else if (actionDelegate instanceof IWorkbenchWindowActionDelegate) { - ((IWorkbenchWindowActionDelegate)actionDelegate).init(site.getWorkbenchWindow()); - } - } - - return actionImpl; - } - - public static void evaluatePropertyState(String propertyName) - { - IEvaluationService service = PlatformUI.getWorkbench().getService(IEvaluationService.class); - if (service != null) { - try { - service.requestEvaluation(propertyName); - } catch (Exception e) { - log.warn("Error evaluating property [" + propertyName + "]"); - } - } - } - -} +/* + * DBeaver - Universal Database Manager + * Copyright (C) 2010-2017 Serge Rider (serge@jkiss.org) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jkiss.dbeaver.ui; + +import org.eclipse.core.commands.Command; +import org.eclipse.core.commands.ParameterizedCommand; +import org.eclipse.core.commands.common.NotDefinedException; +import org.eclipse.core.expressions.EvaluationContext; +import org.eclipse.core.expressions.IEvaluationContext; +import org.eclipse.jface.action.Action; +import org.eclipse.jface.action.ActionContributionItem; +import org.eclipse.jface.action.ContributionItem; +import org.eclipse.jface.action.IAction; +import org.eclipse.jface.bindings.Binding; +import org.eclipse.jface.bindings.TriggerSequence; +import org.eclipse.jface.resource.ImageDescriptor; +import org.eclipse.jface.viewers.ISelection; +import org.eclipse.jface.viewers.IStructuredSelection; +import org.eclipse.ui.*; +import org.eclipse.ui.commands.ICommandService; +import org.eclipse.ui.handlers.IHandlerService; +import org.eclipse.ui.keys.IBindingService; +import org.eclipse.ui.menus.CommandContributionItem; +import org.eclipse.ui.menus.CommandContributionItemParameter; +import org.eclipse.ui.services.IEvaluationService; +import org.eclipse.ui.services.IServiceLocator; +import org.jkiss.code.NotNull; +import org.jkiss.code.Nullable; +import org.jkiss.dbeaver.Log; +import org.jkiss.dbeaver.model.DBPImage; + +/** + * Action utils + */ +public class ActionUtils +{ + private static final Log log = Log.getLog(ActionUtils.class); + + public static CommandContributionItem makeCommandContribution(@NotNull IServiceLocator serviceLocator, @NotNull String commandId) + { + return makeCommandContribution(serviceLocator, commandId, CommandContributionItem.STYLE_PUSH); + } + + public static CommandContributionItem makeCommandContribution(@NotNull IServiceLocator serviceLocator, @NotNull String commandId, int style) + { + return new CommandContributionItem(new CommandContributionItemParameter( + serviceLocator, + null, + commandId, + style)); + } + + public static CommandContributionItem makeCommandContribution(@NotNull IServiceLocator serviceLocator, @NotNull String commandId, int style, @Nullable DBPImage icon) + { + CommandContributionItemParameter parameters = new CommandContributionItemParameter( + serviceLocator, + null, + commandId, + style); + parameters.icon = DBeaverIcons.getImageDescriptor(icon); + return new CommandContributionItem(parameters); + } + + public static CommandContributionItem makeCommandContribution(IServiceLocator serviceLocator, String commandId, String name, DBPImage image) + { + return makeCommandContribution(serviceLocator, commandId, name, image, null, false); + } + + public static ContributionItem makeActionContribution( + @NotNull IAction action, + boolean showText) + { + ActionContributionItem item = new ActionContributionItem(action); + if (showText) { + item.setMode(ActionContributionItem.MODE_FORCE_TEXT); + } + return item; + } + + public static CommandContributionItem makeCommandContribution( + @NotNull IServiceLocator serviceLocator, + @NotNull String commandId, + @Nullable String name, + @Nullable DBPImage image, + @Nullable String toolTip, + boolean showText) + { + final CommandContributionItemParameter contributionParameters = new CommandContributionItemParameter( + serviceLocator, + null, + commandId, + null, + image == null ? null : DBeaverIcons.getImageDescriptor(image), + null, + null, + name, + null, + toolTip, + CommandContributionItem.STYLE_PUSH, + null, + false); + if (showText) { + contributionParameters.mode = CommandContributionItem.MODE_FORCE_TEXT; + } + return new CommandContributionItem(contributionParameters); + } + + public static boolean isCommandEnabled(String commandId, IWorkbenchPartSite site) + { + if (commandId != null && site != null) { + try { + //Command cmd = new Command(); + ICommandService commandService = site.getService(ICommandService.class); + if (commandService != null) { + Command command = commandService.getCommand(commandId); + return command != null && command.isEnabled(); + } + } catch (Exception e) { + log.error("Can't execute command '" + commandId + "'", e); + } + } + return false; + } + + @Nullable + public static String findCommandName(String commandId) + { + ICommandService commandService = PlatformUI.getWorkbench().getService(ICommandService.class); + if (commandService != null) { + Command command = commandService.getCommand(commandId); + if (command != null && command.isDefined()) { + try { + return command.getName(); + } catch (NotDefinedException e) { + log.debug(e); + } + } + } + return "???"; + } + + @Nullable + public static String findCommandDescription(String commandId, IServiceLocator serviceLocator, boolean shortcutOnly) + { + String commandName = null; + String shortcut = null; + ICommandService commandService = serviceLocator.getService(ICommandService.class); + if (commandService != null) { + Command command = commandService.getCommand(commandId); + if (command != null && command.isDefined()) { + try { + commandName = command.getName(); + } catch (NotDefinedException e) { + log.debug(e); + } + } + } + IBindingService bindingService = serviceLocator.getService(IBindingService.class); + if (bindingService != null) { + TriggerSequence sequence = null; + for (Binding b : bindingService.getBindings()) { + ParameterizedCommand parameterizedCommand = b.getParameterizedCommand(); + if (parameterizedCommand != null && commandId.equals(parameterizedCommand.getId())) { + sequence = b.getTriggerSequence(); + } + } + if (sequence == null) { + sequence = bindingService.getBestActiveBindingFor(commandId); + } + if (sequence != null) { + shortcut = sequence.format(); + } + } + if (shortcutOnly) { + return shortcut == null ? "?" : shortcut; + } + if (shortcut == null) { + return commandName; + } + if (commandName == null) { + return shortcut; + } + return commandName + " (" + shortcut + ")"; + } + + public static void runCommand(String commandId, IServiceLocator serviceLocator) + { + runCommand(commandId, null, serviceLocator); + } + + public static void runCommand(String commandId, ISelection selection, IServiceLocator serviceLocator) + { + if (commandId != null) { + try { + ICommandService commandService = serviceLocator.getService(ICommandService.class); + IHandlerService handlerService = serviceLocator.getService(IHandlerService.class); + if (commandService != null) { + Command command = commandService.getCommand(commandId); + boolean needContextPatch = false; + if (selection != null) { + needContextPatch = true; + if (serviceLocator instanceof IWorkbenchSite) { + final ISelection curSelection = ((IWorkbenchSite) serviceLocator).getSelectionProvider().getSelection(); + if (curSelection instanceof IStructuredSelection && selection instanceof IStructuredSelection) { + if (((IStructuredSelection) curSelection).size() == ((IStructuredSelection) selection).size() && + ((IStructuredSelection) curSelection).getFirstElement() == ((IStructuredSelection) selection).getFirstElement()) + { + // The same selection + needContextPatch = false; + } + } + } + } + if (selection != null && needContextPatch) { + // Create new eval context + IEvaluationContext context = new EvaluationContext( + handlerService.createContextSnapshot(false), selection); + if (serviceLocator instanceof IWorkbenchPartSite) { + context.addVariable(ISources.ACTIVE_PART_NAME, ((IWorkbenchPartSite) serviceLocator).getPart()); + } + context.addVariable(ISources.ACTIVE_CURRENT_SELECTION_NAME, selection); + + ParameterizedCommand pc = new ParameterizedCommand(command, null); + handlerService.executeCommandInContext(pc, null, context); + } else if (command != null) { + if (command.isEnabled()) { + handlerService.executeCommand(commandId, null); + } else { + log.warn("Command '" + commandId + "' is disabled"); + } + } else { + log.warn("Command '" + commandId + "' not found"); + } + } + } catch (Exception e) { + log.error("Can't execute command '" + commandId + "'", e); + } + } + } + + public static IAction makeAction( + @NotNull final IActionDelegate actionDelegate, + @Nullable IWorkbenchSite site, + @Nullable ISelection selection, + @Nullable String text, + @Nullable ImageDescriptor image, + @Nullable String toolTip) + { + Action actionImpl = new Action() { + @Override + public void run() { + actionDelegate.run(this); + } + }; + if (text != null) { + actionImpl.setText(text); + } + if (image != null) { + actionImpl.setImageDescriptor(image); + } + if (toolTip != null) { + actionImpl.setToolTipText(toolTip); + } + + actionDelegate.selectionChanged(actionImpl, selection); + + if (site != null) { + if (actionDelegate instanceof IObjectActionDelegate && site instanceof IWorkbenchPartSite) { + ((IObjectActionDelegate)actionDelegate).setActivePart(actionImpl, ((IWorkbenchPartSite) site).getPart()); + } else if (actionDelegate instanceof IWorkbenchWindowActionDelegate) { + ((IWorkbenchWindowActionDelegate)actionDelegate).init(site.getWorkbenchWindow()); + } + } + + return actionImpl; + } + + public static void evaluatePropertyState(String propertyName) + { + IEvaluationService service = PlatformUI.getWorkbench().getService(IEvaluationService.class); + if (service != null) { + try { + service.requestEvaluation(propertyName); + } catch (Exception e) { + log.warn("Error evaluating property [" + propertyName + "]"); + } + } + } + +} diff --git a/plugins/org.jkiss.dbeaver.core/src/org/jkiss/dbeaver/ui/ClipboardData.java b/plugins/org.jkiss.dbeaver.ui/src/org/jkiss/dbeaver/ui/ClipboardData.java similarity index 100% rename from plugins/org.jkiss.dbeaver.core/src/org/jkiss/dbeaver/ui/ClipboardData.java rename to plugins/org.jkiss.dbeaver.ui/src/org/jkiss/dbeaver/ui/ClipboardData.java diff --git a/plugins/org.jkiss.dbeaver.core/src/org/jkiss/dbeaver/ui/CompositeSelectionProvider.java b/plugins/org.jkiss.dbeaver.ui/src/org/jkiss/dbeaver/ui/CompositeSelectionProvider.java similarity index 96% rename from plugins/org.jkiss.dbeaver.core/src/org/jkiss/dbeaver/ui/CompositeSelectionProvider.java rename to plugins/org.jkiss.dbeaver.ui/src/org/jkiss/dbeaver/ui/CompositeSelectionProvider.java index 1ca5d03b88..c43dcddbe1 100644 --- a/plugins/org.jkiss.dbeaver.core/src/org/jkiss/dbeaver/ui/CompositeSelectionProvider.java +++ b/plugins/org.jkiss.dbeaver.ui/src/org/jkiss/dbeaver/ui/CompositeSelectionProvider.java @@ -1,122 +1,122 @@ -/* - * DBeaver - Universal Database Manager - * Copyright (C) 2010-2017 Serge Rider (serge@jkiss.org) - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.jkiss.dbeaver.ui; - -import org.eclipse.jface.viewers.*; -import org.eclipse.swt.events.FocusAdapter; -import org.eclipse.swt.events.FocusEvent; -import org.eclipse.swt.widgets.Control; -import org.jkiss.utils.CommonUtils; - -import java.util.ArrayList; -import java.util.List; - -/** - * Composite selection provider - */ -public class CompositeSelectionProvider implements ISelectionProvider { - - private List listeners = new ArrayList<>(); - private ISelectionProvider provider; - private ISelection selection = StructuredSelection.EMPTY; - - public void trackProvider(final Control control, final ISelectionProvider selectionProvider) - { - control.addFocusListener(new FocusAdapter() { - @Override - public void focusGained(FocusEvent e) - { - setProvider(selectionProvider); - } - }); - } - - @Override - public void addSelectionChangedListener(ISelectionChangedListener listener) - { - listeners.add(listener); - } - - @Override - public void removeSelectionChangedListener(ISelectionChangedListener listener) - { - listeners.remove(listener); - } - - @Override - public ISelection getSelection() - { - ISelectionProvider cp = getProvider(); - if (cp != null) { - return cp.getSelection(); - } else { - return selection; - } - } - - @Override - public void setSelection(ISelection selection) - { - ISelectionProvider cp = getProvider(); - if (cp != null) { - cp.setSelection(selection); - } else { - this.selection = selection; - if (!CommonUtils.isEmpty(listeners)) { - SelectionChangedEvent event = new SelectionChangedEvent(this, selection); - for (ISelectionChangedListener listener : listeners) { - listener.selectionChanged(event); - } - } - } - } - - public ISelectionProvider getProvider() - { - return provider; - } - - public void setProvider(ISelectionProvider newProvider) - { - if (this.provider != newProvider){ - ISelection newSelection = null; - if (!CommonUtils.isEmpty(listeners)) { - if (this.provider != null){ - for (ISelectionChangedListener listener : listeners) { - this.provider.removeSelectionChangedListener(listener); - } - } - - if (newProvider != null) { - for (ISelectionChangedListener listener : listeners) { - newProvider.addSelectionChangedListener(listener); - } - - newSelection = newProvider.getSelection(); - } else { - newSelection = this.selection; - } - } - this.provider = newProvider; - - if (newSelection != null){ - //force a selection change event propagation - setSelection(newSelection); - } - } - } -} +/* + * DBeaver - Universal Database Manager + * Copyright (C) 2010-2017 Serge Rider (serge@jkiss.org) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jkiss.dbeaver.ui; + +import org.eclipse.jface.viewers.*; +import org.eclipse.swt.events.FocusAdapter; +import org.eclipse.swt.events.FocusEvent; +import org.eclipse.swt.widgets.Control; +import org.jkiss.utils.CommonUtils; + +import java.util.ArrayList; +import java.util.List; + +/** + * Composite selection provider + */ +public class CompositeSelectionProvider implements ISelectionProvider { + + private List listeners = new ArrayList<>(); + private ISelectionProvider provider; + private ISelection selection = StructuredSelection.EMPTY; + + public void trackProvider(final Control control, final ISelectionProvider selectionProvider) + { + control.addFocusListener(new FocusAdapter() { + @Override + public void focusGained(FocusEvent e) + { + setProvider(selectionProvider); + } + }); + } + + @Override + public void addSelectionChangedListener(ISelectionChangedListener listener) + { + listeners.add(listener); + } + + @Override + public void removeSelectionChangedListener(ISelectionChangedListener listener) + { + listeners.remove(listener); + } + + @Override + public ISelection getSelection() + { + ISelectionProvider cp = getProvider(); + if (cp != null) { + return cp.getSelection(); + } else { + return selection; + } + } + + @Override + public void setSelection(ISelection selection) + { + ISelectionProvider cp = getProvider(); + if (cp != null) { + cp.setSelection(selection); + } else { + this.selection = selection; + if (!CommonUtils.isEmpty(listeners)) { + SelectionChangedEvent event = new SelectionChangedEvent(this, selection); + for (ISelectionChangedListener listener : listeners) { + listener.selectionChanged(event); + } + } + } + } + + public ISelectionProvider getProvider() + { + return provider; + } + + public void setProvider(ISelectionProvider newProvider) + { + if (this.provider != newProvider){ + ISelection newSelection = null; + if (!CommonUtils.isEmpty(listeners)) { + if (this.provider != null){ + for (ISelectionChangedListener listener : listeners) { + this.provider.removeSelectionChangedListener(listener); + } + } + + if (newProvider != null) { + for (ISelectionChangedListener listener : listeners) { + newProvider.addSelectionChangedListener(listener); + } + + newSelection = newProvider.getSelection(); + } else { + newSelection = this.selection; + } + } + this.provider = newProvider; + + if (newSelection != null){ + //force a selection change event propagation + setSelection(newSelection); + } + } + } +} diff --git a/plugins/org.jkiss.dbeaver.core/src/org/jkiss/dbeaver/ui/CustomSelectionProvider.java b/plugins/org.jkiss.dbeaver.ui/src/org/jkiss/dbeaver/ui/CustomSelectionProvider.java similarity index 96% rename from plugins/org.jkiss.dbeaver.core/src/org/jkiss/dbeaver/ui/CustomSelectionProvider.java rename to plugins/org.jkiss.dbeaver.ui/src/org/jkiss/dbeaver/ui/CustomSelectionProvider.java index e073fecaa8..db9f3d94a0 100644 --- a/plugins/org.jkiss.dbeaver.core/src/org/jkiss/dbeaver/ui/CustomSelectionProvider.java +++ b/plugins/org.jkiss.dbeaver.ui/src/org/jkiss/dbeaver/ui/CustomSelectionProvider.java @@ -1,64 +1,64 @@ -/* - * DBeaver - Universal Database Manager - * Copyright (C) 2010-2017 Serge Rider (serge@jkiss.org) - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.jkiss.dbeaver.ui; - -import org.eclipse.jface.viewers.*; - -import java.util.ArrayList; -import java.util.List; - -/** - * Composite selection provider - */ -public class CustomSelectionProvider implements ISelectionProvider { - - private List listeners = new ArrayList<>(); - private ISelection selection; - - public CustomSelectionProvider() - { - selection = new StructuredSelection(); - } - - @Override - public void addSelectionChangedListener(ISelectionChangedListener listener) - { - listeners.add(listener); - } - - @Override - public void removeSelectionChangedListener(ISelectionChangedListener listener) - { - listeners.remove(listener); - } - - @Override - public ISelection getSelection() - { - return selection; - } - - @Override - public void setSelection(ISelection selection) - { - this.selection = selection; - for (ISelectionChangedListener listener : listeners) { - listener.selectionChanged(new SelectionChangedEvent(this, selection)); - } - - } -} +/* + * DBeaver - Universal Database Manager + * Copyright (C) 2010-2017 Serge Rider (serge@jkiss.org) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jkiss.dbeaver.ui; + +import org.eclipse.jface.viewers.*; + +import java.util.ArrayList; +import java.util.List; + +/** + * Composite selection provider + */ +public class CustomSelectionProvider implements ISelectionProvider { + + private List listeners = new ArrayList<>(); + private ISelection selection; + + public CustomSelectionProvider() + { + selection = new StructuredSelection(); + } + + @Override + public void addSelectionChangedListener(ISelectionChangedListener listener) + { + listeners.add(listener); + } + + @Override + public void removeSelectionChangedListener(ISelectionChangedListener listener) + { + listeners.remove(listener); + } + + @Override + public ISelection getSelection() + { + return selection; + } + + @Override + public void setSelection(ISelection selection) + { + this.selection = selection; + for (ISelectionChangedListener listener : listeners) { + listener.selectionChanged(new SelectionChangedEvent(this, selection)); + } + + } +} diff --git a/plugins/org.jkiss.dbeaver.core/src/org/jkiss/dbeaver/ui/DBIconBinary.java b/plugins/org.jkiss.dbeaver.ui/src/org/jkiss/dbeaver/ui/DBIconBinary.java similarity index 96% rename from plugins/org.jkiss.dbeaver.core/src/org/jkiss/dbeaver/ui/DBIconBinary.java rename to plugins/org.jkiss.dbeaver.ui/src/org/jkiss/dbeaver/ui/DBIconBinary.java index c108f69249..856882b2f8 100644 --- a/plugins/org.jkiss.dbeaver.core/src/org/jkiss/dbeaver/ui/DBIconBinary.java +++ b/plugins/org.jkiss.dbeaver.ui/src/org/jkiss/dbeaver/ui/DBIconBinary.java @@ -1,66 +1,66 @@ -/* - * DBeaver - Universal Database Manager - * Copyright (C) 2010-2017 Serge Rider (serge@jkiss.org) - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.jkiss.dbeaver.ui; - -import org.eclipse.jface.resource.ImageDescriptor; -import org.eclipse.swt.graphics.Image; -import org.eclipse.swt.graphics.ImageData; -import org.jkiss.dbeaver.model.DBPImage; -import org.jkiss.utils.CommonUtils; - -/** - * Image with binary data - */ -public class DBIconBinary implements DBPImage -{ - private final String location; - private Image image; - private ImageDescriptor imageDescriptor; - - public DBIconBinary(final String location, final ImageData data) { - this.location = "binary:" + location; - this.image = new Image(null, data); - imageDescriptor = ImageDescriptor.createFromImageData(data); - } - - public Image getImage() { - return image; - } - - public ImageDescriptor getImageDescriptor() { - return imageDescriptor; - } - - @Override - public String getLocation() { - return location; - } - - @Override - public boolean equals(Object obj) { - if (obj instanceof DBIconBinary) { - return CommonUtils.equalObjects(location, ((DBIconBinary) obj).location); - } - return false; - } - - @Override - public String toString() { - return location; - } -} +/* + * DBeaver - Universal Database Manager + * Copyright (C) 2010-2017 Serge Rider (serge@jkiss.org) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jkiss.dbeaver.ui; + +import org.eclipse.jface.resource.ImageDescriptor; +import org.eclipse.swt.graphics.Image; +import org.eclipse.swt.graphics.ImageData; +import org.jkiss.dbeaver.model.DBPImage; +import org.jkiss.utils.CommonUtils; + +/** + * Image with binary data + */ +public class DBIconBinary implements DBPImage +{ + private final String location; + private Image image; + private ImageDescriptor imageDescriptor; + + public DBIconBinary(final String location, final ImageData data) { + this.location = "binary:" + location; + this.image = new Image(null, data); + imageDescriptor = ImageDescriptor.createFromImageData(data); + } + + public Image getImage() { + return image; + } + + public ImageDescriptor getImageDescriptor() { + return imageDescriptor; + } + + @Override + public String getLocation() { + return location; + } + + @Override + public boolean equals(Object obj) { + if (obj instanceof DBIconBinary) { + return CommonUtils.equalObjects(location, ((DBIconBinary) obj).location); + } + return false; + } + + @Override + public String toString() { + return location; + } +} diff --git a/plugins/org.jkiss.dbeaver.core/src/org/jkiss/dbeaver/ui/DBeaverIcons.java b/plugins/org.jkiss.dbeaver.ui/src/org/jkiss/dbeaver/ui/DBeaverIcons.java similarity index 97% rename from plugins/org.jkiss.dbeaver.core/src/org/jkiss/dbeaver/ui/DBeaverIcons.java rename to plugins/org.jkiss.dbeaver.ui/src/org/jkiss/dbeaver/ui/DBeaverIcons.java index 92d69cfce5..89588ea65a 100644 --- a/plugins/org.jkiss.dbeaver.core/src/org/jkiss/dbeaver/ui/DBeaverIcons.java +++ b/plugins/org.jkiss.dbeaver.ui/src/org/jkiss/dbeaver/ui/DBeaverIcons.java @@ -1,220 +1,220 @@ -/* - * DBeaver - Universal Database Manager - * Copyright (C) 2010-2017 Serge Rider (serge@jkiss.org) - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.jkiss.dbeaver.ui; - -import org.eclipse.jface.resource.ImageDescriptor; -import org.eclipse.swt.SWT; -import org.eclipse.swt.graphics.Color; -import org.eclipse.swt.graphics.GC; -import org.eclipse.swt.graphics.Image; -import org.eclipse.swt.graphics.ImageData; -import org.eclipse.swt.widgets.Display; -import org.jkiss.code.NotNull; -import org.jkiss.dbeaver.Log; -import org.jkiss.dbeaver.model.DBIconComposite; -import org.jkiss.dbeaver.model.DBPImage; - -import java.net.URL; -import java.util.HashMap; -import java.util.Map; - -/** - * DBeaverIcons - */ -public class DBeaverIcons -{ - private static final Log log = Log.getLog(DBeaverIcons.class); - - private static final boolean useLegacyOverlay; - - static { - boolean hasCachedImageDataProvider; - try { - Class.forName("org.eclipse.jface.resource.CompositeImageDescriptor$CachedImageDataProvider"); - hasCachedImageDataProvider = true; - } catch (ClassNotFoundException e) { - hasCachedImageDataProvider = false; - } - useLegacyOverlay = !hasCachedImageDataProvider; - } - - private static class IconDescriptor { - String id; - Image image; - ImageDescriptor imageDescriptor; - - IconDescriptor(String id, ImageDescriptor imageDescriptor) { - this.id = id; - this.image = imageDescriptor.createImage(false); - this.imageDescriptor = imageDescriptor; - } - IconDescriptor(String id, Image image) { - this.id = id; - this.image = image; - this.imageDescriptor = ImageDescriptor.createFromImage(image); - } - } - - private static Map imageMap = new HashMap<>(); - private static Map compositeMap = new HashMap<>(); - private static Image viewMenuImage; - - @NotNull - public static Image getImage(@NotNull DBPImage image) - { - if (image == null) { - return null; - } - if (image instanceof DBIconBinary) { - return ((DBIconBinary) image).getImage(); - } else { - IconDescriptor icon = getIconByLocation(image.getLocation()); - if (icon == null) { - throw new IllegalArgumentException("Image '" + image.getLocation() + "' not found"); - } else if (image instanceof DBIconComposite) { - return getCompositeIcon(icon, (DBIconComposite) image).image; - } else { - return icon.image; - } - } - } - - @NotNull - public static ImageDescriptor getImageDescriptor(@NotNull DBPImage image) - { - if (image == null) { - return null; - } - if (image instanceof DBIconBinary) { - return ((DBIconBinary) image).getImageDescriptor(); - } else { - IconDescriptor icon = getIconByLocation(image.getLocation()); - if (icon == null) { - throw new IllegalArgumentException("Image '" + image.getLocation() + "' not found"); - } else if (image instanceof DBIconComposite) { - return getCompositeIcon(icon, (DBIconComposite) image).imageDescriptor; - } else { - return icon.imageDescriptor; - } - } - } - - private static IconDescriptor getCompositeIcon(IconDescriptor mainIcon, DBIconComposite image) { - if (!image.hasOverlays()) { - return mainIcon; - } - String compositeId = mainIcon.id + "^" + - (image.getTopLeft() == null ? "" : image.getTopLeft().getLocation()) + "^" + - (image.getTopRight() == null ? "" : image.getTopRight().getLocation()) + "^" + - (image.getBottomLeft() == null ? "" : image.getBottomLeft().getLocation()) + "^" + - (image.getBottomRight() == null ? "" : image.getBottomRight().getLocation()); - IconDescriptor icon = compositeMap.get(compositeId); - if (icon == null) { - Image resultImage; - if (useLegacyOverlay) { - OverlayImageDescriptorLegacy ovrImage = new OverlayImageDescriptorLegacy(mainIcon.image.getImageData()); - if (image.getTopLeft() != null) - ovrImage.setTopLeft(new ImageDescriptor[]{getImageDescriptor(image.getTopLeft())}); - if (image.getTopRight() != null) - ovrImage.setTopRight(new ImageDescriptor[]{getImageDescriptor(image.getTopRight())}); - if (image.getBottomLeft() != null) - ovrImage.setBottomLeft(new ImageDescriptor[]{getImageDescriptor(image.getBottomLeft())}); - if (image.getBottomRight() != null) - ovrImage.setBottomRight(new ImageDescriptor[]{getImageDescriptor(image.getBottomRight())}); - resultImage = ovrImage.createImage(); - } else { - OverlayImageDescriptor ovrImage = new OverlayImageDescriptor(mainIcon.imageDescriptor); - if (image.getTopLeft() != null) - ovrImage.setTopLeft(new ImageDescriptor[]{getImageDescriptor(image.getTopLeft())}); - if (image.getTopRight() != null) - ovrImage.setTopRight(new ImageDescriptor[]{getImageDescriptor(image.getTopRight())}); - if (image.getBottomLeft() != null) - ovrImage.setBottomLeft(new ImageDescriptor[]{getImageDescriptor(image.getBottomLeft())}); - if (image.getBottomRight() != null) - ovrImage.setBottomRight(new ImageDescriptor[]{getImageDescriptor(image.getBottomRight())}); - resultImage = ovrImage.createImage(); - } - icon = new IconDescriptor(compositeId, resultImage); - compositeMap.put(compositeId, icon); - } - return icon; - } - - private static IconDescriptor getIconByLocation(String location) { - IconDescriptor icon = imageMap.get(location); - if (icon == null) { - try { - ImageDescriptor imageDescriptor = ImageDescriptor.createFromURL(new URL(location)); - icon = new IconDescriptor(location, imageDescriptor); - if (icon.image == null) { - log.warn("Bad image: " + location); - return null; - } else { - imageMap.put(location, icon); - } - } catch (Exception e) { - log.error(e); - return null; - } - } - return icon; - } - - public static synchronized Image getViewMenuImage() { - if (viewMenuImage == null) { - Display d = Display.getCurrent(); - - Image viewMenu = new Image(d, 16, 16); - Image viewMenuMask = new Image(d, 16, 16); - - Display display = Display.getCurrent(); - GC gc = new GC(viewMenu); - GC maskgc = new GC(viewMenuMask); - gc.setForeground(display - .getSystemColor(SWT.COLOR_WIDGET_DARK_SHADOW)); - gc.setBackground(display.getSystemColor(SWT.COLOR_LIST_BACKGROUND)); - - int[] shapeArray = new int[]{6, 3, 15, 3, 11, 7, 10, 7}; - gc.fillPolygon(shapeArray); - gc.drawPolygon(shapeArray); - - Color black = display.getSystemColor(SWT.COLOR_BLACK); - Color white = display.getSystemColor(SWT.COLOR_WHITE); - - maskgc.setBackground(black); - maskgc.fillRectangle(0, 0, 16, 16); - - maskgc.setBackground(white); - maskgc.setForeground(white); - maskgc.fillPolygon(shapeArray); - maskgc.drawPolygon(shapeArray); - gc.dispose(); - maskgc.dispose(); - - ImageData data = viewMenu.getImageData(); - data.transparentPixel = data.getPixel(0, 0); - - viewMenuImage = new Image(d, viewMenu.getImageData(), - viewMenuMask.getImageData()); - viewMenu.dispose(); - viewMenuMask.dispose(); - } - return viewMenuImage; - } - -} +/* + * DBeaver - Universal Database Manager + * Copyright (C) 2010-2017 Serge Rider (serge@jkiss.org) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jkiss.dbeaver.ui; + +import org.eclipse.jface.resource.ImageDescriptor; +import org.eclipse.swt.SWT; +import org.eclipse.swt.graphics.Color; +import org.eclipse.swt.graphics.GC; +import org.eclipse.swt.graphics.Image; +import org.eclipse.swt.graphics.ImageData; +import org.eclipse.swt.widgets.Display; +import org.jkiss.code.NotNull; +import org.jkiss.dbeaver.Log; +import org.jkiss.dbeaver.model.DBIconComposite; +import org.jkiss.dbeaver.model.DBPImage; + +import java.net.URL; +import java.util.HashMap; +import java.util.Map; + +/** + * DBeaverIcons + */ +public class DBeaverIcons +{ + private static final Log log = Log.getLog(DBeaverIcons.class); + + private static final boolean useLegacyOverlay; + + static { + boolean hasCachedImageDataProvider; + try { + Class.forName("org.eclipse.jface.resource.CompositeImageDescriptor$CachedImageDataProvider"); + hasCachedImageDataProvider = true; + } catch (ClassNotFoundException e) { + hasCachedImageDataProvider = false; + } + useLegacyOverlay = !hasCachedImageDataProvider; + } + + private static class IconDescriptor { + String id; + Image image; + ImageDescriptor imageDescriptor; + + IconDescriptor(String id, ImageDescriptor imageDescriptor) { + this.id = id; + this.image = imageDescriptor.createImage(false); + this.imageDescriptor = imageDescriptor; + } + IconDescriptor(String id, Image image) { + this.id = id; + this.image = image; + this.imageDescriptor = ImageDescriptor.createFromImage(image); + } + } + + private static Map imageMap = new HashMap<>(); + private static Map compositeMap = new HashMap<>(); + private static Image viewMenuImage; + + @NotNull + public static Image getImage(@NotNull DBPImage image) + { + if (image == null) { + return null; + } + if (image instanceof DBIconBinary) { + return ((DBIconBinary) image).getImage(); + } else { + IconDescriptor icon = getIconByLocation(image.getLocation()); + if (icon == null) { + throw new IllegalArgumentException("Image '" + image.getLocation() + "' not found"); + } else if (image instanceof DBIconComposite) { + return getCompositeIcon(icon, (DBIconComposite) image).image; + } else { + return icon.image; + } + } + } + + @NotNull + public static ImageDescriptor getImageDescriptor(@NotNull DBPImage image) + { + if (image == null) { + return null; + } + if (image instanceof DBIconBinary) { + return ((DBIconBinary) image).getImageDescriptor(); + } else { + IconDescriptor icon = getIconByLocation(image.getLocation()); + if (icon == null) { + throw new IllegalArgumentException("Image '" + image.getLocation() + "' not found"); + } else if (image instanceof DBIconComposite) { + return getCompositeIcon(icon, (DBIconComposite) image).imageDescriptor; + } else { + return icon.imageDescriptor; + } + } + } + + private static IconDescriptor getCompositeIcon(IconDescriptor mainIcon, DBIconComposite image) { + if (!image.hasOverlays()) { + return mainIcon; + } + String compositeId = mainIcon.id + "^" + + (image.getTopLeft() == null ? "" : image.getTopLeft().getLocation()) + "^" + + (image.getTopRight() == null ? "" : image.getTopRight().getLocation()) + "^" + + (image.getBottomLeft() == null ? "" : image.getBottomLeft().getLocation()) + "^" + + (image.getBottomRight() == null ? "" : image.getBottomRight().getLocation()); + IconDescriptor icon = compositeMap.get(compositeId); + if (icon == null) { + Image resultImage; + if (useLegacyOverlay) { + OverlayImageDescriptorLegacy ovrImage = new OverlayImageDescriptorLegacy(mainIcon.image.getImageData()); + if (image.getTopLeft() != null) + ovrImage.setTopLeft(new ImageDescriptor[]{getImageDescriptor(image.getTopLeft())}); + if (image.getTopRight() != null) + ovrImage.setTopRight(new ImageDescriptor[]{getImageDescriptor(image.getTopRight())}); + if (image.getBottomLeft() != null) + ovrImage.setBottomLeft(new ImageDescriptor[]{getImageDescriptor(image.getBottomLeft())}); + if (image.getBottomRight() != null) + ovrImage.setBottomRight(new ImageDescriptor[]{getImageDescriptor(image.getBottomRight())}); + resultImage = ovrImage.createImage(); + } else { + OverlayImageDescriptor ovrImage = new OverlayImageDescriptor(mainIcon.imageDescriptor); + if (image.getTopLeft() != null) + ovrImage.setTopLeft(new ImageDescriptor[]{getImageDescriptor(image.getTopLeft())}); + if (image.getTopRight() != null) + ovrImage.setTopRight(new ImageDescriptor[]{getImageDescriptor(image.getTopRight())}); + if (image.getBottomLeft() != null) + ovrImage.setBottomLeft(new ImageDescriptor[]{getImageDescriptor(image.getBottomLeft())}); + if (image.getBottomRight() != null) + ovrImage.setBottomRight(new ImageDescriptor[]{getImageDescriptor(image.getBottomRight())}); + resultImage = ovrImage.createImage(); + } + icon = new IconDescriptor(compositeId, resultImage); + compositeMap.put(compositeId, icon); + } + return icon; + } + + private static IconDescriptor getIconByLocation(String location) { + IconDescriptor icon = imageMap.get(location); + if (icon == null) { + try { + ImageDescriptor imageDescriptor = ImageDescriptor.createFromURL(new URL(location)); + icon = new IconDescriptor(location, imageDescriptor); + if (icon.image == null) { + log.warn("Bad image: " + location); + return null; + } else { + imageMap.put(location, icon); + } + } catch (Exception e) { + log.error(e); + return null; + } + } + return icon; + } + + public static synchronized Image getViewMenuImage() { + if (viewMenuImage == null) { + Display d = Display.getCurrent(); + + Image viewMenu = new Image(d, 16, 16); + Image viewMenuMask = new Image(d, 16, 16); + + Display display = Display.getCurrent(); + GC gc = new GC(viewMenu); + GC maskgc = new GC(viewMenuMask); + gc.setForeground(display + .getSystemColor(SWT.COLOR_WIDGET_DARK_SHADOW)); + gc.setBackground(display.getSystemColor(SWT.COLOR_LIST_BACKGROUND)); + + int[] shapeArray = new int[]{6, 3, 15, 3, 11, 7, 10, 7}; + gc.fillPolygon(shapeArray); + gc.drawPolygon(shapeArray); + + Color black = display.getSystemColor(SWT.COLOR_BLACK); + Color white = display.getSystemColor(SWT.COLOR_WHITE); + + maskgc.setBackground(black); + maskgc.fillRectangle(0, 0, 16, 16); + + maskgc.setBackground(white); + maskgc.setForeground(white); + maskgc.fillPolygon(shapeArray); + maskgc.drawPolygon(shapeArray); + gc.dispose(); + maskgc.dispose(); + + ImageData data = viewMenu.getImageData(); + data.transparentPixel = data.getPixel(0, 0); + + viewMenuImage = new Image(d, viewMenu.getImageData(), + viewMenuMask.getImageData()); + viewMenu.dispose(); + viewMenuMask.dispose(); + } + return viewMenuImage; + } + +} diff --git a/plugins/org.jkiss.dbeaver.core/src/org/jkiss/dbeaver/ui/DynamicFindReplaceTarget.java b/plugins/org.jkiss.dbeaver.ui/src/org/jkiss/dbeaver/ui/DynamicFindReplaceTarget.java similarity index 96% rename from plugins/org.jkiss.dbeaver.core/src/org/jkiss/dbeaver/ui/DynamicFindReplaceTarget.java rename to plugins/org.jkiss.dbeaver.ui/src/org/jkiss/dbeaver/ui/DynamicFindReplaceTarget.java index 00dbbd622b..4177c33416 100644 --- a/plugins/org.jkiss.dbeaver.core/src/org/jkiss/dbeaver/ui/DynamicFindReplaceTarget.java +++ b/plugins/org.jkiss.dbeaver.ui/src/org/jkiss/dbeaver/ui/DynamicFindReplaceTarget.java @@ -1,184 +1,184 @@ -/* - * DBeaver - Universal Database Manager - * Copyright (C) 2010-2017 Serge Rider (serge@jkiss.org) - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.jkiss.dbeaver.ui; - -import org.eclipse.jface.text.IFindReplaceTarget; -import org.eclipse.jface.text.IFindReplaceTargetExtension; -import org.eclipse.jface.text.IFindReplaceTargetExtension3; -import org.eclipse.jface.text.IRegion; -import org.eclipse.swt.graphics.Color; -import org.eclipse.swt.graphics.Point; - -/** - * Composite find/replace target - */ -public class DynamicFindReplaceTarget implements IFindReplaceTarget, IFindReplaceTargetExtension, IFindReplaceTargetExtension3 { - - private IFindReplaceTarget target; - - public DynamicFindReplaceTarget() - { - } - - public IFindReplaceTarget getTarget() - { - return target; - } - - public void setTarget(IFindReplaceTarget target) - { - this.target = target; - } - - @Override - public boolean canPerformFind() - { - IFindReplaceTarget t = getTarget(); - return t != null && t.canPerformFind(); - } - - @Override - public int findAndSelect(int widgetOffset, String findString, boolean searchForward, boolean caseSensitive, boolean wholeWord) - { - IFindReplaceTarget t = getTarget(); - return t == null ? -1 : t.findAndSelect(widgetOffset, findString, searchForward, caseSensitive, wholeWord); - } - - @Override - public Point getSelection() - { - IFindReplaceTarget t = getTarget(); - return t == null ? new Point(0, 0) : t.getSelection(); - } - - @Override - public String getSelectionText() - { - IFindReplaceTarget t = getTarget(); - return t == null ? "" : t.getSelectionText(); - } - - @Override - public boolean isEditable() - { - IFindReplaceTarget t = getTarget(); - return t != null && t.isEditable(); - } - - @Override - public void replaceSelection(String text) - { - IFindReplaceTarget t = getTarget(); - if (t != null) { - t.replaceSelection(text); - } - } - - @Override - public void beginSession() - { - IFindReplaceTarget t = getTarget(); - if (t instanceof IFindReplaceTargetExtension) { - ((IFindReplaceTargetExtension) t).beginSession(); - } - } - - @Override - public void endSession() - { - IFindReplaceTarget t = getTarget(); - if (t instanceof IFindReplaceTargetExtension) { - ((IFindReplaceTargetExtension) t).endSession(); - } - } - - @Override - public IRegion getScope() - { - IFindReplaceTarget t = getTarget(); - if (t instanceof IFindReplaceTargetExtension) { - return ((IFindReplaceTargetExtension) t).getScope(); - } - return null; - } - - @Override - public void setScope(IRegion scope) - { - IFindReplaceTarget t = getTarget(); - if (t instanceof IFindReplaceTargetExtension) { - ((IFindReplaceTargetExtension) t).setScope(scope); - } - } - - @Override - public Point getLineSelection() - { - IFindReplaceTarget t = getTarget(); - if (t instanceof IFindReplaceTargetExtension) { - return ((IFindReplaceTargetExtension) t).getLineSelection(); - } - return getSelection(); - } - - @Override - public void setSelection(int offset, int length) - { - IFindReplaceTarget t = getTarget(); - if (t instanceof IFindReplaceTargetExtension) { - ((IFindReplaceTargetExtension) t).setSelection(offset, length); - } - } - - @Override - public void setScopeHighlightColor(Color color) - { - IFindReplaceTarget t = getTarget(); - if (t instanceof IFindReplaceTargetExtension) { - ((IFindReplaceTargetExtension) t).setScopeHighlightColor(color); - } - } - - @Override - public void setReplaceAllMode(boolean replaceAll) - { - IFindReplaceTarget t = getTarget(); - if (t instanceof IFindReplaceTargetExtension) { - ((IFindReplaceTargetExtension) t).setReplaceAllMode(replaceAll); - } - } - - @Override - public int findAndSelect(int offset, String findString, boolean searchForward, boolean caseSensitive, boolean wholeWord, boolean regExSearch) - { - IFindReplaceTarget t = getTarget(); - if (t instanceof IFindReplaceTargetExtension3) { - return ((IFindReplaceTargetExtension3) t).findAndSelect(offset, findString, searchForward, caseSensitive, wholeWord, regExSearch); - } - return findAndSelect(offset, findString, searchForward, caseSensitive, wholeWord); - } - - @Override - public void replaceSelection(String text, boolean regExReplace) - { - IFindReplaceTarget t = getTarget(); - if (t instanceof IFindReplaceTargetExtension3) { - ((IFindReplaceTargetExtension3) t).replaceSelection(text, regExReplace); - } - } - -} +/* + * DBeaver - Universal Database Manager + * Copyright (C) 2010-2017 Serge Rider (serge@jkiss.org) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jkiss.dbeaver.ui; + +import org.eclipse.jface.text.IFindReplaceTarget; +import org.eclipse.jface.text.IFindReplaceTargetExtension; +import org.eclipse.jface.text.IFindReplaceTargetExtension3; +import org.eclipse.jface.text.IRegion; +import org.eclipse.swt.graphics.Color; +import org.eclipse.swt.graphics.Point; + +/** + * Composite find/replace target + */ +public class DynamicFindReplaceTarget implements IFindReplaceTarget, IFindReplaceTargetExtension, IFindReplaceTargetExtension3 { + + private IFindReplaceTarget target; + + public DynamicFindReplaceTarget() + { + } + + public IFindReplaceTarget getTarget() + { + return target; + } + + public void setTarget(IFindReplaceTarget target) + { + this.target = target; + } + + @Override + public boolean canPerformFind() + { + IFindReplaceTarget t = getTarget(); + return t != null && t.canPerformFind(); + } + + @Override + public int findAndSelect(int widgetOffset, String findString, boolean searchForward, boolean caseSensitive, boolean wholeWord) + { + IFindReplaceTarget t = getTarget(); + return t == null ? -1 : t.findAndSelect(widgetOffset, findString, searchForward, caseSensitive, wholeWord); + } + + @Override + public Point getSelection() + { + IFindReplaceTarget t = getTarget(); + return t == null ? new Point(0, 0) : t.getSelection(); + } + + @Override + public String getSelectionText() + { + IFindReplaceTarget t = getTarget(); + return t == null ? "" : t.getSelectionText(); + } + + @Override + public boolean isEditable() + { + IFindReplaceTarget t = getTarget(); + return t != null && t.isEditable(); + } + + @Override + public void replaceSelection(String text) + { + IFindReplaceTarget t = getTarget(); + if (t != null) { + t.replaceSelection(text); + } + } + + @Override + public void beginSession() + { + IFindReplaceTarget t = getTarget(); + if (t instanceof IFindReplaceTargetExtension) { + ((IFindReplaceTargetExtension) t).beginSession(); + } + } + + @Override + public void endSession() + { + IFindReplaceTarget t = getTarget(); + if (t instanceof IFindReplaceTargetExtension) { + ((IFindReplaceTargetExtension) t).endSession(); + } + } + + @Override + public IRegion getScope() + { + IFindReplaceTarget t = getTarget(); + if (t instanceof IFindReplaceTargetExtension) { + return ((IFindReplaceTargetExtension) t).getScope(); + } + return null; + } + + @Override + public void setScope(IRegion scope) + { + IFindReplaceTarget t = getTarget(); + if (t instanceof IFindReplaceTargetExtension) { + ((IFindReplaceTargetExtension) t).setScope(scope); + } + } + + @Override + public Point getLineSelection() + { + IFindReplaceTarget t = getTarget(); + if (t instanceof IFindReplaceTargetExtension) { + return ((IFindReplaceTargetExtension) t).getLineSelection(); + } + return getSelection(); + } + + @Override + public void setSelection(int offset, int length) + { + IFindReplaceTarget t = getTarget(); + if (t instanceof IFindReplaceTargetExtension) { + ((IFindReplaceTargetExtension) t).setSelection(offset, length); + } + } + + @Override + public void setScopeHighlightColor(Color color) + { + IFindReplaceTarget t = getTarget(); + if (t instanceof IFindReplaceTargetExtension) { + ((IFindReplaceTargetExtension) t).setScopeHighlightColor(color); + } + } + + @Override + public void setReplaceAllMode(boolean replaceAll) + { + IFindReplaceTarget t = getTarget(); + if (t instanceof IFindReplaceTargetExtension) { + ((IFindReplaceTargetExtension) t).setReplaceAllMode(replaceAll); + } + } + + @Override + public int findAndSelect(int offset, String findString, boolean searchForward, boolean caseSensitive, boolean wholeWord, boolean regExSearch) + { + IFindReplaceTarget t = getTarget(); + if (t instanceof IFindReplaceTargetExtension3) { + return ((IFindReplaceTargetExtension3) t).findAndSelect(offset, findString, searchForward, caseSensitive, wholeWord, regExSearch); + } + return findAndSelect(offset, findString, searchForward, caseSensitive, wholeWord); + } + + @Override + public void replaceSelection(String text, boolean regExReplace) + { + IFindReplaceTarget t = getTarget(); + if (t instanceof IFindReplaceTargetExtension3) { + ((IFindReplaceTargetExtension3) t).replaceSelection(text, regExReplace); + } + } + +} diff --git a/plugins/org.jkiss.dbeaver.core/src/org/jkiss/dbeaver/ui/OverlayImageDescriptor.java b/plugins/org.jkiss.dbeaver.ui/src/org/jkiss/dbeaver/ui/OverlayImageDescriptor.java similarity index 100% rename from plugins/org.jkiss.dbeaver.core/src/org/jkiss/dbeaver/ui/OverlayImageDescriptor.java rename to plugins/org.jkiss.dbeaver.ui/src/org/jkiss/dbeaver/ui/OverlayImageDescriptor.java diff --git a/plugins/org.jkiss.dbeaver.core/src/org/jkiss/dbeaver/ui/OverlayImageDescriptorLegacy.java b/plugins/org.jkiss.dbeaver.ui/src/org/jkiss/dbeaver/ui/OverlayImageDescriptorLegacy.java similarity index 100% rename from plugins/org.jkiss.dbeaver.core/src/org/jkiss/dbeaver/ui/OverlayImageDescriptorLegacy.java rename to plugins/org.jkiss.dbeaver.ui/src/org/jkiss/dbeaver/ui/OverlayImageDescriptorLegacy.java diff --git a/plugins/org.jkiss.dbeaver.core/src/org/jkiss/dbeaver/ui/ProxyPageSite.java b/plugins/org.jkiss.dbeaver.ui/src/org/jkiss/dbeaver/ui/ProxyPageSite.java similarity index 96% rename from plugins/org.jkiss.dbeaver.core/src/org/jkiss/dbeaver/ui/ProxyPageSite.java rename to plugins/org.jkiss.dbeaver.ui/src/org/jkiss/dbeaver/ui/ProxyPageSite.java index fa0cc63206..a907e0b289 100644 --- a/plugins/org.jkiss.dbeaver.core/src/org/jkiss/dbeaver/ui/ProxyPageSite.java +++ b/plugins/org.jkiss.dbeaver.ui/src/org/jkiss/dbeaver/ui/ProxyPageSite.java @@ -1,102 +1,102 @@ -/* - * DBeaver - Universal Database Manager - * Copyright (C) 2010-2017 Serge Rider (serge@jkiss.org) - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.jkiss.dbeaver.ui; - -import org.eclipse.jface.action.MenuManager; -import org.eclipse.jface.viewers.ISelectionProvider; -import org.eclipse.swt.widgets.Shell; -import org.eclipse.ui.*; -import org.eclipse.ui.part.IPageSite; - -/** -* ProxyPageSite -*/ -public class ProxyPageSite implements IPageSite { - - private final IWorkbenchPartSite partSite; - - public ProxyPageSite(IWorkbenchPartSite partSite) - { - this.partSite = partSite; - } - - @Override - public void registerContextMenu(String menuId, MenuManager menuManager, ISelectionProvider selectionProvider) - { - partSite.registerContextMenu(menuId, menuManager, selectionProvider); - } - - @Override - public IActionBars getActionBars() - { - if (partSite instanceof IEditorSite) { - return ((IEditorSite)partSite).getActionBars(); - } else if (partSite instanceof IViewSite) { - return ((IViewSite)partSite).getActionBars(); - } else { - return null; - } - } - - @Override - public IWorkbenchPage getPage() - { - return partSite.getPage(); - } - - @Override - public ISelectionProvider getSelectionProvider() - { - return partSite.getSelectionProvider(); - } - - @Override - public Shell getShell() - { - return partSite.getShell(); - } - - @Override - public IWorkbenchWindow getWorkbenchWindow() - { - return partSite.getWorkbenchWindow(); - } - - @Override - public void setSelectionProvider(ISelectionProvider provider) - { - partSite.setSelectionProvider(provider); - } - - @Override - public T getAdapter(Class adapter) - { - return partSite.getAdapter(adapter); - } - - @Override - public T getService(Class api) - { - return partSite.getService(api); - } - - @Override - public boolean hasService(Class api) - { - return partSite.hasService(api); - } -} +/* + * DBeaver - Universal Database Manager + * Copyright (C) 2010-2017 Serge Rider (serge@jkiss.org) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jkiss.dbeaver.ui; + +import org.eclipse.jface.action.MenuManager; +import org.eclipse.jface.viewers.ISelectionProvider; +import org.eclipse.swt.widgets.Shell; +import org.eclipse.ui.*; +import org.eclipse.ui.part.IPageSite; + +/** +* ProxyPageSite +*/ +public class ProxyPageSite implements IPageSite { + + private final IWorkbenchPartSite partSite; + + public ProxyPageSite(IWorkbenchPartSite partSite) + { + this.partSite = partSite; + } + + @Override + public void registerContextMenu(String menuId, MenuManager menuManager, ISelectionProvider selectionProvider) + { + partSite.registerContextMenu(menuId, menuManager, selectionProvider); + } + + @Override + public IActionBars getActionBars() + { + if (partSite instanceof IEditorSite) { + return ((IEditorSite)partSite).getActionBars(); + } else if (partSite instanceof IViewSite) { + return ((IViewSite)partSite).getActionBars(); + } else { + return null; + } + } + + @Override + public IWorkbenchPage getPage() + { + return partSite.getPage(); + } + + @Override + public ISelectionProvider getSelectionProvider() + { + return partSite.getSelectionProvider(); + } + + @Override + public Shell getShell() + { + return partSite.getShell(); + } + + @Override + public IWorkbenchWindow getWorkbenchWindow() + { + return partSite.getWorkbenchWindow(); + } + + @Override + public void setSelectionProvider(ISelectionProvider provider) + { + partSite.setSelectionProvider(provider); + } + + @Override + public T getAdapter(Class adapter) + { + return partSite.getAdapter(adapter); + } + + @Override + public T getService(Class api) + { + return partSite.getService(api); + } + + @Override + public boolean hasService(Class api) + { + return partSite.hasService(api); + } +} diff --git a/plugins/org.jkiss.dbeaver.core/src/org/jkiss/dbeaver/ui/SharedTextColors.java b/plugins/org.jkiss.dbeaver.ui/src/org/jkiss/dbeaver/ui/SharedTextColors.java similarity index 96% rename from plugins/org.jkiss.dbeaver.core/src/org/jkiss/dbeaver/ui/SharedTextColors.java rename to plugins/org.jkiss.dbeaver.ui/src/org/jkiss/dbeaver/ui/SharedTextColors.java index 2b7fb6d320..1aa6a0ee01 100644 --- a/plugins/org.jkiss.dbeaver.core/src/org/jkiss/dbeaver/ui/SharedTextColors.java +++ b/plugins/org.jkiss.dbeaver.ui/src/org/jkiss/dbeaver/ui/SharedTextColors.java @@ -1,120 +1,120 @@ -/* - * DBeaver - Universal Database Manager - * Copyright (C) 2010-2017 Serge Rider (serge@jkiss.org) - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.jkiss.dbeaver.ui; - -import org.eclipse.jface.resource.StringConverter; -import org.eclipse.jface.text.source.ISharedTextColors; -import org.eclipse.swt.graphics.Color; -import org.eclipse.swt.graphics.RGB; -import org.eclipse.swt.widgets.Display; -import org.jkiss.code.NotNull; - -import java.util.HashMap; -import java.util.Map; - -public class SharedTextColors implements ISharedTextColors { - - public static final RGB COLOR_WARNING = new RGB(0xFF, 0x63, 0x47); - - /** - * The display table. - */ - private final Map> fDisplayTable = new HashMap<>(); - private final Map rgbMap = new HashMap<>(); - - public SharedTextColors() - { - super(); - } - - @NotNull - public Color getColor(String rgbString) - { - RGB rgb; - synchronized (rgbMap) { - rgb = rgbMap.get(rgbString); - if (rgb == null) { - rgb = StringConverter.asRGB(rgbString); - rgbMap.put(rgbString, rgb); - } - } - return getColor(rgb); - } - - @NotNull - @Override - public Color getColor(@NotNull RGB rgb) - { - Display display = Display.getCurrent(); - if (display == null) { - display = Display.getDefault(); - } - final Display curDisplay = display; - - Map colorTable; - synchronized (fDisplayTable) { - colorTable = fDisplayTable.get(display); - if (colorTable == null) { - colorTable = new HashMap<>(10); - fDisplayTable.put(curDisplay, colorTable); - display.disposeExec(new Runnable() { - @Override - public void run() { - dispose(curDisplay); - } - }); - } - } - - Color color = colorTable.get(rgb); - if (color == null) { - color = new Color(curDisplay, rgb); - colorTable.put(rgb, color); - } - - return color; - } - - @Override - public void dispose() - { - for (Map rgbColorMap : fDisplayTable.values()) { - dispose(rgbColorMap); - } - fDisplayTable.clear(); - } - - private void dispose(Display display) - { - if (fDisplayTable != null) { - dispose(fDisplayTable.remove(display)); - } - } - - private void dispose(Map colorTable) - { - if (colorTable == null) - return; - - for (Color color : colorTable.values()) { - color.dispose(); - } - - colorTable.clear(); - } - -} +/* + * DBeaver - Universal Database Manager + * Copyright (C) 2010-2017 Serge Rider (serge@jkiss.org) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jkiss.dbeaver.ui; + +import org.eclipse.jface.resource.StringConverter; +import org.eclipse.jface.text.source.ISharedTextColors; +import org.eclipse.swt.graphics.Color; +import org.eclipse.swt.graphics.RGB; +import org.eclipse.swt.widgets.Display; +import org.jkiss.code.NotNull; + +import java.util.HashMap; +import java.util.Map; + +public class SharedTextColors implements ISharedTextColors { + + public static final RGB COLOR_WARNING = new RGB(0xFF, 0x63, 0x47); + + /** + * The display table. + */ + private final Map> fDisplayTable = new HashMap<>(); + private final Map rgbMap = new HashMap<>(); + + public SharedTextColors() + { + super(); + } + + @NotNull + public Color getColor(String rgbString) + { + RGB rgb; + synchronized (rgbMap) { + rgb = rgbMap.get(rgbString); + if (rgb == null) { + rgb = StringConverter.asRGB(rgbString); + rgbMap.put(rgbString, rgb); + } + } + return getColor(rgb); + } + + @NotNull + @Override + public Color getColor(@NotNull RGB rgb) + { + Display display = Display.getCurrent(); + if (display == null) { + display = Display.getDefault(); + } + final Display curDisplay = display; + + Map colorTable; + synchronized (fDisplayTable) { + colorTable = fDisplayTable.get(display); + if (colorTable == null) { + colorTable = new HashMap<>(10); + fDisplayTable.put(curDisplay, colorTable); + display.disposeExec(new Runnable() { + @Override + public void run() { + dispose(curDisplay); + } + }); + } + } + + Color color = colorTable.get(rgb); + if (color == null) { + color = new Color(curDisplay, rgb); + colorTable.put(rgb, color); + } + + return color; + } + + @Override + public void dispose() + { + for (Map rgbColorMap : fDisplayTable.values()) { + dispose(rgbColorMap); + } + fDisplayTable.clear(); + } + + private void dispose(Display display) + { + if (fDisplayTable != null) { + dispose(fDisplayTable.remove(display)); + } + } + + private void dispose(Map colorTable) + { + if (colorTable == null) + return; + + for (Color color : colorTable.values()) { + color.dispose(); + } + + colorTable.clear(); + } + +} diff --git a/plugins/org.jkiss.dbeaver.core/src/org/jkiss/dbeaver/ui/TableToolTip.java b/plugins/org.jkiss.dbeaver.ui/src/org/jkiss/dbeaver/ui/TableToolTip.java similarity index 97% rename from plugins/org.jkiss.dbeaver.core/src/org/jkiss/dbeaver/ui/TableToolTip.java rename to plugins/org.jkiss.dbeaver.ui/src/org/jkiss/dbeaver/ui/TableToolTip.java index f49d594102..96b2339751 100644 --- a/plugins/org.jkiss.dbeaver.core/src/org/jkiss/dbeaver/ui/TableToolTip.java +++ b/plugins/org.jkiss.dbeaver.ui/src/org/jkiss/dbeaver/ui/TableToolTip.java @@ -1,123 +1,123 @@ -/* - * DBeaver - Universal Database Manager - * Copyright (C) 2010-2017 Serge Rider (serge@jkiss.org) - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.jkiss.dbeaver.ui; - -import org.eclipse.swt.SWT; -import org.eclipse.swt.graphics.Point; -import org.eclipse.swt.layout.FillLayout; -import org.eclipse.swt.widgets.*; -import org.jkiss.utils.CommonUtils; - -/** - * Fake table tooltip provider - */ -public class TableToolTip { - - private final Table table; - - public TableToolTip(Table table) { - this.table = table; - applyCustomTolTips(); - } - - public void applyCustomTolTips() - { - // Disable native tooltip - table.setToolTipText (""); //$NON-NLS-1$ - - // Implement a "fake" tooltip - final Listener labelListener = new Listener () { - @Override - public void handleEvent (Event event) { - Label label = (Label)event.widget; - Shell shell = label.getShell (); - switch (event.type) { - case SWT.MouseExit: - shell.dispose (); - break; - } - } - }; - - Listener tableListener = new Listener () { - Shell tip = null; - Label label = null; - @Override - public void handleEvent (Event event) { - switch (event.type) { - case SWT.Dispose: - case SWT.KeyDown: - case SWT.MouseMove: { - if (tip == null) break; - tip.dispose (); - tip = null; - label = null; - break; - } - case SWT.MouseHover: { - Point eventPt = new Point(event.x, event.y); - TableItem item = table.getItem (eventPt); - int selectedColumn = -1; - if (item != null) { - int columnCount = table.getColumnCount(); - for (int i = 0; i < columnCount; i++) { - if (item.getBounds(i).contains(eventPt)) { - selectedColumn = i; - break; - } - } - } - - if (item != null && selectedColumn >= 0) { - String toolTip = getItemToolTip(item, selectedColumn); - if (toolTip != null) { - toolTip = toolTip.trim(); - } - if (!CommonUtils.isEmpty(toolTip)) { - if (tip != null && !tip.isDisposed ()) tip.dispose (); - tip = new Shell (table.getShell(), SWT.ON_TOP | SWT.NO_FOCUS | SWT.TOOL); - tip.setForeground (table.getDisplay().getSystemColor (SWT.COLOR_INFO_FOREGROUND)); - tip.setBackground (table.getDisplay().getSystemColor (SWT.COLOR_INFO_BACKGROUND)); - FillLayout layout = new FillLayout (); - layout.marginWidth = 2; - tip.setLayout (layout); - label = new Label (tip, SWT.NONE); - label.setForeground (table.getDisplay().getSystemColor (SWT.COLOR_INFO_FOREGROUND)); - label.setBackground (table.getDisplay().getSystemColor (SWT.COLOR_INFO_BACKGROUND)); - label.setText (toolTip); - label.addListener (SWT.MouseExit, labelListener); - Point size = tip.computeSize (SWT.DEFAULT, SWT.DEFAULT); - Point pt = table.toDisplay (event.x, event.y); - tip.setBounds (pt.x, pt.y + item.getBounds().height, size.x, size.y); - tip.setVisible (true); - } - } - } - } - } - }; - table.addListener (SWT.Dispose, tableListener); - table.addListener (SWT.KeyDown, tableListener); - table.addListener (SWT.MouseMove, tableListener); - table.addListener (SWT.MouseHover, tableListener); - } - - public String getItemToolTip(TableItem item, int selectedColumn) { - return item.getText(selectedColumn); - } - -} +/* + * DBeaver - Universal Database Manager + * Copyright (C) 2010-2017 Serge Rider (serge@jkiss.org) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jkiss.dbeaver.ui; + +import org.eclipse.swt.SWT; +import org.eclipse.swt.graphics.Point; +import org.eclipse.swt.layout.FillLayout; +import org.eclipse.swt.widgets.*; +import org.jkiss.utils.CommonUtils; + +/** + * Fake table tooltip provider + */ +public class TableToolTip { + + private final Table table; + + public TableToolTip(Table table) { + this.table = table; + applyCustomTolTips(); + } + + public void applyCustomTolTips() + { + // Disable native tooltip + table.setToolTipText (""); //$NON-NLS-1$ + + // Implement a "fake" tooltip + final Listener labelListener = new Listener () { + @Override + public void handleEvent (Event event) { + Label label = (Label)event.widget; + Shell shell = label.getShell (); + switch (event.type) { + case SWT.MouseExit: + shell.dispose (); + break; + } + } + }; + + Listener tableListener = new Listener () { + Shell tip = null; + Label label = null; + @Override + public void handleEvent (Event event) { + switch (event.type) { + case SWT.Dispose: + case SWT.KeyDown: + case SWT.MouseMove: { + if (tip == null) break; + tip.dispose (); + tip = null; + label = null; + break; + } + case SWT.MouseHover: { + Point eventPt = new Point(event.x, event.y); + TableItem item = table.getItem (eventPt); + int selectedColumn = -1; + if (item != null) { + int columnCount = table.getColumnCount(); + for (int i = 0; i < columnCount; i++) { + if (item.getBounds(i).contains(eventPt)) { + selectedColumn = i; + break; + } + } + } + + if (item != null && selectedColumn >= 0) { + String toolTip = getItemToolTip(item, selectedColumn); + if (toolTip != null) { + toolTip = toolTip.trim(); + } + if (!CommonUtils.isEmpty(toolTip)) { + if (tip != null && !tip.isDisposed ()) tip.dispose (); + tip = new Shell (table.getShell(), SWT.ON_TOP | SWT.NO_FOCUS | SWT.TOOL); + tip.setForeground (table.getDisplay().getSystemColor (SWT.COLOR_INFO_FOREGROUND)); + tip.setBackground (table.getDisplay().getSystemColor (SWT.COLOR_INFO_BACKGROUND)); + FillLayout layout = new FillLayout (); + layout.marginWidth = 2; + tip.setLayout (layout); + label = new Label (tip, SWT.NONE); + label.setForeground (table.getDisplay().getSystemColor (SWT.COLOR_INFO_FOREGROUND)); + label.setBackground (table.getDisplay().getSystemColor (SWT.COLOR_INFO_BACKGROUND)); + label.setText (toolTip); + label.addListener (SWT.MouseExit, labelListener); + Point size = tip.computeSize (SWT.DEFAULT, SWT.DEFAULT); + Point pt = table.toDisplay (event.x, event.y); + tip.setBounds (pt.x, pt.y + item.getBounds().height, size.x, size.y); + tip.setVisible (true); + } + } + } + } + } + }; + table.addListener (SWT.Dispose, tableListener); + table.addListener (SWT.KeyDown, tableListener); + table.addListener (SWT.MouseMove, tableListener); + table.addListener (SWT.MouseHover, tableListener); + } + + public String getItemToolTip(TableItem item, int selectedColumn) { + return item.getText(selectedColumn); + } + +} -- GitLab