1 /*******************************************************************************
2  * Copyright (c) 2000, 2010 IBM Corporation and others.
3  *
4  * This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License 2.0
6  * which accompanies this distribution, and is available at
7  * https://www.eclipse.org/legal/epl-2.0/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *     IBM Corporation - initial API and implementation
13  *******************************************************************************/
14 package org.eclipse.search.internal.ui;
15 
16 import java.net.URL;
17 
18 import org.osgi.framework.Bundle;
19 
20 import org.eclipse.swt.graphics.Image;
21 
22 import org.eclipse.core.runtime.FileLocator;
23 import org.eclipse.core.runtime.IPath;
24 import org.eclipse.core.runtime.Path;
25 
26 import org.eclipse.jface.action.IAction;
27 import org.eclipse.jface.resource.ImageDescriptor;
28 import org.eclipse.jface.resource.ImageRegistry;
29 
30 
31 /**
32  * Bundle of all images used by the Search UI plugin.
33  */
34 public class SearchPluginImages {
35 
36 	// The plugin registry
37 	private final static ImageRegistry PLUGIN_REGISTRY= SearchPlugin.getDefault().getImageRegistry();
38 
39 	private static final IPath ICONS_PATH= new Path("$nl$/icons/full"); //$NON-NLS-1$
40 
41 	public static final String T_OBJ= "obj16/"; //$NON-NLS-1$
42 	public static final String T_WIZBAN= "wizban/"; //$NON-NLS-1$
43 	public static final String T_LCL= "lcl16/"; //$NON-NLS-1$
44 	public static final String T_TOOL= "tool16/"; //$NON-NLS-1$
45 	public static final String T_EVIEW= "eview16/"; //$NON-NLS-1$
46 
47 	private static final String NAME_PREFIX= "org.eclipse.search.ui."; //$NON-NLS-1$
48 	private static final int    NAME_PREFIX_LENGTH= NAME_PREFIX.length();
49 
50 	// Define image names
51 	public static final String IMG_TOOL_SEARCH= NAME_PREFIX + "search.png"; //$NON-NLS-1$
52 
53 	public static final String IMG_LCL_REFRESH= NAME_PREFIX + "refresh.png"; //$NON-NLS-1$
54 	public static final String IMG_LCL_PIN_VIEW= NAME_PREFIX + "pin_view.png"; //$NON-NLS-1$
55 
56 	public static final String IMG_LCL_SEARCH_REM= NAME_PREFIX + "search_rem.png"; //$NON-NLS-1$
57 	public static final String IMG_LCL_SEARCH_REM_ALL= NAME_PREFIX + "search_remall.png"; //$NON-NLS-1$
58 	public static final String IMG_LCL_SEARCH_NEXT= NAME_PREFIX + "search_next.png"; //$NON-NLS-1$
59 	public static final String IMG_LCL_SEARCH_PREV= NAME_PREFIX + "search_prev.png"; //$NON-NLS-1$
60 	public static final String IMG_LCL_SEARCH_GOTO= NAME_PREFIX + "search_goto.png"; //$NON-NLS-1$
61 	public static final String IMG_LCL_SEARCH_SORT= NAME_PREFIX + "search_sortmatch.png"; //$NON-NLS-1$
62 	public static final String IMG_LCL_SEARCH_HISTORY= NAME_PREFIX + "search_history.png"; //$NON-NLS-1$
63 	public static final String IMG_LCL_SEARCH_FLAT_LAYOUT= NAME_PREFIX + "flatLayout.png"; //$NON-NLS-1$
64 	public static final String IMG_LCL_SEARCH_HIERARCHICAL_LAYOUT= NAME_PREFIX + "hierarchicalLayout.png"; //$NON-NLS-1$
65 	public static final String IMG_LCL_SEARCH_HORIZONTAL_ORIENTATION= NAME_PREFIX + "horizontalOrientation.png"; //$NON-NLS-1$
66 	public static final String IMG_LCL_SEARCH_VERTICAL_ORIENTATION= NAME_PREFIX + "verticalOrientation.png"; //$NON-NLS-1$
67 	public static final String IMG_LCL_SEARCH_AUTOMATIC_ORIENTATION= NAME_PREFIX + "automaticOrientation.png"; //$NON-NLS-1$
68 	public static final String IMG_LCL_SEARCH_SINGLE_ORIENTATION= NAME_PREFIX + "singleOrientation.png"; //$NON-NLS-1$
69 	public static final String IMG_LCL_SEARCH_CANCEL= NAME_PREFIX + "stop.png"; //$NON-NLS-1$
70 	public static final String IMG_LCL_SEARCH_COLLAPSE_ALL= NAME_PREFIX + "collapseall.png"; //$NON-NLS-1$
71 	public static final String IMG_LCL_SEARCH_EXPAND_ALL= NAME_PREFIX + "expandall.png"; //$NON-NLS-1$
72 	public static final String IMG_LCL_SEARCH_FILTER= NAME_PREFIX + "filter_ps.png"; //$NON-NLS-1$
73 	public static final String IMG_VIEW_SEARCHRES= NAME_PREFIX + "searchres.png"; //$NON-NLS-1$
74 
75 	public static final String IMG_OBJ_TSEARCH_DPDN= NAME_PREFIX + "tsearch_dpdn_obj.png"; //$NON-NLS-1$
76 	public static final String IMG_OBJ_SEARCHMARKER= NAME_PREFIX + "searchm_obj.png"; //$NON-NLS-1$
77 	public static final String IMG_OBJ_TEXT_SEARCH_LINE= NAME_PREFIX + "line_match.png"; //$NON-NLS-1$
78 
79 	// Define images
80 	public static final ImageDescriptor DESC_OBJ_TSEARCH_DPDN= createManaged(T_OBJ, IMG_OBJ_TSEARCH_DPDN);
81 	public static final ImageDescriptor DESC_OBJ_SEARCHMARKER= createManaged(T_OBJ, IMG_OBJ_SEARCHMARKER);
82 	public static final ImageDescriptor DESC_OBJ_TEXT_SEARCH_LINE= createManaged(T_OBJ, IMG_OBJ_TEXT_SEARCH_LINE);
83 	public static final ImageDescriptor DESC_VIEW_SEARCHRES= createManaged(T_EVIEW, IMG_VIEW_SEARCHRES);
84 
get(String key)85 	public static Image get(String key) {
86 		return PLUGIN_REGISTRY.get(key);
87 	}
88 
createManaged(String prefix, String name)89 	private static ImageDescriptor createManaged(String prefix, String name) {
90 		ImageDescriptor result= create(prefix, name.substring(NAME_PREFIX_LENGTH), true);
91 		PLUGIN_REGISTRY.put(name, result);
92 		return result;
93 	}
94 
95 	/*
96 	 * Creates an image descriptor for the given prefix and name in the Search plugin bundle. The path can
97 	 * contain variables like $NL$.
98 	 * If no image could be found, <code>useMissingImageDescriptor</code> decides if either
99 	 * the 'missing image descriptor' is returned or <code>null</code>.
100 	 * or <code>null</code>.
101 	 */
create(String prefix, String name, boolean useMissingImageDescriptor)102 	private static ImageDescriptor create(String prefix, String name, boolean useMissingImageDescriptor) {
103 		IPath path= ICONS_PATH.append(prefix).append(name);
104 		return createImageDescriptor(SearchPlugin.getDefault().getBundle(), path, useMissingImageDescriptor);
105 	}
106 
107 	/*
108 	 * Sets all available image descriptors for the given action.
109 	 */
setImageDescriptors(IAction action, String type, String relPath)110 	public static void setImageDescriptors(IAction action, String type, String relPath) {
111 		relPath= relPath.substring(NAME_PREFIX_LENGTH);
112 
113 		action.setDisabledImageDescriptor(create("d" + type, relPath, false)); //$NON-NLS-1$
114 
115 		ImageDescriptor desc= create("e" + type, relPath, true); //$NON-NLS-1$
116 		action.setHoverImageDescriptor(desc);
117 		action.setImageDescriptor(desc);
118 	}
119 
120 	/*
121 	 * Creates an image descriptor for the given path in a bundle. The path can contain variables
122 	 * like $NL$.
123 	 * If no image could be found, <code>useMissingImageDescriptor</code> decides if either
124 	 * the 'missing image descriptor' is returned or <code>null</code>.
125 	 * Added for 3.1.1.
126 	 */
createImageDescriptor(Bundle bundle, IPath path, boolean useMissingImageDescriptor)127 	public static ImageDescriptor createImageDescriptor(Bundle bundle, IPath path, boolean useMissingImageDescriptor) {
128 		URL url= FileLocator.find(bundle, path, null);
129 		if (url != null) {
130 			return ImageDescriptor.createFromURL(url);
131 		}
132 		if (useMissingImageDescriptor) {
133 			return ImageDescriptor.getMissingImageDescriptor();
134 		}
135 		return null;
136 	}
137 
138 }
139