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.editor;
020:
021: import java.awt.Graphics;
022: import java.awt.Insets;
023: import java.awt.Point;
024: import java.awt.Rectangle;
025: import java.awt.event.MouseAdapter;
026: import java.awt.event.MouseEvent;
027: import java.awt.event.MouseMotionAdapter;
028:
029: import javax.swing.ImageIcon;
030: import javax.swing.JOptionPane;
031: import javax.swing.JViewport;
032: import javax.swing.UIManager;
033:
034: import com.jeta.forms.gui.common.FormSpecAdapter;
035: import com.jeta.forms.gui.common.FormUtils;
036: import com.jeta.forms.gui.components.ComponentSource;
037: import com.jeta.forms.gui.form.FormComponent;
038: import com.jeta.forms.gui.form.GridComponent;
039: import com.jeta.forms.gui.form.GridView;
040: import com.jeta.open.i18n.I18N;
041: import com.jeta.open.registry.JETARegistry;
042: import com.jeta.open.resources.ResourceLoader;
043: import com.jeta.swingbuilder.gui.commands.CommandUtils;
044: import com.jeta.swingbuilder.gui.commands.EditRowSpecCommand;
045: import com.jeta.swingbuilder.gui.dnd.DesignerDragSource;
046: import com.jeta.swingbuilder.gui.project.UserPreferencesNames;
047: import com.jeta.swingbuilder.interfaces.userprops.TSUserPropertiesUtils;
048: import com.jgoodies.forms.layout.RowSpec;
049:
050: public class RowMargin extends Margin {
051: /**
052: * The x-position of the middle of the drag thumb
053: */
054: private int m_ypos;
055:
056: private int m_drag_y_diff;
057:
058: /**
059: * Icon for resize thumb
060: */
061: private static ImageIcon m_row_thumb;
062:
063: static {
064: try {
065: ResourceLoader loader = (ResourceLoader) JETARegistry
066: .lookup(ResourceLoader.COMPONENT_ID);
067: m_row_thumb = loader
068: .loadImage("jeta.resources/images/forms/thumb_horizontal.png");
069: } catch (Exception e) {
070: e.printStackTrace();
071: }
072: }
073:
074: /**
075: * ctor
076: *
077: * @param view
078: * the top level view that will render the resize indicator
079: * @param viewport
080: * the viewport that contains the top level view. Needed when the
081: * view is scrolled right or down
082: */
083: public RowMargin(FormComponent form, GridView topview,
084: ComponentSource compSrc, JViewport viewport, boolean show) {
085: super (Orientation.VERTICAL, form, topview, compSrc, viewport,
086: show);
087: addMouseListener(new MouseHandler());
088: addMouseMotionListener(new MouseMotionHandler());
089: }
090:
091: protected void paintComponent(Graphics g) {
092: if (isPaintMargin()) {
093: Rectangle clip = g.getClipBounds();
094: g.setColor(UIManager.getColor("control"));
095: g.fillRect(clip.x, clip.y, clip.width, clip.height);
096:
097: if (m_gc != null) {
098: java.awt.Graphics2D g2 = (java.awt.Graphics2D) g;
099: g2.drawImage(m_row_thumb.getImage(), 0, m_ypos
100: - THUMB_HEIGHT / 2, this );
101: }
102: }
103: }
104:
105: /**
106: * Changes the thumb position to the specified grid component
107: */
108: void update(GridComponent gc) {
109: DesignerDragSource dds = (DesignerDragSource) JETARegistry
110: .lookup(DesignerDragSource.COMPONENT_ID);
111: if (dds != null && dds != this && dds.isDragging()) {
112: return;
113: }
114:
115: if (isPaintMargin() && !m_dragging
116: && m_compsrc.isSelectionTool()) {
117: m_gc = gc;
118: if (gc != null) {
119: GridView parentview = gc.getParentView();
120: if (parentview == m_view) {
121: m_gc = null;
122: } else {
123: if (parentview != null) {
124: Insets insets = parentview.getInsets();
125: int row_offset = gc.getRow() + gc.getRowSpan()
126: - 1;
127: Point pt = javax.swing.SwingUtilities
128: .convertPoint(
129: parentview,
130: 0,
131: parentview
132: .getRowOrgY(row_offset)
133: + parentview
134: .getRowHeight(row_offset)
135: + insets.top, this );
136: m_ypos = pt.y;
137: }
138: }
139: }
140: repaint();
141: }
142: }
143:
144: private class MouseHandler extends MouseAdapter {
145: public void mousePressed(MouseEvent e) {
146: int y = e.getY();
147: if ((y >= (m_ypos - THUMB_WIDTH / 2))
148: && (y <= (m_ypos + THUMB_WIDTH)) && isPaintMargin()) {
149: if (m_gc != null) {
150: startDrag();
151: m_drag_y_diff = e.getY() - m_ypos;
152: m_resize_indicator.setPosition(m_ypos);
153: int row = m_gc.getRow() + m_gc.getRowSpan() - 1;
154: GridView view = m_gc.getParentView();
155: FormSpecAdapter adapter = new FormSpecAdapter(view
156: .getRowSpec(row));
157: if (adapter.isComponentSize()) {
158: m_units = TSUserPropertiesUtils
159: .getString(
160: UserPreferencesNames.ID_DEFAULT_RESIZE_UNITS,
161: "PX");
162: if (!FormUtils.isValidUnits(m_units))
163: m_units = "PX";
164: } else {
165: m_units = adapter.getConstantUnits();
166: if (m_units == null)
167: m_units = "PX";
168: }
169: }
170: } else {
171: m_view.deselectAll();
172: m_form.setSelected(true);
173: m_gc = null;
174: }
175: }
176:
177: public void mouseReleased(MouseEvent e) {
178: if (m_dragging && m_gc != null) {
179: if (m_comp_size > 0) {
180: int row = m_gc.getRow() + m_gc.getRowSpan() - 1;
181: GridView view = m_gc.getParentView();
182: RowSpec oldspec = view.getRowSpec(row);
183: FormEditor editor = FormEditor.getEditor(view);
184:
185: if (editor != null) {
186: NewSizeAdapter adapter = new NewSizeAdapter(
187: oldspec, m_comp_size, m_units);
188: if (!adapter.isResizeGrow()
189: && !adapter.isBoundedSize()) {
190: String newspec = FormUtils
191: .toEncodedString(adapter);
192: EditRowSpecCommand cmd = new EditRowSpecCommand(
193: view.getParentForm(), row,
194: new RowSpec(newspec), oldspec);
195: CommandUtils.invoke(cmd, editor);
196: } else {
197: String msg = null;
198: if (adapter.isBoundedSize()) {
199: msg = I18N
200: .getLocalizedMessage("The row size is set to bounded.\nYou must manually set the size in the row specification window.");
201: } else if (adapter.isResizeGrow()) {
202: msg = I18N
203: .getLocalizedMessage("The row resize behavior is set to grow.\nYou must manually set the size in the row specification window.");
204: } else {
205: msg = I18N
206: .getLocalizedMessage("You must manually set the size in the row specification window.");
207: }
208:
209: String title = I18N
210: .getLocalizedMessage("Error");
211: JOptionPane.showMessageDialog(m_view, msg,
212: title, JOptionPane.ERROR_MESSAGE);
213: }
214:
215: } else {
216: assert (false);
217: }
218: }
219:
220: javax.swing.SwingUtilities.invokeLater(new Runnable() {
221: public void run() {
222: m_overlay.setResizeIndicator(null);
223: m_overlay.repaint();
224: m_dragging = false;
225: update(m_gc);
226: }
227: });
228: }
229: }
230: }
231:
232: private class MouseMotionHandler extends MouseMotionAdapter {
233: public void mouseDragged(MouseEvent e) {
234: if (m_gc != null && m_dragging) {
235: int y = e.getY() - m_drag_y_diff;
236:
237: GridView parentview = m_gc.getParentView();
238: Insets insets = parentview.getInsets();
239:
240: Point pt = javax.swing.SwingUtilities.convertPoint(
241: RowMargin.this , 0, y, parentview);
242: Point offsetpt = m_viewport.getViewPosition();
243:
244: // pt.y is the new height of the grid component
245: int pixels = pt.y
246: - parentview.getRowOrgY(m_gc.getRow()
247: + m_gc.getRowSpan() - 1) - insets.top;
248: m_comp_size = convertPoint(pixels, m_units);
249: m_ypos = y;
250: repaint();
251: m_resize_indicator.setPosition(m_ypos + offsetpt.y);
252: m_resize_indicator.setSize(m_comp_size, m_units,
253: offsetpt.x);
254: m_overlay.repaint();
255: }
256: }
257: }
258:
259: }
|