From ee9860c5e24a0e20cec5cc9efeac2ee1c611ff95 Mon Sep 17 00:00:00 2001 From: Veronika Irvine Date: Wed, 1 Dec 2004 19:23:02 +0000 Subject: Use SashFormData instead of storing ratios as a Long in control data --- .../common/org/eclipse/swt/custom/SashForm.java | 42 +++++++++++++++++----- .../org/eclipse/swt/custom/SashFormData.java | 27 ++++++++++++++ .../org/eclipse/swt/custom/SashFormLayout.java | 22 +++++++----- 3 files changed, 74 insertions(+), 17 deletions(-) create mode 100644 bundles/org.eclipse.swt/Eclipse SWT Custom Widgets/common/org/eclipse/swt/custom/SashFormData.java diff --git a/bundles/org.eclipse.swt/Eclipse SWT Custom Widgets/common/org/eclipse/swt/custom/SashForm.java b/bundles/org.eclipse.swt/Eclipse SWT Custom Widgets/common/org/eclipse/swt/custom/SashForm.java index 29965749b3..be519bc7b7 100755 --- a/bundles/org.eclipse.swt/Eclipse SWT Custom Widgets/common/org/eclipse/swt/custom/SashForm.java +++ b/bundles/org.eclipse.swt/Eclipse SWT Custom Widgets/common/org/eclipse/swt/custom/SashForm.java @@ -42,7 +42,6 @@ public class SashForm extends Composite { Control maxControl = null; Listener sashListener; static final int DRAG_MINIMUM = 20; - static final String LAYOUT_RATIO = "layout ratio"; //$NON-NLS-1$ /** * Constructs a new instance of this class given its parent @@ -133,9 +132,9 @@ public int[] getWeights() { Control[] cArray = getControls(false); int[] ratios = new int[cArray.length]; for (int i = 0; i < cArray.length; i++) { - Long ratio = (Long)cArray[i].getData(LAYOUT_RATIO); - if (ratio != null) { - ratios[i] = (int)(ratio.longValue() * 1000 >> 16); + Object data = cArray[i].getLayoutData(); + if (data != null && data instanceof SashFormData) { + ratios[i] = (int)(((SashFormData)data).weight * 1000 >> 16); } else { ratios[i] = 200; } @@ -184,8 +183,18 @@ void onDragSash(Event event) { event.doit = false; return; } - c1.setData(LAYOUT_RATIO, new Long((((long)b1.width << 16) + area.width - 1) / area.width)); - c2.setData(LAYOUT_RATIO, new Long((((long)b2.width << 16) + area.width - 1) / area.width)); + Object data1 = c1.getLayoutData(); + if (data1 == null || !(data1 instanceof SashFormData)) { + data1 = new SashFormData(); + c1.setLayoutData(data1); + } + Object data2 = c2.getLayoutData(); + if (data2 == null || !(data2 instanceof SashFormData)) { + data2 = new SashFormData(); + c2.setLayoutData(data2); + } + ((SashFormData)data1).weight = (((long)b1.width << 16) + area.width - 1) / area.width; + ((SashFormData)data2).weight = (((long)b2.width << 16) + area.width - 1) / area.width; } else { int shift = event.y - sashBounds.y; b1.height += shift; @@ -196,8 +205,18 @@ void onDragSash(Event event) { event.doit = false; return; } - c1.setData(LAYOUT_RATIO, new Long((((long)b1.height << 16) + area.height - 1) / area.height)); - c2.setData(LAYOUT_RATIO, new Long((((long)b2.height << 16) + area.height - 1) / area.height)); + Object data1 = c1.getLayoutData(); + if (data1 == null || !(data1 instanceof SashFormData)) { + data1 = new SashFormData(); + c1.setLayoutData(data1); + } + Object data2 = c2.getLayoutData(); + if (data2 == null || !(data2 instanceof SashFormData)) { + data2 = new SashFormData(); + c2.setLayoutData(data2); + } + ((SashFormData)data1).weight = (((long)b1.height << 16) + area.height - 1) / area.height; + ((SashFormData)data2).weight = (((long)b2.height << 16) + area.height - 1) / area.height; } if (event.detail != SWT.DRAG) { c1.setBounds(b1); @@ -335,7 +354,12 @@ public void setWeights(int[] weights) { SWT.error(SWT.ERROR_INVALID_ARGUMENT); } for (int i = 0; i < cArray.length; i++) { - cArray[i].setData(LAYOUT_RATIO, new Long((((long)weights[i] << 16) + total - 1) / total)); + Object data = cArray[i].getLayoutData(); + if (data == null || !(data instanceof SashFormData)) { + data = new SashFormData(); + cArray[i].setLayoutData(data); + } + ((SashFormData)data).weight = (((long)weights[i] << 16) + total - 1) / total; } layout(false); diff --git a/bundles/org.eclipse.swt/Eclipse SWT Custom Widgets/common/org/eclipse/swt/custom/SashFormData.java b/bundles/org.eclipse.swt/Eclipse SWT Custom Widgets/common/org/eclipse/swt/custom/SashFormData.java new file mode 100644 index 0000000000..cfd88c61f5 --- /dev/null +++ b/bundles/org.eclipse.swt/Eclipse SWT Custom Widgets/common/org/eclipse/swt/custom/SashFormData.java @@ -0,0 +1,27 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.swt.custom; + +class SashFormData { + + long weight; + +String getName () { + String string = getClass ().getName (); + int index = string.lastIndexOf ('.'); + if (index == -1) return string; + return string.substring (index + 1, string.length ()); +} + +public String toString () { + return getName()+" {weight="+weight+"}"; //$NON-NLS-2$ +} +} diff --git a/bundles/org.eclipse.swt/Eclipse SWT Custom Widgets/common/org/eclipse/swt/custom/SashFormLayout.java b/bundles/org.eclipse.swt/Eclipse SWT Custom Widgets/common/org/eclipse/swt/custom/SashFormLayout.java index 4149d2e16e..2ebaca8d2e 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT Custom Widgets/common/org/eclipse/swt/custom/SashFormLayout.java +++ b/bundles/org.eclipse.swt/Eclipse SWT Custom Widgets/common/org/eclipse/swt/custom/SashFormLayout.java @@ -55,11 +55,14 @@ protected Point computeSize(Composite composite, int wHint, int hHint, boolean f long[] ratios = new long[cArray.length]; long total = 0; for (int i = 0; i < cArray.length; i++) { - Long ratio = (Long)cArray[i].getData(SashForm.LAYOUT_RATIO); - if (ratio != null) { - ratios[i] = ratio.longValue(); + Object data = cArray[i].getLayoutData(); + if (data != null && data instanceof SashFormData) { + ratios[i] = ((SashFormData)data).weight; } else { - ratios[i] = ((200 << 16) + 999) / 1000; + data = new SashFormData(); + cArray[i].setLayoutData(data); + ((SashFormData)data).weight = ratios[i] = ((200 << 16) + 999) / 1000; + } total += ratios[i]; } @@ -135,11 +138,14 @@ protected void layout(Composite composite, boolean flushCache) { long[] ratios = new long[controls.length]; long total = 0; for (int i = 0; i < controls.length; i++) { - Long ratio = (Long)controls[i].getData(SashForm.LAYOUT_RATIO); - if (ratio != null) { - ratios[i] = ratio.longValue(); + Object data = controls[i].getLayoutData(); + if (data != null && data instanceof SashFormData) { + ratios[i] = ((SashFormData)data).weight; } else { - ratios[i] = ((200 << 16) + 999) / 1000; + data = new SashFormData(); + controls[i].setLayoutData(data); + ((SashFormData)data).weight = ratios[i] = ((200 << 16) + 999) / 1000; + } total += ratios[i]; } -- cgit