Skip to content

Commit

Permalink
allow OnShow when Perspective is initialized, fixed issues with OnSho…
Browse files Browse the repository at this point in the history
…w/OnHide in perspective
  • Loading branch information
JacpFX committed Jul 3, 2015
1 parent 633cfde commit f2d92f7
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 44 deletions.
13 changes: 13 additions & 0 deletions JACP.API/src/main/java/org/jacpfx/api/component/Perspective.java
Expand Up @@ -167,4 +167,17 @@ void postInit(
*/
void setUIType(final UIType type);

/**
* defines the position of an active perspective in workspace
* @param pos
* @param of
*/
void updatePositions(int pos, int of);

/**
* Marks the last perspective to handle, this is the visible perspective after initialisation
* @return true if perspective is the last visible perspective while initialisation
*/
boolean isLast();

}
Expand Up @@ -103,10 +103,9 @@ public final void handleAndReplaceComponent(final Message<Event, Object> action,
private void handleActivePerspective(final Perspective<Node, EventHandler<Event>, Event, Object> perspective, final PerspectiveLayoutInterface<? extends Node, Node> perspectiveLayout, final Node componentOld) {
if (componentOld != null) {
this.handlePerspectiveReassignment(perspective, perspectiveLayout);
} // End outer if
else {
} else {
this.initPerspectiveUI(perspectiveLayout);
} // End else
}
}

private void handleInactivePerspective(final Perspective<Node, EventHandler<Event>, Event, Object> perspective, final PerspectiveLayoutInterface<? extends Node, Node> perspectiveLayout, final Node componentOld) {
Expand All @@ -124,11 +123,11 @@ private void shutDownAndClearComponents(final Perspective<Node, EventHandler<Eve
final List<SubComponent<EventHandler<Event>, Event, Object>> componentsToShutdown = perspective.getSubcomponents();
componentsToShutdown.stream()
.filter(c -> c.getContext().isActive())
.forEach(this::shutDownComponent);
.forEach(PerspectiveHandlerImpl::shutDownComponent);
perspective.removeAllCompnents();
}

private void shutDownComponent(final SubComponent<EventHandler<Event>, Event, Object> component) {
private static void shutDownComponent(final SubComponent<EventHandler<Event>, Event, Object> component) {
if (EmbeddedFXComponent.class.isAssignableFrom(component.getClass())) {
TearDownHandler.shutDownFXComponent(EmbeddedFXComponent.class.cast(component), component.getContext().getParentId());
} else {
Expand All @@ -144,10 +143,9 @@ private void displayNextPossiblePerspective(final Perspective<Node, EventHandler
if (!possiblePerspectiveId.equals(perspectiveIdBefore)) {
final PerspectiveLayoutInterface<? extends Node, Node> perspectiveLayoutReplacementComponent = possiblePerspectiveToShow
.getIPerspectiveLayout();
final Perspective<Node, EventHandler<Event>, Event, Object> Perspective = possiblePerspectiveToShow;
final FXComponentLayout layout = Context.class.cast(possiblePerspectiveToShow.getContext()).getComponentLayout();
// execute OnShow
FXUtil.invokeHandleMethodsByAnnotation(OnShow.class, possiblePerspectiveToShow.getPerspective(), perspectiveLayoutReplacementComponent,
Perspective.getType().equals(UIType.DECLARATIVE) ? Perspective.getDocumentURL() : null, possiblePerspectiveToShow.getContext().getResourceBundle());
onShow(possiblePerspectiveToShow,layout);
this.handlePerspectiveReassignment(possiblePerspectiveToShow, perspectiveLayoutReplacementComponent);
}

Expand Down Expand Up @@ -180,7 +178,7 @@ public final void initComponent(final Message<Event, Object> message,
final String currentPerspectiveId = perspective.getContext().getId();
final String previousPerspectiveId = PerspectiveRegistry.getAndSetCurrentVisiblePerspective(perspective.getContext().getId());

this.updateToolbarButtons(currentPerspectiveId, previousPerspectiveId);
PerspectiveHandlerImpl.updateToolbarButtons(currentPerspectiveId, previousPerspectiveId);

this.log("3.4.4: perspective init subcomponents");
perspective.initComponents(message);
Expand All @@ -197,7 +195,7 @@ public final void initComponent(final Message<Event, Object> message,
* @param currentPerspectiveId
* @param previousPerspectiveId
*/
private void updateToolbarButtons(final String currentPerspectiveId, final String previousPerspectiveId) {
private static void updateToolbarButtons(final String currentPerspectiveId, final String previousPerspectiveId) {
if (previousPerspectiveId != null && !previousPerspectiveId.equals(currentPerspectiveId)) {
final Perspective<Node, EventHandler<Event>, Event, Object> previousPerspective = PerspectiveRegistry.findPerspectiveById(previousPerspectiveId);
// hide all buttons of the previous perspective
Expand Down Expand Up @@ -231,7 +229,7 @@ private void reassignSubcomponents(final Perspective<Node, EventHandler<Event>,
if (subComp instanceof EmbeddedFXComponent && subComp.getContext().isActive()) {
final EmbeddedFXComponent subComponent = (EmbeddedFXComponent) subComp;
this.addComponentByType(subComponent, layout);
} // End outer if
}
});

}
Expand Down Expand Up @@ -260,7 +258,7 @@ private void addComponentByType(final UIComponent<Node, EventHandler<Event>, Eve
}


private void bringRootToFront(int index, final ObservableList<Node> children, final Node root) {
private static void bringRootToFront(int index, final ObservableList<Node> children, final Node root) {
if (index != 0) {
children.remove(index);
GridPane.setHgrow(root, Priority.ALWAYS);
Expand All @@ -269,7 +267,7 @@ private void bringRootToFront(int index, final ObservableList<Node> children, fi
}
}

private void addNewRoot(final ObservableList<Node> children, final Node root) {
private static void addNewRoot(final ObservableList<Node> children, final Node root) {
GridPane.setHgrow(root, Priority.ALWAYS);
GridPane.setVgrow(root, Priority.ALWAYS);
children.add(root);
Expand Down Expand Up @@ -307,21 +305,24 @@ private void runInCachedModeSpeed(final Node rootNode, final Runnable r) {
rootNode.setCacheHint(hint);
}

private void replaceRootNodes(final ObservableList<Node> children, final Node newComp) {
private static void replaceRootNodes(final ObservableList<Node> children, final Node newComp) {
children.setAll(newComp);
}

private void executeOnShow(final Perspective<Node, EventHandler<Event>, Event, Object> perspective, final Perspective<Node, EventHandler<Event>, Event, Object> previousPerspective) {
if (!perspective.equals(previousPerspective)) {
final Perspective<Node, EventHandler<Event>, Event, Object> Perspective = perspective;
final FXComponentLayout layout = new FXComponentLayout(this.getWorkbenchLayout(), null, perspective.getContext().getId());
final FXComponentLayout layout = Context.class.cast(perspective.getContext()).getComponentLayout();
// execute OnShow
FXUtil.invokeHandleMethodsByAnnotation(OnShow.class, perspective.getPerspective(), layout,
Perspective.getType().equals(UIType.DECLARATIVE) ? Perspective.getDocumentURL() : null, Perspective.getContext().getResourceBundle());
onShow(perspective, layout);

}
}

private void onShow(Perspective<Node, EventHandler<Event>, Event, Object> perspective, FXComponentLayout layout) {
FXUtil.invokeHandleMethodsByAnnotation(OnShow.class, perspective.getPerspective(), perspective.getIPerspectiveLayout(), layout,
perspective.getType().equals(UIType.DECLARATIVE) ? perspective.getDocumentURL() : null, perspective.getContext().getResourceBundle());
}

/**
* returns the previous visible perspective
*
Expand Down Expand Up @@ -359,38 +360,39 @@ private void handlePerspectiveInitMethod(final Message<Event, Object> action,
if (FXUtil.getTargetPerspectiveId(action.getTargetId()).equals(perspective.getContext().getId())) {
this.log("3.4.3.1: perspective handle with custom message");
perspective.handlePerspective(action);
} // End if
else {
} else {
this.log("3.4.3.1: perspective handle with default >>init<< message");
perspective.handlePerspective(new MessageImpl(perspective.getContext().getId(), perspective.getContext().getId(), "init", null));
} // End else
}
}

private void handlePerspective(final Perspective<Node, EventHandler<Event>, Event, Object> perspective) {
if (perspective instanceof Perspective) {
final InternalContext context = InternalContext.class.cast(perspective.getContext());
initFXComponentLayout(context, perspective.getContext().getId());

final Perspective<Node, EventHandler<Event>, Event, Object> Perspective = perspective;

handleUIPerspective(perspective, Perspective, Context.class.cast(perspective.getContext()).getComponentLayout());
handleUIPerspective(perspective, Context.class.cast(perspective.getContext()).getComponentLayout());

perspective.postInit(new ComponentHandlerImpl(this.launcher, Perspective.getIPerspectiveLayout(), perspective
perspective.postInit(new ComponentHandlerImpl(this.launcher, perspective.getIPerspectiveLayout(), perspective
.getComponentDelegateQueue()));
}
if(perspective.isLast()) {
// execute OnShow
final FXComponentLayout layout = Context.class.cast(perspective.getContext()).getComponentLayout();
onShow(perspective, layout);
}
}

private void handleUIPerspective(final Perspective<Node, EventHandler<Event>, Event, Object> perspective,
final Perspective<Node, EventHandler<Event>, Event, Object> Perspective,
final FXComponentLayout layout) {
final AFXPerspective perspectiveImpl = AFXPerspective.class.cast(perspective);
if (Perspective.getType().equals(UIType.DECLARATIVE)) {
handleDeclarativePerspective(perspectiveImpl, Perspective);
if (perspective.getType().equals(UIType.DECLARATIVE)) {
handleDeclarativePerspective(perspectiveImpl, perspective);
} else {
handleDefaultPerspective(perspectiveImpl);
}
FXUtil.invokeHandleMethodsByAnnotation(PostConstruct.class,
perspective.getPerspective(), perspectiveImpl.getIPerspectiveLayout(), layout, perspective.getContext().getResourceBundle());

}

private void initFXComponentLayout(final InternalContext context, final String id) {
Expand Down Expand Up @@ -458,9 +460,9 @@ private void hideChildren(final ObservableList<Node> children) {
private void hideChildrenAndExecuteOnHide(final Perspective<Node, EventHandler<Event>, Event, Object> perspective, final Perspective<Node, EventHandler<Event>, Event, Object> previousPerspective, final ObservableList<Node> children) {
// hideChildren(children);
if (previousPerspective != null && !previousPerspective.equals(perspective)) {
final FXComponentLayout layout = new FXComponentLayout(this.getWorkbenchLayout(), null, previousPerspective.getContext().getId());
FXUtil.invokeHandleMethodsByAnnotation(OnHide.class, previousPerspective.getPerspective(), layout,
perspective.getType().equals(UIType.DECLARATIVE) ? perspective.getDocumentURL() : null, perspective.getContext().getResourceBundle());
final FXComponentLayout layout = Context.class.cast(previousPerspective.getContext()).getComponentLayout();
FXUtil.invokeHandleMethodsByAnnotation(OnHide.class, previousPerspective.getPerspective(),previousPerspective.getIPerspectiveLayout(), layout,
previousPerspective.getType().equals(UIType.DECLARATIVE) ? previousPerspective.getDocumentURL() : null, previousPerspective.getContext().getResourceBundle());

}

Expand Down
Expand Up @@ -56,6 +56,7 @@
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TransferQueue;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand Down Expand Up @@ -88,6 +89,9 @@ public abstract class AFXPerspective implements

protected Injectable perspective;

private AtomicInteger pos =new AtomicInteger(-1);
private AtomicInteger of = new AtomicInteger(0);


/**
* {@inheritDoc}
Expand Down Expand Up @@ -380,6 +384,23 @@ public final int compareTo(Component o) {
return this.context.getId().compareTo(o.getContext().getId());
}

@Override
/**
* {@inheritDoc}
*/
public void updatePositions(int pos, int of){
this.pos = new AtomicInteger(pos);
this.of = new AtomicInteger(of);
}

@Override
/**
* {@inheritDoc}
*/
public boolean isLast(){
return this.pos.get()==this.of.get();
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand Down
6 changes: 4 additions & 2 deletions JACP.JavaFX/src/main/java/org/jacpfx/rcp/util/FXUtil.java
Expand Up @@ -143,6 +143,7 @@ public static void invokeHandleMethodsByAnnotation(
final Object... value) {
final Class<?> componentClass = component.getClass();
final Method[] methods = componentClass.getMethods();
final Thread t = Thread.currentThread();
for (final Method m : methods) {
if (m.isAnnotationPresent(annotation)) {
try {
Expand All @@ -166,6 +167,8 @@ public static void invokeHandleMethodsByAnnotation(
} catch (final IllegalAccessException | InvocationTargetException e) {
Logger.getLogger(FXUtil.class.getName()).log(Level.SEVERE,
null, e);
if(e.getCause()!=null)
t.getUncaughtExceptionHandler().uncaughtException(t, e.getCause());
}
break;
}
Expand Down Expand Up @@ -429,8 +432,7 @@ public static <P extends Component<EventHandler<Event>, Object>> List<P> getObse
public static <P extends Component<EventHandler<Event>, Object>> P getObserveableByQualifiedId(
final String qualifiedId, final List<P> components) {
final Optional<P> filter = components.stream().
filter(c->c.getContext().getFullyQualifiedId()!=null).
filter(comp -> comp.getContext().getFullyQualifiedId().equals(qualifiedId)).
filter(comp -> comp.getContext().getFullyQualifiedId()!=null?comp.getContext().getFullyQualifiedId().equals(qualifiedId):false).
findFirst();
if (filter.isPresent()) return filter.get();
return null;
Expand Down
Expand Up @@ -54,10 +54,11 @@
import org.jacpfx.rcp.util.*;

import java.util.List;
import java.util.Optional;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;

/**
* represents the basic JavaFX workbench instance; handles perspective and
Expand Down Expand Up @@ -169,19 +170,19 @@ private Workbench getWorkbenchAnnotation() {
* {@inheritDoc}
*/
public final void initComponents(final Message<Event, Object> action) {
perspectives.forEach(this::initPerspective);
final List<Perspective<Node, EventHandler<Event>, Event, Object>> activeSequentialPerspectiveList = perspectives
.stream()
.filter(p -> p.getContext() != null && p.getContext().isActive())
.collect(Collectors.toList());
if (!activeSequentialPerspectiveList.isEmpty()) {
GlobalMediator.getInstance().handleToolBarButtons(activeSequentialPerspectiveList.get(activeSequentialPerspectiveList.size() - 1), true);
}
final AtomicInteger counter = new AtomicInteger(0);
final AtomicInteger of = new AtomicInteger(perspectives.size());
perspectives.stream().peek(this::registerComponent).peek(persp->{
if(!persp.getContext().isActive())
of.decrementAndGet();
}).peek(p-> p.updatePositions(counter.incrementAndGet(),of.get())).forEach(this::initPerspective);
final Optional<Perspective<Node, EventHandler<Event>, Event, Object>> lastPerspectiveToShow = perspectives.stream().filter(p -> p.isLast()).findFirst();
lastPerspectiveToShow.ifPresent(p-> GlobalMediator.getInstance().handleToolBarButtons(p,true));


}

private void initPerspective(Perspective<Node, EventHandler<Event>, Event, Object> perspective) {
registerComponent(perspective);
log("3.4.1: register component: " + perspective.getContext().getName());
final CountDownLatch waitForInit = new CountDownLatch(1);
log("3.4.2: init perspective");
Expand Down

0 comments on commit f2d92f7

Please sign in to comment.