aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJeanderson Candido2014-05-06 17:18:13 (EDT)
committerLars Vogel2014-05-08 05:24:35 (EDT)
commit9ad2c3d502297ea40ba62f339111f786970e29b8 (patch)
treefe3d9e64547a22732ad3b8ed651605a61c279477
parentbf54cf643d04f45def3546597ee6209fd7dd95a0 (diff)
downloadeclipse.platform.ui-9ad2c3d502297ea40ba62f339111f786970e29b8.zip
eclipse.platform.ui-9ad2c3d502297ea40ba62f339111f786970e29b8.tar.gz
eclipse.platform.ui-9ad2c3d502297ea40ba62f339111f786970e29b8.tar.bz2
Bug 414565 - [JFace][snippets] Update JFace snippets general cleanuprefs/changes/78/26078/4
Refactorings and general cleanup. Change-Id: Ib37e9c3e36dc900c07983aa0ea006354169c79c9 Signed-off-by: Jeanderson Candido <jeandersonbc@gmail.com>
-rw-r--r--examples/org.eclipse.jface.snippets/Eclipse JFace Snippets/org/eclipse/jface/snippets/viewers/Snippet004HideSelection.java8
-rw-r--r--examples/org.eclipse.jface.snippets/Eclipse JFace Snippets/org/eclipse/jface/snippets/viewers/Snippet052DouleClickCellEditor.java110
-rw-r--r--examples/org.eclipse.jface.snippets/Eclipse JFace Snippets/org/eclipse/jface/snippets/viewers/Snippet053StartEditorWithContextMenu.java93
-rw-r--r--examples/org.eclipse.jface.snippets/Eclipse JFace Snippets/org/eclipse/jface/snippets/viewers/Snippet054NativeControlsInViewers.java161
-rw-r--r--examples/org.eclipse.jface.snippets/Eclipse JFace Snippets/org/eclipse/jface/snippets/viewers/Snippet055HideShowColumn.java333
-rw-r--r--examples/org.eclipse.jface.snippets/Eclipse JFace Snippets/org/eclipse/jface/snippets/viewers/Snippet058CellNavigationIn34.java166
-rw-r--r--examples/org.eclipse.jface.snippets/Eclipse JFace Snippets/org/eclipse/jface/snippets/viewers/Snippet061FakedNativeCellEditor.java311
-rw-r--r--examples/org.eclipse.jface.snippets/Eclipse JFace Snippets/org/eclipse/jface/snippets/viewers/Snippet063ComboViewer.java21
-rw-r--r--examples/org.eclipse.jface.snippets/Eclipse JFace Snippets/org/eclipse/jface/snippets/viewers/TextAndDialogCellEditor.java263
9 files changed, 632 insertions, 834 deletions
diff --git a/examples/org.eclipse.jface.snippets/Eclipse JFace Snippets/org/eclipse/jface/snippets/viewers/Snippet004HideSelection.java b/examples/org.eclipse.jface.snippets/Eclipse JFace Snippets/org/eclipse/jface/snippets/viewers/Snippet004HideSelection.java
index 71fd9c1..a3cb2d0 100644
--- a/examples/org.eclipse.jface.snippets/Eclipse JFace Snippets/org/eclipse/jface/snippets/viewers/Snippet004HideSelection.java
+++ b/examples/org.eclipse.jface.snippets/Eclipse JFace Snippets/org/eclipse/jface/snippets/viewers/Snippet004HideSelection.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2006 - 2013 Tom Schindl and others.
+ * Copyright (c) 2006 - 2014 Tom Schindl and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@@ -8,6 +8,7 @@
* Contributors:
* Tom Schindl - initial API and implementation
* Lars Vogel <Lars.Vogel@gmail.com> - Bug 414565
+ * Jeanderson Candido <http://jeandersonbc.github.io> - Bug 414565
*******************************************************************************/
package org.eclipse.jface.snippets.viewers;
@@ -15,6 +16,9 @@ package org.eclipse.jface.snippets.viewers;
import java.util.ArrayList;
import java.util.List;
+import java.util.ArrayList;
+import java.util.List;
+
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.StructuredSelection;
@@ -71,7 +75,6 @@ public class Snippet004HideSelection {
for (int i = 0; i < 10; i++) {
elements.add(new MyModel(i));
}
-
return elements;
}
@@ -86,7 +89,6 @@ public class Snippet004HideSelection {
if (!display.readAndDispatch())
display.sleep();
}
-
display.dispose();
}
diff --git a/examples/org.eclipse.jface.snippets/Eclipse JFace Snippets/org/eclipse/jface/snippets/viewers/Snippet052DouleClickCellEditor.java b/examples/org.eclipse.jface.snippets/Eclipse JFace Snippets/org/eclipse/jface/snippets/viewers/Snippet052DouleClickCellEditor.java
index b40735f..80198e6 100644
--- a/examples/org.eclipse.jface.snippets/Eclipse JFace Snippets/org/eclipse/jface/snippets/viewers/Snippet052DouleClickCellEditor.java
+++ b/examples/org.eclipse.jface.snippets/Eclipse JFace Snippets/org/eclipse/jface/snippets/viewers/Snippet052DouleClickCellEditor.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2006, 2008 Tom Schindl and others.
+ * Copyright (c) 2006, 2014 Tom Schindl and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@@ -7,22 +7,25 @@
*
* Contributors:
* Tom Schindl - initial API and implementation
+ * Jeanderson Candido <http://jeandersonbc.github.io> - Bug 414565
*******************************************************************************/
package org.eclipse.jface.snippets.viewers;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.ColumnLabelProvider;
import org.eclipse.jface.viewers.ColumnViewerEditor;
import org.eclipse.jface.viewers.ColumnViewerEditorActivationEvent;
import org.eclipse.jface.viewers.ColumnViewerEditorActivationStrategy;
import org.eclipse.jface.viewers.ICellModifier;
-import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.jface.viewers.TableViewerEditor;
import org.eclipse.jface.viewers.TextCellEditor;
-import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
@@ -36,41 +39,6 @@ import org.eclipse.swt.widgets.Shell;
*/
public class Snippet052DouleClickCellEditor {
- private class MyContentProvider implements IStructuredContentProvider {
-
- /*
- * (non-Javadoc)
- *
- * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
- */
- @Override
- public Object[] getElements(Object inputElement) {
- return (MyModel[]) inputElement;
- }
-
- /*
- * (non-Javadoc)
- *
- * @see org.eclipse.jface.viewers.IContentProvider#dispose()
- */
- @Override
- public void dispose() {
-
- }
-
- /*
- * (non-Javadoc)
- *
- * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer,
- * java.lang.Object, java.lang.Object)
- */
- @Override
- public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
-
- }
-
- }
-
public static boolean flag = true;
public class MyModel {
@@ -87,13 +55,14 @@ public class Snippet052DouleClickCellEditor {
}
public Snippet052DouleClickCellEditor(Shell shell) {
- final TableViewer v = new TableViewer(shell, SWT.BORDER
+ final TableViewer viewer = new TableViewer(shell, SWT.BORDER
| SWT.FULL_SELECTION);
- v.setContentProvider(new MyContentProvider());
- v.setCellEditors(new CellEditor[] { new TextCellEditor(v.getTable()),
- new TextCellEditor(v.getTable()),
- new TextCellEditor(v.getTable()) });
- v.setCellModifier(new ICellModifier() {
+ viewer.setContentProvider(ArrayContentProvider.getInstance());
+ viewer.setCellEditors(new CellEditor[] {
+ new TextCellEditor(viewer.getTable()),
+ new TextCellEditor(viewer.getTable()),
+ new TextCellEditor(viewer.getTable()) });
+ viewer.setCellModifier(new ICellModifier() {
@Override
public boolean canModify(Object element, String property) {
@@ -112,10 +81,10 @@ public class Snippet052DouleClickCellEditor {
});
- v.setColumnProperties(new String[] { "1", "2", "3" });
+ viewer.setColumnProperties(new String[] { "1", "2", "3" });
ColumnViewerEditorActivationStrategy actSupport = new ColumnViewerEditorActivationStrategy(
- v) {
+ viewer) {
@Override
protected boolean isEditorActivationEvent(
ColumnViewerEditorActivationEvent event) {
@@ -125,43 +94,39 @@ public class Snippet052DouleClickCellEditor {
}
};
- TableViewerEditor.create(v, actSupport,
- ColumnViewerEditor.TABBING_HORIZONTAL
- | ColumnViewerEditor.TABBING_MOVE_TO_ROW_NEIGHBOR
- | ColumnViewerEditor.TABBING_VERTICAL
- | ColumnViewerEditor.KEYBOARD_ACTIVATION);
+ int feature = ColumnViewerEditor.TABBING_HORIZONTAL
+ | ColumnViewerEditor.TABBING_MOVE_TO_ROW_NEIGHBOR
+ | ColumnViewerEditor.TABBING_VERTICAL
+ | ColumnViewerEditor.KEYBOARD_ACTIVATION;
- TableViewerColumn column = new TableViewerColumn(v, SWT.NONE);
- column.getColumn().setWidth(200);
- column.getColumn().setMoveable(true);
- column.getColumn().setText("Column 1");
- column.setLabelProvider(new ColumnLabelProvider());
+ TableViewerEditor.create(viewer, actSupport, feature);
- column = new TableViewerColumn(v, SWT.NONE);
- column.getColumn().setWidth(200);
- column.getColumn().setMoveable(true);
- column.getColumn().setText("Column 2");
- column.setLabelProvider(new ColumnLabelProvider());
+ String[] labels = { "Column 1", "Column 2", "Column 3" };
+ for (String label : labels) {
+ createColumnFor(viewer, label);
+ }
+ viewer.setInput(createModel());
+ viewer.getTable().setLinesVisible(true);
+ viewer.getTable().setHeaderVisible(true);
+ }
- column = new TableViewerColumn(v, SWT.NONE);
+ private TableViewerColumn createColumnFor(final TableViewer viewer,
+ String label) {
+ TableViewerColumn column;
+ column = new TableViewerColumn(viewer, SWT.NONE);
column.getColumn().setWidth(200);
column.getColumn().setMoveable(true);
- column.getColumn().setText("Column 3");
+ column.getColumn().setText(label);
column.setLabelProvider(new ColumnLabelProvider());
-
- MyModel[] model = createModel();
- v.setInput(model);
- v.getTable().setLinesVisible(true);
- v.getTable().setHeaderVisible(true);
+ return column;
}
- private MyModel[] createModel() {
- MyModel[] elements = new MyModel[10];
+ private List<MyModel> createModel() {
+ List<MyModel> elements = new ArrayList<MyModel>();
for (int i = 0; i < 10; i++) {
- elements[i] = new MyModel(i);
+ elements.add(new MyModel(i));
}
-
return elements;
}
@@ -180,7 +145,6 @@ public class Snippet052DouleClickCellEditor {
if (!display.readAndDispatch())
display.sleep();
}
-
display.dispose();
}
diff --git a/examples/org.eclipse.jface.snippets/Eclipse JFace Snippets/org/eclipse/jface/snippets/viewers/Snippet053StartEditorWithContextMenu.java b/examples/org.eclipse.jface.snippets/Eclipse JFace Snippets/org/eclipse/jface/snippets/viewers/Snippet053StartEditorWithContextMenu.java
index 825d663..83c3036 100644
--- a/examples/org.eclipse.jface.snippets/Eclipse JFace Snippets/org/eclipse/jface/snippets/viewers/Snippet053StartEditorWithContextMenu.java
+++ b/examples/org.eclipse.jface.snippets/Eclipse JFace Snippets/org/eclipse/jface/snippets/viewers/Snippet053StartEditorWithContextMenu.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2007, 2008 Marcel and others.
+ * Copyright (c) 2007, 2014 Marcel and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@@ -7,10 +7,12 @@
*
* Contributors:
* Marcel <emmpeegee@gmail.com> - initial API and implementation
+ * Jeanderson Candido <http://jeandersonbc.github.io> - Bug 414565
*******************************************************************************/
package org.eclipse.jface.snippets.viewers;
import java.util.ArrayList;
+import java.util.List;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.ColumnViewerEditor;
@@ -42,63 +44,44 @@ public class Snippet053StartEditorWithContextMenu implements SelectionListener {
private class MyContentProvider implements ITreeContentProvider {
- /* (non-Javadoc)
- * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
- */
@Override
public Object[] getElements(Object inputElement) {
- return ((MyModel)inputElement).child.toArray();
+ return ((MyModel) inputElement).child.toArray();
}
- /* (non-Javadoc)
- * @see org.eclipse.jface.viewers.IContentProvider#dispose()
- */
@Override
public void dispose() {
}
- /* (non-Javadoc)
- * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer, java.lang.Object, java.lang.Object)
- */
@Override
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
}
- /* (non-Javadoc)
- * @see org.eclipse.jface.viewers.ITreeContentProvider#getChildren(java.lang.Object)
- */
@Override
public Object[] getChildren(Object parentElement) {
return getElements(parentElement);
}
- /* (non-Javadoc)
- * @see org.eclipse.jface.viewers.ITreeContentProvider#getParent(java.lang.Object)
- */
@Override
public Object getParent(Object element) {
- if( element == null) {
+ if (element == null) {
return null;
}
-
- return ((MyModel)element).parent;
+ return ((MyModel) element).parent;
}
- /* (non-Javadoc)
- * @see org.eclipse.jface.viewers.ITreeContentProvider#hasChildren(java.lang.Object)
- */
@Override
public boolean hasChildren(Object element) {
- return ((MyModel)element).child.size() > 0;
+ return ((MyModel) element).child.size() > 0;
}
}
public class MyModel {
public MyModel parent;
- public ArrayList child = new ArrayList();
+ public List<MyModel> child = new ArrayList<MyModel>();
public int counter;
public MyModel(int counter, MyModel parent) {
@@ -109,10 +92,9 @@ public class Snippet053StartEditorWithContextMenu implements SelectionListener {
@Override
public String toString() {
String rv = "Item ";
- if( parent != null ) {
+ if (parent != null) {
rv = parent.toString() + ".";
}
-
rv += counter;
return rv;
@@ -122,44 +104,39 @@ public class Snippet053StartEditorWithContextMenu implements SelectionListener {
public Snippet053StartEditorWithContextMenu(Shell shell) {
viewer = new TreeViewer(shell, SWT.BORDER);
viewer.setContentProvider(new MyContentProvider());
- viewer.setCellEditors(new CellEditor[] {new TextCellEditor(viewer.getTree())});
+ viewer.setCellEditors(new CellEditor[] { new TextCellEditor(viewer
+ .getTree()) });
viewer.setColumnProperties(new String[] { "name" });
viewer.setCellModifier(new ICellModifier() {
- /* (non-Javadoc)
- * @see org.eclipse.jface.viewers.ICellModifier#canModify(java.lang.Object, java.lang.String)
- */
@Override
public boolean canModify(Object element, String property) {
return true;
}
- /* (non-Javadoc)
- * @see org.eclipse.jface.viewers.ICellModifier#getValue(java.lang.Object, java.lang.String)
- */
@Override
public Object getValue(Object element, String property) {
- return ((MyModel)element).counter + "";
+ return ((MyModel) element).counter + "";
}
- /* (non-Javadoc)
- * @see org.eclipse.jface.viewers.ICellModifier#modify(java.lang.Object, java.lang.String, java.lang.Object)
- */
@Override
public void modify(Object element, String property, Object value) {
- TreeItem item = (TreeItem)element;
- ((MyModel)item.getData()).counter = Integer.parseInt(value.toString());
+ TreeItem item = (TreeItem) element;
+ ((MyModel) item.getData()).counter = Integer.parseInt(value
+ .toString());
viewer.update(item.getData(), null);
}
});
- TreeViewerEditor.create(viewer, new ColumnViewerEditorActivationStrategy(viewer) {
- @Override
- protected boolean isEditorActivationEvent(ColumnViewerEditorActivationEvent event) {
- return event.eventType == ColumnViewerEditorActivationEvent.PROGRAMMATIC;
- }
- }, ColumnViewerEditor.DEFAULT);
+ TreeViewerEditor.create(viewer,
+ new ColumnViewerEditorActivationStrategy(viewer) {
+ @Override
+ protected boolean isEditorActivationEvent(
+ ColumnViewerEditorActivationEvent event) {
+ return event.eventType == ColumnViewerEditorActivationEvent.PROGRAMMATIC;
+ }
+ }, ColumnViewerEditor.DEFAULT);
Menu menu = new Menu(viewer.getControl());
MenuItem renameItem = new MenuItem(menu, SWT.PUSH);
@@ -176,7 +153,8 @@ public class Snippet053StartEditorWithContextMenu implements SelectionListener {
@Override
public void widgetSelected(SelectionEvent e) {
- IStructuredSelection selection = (IStructuredSelection) viewer.getSelection();
+ IStructuredSelection selection = (IStructuredSelection) viewer
+ .getSelection();
if (selection != null) {
viewer.editElement(selection.getFirstElement(), 0);
}
@@ -184,15 +162,15 @@ public class Snippet053StartEditorWithContextMenu implements SelectionListener {
private MyModel createModel() {
- MyModel root = new MyModel(0,null);
+ MyModel root = new MyModel(0, null);
root.counter = 0;
MyModel tmp;
- for( int i = 1; i < 10; i++ ) {
+ for (int i = 1; i < 10; i++) {
tmp = new MyModel(i, root);
root.child.add(tmp);
- for( int j = 1; j < i; j++ ) {
- tmp.child.add(new MyModel(j,tmp));
+ for (int j = 1; j < i; j++) {
+ tmp.child.add(new MyModel(j, tmp));
}
}
@@ -200,18 +178,17 @@ public class Snippet053StartEditorWithContextMenu implements SelectionListener {
}
public static void main(String[] args) {
- Display display = new Display ();
+ Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
new Snippet053StartEditorWithContextMenu(shell);
- shell.open ();
+ shell.open();
- while (!shell.isDisposed ()) {
- if (!display.readAndDispatch ()) display.sleep ();
+ while (!shell.isDisposed()) {
+ if (!display.readAndDispatch())
+ display.sleep();
}
- display.dispose ();
+ display.dispose();
}
}
-
-
diff --git a/examples/org.eclipse.jface.snippets/Eclipse JFace Snippets/org/eclipse/jface/snippets/viewers/Snippet054NativeControlsInViewers.java b/examples/org.eclipse.jface.snippets/Eclipse JFace Snippets/org/eclipse/jface/snippets/viewers/Snippet054NativeControlsInViewers.java
index 5ab99e7..ec3b1ba 100644
--- a/examples/org.eclipse.jface.snippets/Eclipse JFace Snippets/org/eclipse/jface/snippets/viewers/Snippet054NativeControlsInViewers.java
+++ b/examples/org.eclipse.jface.snippets/Eclipse JFace Snippets/org/eclipse/jface/snippets/viewers/Snippet054NativeControlsInViewers.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2006, 2010 Tom Schindl and others.
+ * Copyright (c) 2006, 2014 Tom Schindl and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@@ -7,16 +7,19 @@
*
* Contributors:
* Tom Schindl - initial API and implementation
+ * Jeanderson Candido <http://jeandersonbc.github.io> - Bug 414565
*******************************************************************************/
package org.eclipse.jface.snippets.viewers;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.CellLabelProvider;
import org.eclipse.jface.viewers.ColumnLabelProvider;
-import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
-import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerCell;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.TableEditor;
@@ -36,48 +39,13 @@ import org.eclipse.swt.widgets.TableItem;
/**
* Example how to place native controls into a viewer with the new JFace-API
* because has the potential to eat up all your handles you should think about
- * alternate approaches e.g. takeing a screenshot of the control
+ * alternate approaches e.g. taking a screenshot of the control
*
* @author Tom Schindl <tom.schindl@bestsolution.at>
*
*/
public class Snippet054NativeControlsInViewers {
- private class MyContentProvider implements IStructuredContentProvider {
-
- /*
- * (non-Javadoc)
- *
- * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(java.lang.Object)
- */
- @Override
- public Object[] getElements(Object inputElement) {
- return (MyModel[]) inputElement;
- }
-
- /*
- * (non-Javadoc)
- *
- * @see org.eclipse.jface.viewers.IContentProvider#dispose()
- */
- @Override
- public void dispose() {
-
- }
-
- /*
- * (non-Javadoc)
- *
- * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse.jface.viewers.Viewer,
- * java.lang.Object, java.lang.Object)
- */
- @Override
- public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
-
- }
-
- }
-
public class MyModel {
public int counter;
@@ -92,14 +60,13 @@ public class Snippet054NativeControlsInViewers {
}
public Snippet054NativeControlsInViewers(Shell shell) {
- final TableViewer v = new TableViewer(shell, SWT.BORDER
+ final TableViewer viewer = new TableViewer(shell, SWT.BORDER
| SWT.FULL_SELECTION);
- v.setContentProvider(new MyContentProvider());
- v.getTable().setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true,2,1));
+ viewer.setContentProvider(ArrayContentProvider.getInstance());
+ viewer.getTable().setLayoutData(
+ new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1));
- TableViewerColumn column = new TableViewerColumn(v, SWT.NONE);
- column.getColumn().setWidth(200);
- column.getColumn().setText("Column 1");
+ TableViewerColumn column = createColumnFor(viewer, "Column 1");
column.setLabelProvider(new ColumnLabelProvider() {
@Override
@@ -109,10 +76,36 @@ public class Snippet054NativeControlsInViewers {
});
- column = new TableViewerColumn(v, SWT.NONE);
- column.getColumn().setWidth(200);
- column.getColumn().setText("Column 2");
- column.setLabelProvider(new CellLabelProvider() {
+ column = createColumnFor(viewer, "Column 2");
+ column.setLabelProvider(createCellLabelProvider());
+
+ viewer.setInput(createModel(10));
+ viewer.getTable().setLinesVisible(true);
+ viewer.getTable().setHeaderVisible(true);
+
+ Button b = createButtonFor(shell, "Modify Input");
+ b.addSelectionListener(new SelectionAdapter() {
+
+ @Override
+ public void widgetSelected(SelectionEvent e) {
+ viewer.setInput(createModel((int) (Math.random() * 10)));
+ }
+
+ });
+
+ b = createButtonFor(shell, "Refresh");
+ b.addSelectionListener(new SelectionAdapter() {
+
+ @Override
+ public void widgetSelected(SelectionEvent e) {
+ viewer.refresh();
+ }
+
+ });
+ }
+
+ private CellLabelProvider createCellLabelProvider() {
+ return new CellLabelProvider() {
@Override
public void update(ViewerCell cell) {
@@ -121,25 +114,24 @@ public class Snippet054NativeControlsInViewers {
@Override
public void widgetDisposed(DisposeEvent e) {
- if( item.getData("EDITOR") != null ) {
- TableEditor editor = (TableEditor) item.getData("EDITOR");
+ if (item.getData("EDITOR") != null) {
+ TableEditor editor = (TableEditor) item
+ .getData("EDITOR");
editor.getEditor().dispose();
editor.dispose();
}
}
};
-
if (item.getData("EDITOR") != null) {
TableEditor editor = (TableEditor) item.getData("EDITOR");
editor.getEditor().dispose();
editor.dispose();
}
-
- if( item.getData("DISPOSELISTNER") != null ) {
- item.removeDisposeListener((DisposeListener) item.getData("DISPOSELISTNER"));
+ if (item.getData("DISPOSELISTNER") != null) {
+ item.removeDisposeListener((DisposeListener) item
+ .getData("DISPOSELISTNER"));
}
-
TableEditor editor = new TableEditor(item.getParent());
item.setData("EDITOR", editor);
Composite comp = new Composite(item.getParent(), SWT.NONE);
@@ -151,56 +143,41 @@ public class Snippet054NativeControlsInViewers {
l.marginTop = 0;
l.marginBottom = 0;
comp.setLayout(l);
- Button rad = new Button(comp, SWT.RADIO);
- Button rad1 = new Button(comp, SWT.RADIO);
- Button rad2 = new Button(comp, SWT.RADIO);
+ new Button(comp, SWT.RADIO);
+ new Button(comp, SWT.RADIO);
+ new Button(comp, SWT.RADIO);
editor.grabHorizontal = true;
editor.setEditor(comp, item, 1);
item.addDisposeListener(listener);
- item.setData("DISPOSELISTNER",listener);
- }
-
- });
-
- MyModel[] model = createModel(10);
- v.setInput(model);
- v.getTable().setLinesVisible(true);
- v.getTable().setHeaderVisible(true);
-
- Button b = new Button(shell,SWT.PUSH);
- b.setText("Modify input");
- b.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
- b.addSelectionListener(new SelectionAdapter() {
-
- @Override
- public void widgetSelected(SelectionEvent e) {
- v.setInput(createModel((int)(Math.random() * 10)));
+ item.setData("DISPOSELISTNER", listener);
}
- });
+ };
+ }
- b = new Button(shell,SWT.PUSH);
- b.setText("Refresh");
+ private Button createButtonFor(Shell shell, String label) {
+ Button b = new Button(shell, SWT.PUSH);
+ b.setText(label);
b.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
- b.addSelectionListener(new SelectionAdapter() {
-
- @Override
- public void widgetSelected(SelectionEvent e) {
- v.refresh();
- }
+ return b;
+ }
- });
+ private TableViewerColumn createColumnFor(final TableViewer viewer,
+ String columnLabel) {
+ TableViewerColumn column = new TableViewerColumn(viewer, SWT.NONE);
+ column.getColumn().setWidth(200);
+ column.getColumn().setText(columnLabel);
+ return column;
}
- private MyModel[] createModel(int amount) {
- MyModel[] elements = new MyModel[amount];
+ private List<MyModel> createModel(int amount) {
+ List<MyModel> elements = new ArrayList<MyModel>();
for (int i = 0; i < amount; i++) {
- elements[i] = new MyModel(i);
+ elements.add(new MyModel(i));
}
-
return elements;
}
@@ -211,7 +188,7 @@ public class Snippet054NativeControlsInViewers {
Display display = new Display();
Shell shell = new Shell(display);
- shell.setLayout(new GridLayout(2,true));
+ shell.setLayout(new GridLayout(2, true));
new Snippet054NativeControlsInViewers(shell);
shell.open();
diff --git a/examples/org.eclipse.jface.snippets/Eclipse JFace Snippets/org/eclipse/jface/snippets/viewers/Snippet055HideShowColumn.java b/examples/org.eclipse.jface.snippets/Eclipse JFace Snippets/org/eclipse/jface/snippets/viewers/Snippet055HideShowColumn.java
index faed3ae..7c46cfd 100644
--- a/examples/org.eclipse.jface.snippets/Eclipse JFace Snippets/org/eclipse/jface/snippets/viewers/Snippet055HideShowColumn.java
+++ b/examples/org.eclipse.jface.snippets/Eclipse JFace Snippets/org/eclipse/jface/snippets/viewers/Snippet055HideShowColumn.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2006, 2013 Tom Schindl and others.
+ * Copyright (c) 2006, 2014 Tom Schindl and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@@ -7,23 +7,24 @@
*
* Contributors:
* Tom Schindl - initial API and implementation
- * Henrik Still<hendrik.still@gammas.de> - bug 415875
+ * Henrik Still <hendrik.still@gammas.de> - Bug 415875
+ * Jeanderson Candido <http://jeandersonbc.github.io> - Bug 414565
*******************************************************************************/
package org.eclipse.jface.snippets.viewers;
import java.util.ArrayList;
+import java.util.List;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.ColumnLabelProvider;
+import org.eclipse.jface.viewers.ColumnViewer;
import org.eclipse.jface.viewers.ColumnViewerEditor;
import org.eclipse.jface.viewers.ColumnViewerEditorActivationEvent;
import org.eclipse.jface.viewers.ColumnViewerEditorActivationStrategy;
import org.eclipse.jface.viewers.EditingSupport;
import org.eclipse.jface.viewers.FocusCellOwnerDrawHighlighter;
-import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ITreeContentProvider;
-import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TextCellEditor;
import org.eclipse.jface.viewers.TreePath;
import org.eclipse.jface.viewers.TreeViewer;
@@ -47,10 +48,95 @@ import org.eclipse.swt.widgets.Shell;
*
*/
public class Snippet055HideShowColumn {
+
+ private final class MyEditingSupport extends EditingSupport {
+ private final TreeViewer v;
+ private final TextCellEditor textCellEditor;
+
+ private MyEditingSupport(ColumnViewer viewer, TreeViewer v,
+ TextCellEditor textCellEditor) {
+ super(viewer);
+ this.v = v;
+ this.textCellEditor = textCellEditor;
+ }
+
+ @Override
+ protected boolean canEdit(Object element) {
+ return true;
+ }
+
+ @Override
+ protected CellEditor getCellEditor(Object element) {
+ return textCellEditor;
+ }
+
+ @Override
+ protected Object getValue(Object element) {
+ return ((MyModel) element).counter + "";
+ }
+
+ @Override
+ protected void setValue(Object element, Object value) {
+ ((MyModel) element).counter = Integer.parseInt(value.toString());
+ v.update(element, null);
+ }
+ }
+
+ class MyContentProvider implements ITreeContentProvider {
+
+ @Override
+ public Object[] getElements(Object inputElement) {
+ return ((MyModel) inputElement).child.toArray();
+ }
+
+ @Override
+ public void dispose() {
+ }
+
+ @Override
+ public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
+ }
+
+ @Override
+ public Object[] getChildren(Object parentElement) {
+ return getElements(parentElement);
+ }
+
+ @Override
+ public Object getParent(Object element) {
+ if (element == null) {
+ return null;
+ }
+ return ((MyModel) element).parent;
+ }
+
+ @Override
+ public boolean hasChildren(Object element) {
+ return ((MyModel) element).child.size() > 0;
+ }
+
+ }
+
+ class MyColumnLabelProvider extends ColumnLabelProvider {
+
+ private String prefix;
+
+ public MyColumnLabelProvider(String prefix) {
+ this.prefix = prefix;
+ }
+
+ @Override
+ public String getText(Object element) {
+ return prefix + " => " + element.toString();
+ }
+
+ }
+
public Snippet055HideShowColumn(final Shell shell) {
final TreeViewer v = new TreeViewer(shell, SWT.BORDER
| SWT.FULL_SELECTION);
- v.getTree().setLayoutData(new GridData(SWT.FILL,SWT.FILL,true,true,2,1));
+ v.getTree().setLayoutData(
+ new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1));
v.getTree().setLinesVisible(true);
v.getTree().setHeaderVisible(true);
@@ -76,151 +162,78 @@ public class Snippet055HideShowColumn {
final TextCellEditor textCellEditor = new TextCellEditor(v.getTree());
- TreeViewerColumn column_1 = new TreeViewerColumn(v, SWT.NONE);
- column_1.getColumn().setWidth(200);
- column_1.getColumn().setMoveable(true);
- column_1.getColumn().setText("Column 1");
- column_1.setLabelProvider(new ColumnLabelProvider() {
-
- @Override
- public String getText(Object element) {
- return "Column 1 => " + element.toString();
- }
-
- });
- column_1.setEditingSupport(new EditingSupport(v) {
- @Override
- protected boolean canEdit(Object element) {
- return true;
- }
-
- @Override
- protected CellEditor getCellEditor(Object element) {
- return textCellEditor;
- }
-
- @Override
- protected Object getValue(Object element) {
- return ((MyModel) element).counter + "";
- }
-
- @Override
- protected void setValue(Object element, Object value) {
- ((MyModel) element).counter = Integer
- .parseInt(value.toString());
- v.update(element, null);
- }
- });
-
- final TreeViewerColumn column_2 = new TreeViewerColumn(v, SWT.NONE);
- column_2.getColumn().setWidth(200);
- column_2.getColumn().setMoveable(true);
- column_2.getColumn().setText("Column 2");
- column_2.setLabelProvider(new ColumnLabelProvider() {
-
- @Override
- public String getText(Object element) {
- return "Column 2 => " + element.toString();
- }
-
- });
- column_2.setEditingSupport(new EditingSupport(v) {
- @Override
- protected boolean canEdit(Object element) {
- return true;
- }
-
- @Override
- protected CellEditor getCellEditor(Object element) {
- return textCellEditor;
- }
-
- @Override
- protected Object getValue(Object element) {
- return ((MyModel) element).counter + "";
- }
-
- @Override
- protected void setValue(Object element, Object value) {
- ((MyModel) element).counter = Integer
- .parseInt(value.toString());
- v.update(element, null);
- }
- });
-
- TreeViewerColumn column_3 = new TreeViewerColumn(v, SWT.NONE);
- column_3.getColumn().setWidth(200);
- column_3.getColumn().setMoveable(true);
- column_3.getColumn().setText("Column 3");
- column_3.setLabelProvider(new ColumnLabelProvider() {
-
- @Override
- public String getText(Object element) {
- return "Column 3 => " + element.toString();
- }
-
- });
- column_3.setEditingSupport(new EditingSupport(v) {
- @Override
- protected boolean canEdit(Object element) {
- return true;
- }
-
- @Override
- protected CellEditor getCellEditor(Object element) {
- return textCellEditor;
- }
-
- @Override
- protected Object getValue(Object element) {
- return ((MyModel) element).counter + "";
- }
-
- @Override
- protected void setValue(Object element, Object value) {
- ((MyModel) element).counter = Integer
- .parseInt(value.toString());
- v.update(element, null);
- }
- });
+ createColumnFor(v, "Column 1", textCellEditor);
+ final TreeViewerColumn column_2 = createColumnFor(v, "Column 2",
+ textCellEditor);
+ createColumnFor(v, "Column 3", textCellEditor);
v.setContentProvider(new MyContentProvider());
-
v.setInput(createModel());
- Button b = new Button(shell, SWT.PUSH);
- b.setText("Edit-Element");
- b.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
+ Button b = createButtonFor(shell, "Edit-Element");
b.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
MyModel root = (MyModel) v.getInput();
TreePath path = new TreePath(new Object[] { root,
- root.child.get(1),
- ((MyModel) root.child.get(1)).child.get(0) });
+ root.child.get(1), root.child.get(1).child.get(0) });
v.editElement(path, 0);
}
});
-
- b = new Button(shell, SWT.PUSH);
- b.setText("Hide/Show 2nd Column");
- b.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
+ b = createButtonFor(shell, "Hide/Show 2nd Column");
b.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
- if(column_2.getColumn().getWidth() == 0){
- column_2.getColumn().setWidth(200);
- }else{
- column_2.getColumn().setWidth(0);
- }
+ int width = column_2.getColumn().getWidth();
+ column_2.getColumn().setWidth(width == 0 ? 200 : 0);
}
});
}
- private MyModel createModel() {
+ private TreeViewerColumn createColumnFor(final TreeViewer v, String label,
+ TextCellEditor textCellEditor) {
+ TreeViewerColumn column = new TreeViewerColumn(v, SWT.NONE);
+ column.getColumn().setWidth(200);
+ column.getColumn().setMoveable(true);
+ column.getColumn().setText(label);
+ column.setLabelProvider(new MyColumnLabelProvider(label));
+ column.setEditingSupport(new MyEditingSupport(v, v, textCellEditor));
+ return column;
+ }
+
+ private Button createButtonFor(final Shell shell, String label) {
+ Button b = new Button(shell, SWT.PUSH);
+ b.setText(label);
+ b.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
+ return b;
+ }
+
+ public class MyModel {
+
+ public MyModel parent;
+ public List<MyModel> child = new ArrayList<MyModel>();
+ public int counter;
+
+ public MyModel(int counter, MyModel parent) {
+ this.parent = parent;
+ this.counter = counter;
+ }
+
+ @Override
+ public String toString() {
+ String rv = "Item ";
+ if (parent != null) {
+ rv = parent.toString() + ".";
+ }
+ rv += counter;
+
+ return rv;
+ }
+ }
+ private MyModel createModel() {
MyModel root = new MyModel(0, null);
root.counter = 0;
@@ -235,14 +248,13 @@ public class Snippet055HideShowColumn {
tmp.child.add(subItem);
}
}
-
return root;
}
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
- shell.setLayout(new GridLayout(2,true));
+ shell.setLayout(new GridLayout(2, true));
new Snippet055HideShowColumn(shell);
shell.open();
@@ -250,68 +262,7 @@ public class Snippet055HideShowColumn {
if (!display.readAndDispatch())
display.sleep();
}
-
display.dispose();
}
- private class MyContentProvider implements ITreeContentProvider {
-
- @Override
- public Object[] getElements(Object inputElement) {
- return ((MyModel) inputElement).child.toArray();
- }
-
- @Override
- public void dispose() {
- }
-
- @Override
- public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
- }
-
- @Override
- public Object[] getChildren(Object parentElement) {
- return getElements(parentElement);
- }
-
- @Override
- public Object getParent(Object element) {
- if (element == null) {
- return null;
- }
- return ((MyModel) element).parent;
- }
-
- @Override
- public boolean hasChildren(Object element) {
- return ((MyModel) element).child.size() > 0;
- }
-
- }
-
- public class MyModel {
- public MyModel parent;
-
- public ArrayList child = new ArrayList();
-
- public int counter;
-
- public MyModel(int counter, MyModel parent) {
- this.parent = parent;
- this.counter = counter;
- }
-
- @Override
- public String toString() {
- String rv = "Item ";
- if (parent != null) {
- rv = parent.toString() + ".";
- }
-
- rv += counter;
-
- return rv;
- }
- }
-
}
diff --git a/examples/org.eclipse.jface.snippets/Eclipse JFace Snippets/org/eclipse/jface/snippets/viewers/Snippet058CellNavigationIn34.java b/examples/org.eclipse.jface.snippets/Eclipse JFace Snippets/org/eclipse/jface/snippets/viewers/Snippet058CellNavigationIn34.java
index 0608ffc..8d1afeb 100644
--- a/examples/org.eclipse.jface.snippets/Eclipse JFace Snippets/org/eclipse/jface/snippets/viewers/Snippet058CellNavigationIn34.java
+++ b/examples/org.eclipse.jface.snippets/Eclipse JFace Snippets/org/eclipse/jface/snippets/viewers/Snippet058CellNavigationIn34.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2006, 2013 Tom Schindl and others.
+ * Copyright (c) 2006, 2014 Tom Schindl and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@@ -9,10 +9,12 @@
* Tom Schindl - initial API and implementation
* Niels Lippke - initial API and implementation
* Lars Vogel (lars.vogel@gmail.com) - Bug 413427
+ * Jeanderson Candido <http://jeandersonbc.github.io> - Bug 414565
*******************************************************************************/
package org.eclipse.jface.snippets.viewers;
+import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.CellNavigationStrategy;
import org.eclipse.jface.viewers.ColumnLabelProvider;
@@ -25,13 +27,11 @@ import org.eclipse.jface.viewers.ColumnViewerEditorDeactivationEvent;
import org.eclipse.jface.viewers.ComboBoxCellEditor;
import org.eclipse.jface.viewers.EditingSupport;
import org.eclipse.jface.viewers.FocusCellOwnerDrawHighlighter;
-import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.jface.viewers.TableViewerEditor;
import org.eclipse.jface.viewers.TableViewerFocusCellManager;
import org.eclipse.jface.viewers.TextCellEditor;
-import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerCell;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
@@ -42,41 +42,25 @@ import org.eclipse.swt.widgets.Shell;
/**
* Example for full feature cell navigation until bug 230955 is fixed
*
- * @author Tom Schindl <tom.schindl@bestsolution.at>, Niels Lippke <niels.lippke@airpas.com>
+ * @author Tom Schindl <tom.schindl@bestsolution.at>, Niels Lippke
+ * <niels.lippke@airpas.com>
*
*/
public class Snippet058CellNavigationIn34 {
- private class MyContentProvider implements IStructuredContentProvider {
-
- @Override
- public Object[] getElements(Object inputElement) {
- return (Person[]) inputElement;
- }
-
- @Override
- public void dispose() {
- }
-
- @Override
- public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
- }
-
- }
-
public class Person {
public String givenname;
public String surname;
public String email;
public String gender;
- public Person(String givenname, String surname, String email, String gender) {
+ public Person(String givenname, String surname, String email,
+ String gender) {
this.givenname = givenname;
this.surname = surname;
this.email = email;
this.gender = gender;
}
-
}
protected abstract class AbstractEditingSupport extends EditingSupport {
@@ -112,13 +96,12 @@ public class Snippet058CellNavigationIn34 {
}
public Snippet058CellNavigationIn34(Shell shell) {
- final TableViewer v = new TableViewer(shell, SWT.BORDER | SWT.FULL_SELECTION);
- v.setContentProvider(new MyContentProvider());
+ final TableViewer v = new TableViewer(shell, SWT.BORDER
+ | SWT.FULL_SELECTION);
+ v.setContentProvider(ArrayContentProvider.getInstance());
- TableViewerColumn column = new TableViewerColumn(v, SWT.NONE);
- column.getColumn().setWidth(200);
- column.getColumn().setText("Givenname");
- column.getColumn().setMoveable(true);
+ TableViewerColumn column = null;
+ column = createColumnFor(v, "Givenname");
column.setLabelProvider(new ColumnLabelProvider() {
@Override
@@ -126,7 +109,6 @@ public class Snippet058CellNavigationIn34 {
return ((Person) element).givenname;
}
});
-
column.setEditingSupport(new AbstractEditingSupport(v) {
@Override
@@ -141,10 +123,7 @@ public class Snippet058CellNavigationIn34 {
});
- column = new TableViewerColumn(v, SWT.NONE);
- column.getColumn().setWidth(200);
- column.getColumn().setText("Surname");
- column.getColumn().setMoveable(true);
+ column = createColumnFor(v, "Surname");
column.setLabelProvider(new ColumnLabelProvider() {
@Override
@@ -153,7 +132,6 @@ public class Snippet058CellNavigationIn34 {
}
});
-
column.setEditingSupport(new AbstractEditingSupport(v) {
@Override
@@ -168,10 +146,7 @@ public class Snippet058CellNavigationIn34 {
});
- column = new TableViewerColumn(v, SWT.NONE);
- column.getColumn().setWidth(200);
- column.getColumn().setText("E-Mail");
- column.getColumn().setMoveable(true);
+ column = createColumnFor(v, "E-Mail");
column.setLabelProvider(new ColumnLabelProvider() {
@Override
@@ -180,8 +155,6 @@ public class Snippet058CellNavigationIn34 {
}
});
-
-
column.setEditingSupport(new AbstractEditingSupport(v) {
@Override
@@ -195,12 +168,7 @@ public class Snippet058CellNavigationIn34 {
}
});
-
-
- column = new TableViewerColumn(v, SWT.NONE);
- column.getColumn().setWidth(200);
- column.getColumn().setText("Gender");
- column.getColumn().setMoveable(true);
+ column = createColumnFor(v, "Gender");
column.setLabelProvider(new ColumnLabelProvider() {
@Override
@@ -210,11 +178,12 @@ public class Snippet058CellNavigationIn34 {
});
- ComboBoxCellEditor editor = new ComboBoxCellEditor(v.getTable(), new String[] {"M","F"});
- editor.setActivationStyle(ComboBoxCellEditor.DROP_DOWN_ON_TRAVERSE_ACTIVATION |
- ComboBoxCellEditor.DROP_DOWN_ON_PROGRAMMATIC_ACTIVATION |
- ComboBoxCellEditor.DROP_DOWN_ON_MOUSE_ACTIVATION |
- ComboBoxCellEditor.DROP_DOWN_ON_KEY_ACTIVATION);
+ ComboBoxCellEditor editor = new ComboBoxCellEditor(v.getTable(),
+ new String[] { "M", "F" });
+ editor.setActivationStyle(ComboBoxCellEditor.DROP_DOWN_ON_TRAVERSE_ACTIVATION
+ | ComboBoxCellEditor.DROP_DOWN_ON_PROGRAMMATIC_ACTIVATION
+ | ComboBoxCellEditor.DROP_DOWN_ON_MOUSE_ACTIVATION
+ | ComboBoxCellEditor.DROP_DOWN_ON_KEY_ACTIVATION);
column.setEditingSupport(new AbstractEditingSupport(v, editor) {
@@ -241,20 +210,23 @@ public class Snippet058CellNavigationIn34 {
@Override
public ViewerCell findSelectedCell(ColumnViewer viewer,
ViewerCell currentSelectedCell, Event event) {
- ViewerCell cell = super.findSelectedCell(viewer, currentSelectedCell, event);
+ ViewerCell cell = super.findSelectedCell(viewer,
+ currentSelectedCell, event);
- if( cell != null ) {
- v.getTable().showColumn(v.getTable().getColumn(cell.getColumnIndex()));
+ if (cell != null) {
+ v.getTable().showColumn(
+ v.getTable().getColumn(cell.getColumnIndex()));
}
-
return cell;
}
};
- TableViewerFocusCellManager focusCellManager = new TableViewerFocusCellManager(v,new FocusCellOwnerDrawHighlighter(v),naviStrat);
+ TableViewerFocusCellManager focusCellManager = new TableViewerFocusCellManager(
+ v, new FocusCellOwnerDrawHighlighter(v), naviStrat);
- ColumnViewerEditorActivationStrategy actSupport = new ColumnViewerEditorActivationStrategy(v) {
+ ColumnViewerEditorActivationStrategy actSupport = new ColumnViewerEditorActivationStrategy(
+ v) {
@Override
protected boolean isEditorActivationEvent(
ColumnViewerEditorActivationEvent event) {
@@ -265,38 +237,40 @@ public class Snippet058CellNavigationIn34 {
}
};
- TableViewerEditor.create(v, focusCellManager, actSupport, ColumnViewerEditor.TABBING_HORIZONTAL
+ int feature = ColumnViewerEditor.TABBING_HORIZONTAL
| ColumnViewerEditor.TABBING_MOVE_TO_ROW_NEIGHBOR
- | ColumnViewerEditor.TABBING_VERTICAL | ColumnViewerEditor.KEYBOARD_ACTIVATION);
+ | ColumnViewerEditor.TABBING_VERTICAL
+ | ColumnViewerEditor.KEYBOARD_ACTIVATION;
- v.getColumnViewerEditor().addEditorActivationListener(new ColumnViewerEditorActivationListener() {
+ TableViewerEditor.create(v, focusCellManager, actSupport, feature);
- @Override
- public void afterEditorActivated(
- ColumnViewerEditorActivationEvent event) {
-
- }
-
- @Override
- public void afterEditorDeactivated(
- ColumnViewerEditorDeactivationEvent event) {
+ v.getColumnViewerEditor().addEditorActivationListener(
+ new ColumnViewerEditorActivationListener() {
- }
+ @Override
+ public void afterEditorActivated(
+ ColumnViewerEditorActivationEvent event) {
+ }
- @Override
- public void beforeEditorActivated(
- ColumnViewerEditorActivationEvent event) {
- ViewerCell cell = (ViewerCell) event.getSource();
- v.getTable().showColumn(v.getTable().getColumn(cell.getColumnIndex()));
- }
+ @Override
+ public void afterEditorDeactivated(
+ ColumnViewerEditorDeactivationEvent event) {
+ }
- @Override
- public void beforeEditorDeactivated(
- ColumnViewerEditorDeactivationEvent event) {
+ @Override
+ public void beforeEditorActivated(
+ ColumnViewerEditorActivationEvent event) {
+ ViewerCell cell = (ViewerCell) event.getSource();
+ v.getTable().showColumn(
+ v.getTable().getColumn(cell.getColumnIndex()));
+ }
- }
+ @Override
+ public void beforeEditorDeactivated(
+ ColumnViewerEditorDeactivationEvent event) {
+ }
- });
+ });
Person[] model = createModel();
v.setInput(model);
@@ -304,16 +278,26 @@ public class Snippet058CellNavigationIn34 {
v.getTable().setHeaderVisible(true);
}
+ private TableViewerColumn createColumnFor(final TableViewer v, String label) {
+ TableViewerColumn column;
+ column = new TableViewerColumn(v, SWT.NONE);
+ column.getColumn().setWidth(200);
+ column.getColumn().setText(label);
+ column.getColumn().setMoveable(true);
+ return column;
+ }
+
private Person[] createModel() {
- Person[] elements = new Person[4];
- elements[0] = new Person("Tom", "Schindl",
- "tom.schindl@bestsolution.at", "M");
- elements[1] = new Person("Boris", "Bokowski",
- "Boris_Bokowski@ca.ibm.com","M");
- elements[2] = new Person("Tod", "Creasey", "Tod_Creasey@ca.ibm.com","M");
- elements[3] = new Person("Wayne", "Beaton", "wayne@eclipse.org","M");
-
- return elements;
+ return new Person[] {
+ new Person("Tom", "Schindl", "tom.schindl@bestsolution.at", "M"),
+ new Person("Boris", "Bokowski", "Boris_Bokowski@ca.ibm.com",
+ "M"),
+ new Person("Tod", "Creasey", "Tod_Creasey@ca.ibm.com", "M"),
+ new Person("Wayne", "Beaton", "wayne@eclipse.org", "M"),
+ new Person("Jeanderson", "Candido", "jeandersonbc@gmail.com",
+ "M"),
+ new Person("Lars", "Vogel", "lars.vogel@gmail.com", "M"),
+ new Person("Hendrik", "Still", "hendrik.still@vogella.com", "M") };
}
/**
diff --git a/examples/org.eclipse.jface.snippets/Eclipse JFace Snippets/org/eclipse/jface/snippets/viewers/Snippet061FakedNativeCellEditor.java b/examples/org.eclipse.jface.snippets/Eclipse JFace Snippets/org/eclipse/jface/snippets/viewers/Snippet061FakedNativeCellEditor.java
index 95fe1df..2968622 100644
--- a/examples/org.eclipse.jface.snippets/Eclipse JFace Snippets/org/eclipse/jface/snippets/viewers/Snippet061FakedNativeCellEditor.java
+++ b/examples/org.eclipse.jface.snippets/Eclipse JFace Snippets/org/eclipse/jface/snippets/viewers/Snippet061FakedNativeCellEditor.java
@@ -1,19 +1,21 @@
/*******************************************************************************
- * Copyright (c) 2006, 2010 Tom Schindl and others.
+ * Copyright (c) 2006, 2014 Tom Schindl and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
- * Tom Schindl<tom.schindl@bestsolution.at> - initial API and implementation
- * Florian Potschka<signalrauschen@gmail.com> - in bug 260061
- * Alexander Ljungberg<siker@norwinter.com> - in bug 260061
+ * Tom Schindl <tom.schindl@bestsolution.at> - initial API and implementation
+ * Florian Potschka <signalrauschen@gmail.com> - Bug 260061
+ * Alexander Ljungberg <siker@norwinter.com> - Bug 260061
+ * Jeanderson Candido <http://jeandersonbc.github.io> - Bug 414565
*******************************************************************************/
package org.eclipse.jface.snippets.viewers;
import java.util.ArrayList;
+import java.util.List;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.jface.util.Util;
@@ -50,9 +52,78 @@ import org.eclipse.swt.widgets.TreeItem;
/**
* A simple TreeViewer to demonstrate usage
- *
+ *
*/
public class Snippet061FakedNativeCellEditor {
+
+ private final class MyEditingSupport extends EditingSupport {
+ private final TextCellEditor textCellEditor;
+ private final TreeViewer viewer;
+
+ private MyEditingSupport(ColumnViewer viewer,
+ TextCellEditor textCellEditor, TreeViewer viewer2) {
+ super(viewer);
+ this.textCellEditor = textCellEditor;
+ this.viewer = viewer2;
+ }
+
+ @Override
+ protected boolean canEdit(Object element) {
+ return true;
+ }
+
+ @Override
+ protected CellEditor getCellEditor(Object element) {
+ return textCellEditor;
+ }
+
+ @Override
+ protected Object getValue(Object element) {
+ return ((File) element).counter + "";
+ }
+
+ @Override
+ protected void setValue(Object element, Object value) {
+ ((File) element).counter = Integer.parseInt(value.toString());
+ viewer.update(element, null);
+ }
+ }
+
+ private class MyContentProvider implements ITreeContentProvider {
+
+ @Override
+ public Object[] getElements(Object inputElement) {
+ return ((File) inputElement).child.toArray();
+ }
+
+ @Override
+ public void dispose() {
+ }
+
+ @Override
+ public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
+ }
+
+ @Override
+ public Object[] getChildren(Object parentElement) {
+ return getElements(parentElement);
+ }
+
+ @Override
+ public Object getParent(Object element) {
+ if (element == null) {
+ return null;
+ }
+ return ((File) element).parent;
+ }
+
+ @Override
+ public boolean hasChildren(Object element) {
+ return ((File) element).child.size() > 0;
+ }
+
+ }
+
public abstract class EmulatedNativeCheckBoxLabelProvider extends
OwnerDrawLabelProvider {
private static final String CHECKED_KEY = "CHECKED";
@@ -73,21 +144,21 @@ public class Snippet061FakedNativeCellEditor {
Color greenScreen = new Color(control.getDisplay(), 222, 223, 224);
Shell shell = new Shell(control.getShell(), SWT.NO_TRIM);
-
+
// otherwise we have a default gray color
shell.setBackground(greenScreen);
- if( Util.isMac() ) {
+ if (Util.isMac()) {
Button button2 = new Button(shell, SWT.CHECK);
Point bsize = button2.computeSize(SWT.DEFAULT, SWT.DEFAULT);
-
+
// otherwise an image is stretched by width
bsize.x = Math.max(bsize.x - 1, bsize.y - 1);
bsize.y = Math.max(bsize.x - 1, bsize.y - 1);
button2.setSize(bsize);
- button2.setLocation(100, 100);
+ button2.setLocation(100, 100);
}
-
+
Button button = new Button(shell, SWT.CHECK);
button.setBackground(greenScreen);
button.setSelection(type);
@@ -95,16 +166,16 @@ public class Snippet061FakedNativeCellEditor {
// otherwise an image is located in a corner
button.setLocation(1, 1);
Point bsize = button.computeSize(SWT.DEFAULT, SWT.DEFAULT);
-
+
// otherwise an image is stretched by width
bsize.x = Math.max(bsize.x - 1, bsize.y - 1);
bsize.y = Math.max(bsize.x - 1, bsize.y - 1);
button.setSize(bsize);
-
+
shell.setSize(bsize);
shell.open();
-
+
GC gc = new GC(shell);
Image image = new Image(control.getDisplay(), bsize.x, bsize.y);
gc.copyArea(image, 0, 0);
@@ -117,7 +188,7 @@ public class Snippet061FakedNativeCellEditor {
Image img = new Image(control.getDisplay(), imageData);
image.dispose();
-
+
return img;
}
@@ -170,13 +241,14 @@ public class Snippet061FakedNativeCellEditor {
}
public Snippet061FakedNativeCellEditor(final Shell shell) {
- final TreeViewer v = new TreeViewer(shell, SWT.BORDER
+ final TreeViewer viewer = new TreeViewer(shell, SWT.BORDER
| SWT.FULL_SELECTION);
- v.getTree().setLinesVisible(true);
- v.getTree().setBackgroundMode(SWT.INHERIT_DEFAULT);
- v.getTree().setHeaderVisible(true);
+ viewer.getTree().setLinesVisible(true);
+ viewer.getTree().setBackgroundMode(SWT.INHERIT_DEFAULT);
+ viewer.getTree().setHeaderVisible(true);
- FocusCellOwnerDrawHighlighter h = new FocusCellOwnerDrawHighlighter(v) {
+ FocusCellOwnerDrawHighlighter h = new FocusCellOwnerDrawHighlighter(
+ viewer) {
@Override
protected Color getSelectedCellBackgroundColorNoFocus(
@@ -194,25 +266,26 @@ public class Snippet061FakedNativeCellEditor {
};
TreeViewerFocusCellManager focusCellManager = new TreeViewerFocusCellManager(
- v, h);
+ viewer, h);
ColumnViewerEditorActivationStrategy actSupport = new ColumnViewerEditorActivationStrategy(
- v);
+ viewer);
+
+ int features = ColumnViewerEditor.TABBING_HORIZONTAL
+ | ColumnViewerEditor.TABBING_MOVE_TO_ROW_NEIGHBOR
+ | ColumnViewerEditor.TABBING_VERTICAL
+ | ColumnViewerEditor.KEYBOARD_ACTIVATION;
- TreeViewerEditor.create(v, focusCellManager, actSupport,
- ColumnViewerEditor.TABBING_HORIZONTAL
- | ColumnViewerEditor.TABBING_MOVE_TO_ROW_NEIGHBOR
- | ColumnViewerEditor.TABBING_VERTICAL
- | ColumnViewerEditor.KEYBOARD_ACTIVATION);
+ TreeViewerEditor.create(viewer, focusCellManager, actSupport, features);
- final TextCellEditor textCellEditor = new TextCellEditor(v.getTree());
- final BooleanCellEditor booleanCellEditor = new BooleanCellEditor(v
- .getTree());
+ final TextCellEditor textCellEditor = new TextCellEditor(
+ viewer.getTree());
+ final BooleanCellEditor booleanCellEditor = new BooleanCellEditor(
+ viewer.getTree());
booleanCellEditor.setChangeOnActivation(true);
- TreeViewerColumn column = new TreeViewerColumn(v, SWT.NONE);
- column.getColumn().setWidth(200);
- column.getColumn().setMoveable(true);
- column.getColumn().setText("File");
+ TreeViewerColumn column = null;
+
+ column = createColumnFor(viewer, SWT.NONE, "File");
column.setLabelProvider(new OwnerDrawLabelProvider() {
@Override
@@ -226,34 +299,11 @@ public class Snippet061FakedNativeCellEditor {
}
});
- column.setEditingSupport(new EditingSupport(v) {
- @Override
- protected boolean canEdit(Object element) {
- return true;
- }
-
- @Override
- protected CellEditor getCellEditor(Object element) {
- return textCellEditor;
- }
-
- @Override
- protected Object getValue(Object element) {
- return ((File) element).counter + "";
- }
-
- @Override
- protected void setValue(Object element, Object value) {
- ((File) element).counter = Integer.parseInt(value.toString());
- v.update(element, null);
- }
- });
+ column.setEditingSupport(new MyEditingSupport(viewer, textCellEditor,
+ viewer));
- column = new TreeViewerColumn(v, SWT.CENTER);
- column.getColumn().setWidth(200);
- column.getColumn().setMoveable(true);
- column.getColumn().setText("Read");
- column.setLabelProvider(new EmulatedNativeCheckBoxLabelProvider(v) {
+ column = createColumnFor(viewer, SWT.CENTER, "Read");
+ column.setLabelProvider(new EmulatedNativeCheckBoxLabelProvider(viewer) {
@Override
protected boolean isChecked(Object element) {
@@ -261,34 +311,10 @@ public class Snippet061FakedNativeCellEditor {
}
});
- column.setEditingSupport(new EditingSupport(v) {
- @Override
- protected boolean canEdit(Object element) {
- return true;
- }
-
- @Override
- protected CellEditor getCellEditor(Object element) {
- return booleanCellEditor;
- }
-
- @Override
- protected Object getValue(Object element) {
- return new Boolean(((File) element).read);
- }
-
- @Override
- protected void setValue(Object element, Object value) {
- ((File) element).read = ((Boolean) value).booleanValue();
- v.update(element, null);
- }
- });
-
- column = new TreeViewerColumn(v, SWT.CENTER);
- column.getColumn().setWidth(200);
- column.getColumn().setMoveable(true);
- column.getColumn().setText("Write");
- column.setLabelProvider(new EmulatedNativeCheckBoxLabelProvider(v) {
+ column.setEditingSupport(new MyEditingSupport(viewer, textCellEditor,
+ viewer));
+ column = createColumnFor(viewer, SWT.CENTER, "Write");
+ column.setLabelProvider(new EmulatedNativeCheckBoxLabelProvider(viewer) {
@Override
protected boolean isChecked(Object element) {
@@ -296,34 +322,10 @@ public class Snippet061FakedNativeCellEditor {
}
});
- column.setEditingSupport(new EditingSupport(v) {
- @Override
- protected boolean canEdit(Object element) {
- return true;
- }
-
- @Override
- protected CellEditor getCellEditor(Object element) {
- return booleanCellEditor;
- }
-
- @Override
- protected Object getValue(Object element) {
- return new Boolean(((File) element).write);
- }
-
- @Override
- protected void setValue(Object element, Object value) {
- ((File) element).write = ((Boolean) value).booleanValue();
- v.update(element, null);
- }
- });
-
- column = new TreeViewerColumn(v, SWT.CENTER);
- column.getColumn().setWidth(200);
- column.getColumn().setMoveable(true);
- column.getColumn().setText("Execute");
- column.setLabelProvider(new EmulatedNativeCheckBoxLabelProvider(v) {
+ column.setEditingSupport(new MyEditingSupport(viewer, textCellEditor,
+ viewer));
+ column = createColumnFor(viewer, SWT.CENTER, "Execute");
+ column.setLabelProvider(new EmulatedNativeCheckBoxLabelProvider(viewer) {
@Override
protected boolean isChecked(Object element) {
@@ -331,36 +333,23 @@ public class Snippet061FakedNativeCellEditor {
}
});
- column.setEditingSupport(new EditingSupport(v) {
- @Override
- protected boolean canEdit(Object element) {
- return true;
- }
-
- @Override
- protected CellEditor getCellEditor(Object element) {
- return booleanCellEditor;
- }
-
- @Override
- protected Object getValue(Object element) {
- return new Boolean(((File) element).execute);
- }
-
- @Override
- protected void setValue(Object element, Object value) {
- ((File) element).execute = ((Boolean) value).booleanValue();
- v.update(element, null);
- }
- });
+ column.setEditingSupport(new MyEditingSupport(viewer, textCellEditor,
+ viewer));
- v.setContentProvider(new MyContentProvider());
+ viewer.setContentProvider(new MyContentProvider());
+ viewer.setInput(createModel());
+ }
- v.setInput(createModel());
+ private TreeViewerColumn createColumnFor(TreeViewer viewer, int style,
+ String label) {
+ TreeViewerColumn column = new TreeViewerColumn(viewer, style);
+ column.getColumn().setWidth(200);
+ column.getColumn().setMoveable(true);
+ column.getColumn().setText(label);
+ return column;
}
private File createModel() {
-
File root = new File(0, null);
root.counter = 0;
@@ -399,46 +388,9 @@ public class Snippet061FakedNativeCellEditor {
}
}
- private class MyContentProvider implements ITreeContentProvider {
-
- @Override
- public Object[] getElements(Object inputElement) {
- return ((File) inputElement).child.toArray();
- }
-
- @Override
- public void dispose() {
- }
-
- @Override
- public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
- }
-
- @Override
- public Object[] getChildren(Object parentElement) {
- return getElements(parentElement);
- }
-
- @Override
- public Object getParent(Object element) {
- if (element == null) {
- return null;
- }
- return ((File) element).parent;
- }
-
- @Override
- public boolean hasChildren(Object element) {
- return ((File) element).child.size() > 0;
- }
-
- }
-
public class File {
public File parent;
-
- public ArrayList child = new ArrayList();
-
+ public List<File> child = new ArrayList<File>();
public int counter;
public boolean read;
@@ -459,7 +411,6 @@ public class Snippet061FakedNativeCellEditor {
if (parent != null) {
rv = parent.toString() + ".";
}
-
rv += counter;
return rv;
diff --git a/examples/org.eclipse.jface.snippets/Eclipse JFace Snippets/org/eclipse/jface/snippets/viewers/Snippet063ComboViewer.java b/examples/org.eclipse.jface.snippets/Eclipse JFace Snippets/org/eclipse/jface/snippets/viewers/Snippet063ComboViewer.java
index df15012..cdcc4a8 100644
--- a/examples/org.eclipse.jface.snippets/Eclipse JFace Snippets/org/eclipse/jface/snippets/viewers/Snippet063ComboViewer.java
+++ b/examples/org.eclipse.jface.snippets/Eclipse JFace Snippets/org/eclipse/jface/snippets/viewers/Snippet063ComboViewer.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2013 Hendrik Still and others.
+ * Copyright (c) 2014 Hendrik Still and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@@ -8,10 +8,14 @@
* Contributors:
* Hendrik Still<hendrik.still@gammas.de> - initial implementation
* Lars Vogel <lars.vogel@gmail.com> - Bug 414565
+ * Jeanderson Candido <http://jeandersonbc.github.io> - Bug 414565
*******************************************************************************/
package org.eclipse.jface.snippets.viewers;
+import java.util.ArrayList;
+import java.util.List;
+
import org.eclipse.jface.layout.GridLayoutFactory;
import org.eclipse.jface.layout.LayoutConstants;
import org.eclipse.jface.viewers.ArrayContentProvider;
@@ -54,23 +58,21 @@ public class Snippet063ComboViewer {
v.setLabelProvider(new LabelProvider());
v.setContentProvider(ArrayContentProvider.getInstance());
- MyModel[] model = createModel();
+ List<MyModel> model = createModel();
v.setInput(model);
// Select the initial Element
- if (model.length > 0) {
- v.setSelection(new StructuredSelection(model[0]));
+ if (model.size() > 0) {
+ v.setSelection(new StructuredSelection(model.get(0)));
}
}
- private MyModel[] createModel() {
- MyModel[] elements = new MyModel[11];
+ private List<MyModel> createModel() {
+ List<MyModel> elements = new ArrayList<MyModel>();
for (int i = 0; i < 10; i++) {
- elements[i] = new MyModel(i);
+ elements.add(new MyModel(i));
}
- elements[10] = new MyModel(42);
-
return elements;
}
@@ -87,7 +89,6 @@ public class Snippet063ComboViewer {
display.sleep();
}
}
-
display.dispose();
}
diff --git a/examples/org.eclipse.jface.snippets/Eclipse JFace Snippets/org/eclipse/jface/snippets/viewers/TextAndDialogCellEditor.java b/examples/org.eclipse.jface.snippets/Eclipse JFace Snippets/org/eclipse/jface/snippets/viewers/TextAndDialogCellEditor.java
index fe53b48..5107226 100644
--- a/examples/org.eclipse.jface.snippets/Eclipse JFace Snippets/org/eclipse/jface/snippets/viewers/TextAndDialogCellEditor.java
+++ b/examples/org.eclipse.jface.snippets/Eclipse JFace Snippets/org/eclipse/jface/snippets/viewers/TextAndDialogCellEditor.java
@@ -1,136 +1,127 @@
-/*******************************************************************************
- * Copyright (c) 2006, 2009 Eric Rizzo and others.
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Eclipse Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/epl-v10.html
- *
- * Contributors:
- * Eric Rizzo - initial implementation
- *******************************************************************************/
-package org.eclipse.jface.snippets.viewers;
-
-import java.text.MessageFormat;
-
-import org.eclipse.jface.dialogs.InputDialog;
-import org.eclipse.jface.viewers.DialogCellEditor;
-import org.eclipse.jface.window.Window;
-import org.eclipse.swt.SWT;
-import org.eclipse.swt.events.FocusAdapter;
-import org.eclipse.swt.events.FocusEvent;
-import org.eclipse.swt.events.KeyAdapter;
-import org.eclipse.swt.events.KeyEvent;
-import org.eclipse.swt.widgets.Composite;
-import org.eclipse.swt.widgets.Control;
-import org.eclipse.swt.widgets.Text;
-
-/**
- * A CellEditor that is a blending of DialogCellEditor and TextCellEditor. The user can either type
- * directly into the Text or use the button to open a Dialog for editing the cell's value.
- *
- */
-public class TextAndDialogCellEditor extends DialogCellEditor {
-
- private Text textField;
- private String dialogMessage;
- private String dialogTitle;
-
-
- public TextAndDialogCellEditor(Composite parent) {
- super(parent);
- }
-
-
- public void setDialogMessage(String dialogMessage) {
- this.dialogMessage = dialogMessage;
- }
-
-
- public void setDialogTitle(String dialogTitle) {
- this.dialogTitle = dialogTitle;
- }
-
-
- @Override
- protected Control createContents(Composite cell) {
- textField = new Text(cell, SWT.NONE);
- textField.setFont(cell.getFont());
- textField.setBackground(cell.getBackground());
- textField.addFocusListener(new FocusAdapter() {
- @Override
- public void focusLost(FocusEvent event) {
- setValueToModel();
- }
- });
-
- textField.addKeyListener(new KeyAdapter() {
- @Override
- public void keyPressed(KeyEvent event) {
- keyReleaseOccured(event);
- }
- });
-
- return textField;
- }
-
- @Override
- protected void keyReleaseOccured(KeyEvent keyEvent) {
- if (keyEvent.keyCode == SWT.CR || keyEvent.keyCode == SWT.KEYPAD_CR) { // Enter key
- setValueToModel();
- }
- super.keyReleaseOccured(keyEvent);
- }
-
- protected void setValueToModel() {
- String newValue = textField.getText();
- boolean newValidState = isCorrect(newValue);
- if (newValidState) {
- markDirty();
- doSetValue(newValue);
- } else {
- // try to insert the current value into the error message.
- setErrorMessage(MessageFormat.format(getErrorMessage(), new Object[] { newValue.toString() }));
- }
- }
-
- @Override
- protected void updateContents(Object value) {
- if (textField == null) {
- return;
- }
-
- String text = "";
- if (value != null) {
- text = value.toString();
- }
- textField.setText(text);
-
- }
-
- @Override
- protected void doSetFocus() {
- // Overridden to set focus to the Text widget instead of the Button.
- textField.setFocus();
- textField.selectAll();
- }
-
-
- @Override
- protected Object openDialogBox(Control cellEditorWindow) {
- InputDialog dialog = new InputDialog(cellEditorWindow.getShell(), dialogTitle, dialogMessage, getDialogInitialValue(), null);
- if (dialog.open() == Window.OK) {
- return dialog.getValue();
- } else {
- return null;
- }
- }
-
- protected String getDialogInitialValue() {
- Object value = getValue();
- if (value == null) {
- return null;
- } else {
- return value.toString();
- }
- }
-}
+/*******************************************************************************
+ * Copyright (c) 2006, 2014 Eric Rizzo and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Eric Rizzo - initial implementation
+ * Jeanderson Candido <http://jeandersonbc.github.io> - Bug 414565
+ *******************************************************************************/
+package org.eclipse.jface.snippets.viewers;
+
+import java.text.MessageFormat;
+
+import org.eclipse.jface.dialogs.InputDialog;
+import org.eclipse.jface.viewers.DialogCellEditor;
+import org.eclipse.jface.window.Window;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.events.FocusAdapter;
+import org.eclipse.swt.events.FocusEvent;
+import org.eclipse.swt.events.KeyAdapter;
+import org.eclipse.swt.events.KeyEvent;
+import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Control;
+import org.eclipse.swt.widgets.Text;
+
+/**
+ * A CellEditor that is a blending of DialogCellEditor and TextCellEditor. The
+ * user can either type directly into the Text or use the button to open a
+ * Dialog for editing the cell's value.
+ *
+ */
+public class TextAndDialogCellEditor extends DialogCellEditor {
+
+ private Text textField;
+ private String dialogMessage;
+ private String dialogTitle;
+
+ public TextAndDialogCellEditor(Composite parent) {
+ super(parent);
+ }
+
+ public void setDialogMessage(String dialogMessage) {
+ this.dialogMessage = dialogMessage;
+ }
+
+ public void setDialogTitle(String dialogTitle) {
+ this.dialogTitle = dialogTitle;
+ }
+
+ @Override
+ protected Control createContents(Composite cell) {
+ textField = new Text(cell, SWT.NONE);
+ textField.setFont(cell.getFont());
+ textField.setBackground(cell.getBackground());
+ textField.addFocusListener(new FocusAdapter() {
+ @Override
+ public void focusLost(FocusEvent event) {
+ setValueToModel();
+ }
+ });
+
+ textField.addKeyListener(new KeyAdapter() {
+ @Override
+ public void keyPressed(KeyEvent event) {
+ keyReleaseOccured(event);
+ }
+ });
+
+ return textField;
+ }
+
+ @Override
+ protected void keyReleaseOccured(KeyEvent keyEvent) {
+ if (keyEvent.keyCode == SWT.CR || keyEvent.keyCode == SWT.KEYPAD_CR) {
+ // Enter key
+ setValueToModel();
+ }
+ super.keyReleaseOccured(keyEvent);
+ }
+
+ protected void setValueToModel() {
+ String newValue = textField.getText();
+ boolean newValidState = isCorrect(newValue);
+ if (newValidState) {
+ markDirty();
+ doSetValue(newValue);
+ } else {
+ // try to insert the current value into the error message.
+ setErrorMessage(MessageFormat.format(getErrorMessage(),
+ new Object[] { newValue.toString() }));
+ }
+ }
+
+ @Override
+ protected void updateContents(Object value) {
+ if (textField == null) {
+ return;
+ }
+ String text = "";
+ if (value != null) {
+ text = value.toString();
+ }
+ textField.setText(text);
+
+ }
+
+ @Override
+ protected void doSetFocus() {
+ // Overridden to set focus to the Text widget instead of the Button.
+ textField.setFocus();
+ textField.selectAll();
+ }
+
+ @Override
+ protected Object openDialogBox(Control cellEditorWindow) {
+ InputDialog dialog = new InputDialog(cellEditorWindow.getShell(),
+ dialogTitle, dialogMessage, getDialogInitialValue(), null);
+ return (dialog.open() == Window.OK ? dialog.getValue() : null);
+ }
+
+ protected String getDialogInitialValue() {
+ Object value = getValue();
+ return (value == null ? null : value.toString());
+ }
+}