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.awt.*;
044: import java.util.*;
045:
046: public class SimpleScrollPanel extends Panel implements LayoutManager {
047: private boolean isHor = true;
048: private boolean isVer = true;
049: private Scrollbar hor;
050: private Scrollbar ver;
051: private Component comp;
052: private Panel panel;
053: private Dimension auf = null;
054:
055: public static final int IS_NEED_SCROLL = 646478;
056:
057: public Component getScrollableObject() {
058: return comp;
059: }
060:
061: public SimpleScrollPanel(Component c) {
062: comp = c;
063: hor = new Scrollbar(Scrollbar.HORIZONTAL);
064: ver = new Scrollbar(Scrollbar.VERTICAL);
065: setLayout(this ); // new BorderLayout(0,0));
066:
067: panel = new Panel();
068: panel.setLayout(null);
069:
070: panel.add(comp);
071: ver.hide();
072: hor.hide();
073:
074: add(panel);
075: add(hor);
076: add(ver);
077:
078: setBackground(getBackground());
079: hor.setBackground(getBackground());
080: ver.setBackground(getBackground());
081: panel.setBackground(getBackground());
082: }
083:
084: public void setBackground(Color c) {
085: super .setBackground(c);
086: hor.setBackground(c);
087: ver.setBackground(c);
088: panel.setBackground(c);
089: }
090:
091: private static final int[] table = { Event.SCROLL_ABSOLUTE,
092: Event.SCROLL_LINE_DOWN, Event.SCROLL_LINE_UP,
093: Event.SCROLL_PAGE_DOWN, Event.SCROLL_PAGE_UP };
094:
095: private boolean drop(Event e) {
096: if (!(e.target instanceof Scrollbar))
097: return true;
098: for (int i = 0; i < table.length; ++i)
099: if (e.id == table[i])
100: return false;
101: return true;
102: }
103:
104: public boolean handleEvent(Event evt) {
105: if (drop(evt))
106: return super .handleEvent(evt);
107: if (evt.target == ver) {
108: comp.move(comp.bounds().x, -ver.getValue());
109: checkValid();
110: return true;
111: }
112:
113: if (evt.target == hor) {
114: comp.move(-hor.getValue(), comp.bounds().y);
115: checkValid();
116: return true;
117: }
118: return super .handleEvent(evt);
119: }
120:
121: private void checkValid() {
122: if (!hor.isEnabled()) {
123: hor.hide();
124: hor.enable();
125: ((Component) this ).invalidate();
126: validate();
127: }
128: if (!ver.isEnabled()) {
129: ver.hide();
130: ver.enable();
131: ((Component) this ).invalidate();
132: validate();
133: }
134: comp.repaint();
135: }
136:
137: public Point location(int xx, int yy) {
138: return new Point(0, 0);
139: }
140:
141: public void addLayoutComponent(String name, Component comp) {
142: }
143:
144: public void removeLayoutComponent(Component comp) {
145: }
146:
147: public Dimension preferredLayoutSize(Container parent) {
148: Dimension zet = comp.preferredSize();
149: // return zet;
150: if (zet.width == 0 || zet.height == 0)
151: zet = comp.size();
152: zet.width += ver.preferredSize().width;
153: zet.height += hor.preferredSize().height;
154: if (auf == null)
155: return zet;
156: else
157: return auf;
158: // return oops(auf,zet);
159: }
160:
161: public Dimension minimumLayoutSize(Container parent) {
162: Dimension zet = comp.minimumSize();
163: // return zet;
164: if (zet.width == 0 || zet.height == 0)
165: zet = comp.size();
166: zet.width += ver.preferredSize().width;
167: zet.height += hor.preferredSize().height;
168: if (auf == null)
169: return zet;
170: return oops(auf, zet);
171: }
172:
173: private Dimension oops(Dimension a, Dimension b) // min(a,b)
174: {
175: int w = Math.min(a.width, b.width);
176: int h = Math.min(a.height, b.height);
177: return new Dimension(w, h);
178: }
179:
180: public void layoutContainer(Container parent) {
181: Dimension x = comp.preferredSize();
182: if (x.width == 0 || x.height == 0)
183: x = comp.size();
184: comp.resize(x.width, x.height);
185: Rectangle r = comp.bounds();
186: if (r.x < 0 || r.y < 0)
187: comp.move(0, 0);
188: checkForSVH();
189: }
190:
191: private Component get0(Container c) {
192: return c.getComponent(0);
193: }
194:
195: public void setMaxSize(Dimension auf) {
196: this .auf = auf;
197: }
198:
199: private void checkForSVH() {
200: Dimension x = comp.preferredSize();
201: if (x.width == 0 || x.height == 0)
202: x = comp.size();
203: Rectangle r = bounds();
204: int hor_h = hor.preferredSize().height;
205: int ver_w = ver.preferredSize().width;
206: int wx = r.width;
207: int wy = r.height;
208:
209: boolean seth = false;
210: boolean setv = false;
211:
212: seth = (x.width > wx)
213: || ((x.height > wy) && (x.width > wx - ver_w));
214: setv = (x.height > wy)
215: || ((x.width > wx) && (x.height > wy - hor_h));
216:
217: if (!seth) {
218: if (hor.isVisible())
219: hor.hide();
220: comp.move(0, 0);
221: } else {
222: hor.move(0, r.height - hor_h);
223: hor.resize(r.width - (setv ? ver_w : 0), hor_h);
224: hor.show();
225: wy -= hor_h;
226: int newh = x.width - wx + (x.height > wy ? ver_w : 0);
227: hor.setValues(0, wx - (setv ? ver_w : 0), 0, x.width);
228: hor.setPageIncrement(wx / 2);
229: }
230:
231: if (!setv) {
232: if (ver.isVisible())
233: ver.hide();
234: comp.move(0, 0);
235: } else {
236: ver.move(r.width - ver_w, 0);
237: ver.resize(ver_w, r.height - (seth ? hor_h : 0));
238: ver.show();
239: wx -= ver_w;
240: int newh = x.height - wy; // hor_h == ver_w
241: ver.setValues(0, wy, 0, x.height);
242: ver.setPageIncrement(wy / 2);
243: }
244: panel.move(0, 0);
245: panel.resize(wx, wy);
246: if (x.width < wx) {
247: x.width = wx; // To fit it horizontally
248: comp.resize(x.width, x.height);
249: }
250: }
251:
252: public Scrollbar getVScrollbar() {
253: return ver;
254: }
255:
256: public Scrollbar getHScrollbar() {
257: return hor;
258: }
259:
260: public void scroll(int newX, int newY) {
261: comp.move(-newX, -newY);
262: hor.setValue(newX);
263: ver.setValue(newY);
264: }
265: }
|