001: /*
002: * All content copyright (c) 2003-2007 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package org.terracotta.ui.util;
006:
007: import org.eclipse.jface.layout.GridDataFactory;
008: import org.eclipse.jface.layout.LayoutConstants;
009: import org.eclipse.jface.util.Geometry;
010: import org.eclipse.swt.SWT;
011: import org.eclipse.swt.custom.CCombo;
012: import org.eclipse.swt.custom.TableEditor;
013: import org.eclipse.swt.events.ControlAdapter;
014: import org.eclipse.swt.events.ControlEvent;
015: import org.eclipse.swt.events.SelectionAdapter;
016: import org.eclipse.swt.events.SelectionEvent;
017: import org.eclipse.swt.graphics.Color;
018: import org.eclipse.swt.graphics.FontMetrics;
019: import org.eclipse.swt.graphics.GC;
020: import org.eclipse.swt.graphics.Point;
021: import org.eclipse.swt.graphics.Rectangle;
022: import org.eclipse.swt.widgets.Button;
023: import org.eclipse.swt.widgets.Composite;
024: import org.eclipse.swt.widgets.Control;
025: import org.eclipse.swt.widgets.Event;
026: import org.eclipse.swt.widgets.Listener;
027: import org.eclipse.swt.widgets.Shell;
028: import org.eclipse.swt.widgets.Table;
029: import org.eclipse.swt.widgets.TableColumn;
030: import org.eclipse.swt.widgets.TableItem;
031: import org.eclipse.swt.widgets.Text;
032:
033: public final class SWTUtil {
034:
035: private SWTUtil() {
036: // cannot instantiate
037: }
038:
039: public static void makeIntField(final Text text) {
040: text.addListener(SWT.Verify, new Listener() {
041: public void handleEvent(Event e) {
042: if (e.widget.getDisplay() == null) {
043: e.widget.removeListener(SWT.Verify, this );
044: return;
045: }
046: String string = e.text;
047: char[] chars = new char[string.length()];
048: string.getChars(0, chars.length, chars, 0);
049: for (int i = 0; i < chars.length; i++) {
050: if (!('0' <= chars[i] && chars[i] <= '9')) {
051: e.doit = false;
052: return;
053: }
054: }
055: }
056: });
057: }
058:
059: public static void setBGColorRecurse(Color color, Control control) {
060: control.setBackground(color);
061: if (control instanceof Composite) {
062: Control[] children = ((Composite) control).getChildren();
063: for (int i = 0; i < children.length; i++) {
064: setBGColorRecurse(color, children[i]);
065: }
066: }
067: }
068:
069: public static Control getAncestorOfClass(Class clazz, Control comp) {
070: if (comp.getClass().equals(clazz))
071: return comp;
072: while ((comp = comp.getParent()) != null) {
073: if (clazz.isAssignableFrom(comp.getClass()))
074: return comp;
075: }
076: return null;
077: }
078:
079: public static int textColumnsToPixels(Control control, int columns) {
080: GC gc = new GC(control);
081: FontMetrics fm = gc.getFontMetrics();
082: int width = columns * fm.getAverageCharWidth();
083: int height = fm.getHeight();
084: gc.dispose();
085: return control.computeSize(width, height).x;
086: }
087:
088: public static int textRowsToPixels(Control control, int rows) {
089: GC gc = new GC(control);
090: FontMetrics fm = gc.getFontMetrics();
091: int height = rows * fm.getHeight();
092: gc.dispose();
093: return control.computeSize(0, height).y;
094: }
095:
096: public static int tableRowsToPixels(Table table, int rows) {
097: return table.getHeaderHeight() + (rows * table.getItemHeight());
098: }
099:
100: public static void applyDefaultButtonSize(Button button) {
101: Point preferredSize = button.computeSize(SWT.DEFAULT,
102: SWT.DEFAULT, false);
103: Point hint = Geometry.max(LayoutConstants.getMinButtonSize(),
104: preferredSize);
105: GridDataFactory.fillDefaults().align(SWT.FILL, SWT.CENTER)
106: .hint(hint).applyTo(button);
107: }
108:
109: public static void placeDialogInCenter(Shell parent, Shell shell) {
110: Rectangle parentSize = parent.getBounds();
111: Rectangle mySize = shell.getBounds();
112: int locationX, locationY;
113: locationX = (parentSize.width - mySize.width) / 2
114: + parentSize.x;
115: locationY = (parentSize.height - mySize.height) / 2
116: + parentSize.y;
117: shell.setLocation(new Point(locationX, locationY));
118: }
119:
120: public static void makeTableColumnsResizeEqualWidth(
121: final Composite tablePanel, final Table table) {
122: final Control control = table;
123: control.addControlListener(new ControlAdapter() {
124: public void controlResized(ControlEvent e) {
125: if (e.widget.getDisplay() == null) {
126: ((Control) e.widget).removeControlListener(this );
127: return;
128: }
129: Rectangle area = control.getBounds();
130: int widthHint = SWT.DEFAULT;
131: int heightHint = SWT.DEFAULT;
132: Point preferredSize = table.computeSize(widthHint,
133: heightHint);
134: int width = area.width - 2 * table.getBorderWidth();
135: if (preferredSize.y > area.height
136: + table.getHeaderHeight()) {
137: Point vBarSize = table.getVerticalBar().getSize();
138: width -= vBarSize.x + 1; // don't know why +1 is needed, but it is
139: }
140: TableColumn[] columns = table.getColumns();
141: int colWidth = width / columns.length;
142: for (int i = 0; i < columns.length; i++) {
143: columns[i].setWidth(colWidth);
144: }
145: }
146: });
147: }
148:
149: public static void makeTableColumnsResizeWeightedWidth(
150: final Composite tablePanel, final Table table,
151: final int[] columnWeights) {
152: int weight = 0;
153: for (int i = 0; i < columnWeights.length; i++) {
154: weight += columnWeights[i];
155: }
156: final int totalWeight = weight;
157: tablePanel.addControlListener(new ControlAdapter() {
158: public void controlResized(ControlEvent e) {
159: if (e.widget.getDisplay() == null) {
160: ((Control) e.widget).removeControlListener(this );
161: return;
162: }
163: Rectangle area = tablePanel.getClientArea();
164: Point preferredSize = table.computeSize(SWT.DEFAULT,
165: SWT.DEFAULT);
166: int width = area.width - 2 * table.getBorderWidth();
167: if (preferredSize.y > area.height
168: + table.getHeaderHeight()) {
169: Point vBarSize = table.getVerticalBar().getSize();
170: width -= vBarSize.x + 1; // don't know why +1 is needed, but it is
171: }
172: TableColumn[] columns = table.getColumns();
173: int widthUnit = width / totalWeight;
174: for (int i = 0; i < columns.length; i++) {
175: columns[i].setWidth(widthUnit * columnWeights[i]);
176: }
177: }
178: });
179: }
180:
181: public static void makeTableColumnsEditable(final Table table,
182: final int[] indices) {
183: final TableEditor editor = new TableEditor(table);
184: editor.horizontalAlignment = SWT.LEFT;
185: editor.grabHorizontal = true;
186: table.addListener(SWT.MouseDown, new Listener() {
187: public void handleEvent(Event event) {
188: if (event.widget.getDisplay() == null) {
189: event.widget.removeListener(SWT.MouseDown, this );
190: return;
191: }
192: Rectangle clientArea = table.getClientArea();
193: Point pt = new Point(event.x, event.y);
194: int index = table.getTopIndex();
195: while (index < table.getItemCount()) {
196: boolean visible = false;
197: final TableItem item = table.getItem(index);
198: for (int i = 0; i < indices.length; i++) {
199: Rectangle rect = item.getBounds(indices[i]);
200: if (rect.contains(pt)) {
201: final int column = indices[i];
202: final Text text = new Text(table, SWT.NONE);
203: final String initialValue = item
204: .getText(indices[i]);
205: Listener textListener = new Listener() {
206: public void handleEvent(final Event e) {
207: Event updateEvent = new Event();
208: switch (e.type) {
209: case SWT.FocusOut:
210: item.setText(column, text
211: .getText());
212: if (!initialValue.equals(text
213: .getText())) {
214: updateEvent.item = item;
215: updateEvent.index = column;
216: table.notifyListeners(
217: SWT.SetData,
218: updateEvent);
219: }
220: text.dispose();
221: break;
222: case SWT.Traverse:
223: switch (e.detail) {
224: case SWT.TRAVERSE_RETURN:
225: item.setText(column, text
226: .getText());
227: if (!initialValue
228: .equals(text
229: .getText())) {
230: updateEvent.item = item;
231: updateEvent.index = column;
232: table.notifyListeners(
233: SWT.SetData,
234: updateEvent);
235: }
236: text.dispose();
237: e.doit = false;
238: break;
239: case SWT.TRAVERSE_ESCAPE:
240: text.dispose();
241: e.doit = false;
242: break;
243: }
244: break;
245: }
246: }
247: };
248: text
249: .addListener(SWT.FocusOut,
250: textListener);
251: text
252: .addListener(SWT.Traverse,
253: textListener);
254: editor.setEditor(text, item, indices[i]);
255: text.setText(initialValue);
256: text.setFocus();
257: return;
258: }
259: if (!visible && rect.intersects(clientArea)) {
260: visible = true;
261: }
262: }
263: if (!visible)
264: return;
265: index++;
266: }
267: }
268: });
269: }
270:
271: public static void makeTableComboItem(final Table table,
272: final int column, final String[] values) {
273: final TableEditor editor = new TableEditor(table);
274: editor.horizontalAlignment = SWT.LEFT;
275: editor.grabHorizontal = true;
276: table.addListener(SWT.MouseDown, new Listener() {
277: public void handleEvent(Event event) {
278: if (event.widget.getDisplay() == null) {
279: event.widget.removeListener(SWT.MouseDown, this );
280: return;
281: }
282: Rectangle clientArea = table.getClientArea();
283: Point pt = new Point(event.x, event.y);
284: int index = table.getTopIndex();
285: while (index < table.getItemCount()) {
286: boolean visible = false;
287: final TableItem item = table.getItem(index);
288: final Rectangle rect = item.getBounds(column);
289: if (rect.contains(pt)) {
290: final CCombo combo = new CCombo(table,
291: SWT.READ_ONLY);
292: for (int i = 0; i < values.length; i++) {
293: combo.add(values[i]);
294: }
295: final String initialValue = item
296: .getText(column);
297: final boolean[] isMouseOverCombo = new boolean[] { false };
298: Listener comboListener = new Listener() {
299: public void handleEvent(final Event e) {
300: Event updateEvent = new Event();
301: switch (e.type) {
302: case SWT.FocusOut:
303: if (isMouseOverCombo[0])
304: return;
305: if (combo.getSelectionIndex() != -1
306: && !initialValue
307: .equals(combo
308: .getText())) {
309: item.setText(column, combo
310: .getText());
311: updateEvent.item = item;
312: updateEvent.index = column;
313: table.notifyListeners(
314: SWT.SetData,
315: updateEvent);
316: }
317: combo.dispose();
318: break;
319: case SWT.Traverse:
320: switch (e.detail) {
321: case SWT.TRAVERSE_RETURN:
322: if (combo.getSelectionIndex() != -1
323: && !initialValue
324: .equals(combo
325: .getText())) {
326: item.setText(column, combo
327: .getText());
328: updateEvent.item = item;
329: updateEvent.index = column;
330: table.notifyListeners(
331: SWT.SetData,
332: updateEvent);
333: }
334: combo.dispose();
335: e.doit = false;
336: break;
337: case SWT.TRAVERSE_ESCAPE:
338: combo.dispose();
339: e.doit = false;
340: break;
341: }
342: break;
343: }
344: }
345: };
346: combo.addListener(SWT.FocusOut, comboListener);
347: combo.addListener(SWT.Traverse, comboListener);
348: combo.addListener(SWT.MouseEnter,
349: new Listener() {
350: public void handleEvent(Event e) {
351: isMouseOverCombo[0] = true;
352: }
353: });
354: combo.addListener(SWT.MouseExit,
355: new Listener() {
356: public void handleEvent(Event e) {
357: isMouseOverCombo[0] = false;
358: }
359: });
360: combo
361: .addSelectionListener(new SelectionAdapter() {
362: public void widgetSelected(
363: SelectionEvent e) {
364: Event updateEvent = new Event();
365: item.setText(column, combo
366: .getText());
367: updateEvent.item = item;
368: updateEvent.index = column;
369: table.notifyListeners(
370: SWT.SetData,
371: updateEvent);
372: combo.dispose();
373: }
374: });
375: editor.setEditor(combo, item, column);
376: combo.select(combo
377: .indexOf(item.getText(column)));
378: combo.setFocus();
379: return;
380: }
381: if (!visible && rect.intersects(clientArea)) {
382: visible = true;
383: }
384: if (!visible)
385: return;
386: index++;
387: }
388: }
389: });
390: }
391: }
|