001: /**
002: * Caption: Zaval Java Resource Editor
003: * $Revision: 0.37 $
004: * $Date: 2002/03/28 9:24:42 $
005: *
006: * @author: Victor Krapivin
007: * @version: 1.3
008: *
009: * Zaval JRC Editor is a visual editor which allows you to manipulate
010: * localization strings for all Java based software with appropriate
011: * support embedded.
012: *
013: * For more info on this product read Zaval Java Resource Editor User's Guide
014: * (It comes within this package).
015: * The latest product version is always available from the product's homepage:
016: * http://www.zaval.org/products/jrc-editor/
017: * and from the SourceForge:
018: * http://sourceforge.net/projects/zaval0002/
019: *
020: * Contacts:
021: * Support : support@zaval.org
022: * Change Requests : change-request@zaval.org
023: * Feedback : feedback@zaval.org
024: * Other : info@zaval.org
025: *
026: * Copyright (C) 2001-2002 Zaval Creative Engineering Group (http://www.zaval.org)
027: *
028: * This program is free software; you can redistribute it and/or
029: * modify it under the terms of the GNU General Public License
030: * (version 2) as published by the Free Software Foundation.
031: *
032: * This program is distributed in the hope that it will be useful,
033: * but WITHOUT ANY WARRANTY; without even the implied warranty of
034: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
035: * GNU General Public License for more details.
036: *
037: * You should have received a copy of the GNU General Public License
038: * along with this program; if not, write to the Free Software
039: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
040: *
041: */package org.zaval.awt;
042:
043: import java.util.*;
044: import java.awt.*;
045:
046: public class ScrollController extends org.zaval.awt.util.Metrics {
047: public static final int SCROLL_SIZE = 16;
048:
049: private int hValue = 0;
050: private int vValue = 0;
051: private ScrollArea area;
052: private ScrollObject sobj;
053: private Dimension pages = new Dimension(0, 0);
054: private Dimension lines = new Dimension(1, 1);
055:
056: public ScrollController() {
057: }
058:
059: public ScrollController(ScrollArea a, ScrollObject o) {
060: setScrollArea(a);
061: setScrollObject(o);
062: }
063:
064: protected int normilize(Scrollbar sb, int prev, int dt) {
065: int v = prev + dt;
066:
067: if (v < 0)
068: dt -= v;
069: else if (v >= sb.getMaximum())
070: dt -= (v - sb.getMaximum());
071:
072: return dt;
073: }
074:
075: protected int next(Scrollbar sb, int prev, int id, int times) {
076: if (sb == null || !sb.isVisible())
077: return 0;
078:
079: int dt = 0;
080: int or = sb.getOrientation();
081: switch (id) {
082: case Event.SCROLL_PAGE_DOWN: {
083: if (or == Scrollbar.HORIZONTAL)
084: dt = getHPageSize(id);
085: else
086: dt = getVPageSize(id);
087: }
088: break;
089: case Event.SCROLL_LINE_UP: {
090: if (or == Scrollbar.HORIZONTAL)
091: dt = -getHLineSize(id);
092: else
093: dt = -getVLineSize(id);
094: }
095: break;
096: case Event.SCROLL_ABSOLUTE: {
097: dt = sb.getValue() - prev;
098: }
099: break;
100: case Event.SCROLL_LINE_DOWN: {
101: if (or == Scrollbar.HORIZONTAL)
102: dt = getHLineSize(id);
103: else
104: dt = getVLineSize(id);
105: }
106: break;
107: case Event.SCROLL_PAGE_UP: {
108: if (or == Scrollbar.HORIZONTAL)
109: dt = -getHPageSize(id);
110: else
111: dt = -getVPageSize(id);
112: }
113: break;
114: }
115:
116: dt *= times;
117: dt = normilize(sb, prev, dt);
118:
119: return dt;
120: }
121:
122: public boolean handle(Event e, int times) {
123: Scrollbar hBar = area.getHBar();
124: Scrollbar vBar = area.getVBar();
125:
126: if (e.target != hBar && e.target != vBar)
127: return false;
128:
129: boolean b = (e.id == Event.SCROLL_PAGE_UP
130: || e.id == Event.SCROLL_PAGE_DOWN
131: || e.id == Event.SCROLL_ABSOLUTE
132: || e.id == Event.SCROLL_LINE_UP || e.id == Event.SCROLL_LINE_DOWN);
133:
134: if (!b)
135: return false;
136:
137: return handle((Scrollbar) e.target, e.id, times);
138: }
139:
140: protected boolean handle(Scrollbar bar, int id, int times) {
141: if (bar == null || !bar.isVisible())
142: return false;
143: Scrollbar hBar = area.getHBar();
144: Scrollbar vBar = area.getVBar();
145:
146: int dx = 0, dy = 0;
147: if (bar == hBar) {
148: dx = next(hBar, hValue, id, times);
149: hValue += dx;
150: if (dx != 0)
151: hBar.setValue(hValue);
152: } else if (bar == vBar) {
153: dy = next(vBar, vValue, id, times);
154: vValue += dy;
155: if (dy != 0)
156: vBar.setValue(vValue);
157: }
158:
159: if (dx != 0 || dy != 0) {
160: ScrollObject sobj = getScrollObject();
161: Point pos = sobj.getSOLocation();
162: sobj.setSOLocation(pos.x - dx, pos.y - dy);
163: }
164:
165: return true;
166: }
167:
168: public void clear() {
169: hValue = 0;
170: vValue = 0;
171: }
172:
173: public int getMaxHorScroll() {
174: Scrollbar hBar = area.getHBar();
175: Scrollbar vBar = area.getVBar();
176: if (hBar == null)
177: return -1;
178: Dimension r = sobj.getSOSize();
179: Dimension d = area.getSASize();
180: int wx = d.width;
181: int wy = d.height;
182: boolean needVBar = false;
183: boolean b1 = (r.width > wx);
184:
185: if (vBar != null)
186: if (r.height > wy || (b1 && r.height > (wy - SCROLL_SIZE))) {
187: needVBar = true;
188: wx -= SCROLL_SIZE;
189: }
190:
191: boolean b2 = (needVBar && r.width > wx);
192: if (b1 || b2) {
193: hValue = 0;
194: int max = (r.width - wx);
195: return max + 1;
196: }
197:
198: return -1;
199: }
200:
201: public int getMaxVerScroll() {
202: Scrollbar hBar = area.getHBar();
203: Scrollbar vBar = area.getVBar();
204: if (vBar == null)
205: return -1;
206: Dimension r = sobj.getSOSize();
207: Dimension d = area.getSASize();
208: int wx = d.width;
209: int wy = d.height;
210: boolean needHBar = false;
211:
212: boolean b1 = (r.height > wy);
213: if (hBar != null)
214: if (r.width > wx || (b1 && r.width > (wx - SCROLL_SIZE))) {
215: wy -= SCROLL_SIZE;
216: needHBar = true;
217: }
218:
219: boolean b2 = (needHBar && r.height > wy);
220: if (b1 || b2) {
221: vValue = 0;
222: int max = (r.height - wy);
223: return max + 1;
224: }
225:
226: return -1;
227: }
228:
229: public ScrollArea getScrollArea() {
230: return area;
231: }
232:
233: public ScrollObject getScrollObject() {
234: return sobj;
235: }
236:
237: public void setScrollArea(ScrollArea a) {
238: area = a;
239: invalidate();
240: }
241:
242: public void setScrollObject(ScrollObject o) {
243: sobj = o;
244: invalidate();
245: }
246:
247: public void recalc() {
248: clear();
249: Scrollbar hBar = area.getHBar();
250: Scrollbar vBar = area.getVBar();
251:
252: Dimension d = area.getSASize();
253: pages.width = d.width;
254: pages.height = d.height;
255: if (hBar != null && hBar.isVisible()) {
256: pages.width -= SCROLL_SIZE;
257: lines.width = pages.width / 4;
258: }
259: if (vBar != null && vBar.isVisible()) {
260: pages.height -= SCROLL_SIZE;
261: lines.height = pages.height / 4;
262: }
263: }
264:
265: public void setV(int id, int times) {
266: clear();
267: handle(area.getVBar(), id, times);
268: }
269:
270: public void setH(int id, int times) {
271: clear();
272: handle(area.getHBar(), id, times);
273: }
274:
275: public int getVPageSize(int id) {
276: return pages.height;
277: }
278:
279: public int getHPageSize(int id) {
280: return pages.width;
281: }
282:
283: public int getHLineSize(int id) {
284: return lines.width;
285: }
286:
287: public int getVLineSize(int id) {
288: return lines.height;
289: }
290:
291: public static Dimension calcPreferredSize(Container parent) {
292: Component[] c = parent.getComponents();
293: int maxx = 0;
294: int maxy = 0;
295:
296: for (int i = 0; i < c.length; i++) {
297: if (!c[i].isVisible())
298: continue;
299: Rectangle r = c[i].bounds();
300: int mx = r.x + r.width;
301: int my = r.y + r.height;
302: if (maxx < mx)
303: maxx = mx;
304: if (maxy < my)
305: maxy = my;
306: }
307: return new Dimension(maxx + 5, maxy + 5);
308: }
309: }
|