001: /*
002: * Copyright 2006 Pentaho Corporation. All rights reserved.
003: * This software was developed by Pentaho Corporation and is provided under the terms
004: * of the Mozilla Public License, Version 1.1, or any later version. You may not use
005: * this file except in compliance with the license. If you need a copy of the license,
006: * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
007: * BI Platform. The Initial Developer is Pentaho Corporation.
008: *
009: * Software distributed under the Mozilla Public License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
011: * the license for the specific language governing your rights and limitations.
012: *
013: * @created Feb 01, 2006
014: * @author Michael D'Amour
015: */
016: package org.pentaho.jfreereport.wizard.utility;
017:
018: import java.io.File;
019: import java.io.FilenameFilter;
020: import java.util.Arrays;
021: import org.eclipse.swt.SWT;
022: import org.eclipse.swt.graphics.Color;
023: import org.eclipse.swt.graphics.Image;
024: import org.eclipse.swt.graphics.Point;
025: import org.eclipse.swt.graphics.RGB;
026: import org.eclipse.swt.widgets.Combo;
027: import org.eclipse.swt.widgets.Composite;
028: import org.eclipse.swt.widgets.Control;
029: import org.eclipse.swt.widgets.Display;
030: import org.eclipse.swt.widgets.Layout;
031: import org.eclipse.swt.widgets.List;
032: import org.eclipse.swt.widgets.Shell;
033: import org.eclipse.swt.widgets.Spinner;
034: import org.eclipse.swt.widgets.TabFolder;
035: import org.eclipse.swt.widgets.Table;
036: import org.eclipse.swt.widgets.Text;
037: import org.pentaho.jfreereport.wizard.ui.swt.SWTButton;
038:
039: public class SWTUtility {
040: public static final String COPYRIGHTMESSAGE = "Copyright \u00A9 2005-2007 Pentaho Corporation. All rights reserved. \nThis software was developed by Pentaho Corporation and is provided under the terms\nof the Mozilla Public License, Version 1.1, or any later version. You may not use\nthis file except in compliance with the license. If you need a copy of the license,\nplease go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho BI Platform.\nThe Initial Developer is Pentaho Corporation.\n\nSoftware distributed under the Mozilla Public License is distributed on an \"AS IS\"\nbasis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to\nthe license for the specific language governing your rights and limitations."; //$NON-NLS-1$
041: private static Image icon;
042: private static FilenameFilter xmlFilter;
043: public static final String[] REPORT_SPEC_FILTERS = new String[] {
044: "*.xreportspec", "*.xml", "*.xreportarc" }; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
045:
046: public static final String[] REPORT_ARCHIVE_FILTERS = new String[] {
047: ".xreportarc", ".zip" }; //$NON-NLS-1$ //$NON-NLS-2$
048:
049: public static final String[] XML_FILTER_STRINGS = new String[] {
050: "*.xml", "*.XML" }; //$NON-NLS-1$ //$NON-NLS-2$
051:
052: public static final String[] IMAGE_FILTER_STRINGS = new String[] {
053: "*.jpg", "*.gif", "*.png", "*.bmp" }; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
054:
055: public static final String[] KETTLE_TRANSFORMATION_FILE = new String[] { "*.ktr" }; // $NON_NLS-1$
056:
057: public static void setIcon(Image image) {
058: icon = image;
059: }
060:
061: public static Image getIcon() {
062: return icon;
063: }
064:
065: public static void centerShellOnDisplay(Shell shell,
066: Display display, int desiredWidth, int desiredHeight) {
067: int screenWidth = display.getPrimaryMonitor().getBounds().width;
068: int screenHeight = display.getPrimaryMonitor().getBounds().height;
069: int applicationX = ((Math.abs(screenWidth - desiredWidth)) / 2);
070: int applicationY = ((Math.abs(screenHeight - desiredHeight)) / 2);
071: shell.setSize(desiredWidth, desiredHeight);
072: shell.setLocation(applicationX, applicationY);
073: if (icon != null) {
074: shell.setImage(icon);
075: }
076: }
077:
078: public static Shell createModalDialogShell(int desiredWidth,
079: int desiredHeight, String title) {
080: Shell shell = new Shell(Display.getCurrent(), SWT.DIALOG_TRIM
081: | SWT.APPLICATION_MODAL);
082: shell.setText(title);
083: centerShellOnDisplay(shell, Display.getCurrent(), desiredWidth,
084: desiredHeight);
085: return shell;
086: }
087:
088: public static Shell createShell(int desiredWidth,
089: int desiredHeight, Layout layout, String title, int style) {
090: Shell shell = new Shell(Display.getCurrent(), style);
091: if (layout != null) {
092: shell.setLayout(layout);
093: }
094: shell.setText(title);
095: centerShellOnDisplay(shell, Display.getCurrent(), desiredWidth,
096: desiredHeight);
097: return shell;
098: }
099:
100: public static void removeChildren(Composite c) {
101: if (c != null) {
102: Control children[] = c.getChildren();
103: for (int i = 0; i < children.length; i++) {
104: children[i].dispose();
105: }
106: }
107: }
108:
109: public static void setEnabled(Composite c, boolean enabled) {
110: // if (c == null) {
111: // return;
112: // }
113: // c.setEnabled(enabled);
114: // // System.out.println(c + ":" + enabled);
115: // Control children[] = c.getChildren();
116: // for (int i = 0; i < children.length; i++) {
117: // if (children[i] instanceof Composite) {
118: // setEnabled((Composite) children[i], enabled);
119: // } else {
120: // children[i].setEnabled(enabled);
121: // // System.out.println(children[i] + ":" + enabled);
122: // }
123: // }
124: }
125:
126: public static void setBackground(Composite c, Color background) {
127: if (!(c instanceof SWTButton)) {
128: c.setBackground(background);
129: }
130: // System.out.println(c + ":" + enabled);
131: Control children[] = c.getChildren();
132: for (int i = 0; i < children.length; i++) {
133: if (children[i] instanceof Text) {
134: // ignore if not READ_ONLY
135: if ((children[i].getStyle() & SWT.READ_ONLY) == SWT.READ_ONLY) {
136: children[i].setBackground(background);
137: }
138: } else if (children[i] instanceof Table) {
139: // ignore
140: } else if (children[i] instanceof Combo) {
141: // ignore
142: } else if (children[i] instanceof List) {
143: // ignore
144: } else if (children[i] instanceof Spinner) {
145: // ignore
146: } else if (children[i] instanceof Composite) {
147: setBackground((Composite) children[i], background);
148: } else if (children[i] instanceof TabFolder) {
149: System.out.println("TabFolder..");
150: TabFolder folder = (TabFolder) children[i];
151: folder.setBackground(background);
152: for (int j = 0; j < folder.getItemCount(); j++) {
153: setBackground((Composite) folder.getItem(j)
154: .getControl(), background);
155: }
156: } else if (!(children[i] instanceof SWTButton)) {
157: children[i].setBackground(background);
158: // System.out.println(children[i] + ":" + enabled);
159: }
160: }
161: }
162:
163: public static void moveSelectedItems(List sourceList, int index) {
164: int selectedIndices[] = sourceList.getSelectionIndices();
165: Arrays.sort(selectedIndices);
166: boolean moveUp = (selectedIndices[0] - index) > 0;
167: int numMove = Math.abs(selectedIndices[0] - index);
168: for (int i = 0; i < numMove; i++) {
169: moveSelectedItems(sourceList, moveUp);
170: }
171: }
172:
173: public static void moveSelectedItems(List sourceList,
174: List destinationList) {
175: int selectedIndices[] = sourceList.getSelectionIndices();
176: Arrays.sort(selectedIndices);
177: int newSelectedIndices[] = new int[selectedIndices.length];
178: for (int i = 0; i < selectedIndices.length; i++) {
179: destinationList.add(sourceList.getItem(selectedIndices[i]));
180: newSelectedIndices[i] = destinationList.getItemCount() - 1;
181: }
182: destinationList.setSelection(newSelectedIndices);
183: sourceList.remove(selectedIndices);
184: }
185:
186: public static void moveSelectedItems(List list, boolean moveUp) {
187: int selectedIndices[] = list.getSelectionIndices();
188: Arrays.sort(selectedIndices);
189: if (moveUp) {
190: // invert array
191: for (int left = 0, right = selectedIndices.length - 1; left < right; left++, right--) {
192: // exchange the first and last
193: int temp = selectedIndices[left];
194: selectedIndices[left] = selectedIndices[right];
195: selectedIndices[right] = temp;
196: }
197: }
198: int newSelectedIndices[] = new int[selectedIndices.length];
199: for (int i = selectedIndices.length - 1; i >= 0; i--) {
200: int index = selectedIndices[i];
201: if ((moveUp && index > 0)
202: || (!moveUp && index < list.getItemCount() - 1)) {
203: index = moveUp ? index - 1 : index + 1;
204: String items[] = list.getItems();
205: for (int j = 0; j < items.length; j++) {
206: if (j == selectedIndices[i]) {
207: // swap
208: String item = items[j];
209: items[j] = items[index];
210: items[index] = item;
211: j++;
212: }
213: }
214: list.removeAll();
215: list.setItems(items);
216: }
217: newSelectedIndices[i] = index;
218: }
219: list.setSelection(newSelectedIndices);
220: }
221:
222: public static int getListIndexForPoint(List list, Point p) {
223: int height = list.getItemHeight();
224: int index = p.y / height;
225: return index;
226: }
227:
228: public static String getListItemForPoint(List list, Point p) {
229: int height = list.getItemHeight();
230: int index = p.y / height;
231: try {
232: return list.getItem(index);
233: } catch (Exception e) {
234: e.printStackTrace();
235: }
236: return null;
237: }
238:
239: public static void getChildrenOfType(java.util.List resultList,
240: Control parent, Class classClass) {
241: if (parent instanceof Composite) {
242: Control children[] = ((Composite) parent).getChildren();
243: for (int i = 0; i < children.length; i++) {
244: if (children[i] instanceof Composite) {
245: if (children[i].getClass().getName().equals(
246: classClass.getName())) {
247: resultList.add(children[i]);
248: }
249: getChildrenOfType(resultList, children[i],
250: classClass);
251: } else {
252: if (children[i].getClass().getName().equals(
253: classClass.getName())) {
254: resultList.add(children[i]);
255: }
256: }
257: }
258: }
259: }
260:
261: public static FilenameFilter getXMLFilenameFilter() {
262: if (xmlFilter == null) {
263: xmlFilter = new FilenameFilter() {
264: public boolean accept(File dir, String name) {
265: if (name.toUpperCase().indexOf(".XML") >= 0) { //$NON-NLS-1$
266: return true;
267: }
268: return false;
269: }
270: };
271: }
272: return xmlFilter;
273: }
274:
275: public static String getJFreeColorString(RGB color) {
276: if (color == null) {
277: return "#FF000"; //$NON-NLS-1$
278: }
279: String r = Integer.toHexString(color.red);
280: if (r.length() == 1) {
281: r = "0" + r; //$NON-NLS-1$
282: }
283: String g = Integer.toHexString(color.green);
284: if (g.length() == 1) {
285: g = "0" + g; //$NON-NLS-1$
286: }
287: String b = Integer.toHexString(color.blue);
288: if (b.length() == 1) {
289: b = "0" + b; //$NON-NLS-1$
290: }
291: return "#" + r + g + b; //$NON-NLS-1$
292: }
293:
294: public static RGB getRGB(String colorSpec) {
295: int red = getRed(colorSpec);
296: int green = getGreen(colorSpec);
297: int blue = getBlue(colorSpec);
298: return new RGB(red, green, blue);
299: }
300:
301: public static int getRed(String colorSpec) {
302: return Integer.parseInt(colorSpec.substring(1, 3), 16);
303: }
304:
305: public static int getGreen(String colorSpec) {
306: return Integer.parseInt(colorSpec.substring(3, 5), 16);
307: }
308:
309: public static int getBlue(String colorSpec) {
310: return Integer.parseInt(colorSpec.substring(5, 7), 16);
311: }
312: }
|