Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 8345362fbfab4786a02e78090b9d09bf9f2a9c69 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*******************************************************************************
 * Copyright (c) 2000, 2007 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     Atsuhiko Yamanaka, JCraft,Inc. - initial API and implementation.
 *     IBM Corporation - ongoing maintenance
 *******************************************************************************/
package org.eclipse.jsch.internal.ui;

import java.net.URL;
import java.util.Hashtable;

import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.IExtension;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jsch.core.IJSchService;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;
import org.osgi.util.tracker.ServiceTracker;

public class JSchUIPlugin extends AbstractUIPlugin{
  public static final String ID="org.eclipse.jsch.ui"; //$NON-NLS-1$
  public static final String DECORATOR_ID="org.eclipse.jsch.ui.decorator"; //$NON-NLS-1$

  private static Hashtable<String, ImageDescriptor> imageDescriptors = new Hashtable<>(20);
  /**
   * The singleton plug-in instance
   */
  private static JSchUIPlugin plugin;
  private ServiceTracker<?, ?> tracker;

  public JSchUIPlugin(){
    super();
    plugin=this;
  }

  /**
   * Returns the singleton plug-in instance.
   *
   * @return the plugin instance
   */
  public static JSchUIPlugin getPlugin(){
    return plugin;
  }

  /**
   * Returns the image descriptor for the given image ID.
   * Returns null if there is no such image.
   * @param id the id of the image descriptor
   * @return the image descriptor
   */
  public static ImageDescriptor getImageDescriptor(String id){
    return imageDescriptors.get(id);
  }

  /**
   * Creates an image and places it in the image registry.
   */
  protected void createImageDescriptor(String id){
    URL url=FileLocator.find(JSchUIPlugin.getPlugin().getBundle(), new Path(
        IUIConstants.ICON_PATH+id), null);
    ImageDescriptor desc=ImageDescriptor.createFromURL(url);
    imageDescriptors.put(id, desc);
  }

  /**
   * Convenience method to get an image descriptor for an extension
   *
   * @param extension  the extension declaring the image
   * @param subdirectoryAndFilename  the path to the image
   * @return the image
   */
  public static ImageDescriptor getImageDescriptorFromExtension(IExtension extension, String subdirectoryAndFilename) {
    URL fullPathString = FileLocator.find(Platform.getBundle(extension.getNamespaceIdentifier()), new Path(subdirectoryAndFilename), null);
    return ImageDescriptor.createFromURL(fullPathString);
  }

  /**
   * Initializes the table of images used in this plugin.
   */
  private void initializeImages() {
    createImageDescriptor(IUIConstants.IMG_KEY_LOCK);
  }

  @Override
public void start(BundleContext context) throws Exception {
    super.start(context);

    initializeImages();

    IPreferenceStore store = getPreferenceStore();
    if (store.getBoolean(IUIConstants.PREF_FIRST_STARTUP)) {
      store.setValue(IUIConstants.PREF_FIRST_STARTUP, false);
    }

    tracker = new ServiceTracker(getBundle().getBundleContext(),IJSchService.class.getName(), null);
    tracker.open();
  }

  @Override
public void stop(BundleContext context) throws Exception{
    super.stop(context);
    tracker.close();
  }

  public IJSchService getJSchService() {
    return (IJSchService)tracker.getService();
  }

  public URL getImageUrl(String relative){
    return FileLocator.find(Platform.getBundle(ID), new Path(IUIConstants.ICON_PATH + relative), null);
  }
}

Back to the top