Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 237846 | Differences between
and this patch

Collapse All | Expand All

(-)src/org/eclipse/swt/nebula/snippets/grid/GridSnippet2.java (+10 lines)
Lines 15-23 Link Here
15
import org.eclipse.nebula.widgets.grid.GridColumn;
15
import org.eclipse.nebula.widgets.grid.GridColumn;
16
import org.eclipse.nebula.widgets.grid.GridItem;
16
import org.eclipse.nebula.widgets.grid.GridItem;
17
import org.eclipse.swt.SWT;
17
import org.eclipse.swt.SWT;
18
import org.eclipse.swt.custom.CCombo;
18
import org.eclipse.swt.layout.FillLayout;
19
import org.eclipse.swt.layout.FillLayout;
19
import org.eclipse.swt.widgets.Display;
20
import org.eclipse.swt.widgets.Display;
20
import org.eclipse.swt.widgets.Shell;
21
import org.eclipse.swt.widgets.Shell;
22
import org.eclipse.swt.widgets.Text;
21
23
22
/*
24
/*
23
 * Create a grid with an item that spans columns.
25
 * Create a grid with an item that spans columns.
Lines 34-45 Link Here
34
36
35
    Grid grid = new Grid(shell,SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
37
    Grid grid = new Grid(shell,SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
36
    grid.setHeaderVisible(true);
38
    grid.setHeaderVisible(true);
39
    
37
    GridColumn column = new GridColumn(grid,SWT.NONE);
40
    GridColumn column = new GridColumn(grid,SWT.NONE);
38
    column.setText("Column 1");
41
    column.setText("Column 1");
39
    column.setWidth(100);
42
    column.setWidth(100);
43
    column.setHeaderControl(new CCombo(grid,SWT.READ_ONLY|SWT.BORDER));
44
    column.setMoveable(true);
45
    
40
    GridColumn column2 = new GridColumn(grid,SWT.NONE);
46
    GridColumn column2 = new GridColumn(grid,SWT.NONE);
41
    column2.setText("Column 2");
47
    column2.setText("Column 2");
42
    column2.setWidth(100);
48
    column2.setWidth(100);
49
    column2.setMoveable(true);
50
    column2.setHeaderControl(new Text(grid,SWT.NONE));
51
    
52
    
43
    GridItem item1 = new GridItem(grid,SWT.NONE);
53
    GridItem item1 = new GridItem(grid,SWT.NONE);
44
    item1.setText("First Item");
54
    item1.setText("First Item");
45
    item1.setText(1,"xxxxxxx");
55
    item1.setText(1,"xxxxxxx");
(-)src/org/eclipse/swt/nebula/snippets/cdatetime/CDTSnippet01.java (-1 / +17 lines)
Lines 3-12 Link Here
3
import org.eclipse.nebula.widgets.cdatetime.CDT;
3
import org.eclipse.nebula.widgets.cdatetime.CDT;
4
import org.eclipse.nebula.widgets.cdatetime.CDateTime;
4
import org.eclipse.nebula.widgets.cdatetime.CDateTime;
5
import org.eclipse.swt.SWT;
5
import org.eclipse.swt.SWT;
6
import org.eclipse.swt.events.SelectionAdapter;
7
import org.eclipse.swt.events.SelectionEvent;
6
import org.eclipse.swt.graphics.Point;
8
import org.eclipse.swt.graphics.Point;
7
import org.eclipse.swt.graphics.Rectangle;
9
import org.eclipse.swt.graphics.Rectangle;
8
import org.eclipse.swt.layout.GridData;
10
import org.eclipse.swt.layout.GridData;
9
import org.eclipse.swt.layout.GridLayout;
11
import org.eclipse.swt.layout.GridLayout;
12
import org.eclipse.swt.widgets.Button;
10
import org.eclipse.swt.widgets.Display;
13
import org.eclipse.swt.widgets.Display;
11
import org.eclipse.swt.widgets.Shell;
14
import org.eclipse.swt.widgets.Shell;
12
15
Lines 25-33 Link Here
25
		GridLayout layout = new GridLayout();
28
		GridLayout layout = new GridLayout();
26
		shell.setLayout(layout);
29
		shell.setLayout(layout);
27
30
28
		final CDateTime cdt = new CDateTime(shell, CDT.BORDER | CDT.DROP_DOWN);
31
		final CDateTime cdt = new CDateTime(shell, CDT.BORDER | CDT.DROP_DOWN | CDT.DATE_LONG | CDT.TIME_SHORT );
29
		cdt.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
32
		cdt.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
30
33
34
		
35
		Button b = new Button(shell,SWT.PUSH);
36
		b.addSelectionListener(new SelectionAdapter() {
37
38
			@Override
39
			public void widgetSelected(SelectionEvent e) {
40
				System.err.println(cdt.getSelection());
41
			}
42
			
43
		});
44
		
31
		shell.pack();
45
		shell.pack();
32
		Point size = shell.getSize();
46
		Point size = shell.getSize();
33
		Rectangle screen = display.getMonitors()[0].getBounds();
47
		Rectangle screen = display.getMonitors()[0].getBounds();
Lines 37-42 Link Here
37
				size.x,
51
				size.x,
38
				size.y
52
				size.y
39
		);
53
		);
54
		
55
		
40
		shell.open();
56
		shell.open();
41
		while (!shell.isDisposed()) {
57
		while (!shell.isDisposed()) {
42
			if (!display.readAndDispatch())
58
			if (!display.readAndDispatch())
(-)META-INF/MANIFEST.MF (-6 / +3 lines)
Lines 9-22 Link Here
9
 org.eclipse.core.runtime,
9
 org.eclipse.core.runtime,
10
 org.eclipse.nebula.jface.gridviewer,
10
 org.eclipse.nebula.jface.gridviewer,
11
 org.eclipse.nebula.widgets.cdatetime,
11
 org.eclipse.nebula.widgets.cdatetime,
12
 org.eclipse.nebula.widgets.ctree,
13
 org.eclipse.nebula.widgets.datechooser,
14
 org.eclipse.nebula.widgets.formattedtext,
15
 org.eclipse.nebula.widgets.gallery,
16
 org.eclipse.nebula.widgets.grid,
12
 org.eclipse.nebula.widgets.grid,
17
 org.eclipse.nebula.widgets.pgroup,
18
 org.eclipse.nebula.widgets.pshelf,
13
 org.eclipse.nebula.widgets.pshelf,
19
 org.eclipse.nebula.widgets.compositetable
14
 org.eclipse.nebula.widgets.calendarcombo;bundle-version="1.0.0",
15
 org.eclipse.nebula.widgets.datechooser;bundle-version="1.0.0",
16
 org.eclipse.nebula.widgets.formattedtext;bundle-version="1.0.0"
20
Export-Package: org.eclipse.swt.nebula.snippets.ctree,
17
Export-Package: org.eclipse.swt.nebula.snippets.ctree,
21
 org.eclipse.swt.nebula.snippets.grid
18
 org.eclipse.swt.nebula.snippets.grid
22
Bundle-ClassPath: .
19
Bundle-ClassPath: .
(-).classpath (-1 / +1 lines)
Lines 1-7 Link Here
1
<?xml version="1.0" encoding="UTF-8"?>
1
<?xml version="1.0" encoding="UTF-8"?>
2
<classpath>
2
<classpath>
3
	<classpathentry kind="src" path="src"/>
4
	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
3
	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
5
	<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
4
	<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
5
	<classpathentry kind="src" path="src"/>
6
	<classpathentry kind="output" path="bin"/>
6
	<classpathentry kind="output" path="bin"/>
7
</classpath>
7
</classpath>
(-)src/org/eclipse/nebula/snippets/gridviewer/Test.java (+223 lines)
Added Link Here
1
package org.eclipse.nebula.snippets.gridviewer;
2
3
import java.util.regex.Matcher;
4
import java.util.regex.Pattern;
5
6
import org.eclipse.jface.viewers.ArrayContentProvider;
7
import org.eclipse.jface.viewers.CellEditor;
8
import org.eclipse.jface.viewers.ColumnLabelProvider;
9
import org.eclipse.jface.viewers.ColumnViewerEditor;
10
import org.eclipse.jface.viewers.ColumnViewerEditorActivationEvent;
11
import org.eclipse.jface.viewers.ColumnViewerEditorActivationListener;
12
import org.eclipse.jface.viewers.ColumnViewerEditorActivationStrategy;
13
import org.eclipse.jface.viewers.ColumnViewerEditorDeactivationEvent;
14
import org.eclipse.jface.viewers.ICellModifier;
15
import org.eclipse.jface.viewers.TextCellEditor;
16
import org.eclipse.nebula.jface.gridviewer.GridTableViewer;
17
import org.eclipse.nebula.jface.gridviewer.GridViewerColumn;
18
import org.eclipse.nebula.jface.gridviewer.GridViewerEditor;
19
import org.eclipse.nebula.widgets.grid.Grid;
20
import org.eclipse.nebula.widgets.grid.GridItem;
21
import org.eclipse.swt.SWT;
22
import org.eclipse.swt.layout.FillLayout;
23
import org.eclipse.swt.widgets.Display;
24
import org.eclipse.swt.widgets.Shell;
25
26
27
public class Test
28
{
29
    private Pattern m_IsValidPattern = null;
30
31
    public class MyModel
32
    {
33
        private String sCounter = "";
34
35
        public MyModel(){
36
        }
37
38
        public void setCounter(String sCounter)
39
        {
40
            this.sCounter = sCounter;
41
        }
42
43
        public String toString()
44
        {
45
            return sCounter;
46
        }
47
    }
48
49
    public class MyTextCellEditor extends TextCellEditor
50
    {
51
        /**
52
         * search pattern is Numeric character
53
         */
54
        private Pattern m_IsValidPattern = null;
55
56
        public MyTextCellEditor(Grid parent) {
57
            super(parent);
58
            m_IsValidPattern = Pattern.compile("[\\w+-\\.]");
59
        }
60
61
        @Override
62
        public void activate(ColumnViewerEditorActivationEvent activationEvent) {
63
64
            // set the activation character
65
            String s1 = String.valueOf( activationEvent.character );
66
            Matcher matcher = m_IsValidPattern.matcher( s1 );
67
            if (matcher.matches()) {
68
                doSetValue(s1);
69
            }
70
            super.activate(activationEvent);
71
        }
72
    }
73
74
75
    public Test(Shell shell)
76
    {
77
        /* allow numbers and characters */
78
        m_IsValidPattern = Pattern.compile("[\\w+-\\.]");
79
80
        final GridTableViewer v = new GridTableViewer(shell, SWT.BORDER
81
                | SWT.H_SCROLL | SWT.V_SCROLL);
82
83
84
        v.setCellEditors(new CellEditor[] {
85
                new MyTextCellEditor(v.getGrid()),
86
                new MyTextCellEditor(v.getGrid())
87
        });
88
89
        v.setCellModifier(new ICellModifier() {
90
91
            public boolean canModify(Object element, String property) {
92
                return true;
93
            }
94
95
            public Object getValue(Object element, String property) {
96
                return element.toString();
97
            }
98
99
            public void modify(Object element, String property, Object value) {
100
                if (element instanceof GridItem) {
101
                    GridItem item = (GridItem)element;
102
                    MyModel model = (MyModel)(item.getData());
103
                    model.setCounter(value.toString());
104
                    v.update(model, null);
105
                }
106
            }
107
        });
108
109
110
        ColumnViewerEditorActivationListener listener = new ColumnViewerEditorActivationListener() {
111
112
            public void afterEditorActivated(
113
                    ColumnViewerEditorActivationEvent event)
114
            {
115
            }
116
117
            public void afterEditorDeactivated(
118
                    ColumnViewerEditorDeactivationEvent event)
119
            {
120
            }
121
122
            public void beforeEditorActivated(
123
                    ColumnViewerEditorActivationEvent event)
124
            {
125
            }
126
127
            public void beforeEditorDeactivated(
128
                    ColumnViewerEditorDeactivationEvent event)
129
            {
130
            }
131
        };
132
133
        v.setContentProvider(new ArrayContentProvider());
134
        v.setColumnProperties(new String[] { "1", "2" });
135
        v.getGrid().setCellSelectionEnabled(true);
136
        v.getGrid().setLinesVisible(true);
137
        v.getGrid().setHeaderVisible(true);
138
139
        ColumnViewerEditorActivationStrategy actSupport = new ColumnViewerEditorActivationStrategy(
140
                v) {
141
            protected boolean isEditorActivationEvent(
142
                    ColumnViewerEditorActivationEvent event)
143
            {
144
                boolean result;
145
146
                String s1          = String.valueOf( event.character );
147
                Matcher matcher    = m_IsValidPattern.matcher( s1 );
148
                boolean bIsCharKey = matcher.matches();
149
150
                boolean bEnableKey =
151
                    (event.keyCode == SWT.CR) ||
152
                    (event.keyCode == SWT.KEYPAD_CR) ||
153
                    (event.keyCode == SWT.F2) ||
154
                    bIsCharKey;
155
156
                result = event.eventType == ColumnViewerEditorActivationEvent.TRAVERSAL
157
                        || event.eventType == ColumnViewerEditorActivationEvent.MOUSE_DOUBLE_CLICK_SELECTION
158
                        || (event.eventType == ColumnViewerEditorActivationEvent.KEY_PRESSED && bEnableKey)
159
                        || event.eventType == ColumnViewerEditorActivationEvent.PROGRAMMATIC;
160
161
                return result;
162
            }
163
        };
164
165
        GridViewerEditor.create(v, actSupport,
166
                ColumnViewerEditor.TABBING_HORIZONTAL
167
                        | ColumnViewerEditor.TABBING_MOVE_TO_ROW_NEIGHBOR
168
                        | ColumnViewerEditor.TABBING_VERTICAL
169
                        | ColumnViewerEditor.KEYBOARD_ACTIVATION);
170
171
        v.getColumnViewerEditor().addEditorActivationListener(listener);
172
173
        GridViewerColumn column = new GridViewerColumn(v, SWT.NONE);
174
        column.getColumn().setWidth(200);
175
        column.getColumn().setMoveable(true);
176
        column.getColumn().setText("Column 1");
177
        column.setLabelProvider(new ColumnLabelProvider());
178
179
        column = new GridViewerColumn(v, SWT.NONE);
180
        column.getColumn().setWidth(200);
181
        column.getColumn().setMoveable(true);
182
        column.getColumn().setText("Column 2");
183
        column.setLabelProvider(new ColumnLabelProvider());
184
185
        MyModel[] model = createModel();
186
187
        v.setInput(model);
188
    }
189
190
    private MyModel[] createModel() {
191
192
        MyModel[] elements = new MyModel[10];
193
194
        for (int i = 0; i < 10; i++)
195
        {
196
            elements[i] = new MyModel();
197
            if (i<3){
198
                Double h = Double.valueOf(i);
199
                elements[i].setCounter( h.toString() );
200
            }
201
        }
202
        return elements;
203
    }
204
205
    public static void main(String[] args)
206
    {
207
        Display display = new Display();
208
209
        Shell shell = new Shell(display);
210
        shell.setLayout(new FillLayout());
211
        new Test(shell);
212
        shell.open();
213
214
        while (!shell.isDisposed())
215
        {
216
            if (!display.readAndDispatch())
217
                display.sleep();
218
        }
219
220
        display.dispose();
221
222
    }
223
} 
(-)src/org/eclipse/nebula/widgets/grid/GridColumn.java (+26 lines)
Lines 18-23 Link Here
18
import org.eclipse.swt.graphics.GC;
18
import org.eclipse.swt.graphics.GC;
19
import org.eclipse.swt.graphics.Point;
19
import org.eclipse.swt.graphics.Point;
20
import org.eclipse.swt.graphics.Rectangle;
20
import org.eclipse.swt.graphics.Rectangle;
21
import org.eclipse.swt.widgets.Control;
21
import org.eclipse.swt.widgets.Event;
22
import org.eclipse.swt.widgets.Event;
22
import org.eclipse.swt.widgets.Item;
23
import org.eclipse.swt.widgets.Item;
23
import org.eclipse.swt.widgets.TypedListener;
24
import org.eclipse.swt.widgets.TypedListener;
Lines 40-45 Link Here
40
 */
41
 */
41
public class GridColumn extends Item
42
public class GridColumn extends Item
42
{
43
{
44
	private GridHeaderEditor controlEditor;
43
45
44
    /**
46
    /**
45
     * Default width of the column.
47
     * Default width of the column.
Lines 1086-1089 Link Here
1086
        cellRenderer.setWordWrap(wordWrap);
1088
        cellRenderer.setWordWrap(wordWrap);
1087
        parent.redraw();
1089
        parent.redraw();
1088
    }
1090
    }
1091
    
1092
    /**
1093
	 * Set a new editor at the top of the control. If there's an editor already
1094
	 * set it is disposed.
1095
	 * 
1096
	 * @param control
1097
	 *            the control to be displayed in the header
1098
	 */
1099
	public void setHeaderControl(Control control) {
1100
		if (this.controlEditor == null) {
1101
			this.controlEditor = new GridHeaderEditor(this);
1102
		}
1103
		this.controlEditor.setEditor(control);
1104
	}
1105
	
1106
	/**
1107
	 * @return the current header control
1108
	 */
1109
	public Control getHeaderControl() {
1110
		if( this.controlEditor != null ) {
1111
			return this.controlEditor.getEditor();
1112
		}
1113
		return null;
1114
	}
1089
}
1115
}
(-)src/org/eclipse/nebula/widgets/grid/GridHeaderRenderer.java (-34 / +46 lines)
Lines 4-43 Link Here
4
4
5
/**
5
/**
6
 * <p>
6
 * <p>
7
 * NOTE:  THIS WIDGET AND ITS API ARE STILL UNDER DEVELOPMENT.  THIS IS A PRE-RELEASE ALPHA 
7
 * NOTE: THIS WIDGET AND ITS API ARE STILL UNDER DEVELOPMENT. THIS IS A
8
 * VERSION.  USERS SHOULD EXPECT API CHANGES IN FUTURE VERSIONS.
8
 * PRE-RELEASE ALPHA VERSION. USERS SHOULD EXPECT API CHANGES IN FUTURE
9
 * </p> 
9
 * VERSIONS.
10
 * The super class for all grid header renderers.  Contains the properties specific
10
 * </p>
11
 * to a grid header.
11
 * The super class for all grid header renderers. Contains the properties
12
 *
12
 * specific to a grid header.
13
 * 
13
 * @author chris.gross@us.ibm.com
14
 * @author chris.gross@us.ibm.com
14
 */
15
 */
15
public abstract class GridHeaderRenderer extends AbstractInternalWidget
16
public abstract class GridHeaderRenderer extends AbstractInternalWidget {
16
{
17
	
17
    /**
18
	/**
18
     * Returns the bounds of the text in the cell.  This is used when displaying in-place tooltips.
19
	 * Returns the bounds of the text in the cell. This is used when displaying
19
     * If <code>null</code> is returned here, in-place tooltips will not be displayed.  If the 
20
	 * in-place tooltips. If <code>null</code> is returned here, in-place
20
     * <code>preferred</code> argument is <code>true</code> then the returned bounds should be large
21
	 * tooltips will not be displayed. If the <code>preferred</code> argument is
21
     * enough to show the entire text.  If <code>preferred</code> is <code>false</code> then the 
22
	 * <code>true</code> then the returned bounds should be large enough to show
22
     * returned bounds should be be relative to the current bounds.
23
	 * the entire text. If <code>preferred</code> is <code>false</code> then the
23
     * 
24
	 * returned bounds should be be relative to the current bounds.
24
     * @param value the object being rendered.
25
	 * 
25
     * @param preferred true if the preferred width of the text should be returned.
26
	 * @param value
26
     * @return bounds of the text.
27
	 *            the object being rendered.
27
     */
28
	 * @param preferred
28
    public Rectangle getTextBounds(Object value, boolean preferred)
29
	 *            true if the preferred width of the text should be returned.
29
    {
30
	 * @return bounds of the text.
30
        return null;
31
	 */
31
    }
32
	public Rectangle getTextBounds(Object value, boolean preferred) {
32
    
33
		return null;
33
    /**
34
	}
34
     * Returns the bounds of the toggle within the header (typically only group headers have toggles)
35
35
     * or null.
36
	/**
36
     * 
37
	 * Returns the bounds of the toggle within the header (typically only group
37
     * @return toggle bounds or null if no toggle exists.
38
	 * headers have toggles) or null.
38
     */
39
	 * 
39
    public Rectangle getToggleBounds()
40
	 * @return toggle bounds or null if no toggle exists.
40
    {
41
	 */
41
        return null;
42
	public Rectangle getToggleBounds() {
42
    }
43
		return null;
44
	}
45
46
	/**
47
	 * Returns the bounds of the control to display
48
	 * 
49
	 * @return the bounds for the control or <code>null</code> if no control is
50
	 *         rendered
51
	 */
52
	protected Rectangle getControlBounds(Object value, boolean preferred) {
53
		return null;
54
	}
43
}
55
}
(-)src/org/eclipse/nebula/widgets/grid/internal/DefaultColumnHeaderRenderer.java (-279 / +276 lines)
Lines 7-13 Link Here
7
 *
7
 *
8
 * Contributors:
8
 * Contributors:
9
 *    chris.gross@us.ibm.com - initial API and implementation
9
 *    chris.gross@us.ibm.com - initial API and implementation
10
 *******************************************************************************/ 
10
 *******************************************************************************/
11
package org.eclipse.nebula.widgets.grid.internal;
11
package org.eclipse.nebula.widgets.grid.internal;
12
12
13
import org.eclipse.nebula.widgets.grid.GridColumn;
13
import org.eclipse.nebula.widgets.grid.GridColumn;
Lines 20-308 Link Here
20
20
21
/**
21
/**
22
 * The column header renderer.
22
 * The column header renderer.
23
 *
23
 * 
24
 * @author chris.gross@us.ibm.com
24
 * @author chris.gross@us.ibm.com
25
 * @since 2.0.0
25
 * @since 2.0.0
26
 */
26
 */
27
public class DefaultColumnHeaderRenderer extends GridHeaderRenderer
27
public class DefaultColumnHeaderRenderer extends GridHeaderRenderer {
28
{
29
28
30
    int leftMargin = 6;
29
	int leftMargin = 6;
31
30
32
    int rightMargin = 6;
31
	int rightMargin = 6;
33
32
34
    int topMargin = 3;
33
	int topMargin = 3;
35
34
36
    int bottomMargin = 3;
35
	int bottomMargin = 3;
37
36
38
    int arrowMargin = 6;
37
	int arrowMargin = 6;
39
38
40
    int imageSpacing = 3;
39
	int imageSpacing = 3;
41
40
42
    private SortArrowRenderer arrowRenderer = new SortArrowRenderer();
41
	private SortArrowRenderer arrowRenderer = new SortArrowRenderer();
43
42
44
    /** 
43
	/**
45
     * {@inheritDoc}
44
	 * {@inheritDoc}
46
     */
45
	 */
47
    public Point computeSize(GC gc, int wHint, int hHint, Object value)
46
	public Point computeSize(GC gc, int wHint, int hHint, Object value) {
48
    {
47
		GridColumn column = (GridColumn) value;
49
        GridColumn column = (GridColumn)value;
48
50
49
		int x = 0;
51
        int x = 0;
50
52
51
		x += leftMargin;
53
        x += leftMargin;
52
54
53
		x += gc.stringExtent(column.getText()).x + rightMargin;
55
        x += gc.stringExtent(column.getText()).x + rightMargin;
54
56
55
		int y = 0;
57
        int y = 0;
56
58
57
		y += topMargin;
59
        y += topMargin;
58
60
59
		y += gc.getFontMetrics().getHeight();
61
        y += gc.getFontMetrics().getHeight();
60
62
61
		y += bottomMargin;
63
        y += bottomMargin;
62
64
63
		if (column.getImage() != null) {
65
        if (column.getImage() != null)
64
			x += column.getImage().getBounds().width + imageSpacing;
66
        {
65
67
            x += column.getImage().getBounds().width + imageSpacing;
66
			y = Math.max(y, topMargin + column.getImage().getBounds().height
68
67
					+ bottomMargin);
69
            y = Math.max(y, topMargin + column.getImage().getBounds().height + bottomMargin);
68
		}
70
        }
69
		
71
70
		y += computeControlSize(column).y;
72
        return new Point(x, y);
71
73
    }
72
		return new Point(x, y);
74
73
	}
75
    /** 
74
76
     * {@inheritDoc}
75
	/**
77
     */
76
	 * {@inheritDoc}
78
    public void paint(GC gc, Object value)
77
	 */
79
    {
78
	public void paint(GC gc, Object value) {
80
        GridColumn column = (GridColumn)value;
79
		GridColumn column = (GridColumn) value;
81
80
82
        boolean flat = (column.getParent().getCellSelectionEnabled() && !column.getMoveable());
81
		boolean flat = (column.getParent().getCellSelectionEnabled() && !column
83
        
82
				.getMoveable());
84
        boolean drawSelected = ((isMouseDown() && isHover()));
83
85
84
		boolean drawSelected = ((isMouseDown() && isHover()));
86
        gc.setBackground(getDisplay().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
85
87
86
		gc.setBackground(getDisplay().getSystemColor(
88
        if (flat && isSelected())
87
				SWT.COLOR_WIDGET_BACKGROUND));
89
        {
88
90
            gc.setBackground(column.getParent().getCellHeaderSelectionBackground());
89
		if (flat && isSelected()) {
91
        }
90
			gc.setBackground(column.getParent()
92
        
91
					.getCellHeaderSelectionBackground());
93
        gc.fillRectangle(getBounds().x, getBounds().y, getBounds().width,
92
		}
94
                         getBounds().height);
93
95
94
		gc.fillRectangle(getBounds().x, getBounds().y, getBounds().width,
96
        int pushedDrawingOffset = 0;
95
				getBounds().height);
97
        if (drawSelected)
96
98
        {
97
		int pushedDrawingOffset = 0;
99
            pushedDrawingOffset = 1;
98
		if (drawSelected) {
100
        }
99
			pushedDrawingOffset = 1;
101
100
		}
102
        int x = leftMargin;
101
103
102
		int x = leftMargin;
104
        if (column.getImage() != null)
103
105
        {
104
		if (column.getImage() != null) {
106
                gc.drawImage(column.getImage(), getBounds().x + x + pushedDrawingOffset,
105
			gc.drawImage(column.getImage(), getBounds().x + x
107
                        getBounds().y + pushedDrawingOffset + getBounds().height - bottomMargin - column.getImage().getBounds().height);        		
106
					+ pushedDrawingOffset, getBounds().y + pushedDrawingOffset
108
            x += column.getImage().getBounds().width + imageSpacing;
107
					+ getBounds().height - bottomMargin
109
        }
108
					- column.getImage().getBounds().height);
110
109
			x += column.getImage().getBounds().width + imageSpacing;
111
        int width = getBounds().width - x;
110
		}
112
111
113
        if (column.getSort() == SWT.NONE)
112
		int width = getBounds().width - x;
114
        {
113
115
            width -= rightMargin;
114
		if (column.getSort() == SWT.NONE) {
116
        }
115
			width -= rightMargin;
117
        else
116
		} else {
118
        {
117
			width -= arrowMargin + arrowRenderer.getSize().x + arrowMargin;
119
            width -= arrowMargin + arrowRenderer.getSize().x + arrowMargin;
118
		}
120
        }
119
121
120
		gc.setForeground(getDisplay().getSystemColor(
122
        gc.setForeground(getDisplay().getSystemColor(SWT.COLOR_WIDGET_FOREGROUND));
121
				SWT.COLOR_WIDGET_FOREGROUND));
123
122
124
        int y = getBounds().y + getBounds().height - bottomMargin - gc.getFontMetrics().getHeight();
123
		int y = bottomMargin;
125
124
		
126
        String text = TextUtils.getShortString(gc, column.getText(), width);
125
		if( column.getHeaderControl() == null ) {
127
        
126
			y = getBounds().y + getBounds().height - bottomMargin
128
        if (column.getAlignment() == SWT.RIGHT)
127
			- gc.getFontMetrics().getHeight();			
129
        {
128
		}
130
            int len = gc.stringExtent(text).x;
129
131
            if (len < width)
130
		String text = TextUtils.getShortString(gc, column.getText(), width);
132
            {
131
133
                x += width - len;
132
		if (column.getAlignment() == SWT.RIGHT) {
134
            }
133
			int len = gc.stringExtent(text).x;
135
        }
134
			if (len < width) {
136
        else if (column.getAlignment() == SWT.CENTER)
135
				x += width - len;
137
        {
136
			}
138
            int len = gc.stringExtent(text).x;
137
		} else if (column.getAlignment() == SWT.CENTER) {
139
            if (len < width)
138
			int len = gc.stringExtent(text).x;
140
            {
139
			if (len < width) {
141
                x += (width - len) / 2;
140
				x += (width - len) / 2;
142
            }
141
			}
143
        }
142
		}
144
        
143
145
        
144
		gc.drawString(text, getBounds().x + x + pushedDrawingOffset, y
146
        gc.drawString(text, getBounds().x + x + pushedDrawingOffset,
145
				+ pushedDrawingOffset, true);
147
                      y + pushedDrawingOffset,true);
146
148
147
		if (column.getSort() != SWT.NONE) {
149
        if (column.getSort() != SWT.NONE)
148
			arrowRenderer.setSelected(column.getSort() == SWT.UP);
150
        {
149
			if (drawSelected) {
151
            arrowRenderer.setSelected(column.getSort() == SWT.UP);
150
				arrowRenderer.setLocation(getBounds().x + getBounds().width
152
            if (drawSelected)
151
						- arrowMargin - arrowRenderer.getBounds().width + 1,
153
            {
152
						getBounds().y
154
                arrowRenderer
153
								+ ((getBounds().height - arrowRenderer
155
                    .setLocation(
154
										.getBounds().height) / 2) + 1);
156
                                 getBounds().x + getBounds().width - arrowMargin
155
			} else {
157
                                     - arrowRenderer.getBounds().width + 1,
156
				arrowRenderer.setLocation(getBounds().x + getBounds().width
158
                                 getBounds().y
157
						- arrowMargin - arrowRenderer.getBounds().width,
159
                                     + ((getBounds().height - arrowRenderer.getBounds().height) / 2)
158
						getBounds().y
160
                                     + 1);
159
								+ ((getBounds().height - arrowRenderer
161
            }
160
										.getBounds().height) / 2));
162
            else
161
			}
163
            {
162
			arrowRenderer.paint(gc, null);
164
                arrowRenderer
163
		}
165
                    .setLocation(
164
166
                                 getBounds().x + getBounds().width - arrowMargin
165
		if (!flat) {
167
                                     - arrowRenderer.getBounds().width,
166
168
                                 getBounds().y
167
			if (drawSelected) {
169
                                     + ((getBounds().height - arrowRenderer.getBounds().height) / 2));
168
				gc.setForeground(getDisplay().getSystemColor(
170
            }
169
						SWT.COLOR_WIDGET_NORMAL_SHADOW));
171
            arrowRenderer.paint(gc, null);
170
			} else {
172
        }
171
				gc.setForeground(getDisplay().getSystemColor(
173
172
						SWT.COLOR_WIDGET_HIGHLIGHT_SHADOW));
174
        if (!flat)
173
			}
175
        {
174
176
              
175
			gc.drawLine(getBounds().x, getBounds().y, getBounds().x
177
            if (drawSelected)
176
					+ getBounds().width - 1, getBounds().y);
178
            {
177
			gc.drawLine(getBounds().x, getBounds().y, getBounds().x,
179
                gc.setForeground(getDisplay().getSystemColor(SWT.COLOR_WIDGET_NORMAL_SHADOW));
178
					getBounds().y + getBounds().height - 1);
180
            }
179
181
            else
180
			if (!drawSelected) {
182
            {
181
				gc.setForeground(getDisplay().getSystemColor(
183
                gc.setForeground(getDisplay().getSystemColor(SWT.COLOR_WIDGET_HIGHLIGHT_SHADOW));
182
						SWT.COLOR_WIDGET_LIGHT_SHADOW));
184
            }
183
				gc.drawLine(getBounds().x + 1, getBounds().y + 1, getBounds().x
185
    
184
						+ getBounds().width - 2, getBounds().y + 1);
186
            gc.drawLine(getBounds().x, getBounds().y, getBounds().x + getBounds().width - 1,
185
				gc.drawLine(getBounds().x + 1, getBounds().y + 1,
187
                        getBounds().y);
186
						getBounds().x + 1, getBounds().y + getBounds().height
188
            gc.drawLine(getBounds().x, getBounds().y, getBounds().x, getBounds().y + getBounds().height
187
								- 2);
189
                                                                     - 1);
188
			}
190
    
189
191
            if (!drawSelected)
190
			if (drawSelected) {
192
            {
191
				gc.setForeground(getDisplay().getSystemColor(
193
                gc.setForeground(getDisplay().getSystemColor(SWT.COLOR_WIDGET_LIGHT_SHADOW));
192
						SWT.COLOR_WIDGET_NORMAL_SHADOW));
194
                gc.drawLine(getBounds().x + 1, getBounds().y + 1,
193
			} else {
195
                            getBounds().x + getBounds().width - 2, getBounds().y + 1);
194
				gc.setForeground(getDisplay().getSystemColor(
196
                gc.drawLine(getBounds().x + 1, getBounds().y + 1, getBounds().x + 1,
195
						SWT.COLOR_WIDGET_DARK_SHADOW));
197
                            getBounds().y + getBounds().height - 2);
196
			}
198
            }
197
			gc.drawLine(getBounds().x + getBounds().width - 1, getBounds().y,
199
    
198
					getBounds().x + getBounds().width - 1, getBounds().y
200
            if (drawSelected)
199
							+ getBounds().height - 1);
201
            {
200
			gc.drawLine(getBounds().x, getBounds().y + getBounds().height - 1,
202
                gc.setForeground(getDisplay().getSystemColor(SWT.COLOR_WIDGET_NORMAL_SHADOW));
201
					getBounds().x + getBounds().width - 1, getBounds().y
203
            }
202
							+ getBounds().height - 1);
204
            else
203
205
            {
204
			if (!drawSelected) {
206
                gc.setForeground(getDisplay().getSystemColor(SWT.COLOR_WIDGET_DARK_SHADOW));
205
				gc.setForeground(getDisplay().getSystemColor(
207
            }
206
						SWT.COLOR_WIDGET_NORMAL_SHADOW));
208
            gc.drawLine(getBounds().x + getBounds().width - 1, getBounds().y, getBounds().x
207
				gc.drawLine(getBounds().x + getBounds().width - 2,
209
                                                                              + getBounds().width - 1,
208
						getBounds().y + 1, getBounds().x + getBounds().width
210
                        getBounds().y + getBounds().height - 1);
209
								- 2, getBounds().y + getBounds().height - 2);
211
            gc.drawLine(getBounds().x, getBounds().y + getBounds().height - 1, getBounds().x
210
				gc.drawLine(getBounds().x + 1, getBounds().y
212
                                                                               + getBounds().width - 1,
211
						+ getBounds().height - 2, getBounds().x
213
                        getBounds().y + getBounds().height - 1);
212
						+ getBounds().width - 2, getBounds().y
214
            
213
						+ getBounds().height - 2);
215
            if (!drawSelected)
214
			}
216
            {
215
217
                gc.setForeground(getDisplay().getSystemColor(SWT.COLOR_WIDGET_NORMAL_SHADOW));
216
		} else {
218
                gc.drawLine(getBounds().x + getBounds().width - 2, getBounds().y + 1,
217
			gc.setForeground(getDisplay().getSystemColor(
219
                            getBounds().x + getBounds().width - 2, getBounds().y + getBounds().height
218
					SWT.COLOR_WIDGET_DARK_SHADOW));
220
                                                                   - 2);
219
221
                gc.drawLine(getBounds().x + 1, getBounds().y + getBounds().height - 2,
220
			gc.drawLine(getBounds().x + getBounds().width - 1, getBounds().y,
222
                            getBounds().x + getBounds().width - 2, getBounds().y + getBounds().height
221
					getBounds().x + getBounds().width - 1, getBounds().y
223
                                                                   - 2);
222
							+ getBounds().height - 1);
224
            }
223
			gc.drawLine(getBounds().x, getBounds().y + getBounds().height - 1,
225
            
224
					getBounds().x + getBounds().width - 1, getBounds().y
226
        }
225
							+ getBounds().height - 1);
227
        else
226
		}
228
        {
227
229
            gc.setForeground(getDisplay().getSystemColor(SWT.COLOR_WIDGET_DARK_SHADOW));
228
	}
230
229
231
            gc.drawLine(getBounds().x + getBounds().width - 1, getBounds().y, getBounds().x
230
	/**
232
                                                                              + getBounds().width - 1,
231
	 * {@inheritDoc}
233
                        getBounds().y + getBounds().height - 1);
232
	 */
234
            gc.drawLine(getBounds().x, getBounds().y + getBounds().height - 1, getBounds().x
233
	public void setDisplay(Display display) {
235
                                                                               + getBounds().width - 1,
234
		super.setDisplay(display);
236
                        getBounds().y + getBounds().height - 1);
235
		arrowRenderer.setDisplay(display);
237
        }
236
	}
238
        
237
239
238
	/**
240
    }
239
	 * {@inheritDoc}
241
240
	 */
242
    /**
241
	public boolean notify(int event, Point point, Object value) {
243
     * {@inheritDoc}
242
		return false;
244
     */
243
	}
245
    public void setDisplay(Display display)
244
246
    {
245
	/**
247
        super.setDisplay(display);
246
	 * {@inheritDoc}
248
        arrowRenderer.setDisplay(display);
247
	 */
249
    }
248
	public Rectangle getTextBounds(Object value, boolean preferred) {
250
249
		GridColumn column = (GridColumn) value;
251
    /** 
250
252
     * {@inheritDoc}
251
		int x = leftMargin;
253
     */
252
254
    public boolean notify(int event, Point point, Object value)
253
		if (column.getImage() != null) {
255
    {
254
			x += column.getImage().getBounds().width + imageSpacing;
256
        return false;
255
		}
257
    }
256
258
257
		GC gc = new GC(column.getParent());
259
    /** 
258
		gc.setFont(column.getParent().getFont());
260
     * {@inheritDoc}
259
		int y = bottomMargin;
261
     */
260
		
262
    public Rectangle getTextBounds(Object value, boolean preferred)
261
		if( column.getHeaderControl() != null ) {
263
    {
262
			y = getBounds().height - bottomMargin
264
        GridColumn column = (GridColumn)value;
263
			- gc.getFontMetrics().getHeight();			
265
        
264
		}
266
        int x = leftMargin;
265
267
266
		Rectangle bounds = new Rectangle(x, y, 0, 0);
268
        if (column.getImage() != null)
267
269
        {
268
		Point p = gc.stringExtent(column.getText());
270
            x += column.getImage().getBounds().width + imageSpacing;
269
271
        }
270
		bounds.height = p.y;
272
271
273
272
		if (preferred) {
274
273
			bounds.width = p.x;
275
        GC gc = new GC(column.getParent());
274
		} else {
276
        gc.setFont(column.getParent().getFont());
275
			int width = getBounds().width - x;
277
        int y = getBounds().height - bottomMargin - gc.getFontMetrics().getHeight();
276
			if (column.getSort() == SWT.NONE) {
278
        
277
				width -= rightMargin;
279
        Rectangle bounds = new Rectangle(x,y,0,0);
278
			} else {
280
        
279
				width -= arrowMargin + arrowRenderer.getSize().x + arrowMargin;
281
        Point p = gc.stringExtent(column.getText());
280
			}
282
        
281
			bounds.width = width;
283
        bounds.height = p.y;
282
		}
284
        
283
285
        if (preferred)
284
		gc.dispose();
286
        {
285
287
            bounds.width = p.x;
286
		return bounds;
288
        }
287
	}
289
        else
288
290
        {
289
	/**
291
            int width = getBounds().width - x;
290
	 * @return the bounds reserved for the control
292
            if (column.getSort() == SWT.NONE)
291
	 */
293
            {
292
	protected Rectangle getControlBounds(Object value, boolean preferred) {
294
                width -= rightMargin;
293
		Rectangle bounds = getBounds();
295
            }
294
		GridColumn column = (GridColumn) value;
296
            else
295
		Point controlSize = computeControlSize(column);
297
            {
296
		return new Rectangle(bounds.x+3,bounds.height-bottomMargin-controlSize.y,bounds.width-6,controlSize.y);
298
                width -= arrowMargin + arrowRenderer.getSize().x + arrowMargin;
297
	}
299
            }
298
300
            bounds.width = width;
299
	private Point computeControlSize(GridColumn column) {
301
        }
300
		if( column.getHeaderControl() != null ) {
302
        
301
			return column.getHeaderControl().computeSize(SWT.DEFAULT, SWT.DEFAULT);
303
        
302
		}
304
        gc.dispose();
303
		return new Point(0,0);
305
        
304
	}
306
        return bounds;        
307
    }    
308
}
305
}
(-)src/org/eclipse/nebula/widgets/grid/GridHeaderEditor.java (+227 lines)
Added Link Here
1
package org.eclipse.nebula.widgets.grid;
2
3
import org.eclipse.swt.SWT;
4
import org.eclipse.swt.custom.ControlEditor;
5
import org.eclipse.swt.events.ControlEvent;
6
import org.eclipse.swt.events.ControlListener;
7
import org.eclipse.swt.events.SelectionEvent;
8
import org.eclipse.swt.events.SelectionListener;
9
import org.eclipse.swt.graphics.Rectangle;
10
import org.eclipse.swt.widgets.Event;
11
import org.eclipse.swt.widgets.Listener;
12
13
/**
14
 * Manager for a Control that appears below the grid column header.
15
 * Based on {@link GridEditor}. 
16
 */
17
class GridHeaderEditor extends ControlEditor {
18
19
  private Grid table;
20
21
  private GridColumn column;
22
  
23
  ControlListener columnListener;
24
  
25
  Listener resizeListener;
26
27
  private final Listener columnVisibleListener;
28
29
  private final Listener columnGroupListener;
30
31
  private final SelectionListener scrollListener;
32
  
33
  /**
34
   * Creates a TableEditor for the specified Table.
35
   * 
36
   * @param column the Table Control above which this editor will be displayed
37
   */
38
  GridHeaderEditor(final GridColumn column)
39
  {
40
      super(column.getParent());
41
      
42
      this.table = column.getParent();
43
      this.column = column;
44
      
45
      
46
      columnListener = new ControlListener()
47
      {
48
          public void controlMoved(ControlEvent e)
49
          {
50
              table.getDisplay().asyncExec(new Runnable() {
51
52
				public void run() {
53
					layout();
54
				}
55
            	  
56
              });
57
          }
58
59
          public void controlResized(ControlEvent e)
60
          {
61
              layout();
62
          }
63
      };
64
      
65
      columnVisibleListener = new Listener()
66
      {
67
        public void handleEvent(Event event)
68
          {
69
            getEditor().setVisible(((GridColumn)event.widget).isVisible());
70
            if (getEditor().isVisible()) layout();
71
          }  
72
      };
73
      
74
      resizeListener = new Listener()
75
      {
76
       public void handleEvent(Event event)
77
          {
78
              layout();
79
          }   
80
      };
81
      
82
      scrollListener = new SelectionListener()
83
      {
84
          public void widgetSelected(SelectionEvent e)
85
          {
86
              layout();
87
          }
88
          public void widgetDefaultSelected(SelectionEvent e)
89
          {
90
          }        
91
      };
92
      
93
      columnGroupListener = new Listener()
94
      {
95
          public void handleEvent(Event event)
96
          {
97
              if (getEditor() == null || getEditor().isDisposed()) return;
98
              getEditor().setVisible(column.isVisible());
99
              if (getEditor().isVisible()) layout();
100
          }
101
      };
102
103
      // The following three listeners are workarounds for
104
      // Eclipse bug 105764
105
      // https://bugs.eclipse.org/bugs/show_bug.cgi?id=105764
106
      table.addListener(SWT.Resize, resizeListener);
107
      
108
      if (table.getVerticalScrollBarProxy() != null)
109
      {
110
          table.getVerticalScrollBarProxy().addSelectionListener(scrollListener);
111
      }
112
      if (table.getHorizontalScrollBarProxy() != null)
113
      {
114
          table.getHorizontalScrollBarProxy().addSelectionListener(scrollListener);
115
      }
116
117
      // To be consistent with older versions of SWT, grabVertical defaults to
118
      // true
119
      grabVertical = true;
120
      
121
      initColumn();
122
  }
123
124
  /**
125
   * Returns the bounds of the editor.
126
   * @return bounds of the editor.
127
   */
128
  protected Rectangle internalComputeBounds()
129
  {
130
	return column.getHeaderRenderer().getControlBounds(column,true);
131
  }
132
133
  /**
134
   * Removes all associations between the TableEditor and the cell in the
135
   * table. The Table and the editor Control are <b>not</b> disposed.
136
   */
137
  public void dispose()
138
  {
139
      if (!table.isDisposed() && !column.isDisposed() )
140
      {
141
    	  column.removeControlListener(columnListener);
142
          if (column.getColumnGroup() != null){
143
        	  column.getColumnGroup().removeListener(SWT.Expand, columnGroupListener);
144
        	  column.getColumnGroup().removeListener(SWT.Collapse, columnGroupListener);
145
          }
146
      }
147
      
148
      if (!table.isDisposed())
149
      {
150
          table.removeListener(SWT.Resize, resizeListener);
151
          
152
          if (table.getVerticalScrollBarProxy() != null)
153
              table.getVerticalScrollBarProxy().removeSelectionListener(scrollListener);
154
          
155
          if (table.getHorizontalScrollBarProxy() != null)
156
              table.getHorizontalScrollBarProxy().removeSelectionListener(scrollListener);
157
      }
158
      
159
      columnListener = null;
160
      resizeListener = null;
161
      table = null;
162
      super.dispose();
163
  }
164
165
  /**
166
   * Sets the zero based index of the column of the cell being tracked by this
167
   * editor.
168
   * 
169
   * @param column the zero based index of the column of the cell being
170
   * tracked by this editor
171
   */
172
  void initColumn()
173
  {
174
175
	  column.addControlListener(columnListener);
176
	  column.addListener(SWT.Show, columnVisibleListener);
177
	  column.addListener(SWT.Hide, columnVisibleListener);
178
	  
179
      if (column.getColumnGroup() != null){
180
    	  column.getColumnGroup().addListener(SWT.Expand, columnGroupListener);
181
    	  column.getColumnGroup().addListener(SWT.Collapse, columnGroupListener);
182
      }
183
      layout();
184
  }
185
  
186
  /** 
187
   * {@inheritDoc}
188
   */
189
  public void layout()
190
  {
191
      if (table.isDisposed())
192
          return;
193
194
      
195
      table.getDisplay().asyncExec(new Runnable() {
196
197
		public void run() {
198
		      boolean hadFocus = false;
199
200
		      if (getEditor() == null || getEditor().isDisposed()) {
201
		          return;
202
		      }
203
		      
204
		      if (getEditor().getVisible())
205
		      {
206
		          hadFocus = getEditor().isFocusControl();
207
		      }
208
			Rectangle rect = internalComputeBounds();
209
		      if( rect == null ) {
210
		    	  getEditor().setVisible(false);
211
		    	  return;
212
		      }
213
		      getEditor().setBounds(rect);
214
		      
215
		      if (hadFocus)
216
		      {
217
		          if (getEditor() == null || getEditor().isDisposed())
218
		              return;
219
		          getEditor().setFocus();
220
		      }
221
		}
222
    	  
223
      });
224
225
  }
226
227
}

Return to bug 237846