001: /*
002: * Copyright (C) 2005 Jeff Tassin
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library 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 GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018:
019: package com.jeta.swingbuilder.gui.main;
020:
021: import java.awt.Dimension;
022: import java.awt.Point;
023: import java.awt.Rectangle;
024:
025: import javax.swing.JFrame;
026: import javax.swing.JPanel;
027: import javax.swing.JSplitPane;
028: import javax.swing.SwingUtilities;
029:
030: import com.jeta.forms.components.image.ImageComponent;
031: import com.jeta.forms.components.panel.FormPanel;
032: import com.jeta.forms.gui.common.FormUtils;
033: import com.jeta.open.i18n.I18N;
034: import com.jeta.open.registry.JETARegistry;
035: import com.jeta.swingbuilder.common.ComponentNames;
036: import com.jeta.swingbuilder.gui.utils.FormDesignerUtils;
037: import com.jeta.swingbuilder.interfaces.app.ObjectStore;
038: import com.jeta.swingbuilder.resources.Icons;
039: import com.jeta.swingbuilder.store.FrameState;
040:
041: /**
042: * This class is responsible for handling the docking behavior of the properties
043: * window.
044: *
045: * @author Jeff Tassin
046: */
047: class FrameDocker {
048: /**
049: * The main container in the application frame that contains the tool
050: * palette, form editor, and optionally the properties window
051: */
052: private JPanel m_main_panel;
053:
054: /**
055: * The main split pane in the application frame.
056: */
057: private JSplitPane m_split;
058:
059: /**
060: * The properties frame window. Null if docked.
061: */
062: private JFrame m_properties_frame;
063:
064: /**
065: * The properties view
066: */
067: private FormPanel m_properties_view;
068:
069: /**
070: * Panel that contains the form editor and component toolbar
071: */
072: private JPanel m_editor_panel;
073:
074: /**
075: * The last know split pane divider location
076: */
077: private int m_divider_location = 0;
078:
079: public static final String ID_PROPERTIES_FRAME_STATE = "property.frame.bounds";
080:
081: /**
082: * ctor
083: */
084: FrameDocker(JPanel main_panel, JPanel editor_panel,
085: JSplitPane split, FormPanel properties_view) {
086: m_main_panel = main_panel;
087: m_editor_panel = editor_panel;
088: m_split = split;
089: m_properties_view = properties_view;
090: }
091:
092: /**
093: * Docks or undocks properties window window
094: */
095: void dockPropertiesFrame(Rectangle bounds) {
096: if (m_properties_frame == null) {
097: Point frame_org = new Point(0, 0);
098: SwingUtilities.convertPointToScreen(frame_org,
099: m_properties_view);
100:
101: togglePropertiesFrame();
102:
103: m_properties_frame = new JFrame();
104: m_properties_frame.setTitle(I18N
105: .getLocalizedMessage("Form Properties"));
106: javax.swing.ImageIcon icon = FormDesignerUtils
107: .loadImage(Icons.LINKED_FORM_16);
108: if (icon != null)
109: m_properties_frame.setIconImage(icon.getImage());
110:
111: ImageComponent btn = (ImageComponent) m_properties_view
112: .getComponentByName(FormPropertiesNames.ID_DOCK_FRAME);
113: btn.setIcon(FormDesignerUtils
114: .loadImage(Icons.SPLIT_WINDOWS_16));
115:
116: m_properties_frame.getContentPane().add(m_properties_view);
117:
118: if (bounds == null)
119: setPropertyPaneBounds(frame_org.x - 10,
120: frame_org.y - 10,
121: m_properties_view.getWidth() + 20,
122: m_properties_view.getHeight() + 20);
123: else
124: setPropertyPaneBounds(bounds.x, bounds.y, bounds.width,
125: bounds.height);
126:
127: m_properties_frame.setVisible(true);
128: } else {
129: ImageComponent btn = (ImageComponent) m_properties_view
130: .getComponentByName(FormPropertiesNames.ID_DOCK_FRAME);
131: btn.setIcon(FormDesignerUtils.loadImage(Icons.WINDOWS_16));
132:
133: m_properties_frame.getContentPane().remove(
134: m_properties_view);
135: m_properties_frame.dispose();
136: m_properties_frame = null;
137: m_main_panel.remove(m_editor_panel);
138: m_split.add(m_editor_panel);
139: m_split.add(m_properties_view);
140:
141: if (m_divider_location > 0)
142: m_split.setDividerLocation(m_divider_location);
143: else
144: m_split.setDividerLocation(0.7);
145:
146: m_main_panel.add(m_split);
147:
148: m_main_panel.revalidate();
149: m_main_panel.repaint();
150: }
151: }
152:
153: /**
154: * Retrieves the last known frame bounds of the properties frame from the
155: * property store and sets the frame size.
156: */
157: void initializeFrameBounds() {
158: try {
159: ObjectStore os = (ObjectStore) JETARegistry
160: .lookup(ComponentNames.APPLICATION_STATE_STORE);
161: FrameState fstate = (FrameState) os
162: .load(ID_PROPERTIES_FRAME_STATE);
163: if (fstate != null) {
164: if (!fstate.isDocked()) {
165: dockPropertiesFrame(fstate.getBounds());
166: }
167: }
168: } catch (Exception e) {
169: // ignore
170: }
171: }
172:
173: private void setPropertyPaneBounds(int x, int y, int width,
174: int height) {
175: Dimension screensz = java.awt.Toolkit.getDefaultToolkit()
176: .getScreenSize();
177:
178: width = Math.min(width, screensz.width * 8 / 10);
179: height = Math.min(height, screensz.height * 8 / 10);
180:
181: width = Math.max(64, width);
182: height = Math.max(64, height);
183:
184: x = Math.min(screensz.width - width - 50, x);
185: y = Math.min(screensz.height - height - 50, y);
186:
187: x = Math.max(20, x);
188: y = Math.max(20, y);
189:
190: m_properties_frame.setBounds(x, y, width, height);
191: }
192:
193: /**
194: * Saves the frame state to the object store and
195: */
196: void shutDown() {
197: try {
198: ObjectStore os = (ObjectStore) JETARegistry
199: .lookup(ComponentNames.APPLICATION_STATE_STORE);
200:
201: FrameState fstate;
202: if (m_properties_frame == null)
203: fstate = new FrameState();
204: else
205: fstate = new FrameState(m_properties_frame.getBounds());
206:
207: os.store(ID_PROPERTIES_FRAME_STATE, fstate);
208: } catch (Exception e) {
209: // ignore
210: }
211: }
212:
213: /**
214: * Hides or displays properties window window
215: */
216: void togglePropertiesFrame() {
217: if (m_properties_frame == null) {
218: if (m_properties_view.getParent() == m_split) {
219: m_divider_location = m_split.getDividerLocation();
220: m_split.remove(m_properties_view);
221: m_split.remove(m_editor_panel);
222: m_main_panel.removeAll();
223: m_main_panel.add(m_editor_panel);
224: } else {
225: m_main_panel.removeAll();
226: m_split.add(m_editor_panel);
227: m_split.add(m_properties_view);
228: m_main_panel.add(m_split);
229:
230: if (m_divider_location > 0)
231: m_split.setDividerLocation(m_divider_location);
232: else
233: m_split.setDividerLocation(0.7);
234: }
235:
236: m_main_panel.revalidate();
237: m_main_panel.repaint();
238: } else {
239: if (m_properties_frame.isActive()
240: && m_properties_frame.isVisible()) {
241: m_properties_frame.setVisible(false);
242: } else {
243: m_properties_frame.setVisible(true);
244: if (m_properties_frame.getExtendedState() == java.awt.Frame.ICONIFIED)
245: m_properties_frame
246: .setExtendedState(java.awt.Frame.NORMAL);
247: m_properties_frame.toFront();
248: }
249: }
250: }
251:
252: /**
253: * Updates all child components when the look and feel has changed
254: */
255: public void updateUI() {
256: if (m_properties_frame != null)
257: FormUtils.updateLookAndFeel(m_properties_frame);
258: }
259:
260: }
|