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