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.Component;
022: import java.awt.Dimension;
023:
024: import javax.swing.BorderFactory;
025: import javax.swing.JComponent;
026: import javax.swing.JViewport;
027:
028: import com.jeta.forms.gui.common.FormSpecAdapter;
029: import com.jeta.forms.gui.components.ComponentSource;
030: import com.jeta.forms.gui.form.FormComponent;
031: import com.jeta.forms.gui.form.GridComponent;
032: import com.jeta.forms.gui.form.GridView;
033: import com.jeta.open.registry.JETARegistry;
034: import com.jeta.swingbuilder.gui.dnd.DesignerDragSource;
035: import com.jeta.swingbuilder.gui.utils.Units;
036: import com.jgoodies.forms.layout.ColumnSpec;
037: import com.jgoodies.forms.layout.RowSpec;
038:
039: public abstract class Margin extends JComponent implements
040: DesignerDragSource {
041: static final int THUMB_WIDTH = 16;
042: static final int THUMB_HEIGHT = 16;
043:
044: private boolean m_show;
045:
046: protected GridComponent m_gc;
047:
048: protected boolean m_dragging = false;
049:
050: protected JViewport m_viewport;
051: protected FormComponent m_form;
052: protected GridView m_view;
053: protected DesignGridOverlay m_overlay;
054: protected ResizeIndicator m_resize_indicator;
055:
056: private Orientation m_orientation;
057:
058: /**
059: * The object that determines the currently selected component to create
060: * when adding a component to the form.
061: */
062: protected ComponentSource m_compsrc;
063:
064: /**
065: * The current size of the component
066: */
067: protected double m_comp_size;
068:
069: protected String m_units = "PX";
070:
071: protected Units m_unit_converter = Units.getInstance();
072:
073: public Margin(Orientation orientation, FormComponent fc,
074: GridView topview, ComponentSource compSrc,
075: JViewport viewport, boolean show) {
076: m_orientation = orientation;
077: setPaintMargins(show);
078: setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
079: setOpaque(false);
080:
081: m_form = fc;
082: m_view = topview;
083: m_compsrc = compSrc;
084: m_viewport = viewport;
085: m_overlay = (DesignGridOverlay) m_view.getOverlay();
086: assert (m_overlay != null);
087: if (Orientation.VERTICAL.equals(orientation))
088: m_resize_indicator = new ResizeIndicator(m_view,
089: Orientation.HORIZONTAL);
090: else
091: m_resize_indicator = new ResizeIndicator(m_view,
092: Orientation.VERTICAL);
093: }
094:
095: /**
096: * DesignerDragSource implementation
097: */
098: public void cancelDrag() {
099: m_dragging = false;
100: m_overlay.setResizeIndicator(null);
101: m_view.repaint();
102: update();
103: }
104:
105: double convertPoint(int pixels, String units) {
106: if ("DLU".equalsIgnoreCase(units)) {
107: if (Orientation.HORIZONTAL.equals(m_orientation))
108: return m_unit_converter
109: .pixelAsDialogUnitX(pixels, this );
110: else
111: return m_unit_converter
112: .pixelAsDialogUnitY(pixels, this );
113: } else if ("PT".equalsIgnoreCase(units))
114: return m_unit_converter.pixelAsPoint(pixels, this );
115: else if ("IN".equalsIgnoreCase(units))
116: return m_unit_converter.pixelAsInch(pixels, this );
117: else if ("MM".equalsIgnoreCase(units))
118: return m_unit_converter.pixelAsMillimeter(pixels, this );
119: else if ("CM".equalsIgnoreCase(units))
120: return m_unit_converter.pixelAsCentimeter(pixels, this );
121: else
122: return (double) pixels;
123: }
124:
125: public boolean isDragging() {
126: return m_dragging;
127: }
128:
129: boolean isPaintMargin() {
130: return m_show;
131: }
132:
133: public void setPaintMargins(boolean show) {
134: m_show = show;
135: if (show)
136: setPreferredSize(new Dimension(THUMB_WIDTH, THUMB_HEIGHT));
137: else
138: setPreferredSize(new Dimension(THUMB_WIDTH / 2,
139: THUMB_HEIGHT / 2));
140:
141: revalidate();
142: }
143:
144: protected void startDrag() {
145: m_dragging = true;
146: /**
147: * we need to request focus on the overlay so we can get Escape key
148: * events for cancel
149: */
150: m_overlay.requestFocus();
151: JETARegistry.rebind(DesignerDragSource.COMPONENT_ID, this );
152: m_overlay.setResizeIndicator(m_resize_indicator);
153: }
154:
155: abstract void update(GridComponent gc);
156:
157: void update() {
158: update(m_gc);
159: }
160:
161: private String getBeanDelegate(GridComponent gc) {
162: Component comp = gc.getBeanDelegate();
163: if (comp != null)
164: return comp.getClass().getName();
165: return "null";
166: }
167:
168: public static class NewSizeAdapter extends FormSpecAdapter {
169: private double m_newsize;
170: private String m_units;
171:
172: public NewSizeAdapter(ColumnSpec spec, double newsize,
173: String units) {
174: super (spec);
175: m_newsize = newsize;
176: m_units = units;
177: }
178:
179: public NewSizeAdapter(RowSpec spec, double newsize, String units) {
180: super (spec);
181: m_newsize = newsize;
182: m_units = units;
183: }
184:
185: /**
186: * @return the units (integer) (double) PX, PT, DLU IN, MM, CM
187: */
188: public String getConstantUnits() {
189: return m_units;
190: }
191:
192: /**
193: * @return the size.
194: */
195: public double getConstantSize() {
196: return (double) m_newsize;
197: }
198:
199: /**
200: * @return CONSTANT, COMPONENT, BOUNDED
201: */
202: public String getSizeType() {
203: return "CONSTANT";
204: }
205: }
206:
207: }
|