Parent: [a7a9ff] (diff)

Download this file

TimeCoordinatorController.java    193 lines (172 with data), 6.4 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
* Copyright 2013-2014 TECO - Karlsruhe Institute of Technology.
*
* This file is part of TACET.
*
* TACET is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* TACET is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with TACET. If not, see <http://www.gnu.org/licenses/>.
*/
package squirrel.controller;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import squirrel.controller.video.MainController;
import squirrel.model.TimeCoordinator;
import squirrel.view.TimeCoordinatorView;
public class TimeCoordinatorController implements ActionListener, MouseListener {
private List<TimeCoordinatorView> controls;
private TimeCoordinator timeCoordinator;
private MainController mainController;
private boolean mainControllerSet = false;
private boolean mousePressedPlaying = false;
public TimeCoordinatorController(TimeCoordinator timeCoordinator) {
this.timeCoordinator = timeCoordinator;
controls = new ArrayList<TimeCoordinatorView>();
}
public void addControls(TimeCoordinatorView tcv) {
this.controls.add(tcv);
timeCoordinator.addListener(tcv);
}
public void addMainController(MainController mainController) {
this.mainController = mainController;
this.mainControllerSet = true;
timeCoordinator.addListener(mainController);
}
public long getMaxTime() {
return timeCoordinator.getMaxTime();
}
public long getTime() {
return timeCoordinator.getTime();
}
@Override
public void mouseClicked(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent arg0) {
for (TimeCoordinatorView tcv : controls) {
if (arg0.getSource() == tcv.positionSlider) {
if (timeCoordinator.isPlaying()) {
mousePressedPlaying = true;
timeCoordinator.pause();
if (mainControllerSet) {
try {
mainController.pauseAll();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} else {
mousePressedPlaying = false;
}
}
}
}
@Override
public void mouseReleased(MouseEvent arg0) {
for (TimeCoordinatorView tcv : controls) {
if (arg0.getSource() == tcv.positionSlider) {
setSliderBasedPosition(tcv);
}
}
}
private void setSliderBasedPosition(TimeCoordinatorView tcv) {
timeCoordinator.setTime(tcv.getPositionValue());
if (mainControllerSet) {
try {
mainController.setTimeAll(timeCoordinator.getTime());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
updatePosition();
if (mousePressedPlaying) {
timeCoordinator.play();
if (mainControllerSet) {
try {
mainController.playAll();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
private void updatePosition() {
for (TimeCoordinatorView tcv : controls) {
tcv.setPositionValue(timeCoordinator.getTime());
}
}
@Override
public void actionPerformed(ActionEvent arg0) {
for (TimeCoordinatorView tcv : controls) {
try {
if (arg0.getSource() == tcv.playButton) {
if (timeCoordinator.isPlaying()) {
timeCoordinator.pause();
if (mainControllerSet) {
mainController.pauseAll();
}
} else {
timeCoordinator.play();
if (mainControllerSet) {
mainController.playAll();
}
}
} else if (arg0.getSource() == tcv.stopButton) {
timeCoordinator.stop();
if (mainControllerSet) {
mainController.stopAll();
}
} else if (arg0.getSource() == tcv.next) {
timeCoordinator.pause();
timeCoordinator.nextStep();
if (mainControllerSet) {
mainController.nextAll();
}
} else if (arg0.getSource() == tcv.prev) {
timeCoordinator.pause();
timeCoordinator.prevStep();
if (mainControllerSet) {
mainController.prevAll();
}
} else if (arg0.getSource() == tcv.toggleMuteButton) {
if (mainControllerSet) {
if (mainController.isMute()) {
mainController.unMuteAll();
} else {
mainController.muteAll();
}
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}