View Javadoc

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