Skip to content

Commit 50b0e6c

Browse files
hansonrhansonr
authored andcommitted
JScrollBar fixes
1 parent 394fd1e commit 50b0e6c

File tree

6 files changed

+89
-74
lines changed

6 files changed

+89
-74
lines changed
234 Bytes
Binary file not shown.

sources/net.sf.j2s.java.core/src/swingjs/jquery/JQueryUI.js

Lines changed: 0 additions & 26 deletions
This file was deleted.

sources/net.sf.j2s.java.core/src/swingjs/jquery/jquery-ui-j2sslider.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -628,7 +628,7 @@
628628
if (val <= this._valueMin()) {
629629
return this._valueMin();
630630
}
631-
var max = this._valueMax() * (1-this.handleFraction);
631+
var max = (this._valueMax() - this._valueMin()) * (1-this.handleFraction) + this._valueMin();
632632
if (val >= max) {
633633
return max;
634634
}

sources/net.sf.j2s.java.core/src/swingjs/plaf/JSScrollBarUI.java

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,23 @@
22

33
import java.awt.Dimension;
44
import java.beans.PropertyChangeEvent;
5+
6+
import javax.swing.JScrollBar;
57
import javax.swing.event.ChangeEvent;
68
import swingjs.api.js.DOMNode;
79

10+
/**
11+
* The SwingjJS implementation of a JScrollBar utilizes all the JSSlider
12+
* capabilities. It adds an variable extent (handle size) and also has
13+
* a different appearance.
14+
*
15+
* TODO: block and unit increments. Right now it just tracks to where the
16+
* mouse was clicked.
17+
*
18+
* @author hansonr
19+
*
20+
*/
821
public class JSScrollBarUI extends JSSliderUI {
9-
// not a perfect solution
10-
// TODO -- tie in setHorizontal/setVertical
1122

1223
JSScrollPaneUI myScrollPaneUI;
1324

@@ -20,14 +31,15 @@ public JSScrollBarUI() {
2031

2132
@Override
2233
public void propertyChange(PropertyChangeEvent e) {
23-
super.propertyChange(e);
34+
super.propertyChange(e);
2435
if (debugging)
2536
System.out.println(id + " propertyChange " + dumpEvent(e));
2637
}
2738

2839
@Override
2940
public void stateChanged(ChangeEvent e) {
3041
super.stateChanged(e);
42+
setScrollBarExtentAndCSS();
3143
if (debugging)
3244
System.out.println(id + " stateChange " + dumpEvent(e));
3345
}
@@ -48,7 +60,41 @@ public void setVisible(boolean b) {
4860
DOMNode.setVisible(getOuterNode(), b);
4961
DOMNode.setVisible(jqSlider, b);
5062
}
51-
63+
64+
/**
65+
* The scrollbar handle size is set in the j2sSlider jQuery component
66+
* as a fraction, since the slider track is not the same size
67+
* as the JScrollBar component. Could be done other ways and just
68+
* send the extent, I suppose. We fall back to 0.1 if anything is wrong.
69+
*
70+
*/
71+
@Override
72+
void setScrollBarExtentAndCSS() {
73+
String left;
74+
String top;
75+
JScrollBar sb = (JScrollBar) jc;
76+
int extent = sb.getVisibleAmount();
77+
int max = sb.getMaximum();
78+
int min = sb.getMinimum();
79+
float f = (extent > 0 && max > min && extent <= max - min
80+
? extent * 1f / (max - min) : 0.1f);
81+
setSliderAttr("handleSize", f);
82+
if (myScrollPaneUI == null) {
83+
// in
84+
left = "3px";
85+
top = "3px";
86+
} else {
87+
left = "0px";
88+
top = "0px";
89+
}
90+
if (orientation == "vertical") {
91+
DOMNode.setStyles(sliderTrack, "left", left, "width", "12px", "background", "lightgrey");
92+
DOMNode.setStyles(sliderHandle, "left", "-1px", "margin-bottom", "0px");
93+
} else {
94+
DOMNode.setStyles(sliderTrack, "top", top, "height", "12px", "background", "lightgrey");
95+
DOMNode.setStyles(sliderHandle, "top", "-1px", "margin-left", "0px");
96+
}
97+
}
5298

5399
}
54100

sources/net.sf.j2s.java.core/src/swingjs/plaf/JSScrollPaneUI.java

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -193,14 +193,32 @@ static void loadActionMap(LazyActionMap map) {
193193
@Override
194194
public void paint(Graphics g, JComponent c) {
195195
checkTextAreaHeight();
196+
// unnecessary updateScrollBarExtents();
196197
Border vpBorder = scrollpane.getViewportBorder();
197198
if (vpBorder != null) {
198199
Rectangle r = scrollpane.getViewportBorderBounds();
199200
vpBorder.paintBorder(scrollpane, g, r.x, r.y, r.width, r.height);
200201
}
201202
}
202203

203-
private boolean haveTextArea = false;
204+
// private void updateScrollBarExtents() {
205+
// JViewport vp = scrollpane.getViewport();
206+
// if (vp == null)
207+
// return;
208+
// JComponent sc = (JComponent) vp.getView();
209+
// JScrollBar vsb = scrollpane.getVerticalScrollBar();
210+
// JScrollBar hsb = scrollpane.getHorizontalScrollBar();
211+
// if (vsb != null) {
212+
// vsb.setVisibleAmount(vp.getHeight());
213+
// ((JSScrollBarUI) vsb.getUI()).setScrollBarExtentAndCSS();
214+
// }
215+
// if (hsb != null) {
216+
// vsb.setVisibleAmount(vp.getWidth());
217+
// ((JSScrollBarUI) hsb.getUI()).setScrollBarExtentAndCSS();
218+
// }
219+
// }
220+
221+
204222
private Dimension textAreaSize;
205223

206224

@@ -209,19 +227,15 @@ public void paint(Graphics g, JComponent c) {
209227
* size of the textarea itself
210228
*/
211229
private void checkTextAreaHeight() {
212-
haveTextArea = false;
213230
JViewport vp = scrollpane.getViewport();
214231
if (vp == null)
215232
return;
216233
JComponent sc = (JComponent) vp.getView();
217-
int totalHeight = sc.getBounds().height;
218-
int totalWidth = sc.getBounds().width;
219-
int viewHeight = vp.getHeight();
220-
int viewWidth = vp.getWidth();
221-
JScrollBar vsb = scrollpane.getVerticalScrollBar();
222-
JScrollBar hsb = scrollpane.getHorizontalScrollBar();
223234
if (sc != null && sc.getUI() != null && sc.getUIClassID() == "TextAreaUI") {
224-
haveTextArea = true;
235+
int totalHeight = sc.getBounds().height;
236+
int totalWidth = sc.getBounds().width;
237+
JScrollBar vsb = scrollpane.getVerticalScrollBar();
238+
JScrollBar hsb = scrollpane.getHorizontalScrollBar();
225239
if (textAreaSize == null)
226240
textAreaSize = new Dimension();
227241
((JSTextAreaUI) sc.getUI()).getTextAreaTextSize(textAreaSize);
@@ -234,10 +248,6 @@ private void checkTextAreaHeight() {
234248
textAreaSize.width = totalWidth;
235249
sc.setSize(textAreaSize);
236250
}
237-
if (vsb != null)
238-
((JSScrollBarUI) vsb.getUI()).setScrollPaneCSS(viewHeight * 1f / totalHeight);
239-
if (hsb != null)
240-
((JSScrollBarUI) hsb.getUI()).setScrollPaneCSS(viewWidth * 1f / totalWidth);
241251
}
242252

243253
/**
@@ -764,11 +774,9 @@ private void updateScrollBar(PropertyChangeEvent pce, ChangeListener cl,
764774
if (sb != null) {
765775
if (isVertical) {
766776
vertBarUI = (JSScrollBarUI) sb.getUI();
767-
vertBarUI.isScrollPaneScrollBar = true;
768777
vertBarUI.myScrollPaneUI = this;
769778
} else {
770779
horizBarUI = (JSScrollBarUI) sb.getUI();
771-
horizBarUI.isScrollPaneScrollBar = true;
772780
horizBarUI.myScrollPaneUI = this;
773781
}
774782

@@ -1158,6 +1166,7 @@ class Handler implements ChangeListener, PropertyChangeListener {// ,
11581166
//
11591167
@Override
11601168
public void stateChanged(ChangeEvent e) {
1169+
textAreaSize = null;
11611170
JViewport viewport = scrollpane.getViewport();
11621171
if (viewport != null) {
11631172
if (e.getSource() == viewport) {
@@ -1181,6 +1190,7 @@ private void vsbStateChanged(JViewport viewport, ChangeEvent e) {
11811190
Point p = viewport.getViewPosition();
11821191
p.y = model.getValue();
11831192
viewport.setViewPosition(p);
1193+
scrollpane.getVerticalScrollBar().setVisibleAmount(viewport.getHeight());
11841194
}
11851195

11861196
private void hsbStateChanged(JViewport viewport, ChangeEvent e) {
@@ -1215,6 +1225,7 @@ private void hsbStateChanged(JViewport viewport, ChangeEvent e) {
12151225
}
12161226
}
12171227
}
1228+
scrollpane.getHorizontalScrollBar().setVisibleAmount(viewport.getWidth());
12181229
viewport.setViewPosition(p);
12191230
}
12201231

sources/net.sf.j2s.java.core/src/swingjs/plaf/JSSliderUI.java

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@ public class JSSliderUI extends JSLightweightUI implements PropertyChangeListene
4040

4141
protected String orientation;
4242

43-
boolean isScrollPaneScrollBar; // vertical scrollbars on scroll panes are inverted
44-
4543
protected DOMNode jqSlider;
4644
private int z0 = Integer.MIN_VALUE;
4745
private BoundedRangeModel model;
@@ -218,7 +216,7 @@ private void setup(boolean isNew) {
218216
}
219217
}
220218

221-
private void setSliderAttr(String key, float val) {
219+
protected void setSliderAttr(String key, float val) {
222220
Object slider = $(jqSlider);
223221
/**
224222
* @j2sNative
@@ -269,7 +267,7 @@ public void setSlider() {
269267
DOMNode node = DOMNode.createElement("div", id + "_t" + i);
270268
$(node).addClass("swingjs");
271269
$(node).addClass(tickClass);
272-
boolean isMajor = (i % check == 0);
270+
boolean isMajor = (i % check == 0);
273271
float frac = (isHoriz == isInverted ? 1 - fracSpacing * i : fracSpacing
274272
* i);
275273
String spt = (frac * length + margin) + "px";
@@ -324,10 +322,9 @@ public void setSlider() {
324322
} else {
325323
DOMNode.setStyles(sliderTrack, isHoriz ? "top" : "left", barPlace + "%");
326324
}
327-
if (!isHoriz && !isScrollPaneScrollBar)
325+
if (!isHoriz && !isScrollBar)
328326
DOMNode.setStyles(sliderTrack, "height", length + "px");
329-
if (isScrollPaneScrollBar)
330-
setScrollPaneCSS(0);
327+
setScrollBarExtentAndCSS();
331328
setHTMLSize(domNode, false);
332329
}
333330

@@ -353,14 +350,12 @@ public void propertyChange(PropertyChangeEvent e) {
353350

354351
@Override
355352
public void stateChanged(ChangeEvent e) {
356-
// from Java
357-
//isTainted = true;
358-
int v;
353+
int v;
359354
if ((v = jSlider.getMinimum()) != min)
360355
setSliderAttr("min", min = v);
361356
if ((v = jSlider.getMaximum()) != max)
362-
setSliderAttr("max", max = v);
363-
if ((v = jSlider.getValue()) != val)
357+
setSliderAttr("max", max = v);
358+
if ((v = jSlider.getValue()) != val)
364359
setSliderAttr("value", val = v);
365360
setup(false);
366361
}
@@ -371,27 +366,16 @@ public void setInnerComponentBounds(int width, int height) {
371366
if (!paintTicks && !paintLabels) {
372367
if (orientation == "vertical") {
373368
DOMNode.setStyles(sliderTrack, "height", (height - 20) + "px");
369+
} else if (isScrollBar) {
370+
DOMNode.setStyles(sliderTrack, "width", (width - 20) + "px");
374371
}
375-
if (isScrollPaneScrollBar)
376-
setScrollPaneCSS(0);
372+
setScrollBarExtentAndCSS();
377373

378374
}
379375
}
380376

381-
private int handleSize = 0;
382-
383-
void setScrollPaneCSS(float handleSize) {
384-
isScrollPaneScrollBar = true;
385-
if (handleSize > 0) {
386-
setSliderAttr("handleSize", handleSize);
387-
}
388-
if (orientation == "vertical") {
389-
DOMNode.setStyles(sliderTrack, "left", "0px", "width", "12px");
390-
DOMNode.setStyles(sliderHandle, "left", "-1px", "margin-bottom", "0px");
391-
} else {
392-
DOMNode.setStyles(sliderTrack, "top", "0px", "height", "12px");
393-
DOMNode.setStyles(sliderHandle, "top", "-1px", "margin-left", "0px");
394-
}
377+
void setScrollBarExtentAndCSS() {
378+
// ScrollBar subclass only
395379
}
396380

397381

0 commit comments

Comments
 (0)