View Javadoc

1   /*
2    *  File: SwtHierarchyControlPanel.java 
3    *  Copyright (c) 2004-2007  Peter Kliem (Peter.Kliem@jaret.de)
4    *  A commercial license is available, see http://www.jaret.de.
5    *
6    *  This program is free software; you can redistribute it and/or modify
7    *  it under the terms of the GNU General Public License as published by
8    *  the Free Software Foundation; either version 2 of the License, or
9    *  (at your option) any later version.
10   *
11   *  This program is distributed in the hope that it will be useful,
12   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   *  GNU General Public License for more details.
15   *
16   *  You should have received a copy of the GNU General Public License
17   *  along with this program; if not, write to the Free Software
18   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19   */
20  package de.jaret.examples.timebars.hierarchy.swt;
21  
22  import org.eclipse.swt.SWT;
23  import org.eclipse.swt.custom.CCombo;
24  import org.eclipse.swt.events.SelectionAdapter;
25  import org.eclipse.swt.events.SelectionEvent;
26  import org.eclipse.swt.layout.RowLayout;
27  import org.eclipse.swt.widgets.Button;
28  import org.eclipse.swt.widgets.Composite;
29  import org.eclipse.swt.widgets.Label;
30  import org.eclipse.swt.widgets.Scale;
31  
32  import de.jaret.util.ui.timebars.model.IIntervalRelation;
33  import de.jaret.util.ui.timebars.model.IRelationalInterval;
34  import de.jaret.util.ui.timebars.model.IntervalRelation;
35  import de.jaret.util.ui.timebars.model.TimeBarSelectionListener;
36  import de.jaret.util.ui.timebars.model.TimeBarSelectionModel;
37  import de.jaret.util.ui.timebars.swt.TimeBarViewer;
38  import de.jaret.util.ui.timebars.swt.renderer.DefaultHierarchyRenderer;
39  
40  /***
41   * Hierarhy settings for the hierachy example.
42   * 
43   * @author Peter Kliem
44   * @version $Id: SwtHierarchyControlPanel.java 801 2008-12-27 22:44:54Z kliem $
45   */
46  public class SwtHierarchyControlPanel extends Composite {
47  
48      private TimeBarViewer _tbv;
49  
50      public SwtHierarchyControlPanel(Composite parent, int style, TimeBarViewer tbv) {
51          super(parent, style);
52          _tbv = tbv;
53          createControls(this);
54      }
55  
56      /***
57       * @param panel
58       */
59      private void createControls(SwtHierarchyControlPanel panel) {
60          panel.setLayout(new RowLayout());
61  
62          // check whether this has been initialized nd return otherwise
63          if (_tbv == null) {
64              return; 
65          }
66  
67          final DefaultHierarchyRenderer renderer = (DefaultHierarchyRenderer) _tbv.getHierarchyRenderer();
68  
69          Label l = new Label(this, SWT.NULL);
70          l.setText("Levelwidth:");
71  
72          final Scale levelWidthScale = new Scale(this, SWT.HORIZONTAL);
73          levelWidthScale.setMaximum(300);
74          levelWidthScale.setMinimum(5);
75          levelWidthScale.setSelection(renderer.getLevelWidth());
76          levelWidthScale.addSelectionListener(new SelectionAdapter() {
77              public void widgetSelected(SelectionEvent ev) {
78                  int val = levelWidthScale.getSelection();
79                  renderer.setLevelWidth(val);
80                  _tbv.redraw();
81              }
82          });
83  
84          final Button fixedLevelCheck = new Button(this, SWT.CHECK);
85          fixedLevelCheck.setText("fixed level width");
86          fixedLevelCheck.setSelection(renderer.getFixedLevelWidth());
87          fixedLevelCheck.addSelectionListener(new SelectionAdapter() {
88              public void widgetSelected(SelectionEvent arg0) {
89                  renderer.setFixedLevelWidth(fixedLevelCheck.getSelection());
90                  _tbv.redraw();
91              }
92          });
93  
94          final Button drawIconsCheck = new Button(this, SWT.CHECK);
95          drawIconsCheck.setText("Draw icons");
96          drawIconsCheck.setSelection(renderer.getDrawIcons());
97          drawIconsCheck.addSelectionListener(new SelectionAdapter() {
98              public void widgetSelected(SelectionEvent arg0) {
99                  renderer.setDrawIcons(drawIconsCheck.getSelection());
100                 _tbv.redraw();
101             }
102         });
103 
104         final Button drawLabelsCheck = new Button(this, SWT.CHECK);
105         drawLabelsCheck.setText("Draw labels");
106         drawLabelsCheck.setSelection(renderer.getDrawLabels());
107         drawLabelsCheck.addSelectionListener(new SelectionAdapter() {
108             public void widgetSelected(SelectionEvent arg0) {
109                 renderer.setDrawLabels(drawLabelsCheck.getSelection());
110                 _tbv.redraw();
111             }
112         });
113 
114         final Button symbolCheck = new Button(this, SWT.CHECK);
115         symbolCheck.setText("Default symbols");
116         symbolCheck.setSelection(false);
117         symbolCheck.addSelectionListener(new SelectionAdapter() {
118             public void widgetSelected(SelectionEvent arg0) {
119                 if (symbolCheck.getSelection()) {
120                     renderer.setRscNames(null, null, null);
121                     renderer.setSize(12);
122                 } else {
123                     renderer.setRscNames("/de/jaret/examples/timebars/hierarchy/swt/collapsed.png",
124                             "/de/jaret/examples/timebars/hierarchy/swt/expanded.png",
125                             "/de/jaret/examples/timebars/hierarchy/swt/leaf.png");
126                     renderer.setSize(16);
127                 }
128                 _tbv.redraw();
129             }
130         });
131 
132 
133         final Button drawTreeCheck = new Button(this, SWT.CHECK);
134         drawTreeCheck.setText("Draw tree");
135         drawTreeCheck.setSelection(renderer.getDrawTreeLines());
136         drawTreeCheck.addSelectionListener(new SelectionAdapter() {
137             public void widgetSelected(SelectionEvent arg0) {
138                 renderer.setDrawTreeLines(drawTreeCheck.getSelection());
139                 _tbv.redraw();
140             }
141         });
142 
143         final Button hideRootCheck = new Button(this, SWT.CHECK);
144         hideRootCheck.setText("hide root");
145         hideRootCheck.setSelection(_tbv.getHideRoot());
146         hideRootCheck.addSelectionListener(new SelectionAdapter() {
147             public void widgetSelected(SelectionEvent arg0) {
148                 _tbv.setHideRoot(hideRootCheck.getSelection());
149             //    _tbv.redraw();
150             }
151         });
152         
153         l = new Label(this, SWT.NULL);
154         l.setText("Hierarchy width:");
155 
156         final Scale hierarchyWidthScale = new Scale(this, SWT.HORIZONTAL);
157         hierarchyWidthScale.setMaximum(400);
158         hierarchyWidthScale.setMinimum(0);
159         hierarchyWidthScale.setSelection(_tbv.getHierarchyWidth());
160         hierarchyWidthScale.addSelectionListener(new SelectionAdapter() {
161             public void widgetSelected(SelectionEvent ev) {
162                 int val = hierarchyWidthScale.getSelection();
163                 _tbv.setHierarchyWidth(val);
164                 _tbv.redraw();
165             }
166         });
167         
168         
169         // button adding relation 
170         // enabled whenever 2 intervals are selected
171         final Button addRelation = new Button(this, SWT.PUSH);
172         addRelation.setText("add Relation");
173         addRelation.setEnabled(_tbv.getSelectionModel().getSelectedIntervals().size() == 2);
174         addRelation.addSelectionListener(new SelectionAdapter() {
175             public void widgetSelected(SelectionEvent arg0) {
176                 // exactly two intervals are selected
177                 // these are IRelationalIntervals
178                 IRelationalInterval i1 = (IRelationalInterval) _tbv.getSelectionModel().getSelectedIntervals().get(0);
179                 IRelationalInterval i2 = (IRelationalInterval) _tbv.getSelectionModel().getSelectedIntervals().get(1);
180                 IIntervalRelation relation = new IntervalRelation(i1,i2);
181                 i1.addRelation(relation);
182                 i2.addRelation(relation);
183             }
184         });
185 
186         _tbv.getSelectionModel().addTimeBarSelectionListener(new TimeBarSelectionListener() {
187 
188             public void elementAddedToSelection(TimeBarSelectionModel selectionModel, Object element) {
189                 addRelation.setEnabled(selectionModel.getSelectedIntervals().size() == 2 &&
190                         selectionModel.getSelectedIntervals().get(0) instanceof IRelationalInterval &&
191                         selectionModel.getSelectedIntervals().get(1) instanceof IRelationalInterval);
192             }
193 
194             public void elementRemovedFromSelection(TimeBarSelectionModel selectionModel, Object element) {
195                 addRelation.setEnabled(selectionModel.getSelectedIntervals().size() == 2 &&
196                         selectionModel.getSelectedIntervals().get(0) instanceof IRelationalInterval &&
197                         selectionModel.getSelectedIntervals().get(1) instanceof IRelationalInterval);
198             }
199 
200             public void selectionChanged(TimeBarSelectionModel selectionModel) {
201                 addRelation.setEnabled(selectionModel.getSelectedIntervals().size() == 2 &&
202                         selectionModel.getSelectedIntervals().get(0) instanceof IRelationalInterval &&
203                         selectionModel.getSelectedIntervals().get(1) instanceof IRelationalInterval);
204             }
205         });
206         
207         
208         // button deleting relations 
209         // enabled whenever relations are selected
210         final Button delRelation = new Button(this, SWT.PUSH);
211         delRelation.setText("del Relation");
212         delRelation.setEnabled(_tbv.getSelectionModel().getSelectedIntervals().size() == 2);
213         delRelation.addSelectionListener(new SelectionAdapter() {
214             public void widgetSelected(SelectionEvent arg0) {
215                 for (IIntervalRelation relation : _tbv.getSelectionModel().getSelectedRelations()) {
216                     relation.getStartInterval().removeRelation(relation);
217                     relation.getEndInterval().removeRelation(relation);
218                 }
219                 _tbv.redraw();
220             }
221         });
222 
223      
224 
225         l = new Label(this, SWT.NULL);
226         l.setText("Relation type");
227 
228         
229         // combo changing the type of all selected relations
230         final CCombo relationTypeCombo = new CCombo(this, SWT.READ_ONLY | SWT.BORDER);
231         relationTypeCombo.setItems(new String[] {"BEGIN_END", "BEGIN_BEGIN", "END_BEGIN", "END_END"});
232         relationTypeCombo.select(0);
233         relationTypeCombo.addSelectionListener(new SelectionAdapter() {
234             public void widgetSelected(SelectionEvent arg0) {
235                 String selected = relationTypeCombo.getText();
236                 IIntervalRelation.Type type = IIntervalRelation.Type.valueOf(selected);
237                 for (IIntervalRelation relation : _tbv.getSelectionModel().getSelectedRelations()) {
238                     relation.setType(type);
239                 }
240                 _tbv.redraw();
241             }
242         });
243         
244         l = new Label(this, SWT.NULL);
245         l.setText("Relation direction");
246         // combo changing the direction of all selected relations
247         final CCombo relationDirectionCombo = new CCombo(this, SWT.READ_ONLY | SWT.BORDER);
248         relationDirectionCombo.setItems(new String[] {"BI", "NONE", "FORWARD", "BACK"});
249         relationDirectionCombo.select(2);
250         relationDirectionCombo.addSelectionListener(new SelectionAdapter() {
251             public void widgetSelected(SelectionEvent arg0) {
252                 String selected = relationDirectionCombo.getText();
253                 IIntervalRelation.Direction dir = IIntervalRelation.Direction.valueOf(selected);
254                 for (IIntervalRelation relation : _tbv.getSelectionModel().getSelectedRelations()) {
255                     relation.setDirection(dir);
256                 }
257                 _tbv.redraw();
258             }
259         });
260         
261         
262         
263         // selection listener for enabling/disabling relation buttons and combos
264         _tbv.getSelectionModel().addTimeBarSelectionListener(new TimeBarSelectionListener() {
265 
266             public void elementAddedToSelection(TimeBarSelectionModel selectionModel, Object element) {
267                 delRelation.setEnabled(selectionModel.hasRelationSelection());
268                 relationTypeCombo.setEnabled(selectionModel.hasRelationSelection());
269                 relationDirectionCombo.setEnabled(selectionModel.hasRelationSelection());
270             }
271 
272             public void elementRemovedFromSelection(TimeBarSelectionModel selectionModel, Object element) {
273                 delRelation.setEnabled(selectionModel.hasRelationSelection());
274                 relationTypeCombo.setEnabled(selectionModel.hasRelationSelection());
275                 relationDirectionCombo.setEnabled(selectionModel.hasRelationSelection());
276             }
277 
278             public void selectionChanged(TimeBarSelectionModel selectionModel) {
279                 delRelation.setEnabled(selectionModel.hasRelationSelection());
280                 relationTypeCombo.setEnabled(selectionModel.hasRelationSelection());
281                 relationDirectionCombo.setEnabled(selectionModel.hasRelationSelection());
282             }
283         });
284         
285         // check setFirstRowDisplayed
286 //        final Button scrolltolast = new Button(this, SWT.PUSH);
287 //        scrolltolast.setText("scroll to last row ");
288 //        scrolltolast.addSelectionListener(new SelectionAdapter() {
289 //            public void widgetSelected(SelectionEvent arg0) {
290 //            // last model row = 
291 //            TimeBarRow lastrow = _tbv.getModel().getRow(_tbv.getModel().getRowCount()-1);
292 //            _tbv.setFirstRowDisplayed(lastrow);
293 //            }
294 //        });
295     
296         
297     
298         
299         
300 //        final Scale horRatio = new Scale(this, SWT.HORIZONTAL);
301 //        horRatio.setMaximum(100);
302 //        horRatio.setMinimum(0);
303 //        final Scale verRatio = new Scale(this, SWT.HORIZONTAL);
304 //        verRatio.setMaximum(100);
305 //        verRatio.setMinimum(0);
306 //        final Button scrollRatio = new Button(this, SWT.PUSH);
307 //        scrollRatio.setText("scroll focussed intervall to ratio ");
308 //        scrollRatio.addSelectionListener(new SelectionAdapter() {
309 //            public void widgetSelected(SelectionEvent arg0) {
310 //                Interval interval = _tbv.getFocussedInterval();
311 //                double hor = (double)horRatio.getSelection()/100.0;
312 //                double ver = (double)verRatio.getSelection()/100.0;
313 //                System.out.println("hor/ver interval:"+hor+"/"+ver+" "+interval);
314 //                _tbv.scrollIntervalToVisible(interval, hor, ver);
315 //            }
316 //        });
317         
318         
319     }
320 
321 }