001: /*
002: This file is part of BORG.
003:
004: BORG is free software; you can redistribute it and/or modify
005: it under the terms of the GNU General Public License as published by
006: the Free Software Foundation; either version 2 of the License, or
007: (at your option) any later version.
008:
009: BORG is distributed in the hope that it will be useful,
010: but WITHOUT ANY WARRANTY; without even the implied warranty of
011: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: GNU General Public License for more details.
013:
014: You should have received a copy of the GNU General Public License
015: along with BORG; if not, write to the Free Software
016: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017:
018: Copyright 2003 by Mike Berger
019: */
020: /*
021: * View.java
022: *
023: * Created on May 23, 2003, 11:06 AM
024: */
025:
026: package net.sf.borg.ui;
027:
028: import java.awt.Dimension;
029: import java.awt.Frame;
030: import java.awt.Image;
031: import java.awt.Rectangle;
032: import java.awt.Toolkit;
033: import java.awt.event.ActionEvent;
034: import java.awt.event.ActionListener;
035: import java.awt.event.KeyEvent;
036:
037: import javax.swing.JButton;
038: import javax.swing.JComponent;
039: import javax.swing.KeyStroke;
040:
041: import net.sf.borg.common.PrefName;
042: import net.sf.borg.common.Prefs;
043: import net.sf.borg.model.Model;
044:
045: /**
046: * $Id: View.java 379 2005-09-10 07:12:16Z membar $
047: * @author MBERGER
048: */
049:
050: // Views show data from a model and respond to refresh
051: // cand destroy allbacks from Models
052: public abstract class View extends javax.swing.JFrame implements
053: Model.Listener {
054: static Image image = Toolkit.getDefaultToolkit().getImage(
055: View.class.getResource("/resource/borg32x32.jpg"));
056:
057: private PrefName prefName_ = null;
058:
059: private void initialize() {
060: setIconImage(image);
061: }
062:
063: public abstract void refresh();
064:
065: public abstract void destroy();
066:
067: public void remove() {
068: destroy();
069: }
070:
071: public View() {
072: initialize();
073: }
074:
075: private void recordSize(boolean resize) {
076: String s = Prefs.getPref(prefName_);
077: ViewSize vsnew = ViewSize.fromString(s);
078:
079: if (!resize) {
080: // for a move, ignore the event if the size changes - this is
081: // part of a maximize or minimize and the resize will follow
082: if (vsnew.getHeight() != this .getBounds().height
083: || vsnew.getWidth() != this .getBounds().width)
084: return;
085:
086: // if x or y < 0, then this is likely the move before a maximize
087: // so ignore it
088: if (this .getBounds().x < 0 || this .getBounds().y < 0)
089: return;
090:
091: vsnew.setX(this .getBounds().x);
092: vsnew.setY(this .getBounds().y);
093: vsnew.setWidth(this .getBounds().width);
094: vsnew.setHeight(this .getBounds().height);
095: } else if (this .getExtendedState() == Frame.MAXIMIZED_BOTH) {
096: vsnew.setMaximized(true);
097: } else {
098: // only reset bounds if we are not maximized
099: vsnew.setMaximized(false);
100: vsnew.setX(this .getBounds().x);
101: vsnew.setY(this .getBounds().y);
102: vsnew.setWidth(this .getBounds().width);
103: vsnew.setHeight(this .getBounds().height);
104:
105: }
106:
107: //System.out.println(vsnew.toString());
108: Prefs.putPref(this .prefName_, vsnew.toString());
109:
110: }
111:
112: // function to call to register a view with the model
113: protected void addModel(Model m) {
114: m.addListener(this );
115: }
116:
117: // called from the subclass to cause the View to use preferences to
118: // persist a View's size and locaiton if the user resizes it
119: public void manageMySize(PrefName pname) {
120: prefName_ = pname;
121:
122: // set the initial size
123: String s = Prefs.getPref(prefName_);
124: ViewSize vs = ViewSize.fromString(s);
125:
126: if (vs.getX() != -1) {
127: setBounds(new Rectangle(vs.getX(), vs.getY(),
128: vs.getWidth(), vs.getHeight()));
129: } else if (vs.getWidth() != -1) {
130: setSize(new Dimension(vs.getWidth(), vs.getHeight()));
131: }
132: if (vs.isMaximized()) {
133: setExtendedState(Frame.MAXIMIZED_BOTH);
134: }
135: validate();
136:
137: // add listeners to record any changes
138: this
139: .addComponentListener(new java.awt.event.ComponentAdapter() {
140: public void componentResized(
141: java.awt.event.ComponentEvent e) {
142: //System.out.println("resize");
143: recordSize(true);
144: }
145:
146: public void componentMoved(
147: java.awt.event.ComponentEvent e) {
148: //System.out.println("move");
149: recordSize(false);
150: }
151: });
152: }
153:
154: // protected //
155: protected void setDismissButton(final JButton bn) {
156: getLayeredPane().registerKeyboardAction(new ActionListener() {
157: public final void actionPerformed(ActionEvent e) {
158: bn.getActionListeners()[0].actionPerformed(e);
159: }
160: }, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
161: JComponent.WHEN_IN_FOCUSED_WINDOW);
162: }
163: }
|