Jface: custom FieldEditor bad layout - java

I'd like to create a custom FieldEditor for a preference page. the aim is to override ScaleFieldEditor to provide a Scale with two labels over it (one to display the min value, the other to display the max).
It made one. It is working but the layout is not good when I add a FileFieldeditor to preference page.
It's even worst if I add the FileFieldEditor immediately after the custom ScaleFieldEditor:
http://imageshack.us/g/844/customscalelayoutok.png/
(Sorry, I cannot add images to this post).
I made a Composite which contains the 2 Labels and the Scale. I used a GridLayout:
public class ScaleWithLabelFieldEditor extends ScaleFieldEditor {
/**
* The maximum value label
*/
private Label maxLabel;
/**
* The minimum value label
*/
private Label minLabel;
/**
* A composite that contains the scale and the min & max values label
*/
private Composite controls;
public ScaleWithLabelFieldEditor(String name, String labelText,
Composite parent) {
super(name, labelText, parent);
}
public ScaleWithLabelFieldEditor(String name, String labelText,
Composite parent, int min, int max, int increment, int pageIncrement) {
super(name, labelText, parent, min, max, increment, pageIncrement);
}
#Override
protected void doFillIntoGrid(Composite parent, int numColumns) {
Control labelControl = getLabelControl(parent);
GridData gd = new GridData();
labelControl.setLayoutData(gd);
controls = getControls(parent, 2);
gd = new GridData(GridData.FILL_HORIZONTAL);
gd.horizontalSpan = numColumns -1;
gd.grabExcessHorizontalSpace = true;
controls.setLayoutData(gd);
updateControls();
parent.layout();
}
/**
* Initialize (if not done yet) the controls composite
*
* #param parent
* the parent composite
* #return the controls composite that contains the scaler and the min/max
* labels
*/
protected Composite getControls(Composite parent, int numColumns) {
if (controls == null) {
controls = new Composite(parent, SWT.NONE);
GridLayout layout = new GridLayout(numColumns, false);
layout.numColumns = numColumns;
controls.setLayout(layout);
// Initialize the min value label
minLabel = getMinLabel(controls);
minLabel.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, false, false));
// Initialize the max value label
maxLabel = getMaxLabel(controls);
maxLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.TOP, false, false));
// Initialize the scale
scale = getScale(controls);
scale.setLayoutData(new GridData(SWT.FILL, SWT.BOTTOM, true, false, numColumns, 1));
}
return controls;
}
/**
* Nothing to do, already handle with controls composite
*/
#Override
protected void adjustForNumColumns(int numColumns) {
// Nothing to do, already handle with controls composite
}
/**
* Update the scale and the labels above
*/
protected void updateControls() {
if (controls != null && !controls.isDisposed()) {
// Update scale
scale.setMinimum(getMinimum());
scale.setMaximum(getMaximum());
scale.setIncrement(getIncrement());
scale.setPageIncrement(getPageIncrement());
// Update labels
maxLabel.setText(Integer.toString(getMaximum()));
minLabel.setText(Integer.toString(getMinimum()));
}
}
What I should do to make this field layout work? Did I miss something in GridLayout/GridData use?

The problem seems to be, that you did not implement the adjustForNumColumns(int numColumns) method. When you add the FileFieldEditor the number of columns changes from 2 to 3. And your field editor must adapt to that which it currently does not.

It seems like the horizontalSpan is not spanning until the end? So, may be you should try to remove -1 in gd.horizontalSpan = numColumns -1;

Related

Java SWT Resize Tripple SashForm constant middle smoothly

I implemented a Java SWT SashForm with 3 panes:
SashForm oSash = new SashForm(cmptParent, SWT.NONE);
GridLayout gridLayout = new GridLayout();
gridLayout.numColumns = 3;
oSash.setLayout(gridLayout);
oSash.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, true));
Composite oPaneLeft = new Composite(oSash, SWT.NONE);
...
Composite oPaneMiddle = new Composite(oSash, SWT.NONE);
...
Composite oPaneRight = new Composite(oSash, SWT.NONE);
The idea is to have a fixed size middle partition. Setting up initial widths is simple.
I want to be be able to resize the form by dragging the middle. The user clicks on the middle and drags left or right, thereby keeping the middle pane fixed, just sliding left or right. I am able to implement this functionality as follows:
private static Boolean sisResizeSashMiddle = false;
private static int siPosSashMiddleOffset = 0;
...
cmptPaneMiddle = new Composite(cmptParent, SWT.NONE);
cmptPaneMiddle.addMouseListener(new MouseAdapter()
{
#Override
public void mouseDown(MouseEvent arg0)
{
// The user wishes to resize the sash.
AppMain.sisResizeSashMiddle = true;
AppMain.siPosSashMiddleOffset = arg0.x - AppMain.siPosSashMiddleStart;
}
#Override
public void mouseUp(MouseEvent arg0)
{
// The user finished resizing the sash.
AppMain.sisResizeSashMiddle = false;
}
});
cmptPaneMiddle.addMouseMoveListener(new MouseMoveListener()
{
public void mouseMove(MouseEvent arg0)
{
// Only resize the sashes if user hold down the mouse while dragging.
if (true == AppMain.sisResizeSashMiddle)
{
// Compute the width of each sash.
int icxShell = shell.getSize().x;
int icxLeft = arg0.x - AppMain.siPosSashMiddleOffset;
int icxMiddle = AppMain.BrowserSash_Pane_Middle_Width;
int icxRight = shell.getSize().x - icxLeft - icxMiddle;
// Compute the weights.
int iWeightLeft = 10000 * icxLeft / icxShell;
int iWeightMiddle = 10000 * icxMiddle / icxShell;
int iWeightRight = 10000 * icxRight / icxShell;
// Set the weights.
int[] weights = new int[] {iWeightLeft, iWeightMiddle, iWeightRight};
oSash.setWeights(weights);
}
}
});
My issue is that sliding implementation is jerky and jittery, definitely not smooth. Is there a better way to get the same effect, just smooth with no jerky behavior?
Try using the SWT.SMOOTH flag on the SashForm:
SashForm oSash = new SashForm(cmptParent, SWT.SMOOTH);

SWT Table per-pixel scrolling

SWT Table has void setTopIndex(int index) and int getTopIndex().
I really need setTopPixel(int pixel) or setTopIndexFractional(double index).
I tried putting a Table into a ScrolledComposite, as such:
public class PixelTable extends ScrolledComposite {
private final Table table;
private final double itemHeight;
public PixelTable(Composite parent) {
super(parent, SWT.V_SCROLL | SWT.BORDER);
// setup the ScrolledComposite
table = new Table(this, SWT.VIRTUAL);
this.setExpandHorizontal(true);
this.setContent(table);
itemHeight = table.getItemHeight();
// setup the Table
table.setLinesVisible(true);
table.addListener(SWT.SetData, new Listener() {
#Override
public void handleEvent(Event e) {
int row = e.index;
TableItem item = (TableItem) e.item;
item.setText(Integer.toString(row));
}
});
}
/** Returns the fractional top index. */
public double getTopIndex() {
return getOrigin().y / itemHeight;
}
/** Sets the fractional top index. */
public void setTopIndex(double top) {
this.setOrigin(0, (int) Math.round(top * itemHeight));
}
/** Sets the item count. */
public void setItemCount(int count) {
table.setItemCount(count);
Point size = table.computeSize(SWT.DEFAULT, SWT.DEFAULT);
table.setSize(size);
}
}
This works for while, but once the inner Table's size gets bigger than ~65,000 pixels (~2,500 rows), the Table shows its scroll bar as well (probably an unsigned short somewhere on the native side). Additionally, it will be pretty tricky to get Table headers to work with this approach, since they'll scroll away.
Does anybody know what the issues would be (or how to get started) to add a per-pixel interface to the SWT API? I'm willing to branch the SWT source if necessary. I only care about the latest APIs for Win / OS X / Linux (e.g. I don't care if I have to break compatibility with old stuff).
If you want per-pixel scrolling, then just do the scrolling yourself. All you need to do is listen for SWT.MouseWheel, set Event#doit to false and then set the selection of the ScrollBar according to the scroll direction (increase/decrease by 1 (<-- that's the one pixel)).
Here is an example:
public static void main(String[] args)
{
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setText("StackOverflow");
shell.setLayout(new FillLayout());
final Table table = new Table(shell, SWT.V_SCROLL);
for (int i = 0; i < 100; i++)
new TableItem(table, SWT.NONE).setText("Item: " + i);
table.addListener(SWT.MouseWheel, new Listener()
{
#Override
public void handleEvent(Event e)
{
int increment = table.getVerticalBar().getSelection();
if (e.count < 0)
increment++;
else if (e.count > 0)
increment--;
table.getVerticalBar().setSelection(increment);
e.doit = false;
}
});
shell.pack();
shell.setSize(400, 200);
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch())
{
display.sleep();
}
}
display.dispose();
}

Adding canvas dynamically in GWT

I'm currently working on an image editor application.
I'm trying to provide the option in my application to work with different layers like in GIMP or Photoshop. My approach is to add a Canvas for each layer the user added.
Everything is working pretty nice but somehow my dynamically added canvases don't show up.
In my classes constructor I added 1 general canvas which holds a background-image and which can't be edited. This canvas(which is global) does show up and works properly. All layers that can be edited are stored in a List<Canvas> canvasLayers.
This is the method which should add a canvas to my List and Formpanel.
public void addCanvasLayer(String layerName, int id){
Canvas newCanvas = Canvas.createIfSupported();
newCanvas.setWidth(this.scaledImageWidth + "px");
newCanvas.setHeight(this.scaledImageHeight + "px");
newCanvas.setCoordinateSpaceWidth(this.scaledImageWidth);
newCanvas.setCoordinateSpaceHeight(this.scaledImageHeight);
newCanvas.getElement().setId(layerName);
newCanvas.getElement().getStyle().setZIndex(id);
this.fpCanvas.add(newCanvas, new AbsoluteData(0, 0));
this.canvasLayers.add(newCanvas);
this.addCanvasHandler(newCanvas);
context = newCanvas.getContext2d();
}
All it does is creating a canvas, setting its size to the main width and height, setting its id and z-index, adding the canvas to my canvaslist, adding handlers to the canvas(clickhandler, onMouseDownHandler,...) and adding the canvas to the main context.
ScaledImageWidth and -Height definitely are NOT null or 0 when adding the canvas.
Any ideas?
Edit: Solved the problem by adding fpCanvas.layout(); to the end of the method.
My formpanel just had to be refreshed <.< ....
Here is how I manage layers of canvas:
public abstract class Layer
extends Composite
{
/**
* The canvas used in this layer
*/
private Canvas m_canvas;
public Layer()
{
m_canvas = Canvas.createIfSupported();
initWidget( m_canvas );
}
/**
* Get the name of the layer (debug purpose only)
* #return the name of the layer
*/
protected abstract String getLayerName();
/**
* Set the z-index of the layer
* #param zindex the new z-index
*/
public void setZ( int zindex )
{
m_canvas.getElement().getStyle().setZIndex( zindex );
}
public int getZ()
{
return Integer.parseInt( m_canvas.getElement().getStyle().getZIndex() );
}
#Override
public void setPixelSize( int width, int height )
{
super.setPixelSize( width, height );
m_canvas.setCoordinateSpaceWidth( width );
m_canvas.setCoordinateSpaceHeight( height );
}
/**
* Draw the layer
*/
public void draw()
{
}
/**
* Get the context2d where this layer will be drawn
*
* #return the Context2d
*/
public final Context2d getContext2d()
{
return m_canvas.getContext2d();
}
/**
* Clear the layer
*/
public void clear()
{
Context2d context2d = getContext2d();
if ( m_canvas != null && m_canvas.isAttached() )
{
context2d.clearRect( 0, 0, m_canvas.getOffsetWidth(), m_canvas.getOffsetHeight() );
}
}
public LayoutPanel getEnclosingLayoutPanel()
{
return (LayoutPanel)getParent();
}
}
and to add it:
public void addLayer( Layer layer, int zindex )
{
// m_main is a LayoutPanel
int width = m_main.getOffsetWidth();
int height = m_main.getOffsetHeight();
m_layers.add( layer );
layer.setZ( zindex );
if ( m_main.getWidgetCount() > 0 )
{
m_main.insert( layer, 1 );
}
else
{
m_main.add( layer );
}
layer.setPixelSize( width, height );
(...)
Solved the problem by adding fpCanvas.layout(); to the end of the method. My formpanel just had to be refreshed <.< ....

Scrolling Through a JPanel When Mouse is Hovering Over JTable

I have a JTable inside a JPanel. I can scroll up and down the JPanel using the mouse's scroll wheel, but when my mouse is hovering over the JTable, I have to move it out of the table to scroll back up the JPanel using the scroll wheel. Is there a way I can scroll up and down the JPanel using the scroll wheel if the mouse is hovering over the JTable?
I took Xeon's advice in the comment above and implemented a mouse wheel listener that forwards mouse wheel events to the parent component. See the code below.
public class CustomMouseWheelListener implements MouseWheelListener {
private JScrollBar bar;
private int previousValue = 0;
private JScrollPane parentScrollPane;
private JScrollPane customScrollPane;
/** #return The parent scroll pane, or null if there is no parent. */
private JScrollPane getParentScrollPane() {
if (this.parentScrollPane == null) {
Component parent = this.customScrollPane.getParent();
while (!(parent instanceof JScrollPane) && parent != null) {
parent = parent.getParent();
}
this.parentScrollPane = (JScrollPane) parent;
}
return this.parentScrollPane;
}
/**
* Creates a new CustomMouseWheelListener.
* #param customScrollPane The scroll pane to which this listener belongs.
*/
public CustomMouseWheelListener(JScrollPane customScrollPane) {
ValidationUtils.checkNull(customScrollPane);
this.customScrollPane = customScrollPane;
this.bar = this.customScrollPane.getVerticalScrollBar();
}
/** {#inheritDoc} */
#Override
public void mouseWheelMoved(MouseWheelEvent event) {
JScrollPane parent = getParentScrollPane();
if (parent != null) {
if (event.getWheelRotation() < 0) {
if (this.bar.getValue() == 0 && this.previousValue == 0) {
parent.dispatchEvent(cloneEvent(event));
}
}
else {
if (this.bar.getValue() == getMax() && this.previousValue == getMax()) {
parent.dispatchEvent(cloneEvent(event));
}
}
this.previousValue = this.bar.getValue();
}
else {
this.customScrollPane.removeMouseWheelListener(this);
}
}
/** #return The maximum value of the scrollbar. */
private int getMax() {
return this.bar.getMaximum() - this.bar.getVisibleAmount();
}
/**
* Copies the given MouseWheelEvent.
*
* #param event The MouseWheelEvent to copy.
* #return A copy of the mouse wheel event.
*/
private MouseWheelEvent cloneEvent(MouseWheelEvent event) {
return new MouseWheelEvent(getParentScrollPane(), event.getID(), event.getWhen(),
event.getModifiers(), 1, 1, event.getClickCount(), false, event.getScrollType(),
event.getScrollAmount(), event.getWheelRotation());
}
}
Thanks denshaotoko for sharing your code. I've implemented a solution along the same lines (event forwarding) but put it into the scroll pane directly. Thought it might be useful to others.
/**
* Scroll pane that only scrolls when it owns focus. When not owning focus (i.e. mouse
* hover), propagates mouse wheel events to its container.
* <p>
* This is a solution for <i>"I have a JTable inside a JPanel. When my mouse is hovering
* over the JTable, I have to move it out of the table to scroll the JPanel."</i>
*/
public class ScrollWhenFocusedPane extends JScrollPane {
// Note: don't leave users with "scroll on focus" behaviour
// on widgets that they cannot focus. These will be okay.
public ScrollWhenFocusedPane (JTree view) {super (view);}
public ScrollWhenFocusedPane (JList view) {super (view);}
public ScrollWhenFocusedPane (JTable view) {super (view);}
public ScrollWhenFocusedPane (JTextArea view) {super (view);}
#Override
protected void processMouseWheelEvent (MouseWheelEvent evt) {
Component outerWidget = SwingUtilities.getAncestorOfClass (Component.class, this);
// Case 1: we don't have focus, so we don't scroll
Component innerWidget = getViewport().getView();
if (!innerWidget.hasFocus())
outerWidget.dispatchEvent(evt);
// Case 2: we have focus
else {
JScrollBar innerBar = getVerticalScrollBar();
if (!innerBar.isShowing()) // Deal with horizontally scrolling widgets
innerBar = getHorizontalScrollBar();
boolean wheelUp = evt.getWheelRotation() < 0;
boolean atTop = (innerBar.getValue() == 0);
boolean atBottom = (innerBar.getValue() == (innerBar.getMaximum() - innerBar.getVisibleAmount()));
// Case 2.1: we've already scrolled as much as we could
if ((wheelUp & atTop) || (!wheelUp & atBottom))
outerWidget.dispatchEvent(evt);
// Case 2.2: we'll scroll
else
super.processMouseWheelEvent (evt);
}
}
}

Background color of widgets in JFace PopupDialog

I want to use the JFace PopupDialog as lightweight dialog for user input. But I have some problems with the background color of text widgets.
As you can see below in 1, a SWT.MULTI text widget has no background and border, a SWT.SINGLE text widget has no background.
I tried to override the background color with:
Text comment = new Text(composite, SWT.MULTI|SWT.BORDER);
comment.setFocus();
comment.setBackground(new Color(Display.getDefault(), new RGB(000, 000, 000)));
// method of PopupDialog
applyBackgroundColor(new Color(Display.getDefault(), new RGB(000, 000, 000)), comment);
Does anybody has any idea how to handle this properly?
Thanks in advance!
EDIT: As requested, here is the source for the popup. I subclassed the PopupDialog, as I wanted the popup to be opened next to the Cursor location:
public class MouseLocationPopupDialog extends PopupDialog {
private final static int SHELL_STYLE = PopupDialog.INFOPOPUP_SHELLSTYLE;
public MouseLocationPopupDialog(Shell parent, String infoText) {
this(parent, SHELL_STYLE, true, false, false, false, false, null, infoText);
}
public MouseLocationPopupDialog(Shell parent, String titleText, String infoText) {
this(parent, SHELL_STYLE, true, false, false, false, false, titleText, infoText);
}
public MouseLocationPopupDialog(Shell parent, String infoText, final Point size) {
this(parent, infoText);
getShell().setSize(size);
}
public MouseLocationPopupDialog(Shell parent, int shellStyle, boolean takeFocusOnOpen, boolean persistSize, boolean persistLocation, boolean showDialogMenu, boolean showPersistActions, String titleText, String infoText) {
super(parent, shellStyle, takeFocusOnOpen, persistSize, persistLocation, showDialogMenu, showPersistActions, titleText, infoText);
}
#Override
protected void adjustBounds() {
super.adjustBounds();
Display d = Display.getCurrent();
if (d == null) {
d = Display.getDefault();
}
Point point = d.getCursorLocation();
getShell().setLocation(point.x + 9, point.y + 14);
}
}
The actual usage is as follows:
final PopupDialog dialog = new MouseLocationPopupDialog(HandlerUtil.getActiveShell(event), "Title", "Bottom bar") {
#Override
protected Control createDialogArea(Composite parent) {
Control composite = super.createDialogArea(parent);
Composite table = new Composite((Composite) composite, SWT.NONE);
table.setLayout(new GridLayout(2, true));
// text is a member variable
text = new Text(table, SWT.SINGLE | SWT.BORDER);
Button submit = new Button(table, SWT.PUSH);
return composite;
}
#Override
protected Control createContents(Composite parent) {
Control contents = super.createContents(parent);
final Color backgroundColor = new Color(Display.getCurrent(), new RGB(255, 255, 255));
text.setBackground(backgroundColor);
final Color foregroundColor = new Color(Display.getCurrent(), new RGB(0,0,0));
text.setForeground(foregroundColor);
backgroundColor.dispose();
foregroundColor.dispose();
return contents;
}
};
dialog.open();
Note that this Popup is independent from other UI elements: The code will not wait for the completion of the popups open() like other JFace dialogs (e.g. TitleAreaDialog)
First of all, use SWT.BORDER instead of SWT.BORDER_SOLID. If you're lucky, this somehow causes your problem. Other than that, from your small snippet alone it's hard to see what goes wrong. Unless there is some other code that resets the background color later on, this should work.
Update:
Try to override the method getBackground() of PopupDialog and let it return the color you want. Your code probably is in createDialogArea(..) and PopupDialog applies this color to basically everything after your code.
If you only want to change the background color of specific controls, you could try the following:
#Override
protected Control createContents(Composite parent) {
Composite contents = super.createContents(parent);
// set the color here
return contents;
}

Categories

Resources