001 /*
002 GRANITE DATA SERVICES
003 Copyright (C) 2011 GRANITE DATA SERVICES S.A.S.
004
005 This file is part of Granite Data Services.
006
007 Granite Data Services is free software; you can redistribute it and/or modify
008 it under the terms of the GNU Library General Public License as published by
009 the Free Software Foundation; either version 2 of the License, or (at your
010 option) any later version.
011
012 Granite Data Services is distributed in the hope that it will be useful, but
013 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
014 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
015 for more details.
016
017 You should have received a copy of the GNU Library General Public License
018 along with this library; if not, see <http://www.gnu.org/licenses/>.
019 */
020
021 package org.granite.builder.util;
022
023 import java.util.HashMap;
024 import java.util.Map;
025
026 import org.eclipse.swt.SWT;
027 import org.eclipse.swt.events.SelectionListener;
028 import org.eclipse.swt.graphics.Color;
029 import org.eclipse.swt.graphics.Device;
030 import org.eclipse.swt.graphics.Image;
031 import org.eclipse.swt.graphics.RGB;
032 import org.eclipse.swt.layout.GridData;
033 import org.eclipse.swt.layout.GridLayout;
034 import org.eclipse.swt.widgets.Button;
035 import org.eclipse.swt.widgets.Composite;
036 import org.eclipse.swt.widgets.Display;
037 import org.eclipse.swt.widgets.Tree;
038 import org.eclipse.swt.widgets.TreeItem;
039 import org.eclipse.ui.PlatformUI;
040
041 /**
042 * @author Franck WOLFF
043 */
044 public class SWTUtil {
045
046 private static final String PREFIX_KEY = "PREFIX";
047 private static final String EMPTY_KEY = "EMPTY";
048 private static final String VALUE_KEY = "VALUE";
049
050 public static final String IMG_PKG_FOLDER = "icons/packagefolder_obj.gif";
051 public static final String IMG_PKG_FOLDER_ERROR = "icons/packagefolder_obj_error.gif";
052 public static final String IMG_LIBRARY = "icons/library_obj.gif";
053 public static final String IMG_JAR = "icons/jar_obj.gif";
054 public static final String IMG_JAR_LIBRARY = "icons/jar_l_obj.gif";
055 public static final String IMG_SETTINGS = "icons/settings_obj.gif";
056 public static final String IMG_INCLUDES = "icons/inclusion_filter_attrib.gif";
057 public static final String IMG_EXCLUDES = "icons/exclusion_filter_attrib.gif";
058 public static final String IMG_OUT_FOLDER = "icons/externalize.gif";
059 public static final String IMG_WARNING = "icons/warning_obj.gif";
060 public static final String IMG_TEMPLATE = "icons/template_obj.gif";
061 public static final String IMG_FILE = "icons/file_obj.gif";
062 public static final String IMG_PROJECTS = "icons/projects.gif";
063 public static final String IMG_GPROJECT = "icons/gproject.gif";
064 public static final String IMG_GPROJECT_ERROR = "icons/gproject_error.gif";
065 public static final String IMG_WIZARD = "icons/gdswiz.gif";
066
067 public static final RGB WHITE = new RGB(0xff, 0xff, 0xff);
068 public static final RGB LIGHT_RED = new RGB(0xff, 0x80, 0x80);
069
070 public static final Map<String, Image> IMAGES_CACHE = new HashMap<String, Image>();
071 public static final Map<RGB, Color> COLORS_CACHE = new HashMap<RGB, Color>();
072
073 public static Button newButton(Composite parent, String text, boolean enabled, SelectionListener listener) {
074 Button button = new Button(parent, SWT.NONE);
075 button.setText(" " + text + " ");
076 button.setEnabled(enabled);
077 if (listener != null)
078 button.addSelectionListener(listener);
079 return button;
080 }
081
082 public static Composite createGridComposite(Composite parent, int numColumns) {
083 Composite composite = new Composite(parent, SWT.NONE);
084 GridLayout layout = new GridLayout();
085 layout.numColumns = numColumns;
086 composite.setLayout(layout);
087
088 GridData data = new GridData();
089 data.verticalAlignment = GridData.FILL;
090 data.horizontalAlignment = GridData.FILL;
091 composite.setLayoutData(data);
092
093 return composite;
094 }
095
096 public static TreeItem addTreeItem(Tree tree, String image, String text, String prefix, String empty) {
097 TreeItem item = new TreeItem(tree, SWT.NONE);
098
099 item.setImage(getImage(tree.getDisplay(), image));
100
101 prefix = StringUtil.unNull(prefix);
102 empty = StringUtil.unNull(empty);
103 text = StringUtil.unNull(text);
104
105 item.setData(PREFIX_KEY, prefix);
106 item.setData(EMPTY_KEY, empty);
107 item.setData(VALUE_KEY, text);
108
109 text = prefix + (text.length() == 0 ? empty : text);
110
111 item.setText(text);
112
113 return item;
114 }
115
116 public static TreeItem addTreeItem(TreeItem treeItem, String image, String text, String prefix, String empty) {
117 TreeItem item = new TreeItem(treeItem, SWT.NONE);
118
119 item.setImage(getImage(treeItem.getDisplay(), image));
120
121 prefix = StringUtil.unNull(prefix);
122 empty = StringUtil.unNull(empty);
123 text = StringUtil.unNull(text);
124
125 item.setData(PREFIX_KEY, prefix);
126 item.setData(EMPTY_KEY, empty);
127 item.setData(VALUE_KEY, text);
128
129 text = prefix + (text.length() == 0 ? empty : text);
130
131 item.setText(text);
132
133 return item;
134 }
135
136 public static void setTreeItemText(TreeItem item, String text) {
137 if (item.getData(VALUE_KEY) != null) {
138 String prefix = StringUtil.unNull((String)item.getData(PREFIX_KEY));
139 String empty = StringUtil.unNull((String)item.getData(EMPTY_KEY));
140
141 item.setData(VALUE_KEY, text);
142
143 text = prefix + (text.length() == 0 ? empty : text);
144 }
145 item.setText(text);
146 }
147
148 public static String getTreeItemText(TreeItem item) {
149 if (item.getData(VALUE_KEY) != null)
150 return (String)item.getData(VALUE_KEY);
151 return item.getText();
152 }
153
154 public static GridData newGridData(int style, int horizontalSpan) {
155 GridData gd = new GridData(style);
156 gd.horizontalSpan = horizontalSpan;
157 return gd;
158 }
159
160 public static Image getImage(Device device, String path) {
161 Image image = IMAGES_CACHE.get(path);
162 if (image == null) {
163 image = new Image(device, SWTUtil.class.getClassLoader().getResourceAsStream(path));
164 IMAGES_CACHE.put(path, image);
165 }
166 return image;
167 }
168
169 public static Color getColor(Device device, RGB rgb) {
170 Color color = COLORS_CACHE.get(rgb);
171 if (color == null) {
172 color = new Color(device, rgb);
173 COLORS_CACHE.put(rgb, color);
174 }
175 return color;
176 }
177
178 public static Display getCurrentDisplay() {
179 Display display = Display.getCurrent();
180 if (display == null)
181 display = PlatformUI.createDisplay();
182 return display;
183 }
184 }