View Javadoc

1   /*
2    *  File: MilliControlPanel.java 
3    *  Copyright (c) 2004-2009  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.millis.swt;
21  
22  import java.util.Locale;
23  
24  import org.eclipse.swt.SWT;
25  import org.eclipse.swt.custom.CCombo;
26  import org.eclipse.swt.events.SelectionAdapter;
27  import org.eclipse.swt.events.SelectionEvent;
28  import org.eclipse.swt.events.SelectionListener;
29  import org.eclipse.swt.layout.RowData;
30  import org.eclipse.swt.layout.RowLayout;
31  import org.eclipse.swt.printing.PrintDialog;
32  import org.eclipse.swt.printing.Printer;
33  import org.eclipse.swt.printing.PrinterData;
34  import org.eclipse.swt.widgets.Button;
35  import org.eclipse.swt.widgets.Composite;
36  import org.eclipse.swt.widgets.Display;
37  import org.eclipse.swt.widgets.Label;
38  import org.eclipse.swt.widgets.Scale;
39  
40  import de.jaret.util.date.holidayenumerator.HolidayEnumeratorFactory;
41  import de.jaret.util.ui.timebars.TimeBarViewerInterface;
42  import de.jaret.util.ui.timebars.ViewConfiguration;
43  import de.jaret.util.ui.timebars.swt.TimeBarPrinter;
44  import de.jaret.util.ui.timebars.swt.TimeBarViewer;
45  import de.jaret.util.ui.timebars.swt.renderer.BoxTimeScaleRenderer;
46  import de.jaret.util.ui.timebars.swt.renderer.DefaultGapRenderer;
47  import de.jaret.util.ui.timebars.swt.renderer.DefaultTimeScaleRenderer;
48  
49  /***
50   * Common settings for a timebar viewer for demonstrating.
51   * 
52   * @author Peter Kliem
53   * @version $Id: MilliControlPanel.java 1083 2011-07-01 20:29:16Z kliem $
54   */
55  public class MilliControlPanel extends Composite {
56  
57      private TimeBarViewer _tbv;
58      private TimeBarViewer _tbv2;
59  
60      public MilliControlPanel(Composite arg0, int arg1, TimeBarViewer tbv, TimeBarViewer tbv2) {
61          super(arg0, arg1);
62          _tbv = tbv;
63          _tbv2 = tbv2;
64          createControls(this);
65      }
66  
67      /***
68       * @param panel
69       */
70      private void createControls(MilliControlPanel panel) {
71          panel.setLayout(new RowLayout());
72  
73          // check whether this has been initialized nd return otherwise
74          if (_tbv == null) {
75              return; 
76          }
77          
78          if (_tbv.getPixelPerSecond() < 300) {
79              final Scale pixPerSecondsScale = new Scale(this, SWT.HORIZONTAL);
80              pixPerSecondsScale.setMaximum(700);
81              pixPerSecondsScale.setMinimum(1);
82              if (_tbv.getPixelPerSecond() * (24.0 * 60.0 * 60.0) > 700) {
83                  pixPerSecondsScale.setMaximum((int) (_tbv.getPixelPerSecond() * (24.0 * 60.0 * 60.0)));
84              }
85              pixPerSecondsScale.addSelectionListener(new SelectionListener() {
86                  public void widgetSelected(SelectionEvent ev) {
87                      int val = pixPerSecondsScale.getSelection();
88                      double pps = ((double) val) / (24.0 * 60.0 * 60.0);
89                      System.out.println("scale " + val + "pps " + pps);
90                      _tbv.setPixelPerSecond(pps);
91                  }
92  
93                  public void widgetDefaultSelected(SelectionEvent arg0) {
94                  }
95              });
96              pixPerSecondsScale.setSelection((int) (_tbv.getPixelPerSecond() * (24.0 * 60.0 * 60.0)));
97              RowData rd = new RowData(800, 40);
98              pixPerSecondsScale.setLayoutData(rd);
99          } else {
100             // this is for the millisecond example
101             final Scale pixPerSecondsScale = new Scale(this, SWT.HORIZONTAL);
102             pixPerSecondsScale.setMaximum(8000);
103             pixPerSecondsScale.setMinimum(300);
104             pixPerSecondsScale.addSelectionListener(new SelectionListener() {
105                 public void widgetSelected(SelectionEvent ev) {
106                     int val = pixPerSecondsScale.getSelection();
107                     double pps = (double) val;
108                     System.out.println("scale " + val + "pps " + pps);
109                     _tbv.setPixelPerSecond(pps);
110                 }
111 
112                 public void widgetDefaultSelected(SelectionEvent arg0) {
113                 }
114             });
115             pixPerSecondsScale.setSelection((int) _tbv.getPixelPerSecond());
116             RowData rd = new RowData(800, 40);
117             pixPerSecondsScale.setLayoutData(rd);
118 
119         }
120         final Scale rowHeightScale = new Scale(this, SWT.HORIZONTAL);
121         rowHeightScale.setMaximum(300);
122         rowHeightScale.setMinimum(5);
123         rowHeightScale.setSelection(_tbv.getRowHeight());
124         rowHeightScale.addSelectionListener(new SelectionListener() {
125             public void widgetSelected(SelectionEvent ev) {
126                 int val = rowHeightScale.getSelection();
127                 _tbv.setRowHeight(val);
128             }
129 
130             public void widgetDefaultSelected(SelectionEvent arg0) {
131             }
132         });
133 
134         final Button gapCheck = new Button(this, SWT.CHECK);
135         gapCheck.setText("GapRenderer");
136         gapCheck.addSelectionListener(new SelectionAdapter() {
137             public void widgetSelected(SelectionEvent arg0) {
138                 if (gapCheck.getSelection()) {
139                     _tbv.setGapRenderer(new DefaultGapRenderer());
140                 } else {
141                     _tbv.setGapRenderer(null);
142                 }
143             }
144         });
145 
146         Button b = new Button(this, SWT.PUSH);
147         b.setText("Print");
148         b.addSelectionListener(new SelectionAdapter() {
149 
150             public void widgetSelected(SelectionEvent arg0) {
151                 print();
152             }
153         });
154 
155         final Button drawRowGridCheck = new Button(this, SWT.CHECK);
156         drawRowGridCheck.setText("Draw row grid");
157         drawRowGridCheck.setSelection(_tbv.getDrawRowGrid());
158         drawRowGridCheck.addSelectionListener(new SelectionAdapter() {
159             public void widgetSelected(SelectionEvent arg0) {
160                 _tbv.setDrawRowGrid(drawRowGridCheck.getSelection());
161             }
162         });
163 
164         final Button drawOverlappingCheck = new Button(this, SWT.CHECK);
165         drawOverlappingCheck.setText("Draw overlapping");
166         drawOverlappingCheck.setSelection(_tbv.getDrawOverlapping());
167         drawOverlappingCheck.addSelectionListener(new SelectionAdapter() {
168             public void widgetSelected(SelectionEvent arg0) {
169                 _tbv.setDrawOverlapping(drawOverlappingCheck.getSelection());
170             }
171         });
172 
173         final Button lineDragCheck = new Button(this, SWT.CHECK);
174         lineDragCheck.setText("Allow line drag");
175         lineDragCheck.setSelection(_tbv.isLineDraggingAllowed());
176         lineDragCheck.addSelectionListener(new SelectionAdapter() {
177             public void widgetSelected(SelectionEvent arg0) {
178                 _tbv.setLineDraggingAllowed(lineDragCheck.getSelection());
179             }
180         });
181 
182         Label l = new Label(this, SWT.NULL);
183         l.setText("Time scale position:");
184 
185         final CCombo tsPositionCombo = new CCombo(this, SWT.READ_ONLY | SWT.BORDER);
186         tsPositionCombo.setItems(new String[] {"Top", "Bottom", "none"});
187         int index = 0; // top
188         if (_tbv.getTimeScalePosition() == TimeBarViewerInterface.TIMESCALE_POSITION_BOTTOM) {
189             index = 1;
190         } else if (_tbv.getTimeScalePosition() == TimeBarViewerInterface.TIMESCALE_POSITION_NONE) {
191             index = 2;
192         }
193         tsPositionCombo.select(index);
194         tsPositionCombo.addSelectionListener(new SelectionAdapter() {
195             public void widgetSelected(SelectionEvent arg0) {
196                 if (tsPositionCombo.getSelectionIndex() == 0) {
197                     _tbv.setTimeScalePosition(TimeBarViewerInterface.TIMESCALE_POSITION_TOP);
198                 } else if (tsPositionCombo.getSelectionIndex() == 1) {
199                     _tbv.setTimeScalePosition(TimeBarViewerInterface.TIMESCALE_POSITION_BOTTOM);
200                 } else if (tsPositionCombo.getSelectionIndex() == 2) {
201                     _tbv.setTimeScalePosition(TimeBarViewerInterface.TIMESCALE_POSITION_NONE);
202                 }
203             }
204         });
205 
206         final Button boxTSCheck = new Button(this, SWT.CHECK);
207         boxTSCheck.setText("Use BoxTimeScaleRenderer");
208         boxTSCheck.setSelection(_tbv.getTimeScaleRenderer() instanceof BoxTimeScaleRenderer);
209         boxTSCheck.addSelectionListener(new SelectionAdapter() {
210             public void widgetSelected(SelectionEvent arg0) {
211                 if (boxTSCheck.getSelection()) {
212                     _tbv.setTimeScaleRenderer(new BoxTimeScaleRenderer());
213                 } else {
214                     DefaultTimeScaleRenderer scaleRenderer = new DefaultTimeScaleRenderer();
215                     scaleRenderer.setHolidayEnumerator(HolidayEnumeratorFactory.getHolidayEnumeratorInstance(Locale
216                             .getDefault(), "NRW"));
217                     _tbv.setTimeScaleRenderer(scaleRenderer);
218                 }
219             }
220         });
221 
222         final Button optScrollingCheck = new Button(this, SWT.CHECK);
223         optScrollingCheck.setText("Use optimized scrolling");
224         optScrollingCheck.setSelection(_tbv.getOptimizeScrolling());
225         optScrollingCheck.addSelectionListener(new SelectionAdapter() {
226             public void widgetSelected(SelectionEvent arg0) {
227                 _tbv.setOptimizeScrolling(optScrollingCheck.getSelection());
228                 if (_tbv2 != null) {
229                     _tbv2.setOptimizeScrolling(optScrollingCheck.getSelection());
230                 }
231             }
232         });
233 
234         l = new Label(this, SWT.NULL);
235         l.setText("Orientation:");
236 
237         final CCombo orientationCombo = new CCombo(this, SWT.READ_ONLY | SWT.BORDER);
238         orientationCombo.setItems(new String[] {TimeBarViewerInterface.Orientation.HORIZONTAL.toString(),
239                 TimeBarViewerInterface.Orientation.VERTICAL.toString()});
240         index = 0; // horizontal
241         if (_tbv.getTBOrientation() == TimeBarViewerInterface.Orientation.VERTICAL) {
242             index = 1;
243         }
244         orientationCombo.select(index);
245         orientationCombo.addSelectionListener(new SelectionAdapter() {
246             public void widgetSelected(SelectionEvent arg0) {
247                 if (orientationCombo.getSelectionIndex() == 0) {
248                     _tbv.setTBOrientation(TimeBarViewerInterface.Orientation.HORIZONTAL);
249                 } else if (orientationCombo.getSelectionIndex() == 1) {
250                     _tbv.setTBOrientation(TimeBarViewerInterface.Orientation.VERTICAL);
251                 } else {
252                     throw new RuntimeException("illegal");
253                 }
254             }
255         });
256 
257         // var row heights
258         final Button varHeightsCheck = new Button(this, SWT.CHECK);
259         varHeightsCheck.setText("Var heights/widths");
260         varHeightsCheck.setSelection(_tbv.getTimeBarViewState().getUseVariableRowHeights());
261         varHeightsCheck.addSelectionListener(new SelectionAdapter() {
262             public void widgetSelected(SelectionEvent arg0) {
263                 _tbv.getTimeBarViewState().setUseVariableRowHeights(varHeightsCheck.getSelection());
264                 if (_tbv2 != null) {
265                     _tbv2.getTimeBarViewState().setUseVariableRowHeights(varHeightsCheck.getSelection());
266                 }
267             }
268         });
269         // allow height drag
270         final Button heightDragCheck = new Button(this, SWT.CHECK);
271         heightDragCheck.setText("Drag heights/widths");
272         heightDragCheck.setSelection(_tbv.isRowHeightDragginAllowed());
273         heightDragCheck.addSelectionListener(new SelectionAdapter() {
274             public void widgetSelected(SelectionEvent arg0) {
275                 _tbv.setRowHeightDraggingAllowed(heightDragCheck.getSelection());
276                 if (_tbv2 != null) {
277                     _tbv2.setRowHeightDraggingAllowed(heightDragCheck.getSelection());
278                 }
279             }
280         });
281 
282         // rem from synchronizer (Test)
283         // final Button remFromSynchronizer = new Button(this, SWT.PUSH);
284         // remFromSynchronizer.setText("Rem from synch");
285         // remFromSynchronizer.addSelectionListener(new SelectionAdapter() {
286         // public void widgetSelected(SelectionEvent arg0) {
287         // SwtPdiExample.tbvSync.remViewer(_tbv2);
288         // }
289         // });
290 
291         l = new Label(this, SWT.NULL);
292         l.setText("Header width:");
293 
294         final Scale headerWidthScale = new Scale(this, SWT.HORIZONTAL);
295         headerWidthScale.setMaximum(300);
296         headerWidthScale.setMinimum(0);
297         headerWidthScale.setSelection(_tbv.getYAxisWidth());
298         headerWidthScale.addSelectionListener(new SelectionAdapter() {
299             public void widgetSelected(SelectionEvent ev) {
300                 int val = headerWidthScale.getSelection();
301                 _tbv.setYAxisWidth(val);
302                 _tbv.redraw();
303             }
304         });
305 
306         // autoscroll delta
307         l = new Label(this, SWT.NULL);
308         l.setText("AutoscrollDelta :");
309 
310         final Scale autoscrollDeltaScale = new Scale(this, SWT.HORIZONTAL);
311         autoscrollDeltaScale.setMaximum(300);
312         autoscrollDeltaScale.setMinimum(0);
313         autoscrollDeltaScale.setSelection(_tbv.getAutoscrollDelta());
314         autoscrollDeltaScale.addSelectionListener(new SelectionAdapter() {
315             public void widgetSelected(SelectionEvent ev) {
316                 int val = autoscrollDeltaScale.getSelection();
317                 _tbv.setAutoscrollDelta(val);
318                 if (_tbv2 != null) {
319                     _tbv2.setAutoscrollDelta(val);
320                 }
321             }
322         });
323 
324         // drag all selected
325         final Button allSelectedDragCheck = new Button(this, SWT.CHECK);
326         allSelectedDragCheck.setText("Drag selected");
327         allSelectedDragCheck.setSelection(_tbv.getDragAllSelectedIntervals());
328         allSelectedDragCheck.addSelectionListener(new SelectionAdapter() {
329             public void widgetSelected(SelectionEvent arg0) {
330                 _tbv.setDragAllSelectedIntervals(allSelectedDragCheck.getSelection());
331                 if (_tbv2 != null) {
332                     _tbv2.setDragAllSelectedIntervals(allSelectedDragCheck.getSelection());
333                 }
334             }
335         });
336 
337     }
338 
339     /***
340      * Very simple print command.
341      */
342     public void print() {
343         // use a stanard print dialog to choose a printer
344         PrintDialog pd = new PrintDialog(Display.getCurrent().getActiveShell());
345         PrinterData pdata = pd.open();
346         // create the printer device
347         Printer printer = new Printer(pdata);
348 
349         // create a view configuration for configuring the print
350         ViewConfiguration viewConfiguration = new ViewConfiguration();
351         // set the seconds per page to be the seconds displayed by the viewer
352         viewConfiguration.setSecondsPerPage(_tbv.getEndDate().diffSeconds(_tbv.getStartDate()));
353         // start date is the current start on the screen
354         // the end date is left null -> printing will print all remaining data
355         viewConfiguration.setStartDate(_tbv.getStartDate().copy());
356         // set the footer text
357         viewConfiguration.setFootLine(_tbv.getTitle());
358         // set a name for the viewconfiguration (this will be the name of the printing job)
359         viewConfiguration.setName(_tbv.getTitle() + "_print");
360 
361         // create the time bar printer instance and initilaize it
362         TimeBarPrinter tbp = new TimeBarPrinter(printer);
363         tbp.init(_tbv);
364         // do the actual print
365         tbp.print(viewConfiguration);
366 
367         // dispose the time bar printer
368         tbp.dispose();
369         // dispose the printer device
370         printer.dispose();
371     }
372 
373 }