I cannot seem to find any documentation of what events fire and when in GXT.
The API docs have lists of all the events that could fire (in Events). And it describes how to handle events that you catch. But I'm interested in the opposite side, which events are fired when I take a certain action.
I can set some listeners for various different components, or I can use addListener with a specific event code to catch individual events. That's spotty, and I seem to be using trial-and-error to guess what I might want to catch.
Is there a way to log all the events that are firing? Or catch all of them so I could look at them in a debugger?
Or is there some documentation I am missing that has the information? Something along the lines of "when you click on a widget, a ButtonEvent is fired. Events.x is fired on the hover, Events.y on the click."
Maybe someone will find this useful, I've created utility class for seeing what kind of events are risen. The idea of course was proposed in accepted answer.
import java.util.HashMap;
import java.util.Map;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.EventType;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.widget.Component;
/**
* Class for debugging purposes. Sometimes it is hard to tell what type of event
* is invoked and when. During debug process you can just do:
*
* EventUtils.attachDebugListeners(c);
* EventUtils.attachDebugListeners(c, "NAME");
*
* You'll then get information about events as they are invoked.
*
* List of events copied from {#link Events} class.
*
*/
public class EventUtils {
public static void attachDebugListeners(final Component c) {
attachDebugListeners(c, null);
}
public static void attachDebugListeners(final Component c, final String msg) {
for (final EventType type : eventTypeNames.keySet()) {
c.addListener(type, new Listener<BaseEvent>() {
#Override
public void handleEvent(BaseEvent be) {
String typeName = eventTypeNames.get(type);
if (msg != null)
System.out.print(msg + " -> ");
System.out.println(typeName);
}
});
}
}
final static Map<EventType, String> eventTypeNames = new HashMap<EventType, String>();
static {
eventTypeNames.put(Events.Activate, "Events.Activate");
eventTypeNames.put(Events.Add, "Events.Add");
eventTypeNames.put(Events.Adopt, "Events.Adopt");
eventTypeNames.put(Events.AfterEdit, "Events.AfterEdit");
eventTypeNames.put(Events.AfterLayout, "Events.AfterLayout");
eventTypeNames.put(Events.ArrowClick, "Events.ArrowClick");
eventTypeNames.put(Events.Attach, "Events.Attach");
eventTypeNames.put(Events.AutoHide, "Events.AutoHide");
eventTypeNames.put(Events.BeforeAdd, "Events.BeforeAdd");
eventTypeNames.put(Events.BeforeAdopt, "Events.BeforeAdopt");
eventTypeNames.put(Events.BeforeBind, "Events.BeforeBind");
eventTypeNames.put(Events.BeforeCancelEdit, "Events.BeforeCancelEdit");
eventTypeNames.put(Events.BeforeChange, "Events.BeforeChange");
eventTypeNames
.put(Events.BeforeCheckChange, "Events.BeforeCheckChange");
eventTypeNames.put(Events.BeforeClose, "Events.BeforeClose");
eventTypeNames.put(Events.BeforeCollapse, "Events.BeforeCollapse");
eventTypeNames.put(Events.BeforeComplete, "Events.BeforeComplete");
eventTypeNames.put(Events.BeforeEdit, "Events.BeforeEdit");
eventTypeNames.put(Events.BeforeExpand, "Events.BeforeExpand");
eventTypeNames.put(Events.BeforeHide, "Events.BeforeHide");
eventTypeNames.put(Events.BeforeLayout, "Events.BeforeLayout");
eventTypeNames.put(Events.BeforeOpen, "Events.BeforeOpen");
eventTypeNames.put(Events.BeforeOrphan, "Events.BeforeOrphan");
eventTypeNames.put(Events.BeforeQuery, "Events.BeforeQuery");
eventTypeNames.put(Events.BeforeRemove, "Events.BeforeRemove");
eventTypeNames.put(Events.BeforeRender, "Events.BeforeRender");
eventTypeNames.put(Events.BeforeSelect, "Events.BeforeSelect");
eventTypeNames.put(Events.BeforeShow, "Events.BeforeShow");
eventTypeNames.put(Events.BeforeStartEdit, "Events.BeforeStartEdit");
eventTypeNames.put(Events.BeforeStateRestore,
"Events.BeforeStateRestore");
eventTypeNames.put(Events.BeforeStateSave, "Events.BeforeStateSave");
eventTypeNames.put(Events.BeforeSubmit, "Events.BeforeSubmit");
eventTypeNames.put(Events.Bind, "Events.Bind");
eventTypeNames.put(Events.Blur, "Events.Blur");
eventTypeNames.put(Events.BodyScroll, "Events.BodyScroll");
eventTypeNames.put(Events.BrowserEvent, "Events.BrowserEvent");
eventTypeNames.put(Events.CancelEdit, "Events.CancelEdit");
eventTypeNames.put(Events.CellClick, "Events.CellClick");
eventTypeNames.put(Events.CellDoubleClick, "Events.CellDoubleClick");
eventTypeNames.put(Events.CellMouseDown, "Events.CellMouseDown");
eventTypeNames.put(Events.CellMouseUp, "Events.CellMouseUp");
eventTypeNames.put(Events.Change, "Events.Change");
eventTypeNames.put(Events.CheckChange, "Events.CheckChange");
eventTypeNames.put(Events.CheckChanged, "Events.CheckChanged");
eventTypeNames.put(Events.Clear, "Events.Clear");
eventTypeNames.put(Events.Close, "Events.Close");
eventTypeNames.put(Events.Collapse, "Events.Collapse");
eventTypeNames.put(Events.ColumnClick, "Events.ColumnClick");
eventTypeNames.put(Events.ColumnResize, "Events.ColumnResize");
eventTypeNames.put(Events.Complete, "Events.Complete");
eventTypeNames.put(Events.ContextMenu, "Events.ContextMenu");
eventTypeNames.put(Events.Deactivate, "Events.Deactivate");
eventTypeNames.put(Events.Detach, "Events.Detach");
eventTypeNames.put(Events.Disable, "Events.Disable");
eventTypeNames.put(Events.DoubleClick, "Events.DoubleClick");
eventTypeNames.put(Events.DragCancel, "Events.DragCancel");
eventTypeNames.put(Events.DragEnd, "Events.DragEnd");
eventTypeNames.put(Events.DragEnter, "Events.DragEnter");
eventTypeNames.put(Events.DragFail, "Events.DragFail");
eventTypeNames.put(Events.DragLeave, "Events.DragLeave");
eventTypeNames.put(Events.DragMove, "Events.DragMove");
eventTypeNames.put(Events.DragStart, "Events.DragStart");
eventTypeNames.put(Events.Drop, "Events.Drop");
eventTypeNames.put(Events.EffectCancel, "Events.EffectCancel");
eventTypeNames.put(Events.EffectComplete, "Events.EffectComplete");
eventTypeNames.put(Events.EffectStart, "Events.EffectStart");
eventTypeNames.put(Events.Enable, "Events.Enable");
eventTypeNames.put(Events.Exception, "Events.Exception");
eventTypeNames.put(Events.Expand, "Events.Expand");
eventTypeNames.put(Events.Focus, "Events.Focus");
eventTypeNames.put(Events.HeaderChange, "Events.HeaderChange");
eventTypeNames.put(Events.HeaderClick, "Events.HeaderClick");
eventTypeNames
.put(Events.HeaderContextMenu, "Events.HeaderContextMenu");
eventTypeNames
.put(Events.HeaderDoubleClick, "Events.HeaderDoubleClick");
eventTypeNames.put(Events.HeaderMouseDown, "Events.HeaderMouseDown");
eventTypeNames.put(Events.HiddenChange, "Events.HiddenChange");
eventTypeNames.put(Events.Hide, "Events.Hide");
eventTypeNames.put(Events.Invalid, "Events.Invalid");
eventTypeNames.put(Events.KeyDown, "Events.KeyDown");
eventTypeNames.put(Events.KeyPress, "Events.KeyPress");
eventTypeNames.put(Events.KeyUp, "Events.KeyUp");
eventTypeNames.put(Events.LiveGridViewUpdate,
"Events.LiveGridViewUpdate");
eventTypeNames.put(Events.Maximize, "Events.Maximize");
eventTypeNames.put(Events.MenuHide, "Events.MenuHide");
eventTypeNames.put(Events.MenuShow, "Events.MenuShow");
eventTypeNames.put(Events.Minimize, "Events.Minimize");
eventTypeNames.put(Events.Move, "Events.Move");
eventTypeNames.put(Events.OnBlur, "Events.OnBlur");
eventTypeNames.put(Events.OnChange, "Events.OnChange");
eventTypeNames.put(Events.OnClick, "Events.OnClick");
eventTypeNames.put(Events.OnContextMenu, "Events.OnContextMenu");
eventTypeNames.put(Events.OnDoubleClick, "Events.OnDoubleClick");
eventTypeNames.put(Events.OnError, "Events.OnError");
eventTypeNames.put(Events.OnFocus, "Events.OnFocus");
eventTypeNames.put(Events.OnKeyDown, "Events.OnKeyDown");
eventTypeNames.put(Events.OnKeyPress, "Events.OnKeyPress");
eventTypeNames.put(Events.OnKeyUp, "Events.OnKeyUp");
eventTypeNames.put(Events.OnLoad, "Events.OnLoad");
eventTypeNames.put(Events.OnLoseCapture, "Events.OnLoseCapture");
eventTypeNames.put(Events.OnMouseDown, "Events.OnMouseDown");
eventTypeNames.put(Events.OnMouseMove, "Events.OnMouseMove");
eventTypeNames.put(Events.OnMouseOut, "Events.OnMouseOut");
eventTypeNames.put(Events.OnMouseOver, "Events.OnMouseOver");
eventTypeNames.put(Events.OnMouseUp, "Events.OnMouseUp");
eventTypeNames.put(Events.OnMouseWheel, "Events.OnMouseWheel");
eventTypeNames.put(Events.OnScroll, "Events.OnScroll");
eventTypeNames.put(Events.Open, "Events.Open");
eventTypeNames.put(Events.Orphan, "Events.Orphan");
eventTypeNames.put(Events.Ready, "Events.Ready");
eventTypeNames.put(Events.Refresh, "Events.Refresh");
eventTypeNames.put(Events.Register, "Events.Register");
eventTypeNames.put(Events.Remove, "Events.Remove");
eventTypeNames.put(Events.Render, "Events.Render");
eventTypeNames.put(Events.Resize, "Events.Resize");
eventTypeNames.put(Events.ResizeEnd, "Events.ResizeEnd");
eventTypeNames.put(Events.ResizeStart, "Events.ResizeStart");
eventTypeNames.put(Events.Restore, "Events.Restore");
eventTypeNames.put(Events.RowClick, "Events.RowClick");
eventTypeNames.put(Events.RowDoubleClick, "Events.RowDoubleClick");
eventTypeNames.put(Events.RowMouseDown, "Events.RowMouseDown");
eventTypeNames.put(Events.RowMouseUp, "Events.RowMouseUp");
eventTypeNames.put(Events.RowUpdated, "Events.RowUpdated");
eventTypeNames.put(Events.Scroll, "Events.Scroll");
eventTypeNames.put(Events.Select, "Events.Select");
eventTypeNames.put(Events.SelectionChange, "Events.SelectionChange");
eventTypeNames.put(Events.Show, "Events.Show");
eventTypeNames.put(Events.SortChange, "Events.SortChange");
eventTypeNames.put(Events.SpecialKey, "Events.SpecialKey");
eventTypeNames.put(Events.StartEdit, "Events.StartEdit");
eventTypeNames.put(Events.StateChange, "Events.StateChange");
eventTypeNames.put(Events.StateRestore, "Events.StateRestore");
eventTypeNames.put(Events.StateSave, "Events.StateSave");
eventTypeNames.put(Events.Submit, "Events.Submit");
eventTypeNames.put(Events.Toggle, "Events.Toggle");
eventTypeNames.put(Events.TriggerClick, "Events.TriggerClick");
eventTypeNames.put(Events.TwinTriggerClick, "Events.TwinTriggerClick");
eventTypeNames.put(Events.UnBind, "Events.UnBind");
eventTypeNames.put(Events.Unregister, "Events.Unregister");
eventTypeNames.put(Events.Update, "Events.Update");
eventTypeNames.put(Events.Valid, "Events.Valid");
eventTypeNames.put(Events.ValidateDrop, "Events.ValidateDrop");
eventTypeNames.put(Events.ValidateEdit, "Events.ValidateEdit");
eventTypeNames.put(Events.ViewReady, "Events.ViewReady");
}
}
I ended up using brute force: Created a Map of EventType and name, then attach a Listener for each type of event that the component could receive. Then I just set a breakpoint inside the Listener, and I could see what events were received when anything happened.
If it hadn't been throwaway code, I would have cleaned it up into a utility class, not used an anonymous Listener class, etc.
final Map<EventType, String> eventTypeNames = new HashMap<EventType, String>();
eventTypeNames.put(Events.BeforeExpand, "BeforeExpand");
eventTypeNames.put(Events.Expand, "Expand");
...
eventTypeNames.put(Events.BeforeStateSave, "BeforeStateSave");
eventTypeNames.put(Events.StateSave, "StateSave");
for (EventType eventType : Arrays.asList(
Events.BeforeExpand,
Events.Expand,
...
Events.BeforeStateSave,
Events.StateSave
)) {
this.addListener(eventType, new Listener<BaseEvent>() {
public void handleEvent(final BaseEvent be) {
String type = eventTypeNames.get(be.getType());
String ev = be.toString();
}
});
}
The API docs for the various widgets describe what events will fire and when they will fire. For an example, let's say we wanted take an action any time a user chooses a new TabItem in a TabPanel.
TabPanel's API documentation (located at http://extjs.com/deploy/gxtdocs/com/extjs/gxt/ui/client/widget/TabPanel.html) shows several events; we're interested in Select:
Select : TabPanelEvent(container, item)
Fires after a item is selected.
container : this
item : the item that was selected
So, to capture the event (which it appears you understand, but I will include for completeness' sake) the process is to add a listener to the TabPanel, watching specifically for the Events.Select event:
tp.addListener(Events.Select, new Listener<TabPanelEvent>(){
public void handleEvent(TabPanelEvent be)
{
MessageBox.alert("Test", be.item.getText(), null);
}
});
Note that many events have a property called doit which you may set to false to cancel the event.
A complete code listing:
package edu.fresno.client;
import com.extjs.gxt.ui.client.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.TabPanelEvent;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.TabItem;
import com.extjs.gxt.ui.client.widget.TabPanel;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.RootPanel;
public class GWTSandbox implements EntryPoint {
public void onModuleLoad() {
TabPanel tp = new TabPanel();
TabItem ti1 = new TabItem("TabItem1");
TabItem ti2 = new TabItem("TabItem2");
tp.add(ti1);
tp.add(ti2);
tp.addListener(Events.Select, new Listener<TabPanelEvent>(){
public void handleEvent(TabPanelEvent be)
{
MessageBox.alert("Test", be.item.getText(), null);
}
});
ContentPanel panel = new ContentPanel();
panel.setLayout(new FitLayout());
panel.add(tp);
RootPanel.get().add(panel);
}
}
You could add following code for the constructor:
ContentPanel panel =new ContentPanel(){
public boolean fireEvent(EventType type) {
System.out.println(type.getEventCode());
return super.fireEvent(type);
}
public boolean fireEvent(EventType eventType, BaseEvent be) {
System.out.println(eventType.getEventCode());
return super.fireEvent(eventType, be);
}
public boolean fireEvent(EventType type, ComponentEvent ce) {
System.out.println(type.getEventCode());
return super.fireEvent(type, ce);
}
};
then it will print any event this component can receive.
Related
I am solving a tough problem in OptaPlanner. The best algorithm I found so far is to use a custom move factory, a computationally intensive one. After noticing that I was utilising a single CPU core, I discovered that OptaPlanner only spreads on multiple threads the score calculation, while it performs the move generation in a single thread.
To mitigate the problem, I implemented the multi-threading in my move factory via the following abstract class, which I then extend with the actual logic (I did this because I actually have three computationally expensive custom move factories):
package my.solver.move;
import lombok.AllArgsConstructor;
import lombok.NonNull;
import org.optaplanner.core.impl.domain.solution.descriptor.SolutionDescriptor;
import org.optaplanner.core.impl.heuristic.move.CompositeMove;
import org.optaplanner.core.impl.heuristic.move.Move;
import org.optaplanner.core.impl.heuristic.selector.move.factory.MoveIteratorFactory;
import org.optaplanner.core.impl.score.director.ScoreDirector;
import java.util.Iterator;
import java.util.Objects;
import java.util.Random;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Function;
public abstract class MultiThreadedMoveFactory<T> implements MoveIteratorFactory<T> {
private final ThreadPoolExecutor threadPoolExecutor;
public MultiThreadedMoveFactory(
#NonNull String threadPrefix
) {
int availableProcessorCount = Runtime.getRuntime().availableProcessors();
int resolvedThreadCount = Math.max(1, availableProcessorCount);
ThreadFactory threadFactory = new SolverThreadFactory(threadPrefix);
threadPoolExecutor = (ThreadPoolExecutor) Executors.newFixedThreadPool(resolvedThreadCount, threadFactory);
}
#AllArgsConstructor
public class MoveGeneratorData {
T solution;
SolutionDescriptor<T> solutionDescriptor;
Random random;
BlockingQueue<Move<T>> generatedMoves;
}
protected abstract int getNumMoves();
#Override
public long getSize(ScoreDirector<T> scoreDirector) {
return getNumMoves();
}
protected class MovesIterator implements Iterator<Move<T>> {
private final BlockingQueue<Move<T>> generatedMoves = new ArrayBlockingQueue<>(getNumMoves());
public MovesIterator(
#NonNull T solution,
#NonNull SolutionDescriptor<T> solutionDescriptor,
#NonNull Random random,
#NonNull Function<MoveGeneratorData, Runnable> moveGeneratorFactory
) {
MoveGeneratorData moveGeneratorData = new MoveGeneratorData(solution, solutionDescriptor, random, generatedMoves);
for (int i = 0; i < getNumMoves(); i++) {
threadPoolExecutor.submit(moveGeneratorFactory.apply(moveGeneratorData));
}
}
#Override
public boolean hasNext() {
if (!generatedMoves.isEmpty()) {
return true;
}
while (threadPoolExecutor.getActiveCount() > 0) {
try {
//noinspection BusyWait
Thread.sleep(50);
} catch (InterruptedException e) {
return false;
}
}
return !generatedMoves.isEmpty();
}
#Override
public Move<T> next() {
//noinspection unchecked
return Objects.requireNonNullElseGet(generatedMoves.poll(), CompositeMove::new);
}
}
private static final AtomicInteger poolNumber = new AtomicInteger(1);
private static class SolverThreadFactory implements ThreadFactory {
private final ThreadGroup group;
private final AtomicInteger threadNumber = new AtomicInteger(1);
private final String namePrefix;
public SolverThreadFactory(String threadPrefix) {
SecurityManager s = System.getSecurityManager();
group = (s != null) ? s.getThreadGroup() : Thread.currentThread().getThreadGroup();
namePrefix = "MyPool-" + poolNumber.getAndIncrement() + "-" + threadPrefix + "-";
}
#Override
public Thread newThread(#NonNull Runnable r) {
Thread t = new Thread(group, r, namePrefix + threadNumber.getAndIncrement(), 0);
if (t.isDaemon()) {
t.setDaemon(false);
}
if (t.getPriority() != Thread.NORM_PRIORITY) {
t.setPriority(Thread.NORM_PRIORITY);
}
return t;
}
}
#Override
public Iterator<? extends Move<T>> createOriginalMoveIterator(ScoreDirector<T> scoreDirector) {
return createMoveIterator(scoreDirector, new Random());
}
#Override
public Iterator<? extends Move<T>> createRandomMoveIterator(ScoreDirector<T> scoreDirector, Random workingRandom) {
return createMoveIterator(scoreDirector, workingRandom);
}
public abstract Iterator<? extends Move<T>> createMoveIterator(ScoreDirector<T> scoreDirector, Random random);
}
However, the solver seems to hang after a while. The debugger tells me that it's waiting on an innerQueue.take() in OrderByMoveIndexBlockingQueue. This is caused by my move factory: if I revert the above and only use the previous implementation, which was single-threaded, the problem goes away.
I do not quite understand where the problem is, so the question is: how can I fix it?
No, no, no. This approach is doomed. I think. (Prove me wrong.)
JIT selection
First learn about Just In Time selection (see docs) of moves.
Instead of generating all moves (which can be billions) at the beginning of each step, only generate those that will actually be evaluated. Most LS algorithms will only evaluate a few moves per step.
Watch the TRACE log to see how many milliseconds it takes to start a step. Typically you want to do evaluate 10000 moves per second, so it should take 0 or 1 milliseconds to start a step (the log only shows in milliseconds).
Multithreaded solving
Then learn about moveThreadCount to enable multithreaded solving. See this blog post. Know that this still does the move selection on 1 thread, for reproducibility reasons. But the move evaluation is spread across threads.
Caching for move selection
But your custom moves are smart, so the move selection must be smart?
First determine what "solution state" query information you need to generate the moves - for example a Map<Employee, List<Shift>> - then cache that:
either calculate that map at the beginning of each step, if it doesn't take too long (but this won't scale because it doesn't do deltas)
or use a shadow variable (#InverseRelationShadowVariable works fine in this case), because these are updated through deltas. But it does do the delta's for every move and undo move too...
Or hack in an actual new MoveSelector, which can listen to stepEnded() events and actually apply the delta of the last step on that Map, without doing any of the deltas of every move and undo move. We should probably standardize this approach and make it part of our public API some day.
I was able to make the factory work by removing any trace of JIT-ing from hasNext: block the method until all moves have been generated, and only then return true, and keep returning true until all moves have been consumed.
#Override
public boolean hasNext() {
while (!generationComplete && generatedMoves.size() < getNumMoves()) {
try {
// We get a warning because the event we are waiting for could happen earlier than the end of sleep
// and that means we would be wasting time, but that is negligible so we silence it
//noinspection BusyWait
Thread.sleep(50);
} catch (InterruptedException e) {
return false;
}
}
generationComplete = true;
return !generatedMoves.isEmpty();
}
To the best of my understanding, the solution I am using not only works, but it is the best I found in a few months of iterations.
I'm new guy here :)
I have a small problem which concerns binding in JavaFX. I have created Task which is working as a clock and returns value which has to be set in a special label (label_Time). This label presents how many seconds left for player's answer in quiz.
The problem is how to automatically change value in label using the timer task? I tried to link value from timer Task (seconds) to label_Time value in such a way...
label_Time.textProperty().bind(timer.getSeconds());
...but it doesn't work. Is it any way to do this thing?
Thanks in advance for your answer! :)
Initialize method in Controller class:
public void initialize(URL url, ResourceBundle rb) {
Timer2 timer = new Timer2();
label_Time.textProperty().bind(timer.getSeconds());
new Thread(timer).start();
}
Task class "Timer2":
public class Timer2 extends Task{
private static final int SLEEP_TIME = 1000;
private static int sec;
private StringProperty seconds;
public Timer2(){
Timer2.sec = 180;
this.seconds = new SimpleStringProperty("180");
}
#Override protected StringProperty call() throws Exception {
int iterations;
for (iterations = 0; iterations < 1000; iterations++) {
if (isCancelled()) {
updateMessage("Cancelled");
break;
}
System.out.println("TIK! " + sec);
seconds.setValue(String.valueOf(sec));
System.out.println("TAK! " + seconds.getValue());
// From the counter we subtract one second
sec--;
//Block the thread for a short time, but be sure
//to check the InterruptedException for cancellation
try {
Thread.sleep(10);
} catch (InterruptedException interrupted) {
if (isCancelled()) {
updateMessage("Cancelled");
break;
}
}
}
return seconds;
}
public StringProperty getSeconds(){
return this.seconds;
}
}
Why your app does not work
What is happening is that you run the task on it's own thread, set the seconds property in the task, then the binding triggers an immediate update of the label text while still on the task thread.
This violates a rule for JavaFX thread processing:
An application must attach nodes to a Scene, and modify nodes that are already attached to a Scene, on the JavaFX Application Thread.
This is the reason that your originally posted program does not work.
How to fix it
To modify your original program so that it will work, wrap the modification of the property in the task inside a Platform.runLater construct:
Platform.runLater(new Runnable() {
#Override public void run() {
System.out.println("TIK! " + sec);
seconds.setValue(String.valueOf(sec));
System.out.println("TAK! " + seconds.getValue());
}
});
This ensures that when you write out to the property, you are already on the JavaFX application thread, so that when the subsequent change fires for the bound label text, that change will also occur on the JavaFX application thread.
On Property Naming Conventions
It is true that the program does not correspond to JavaFX bean conventions as Matthew points out. Conforming to those conventions is both useful in making the program more readily understandable and also for making use of things like the PropertyValueFactory which reflect on property method names to allow table and list cells to automatically update their values as the underlying property is updated. However, for your example, not following JavaFX bean conventions does not explain why the program does not work.
Alternate Solution
Here is an alternate solution to your countdown binding problem which uses the JavaFX animation framework rather than the concurrency framework. I prefer this because it keeps everything on the JavaFX application thread and you don't need to worry about concurrency issues which are difficult to understand and debug.
import javafx.animation.*;
import javafx.application.Application;
import javafx.beans.*;
import javafx.beans.binding.Bindings;
import javafx.beans.property.*;
import javafx.event.*;
import javafx.geometry.Pos;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.Duration;
public class CountdownTimer extends Application {
#Override public void start(final Stage stage) throws Exception {
final CountDown countdown = new CountDown(10);
final CountDownLabel countdownLabel = new CountDownLabel(countdown);
final Button countdownButton = new Button(" Start ");
countdownButton.setOnAction(new EventHandler<ActionEvent>() {
#Override public void handle(ActionEvent t) {
countdownButton.setText("Restart");
countdown.start();
}
});
VBox layout = new VBox(10);
layout.getChildren().addAll(countdownLabel, countdownButton);
layout.setAlignment(Pos.BASELINE_RIGHT);
layout.setStyle("-fx-background-color: cornsilk; -fx-padding: 20; -fx-font-size: 20;");
stage.setScene(new Scene(layout));
stage.show();
}
public static void main(String[] args) throws Exception {
launch(args);
}
}
class CountDownLabel extends Label {
public CountDownLabel(final CountDown countdown) {
textProperty().bind(Bindings.format("%3d", countdown.timeLeftProperty()));
}
}
class CountDown {
private final ReadOnlyIntegerWrapper timeLeft;
private final ReadOnlyDoubleWrapper timeLeftDouble;
private final Timeline timeline;
public ReadOnlyIntegerProperty timeLeftProperty() {
return timeLeft.getReadOnlyProperty();
}
public CountDown(final int time) {
timeLeft = new ReadOnlyIntegerWrapper(time);
timeLeftDouble = new ReadOnlyDoubleWrapper(time);
timeline = new Timeline(
new KeyFrame(
Duration.ZERO,
new KeyValue(timeLeftDouble, time)
),
new KeyFrame(
Duration.seconds(time),
new KeyValue(timeLeftDouble, 0)
)
);
timeLeftDouble.addListener(new InvalidationListener() {
#Override public void invalidated(Observable o) {
timeLeft.set((int) Math.ceil(timeLeftDouble.get()));
}
});
}
public void start() {
timeline.playFromStart();
}
}
Update for additional questions on Task execution strategy
Is it possible to run more than one Task which includes a Platform.runLater(new Runnable()) method ?
Yes, you can use multiple tasks. Each task can be of the same type or a different type.
You can create a single thread and run each task on the thread sequentially, or you can create multiple threads and run the tasks in parallel.
For managing multiple tasks, you can create an overseer Task. Sometimes it is appropriate to use a Service for managing the multiple tasks and the Executors framework for managing multiple threads.
There is an example of a Task, Service, Executors co-ordination approach: Creating multiple parallel tasks by a single service In each task.
In each task you can place no runlater call, a single runlater call or multiple runlater calls.
So there is a great deal of flexibility available.
Or maybe I should create one general task which will be only take data from other Tasks and updating a UI?
Yes you can use a co-ordinating task approach like this if complexity warrants it. There is an example of such an approach in in Render 300 charts off screen and save them to files.
Your "Timer2" class doesn't conform to the JavaFX bean conventions:
public String getSeconds();
public void setSeconds(String seconds);
public StringProperty secondsProperty();
I'm stuck trying to figure out how to create a back command to the previous screen.The page I'm trying to return to does not have a form but a List but when I set the 'back' command listener to the list it just seems to throw a null pointer exception.
Here is my main class
import java.util.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
/**
*
*/
public class CalFrontEnd extends MIDlet implements CommandListener
{
private Display display;
protected List list = new List("Please Select a Option", List.IMPLICIT);
private Command select = new Command("Select", Command.SCREEN, 1);
private Command exit = new Command("Exit", Command.EXIT, 2);
private Command save = new Command("Save,", Command.SCREEN, 2);
private DateField calendar;
/**
*
*/
public CalFrontEnd()
{
display = Display.getDisplay(this);
list.append("Select Date", null);
list.append("Add Events", null);
list.append("Remove Events", null);
list.append("Browse Events", null);
list.addCommand(select);
list.addCommand(exit);
list.setCommandListener(this);
}
/**
* Start Application
*/
public void startApp()
{
display.setCurrent(list);
}
/**
* Pause Application Method
*/
public void pauseApp()
{}
/**
* Destroy Application Method
*/
public void destroyApp(boolean unconditional)
{}
/**
*
* #param command
* #param displayable
*/
public void commandAction(Command command, Displayable displayable)
{
if (displayable == list) {
if (command == List.SELECT_COMMAND) {
switch (list.getSelectedIndex()) {
case 0: // select Date
SelectDate myDate = new SelectDate(display);
myDate.BuildCalendar();
break;
case 1: //add Events
AddEvents myAEvents = new AddEvents(display);
myAEvents.BuildAddEvents();
break;
case 2: //Remove Events
RemoveEvents myREvents = new RemoveEvents(display);
myREvents.BuildRemoveEvents();
break;
case 3: //Browse Events
BrowseEvents myBEvents = new BrowseEvents(display);
myBEvents.BuildBrowseEvents();
break;
}
} else if (command == exit) {
destroyApp(false);
notifyDestroyed();
}
}
}
}
And this is the class which I'm trying to use the back button on
import java.util.*;
import javax.microedition.lcdui.*;
/**
*
*/
public class SelectDate extends CalFrontEnd implements CommandListener
{
private DateField calendar;
private Form form = new Form("Please Select a Date");
private Command select = new Command("Select", Command.SCREEN, 1);
private Command back = new Command("Back", Command.BACK, 2);
private Command save = new Command("Save,", Command.SCREEN, 2);
private Display display;
/**
*
*/
public SelectDate(Display display)
{
this.display = display;
}
/**
*
*/
public void BuildCalendar()
{
calendar = new DateField("Date In :", DateField.DATE, TimeZone.getTimeZone("GMT"));
form.append(calendar);
form.addCommand(back);
form.setCommandListener(this);
display.setCurrent(form);
}
/**
*
* #param command
* #param displayable
*/
public void commandAction(Command command, Display display)
{
if (command == back)
{
display.setCurrent(list);
}
}
}
Inappropriate use of inheritance has brought you into trouble here. Look, there is a list field in SelectDate class but it is not visible in code, because it is inherited from superclass (extends CalFrontEnd and protected List list are where all your trouble really begins).
When you create an instance of SelectDate (new SelectDate(display)) this field is initialized with null - and you never change it after that. It's hard for you to notice that because the very declaration of list is buried in other file in superclass. And, which makes things even harder, compiler can't help you here because the field is visible to it and it believes things are OK.
You know, this is only a beginning of your troubles related to overuse of inheritance. Further down the road, more headaches are waiting to happen. Think for example of what would happen if you accidentally remove or rename commandAction in SelectDate? Compiler will think it's all-right - just because superclass has this method, too. You'll only notice that things went wrong in some misterious way when you run the code and find out that commands at select date screen stop responding or began doing weird things. Actually it would be safer to hide CommandListener for both classes just to avoid this kind mistakes but that was discussed in another question.
I strongly recommend to wipe out extends CalFrontEnd from SelectDate. That will help compiler help you to find various logical issues in your code.
As for list to show by Back command, you can for example pass it as additional constructor parameter, like as below:
public class SelectDate implements CommandListener // drop "extends CalFrontEnd"
{
// ...
private Display display;
private List list; // add the field for the list
public SelectDate(Display display, List list) // add list as parameter
{
this.display = display;
this.list = list; // initialize field
}
// ... commandAction code will get the right "list" now
}
There are a number of problems regarding your code.
One gnat has already mentioned (Remove extends CalFrontEnd in SelectData class).
Secondly you are not calling select command in commandAction of your code (command you are calling is List.SELECT_COMMAND which is not select). So change if (command == List.SELECT_COMMAND) to if (command == select).
Thirdly documentation of commandAction in CommandListener declares its second parameter to be Displayable while you are declaring it with Display.
the error is that there's no variable called list, the solution however is to simply change the code under your back button from
display.setCurrent(list)
to
display.setCurrent(CalFrontEnd.list)
so i'm trying to set up an application where i have multiple panels inside a jframe. lets say 3 of them are purely for display purposes, and one of them is for control purposes. i'm using a borderLayout but i don't think the layout should really affect things here.
my problem is this: i want the repainting of the three display panels to be under the control of buttons in the control panel, and i want them to all execute in sync whenever a button on the control panel is pressed. to do this, i set up this little method :
public void update(){
while(ButtonIsOn){
a.repaint();
b.repaint()
c.repaint();
System.out.println("a,b, and c should have repainted");
}
}
where a,b, and c are all display panels and i want a,b,and c to all repaint continously until i press the button again. the problem is, when i execute the loop, the message prints in an infinite loop, but none of the panels do anything, ie, none of them repaint.
i've been reading up on the event dispatch thread and swing multithreading, but nothing i've found so far has really solved my problem. could someone give me the gist of what i'm doing wrong here, or even better, some sample code that handles the situation i'm describing? thanks...
The java.util.concurrent package provides very powerful tools for concurrent programing.
In the code below, I make use of a ReentrantLock (which works much like the Java synchronized keyword, ensuring mutually exclusive access by multiple threads to a single block of code). The other great thing which ReentrantLock provides are Conditions, which allow Threads to wait for a particular event before continuing.
Here, RepaintManager simply loops, calling repaint() on the JPanel. However, when toggleRepaintMode() is called, it blocks, waiting on the modeChanged Condition until toggleRepaintMode() is called again.
You should be able to run the following code right out of the box. Pressing the JButton toggle repainting of the JPanel (which you can see working by the System.out.println statements).
In general, I'd highly recommend getting familiar with the capabilities that java.util.concurrent offers. There's lots of very powerful stuff there. There's a good tutorial at http://docs.oracle.com/javase/tutorial/essential/concurrency/
import java.awt.Component;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Collection;
import java.util.Collections;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class RepaintTest {
public static void main(String[] args) {
JFrame frame = new JFrame();
JPanel panel = new JPanel()
{
#Override
public void paintComponent( Graphics g )
{
super.paintComponent( g );
// print something when the JPanel repaints
// so that we know things are working
System.out.println( "repainting" );
}
};
frame.add( panel );
final JButton button = new JButton("Button");
panel.add(button);
// create and start an instance of our custom
// RepaintThread, defined below
final RepaintThread thread = new RepaintThread( Collections.singletonList( panel ) );
thread.start();
// add an ActionListener to the JButton
// which turns on and off the RepaintThread
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
thread.toggleRepaintMode();
}
});
frame.setSize( 300, 300 );
frame.setVisible( true );
}
public static class RepaintThread extends Thread
{
ReentrantLock lock;
Condition modeChanged;
boolean repaintMode;
Collection<? extends Component> list;
public RepaintThread( Collection<? extends Component> list )
{
this.lock = new ReentrantLock( );
this.modeChanged = this.lock.newCondition();
this.repaintMode = false;
this.list = list;
}
#Override
public void run( )
{
while( true )
{
lock.lock();
try
{
// if repaintMode is false, wait until
// Condition.signal( ) is called
while ( !repaintMode )
try { modeChanged.await(); } catch (InterruptedException e) { }
}
finally
{
lock.unlock();
}
// call repaint on all the Components
// we're not on the event dispatch thread, but
// repaint() is safe to call from any thread
for ( Component c : list ) c.repaint();
// wait a bit
try { Thread.sleep( 50 ); } catch (InterruptedException e) { }
}
}
public void toggleRepaintMode( )
{
lock.lock();
try
{
// update the repaint mode and notify anyone
// awaiting on the Condition that repaintMode has changed
this.repaintMode = !this.repaintMode;
this.modeChanged.signalAll();
}
finally
{
lock.unlock();
}
}
}
}
jComponent.getTopLevelAncestor().repaint();
You could use SwingWorker for this. SwingWorker was designed to perform long running tasks in the background without blocking the event dispatcher thread. So, you need to extend SwingWorker and implement certain methods that will make sense to you. Note that all long running action should happen in the doInBackground() method, and the Swing UI elements should be updated only on the done() method.
So here is an example :
class JPanelTask extends SwingWorker<String, Object>{
JPanel panel = null;
Color bg = null;
public JPanelTask(JPanel panel){
this.panel = panel;
}
#Override
protected String doInBackground() throws Exception {
//loooong running computation.
return "COMPLETE";
}
#Override
protected void done() {
panel.repaint();
}
}
Now, in your "control" button's action performed event, you could do the following :
controlButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
JPanelTask task1 = new JPanelTask(panel1);
task1.execute();
JPanelTask task2 = new JPanelTask(panel2);
task2.execute();
//so on..
}
});
Another way is using javax.swing.Timer. Timer helps you to fire a change to your ui elements in a timely fasthion.This may not be the most appropriate solution. But it gets the work done too.
Again you should be careful about updating UI elements in right places.
Is anyone aware of a method to dynamically combine/minify all the h:outputStylesheet resources and then combine/minify all h:outputScript resources in the render phase? The comined/minified resource would probably need to be cached with a key based on the combined resource String or something to avoid excessive processing.
If this feature doesn't exist I'd like to work on it. Does anyone have ideas on the best way to implement something like this. A Servlet filter would work I suppose but the filter would have to do more work than necessary -- basically examining the whole rendered output and replacing matches. Implementing something in the render phase seems like it would work better as all of the static resources are available without having to parse the entire output.
Thanks for any suggestions!
Edit: To show that I'm not lazy and will really work on this with some guidance, here is a stub that captures Script Resources name/library and then removes them from the view. As you can see I have some questions about what to do next ... should I make http requests and get the resources to combine, then combine them and save them to the resource cache?
package com.davemaple.jsf.listener;
import java.util.ArrayList;
import java.util.List;
import javax.faces.component.UIComponent;
import javax.faces.component.UIOutput;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;
import javax.faces.event.AbortProcessingException;
import javax.faces.event.PhaseEvent;
import javax.faces.event.PhaseId;
import javax.faces.event.PhaseListener;
import javax.faces.event.PreRenderViewEvent;
import javax.faces.event.SystemEvent;
import javax.faces.event.SystemEventListener;
import org.apache.log4j.Logger;
/**
* A Listener that combines CSS/Javascript Resources
*
* #author David Maple<d#davemaple.com>
*
*/
public class ResourceComboListener implements PhaseListener, SystemEventListener {
private static final long serialVersionUID = -8430945481069344353L;
private static final Logger LOGGER = Logger.getLogger(ResourceComboListener.class);
#Override
public PhaseId getPhaseId() {
return PhaseId.RESTORE_VIEW;
}
/*
* (non-Javadoc)
* #see javax.faces.event.PhaseListener#beforePhase(javax.faces.event.PhaseEvent)
*/
public void afterPhase(PhaseEvent event) {
FacesContext.getCurrentInstance().getViewRoot().subscribeToViewEvent(PreRenderViewEvent.class, this);
}
/*
* (non-Javadoc)
* #see javax.faces.event.PhaseListener#afterPhase(javax.faces.event.PhaseEvent)
*/
public void beforePhase(PhaseEvent event) {
//nothing here
}
/*
* (non-Javadoc)
* #see javax.faces.event.SystemEventListener#isListenerForSource(java.lang.Object)
*/
public boolean isListenerForSource(Object source) {
return (source instanceof UIViewRoot);
}
/*
* (non-Javadoc)
* #see javax.faces.event.SystemEventListener#processEvent(javax.faces.event.SystemEvent)
*/
public void processEvent(SystemEvent event) throws AbortProcessingException {
FacesContext context = FacesContext.getCurrentInstance();
UIViewRoot viewRoot = context.getViewRoot();
List<UIComponent> scriptsToRemove = new ArrayList<UIComponent>();
if (!context.isPostback()) {
for (UIComponent component : viewRoot.getComponentResources(context, "head")) {
if (component.getClass().equals(UIOutput.class)) {
UIOutput uiOutput = (UIOutput) component;
if (uiOutput.getRendererType().equals("javax.faces.resource.Script")) {
String library = uiOutput.getAttributes().get("library").toString();
String name = uiOutput.getAttributes().get("name").toString();
// make https requests to get the resources?
// combine then and save to resource cache?
// insert new UIOutput script?
scriptsToRemove.add(component);
}
}
}
for (UIComponent component : scriptsToRemove) {
viewRoot.getComponentResources(context, "head").remove(component);
}
}
}
}
This answer doesn't cover minifying and compression. Minifying of individual CSS/JS resources is better to be delegated to build scripts like YUI Compressor Ant task. Manually doing it on every request is too expensive. Compression (I assume you mean GZIP?) is better to be delegated to the servlet container you're using. Manually doing it is overcomplicated. On Tomcat for example it's a matter of adding a compression="on" attribute to the <Connector> element in /conf/server.xml.
The SystemEventListener is already a good first step (apart from some PhaseListener unnecessity). Next, you'd need to implement a custom ResourceHandler and Resource. That part is not exactly trivial. You'd need to reinvent pretty a lot if you want to be JSF implementation independent.
First, in your SystemEventListener, you'd like to create new UIOutput component representing the combined resource so that you can add it using UIViewRoot#addComponentResource(). You need to set its library attribute to something unique which is understood by your custom resource handler. You need to store the combined resources in an application wide variable along an unique name based on the combination of the resources (a MD5 hash maybe?) and then set this key as name attribute of the component. Storing as an application wide variable has a caching advantage for both the server and the client.
Something like this:
String combinedResourceName = CombinedResourceInfo.createAndPutInCacheIfAbsent(resourceNames);
UIOutput component = new UIOutput();
component.setRendererType(rendererType);
component.getAttributes().put(ATTRIBUTE_RESOURCE_LIBRARY, CombinedResourceHandler.RESOURCE_LIBRARY);
component.getAttributes().put(ATTRIBUTE_RESOURCE_NAME, combinedResourceName + extension);
context.getViewRoot().addComponentResource(context, component, TARGET_HEAD);
Then, in your custom ResourceHandler implementation, you'd need to implement the createResource() method accordingly to create a custom Resource implementation whenever the library matches the desired value:
#Override
public Resource createResource(String resourceName, String libraryName) {
if (RESOURCE_LIBRARY.equals(libraryName)) {
return new CombinedResource(resourceName);
} else {
return super.createResource(resourceName, libraryName);
}
}
The constructor of the custom Resource implementation should grab the combined resource info based on the name:
public CombinedResource(String name) {
setResourceName(name);
setLibraryName(CombinedResourceHandler.RESOURCE_LIBRARY);
setContentType(FacesContext.getCurrentInstance().getExternalContext().getMimeType(name));
this.info = CombinedResourceInfo.getFromCache(name.split("\\.", 2)[0]);
}
This custom Resource implementation must provide a proper getRequestPath() method returning an URI which will then be included in the rendered <script> or <link> element:
#Override
public String getRequestPath() {
FacesContext context = FacesContext.getCurrentInstance();
String path = ResourceHandler.RESOURCE_IDENTIFIER + "/" + getResourceName();
String mapping = getFacesMapping();
path = isPrefixMapping(mapping) ? (mapping + path) : (path + mapping);
return context.getExternalContext().getRequestContextPath()
+ path + "?ln=" + CombinedResourceHandler.RESOURCE_LIBRARY;
}
Now, the HTML rendering part should be fine. It'll look something like this:
<link type="text/css" rel="stylesheet" href="/playground/javax.faces.resource/dd08b105bf94e3a2b6dbbdd3ac7fc3f5.css.xhtml?ln=combined.resource" />
<script type="text/javascript" src="/playground/javax.faces.resource/2886165007ccd8fb65771b75d865f720.js.xhtml?ln=combined.resource"></script>
Next, you have to intercept on combined resource requests made by the browser. That's the hardest part. First, in your custom ResourceHandler implementation, you need to implement the handleResourceRequest() method accordingly:
#Override
public void handleResourceRequest(FacesContext context) throws IOException {
if (RESOURCE_LIBRARY.equals(context.getExternalContext().getRequestParameterMap().get("ln"))) {
streamResource(context, new CombinedResource(getCombinedResourceName(context)));
} else {
super.handleResourceRequest(context);
}
}
Then you have to do the whole lot of work of implementing the other methods of the custom Resource implementation accordingly such as getResponseHeaders() which should return proper caching headers, getInputStream() which should return the InputStreams of the combined resources in a single InputStream and userAgentNeedsUpdate() which should respond properly on caching related requests.
#Override
public Map<String, String> getResponseHeaders() {
Map<String, String> responseHeaders = new HashMap<String, String>(3);
SimpleDateFormat sdf = new SimpleDateFormat(PATTERN_RFC1123_DATE, Locale.US);
sdf.setTimeZone(TIMEZONE_GMT);
responseHeaders.put(HEADER_LAST_MODIFIED, sdf.format(new Date(info.getLastModified())));
responseHeaders.put(HEADER_EXPIRES, sdf.format(new Date(System.currentTimeMillis() + info.getMaxAge())));
responseHeaders.put(HEADER_ETAG, String.format(FORMAT_ETAG, info.getContentLength(), info.getLastModified()));
return responseHeaders;
}
#Override
public InputStream getInputStream() throws IOException {
return new CombinedResourceInputStream(info.getResources());
}
#Override
public boolean userAgentNeedsUpdate(FacesContext context) {
String ifModifiedSince = context.getExternalContext().getRequestHeaderMap().get(HEADER_IF_MODIFIED_SINCE);
if (ifModifiedSince != null) {
SimpleDateFormat sdf = new SimpleDateFormat(PATTERN_RFC1123_DATE, Locale.US);
try {
info.reload();
return info.getLastModified() > sdf.parse(ifModifiedSince).getTime();
} catch (ParseException ignore) {
return true;
}
}
return true;
}
I've here a complete working proof of concept, but it's too much of code to post as a SO answer. The above was just a partial to help you in the right direction. I assume that the missing method/variable/constant declarations are self-explaining enough to write your own, otherwise let me know.
Update: as per the comments, here's how you can collect resources in CombinedResourceInfo:
private synchronized void loadResources(boolean forceReload) {
if (!forceReload && resources != null) {
return;
}
FacesContext context = FacesContext.getCurrentInstance();
ResourceHandler handler = context.getApplication().getResourceHandler();
resources = new LinkedHashSet<Resource>();
contentLength = 0;
lastModified = 0;
for (Entry<String, Set<String>> entry : resourceNames.entrySet()) {
String libraryName = entry.getKey();
for (String resourceName : entry.getValue()) {
Resource resource = handler.createResource(resourceName, libraryName);
resources.add(resource);
try {
URLConnection connection = resource.getURL().openConnection();
contentLength += connection.getContentLength();
long lastModified = connection.getLastModified();
if (lastModified > this.lastModified) {
this.lastModified = lastModified;
}
} catch (IOException ignore) {
// Can't and shouldn't handle it here anyway.
}
}
}
}
(the above method is called by reload() method and by getters depending on one of the properties which are to be set)
And here's how the CombinedResourceInputStream look like:
final class CombinedResourceInputStream extends InputStream {
private List<InputStream> streams;
private Iterator<InputStream> streamIterator;
private InputStream currentStream;
public CombinedResourceInputStream(Set<Resource> resources) throws IOException {
streams = new ArrayList<InputStream>();
for (Resource resource : resources) {
streams.add(resource.getInputStream());
}
streamIterator = streams.iterator();
streamIterator.hasNext(); // We assume it to be always true; CombinedResourceInfo won't be created anyway if it's empty.
currentStream = streamIterator.next();
}
#Override
public int read() throws IOException {
int read = -1;
while ((read = currentStream.read()) == -1) {
if (streamIterator.hasNext()) {
currentStream = streamIterator.next();
} else {
break;
}
}
return read;
}
#Override
public void close() throws IOException {
IOException caught = null;
for (InputStream stream : streams) {
try {
stream.close();
} catch (IOException e) {
if (caught == null) {
caught = e; // Don't throw it yet. We have to continue closing all other streams.
}
}
}
if (caught != null) {
throw caught;
}
}
}
Update 2: a concrete and reuseable solution is available in OmniFaces. See also CombinedResourceHandler showcase page and API documentation for more detail.
You may want to evaluate JAWR before implementing your own solution. I've used it in couple of projects and it was a big success. It used in JSF 1.2 projects but I think it will be easy to extend it to work with JSF 2.0. Just give it a try.
Omnifaces provided CombinedResourceHandler is an excellent utility, but I also love to share about this excellent maven plugin:- resources-optimizer-maven-plugin that can be used to minify/compress js/css files &/or aggregate them into fewer resources during the build time & not dynamically during runtime which makes it a more performant solution, I believe.
Also have a look at this excellent library as well:- webutilities
I have an other solution for JSF 2. Might also rok with JSF 1, but i do not know JSF 1 so i can not say. The Idea works mainly with components from h:head and works also for stylesheets. The result
is always one JavaScript (or Stylesheet) file for a page! It is hard for me to describe but i try.
I overload the standard JSF ScriptRenderer (or StylesheetRenderer) and configure the renderer
for the h:outputScript component in the faces-config.xml.
The new Renderer will now not write anymore the script-Tag but it will collect all resources
in a list. So first resource to be rendered will be first item in the list, the next follows
and so on. After last h:outputScript component ist rendered, you have to render 1 script-Tag
for the JavaScript file on this page. I make this by overloading the h:head renderer.
Now comes the idea:
I register an filter! The filter will look for this 1 script-Tag request. When this request comes,
i will get the list of resources for this page. Now i can fill the response from the list of
resources. The order will be correct, because the JSF rendering put the resources in correct order
into the list. After response is filled, the list should be cleared. Also you can do more
optimizations because you have the code in the filter....
I have code that works superb. My code also can handle browser caching and dynamic script rendering.
If anybody is interested i can share the code.