Skip to main content
summaryrefslogtreecommitdiffstats
blob: 6574cec49d05b663729a05754054492a9b69e7aa (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/******************************************************************************* 
 * Copyright (c) 2012 TESIS DYNAware GmbH 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: 
 *     Torsten Sommer <torsten.sommer@tesis.de> - initial API and implementation 
 *******************************************************************************/
package org.eclipse.fx.demo.contacts.views;

import java.util.List;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.ListChangeListener;
import javafx.scene.control.SelectionMode;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.layout.BorderPane;

import javax.inject.Inject;

import org.eclipse.e4.ui.model.application.MApplication;
import org.eclipse.emf.common.notify.AdapterFactory;
import org.eclipse.emf.edit.domain.EditingDomain;
import org.eclipse.fx.demo.contacts.ContactsPackage;
import org.eclipse.fx.demo.contacts.model.ContactsManager;
import org.eclipse.fx.emf.edit.ui.AdapterFactoryObservableList;
import org.eclipse.fx.emf.edit.ui.AdapterFactoryTableCellFactory;
import org.eclipse.fx.emf.edit.ui.EAttributeCellEditHandler;
import org.eclipse.fx.emf.edit.ui.ProxyCellValueFactory;
import org.eclipse.fx.emf.edit.ui.dnd.CellDragAdapter;
import org.eclipse.fx.emf.edit.ui.dnd.EditingDomainCellDropAdapter;


@SuppressWarnings("restriction")
public class ContactsTableView {

	@Inject
	public ContactsTableView(BorderPane parent, final MApplication application, final ContactsManager contactsManager) {
		EditingDomain editingDomain = contactsManager.getEditingDomain();
		AdapterFactory adapterFactory = contactsManager.getAdapterFactory();

		TableView<Object> tableView = new TableView<>();

		parent.setCenter(tableView);

		TableColumn<Object, Object> firstNameColumn = new TableColumn<>("First Name");
		TableColumn<Object, Object> lastNameColumn = new TableColumn<>("Last Name");

		tableView.getColumns().addAll(firstNameColumn, lastNameColumn);

		firstNameColumn.setCellValueFactory(new ProxyCellValueFactory<Object, Object>());
		AdapterFactoryTableCellFactory<Object, Object> firstNameCellFactory = new AdapterFactoryTableCellFactory<>(adapterFactory, 0);
		firstNameCellFactory.addCellCreationListener(new CellDragAdapter());
		firstNameCellFactory.addCellCreationListener(new EditingDomainCellDropAdapter(editingDomain));
		firstNameColumn.setCellFactory(firstNameCellFactory);
		firstNameColumn.setSortable(false);

		lastNameColumn.setCellValueFactory(new ProxyCellValueFactory<Object, Object>());
		AdapterFactoryTableCellFactory<Object, Object> lastNameCellFactory = new AdapterFactoryTableCellFactory<Object, Object>(
				adapterFactory, 1);
		lastNameCellFactory.addCellCreationListener(new CellDragAdapter());
		lastNameCellFactory.addCellCreationListener(new EditingDomainCellDropAdapter(editingDomain));
		lastNameColumn.setCellFactory(lastNameCellFactory);
		lastNameColumn.setSortable(false);

		tableView.setItems(new AdapterFactoryObservableList<Object>(adapterFactory, contactsManager.getRootGroup()));
		tableView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);

		// add edit support
		tableView.setEditable(true);
		firstNameCellFactory.addCellEditHandler(new EAttributeCellEditHandler(ContactsPackage.eINSTANCE.getContact_FirstName(),
				editingDomain));
		lastNameCellFactory
				.addCellEditHandler(new EAttributeCellEditHandler(ContactsPackage.eINSTANCE.getContact_LastName(), editingDomain));

		// add the context menu
		ContextMenuProvider contextMenuProvider = new ContextMenuProvider(contactsManager);
		firstNameCellFactory.addCellUpdateListener(contextMenuProvider);
		lastNameCellFactory.addCellUpdateListener(contextMenuProvider);

		tableView.getSelectionModel().getSelectedItems().addListener(new ListChangeListener<Object>() {

			@Override
			public void onChanged(Change<?> change) {
				application.getContext().set(List.class, change.getList());
			}

		});

		tableView.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<Object>() {

			public void changed(ObservableValue<? extends Object> arg0, Object arg1, Object arg2) {
				application.getContext().set(Object.class, arg2);
			}

		});

	}

}

Back to the top