Project

General

Profile

Download (10.2 KB) Statistics
| Branch: | Tag: | Revision:
1 4c82bcaa Alexander Oppermann
package eu.etaxonomy.cdm.vaadin.view.dbstatus;
2
3 8ef14383 Alexander Oppermann
import java.sql.SQLException;
4 1746c51d Alexander Oppermann
import java.util.ArrayList;
5
import java.util.Arrays;
6
import java.util.List;
7 1a3e979c Alexander Oppermann
import java.util.UUID;
8 1746c51d Alexander Oppermann
9 092fd728 Patrick Plitzner
import com.vaadin.data.Item;
10 7376bad6 Alexander Oppermann
import com.vaadin.data.Property;
11 fda2cb06 Patrick Plitzner
import com.vaadin.event.ItemClickEvent;
12
import com.vaadin.event.ItemClickEvent.ItemClickListener;
13 7376bad6 Alexander Oppermann
import com.vaadin.event.ShortcutAction.KeyCode;
14
import com.vaadin.event.ShortcutAction.ModifierKey;
15 6689ac7b Alexander Oppermann
import com.vaadin.navigator.View;
16
import com.vaadin.navigator.ViewChangeListener.ViewChangeEvent;
17 7376bad6 Alexander Oppermann
import com.vaadin.server.VaadinSession;
18 4c82bcaa Alexander Oppermann
import com.vaadin.ui.AbsoluteLayout;
19 3f6f86d5 Patrick Plitzner
import com.vaadin.ui.Alignment;
20 7376bad6 Alexander Oppermann
import com.vaadin.ui.Button;
21 6689ac7b Alexander Oppermann
import com.vaadin.ui.Button.ClickEvent;
22
import com.vaadin.ui.Button.ClickListener;
23 4c82bcaa Alexander Oppermann
import com.vaadin.ui.CustomComponent;
24 3f6f86d5 Patrick Plitzner
import com.vaadin.ui.ListSelect;
25 1746c51d Alexander Oppermann
import com.vaadin.ui.Notification;
26 6f0224b6 Patrick Plitzner
import com.vaadin.ui.Notification.Type;
27 4c82bcaa Alexander Oppermann
import com.vaadin.ui.Table;
28 3f6f86d5 Patrick Plitzner
import com.vaadin.ui.UI;
29
import com.vaadin.ui.VerticalLayout;
30 1a3e979c Alexander Oppermann
import com.vaadin.ui.Window;
31 1b9d7954 Alexander Oppermann
32 7376bad6 Alexander Oppermann
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
33 89800b69 Patrick Plitzner
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
34 5c8b52af Patrick Plitzner
import eu.etaxonomy.cdm.model.common.Language;
35
import eu.etaxonomy.cdm.model.common.Representation;
36 1746c51d Alexander Oppermann
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
37 5c8b52af Patrick Plitzner
import eu.etaxonomy.cdm.model.description.PresenceAbsenceTerm;
38 1746c51d Alexander Oppermann
import eu.etaxonomy.cdm.model.taxon.Taxon;
39 1a3e979c Alexander Oppermann
import eu.etaxonomy.cdm.vaadin.component.DetailWindow;
40 1746c51d Alexander Oppermann
import eu.etaxonomy.cdm.vaadin.component.HorizontalToolbar;
41 8ef14383 Alexander Oppermann
import eu.etaxonomy.cdm.vaadin.container.CdmSQLContainer;
42 aab82329 Patrick Plitzner
import eu.etaxonomy.cdm.vaadin.container.PresenceAbsenceTermContainer;
43 4f7ccb87 Patrick Plitzner
import eu.etaxonomy.cdm.vaadin.presenter.dbstatus.DistributionTablePresenter;
44 b2063ede Patrick Plitzner
import eu.etaxonomy.cdm.vaadin.util.CdmQueryFactory;
45 5f031b4f Patrick Plitzner
import eu.etaxonomy.cdm.vaadin.util.DistributionEditorUtil;
46 5c8b52af Patrick Plitzner
import eu.etaxonomy.cdm.vaadin.util.TermCacher;
47 4c82bcaa Alexander Oppermann
48 49a35b7d Patrick Plitzner
public class DistributionTableView extends CustomComponent implements View{
49 b2063ede Patrick Plitzner
50
	private static final long serialVersionUID = 1L;
51 be1478a2 Alexander Oppermann
    private HorizontalToolbar toolbar;
52 1a3e979c Alexander Oppermann
	private Table table;
53
54 4f7ccb87 Patrick Plitzner
	private DistributionTablePresenter listener;
55 1a3e979c Alexander Oppermann
56 f5d57f6d Patrick Plitzner
	private List<String> columnList;
57
	private ArrayList<String> headerList;
58 32bf574c Patrick Plitzner
    private CdmSQLContainer container;
59 1a3e979c Alexander Oppermann
60 4c82bcaa Alexander Oppermann
	/**
61
	 * The constructor should first build the main layout, set the
62
	 * composition root and then do any custom initialization.
63
	 *
64
	 * The constructor will not be automatically regenerated by the
65
	 * visual editor.
66
	 */
67
	public DistributionTableView() {
68 f5d57f6d Patrick Plitzner
		AbsoluteLayout mainLayout = initLayout();
69 4c82bcaa Alexander Oppermann
		setCompositionRoot(mainLayout);
70 7376bad6 Alexander Oppermann
		createEditClickListener();
71 1a3e979c Alexander Oppermann
72 4c82bcaa Alexander Oppermann
	}
73 1a3e979c Alexander Oppermann
74 f5d57f6d Patrick Plitzner
	private AbsoluteLayout initLayout() {
75
		AbsoluteLayout mainLayout = new AbsoluteLayout();
76 4c82bcaa Alexander Oppermann
		mainLayout.setImmediate(false);
77
		mainLayout.setWidth("100%");
78
		mainLayout.setHeight("100%");
79 4f7ccb87 Patrick Plitzner
80 4c82bcaa Alexander Oppermann
		setWidth("100.0%");
81
		setHeight("100.0%");
82 f5d57f6d Patrick Plitzner
83 1746c51d Alexander Oppermann
		//Horizontal Toolbar
84 7376bad6 Alexander Oppermann
		toolbar = new HorizontalToolbar();
85
		mainLayout.addComponent(toolbar, "top:0.0px;right:0.0px;");
86 4f7ccb87 Patrick Plitzner
87 5c8b52af Patrick Plitzner
		// table + formatting
88
		table = new Table(){
89
			private static final long serialVersionUID = -5148756917468804385L;
90
91
			@Override
92
			protected String formatPropertyValue(Object rowId, Object colId, Property<?> property) {
93
				String formattedValue = null;
94
				PresenceAbsenceTerm presenceAbsenceTerm = null;
95
				Object value = property.getValue();
96
				if(value instanceof String){
97
					presenceAbsenceTerm = TermCacher.getInstance().getPresenceAbsenceTerm((String) value);
98
				}
99
				if(presenceAbsenceTerm!=null){
100
					Representation representation = presenceAbsenceTerm.getRepresentation(Language.DEFAULT());
101
					if(representation!=null){
102
						if(DistributionEditorUtil.isAbbreviatedLabels()){
103
							formattedValue = representation.getAbbreviatedLabel();
104
						}
105
						else{
106
							formattedValue = representation.getLabel();
107
						}
108
					}
109
					if(formattedValue==null){
110
						formattedValue = presenceAbsenceTerm.getTitleCache();
111
					}
112
					return formattedValue;
113
				}
114
				return super.formatPropertyValue(rowId, colId, property);
115
			}
116
		};
117 1a3e979c Alexander Oppermann
		table.setImmediate(false);
118
		table.setWidth("100.0%");
119
		table.setHeight("100.0%");
120 32bf574c Patrick Plitzner
121
        table.setColumnReorderingAllowed(true);
122
        table.setSortEnabled(true);
123 5c8b52af Patrick Plitzner
        
124 32bf574c Patrick Plitzner
        table.setColumnCollapsingAllowed(true);
125
        table.setSelectable(true);
126
        table.setPageLength(20);
127
        table.setFooterVisible(true);
128
        table.setCacheRate(20);
129
130
		table.addItemClickListener(new ItemClickListener() {
131
            private static final long serialVersionUID = 2743935539139014771L;
132
            @Override
133
            public void itemClick(ItemClickEvent event) {
134
                if(!(event.getPropertyId().toString().equalsIgnoreCase(CdmQueryFactory.TAXON_COLUMN)) && !(event.getPropertyId().toString().equalsIgnoreCase(CdmQueryFactory.RANK_COLUMN))){
135
                    final Item item = event.getItem();
136
                    Property<?> itemProperty = item.getItemProperty("uuid");
137
                    UUID uuid = UUID.fromString(itemProperty.getValue().toString());
138
                    final Taxon taxon = HibernateProxyHelper.deproxy(listener.getTaxonService().load(uuid), Taxon.class);
139
                    final String areaID = (String) event.getPropertyId();
140
141
                    //popup window
142
                    final Window popup = new Window("Choose distribution status");
143
                    final ListSelect termSelect = new ListSelect();
144
                    termSelect.setSizeFull();
145 5c8b52af Patrick Plitzner
                    termSelect.setNullSelectionAllowed(false);
146 3b0bc0db Patrick Plitzner
                    termSelect.setContainerDataSource(PresenceAbsenceTermContainer.getInstance());
147 32bf574c Patrick Plitzner
                    Button btnOk = new Button("OK", new ClickListener() {
148
                        private static final long serialVersionUID = -3732219609337335697L;
149
                        @Override
150
                        public void buttonClick(ClickEvent event) {
151
                            Object distributionStatus = termSelect.getValue();
152
                            listener.updateDistributionField(areaID, distributionStatus, taxon);
153
                            container.refresh();
154
                            popup.close();
155
                        }
156
                    });
157
                    VerticalLayout layout = new VerticalLayout(termSelect, btnOk);
158
                    layout.setComponentAlignment(btnOk, Alignment.BOTTOM_RIGHT);
159
                    popup.setContent(layout);
160
                    popup.setModal(true);
161
                    popup.center();
162
                    UI.getCurrent().addWindow(popup);
163
                }
164
            }
165
        });
166
167
168 1a3e979c Alexander Oppermann
		mainLayout.addComponent(table, "top:75px;right:0.0px;");
169 4c82bcaa Alexander Oppermann
		return mainLayout;
170
	}
171
172 4f7ccb87 Patrick Plitzner
	public void addListener(DistributionTablePresenter listener) {
173 f5d57f6d Patrick Plitzner
	   this.listener = listener;
174
	}
175
176 6f0224b6 Patrick Plitzner
	@Override
177
	public void enter(ViewChangeEvent event) {
178
		try {
179
			container = listener.getSQLContainer();
180
		} catch (SQLException e) {
181 073d62ba Patrick Plitzner
			DistributionEditorUtil.showSqlError(e);
182 6f0224b6 Patrick Plitzner
			return;
183
		}
184 cb4029b0 Patrick Plitzner
		if(container==null){
185
			return;
186
		}
187 1a3e979c Alexander Oppermann
188
		table.setContainerDataSource(container);
189
190 b2063ede Patrick Plitzner
		columnList = new ArrayList<String>(Arrays.asList(new String[]{CdmQueryFactory.TAXON_COLUMN,CdmQueryFactory.RANK_COLUMN}));
191 fda2cb06 Patrick Plitzner
		List<String> namedAreas = listener.getNamedAreasLabels();
192 785fc5d7 Patrick Plitzner
		columnList.addAll(namedAreas);
193
		table.setVisibleColumns(columnList.toArray());
194 1a3e979c Alexander Oppermann
195 b2063ede Patrick Plitzner
		headerList = new ArrayList<String>(Arrays.asList(new String[]{CdmQueryFactory.TAXON_COLUMN,"Rang"}));
196 fda2cb06 Patrick Plitzner
		headerList.addAll(listener.getNamedAreasLabels());
197 785fc5d7 Patrick Plitzner
		String[] string = new String[headerList.size()];
198
		table.setColumnHeaders(headerList.toArray(string));
199 1746c51d Alexander Oppermann
200 b2063ede Patrick Plitzner
		table.setColumnFooter(CdmQueryFactory.TAXON_COLUMN, "Total amount of Taxa displayed: " + container.size());
201 32bf574c Patrick Plitzner
202
203 1a3e979c Alexander Oppermann
	}
204
205
	private void createEditClickListener(){
206
		Button detailButton = toolbar.getDetailButton();
207
		detailButton.setCaption("Detail View");
208
		detailButton.addClickListener(new ClickListener() {
209 1746c51d Alexander Oppermann
210 aab82329 Patrick Plitzner
			private static final long serialVersionUID = 1479133195403139547L;
211
212 7376bad6 Alexander Oppermann
			@Override
213 1a3e979c Alexander Oppermann
			public void buttonClick(ClickEvent event) {
214 ce1318e1 Patrick Plitzner
				Object selectedItemId = DistributionTableView.this.table.getValue();
215
				if(selectedItemId!=null){
216
					final UUID uuid = UUID.fromString(table.getItem(selectedItemId).getItemProperty("uuid").getValue().toString());
217
					Taxon taxon = HibernateProxyHelper.deproxy(listener.getTaxonService().load(uuid), Taxon.class);
218 89800b69 Patrick Plitzner
					List<DescriptionElementBase> listDescriptions = listener.listDescriptionElementsForTaxon(taxon, null);
219 75f20bed Patrick Plitzner
					DetailWindow detailWindow = new DetailWindow(taxon, listDescriptions);
220
					Window window = detailWindow.createWindow();
221
					window.center();
222 89800b69 Patrick Plitzner
					getUI().addWindow(window);
223 ce1318e1 Patrick Plitzner
				}
224
				else{
225
					Notification.show("Please select a taxon", Type.HUMANIZED_MESSAGE);
226 7376bad6 Alexander Oppermann
				}
227
			}
228 1a3e979c Alexander Oppermann
		});
229 7376bad6 Alexander Oppermann
230 5eece89f Patrick Plitzner
		Button distributionSettingsButton = toolbar.getDistributionSettingsButton();
231
		distributionSettingsButton.addClickListener(new ClickListener() {
232 7376bad6 Alexander Oppermann
233 5eece89f Patrick Plitzner
			private static final long serialVersionUID = -8695281619014251132L;
234 aab82329 Patrick Plitzner
235
			@Override
236 1a3e979c Alexander Oppermann
            public void buttonClick(ClickEvent event) {
237 5eece89f Patrick Plitzner
                openDistributionSettings();
238 1a3e979c Alexander Oppermann
            }
239
        });
240 7376bad6 Alexander Oppermann
241 5eece89f Patrick Plitzner
		Button settingsButton = toolbar.getSettingsButton();
242
		settingsButton.addClickListener(new ClickListener() {
243 32bf574c Patrick Plitzner
244 5eece89f Patrick Plitzner
			private static final long serialVersionUID = -147703680580181544L;
245
246
			@Override
247
			public void buttonClick(ClickEvent event) {
248
				openSettings();
249
			}
250
		});
251
252 7376bad6 Alexander Oppermann
		Button saveButton = toolbar.getSaveButton();
253
		saveButton.setClickShortcut(KeyCode.S, ModifierKey.CTRL);
254
		saveButton.setDescription("Shortcut: CTRL+S");
255
		saveButton.setCaption("Save Data");
256
		saveButton.addClickListener(new ClickListener() {
257
			private static final long serialVersionUID = 1L;
258
			@Override
259
			public void buttonClick(ClickEvent event) {
260 558d5bd1 Patrick Plitzner
				ConversationHolder conversationHolder = (ConversationHolder) VaadinSession.getCurrent().getAttribute(DistributionEditorUtil.SATTR_CONVERSATION);
261 7376bad6 Alexander Oppermann
				try{
262
					conversationHolder.commit();
263
				}catch(Exception stateException){
264
					//TODO create Table without DTO
265
				}
266
				Notification.show("Data saved", Notification.Type.HUMANIZED_MESSAGE);
267
			}
268
		});
269
270 1a3e979c Alexander Oppermann
	}
271 32bf574c Patrick Plitzner
272 cb4029b0 Patrick Plitzner
	public void openSettings() {
273 5eece89f Patrick Plitzner
		SettingsConfigWindow cw = new SettingsConfigWindow(this);
274
		Window window  = cw.createWindow();
275
		getUI().addWindow(window);
276
	}
277
278
	public void openDistributionSettings() {
279
		DistributionSettingsConfigWindow cw = new DistributionSettingsConfigWindow(this);
280 cb4029b0 Patrick Plitzner
        Window window  = cw.createWindow();
281
        getUI().addWindow(window);
282
	}
283 5c8b52af Patrick Plitzner
	
284 4c82bcaa Alexander Oppermann
}