/*
* Copyright 2009 Peter Karich, peat_hal 'at' users 'dot' sourceforge 'dot' net.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* under the License.
*/
package de.timefinder.core.ui.command;
import de.timefinder.algo.constraint.DifferentDayConstraint;
import de.timefinder.core.ui.metadata.DualEventDescriptor;
import de.timefinder.core.util.DualEvent;
import de.timefinder.data.Event;
import de.timefinder.data.access.Dao;
import java.util.List;
import org.springframework.richclient.application.ApplicationServicesLocator;
import org.springframework.richclient.application.ViewDescriptorRegistry;
import org.springframework.richclient.application.statusbar.StatusBar;
/**
*
* @author Peter Karich, peat_hal 'at' users 'dot' sourceforge 'dot' net
*/
public class DualEventCommand extends TimeFinderCommand {
public static final String ID = "dualEventCommand";
public DualEventCommand() {
super(ID);
}
public static void main(String[] args) {
new ChangeSettingsCommand().doOnce();
}
@Override
protected void doOnce() {
ViewDescriptorRegistry viewDescriptorRegistry = (ViewDescriptorRegistry) ApplicationServicesLocator.services().getService(ViewDescriptorRegistry.class);
final DualEventDescriptor view = (DualEventDescriptor) viewDescriptorRegistry.getViewDescriptor("dualEventView");
final List<DualEvent> eventsList = view.getDualEvents();
final Dao<Event> dao = view.getDao();
final StatusBar bar = getApplicationWindow().getStatusBar();
bar.getProgressMonitor().taskStarted(tr.get(ID + ".startTask"), 100);
MySwingWorker sw = new MySwingWorker(ID) {
@Override
protected void myconstruct() throws Exception {
double delta = 90.0 / dao.getAll().size();
double counter = 1;
for (Event a : dao.getAll()) {
bar.getProgressMonitor().worked((int) counter);
counter += delta;
if (!firstEvent(a))
continue;
for (Event b : dao.getAll()) {
if (a == b || !firstEvent(b))
continue;
eventsList.add(new DualEvent(a, b));
}
}
logger.info("Read dual events:" + eventsList.size());
}
private boolean firstEvent(Event ev) {
DifferentDayConstraint constr = ev.getConstraint(DifferentDayConstraint.class);
if (constr != null) {
// only one event of the event group
if (constr.getEvents().size() > 0
&& ev == constr.getEvents().iterator().next())
return true;
}
return false;
}
@Override
protected void done() {
bar.getProgressMonitor().worked(95);
view.createShowViewCommand(getApplicationWindow()).execute();
super.done();
}
};
sw.execute();
}
}