20 package com.generalrobotix.ui.view;
22 import java.text.DecimalFormat;
24 import org.eclipse.swt.SWT;
25 import org.eclipse.swt.events.KeyEvent;
26 import org.eclipse.swt.events.KeyListener;
27 import org.eclipse.swt.events.SelectionAdapter;
28 import org.eclipse.swt.events.SelectionEvent;
29 import org.eclipse.swt.events.MouseEvent;
30 import org.eclipse.swt.events.MouseListener;
31 import org.eclipse.swt.events.MouseTrackListener;
32 import org.eclipse.swt.events.MouseMoveListener;
33 import org.eclipse.swt.layout.GridData;
34 import org.eclipse.swt.layout.GridLayout;
35 import org.eclipse.swt.widgets.Button;
36 import org.eclipse.swt.widgets.Composite;
37 import org.eclipse.swt.widgets.Display;
38 import org.eclipse.swt.widgets.Label;
39 import org.eclipse.swt.widgets.Scale;
40 import org.eclipse.swt.widgets.Text;
41 import org.eclipse.ui.IActionBars;
42 import org.eclipse.jface.action.Action;
55 @SuppressWarnings(
"serial")
60 public static final String TITLE =
"Logger";
65 private int current_ = 0;
66 private double playRate_ = 1.0;
67 private int frameRate_ = defaultFrameRate_;
68 private boolean isPlaying_ =
false;
69 private boolean isControlDisabled_ =
false;
70 private boolean inSimulation_ =
false;
71 private boolean buttonClick_ =
false;
81 private Button[] btns_ =
new Button[2];
83 private String[] btnToolTips =
new String[] {
87 private final static String[] icons_ = {
"icon_frame-.png",
"icon_frame+.png" };
91 private Action[] toolActs_ =
new Action[6];
92 private String[] actToolTips_ =
new String[] {
97 private final static String[] actIcons_ = {
103 "icon_fastfwd.png" };
104 private final static String[] actDIcons_ = {
108 "icond_playback.png",
110 "icond_fastfwd.png" };
112 private final static int maxFrameRate_ = 50;
113 private final static int defaultFrameRate_ = 10;
116 private static DecimalFormat FORMAT_FAST =
new DecimalFormat(
"Play x ##;Play x-##");
117 private static DecimalFormat FORMAT_SLOW =
new DecimalFormat(
"Play x 1/##;Play x-1/##");
118 private static String timeFormat =
"%8.3f";
122 private int mouseBtnActionNum = 0;
123 private int mouseBtnRepeat_ = 200;
124 private int mouseBtnAccelNeed_ = 5;
125 private int mouseBtnAccelRepeat_ = 30;
126 private boolean isMouseOverBtnR_ =
false;
127 private boolean isMouseOverBtnF_ =
false;
133 double logTimeStepMs_;
138 final long mills2nano = 1000 * 1000;
148 super(name, manager, vp, parent);
150 GridLayout gl =
new GridLayout(6,
false);
152 composite_.setLayout( gl );
155 lblPlayRate_ =
new Label( composite_, SWT.NONE);
157 GridData lblGridData =
new GridData();
158 lblGridData.widthHint = 80;
159 lblPlayRate_.setLayoutData(lblGridData);
163 Composite btnComp =
new Composite ( composite_, SWT.NONE);
164 GridLayout buttonLayout =
new GridLayout(2,
false);
165 buttonLayout.marginHeight = 0;
166 buttonLayout.marginWidth = 0;
167 btnComp.setLayout( buttonLayout );
169 btnFrameR_ =
new Button( btnComp, SWT.NONE );
171 btnFrameR_.addMouseTrackListener(
new MouseTrackListener(){
172 public void mouseEnter(MouseEvent e){
173 isMouseOverBtnR_ =
true;
175 public void mouseExit(MouseEvent e){
176 isMouseOverBtnR_ =
false;
178 public void mouseHover(MouseEvent e){
182 btnFrameR_.addMouseMoveListener(
new MouseMoveListener(){
183 public void mouseMove(MouseEvent e){
184 isMouseOverBtnR_ = (0 <= e.x && e.x < btnFrameR_.getSize().x && 0 <= e.y && e.y < btnFrameR_.getSize().y);
187 btnFrameR_.addMouseListener(
new MouseListener(){
188 public void mouseDoubleClick(MouseEvent e){
190 public void mouseDown(MouseEvent e) {
196 public void mouseUp(MouseEvent e) {
203 btnFrameF_ =
new Button( btnComp, SWT.NONE );
205 btnFrameF_.addMouseTrackListener(
new MouseTrackListener(){
206 public void mouseEnter(MouseEvent e){
207 isMouseOverBtnF_ =
true;
209 public void mouseExit(MouseEvent e){
210 isMouseOverBtnF_ =
false;
212 public void mouseHover(MouseEvent e){
216 btnFrameF_.addMouseMoveListener(
new MouseMoveListener(){
217 public void mouseMove(MouseEvent e){
218 isMouseOverBtnF_ = (0 <= e.x && e.x < btnFrameF_.getSize().x && 0 <= e.y && e.y < btnFrameF_.getSize().y);
221 btnFrameF_.addMouseListener(
new MouseListener(){
222 public void mouseDoubleClick(MouseEvent e){
224 public void mouseDown(MouseEvent e) {
227 startForwardPosTimer();
230 public void mouseUp(MouseEvent e) {
232 stopForwardPosTimer();
237 btns_[0] = btnFrameR_;
238 btns_[1] = btnFrameF_;
240 for(
int i=0;
i<btns_.length;
i++) {
242 btns_[
i].setToolTipText( btnToolTips[i] );
246 tFldTime_ =
new Text(composite_, SWT.SINGLE|SWT.BORDER);
248 GridData textGridData =
new GridData();
249 textGridData.widthHint = 60;
250 tFldTime_.setLayoutData(textGridData);
251 tFldTime_.addKeyListener(
new KeyListener(){
252 public void keyPressed(KeyEvent e) {}
253 public void keyReleased(KeyEvent e) {
254 if (e.character == SWT.CR) {
255 String str = tFldTime_.getText();
257 double tm = Double.parseDouble(str);
262 }
catch(Exception ex){
270 sliderTime_ =
new Scale( composite_, SWT.NONE );
271 sliderTime_.addSelectionListener(
new SelectionAdapter(){
272 public void widgetSelected(SelectionEvent e){
273 currentItem_.
setPosition(sliderTime_.getSelection());
277 sliderTime_.addKeyListener(
new KeyListener(){
278 public void keyPressed(KeyEvent e) {
279 if (e.keyCode == SWT.ARROW_LEFT || e.keyCode == SWT.ARROW_DOWN){
282 currentItem_.
setPosition(sliderTime_.getSelection());
283 }
else if (e.keyCode == SWT.ARROW_RIGHT || e.keyCode == SWT.ARROW_UP){
286 currentItem_.
setPosition(sliderTime_.getSelection());
289 public void keyReleased(KeyEvent e) {
292 GridData gd =
new GridData(SWT.HORIZONTAL|SWT.FILL|GridData.FILL_HORIZONTAL);
293 sliderTime_.setLayoutData(gd);
296 lblFrameRate_ =
new Label(composite_, SWT.NONE);
297 lblFrameRate_.setText(
MessageBundle.
get(
"GrxLoggerView.label.FPS")+defaultFrameRate_);
300 sliderFrameRate_ =
new Scale( composite_, SWT.HORIZONTAL );
302 sliderFrameRate_.setSize(80, 27);
303 sliderFrameRate_.setMaximum(maxFrameRate_);
304 sliderFrameRate_.setMinimum(1);
305 sliderFrameRate_.setSelection(defaultFrameRate_);
306 sliderFrameRate_.addSelectionListener(
new SelectionAdapter(){
307 public void widgetSelected(SelectionEvent e){
308 frameRate_ = sliderFrameRate_.getSelection();
309 lblFrameRate_.setText(
MessageBundle.
get(
"GrxLoggerView.label.FPS")+String.valueOf(frameRate_) );
313 Action actFRwd_ =
new Action(){
317 else if (playRate_ > 0)
319 else if (playRate_ > -64)
326 Action actSRwd_ =
new Action(){
329 playRate_ = -1.0/8.0;
330 else if (playRate_ > 0)
332 else if (1/playRate_ > -64)
338 Action actPause_ =
new Action(){
344 Action actPlay_ =
new Action(){
352 Action actSFwd_ =
new Action(){
356 else if (playRate_ < 0)
358 else if (1/playRate_ < 64)
365 Action actFFwd_ =
new Action(){
369 else if (playRate_ < 0)
371 else if (playRate_ < 64)
378 toolActs_[0] = actFRwd_;
379 toolActs_[1] = actSRwd_;
380 toolActs_[2] = actPause_;
381 toolActs_[3] = actPlay_;
382 toolActs_[4] = actSFwd_;
383 toolActs_[5] = actFFwd_;
384 IActionBars bars = vp.getViewSite().getActionBars();
385 for(
int i = 0;
i < toolActs_.length;
i++) {
388 toolActs_[
i].setToolTipText( actToolTips_[i] );
389 bars.getToolBarManager().add(toolActs_[i]);
393 backPosRun_ =
new Runnable() {
395 Display display = btnFrameR_.getDisplay();
396 if (!display.isDisposed())
400 mouseBtnActionNum += 1;
403 display.timerExec((mouseBtnActionNum < mouseBtnAccelNeed_ ? mouseBtnRepeat_ : mouseBtnAccelRepeat_),
this);
407 forwardPosRun_ =
new Runnable() {
409 Display display = btnFrameF_.getDisplay();
410 if (!display.isDisposed())
414 mouseBtnActionNum += 1;
417 display.timerExec((mouseBtnActionNum < mouseBtnAccelNeed_ ? mouseBtnRepeat_ : mouseBtnAccelRepeat_),
this);
422 setScrollMinSize(SWT.DEFAULT,SWT.DEFAULT);
430 if(currentItem_ !=
null){
435 if(currentItem_!=
null){
436 _setTimeSeriesItem(currentItem_);
439 updatePosition(currentItem_, currentItem_.
getPosition());
441 _setTimeSeriesItem(
null);
451 if (current_ == currentItem_.
getLogSize()-1 && playRate_ > 0){
460 if(currentItem_ ==
null)
return;
462 if (_isAtTheEndAfterPlayback()) currentItem_.
setPosition(0);
465 logTimeStepMs_ = currentItem_.
getDbl(
"logTimeStep", 0.001)*1000;
470 logTimeStepMs_ = currentItem_.
getDbl(
"logTimeStep", 0.001)*1000;
472 logTimeStepMs_ = (currentItem_.
getTime(1) - currentItem_.
getTime(0))*1000;
476 Runnable playRun_ =
new Runnable() {
478 if(!isPlaying_)
return;
480 long nowNs = System.nanoTime();
481 boolean _continue =
true;
483 int diffpos = (
int)((nowNs - prevDispNs_) / (logTimeStepMs_ * mills2nano) * playRate_);
486 if(inSimulation_ && !buttonClick_)
489 newpos = current_ + diffpos;
490 if (sliderMax < newpos) {
494 }
else if (newpos < 0) {
500 if(newpos != current_){
503 diffpos = newpos - current_;
505 prevDispNs_ += (long)((logTimeStepMs_ * mills2nano) * diffpos / playRate_);
509 nowNs = System.nanoTime();
510 int sleepTime = (
int)((1000.0d/frameRate_) - ((nowNs - nextTimerNs_) / mills2nano));
513 if(sleepTime < 1) sleepTime = 1;
514 nextTimerNs_ = nowNs + (sleepTime * mills2nano);
515 Display display = composite_.getDisplay();
516 if (!display.isDisposed())
517 display.timerExec(sleepTime,
this);
525 long nowNs = System.nanoTime();
527 int sleepTime = (
int)(1000.0d/frameRate_);
528 nextTimerNs_ = nowNs + (sleepTime * mills2nano);
529 Display display = composite_.getDisplay();
530 if (!display.isDisposed())
531 display.timerExec(sleepTime, playRun_);
533 if (Math.abs(playRate_)<1.0)
534 lblPlayRate_.setText(FORMAT_SLOW.format(1/playRate_));
536 lblPlayRate_.setText(FORMAT_FAST.format(playRate_));
559 super.restoreProperties();
561 timeFormat = getStr(
"timeFormat",
"%8.3f");
567 sliderTime_.setSelection(0);
569 _updateTimeField(item);
575 sliderTimeSetMaximam(1);
579 sliderTimeSetMaximam(1);
586 GrxWorldStateItem worldStateItem = (GrxWorldStateItem) item;
589 if(currentItem_!=worldStateItem){
590 _setTimeSeriesItem(worldStateItem);
591 currentItem_ = worldStateItem;
594 updatePosition(currentItem_, currentItem_.
getPosition());
599 if(currentItem_==worldStateItem){
602 _setTimeSeriesItem(
null);
610 GrxSimulationItem simItem = (GrxSimulationItem) item;
613 if(simItem_!=simItem){
620 if(simItem_==simItem){
637 tFldTime_.setText(String.format(timeFormat, t));
650 return sliderTime_.getMaximum();
662 if(simItem_==plugin){
663 if((String)
arg[0]==
"StartSimulation"){
664 inSimulation_ =
true;
665 buttonClick_ =
false;
673 }
else if((String)
arg[0]==
"StopSimulation"){
674 inSimulation_ =
false;
679 }
else if(currentItem_==plugin) {
680 if((String)
arg[0]==
"ClearLog"){
683 _setTimeSeriesItem(currentItem_);
684 }
else if((String)
arg[0]==
"LoadLog"){
685 _setTimeSeriesItem(currentItem_ );
686 updatePosition(currentItem_, currentItem_.
getPosition());
692 if(currentItem_ != plugin)
695 int pos = arg_pos.intValue();
697 if ( pos < 0 || logSize < pos){
701 sliderTimeSetMaximam(logSize-1);
702 sliderTime_.setSelection(pos);
703 _updateTimeField(currentItem_);
715 if (isControlDisabled_)
return;
716 sliderTime_.setEnabled(b);
717 tFldTime_.setEnabled(b);
718 lblPlayRate_.setEnabled(b);
719 for (
int i=0;
i<btns_.length;
i++)
720 btns_[
i].setEnabled(b);
722 for (
int i=0;
i<toolActs_.length;
i++)
723 toolActs_[
i].setEnabled(b);
729 isControlDisabled_ =
true;
733 isControlDisabled_ =
false;
739 if(currentItem_!=
null)
751 sliderTime_.setMaximum(value);
754 sliderTime_.setPageIncrement(5);
756 sliderTime_.setPageIncrement(50);
758 sliderTime_.setPageIncrement(500);
764 currentItem_.
setPosition(sliderTime_.getSelection()-1);
769 currentItem_.
setPosition(sliderTime_.getSelection()+1);
772 mouseBtnActionNum = 0;
774 Display display = btnFrameR_.getDisplay();
775 if (!display.isDisposed())
777 display.timerExec(mouseBtnRepeat_, backPosRun_);
781 mouseBtnActionNum = 0;
783 Display display = btnFrameR_.getDisplay();
784 if (!display.isDisposed())
786 display.timerExec(-1, backPosRun_);
790 mouseBtnActionNum = 0;
792 Display display = btnFrameF_.getDisplay();
793 if (!display.isDisposed())
795 display.timerExec(mouseBtnRepeat_, forwardPosRun_);
799 mouseBtnActionNum = 0;
801 Display display = btnFrameF_.getDisplay();
802 if (!display.isDisposed())
804 display.timerExec(-1, forwardPosRun_);
void registerItemChange(GrxBaseItem item, int event)
static final String get(String key)
void update(GrxBasePlugin plugin, Object... arg)
void addObserver(GrxObserver v)
#define null
our own NULL pointer
GrxLoggerView(String name, GrxPluginManager manager, GrxBaseViewPart vp, Composite parent)
png_infop png_charpp name
void deleteObserver(GrxObserver v)
void _setTimeSeriesItem(GrxWorldStateItem item)
boolean _isAtTheEndAfterPlayback()
Image getImage(String iconName)
void setPosition(Integer pos)
static final int SELECTED_ITEM
void deletePosObserver(GrxPositionObserver v)
static Activator getDefault()
void sliderTimeSetMaximam(int value)
static final int REMOVE_ITEM
void setEnabled(boolean b)
int getPositionAt(Double t)
void updatePosition(GrxBasePlugin plugin, Integer arg_pos)
static void println(String s)
final Double getDbl(String key, Double defaultVal)
get double value associated to key
void addPosObserver(GrxPositionObserver v)
プラグイン管理クラス GrxUIの核になるクラス。プラグインのロード等の、初期化を実行する。 プラグインとそ...
boolean isPlaying()
check where playing now or not
void pause()
pause playback
void _updateTimeField(GrxWorldStateItem item)
static final int NOTSELECTED_ITEM
ImageDescriptor getDescriptor(String iconName)
void startForwardPosTimer()
void stopForwardPosTimer()