AbstractPivotGriffonApplication.java
001 /*
002  * Copyright 2008-2016 the original author or authors.
003  *
004  * Licensed under the Apache License, Version 2.0 (the "License");
005  * you may not use this file except in compliance with the License.
006  * You may obtain a copy of the License at
007  *
008  *     http://www.apache.org/licenses/LICENSE-2.0
009  *
010  * Unless required by applicable law or agreed to in writing, software
011  * distributed under the License is distributed on an "AS IS" BASIS,
012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013  * See the License for the specific language governing permissions and
014  * limitations under the License.
015  */
016 package griffon.pivot;
017 
018 import griffon.core.ApplicationBootstrapper;
019 import griffon.exceptions.InstanceNotFoundException;
020 import org.apache.pivot.wtk.Application;
021 import org.apache.pivot.wtk.ApplicationContext;
022 import org.apache.pivot.wtk.Display;
023 import org.apache.pivot.wtk.Window;
024 import org.codehaus.griffon.runtime.core.AbstractGriffonApplication;
025 import org.codehaus.griffon.runtime.pivot.PivotApplicationBootstrapper;
026 
027 import javax.annotation.Nonnull;
028 import java.util.List;
029 import java.util.Map;
030 
031 import static griffon.util.GriffonClassUtils.setPropertiesNoException;
032 import static java.util.Arrays.asList;
033 import static org.apache.pivot.wtk.ApplicationContext.queueCallback;
034 import static org.apache.pivot.wtk.ApplicationContext.scheduleCallback;
035 import static org.apache.pivot.wtk.ApplicationContext.scheduleRecurringCallback;
036 
037 /**
038  @author Andres Almiray
039  @since 2.0.0
040  */
041 public abstract class AbstractPivotGriffonApplication extends AbstractGriffonApplication implements Application {
042     public AbstractPivotGriffonApplication() {
043         this(EMPTY_ARGS);
044     }
045 
046     public AbstractPivotGriffonApplication(@Nonnull String[] args) {
047         super(args);
048     }
049 
050     @Override
051     public boolean shutdown() {
052         if (super.shutdown()) {
053             exit();
054         }
055         return false;
056     }
057 
058     public void exit() {
059         System.exit(0);
060     }
061 
062     @Nonnull
063     @Override
064     public Object createApplicationContainer(@Nonnull Map<String, Object> attributes) {
065         Window window = new Window();
066         setPropertiesNoException(window, attributes);
067         return window;
068     }
069 
070     @Override
071     public void startup(Display display, org.apache.pivot.collections.Map<String, String> propertiesthrows Exception {
072         ApplicationBootstrapper bootstrapper = createApplicationBootstrapper(display);
073         bootstrapper.bootstrap();
074         afterStartup();
075     }
076 
077     @Nonnull
078     protected ApplicationBootstrapper createApplicationBootstrapper(@Nonnull Display display) {
079         return new PivotApplicationBootstrapper(this, display);
080     }
081 
082     protected void afterStartup() {
083         initialize();
084         startup();
085         ready();
086     }
087 
088     @Override
089     public boolean shutdown(boolean optionalthrows Exception {
090         shutdown();
091         return false;
092     }
093 
094     public void suspend() {
095         event("AppSuspend", asList(this));
096     }
097 
098     public void resume() {
099         event("AppResume", asList(this));
100     }
101 
102     public void schedule(long delay, Runnable callback) {
103         scheduleCallback(callback, delay);
104     }
105 
106     public void scheduleRecurring(long period, Runnable callback) {
107         scheduleRecurringCallback(callback, period);
108     }
109 
110     public void scheduleRecurring(long delay, long period, Runnable callback) {
111         scheduleRecurringCallback(callback, delay, period);
112     }
113 
114     public void queue(Runnable callback) {
115         queue(false, callback);
116     }
117 
118     public void queue(boolean wait, Runnable callback) {
119         queueCallback(callback, wait);
120     }
121 
122     public ApplicationContext.ResourceCacheDictionary getResourceCache() {
123         return ApplicationContext.getResourceCache();
124     }
125 
126     private void event(@Nonnull String eventName, @Nonnull List<?> args) {
127         try {
128             getEventRouter().publishEvent(eventName, args);
129         catch (InstanceNotFoundException infe) {
130             // ignore
131         }
132     }
133 }