1 | package de.uka.ipd.sdq.sensorframework.visualisation.dialogs; |
2 | |
3 | import java.util.ArrayList; |
4 | |
5 | import org.eclipse.jface.dialogs.IDialogConstants; |
6 | import org.eclipse.jface.dialogs.TitleAreaDialog; |
7 | import org.eclipse.jface.viewers.ISelectionChangedListener; |
8 | import org.eclipse.jface.viewers.IStructuredSelection; |
9 | import org.eclipse.jface.viewers.SelectionChangedEvent; |
10 | import org.eclipse.jface.viewers.TreeViewer; |
11 | import org.eclipse.swt.SWT; |
12 | import org.eclipse.swt.events.SelectionAdapter; |
13 | import org.eclipse.swt.events.SelectionEvent; |
14 | import org.eclipse.swt.events.ShellAdapter; |
15 | import org.eclipse.swt.events.ShellEvent; |
16 | import org.eclipse.swt.graphics.Point; |
17 | import org.eclipse.swt.layout.FormAttachment; |
18 | import org.eclipse.swt.layout.FormData; |
19 | import org.eclipse.swt.layout.FormLayout; |
20 | import org.eclipse.swt.layout.GridData; |
21 | import org.eclipse.swt.layout.GridLayout; |
22 | import org.eclipse.swt.widgets.Button; |
23 | import org.eclipse.swt.widgets.Composite; |
24 | import org.eclipse.swt.widgets.Control; |
25 | import org.eclipse.swt.widgets.Label; |
26 | import org.eclipse.swt.widgets.Shell; |
27 | import org.eclipse.swt.widgets.Tree; |
28 | |
29 | import de.uka.ipd.sdq.sensorframework.entities.ExperimentRun; |
30 | import de.uka.ipd.sdq.sensorframework.visualisation.views.TreeLabelProvider; |
31 | import de.uka.ipd.sdq.sensorframework.visualisation.views.TreeObject; |
32 | |
33 | /** @author roman */ |
34 | public class ExperimentRunsDialog extends TitleAreaDialog { |
35 | |
36 | private TreeObject selectedObject = null; |
37 | |
38 | private Label selectedField; |
39 | private Button okButton,cancelButton; |
40 | |
41 | public ExperimentRunsDialog(Shell parentShell) { |
42 | super(parentShell); |
43 | this.setShellStyle(SWT.RESIZE|SWT.MAX|SWT.CLOSE); |
44 | } |
45 | |
46 | /* (non-Javadoc) |
47 | * @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets.Shell) |
48 | */ |
49 | @Override |
50 | protected void configureShell(Shell newShell) { |
51 | super.configureShell(newShell); |
52 | newShell.setText("Select Run"); |
53 | newShell.addShellListener(new ShellAdapter(){ |
54 | |
55 | /* (non-Javadoc) |
56 | * @see org.eclipse.swt.events.ShellAdapter#shellClosed(org.eclipse.swt.events.ShellEvent) |
57 | */ |
58 | @Override |
59 | public void shellClosed(ShellEvent e) { |
60 | selectedObject = null; |
61 | } |
62 | }); |
63 | } |
64 | |
65 | /* (non-Javadoc) |
66 | * @see org.eclipse.jface.dialogs.TitleAreaDialog#createDialogArea(org.eclipse.swt.widgets.Composite) |
67 | */ |
68 | @Override |
69 | protected Control createDialogArea(Composite parent) { |
70 | Composite area = (Composite) super.createDialogArea(parent); |
71 | Composite container = new Composite(area, SWT.NONE); |
72 | container.setLayout(new FormLayout()); |
73 | container.setLayoutData(new GridData(GridData.FILL_BOTH)); |
74 | |
75 | final TreeViewer viewer = new TreeViewer(container, SWT.BORDER); |
76 | final Tree tree = viewer.getTree(); |
77 | final FormData fd_tree = new FormData(); |
78 | fd_tree.bottom = new FormAttachment(100, -50); |
79 | fd_tree.right = new FormAttachment(100, -5); |
80 | fd_tree.top = new FormAttachment(0, 5); |
81 | fd_tree.left = new FormAttachment(0, 5); |
82 | tree.setLayoutData(fd_tree); |
83 | |
84 | viewer.setContentProvider(new ExperimentRunsDialogContentProvider()); |
85 | viewer.setLabelProvider(new TreeLabelProvider()); |
86 | viewer.setInput(new ArrayList<Object>()); |
87 | viewer.addSelectionChangedListener(new ISelectionChangedListener() { |
88 | public void selectionChanged(SelectionChangedEvent event) { |
89 | |
90 | IStructuredSelection sel = (IStructuredSelection) event |
91 | .getSelection(); |
92 | Object objeckt = sel.getFirstElement(); |
93 | |
94 | if (objeckt instanceof TreeObject) { |
95 | selectedObject = (TreeObject) objeckt; |
96 | setSelectedField(selectedObject); |
97 | okButton.setEnabled(true); |
98 | } else |
99 | okButton.setEnabled(false); |
100 | } |
101 | }); |
102 | |
103 | Label selectedRunLabel; |
104 | selectedRunLabel = new Label(container, SWT.NONE); |
105 | final FormData fd_selectedRunLabel = new FormData(); |
106 | fd_selectedRunLabel.top = new FormAttachment(tree, 10, SWT.DEFAULT); |
107 | fd_selectedRunLabel.bottom = new FormAttachment(100, -20); |
108 | fd_selectedRunLabel.left = new FormAttachment(0, 10); |
109 | selectedRunLabel.setLayoutData(fd_selectedRunLabel); |
110 | GridLayout labelLayout = new GridLayout(); |
111 | labelLayout.numColumns = 2; |
112 | selectedRunLabel.setText("Selected Run:"); |
113 | |
114 | selectedField = new Label(container, SWT.NONE); |
115 | fd_selectedRunLabel.right = new FormAttachment(selectedField, -5, |
116 | SWT.LEFT); |
117 | final FormData fd_label = new FormData(); |
118 | fd_label.top = new FormAttachment(tree, 10, SWT.DEFAULT); |
119 | fd_label.bottom = new FormAttachment(100, -27); |
120 | fd_label.left = new FormAttachment(0, 95); |
121 | fd_label.right = new FormAttachment(tree, 0, SWT.RIGHT); |
122 | selectedField.setLayoutData(fd_label); |
123 | selectedField.setText("..."); |
124 | |
125 | Label separator = new Label(container, SWT.SEPARATOR | SWT.HORIZONTAL); |
126 | final FormData fd_separator = new FormData(); |
127 | fd_separator.top = new FormAttachment(selectedField, 12, SWT.DEFAULT); |
128 | fd_separator.left = new FormAttachment(0, 5); |
129 | fd_separator.right = new FormAttachment(100, -5); |
130 | fd_separator.bottom = new FormAttachment(100, -5); |
131 | separator.setLayoutData(fd_separator); |
132 | |
133 | setMessage("Select you run..."); |
134 | // |
135 | return area; |
136 | } |
137 | |
138 | /** set selectedField value */ |
139 | private void setSelectedField(TreeObject treeObject) { |
140 | ExperimentRun run = (ExperimentRun) treeObject.getObject(); |
141 | |
142 | if (selectedField != null) |
143 | selectedField.setText(run.getExperimentDateTime()); |
144 | } |
145 | |
146 | /* (non-Javadoc) |
147 | * @see org.eclipse.jface.dialogs.Dialog#createButtonsForButtonBar(org.eclipse.swt.widgets.Composite) |
148 | */ |
149 | @Override |
150 | protected void createButtonsForButtonBar(Composite parent) { |
151 | cancelButton =createButton(parent, IDialogConstants.CANCEL_ID, |
152 | IDialogConstants.CANCEL_LABEL, false); |
153 | cancelButton.addSelectionListener(new SelectionAdapter(){ |
154 | |
155 | /* (non-Javadoc) |
156 | * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent) |
157 | */ |
158 | @Override |
159 | public void widgetSelected(SelectionEvent e) { |
160 | selectedObject = null; |
161 | } |
162 | }); |
163 | okButton = createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, |
164 | true); |
165 | okButton.setEnabled(false); |
166 | } |
167 | |
168 | /* (non-Javadoc) |
169 | * @see org.eclipse.jface.dialogs.TitleAreaDialog#getInitialSize() |
170 | */ |
171 | @Override |
172 | protected Point getInitialSize() { |
173 | return new Point(397, 372); |
174 | } |
175 | |
176 | public TreeObject getResult() { |
177 | return selectedObject; |
178 | } |
179 | } |