RnkO1 : java
Umieszczono: 2012-07-31 15:52:41
Format: Czysty text, HTML, HTML źródło, Plik svg, Plik svg źródło, RTF, LaTeX
W każdym formacie kodowanie wyjściowe to UTF-8

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
import java.awt.*;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.image.BufferedImage;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.JViewport;
import javax.swing.SwingUtilities;
import javax.swing.border.LineBorder;

public class JScrollNavigator extends JPanel implements ComponentListener, AdjustmentListener {

	private JScrollPane jScrollPane;

	private NavBox overBox = new NavBox();

	boolean isAdjusting = false;

	public JScrollNavigator() {

		this.setSize(new Dimension(80, 100));

		setBackground(Color.BLACK);

		setLayout(null);
		add(overBox);

		overBox.setOpaque(false);

		addComponentListener(new ComponentAdapter() {

			public void componentResized(ComponentEvent e) {
				updateOverBox();
			}

		});

		overBox.addComponentListener(this);
	}

	public void setJScrollPane(JScrollPane jScrollPane) {
		this.jScrollPane = jScrollPane;

		Component view = jScrollPane.getViewport().getView();

		if(view != null) {
			view.addComponentListener(this);
			jScrollPane.getViewport().addComponentListener(this);
			jScrollPane.getHorizontalScrollBar().addAdjustmentListener(this);
			jScrollPane.getVerticalScrollBar().addAdjustmentListener(this);
			updateOverBox();
		}

	}

	private void updateOverBox() {
		isAdjusting = true;

		JViewport viewport = this.jScrollPane.getViewport();

		Dimension d = viewport.getViewSize();
		Rectangle vRect = viewport.getViewRect();

		int vWidth = d.width;
		int vHeight = d.height;

		int w = getWidth();
		int h = getHeight();

		float xMult = (float) w / vWidth;
		float yMult = (float) h / vHeight;

		int newX = (int) (vRect.x * xMult);
		int newY = (int) (vRect.y * yMult);
		int newW = (int) (vRect.width * xMult);
		int newH = (int) (vRect.height * yMult);

		overBox.setLocation(newX, newY);
		overBox.setSize(newW, newH);

		isAdjusting = false;

	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			@Override
			public void run() {
				JScrollPane jsp = new JScrollPane();
				JTextPane blueEditorPane = new JTextPane();

				StringBuffer buffer = new StringBuffer();
				for(int i = 0; i < 10; i++) {
					for(int j = 0; j < 4; ++j) {
						buffer.append("test" + i + ", " + j);
					}

					if(i == 5) {
						for(int j = 0; j < 5; ++j) {
							buffer.append("\n");
						}
					}
					buffer.append("\n");
				}
				blueEditorPane.setText(buffer.toString());

				jsp.setViewportView(blueEditorPane);

				JScrollNavigator nav = new JScrollNavigator();
				nav.setJScrollPane(jsp);

				JFrame f = new JFrame();
				f.getContentPane().add(jsp);
				nav.setPreferredSize(new Dimension(100, 100));
				f.getContentPane().add(nav, BorderLayout.WEST);
				f.setSize(300, 200);
				f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				f.setVisible(true);
			}
		});
	}

	@Override
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		Component view = jScrollPane.getViewport().getView();
		BufferedImage img = new BufferedImage(view.getWidth(), view.getHeight(), BufferedImage.TYPE_INT_ARGB);
		Graphics2D g2d = img.createGraphics();
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

		// Paint JScrollPane view to off-screen image and then scale.
		// It is this action that causes the display corruption!
		view.paint(g2d);
		g2d.drawImage(img, 0, 0, null);
		Image scaled = img.getScaledInstance(getWidth(), getHeight(), 0);

		g.drawImage(scaled, 0, 0, null);
	}

	public void componentResized(ComponentEvent e) {
		if(e.getSource() == jScrollPane.getViewport() || e.getSource() == jScrollPane.getViewport().getView()) {
			updateOverBox();
		}
	}

	public void componentMoved(ComponentEvent e) {
		if(e.getSource() == overBox) {
			isAdjusting = true;

			Rectangle r = overBox.getBounds();

			JViewport viewport = this.jScrollPane.getViewport();
			Dimension d = viewport.getViewSize();

			int vWidth = d.width;
			int vHeight = d.height;

			int w = getWidth();
			int h = getHeight();

			float xMult = (float) vWidth / w;
			float yMult = (float) vHeight / h;

			int newX = (int) (r.x * xMult);
			int newY = (int) (r.y * yMult);
			int newW = (int) (r.width * xMult);
			int newH = (int) (r.height * yMult);

			Rectangle newRect = new Rectangle(newX, newY, newW, newH);

			((JComponent) viewport.getView()).scrollRectToVisible(newRect);

			isAdjusting = false;
		}
	}

	public void componentShown(ComponentEvent e) {
	}

	public void componentHidden(ComponentEvent e) {
	}

	public void adjustmentValueChanged(AdjustmentEvent e) {
		if(!isAdjusting) {
			updateOverBox();
		}
	}

	class NavBox extends JPanel {
		boolean dragging = false;

		Point origin = null;

		int originX = -1;

		int originY = -1;

		public NavBox() {
			this.setBorder(new LineBorder(Color.GREEN, 1));

			this.addMouseListener(new MouseAdapter() {

				public void mousePressed(MouseEvent e) {
					origin = SwingUtilities.convertPoint(NavBox.this, e.getPoint(), NavBox.this.getParent());
					originX = NavBox.this.getX();
					originY = NavBox.this.getY();
				}

				public void mouseReleased(MouseEvent e) {
					origin = null;
					originX = -1;
					originY = -1;
				}

			});

			this.addMouseMotionListener(new MouseMotionAdapter() {

				public void mouseDragged(MouseEvent e) {
					NavBox box = NavBox.this;
					Container c = box.getParent();

					int leftBound = -originX;
					int rightBound = c.getWidth() - box.getWidth() - originX;
					int topBound = -originY;
					int bottomBound = c.getHeight() - box.getHeight() - originY;

					Point p = SwingUtilities.convertPoint(box, e.getPoint(), c);

					int xDiff = p.x - origin.x;
					int yDiff = p.y - origin.y;

					if(xDiff < leftBound) {
						xDiff = leftBound;
					}

					if(xDiff > rightBound) {
						xDiff = rightBound;
					}

					if(yDiff < topBound) {
						yDiff = topBound;
					}

					if(yDiff > bottomBound) {
						yDiff = bottomBound;
					}

					box.setLocation(originX + xDiff, originY + yDiff);
				}

			});
		}
	}

}