Skip to content

Commit 2dee06a

Browse files
committed
Fix Scrolling while Zooming,
Ctrl inhibits scroll functions in RScrollPane Addresses #53
1 parent 9290349 commit 2dee06a

File tree

2 files changed

+224
-35
lines changed

2 files changed

+224
-35
lines changed

src/us/deathmarine/luyten/JFontChooser.java

+37-25
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@
5555
* <pre>
5656
**/
5757
public class JFontChooser extends JComponent {
58+
/**
59+
*
60+
*/
61+
private static final long serialVersionUID = 8856126034081661L;
5862
// class variables
5963
/**
6064
* Return value from <code>showDialog()</code>.
@@ -83,20 +87,20 @@ public class JFontChooser extends JComponent {
8387
// instance variables
8488
protected int dialogResultValue = ERROR_OPTION;
8589

86-
private String[] fontStyleNames = null;
87-
private String[] fontFamilyNames = null;
88-
private String[] fontSizeStrings = null;
89-
private JTextField fontFamilyTextField = null;
90-
private JTextField fontStyleTextField = null;
91-
private JTextField fontSizeTextField = null;
92-
private JList fontNameList = null;
93-
private JList fontStyleList = null;
94-
private JList fontSizeList = null;
95-
private JPanel fontNamePanel = null;
96-
private JPanel fontStylePanel = null;
97-
private JPanel fontSizePanel = null;
98-
private JPanel samplePanel = null;
99-
private JTextField sampleText = null;
90+
private String[] fontStyleNames;
91+
private String[] fontFamilyNames;
92+
private String[] fontSizeStrings;
93+
private JTextField fontFamilyTextField;
94+
private JTextField fontStyleTextField;
95+
private JTextField fontSizeTextField;
96+
private JList<?> fontNameList;
97+
private JList<?> fontStyleList;
98+
private JList<?> fontSizeList;
99+
private JPanel fontNamePanel;
100+
private JPanel fontStylePanel;
101+
private JPanel fontSizePanel;
102+
private JPanel samplePanel;
103+
private JTextField sampleText;
100104

101105
/**
102106
* Constructs a <code>JFontChooser</code> object.
@@ -172,9 +176,9 @@ public JTextField getFontSizeTextField() {
172176
return fontSizeTextField;
173177
}
174178

175-
public JList getFontFamilyList() {
179+
public JList<?> getFontFamilyList() {
176180
if (fontNameList == null) {
177-
fontNameList = new JList(getFontFamilies());
181+
fontNameList = new JList<Object>(getFontFamilies());
178182
fontNameList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
179183
fontNameList.addListSelectionListener(new ListSelectionHandler(getFontFamilyTextField()));
180184
fontNameList.setSelectedIndex(0);
@@ -184,9 +188,9 @@ public JList getFontFamilyList() {
184188
return fontNameList;
185189
}
186190

187-
public JList getFontStyleList() {
191+
public JList<?> getFontStyleList() {
188192
if (fontStyleList == null) {
189-
fontStyleList = new JList(getFontStyleNames());
193+
fontStyleList = new JList<Object>(getFontStyleNames());
190194
fontStyleList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
191195
fontStyleList.addListSelectionListener(new ListSelectionHandler(getFontStyleTextField()));
192196
fontStyleList.setSelectedIndex(0);
@@ -196,9 +200,9 @@ public JList getFontStyleList() {
196200
return fontStyleList;
197201
}
198202

199-
public JList getFontSizeList() {
203+
public JList<?> getFontSizeList() {
200204
if (fontSizeList == null) {
201-
fontSizeList = new JList(this.fontSizeStrings);
205+
fontSizeList = new JList<Object>(this.fontSizeStrings);
202206
fontSizeList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
203207
fontSizeList.addListSelectionListener(new ListSelectionHandler(getFontSizeTextField()));
204208
fontSizeList.setSelectedIndex(0);
@@ -390,7 +394,7 @@ protected class ListSelectionHandler implements ListSelectionListener {
390394

391395
public void valueChanged(ListSelectionEvent e) {
392396
if (e.getValueIsAdjusting() == false) {
393-
JList list = (JList) e.getSource();
397+
JList<?> list = (JList<?>) e.getSource();
394398
String selectedValue = (String) list.getSelectedValue();
395399

396400
String oldValue = textComponent.getText();
@@ -423,9 +427,9 @@ public void focusLost(FocusEvent e) {
423427
}
424428

425429
protected class TextFieldKeyHandlerForListSelectionUpDown extends KeyAdapter {
426-
private JList targetList;
430+
private JList<?> targetList;
427431

428-
public TextFieldKeyHandlerForListSelectionUpDown(JList list) {
432+
public TextFieldKeyHandlerForListSelectionUpDown(JList<?> list) {
429433
this.targetList = list;
430434
}
431435

@@ -454,9 +458,9 @@ public void keyPressed(KeyEvent e) {
454458
}
455459

456460
protected class ListSearchTextFieldDocumentHandler implements DocumentListener {
457-
JList targetList;
461+
JList<?> targetList;
458462

459-
public ListSearchTextFieldDocumentHandler(JList targetList) {
463+
public ListSearchTextFieldDocumentHandler(JList<?> targetList) {
460464
this.targetList = targetList;
461465
}
462466

@@ -511,6 +515,10 @@ public void run() {
511515
}
512516

513517
protected class DialogOKAction extends AbstractAction {
518+
/**
519+
*
520+
*/
521+
private static final long serialVersionUID = 1618273732543947323L;
514522
protected static final String ACTION_NAME = "OK";
515523
private JDialog dialog;
516524

@@ -528,6 +536,10 @@ public void actionPerformed(ActionEvent e) {
528536
}
529537

530538
protected class DialogCancelAction extends AbstractAction {
539+
/**
540+
*
541+
*/
542+
private static final long serialVersionUID = -4941763616565382601L;
531543
protected static final String ACTION_NAME = "Cancel";
532544
private JDialog dialog;
533545

src/us/deathmarine/luyten/OpenFile.java

+187-10
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package us.deathmarine.luyten;
22

3+
import java.awt.Component;
34
import java.awt.Cursor;
45
import java.awt.Font;
56
import java.awt.Panel;
6-
import java.awt.PopupMenu;
77
import java.awt.Rectangle;
88
import java.awt.event.ActionEvent;
99
import java.awt.event.ActionListener;
@@ -15,21 +15,21 @@
1515
import java.awt.event.MouseWheelEvent;
1616
import java.awt.event.MouseWheelListener;
1717
import java.io.StringWriter;
18-
import java.lang.reflect.Field;
1918
import java.util.Arrays;
2019
import java.util.HashSet;
2120
import java.util.Map;
2221
import java.util.Set;
2322
import java.util.TreeMap;
2423
import java.util.concurrent.ConcurrentHashMap;
2524

26-
import javax.swing.JDialog;
2725
import javax.swing.JLabel;
2826
import javax.swing.JMenuItem;
2927
import javax.swing.JPopupMenu;
3028
import javax.swing.JScrollBar;
31-
import javax.swing.JScrollPane;
29+
import javax.swing.JViewport;
3230
import javax.swing.ScrollPaneConstants;
31+
import javax.swing.Scrollable;
32+
import javax.swing.SwingConstants;
3333
import javax.swing.SwingUtilities;
3434
import javax.swing.event.HyperlinkEvent;
3535
import org.fife.ui.rsyntaxtextarea.LinkGenerator;
@@ -154,6 +154,7 @@ else if (name.toLowerCase().endsWith(".py"))
154154
public void actionPerformed(ActionEvent e) {
155155
JFontChooser fontChooser = new JFontChooser();
156156
fontChooser.setSelectedFont(textArea.getFont());
157+
fontChooser.setSelectedFontSize(textArea.getFont().getSize());
157158
int result = fontChooser.showDialog(mainWindow);
158159
if (result == JFontChooser.OK_OPTION)
159160
textArea.setFont(fontChooser.getSelectedFont());
@@ -211,19 +212,195 @@ public int getSourceOffset() {
211212
}
212213
});
213214

214-
//Add Ctrl+Wheel Zoom for Text Size
215+
/*
216+
* Add Ctrl+Wheel Zoom for Text Size
217+
* Removes all standard listeners and writes new listeners for wheelscroll movement.
218+
*/
219+
for(MouseWheelListener listeners :scrollPane.getMouseWheelListeners()){
220+
scrollPane.removeMouseWheelListener(listeners);
221+
};
215222
scrollPane.addMouseWheelListener(new MouseWheelListener(){
216223
@Override
217224
public void mouseWheelMoved(MouseWheelEvent e) {
225+
218226
if ((e.getModifiersEx() & InputEvent.CTRL_DOWN_MASK) != 0){
219227
Font font = textArea.getFont();
220228
int size = font.getSize();
221-
if(e.getWheelRotation() > 0){ //Down
222-
textArea.setFont(new Font(font.getName(), font.getStyle(), ++size));
223-
}else{
224-
textArea.setFont(new Font(font.getName(), font.getStyle(), --size >= 2 ? --size : 2));
229+
if(e.getWheelRotation() > 0){
230+
textArea.setFont(new Font(font.getName(), font.getStyle(), --size >= 8 ? --size : 8));
231+
}else{
232+
textArea.setFont(new Font(font.getName(), font.getStyle(), ++size));
225233
}
226-
}
234+
}else{
235+
if (scrollPane.isWheelScrollingEnabled() &&
236+
e.getWheelRotation() != 0) {
237+
JScrollBar toScroll = scrollPane.getVerticalScrollBar();
238+
int direction = e.getWheelRotation() < 0 ? -1 : 1;
239+
int orientation = SwingConstants.VERTICAL;
240+
if (toScroll == null || !toScroll.isVisible()) {
241+
toScroll = scrollPane.getHorizontalScrollBar();
242+
if (toScroll == null || !toScroll.isVisible()) {
243+
return;
244+
}
245+
orientation = SwingConstants.HORIZONTAL;
246+
}
247+
e.consume();
248+
249+
if (e.getScrollType() == MouseWheelEvent.WHEEL_UNIT_SCROLL) {
250+
JViewport vp = scrollPane.getViewport();
251+
if (vp == null) {
252+
return;
253+
}
254+
Component comp = vp.getView();
255+
int units = Math.abs(e.getUnitsToScroll());
256+
boolean limitScroll = Math.abs(e.getWheelRotation()) == 1;
257+
Object fastWheelScroll = toScroll.getClientProperty(
258+
"JScrollBar.fastWheelScrolling");
259+
if (Boolean.TRUE == fastWheelScroll &&
260+
comp instanceof Scrollable) {
261+
Scrollable scrollComp = (Scrollable) comp;
262+
Rectangle viewRect = vp.getViewRect();
263+
int startingX = viewRect.x;
264+
boolean leftToRight =
265+
comp.getComponentOrientation().isLeftToRight();
266+
int scrollMin = toScroll.getMinimum();
267+
int scrollMax = toScroll.getMaximum() -
268+
toScroll.getModel().getExtent();
269+
270+
if (limitScroll) {
271+
int blockIncr =
272+
scrollComp.getScrollableBlockIncrement(viewRect,
273+
orientation,
274+
direction);
275+
if (direction < 0) {
276+
scrollMin = Math.max(scrollMin,
277+
toScroll.getValue() - blockIncr);
278+
}
279+
else {
280+
scrollMax = Math.min(scrollMax,
281+
toScroll.getValue() + blockIncr);
282+
}
283+
}
284+
285+
for (int i = 0; i < units; i++) {
286+
int unitIncr =
287+
scrollComp.getScrollableUnitIncrement(viewRect,
288+
orientation, direction);
289+
if (orientation == SwingConstants.VERTICAL) {
290+
if (direction < 0) {
291+
viewRect.y -= unitIncr;
292+
if (viewRect.y <= scrollMin) {
293+
viewRect.y = scrollMin;
294+
break;
295+
}
296+
}
297+
else { // (direction > 0
298+
viewRect.y += unitIncr;
299+
if (viewRect.y >= scrollMax) {
300+
viewRect.y = scrollMax;
301+
break;
302+
}
303+
}
304+
}
305+
else {
306+
if ((leftToRight && direction < 0) ||
307+
(!leftToRight && direction > 0)) {
308+
viewRect.x -= unitIncr;
309+
if (leftToRight) {
310+
if (viewRect.x < scrollMin) {
311+
viewRect.x = scrollMin;
312+
break;
313+
}
314+
}
315+
}
316+
else if ((leftToRight && direction > 0) ||
317+
(!leftToRight && direction < 0)) {
318+
viewRect.x += unitIncr;
319+
if (leftToRight) {
320+
if (viewRect.x > scrollMax) {
321+
viewRect.x = scrollMax;
322+
break;
323+
}
324+
}
325+
}
326+
else {
327+
assert false : "Non-sensical ComponentOrientation / scroll direction";
328+
}
329+
}
330+
}
331+
if (orientation == SwingConstants.VERTICAL) {
332+
toScroll.setValue(viewRect.y);
333+
}
334+
else {
335+
if (leftToRight) {
336+
toScroll.setValue(viewRect.x);
337+
}
338+
else {
339+
int newPos = toScroll.getValue() -
340+
(viewRect.x - startingX);
341+
if (newPos < scrollMin) {
342+
newPos = scrollMin;
343+
}
344+
else if (newPos > scrollMax) {
345+
newPos = scrollMax;
346+
}
347+
toScroll.setValue(newPos);
348+
}
349+
}
350+
} else {
351+
int delta;
352+
int limit = -1;
353+
354+
if (limitScroll) {
355+
if (direction < 0) {
356+
limit = toScroll.getValue() - toScroll.getBlockIncrement(direction);
357+
} else {
358+
limit = toScroll.getValue() + toScroll.getBlockIncrement(direction);
359+
}
360+
}
361+
362+
for (int i = 0; i < units; i++) {
363+
if (direction > 0) {
364+
delta = toScroll.getUnitIncrement(direction);
365+
} else {
366+
delta = -toScroll.getUnitIncrement(direction);
367+
}
368+
int oldValue = toScroll.getValue();
369+
int newValue = oldValue + delta;
370+
if (delta > 0 && newValue < oldValue) {
371+
newValue = toScroll.getMaximum();
372+
} else if (delta < 0 && newValue > oldValue) {
373+
newValue = toScroll.getMinimum();
374+
}
375+
if (oldValue == newValue) {
376+
break;
377+
}
378+
if (limitScroll && i > 0) {
379+
assert limit != -1;
380+
if ((direction < 0 && newValue < limit)
381+
|| (direction > 0 && newValue > limit)) {
382+
break;
383+
}
384+
}
385+
toScroll.setValue(newValue);
386+
}
387+
388+
}
389+
} else if (e.getScrollType() == MouseWheelEvent.WHEEL_BLOCK_SCROLL) {
390+
int oldValue = toScroll.getValue();
391+
int blockIncrement = toScroll.getBlockIncrement(direction);
392+
int delta = blockIncrement * ((direction > 0) ? +1 : -1);
393+
int newValue = oldValue + delta;
394+
if (delta > 0 && newValue < oldValue) {
395+
newValue = toScroll.getMaximum();
396+
}
397+
else if (delta < 0 && newValue > oldValue) {
398+
newValue = toScroll.getMinimum();
399+
}
400+
toScroll.setValue(newValue);
401+
}
402+
}
403+
}
227404
e.consume();
228405
}
229406
});

0 commit comments

Comments
 (0)