ref #7006 Avoid multiple warnings when opening prefs with no session
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / preference / DefaultFeatureTreePreferenecs.java
1 /**
2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
8 */
9
10 package eu.etaxonomy.taxeditor.preference;
11
12 import java.util.List;
13
14 import javax.annotation.PostConstruct;
15
16 import org.eclipse.jface.viewers.ISelectionChangedListener;
17 import org.eclipse.jface.viewers.IStructuredSelection;
18 import org.eclipse.jface.viewers.ListViewer;
19 import org.eclipse.jface.viewers.SelectionChangedEvent;
20 import org.eclipse.jface.viewers.StructuredSelection;
21 import org.eclipse.swt.SWT;
22 import org.eclipse.swt.custom.CLabel;
23 import org.eclipse.swt.layout.GridData;
24 import org.eclipse.swt.layout.GridLayout;
25 import org.eclipse.swt.widgets.Composite;
26 import org.eclipse.swt.widgets.Control;
27
28 import eu.etaxonomy.cdm.api.service.IFeatureTreeService;
29 import eu.etaxonomy.cdm.model.description.FeatureTree;
30 import eu.etaxonomy.taxeditor.featuretree.FeatureTreeContentProvider;
31 import eu.etaxonomy.taxeditor.featuretree.FeatureTreeLabelProvider;
32 import eu.etaxonomy.taxeditor.preference.menu.CdmPreferencePage;
33 import eu.etaxonomy.taxeditor.store.CdmStore;
34
35 /**
36 * <p>DefaultFeatureTreePreferenecs class.</p>
37 *
38 * @author n.hoffmann
39 * @created Sep 16, 2010
40 * @version 1.0
41 */
42 public class DefaultFeatureTreePreferenecs extends CdmPreferencePage {
43
44 private FeatureTree defaultFeatureTreeForTextualDescription;
45 private FeatureTree defaultFeatureTreeForStructuredDescription;
46
47 /* (non-Javadoc)
48 * @see org.eclipse.jface.preference.PreferencePage#createContents(org.eclipse.swt.widgets.Composite)
49 */
50 /** {@inheritDoc} */
51 @Override
52 @PostConstruct
53 protected Control createContents(Composite parent) {
54 defaultFeatureTreeForTextualDescription = PreferencesUtil.getDefaultFeatureTreeForTextualDescription();
55 defaultFeatureTreeForStructuredDescription = PreferencesUtil.getDefaultFeatureTreeForStructuredDescription();
56
57 Composite composite = new Composite(parent, SWT.NULL);
58 composite.setLayout(new GridLayout());
59 if(CdmStore.isActive()) {
60 createTextTreeSelection(composite);
61 createStructureTreeSelection(composite);
62 }
63 init();
64 return composite;
65 }
66
67 private void createTextTreeSelection(Composite parent){
68 final CLabel label = new CLabel(parent, SWT.NONE);
69 label.setText("Deafult Feature Tree to be used for textual descriptions");
70
71 final ListViewer viewer = new ListViewer(parent);
72 viewer.getControl().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
73
74 viewer.setContentProvider(new FeatureTreeContentProvider());
75 viewer.setLabelProvider(new FeatureTreeLabelProvider());
76
77 viewer.addSelectionChangedListener(new ISelectionChangedListener() {
78
79 @Override
80 public void selectionChanged(SelectionChangedEvent arg0) {
81 IStructuredSelection selection = (IStructuredSelection) viewer.getSelection();
82
83 defaultFeatureTreeForTextualDescription = (FeatureTree) selection.getFirstElement();
84 }
85 });
86
87 List<FeatureTree> input = CdmStore.getService(IFeatureTreeService.class).list(FeatureTree.class, null, null, null, null);
88
89 viewer.setInput(input);
90
91 if(defaultFeatureTreeForTextualDescription != null){
92 IStructuredSelection selection = new StructuredSelection(defaultFeatureTreeForTextualDescription);
93 viewer.setSelection(selection, true);
94 }
95 }
96
97 private void createStructureTreeSelection(Composite parent){
98 final CLabel label = new CLabel(parent, SWT.NONE);
99 label.setText("Default Feature Tree to be used for structured descriptions");
100
101 final ListViewer viewer = new ListViewer(parent);
102 viewer.getControl().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
103
104 viewer.setContentProvider(new FeatureTreeContentProvider());
105 viewer.setLabelProvider(new FeatureTreeLabelProvider());
106
107 viewer.addSelectionChangedListener(new ISelectionChangedListener() {
108
109 @Override
110 public void selectionChanged(SelectionChangedEvent arg0) {
111 IStructuredSelection selection = (IStructuredSelection) viewer.getSelection();
112
113 defaultFeatureTreeForStructuredDescription = (FeatureTree) selection.getFirstElement();
114 }
115 });
116
117 List<FeatureTree> input = CdmStore.getService(IFeatureTreeService.class).list(FeatureTree.class, null, null, null, null);
118
119 viewer.setInput(input);
120
121 if(defaultFeatureTreeForStructuredDescription != null){
122 IStructuredSelection selection = new StructuredSelection(defaultFeatureTreeForStructuredDescription);
123 viewer.setSelection(selection, true);
124 }
125 }
126
127 /** {@inheritDoc} */
128 public void init() {
129 setPreferenceStore(PreferencesUtil.getPreferenceStore());
130 }
131
132 /** {@inheritDoc} */
133 @Override
134 public boolean performOk() {
135 String defaultFeatureTreeForTextualDescriptionUuid = "";
136 String defaultFeatureTreeForStructuredDescriptionUuid = "";
137
138 if(defaultFeatureTreeForTextualDescription != null){
139 defaultFeatureTreeForTextualDescriptionUuid = defaultFeatureTreeForTextualDescription.getUuid().toString();
140 }
141 if(defaultFeatureTreeForStructuredDescription != null){
142 defaultFeatureTreeForStructuredDescriptionUuid = defaultFeatureTreeForStructuredDescription.getUuid().toString();
143 }
144
145 getPreferenceStore().setValue(IPreferenceKeys.FEATURE_TREE_DEFAULT_TEXT, defaultFeatureTreeForTextualDescriptionUuid);
146 getPreferenceStore().setValue(IPreferenceKeys.FEATURE_TREE_DEFAULT_STRUCTURE, defaultFeatureTreeForStructuredDescriptionUuid);
147
148 return super.performOk();
149 }
150 }