How to resize a WizardDialog depending on the WizardPage? - resize

I am creating a JFace WizardDialog including a single WizardPage that is changing its contents (controls) depending on user selection. In short:
final PageAccess page = new PageAccess(...);
WizCreate wiz = new WizCreate() {
#Override
public boolean performFinish() {
page.performFinish();
return true;
}
};
wiz.addPage(page);
Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
WizardDialog dialog = new WizardDialog(shell, wiz);
dialog.open();
PageAccess extends WizardPage, and WizCreate extends Wizard.
All the Composites in the page use GridLayout.
The page implements a SelectionListener and redraws its contents.
When the page changes its size (expand its width because of new controls), the dialog is not resized so that the page is cut off.
So how to resize the dialog depending on the page?

WizardDialog implements IWizardContainer2 which has an updateSize method that you can call to recalculate the size.
In your page you can use
if (getContainer() instanceof IWizardContainer2 container) {
container.updateSize();
}
(code uses Java 16 instanceof type patterns, for older releases it would be a bit more complicated).
Note that WizardDialog only ever increases the size, it won't reduce the size.

I managed by calculating the preferred size of the main control, and then resizing the dialog shell:
Point preferredSize = getControl().computeSize(SWT.DEFAULT, SWT.DEFAULT, true);
WizardDialog dialog = (WizardDialog) getContainer();
dialog.getShell().setSize(preferredSize.x+..., preferredSize.y+...);

Related

javafx TabPane setSelection doesn't switch the tab

I want to control the TabPane and switch tabs with a Canvas, basically hide the tab headers and use canvas instead, the canvas displays different "Devices" and when user click on the device, the TabPane switch to show the content of that device.
fun canvasFlow_Click(mouseEvent: MouseEvent) {
val d = flowPresenter.click(mouseEvent)
if (d != null) {
flowPresenter.select(d)
logger.info("switch to ${d.position}")
tab.selectionModel.clearSelection()
tab.selectionModel.select(d.position)
}
}
Why select doesn't work? I don't see tab content changed. from log, I see "switch to 1", "switch to 2" correctly, just the tab doesn't switch! Why?
Note: this is plain javafx and might not be the exact solution in your case (hard to tell without example) - but actually I can reproduce the described behavior.
The problem seems to be calling clearSelection: TabPane with tabs should have exactly one selected tab - which is not specified. But every internal collaborator (skin, header, listener) goes to great lengths to guarantee that constraint - this assumption was wrong, in fact the skin gets confused if the selection changes to empty - the previously selected content is not removed. Might be a bug, either don't allow the selectionModel to be empty or improve the skin to cope with it (wondering how that would be supposed to look - hide the content?)
Anyway, here the solution is to not call that method before manually selecting a tab. There's no need to do so anyway, it's a SingleSelectionModel so takes care of allowing at most one item selected.
An example: un/comment the clearSelection and see how the content is not/ correctly updated with/out the call.
public class TabPaneSelection extends Application {
private Parent createContent() {
TabPane pane = new TabPane();
HBox headers = new HBox();
for (int i = 0; i < 3; i++) {
int index = i;
Tab tab = new Tab("header " + i, new Label("content " + i));
pane.getTabs().add(tab);
Button button = new Button(tab.getText());
button.setOnAction(e -> {
// do __not__ clear selection
//pane.getSelectionModel().clearSelection();
pane.getSelectionModel().select(index);
});
headers.getChildren().add(button);
}
BorderPane content = new BorderPane(pane);
content.setTop(headers);
return content;
}
#Override
public void start(Stage stage) throws Exception {
stage.setScene(new Scene(createContent(), 400, 300));
//stage.setTitle(FXUtils.fxVersion());
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}

Codename One Drag & Drop blackens dialog background

I'm developing an app that uses drag & drop operations. The drag & drop part includes four containers as drop targets and two draggable labels.
The app works as expected in the simulator, but when I install it on my Android phone, the background of the dialog (otherwise white) turns black for a swift moment as soon as the draggable components (labels) are released (then gets back to the original light color).
In the theme builder, I have set the background of the dialog to an almost white color (unselected, selected, pressed, disabled), but that doesn't show any effect.
Is there any turnaround to keep the background color stable? Any help would be appreciated.
Here is the relevant code excerpt:
Dialog dialog_W1 = new Dialog();
dialog_W1.setScrollableY(true);
dialog_W1.setLayout(new BoxLayout(2));
dialog_W1.setScrollableY(true);
dialog_W1.setTitle("Acerte o bichinho");
Image img_Cat = Image.createImage("/cat.png");
ScaleImageLabel label_Scaled_Image = new ScaleImageLabel(img_Cat);
label_Scaled_Image.setBackgroundType(Style.BACKGROUND_IMAGE_SCALED_FIT);
label_Scaled_Image.setUIID("NewLabel");
Label label_1 = new Label("ga");
Label label_2 = new Label("to");
label_1.setUIID("LabelWord");
label_2.setUIID("LabelWord");
label_1.setDraggable(true);
label_2.setDraggable(true);
Container c_1 = new Container();
Container c_2 = new Container();
Container c_3 = new Container();
Container c_4 = new Container();
c_1.setDropTarget(true);
c_2.setDropTarget(true);
c_3.setDropTarget(true);
c_4.setDropTarget(true);
c_1.setLayout(new BoxLayout(2));
c_2.setLayout(new BoxLayout(2));
c_3.setLayout(new BoxLayout(2));
c_4.setLayout(new BoxLayout(2));
c_1.setUIID("Container_1");
c_2.setUIID("Container_1");
c_3.setUIID("Container_2");
c_4.setUIID("Container_2");
c_3.add(label_2);
c_4.add(label_1);
GridLayout gl = new GridLayout(2,2);
Container container_0 = new Container(gl);
container_0.add(c_1);
container_0.add(c_2);
container_0.add(c_3);
container_0.add(c_4);
ArrayList <Container> list_Containers = new ArrayList<>();
list_Containers.add(c_1);
list_Containers.add(c_2);
list_Containers.add(c_3);
list_Containers.add(c_4);
ArrayList <Label> list_Labels = new ArrayList<>();
list_Labels.add(label_1);
list_Labels.add(label_2);
for (Label label : list_Labels) {
label.addDragOverListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
for (Container c : list_Containers) {
int int_C = c.getComponentCount();
if (int_C == 1) {
c.setDropTarget(false);
} else if (int_C == 0) {
c.setDropTarget(true);
}
}
}
});
}
I suggest showing the Dialog as a modless dialog instead of using the show method use showModless() or similar.
Odd things like this can happen because of the EDT nesting inherent in modal dialogs, normally we don't recommend using dialog for anything more than simple notifications. When we use drag and drop from a dialog like component we use InteractionDialog.

UWP Light dismiss ContentDialog

Is there a way to make the ContentDialog light dismiss?, so when the user clicks on any thing outside the ContentDialog it should be closed.
Thanks.
By default, ContentDialog is placed in PopupRoot. Behind it, there is a Rectangle which dim and prevent interaction with other elements in the app. You can find it with help of VisualTreeHelper and register a Tapped event to it, so when it's tapped you can hide ContentDialog.
You can do this after calling ShowAsync outside ContentDialog code or you can do it inside ContentDialog code. Personally, I implement a class which derives from ContentElement and I override OnApplyTemplate like this:
protected override void OnApplyTemplate()
{
// this is here by default
base.OnApplyTemplate();
// get all open popups
// normally there are 2 popups, one for your ContentDialog and one for Rectangle
var popups = VisualTreeHelper.GetOpenPopups(Window.Current);
foreach (var popup in popups)
{
if (popup.Child is Rectangle)
{
// I store a refrence to Rectangle to be able to unregester event handler later
_lockRectangle = popup.Child as Rectangle;
_lockRectangle.Tapped += OnLockRectangleTapped;
}
}
}
and in OnLockRectangleTapped:
private void OnLockRectangleTapped(object sender, TappedRoutedEventArgs e)
{
this.Hide();
_lockRectangle.Tapped -= OnLockRectangleTapped;
}
Unfortunately ContentDialog does not offer such behavior.
There are two alternatives you can consider:
Popup - a special control built for this purpose, which displays dialog-like UI on top of the app content. This control actually offers a IsLightDismissEnabled for the behavior you need. Since the Anniversary Update (SDK version 1607) also has a LightDismissOverlayMode, which can be set to "On" to automatically darken the UI around the Popup when displayed. More details are on MSDN.
Custom UI - you can create a new layer on top of your existing UI in XAML, have this layer cover the entire screen and watch for the Tapped event to dismiss it when displayed. This is more cumbersome, but you have a little more control over how it is displayed

JavaFX - SplitPane, resize & proportions

How can I achieve proportional resize of whole SplitPane?
public class JfxSplitPaneTest extends Application {
#Override
public void start(Stage stage) throws Exception {
SplitPane split = new SplitPane();
StackPane child1 = new StackPane();
child1.setStyle("-fx-background-color: #0000AA;");
StackPane child2 = new StackPane();
child2.setStyle("-fx-background-color: #00AA00;");
StackPane child3 = new StackPane();
child3.setStyle("-fx-background-color: #AA0000;");
split.getItems().addAll(child1, child2, child3);
//split.setDividerPositions(0.1f, 0.6f, 0.9f)
stage.setScene(new Scene(split, 400, 300));
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Start the program:
Set dividers how I like them:
Make window width really small (I continued and made it even smaller, no pic):
Resize back and observe that dividers are neither how I set them nor how they were when program started.
Doing this makes it closer to what I'd expect:
child1.setMinWidth(Region.USE_PREF_SIZE)
child1.setPrefWidth(100)
child2.setMinWidth(Region.USE_PREF_SIZE)
child2.setPrefWidth(200)
child3.setMinWidth(Region.USE_PREF_SIZE)
child3.setPrefWidth(300)
except that dividers are initally at wrong position (until I resize SplitPane, 1px is enough) and when shrinking window width, dividers are inside components.
How can I make this work please?
Try to set the divisors like AS3Boyan said
split.setDividerPositions(0.1f, 0.6f, 0.9f)
and add this:
How can I avoid a SplitPane to resize one of the panes when the window resizes?
Try to add Change Listener to stage.widthProperty() and stage.heightProperty(). And call this:
split.setDividerPositions(0.1f, 0.6f, 0.9f)
The rule of thumb is to use setDividerPositions with Platform.runLater when the resize event is triggered.
You probably want to start with a default ratio, let the user change that ratio, and maintain this ratio whatever it is when there's a resize event. Let's consider a 25/75 vertical FX splitPane in some stage:
splitPane.setDividerPositions(0.25f, 0.75f);
stage.heightProperty().addListener((obs, oldVal, newVal) -> {
double[] positions = splitPane.getDividerPositions(); // reccord the current ratio
Platform.runLater(() -> splitPane.setDividerPositions(positions)); // apply the now former ratio
});

How to Autofit the widgets in a view to variable window size automatically

I have an RCP application in which i changed position of the window(shell) and its size by overriding the postWindowCreate() method in ApplicationWorkbenchWindowAdvisor class, the widgets in the views are not fitted to the changed window size, to see all widgets either i have to maximize the window or move scroll bars of the view to see all the widgets inside the view, is it possible to auto-fit(show all widgets in the current window size without moving scroll bars or maximizing the window) the widgets in the view even if the screen size varies.
Usually, you change the size of the shell in WorkbenchWindowAdvisor.preWindowOpen(), not in WorkbenchWindowAdvisor.postWindowCreate():
public class ApplicationWorkbenchWindowAdvisor extends WorkbenchWindowAdvisor {
#Override
public void preWindowOpen() {
final IWorkbenchWindowConfigurer configurer = getWindowConfigurer();
configurer.setInitialSize(new Point(800, 600));
configurer.setShowCoolBar(false);
configurer.setShowStatusLine(false);
// configurer.setShellStyle(SWT.TITLE | SWT.RESIZE);
}
}
Likewise, you can set the window title and the style of the window.
The position of the window is usually best set in WorkbenchWindowAdvisor.postWindowCreate()...
If you must change the size of the window, then remember to call shell.pack()!