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